2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.common.scripts
25 import org.apache.commons.lang3.StringUtils
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.json.JSONArray
28 import org.json.JSONObject
29 import org.onap.logging.ref.slf4j.ONAPLogConstants
30 import org.onap.so.bpmn.core.UrnPropertiesReader
31 import org.onap.so.bpmn.core.json.JsonUtils
32 import org.onap.so.client.HttpClient
33 import org.onap.so.client.HttpClientFactory
34 import org.onap.so.logger.ErrorCode
35 import org.onap.so.logger.MessageEnum
37 import org.slf4j.Logger
38 import org.slf4j.LoggerFactory
39 import org.onap.so.utils.TargetEntity
40 import org.springframework.web.util.UriUtils
42 import javax.ws.rs.core.MediaType
43 import javax.ws.rs.core.Response
46 * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
50 class CatalogDbUtils {
51 private static final Logger logger = LoggerFactory.getLogger( CatalogDbUtils.class);
53 private HttpClientFactory httpClientFactory
54 private MsoUtils msoUtils
55 private JsonUtils jsonUtils
56 static private String defaultDbAdapterVersion = "v2"
58 CatalogDbUtils(HttpClientFactory httpClientFactory, MsoUtils msoUtils, JsonUtils jsonUtils) {
59 this.httpClientFactory = httpClientFactory
60 this.msoUtils = msoUtils
61 this.jsonUtils = jsonUtils
64 public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid, String catalogUtilsVersion) {
65 JSONArray vnfsList = null
66 String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
68 logger.debug("ENDPOINT: " + endPoint)
69 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
71 if (catalogDbResponse != null) {
72 if (!catalogUtilsVersion.equals("v1")) {
73 JSONObject responseJson = new JSONObject(catalogDbResponse)
74 vnfsList = responseJson.getJSONArray("serviceVnfs")
77 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
83 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
84 "Exception in Querying Catalog DB", "BPMN",
85 ErrorCode.UnknownError.getValue(), e.message);
92 public JSONObject getServiceResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
93 JSONObject resources = null
94 String endPoint = "/serviceResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
96 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
98 if (catalogDbResponse != null) {
99 if (!catalogUtilsVersion.equals("v1")) {
100 resources = new JSONObject(catalogDbResponse)
103 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
107 catch (Exception e) {
108 msoUtils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
115 public String getServiceResourcesByServiceModelInvariantUuidString(DelegateExecution execution, String serviceModelInvariantUuid) {
116 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
118 return getResponseFromCatalogDb(execution, endPoint)
120 catch (Exception e) {
121 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
122 "Exception in Querying Catalog DB", "BPMN",
123 ErrorCode.UnknownError.getValue(), e.message);
128 public JSONObject getServiceResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
129 JSONObject resources = null
130 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
132 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
134 if (catalogDbResponse != null) {
135 if (!catalogUtilsVersion.equals("v1")) {
136 resources = new JSONObject(catalogDbResponse)
139 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
144 catch (Exception e) {
145 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
146 "Exception in Querying Catalog DB", "BPMN",
147 ErrorCode.UnknownError.getValue(), e.message);
154 public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
155 JSONObject resources = null
156 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
158 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
160 if (catalogDbResponse != null) {
161 if (!catalogUtilsVersion.equals("v1")) {
162 resources = new JSONObject(catalogDbResponse)
165 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
170 catch (Exception e) {
171 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
172 "Exception in Querying Catalog DB", "BPMN",
173 ErrorCode.UnknownError.getValue(), e.message);
180 private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
181 JSONArray modelInfos = null
183 logger.debug("parseNetworksJson - catalogUtilsVersion is " + catalogUtilsVersion)
185 // Create array of jsons
187 JSONObject responseJson = new JSONObject(catalogDbResponse)
188 JSONArray networks = responseJson.getJSONArray(arrayName)
189 modelInfos = new JSONArray()
191 for (int i = 0; i < networks.length(); i++) {
193 JSONObject network = networks.getJSONObject(i)
194 JSONObject modelJson = new JSONObject()
195 JSONObject modelInfo = buildModelInfo("network", network, catalogUtilsVersion)
196 modelJson.put("modelInfo", modelInfo)
197 String networkType = jsonUtils.getJsonValueForKey(network, "networkType")
198 modelJson.put("networkType", networkType)
200 switch (catalogUtilsVersion) {
204 String toscaNodeType = jsonUtils.getJsonValueForKey(network, "toscaNodeType")
205 modelJson.put("toscaNodeType", toscaNodeType)
206 String networkTechnology = jsonUtils.getJsonValueForKey(network, "networkTechnology")
207 modelJson.put("networkTechnology", networkTechnology)
208 String networkRole = jsonUtils.getJsonValueForKey(network, "networkRole")
209 modelJson.put("networkRole", networkRole)
210 String networkScope = jsonUtils.getJsonValueForKey(network, "networkScope")
211 modelJson.put("networkScope", networkScope)
214 modelInfos.put(modelJson)
217 String modelInfosString = modelInfos.toString()
218 logger.debug("Returning networks JSON: " + modelInfosString)
220 } catch (Exception e) {
221 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
222 "Exception in parsing Catalog DB Response", "BPMN",
223 ErrorCode.UnknownError.getValue(), e.message);
229 private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
230 JSONArray modelInfos = null
232 logger.debug("parseVnfsJson - catalogUtilsVersion is " + catalogUtilsVersion)
235 // Create array of jsons
237 JSONObject responseJson = new JSONObject(catalogDbResponse)
238 JSONArray vnfs = responseJson.getJSONArray(arrayName)
239 modelInfos = new JSONArray()
241 for (int i = 0; i < vnfs.length(); i++) {
242 JSONObject vnf = vnfs.getJSONObject(i)
244 logger.debug(vnf.toString(2))
245 JSONObject modelInfo = buildModelInfo("vnf", vnf, catalogUtilsVersion)
246 JSONObject modelJson = new JSONObject()
247 modelJson.put("modelInfo", modelInfo)
248 switch(catalogUtilsVersion) {
252 String toscaNodeType = jsonUtils.getJsonValueForKey(vnf, "toscaNodeType")
253 modelJson.put("toscaNodeType", toscaNodeType)
254 String nfType = jsonUtils.getJsonValueForKey(vnf, "nfType")
255 modelJson.put("nfType", nfType)
256 String nfRole = jsonUtils.getJsonValueForKey(vnf, "nfRole")
257 modelJson.put("nfRole", nfRole)
258 String nfCode = jsonUtils.getJsonValueForKey(vnf, "nfCode")
259 modelJson.put("nfNamingCode", nfCode)
260 String nfFunction = jsonUtils.getJsonValueForKey(vnf, "nfFunction")
261 modelJson.put("nfFunction", nfFunction)
262 String multiStageDesign = jsonUtils.getJsonValueForKey(vnf, "multiStageDesign")
263 modelJson.put("multiStageDesign", multiStageDesign)
267 JSONArray vfModules = null
269 vfModules = vnf.getJSONArray("vfModules")
270 } catch (Exception e)
272 logger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
275 if (vfModules != null) {
276 JSONArray vfModuleInfo = new JSONArray()
277 for (int j = 0; j < vfModules.length(); j++) {
278 JSONObject vfModule = vfModules.getJSONObject(j)
279 JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule, catalogUtilsVersion)
280 JSONObject vfModuleModelJson = new JSONObject()
281 vfModuleModelJson.put("modelInfo", vfModuleModelInfo)
282 String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
283 vfModuleModelJson.put("vfModuleType", vfModuleType)
284 vfModuleModelJson.put("isBase", jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase"))
285 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
286 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
287 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
288 vfModuleModelJson.put("initialCount", initialCount.intValue())
289 vfModuleInfo.put(vfModuleModelJson)
291 modelJson.put("vfModules", vfModuleInfo)
293 modelInfos.put(modelJson)
296 String modelInfosString = modelInfos.toString()
297 logger.debug("Returning vnfs JSON: " + modelInfosString)
299 } catch (Exception e) {
300 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
301 "Exception in parsing Catalog DB Response", "BPMN",
302 ErrorCode.UnknownError.getValue(), e.message);
308 private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
309 JSONArray modelInfos = null
311 logger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
314 // Create array of jsons
316 JSONObject responseJson = new JSONObject(catalogDbResponse)
317 JSONArray allottedResources = responseJson.getJSONArray(arrayName)
318 modelInfos = new JSONArray()
320 for (int i = 0; i < allottedResources.length(); i++) {
321 JSONObject allottedResource = allottedResources.getJSONObject(i)
322 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource, catalogUtilsVersion)
323 JSONObject modelJson = new JSONObject()
324 modelJson.put("modelInfo", modelInfo)
325 switch(catalogUtilsVersion) {
329 String toscaNodeType = jsonUtils.getJsonValueForKey(allottedResource, "toscaNodeType")
330 modelJson.put("toscaNodeType", toscaNodeType)
331 String nfType = jsonUtils.getJsonValueForKey(allottedResource, "nfType")
332 modelJson.put("nfType", nfType)
333 String nfRole = jsonUtils.getJsonValueForKey(allottedResource, "nfRole")
334 modelJson.put("nfRole", nfRole)
335 String nfCode = jsonUtils.getJsonValueForKey(allottedResource, "nfCode")
336 modelJson.put("nfNamingCode", nfCode)
337 String nfFunction = jsonUtils.getJsonValueForKey(allottedResource, "nfFunction")
338 modelJson.put("nfFunction", nfFunction)
339 String providingServiceModelName = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelName")
340 modelJson.put("providingServiceModelName", providingServiceModelName)
341 String providingServiceModelUuid = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelUuid")
342 modelJson.put("providingServiceModelUuid", providingServiceModelUuid)
347 modelInfos.put(modelJson)
350 String modelInfosString = modelInfos.toString()
351 logger.debug("Returning allottedResources JSON: " + modelInfosString)
353 } catch (Exception e) {
354 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
355 "Exception in parsing Catalog DB Response", "BPMN",
356 ErrorCode.UnknownError.getValue(), e.message);
362 private JSONObject parseServiceResourcesJson (String catalogDbResponse, String catalogUtilsVersion) {
363 JSONObject serviceResources = new JSONObject()
364 JSONObject serviceResourcesObject = new JSONObject()
367 // Create array of jsons
369 JSONObject responseJson = new JSONObject(catalogDbResponse)
370 JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
371 JSONObject modelInfo = buildModelInfo("", serviceResourcesRoot, catalogUtilsVersion)
372 serviceResources.put("modelInfo", modelInfo)
373 JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "serviceVnfs", catalogUtilsVersion)
374 serviceResources.put("serviceVnfs", vnfsArray)
375 JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "serviceNetworks", catalogUtilsVersion)
376 serviceResources.put("serviceNetworks", networksArray)
377 JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "serviceAllottedResources", catalogUtilsVersion)
378 serviceResources.put("serviceAllottedResources", allottedResourcesArray)
379 serviceResourcesObject.put("serviceResources", serviceResources)
380 logger.debug("Returning serviceResources JSON: " + serviceResourcesObject.toString())
382 } catch (Exception e) {
383 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
384 "Exception in parsing Catalog DB Response", "BPMN",
385 ErrorCode.UnknownError.getValue(), e.message);
388 return serviceResourcesObject
391 private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb, String catalogUtilsVersion) {
392 JSONObject modelInfo = null
394 modelInfo = new JSONObject()
395 modelInfo.put("modelType", modelType)
396 String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
397 modelInfo.put("modelInvariantId", modelInvariantId)
398 if(modelType.equalsIgnoreCase("allottedResource") || modelType.equalsIgnoreCase("vnf")){
399 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
400 modelInfo.put("modelInstanceName", modelInstanceName)
402 if ((!"vfModule".equals(modelType) && !"vnf".equals(modelType)) || !catalogUtilsVersion.equals("v1")) {
403 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
404 modelInfo.put("modelVersionId", modelVersionId)
407 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
408 modelInfo.put("modelVersionId", modelVersionId)
410 String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
411 modelInfo.put("modelName", modelName)
412 String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
413 modelInfo.put("modelVersion", modelVersion)
414 if (!"vfModule".equals(modelType)) {
415 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
416 modelInfo.put("modelCustomizationName", modelCustomizationName)
418 String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
419 switch (catalogUtilsVersion) {
421 modelInfo.put("modelCustomizationId", modelCustomizationId)
424 modelInfo.put("modelCustomizationUuid", modelCustomizationId)
427 JSONObject modelJson = new JSONObject()
428 modelJson.put("modelInfo", modelInfo)
430 catch (Exception e) {
431 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
432 "Exception while parsing model information", "BPMN",
433 ErrorCode.UnknownError.getValue(), e.message);
438 private String getResponseFromCatalogDb (DelegateExecution execution, String endPoint) {
441 String catalogDbEndpoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint",execution)
442 String queryEndpoint = catalogDbEndpoint + "/" + defaultDbAdapterVersion + endPoint
443 def responseData = ''
444 HttpClient client = httpClientFactory.newJsonClient(new URL(queryEndpoint), TargetEntity.CATALOG_DB)
445 client.addAdditionalHeader(ONAPLogConstants.Headers.REQUEST_ID, UUID.randomUUID().toString())
446 client.addAdditionalHeader('X-FromAppId', "BPMN")
447 client.addAdditionalHeader('Accept', MediaType.APPLICATION_JSON)
448 String basicAuthCred = execution.getVariable("BasicAuthHeaderValueDB")
449 client.addAdditionalHeader("Authorization", StringUtils.defaultIfEmpty(basicAuthCred, getBasicDBAuthHeader(execution)))
451 logger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
452 Response response = client.get()
454 responseData = response.readEntity(String.class)
455 if (responseData != null) {
456 logger.debug("Received data from Catalog DB: " + responseData)
459 logger.debug('Response code:' + response.getStatus())
460 logger.debug('Response:' + System.lineSeparator() + responseData)
461 if (response.getStatus() == 200) {
462 // parse response as needed
469 catch (Exception e) {
470 logger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
477 * get resource recipe by resource model uuid and action
479 public JSONObject getResourceRecipe(DelegateExecution execution, String resourceModelUuid, String action) {
480 String endPoint = "/resourceRecipe?resourceModelUuid=" + UriUtils.encode(resourceModelUuid, "UTF-8")+ "&action=" + UriUtils.encode(action, "UTF-8")
481 JSONObject responseJson = null
483 logger.debug("ENDPOINT: " + endPoint)
484 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
486 if (catalogDbResponse != null) {
487 responseJson = new JSONObject(catalogDbResponse)
490 catch (Exception e) {
491 msoUtils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
498 private String getBasicDBAuthHeader(DelegateExecution execution) {
500 String encodedString = null
502 String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
503 msoUtils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
505 encodedString = msoUtils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
506 execution.setVariable("BasicAuthHeaderValueDB",encodedString)
507 } catch (IOException ex) {
508 String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
509 msoUtils.log("ERROR", dataErrorMessage)