2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.common.scripts
23 import org.json.JSONObject;
24 import org.json.JSONArray;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants
26 import org.onap.so.bpmn.core.UrnPropertiesReader;
27 import org.springframework.web.util.UriUtils;
29 import org.onap.so.bpmn.core.json.JsonUtils
30 import org.onap.so.client.HttpClient
32 import javax.ws.rs.core.MediaType
33 import javax.ws.rs.core.Response
34 import org.camunda.bpm.engine.delegate.DelegateExecution
36 import org.onap.so.logger.MsoLogger;
37 import org.onap.so.utils.TargetEntity
38 import org.onap.so.logger.MessageEnum
43 * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
47 class CatalogDbUtils {
48 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CatalogDbUtils.class);
51 MsoUtils utils = new MsoUtils()
52 JsonUtils jsonUtils = new JsonUtils()
53 static private String defaultDbAdapterVersion = "v2"
55 public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid) {
56 JSONArray vnfsList = null
57 String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
59 msoLogger.debug("ENDPOINT: " + endPoint)
60 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
62 if (catalogDbResponse != null) {
63 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", "v1")
68 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
75 public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid, String catalogUtilsVersion) {
76 JSONArray vnfsList = null
77 String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
79 msoLogger.debug("ENDPOINT: " + endPoint)
80 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
82 if (catalogDbResponse != null) {
83 if (!catalogUtilsVersion.equals("v1")) {
84 JSONObject responseJson = new JSONObject(catalogDbResponse)
85 vnfsList = responseJson.getJSONArray("serviceVnfs")
88 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
94 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
101 public JSONObject getServiceResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
102 JSONObject resources = null
103 String endPoint = "/serviceResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
105 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
107 if (catalogDbResponse != null) {
108 if (!catalogUtilsVersion.equals("v1")) {
109 resources = new JSONObject(catalogDbResponse)
112 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
116 catch (Exception e) {
117 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
124 public String getServiceResourcesByServiceModelInvariantUuidString(DelegateExecution execution, String serviceModelInvariantUuid) {
125 String resources = null
126 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
128 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
130 if (catalogDbResponse != null) {
132 resources = catalogDbResponse
136 catch (Exception e) {
137 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
144 public JSONObject getServiceResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
145 JSONObject resources = null
146 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
148 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
150 if (catalogDbResponse != null) {
151 if (!catalogUtilsVersion.equals("v1")) {
152 resources = new JSONObject(catalogDbResponse)
155 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
160 catch (Exception e) {
161 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
168 public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
169 JSONObject resources = null
170 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
172 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
174 if (catalogDbResponse != null) {
175 if (!catalogUtilsVersion.equals("v1")) {
176 resources = new JSONObject(catalogDbResponse)
179 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
184 catch (Exception e) {
185 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
192 private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
193 JSONArray modelInfos = null
195 msoLogger.debug("parseNetworksJson - catalogUtilsVersion is " + catalogUtilsVersion)
197 // Create array of jsons
199 JSONObject responseJson = new JSONObject(catalogDbResponse)
200 JSONArray networks = responseJson.getJSONArray(arrayName)
201 modelInfos = new JSONArray()
203 for (int i = 0; i < networks.length(); i++) {
205 JSONObject network = networks.getJSONObject(i)
206 JSONObject modelJson = new JSONObject()
207 JSONObject modelInfo = buildModelInfo("network", network, catalogUtilsVersion)
208 modelJson.put("modelInfo", modelInfo)
209 String networkType = jsonUtils.getJsonValueForKey(network, "networkType")
210 modelJson.put("networkType", networkType)
212 switch (catalogUtilsVersion) {
216 String toscaNodeType = jsonUtils.getJsonValueForKey(network, "toscaNodeType")
217 modelJson.put("toscaNodeType", toscaNodeType)
218 String networkTechnology = jsonUtils.getJsonValueForKey(network, "networkTechnology")
219 modelJson.put("networkTechnology", networkTechnology)
220 String networkRole = jsonUtils.getJsonValueForKey(network, "networkRole")
221 modelJson.put("networkRole", networkRole)
222 String networkScope = jsonUtils.getJsonValueForKey(network, "networkScope")
223 modelJson.put("networkScope", networkScope)
226 modelInfos.put(modelJson)
229 String modelInfosString = modelInfos.toString()
230 msoLogger.debug("Returning networks JSON: " + modelInfosString)
232 } catch (Exception e) {
233 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
239 private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
240 JSONArray modelInfos = null
242 msoLogger.debug("parseVnfsJson - catalogUtilsVersion is " + catalogUtilsVersion)
245 // Create array of jsons
247 JSONObject responseJson = new JSONObject(catalogDbResponse)
248 JSONArray vnfs = responseJson.getJSONArray(arrayName)
249 modelInfos = new JSONArray()
251 for (int i = 0; i < vnfs.length(); i++) {
252 JSONObject vnf = vnfs.getJSONObject(i)
254 msoLogger.debug(vnf.toString(2))
255 JSONObject modelInfo = buildModelInfo("vnf", vnf, catalogUtilsVersion)
256 JSONObject modelJson = new JSONObject()
257 modelJson.put("modelInfo", modelInfo)
258 switch(catalogUtilsVersion) {
262 String toscaNodeType = jsonUtils.getJsonValueForKey(vnf, "toscaNodeType")
263 modelJson.put("toscaNodeType", toscaNodeType)
264 String nfType = jsonUtils.getJsonValueForKey(vnf, "nfType")
265 modelJson.put("nfType", nfType)
266 String nfRole = jsonUtils.getJsonValueForKey(vnf, "nfRole")
267 modelJson.put("nfRole", nfRole)
268 String nfCode = jsonUtils.getJsonValueForKey(vnf, "nfCode")
269 modelJson.put("nfNamingCode", nfCode)
270 String nfFunction = jsonUtils.getJsonValueForKey(vnf, "nfFunction")
271 modelJson.put("nfFunction", nfFunction)
272 String multiStageDesign = jsonUtils.getJsonValueForKey(vnf, "multiStageDesign")
273 modelJson.put("multiStageDesign", multiStageDesign)
277 JSONArray vfModules = null
279 vfModules = vnf.getJSONArray("vfModules")
280 } catch (Exception e)
282 msoLogger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
285 if (vfModules != null) {
286 JSONArray vfModuleInfo = new JSONArray()
287 for (int j = 0; j < vfModules.length(); j++) {
288 JSONObject vfModule = vfModules.getJSONObject(j)
289 JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule, catalogUtilsVersion)
290 JSONObject vfModuleModelJson = new JSONObject()
291 vfModuleModelJson.put("modelInfo", vfModuleModelInfo)
292 String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
293 vfModuleModelJson.put("vfModuleType", vfModuleType)
294 vfModuleModelJson.put("isBase", jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase"))
295 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
296 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
297 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
298 vfModuleModelJson.put("initialCount", initialCount.intValue())
299 vfModuleInfo.put(vfModuleModelJson)
301 modelJson.put("vfModules", vfModuleInfo)
303 modelInfos.put(modelJson)
306 String modelInfosString = modelInfos.toString()
307 msoLogger.debug("Returning vnfs JSON: " + modelInfosString)
309 } catch (Exception e) {
310 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
316 private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
317 JSONArray modelInfos = null
319 msoLogger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
322 // Create array of jsons
324 JSONObject responseJson = new JSONObject(catalogDbResponse)
325 JSONArray allottedResources = responseJson.getJSONArray(arrayName)
326 modelInfos = new JSONArray()
328 for (int i = 0; i < allottedResources.length(); i++) {
329 JSONObject allottedResource = allottedResources.getJSONObject(i)
330 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource, catalogUtilsVersion)
331 JSONObject modelJson = new JSONObject()
332 modelJson.put("modelInfo", modelInfo)
333 switch(catalogUtilsVersion) {
337 String toscaNodeType = jsonUtils.getJsonValueForKey(allottedResource, "toscaNodeType")
338 modelJson.put("toscaNodeType", toscaNodeType)
339 String nfType = jsonUtils.getJsonValueForKey(allottedResource, "nfType")
340 modelJson.put("nfType", nfType)
341 String nfRole = jsonUtils.getJsonValueForKey(allottedResource, "nfRole")
342 modelJson.put("nfRole", nfRole)
343 String nfCode = jsonUtils.getJsonValueForKey(allottedResource, "nfCode")
344 modelJson.put("nfNamingCode", nfCode)
345 String nfFunction = jsonUtils.getJsonValueForKey(allottedResource, "nfFunction")
346 modelJson.put("nfFunction", nfFunction)
347 String providingServiceModelName = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelName")
348 modelJson.put("providingServiceModelName", providingServiceModelName)
349 String providingServiceModelUuid = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelUuid")
350 modelJson.put("providingServiceModelUuid", providingServiceModelUuid)
355 modelInfos.put(modelJson)
358 String modelInfosString = modelInfos.toString()
359 msoLogger.debug("Returning allottedResources JSON: " + modelInfosString)
361 } catch (Exception e) {
362 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
368 private JSONObject parseServiceResourcesJson (String catalogDbResponse, String catalogUtilsVersion) {
369 JSONObject serviceResources = new JSONObject()
370 JSONObject serviceResourcesObject = new JSONObject()
373 // Create array of jsons
375 JSONObject responseJson = new JSONObject(catalogDbResponse)
376 JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
377 JSONObject modelInfo = buildModelInfo("", serviceResourcesRoot, catalogUtilsVersion)
378 serviceResources.put("modelInfo", modelInfo)
379 JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "serviceVnfs", catalogUtilsVersion)
380 serviceResources.put("serviceVnfs", vnfsArray)
381 JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "serviceNetworks", catalogUtilsVersion)
382 serviceResources.put("serviceNetworks", networksArray)
383 JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "serviceAllottedResources", catalogUtilsVersion)
384 serviceResources.put("serviceAllottedResources", allottedResourcesArray)
385 serviceResourcesObject.put("serviceResources", serviceResources)
386 msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesObject.toString())
388 } catch (Exception e) {
389 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
392 return serviceResourcesObject
395 private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb, String catalogUtilsVersion) {
396 JSONObject modelInfo = null
398 modelInfo = new JSONObject()
399 modelInfo.put("modelType", modelType)
400 String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
401 modelInfo.put("modelInvariantId", modelInvariantId)
402 if(modelType.equalsIgnoreCase("allottedResource") || modelType.equalsIgnoreCase("vnf")){
403 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
404 modelInfo.put("modelInstanceName", modelInstanceName)
406 if ((!"vfModule".equals(modelType) && !"vnf".equals(modelType)) || !catalogUtilsVersion.equals("v1")) {
407 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
408 modelInfo.put("modelVersionId", modelVersionId)
411 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
412 modelInfo.put("modelVersionId", modelVersionId)
414 String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
415 modelInfo.put("modelName", modelName)
416 String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
417 modelInfo.put("modelVersion", modelVersion)
418 if (!"vfModule".equals(modelType)) {
419 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
420 modelInfo.put("modelCustomizationName", modelCustomizationName)
422 String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
423 switch (catalogUtilsVersion) {
425 modelInfo.put("modelCustomizationId", modelCustomizationId)
428 modelInfo.put("modelCustomizationUuid", modelCustomizationId)
431 JSONObject modelJson = new JSONObject()
432 modelJson.put("modelInfo", modelInfo)
434 catch (Exception e) {
435 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception while parsing model information", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
440 private String getResponseFromCatalogDb (DelegateExecution execution, String endPoint) {
443 String catalogDbEndpoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint",execution)
444 String queryEndpoint = catalogDbEndpoint + "/" + defaultDbAdapterVersion + endPoint
445 def responseData = ''
446 def bpmnRequestId = UUID.randomUUID().toString()
448 URL url = new URL(queryEndpoint)
449 HttpClient client = new HttpClient(url, MediaType.APPLICATION_JSON, TargetEntity.CATALOG_DB)
450 client.addAdditionalHeader(ONAPLogConstants.Headers.REQUEST_ID, bpmnRequestId)
451 client.addAdditionalHeader('X-FromAppId', "BPMN")
452 client.addAdditionalHeader('Accept', MediaType.APPLICATION_JSON)
453 String basicAuthCred = execution.getVariable("BasicAuthHeaderValueDB")
454 if (basicAuthCred != null && !"".equals(basicAuthCred)) {
455 client.addAdditionalHeader("Authorization", basicAuthCred)
457 client.addAdditionalHeader("Authorization", getBasicDBAuthHeader(execution))
460 msoLogger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
461 Response response = client.get()
463 responseData = response.readEntity(String.class)
464 if (responseData != null) {
465 msoLogger.debug("Received data from Catalog DB: " + responseData)
468 msoLogger.debug('Response code:' + response.getStatus())
469 msoLogger.debug('Response:' + System.lineSeparator() + responseData)
470 if (response.getStatus() == 200) {
471 // parse response as needed
478 catch (Exception e) {
479 msoLogger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
486 * get resource recipe by resource model uuid and action
488 public JSONObject getResourceRecipe(DelegateExecution execution, String resourceModelUuid, String action) {
489 String endPoint = "/resourceRecipe?resourceModelUuid=" + UriUtils.encode(resourceModelUuid, "UTF-8")+ "&action=" + UriUtils.encode(action, "UTF-8")
490 JSONObject responseJson = null
492 msoLogger.debug("ENDPOINT: " + endPoint)
493 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
495 if (catalogDbResponse != null) {
496 responseJson = new JSONObject(catalogDbResponse)
499 catch (Exception e) {
500 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
507 private String getBasicDBAuthHeader(DelegateExecution execution) {
509 String encodedString = null
511 String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
512 utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
514 encodedString = utils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
515 execution.setVariable("BasicAuthHeaderValueDB",encodedString)
516 } catch (IOException ex) {
517 String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
518 utils.log("ERROR", dataErrorMessage)