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.openecomp.mso.bpmn.common.scripts
23 import org.json.JSONObject;
24 import org.json.JSONArray;
26 import org.springframework.web.util.UriUtils;
28 import org.openecomp.mso.bpmn.core.json.JsonUtils
31 import groovy.json.JsonBuilder
32 import groovy.json.JsonSlurper
33 import groovy.util.slurpersupport.GPathResult
34 import groovy.xml.QName;
36 import org.openecomp.mso.logger.MsoLogger;
37 import org.openecomp.mso.rest.APIResponse;
38 import org.openecomp.mso.rest.RESTClient
39 import org.openecomp.mso.rest.RESTConfig
43 * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
47 class CatalogDbUtils {
49 MsoUtils utils = new MsoUtils()
50 JsonUtils jsonUtils = new JsonUtils()
51 MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
53 public JSONArray getAllNetworksByServiceModelUuid(String catalogDbEndpoint, String serviceModelUuid) {
54 JSONArray networksList = null
55 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
57 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
59 if (catalogDbResponse != null) {
60 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
65 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
71 public JSONArray getAllNetworksByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
72 JSONArray networksList = null
73 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
75 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
77 if (catalogDbResponse != null) {
78 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
83 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
89 public JSONArray getAllNetworksByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
90 JSONArray networksList = null
91 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
93 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
95 if (catalogDbResponse != null) {
96 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
100 catch (Exception e) {
101 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
107 public JSONArray getAllNetworksByNetworkModelCustomizationUuid(String catalogDbEndpoint, String networkModelCustomizationUuid) {
108 JSONArray networksList = null
109 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?networkModelCustomizationUuid=" + UriUtils.encode(networkModelCustomizationUuid, "UTF-8")
111 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
113 if (catalogDbResponse != null) {
114 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
118 catch (Exception e) {
119 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
125 public JSONArray getAllNetworksByNetworkType(String catalogDbEndpoint, String networkType) {
126 JSONArray networksList = null
127 String endPoint = catalogDbEndpoint + "/v1/serviceNetworks?networkType=" + UriUtils.encode(networkType, "UTF-8")
129 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
131 if (catalogDbResponse != null) {
132 networksList = parseNetworksJson(catalogDbResponse, "serviceNetworks")
136 catch (Exception e) {
137 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
144 public JSONArray getAllVnfsByServiceModelUuid(String catalogDbEndpoint, String serviceModelUuid) {
145 JSONArray vnfsList = null
146 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
148 msoLogger.debug("ENDPOINT: " + endPoint)
149 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
151 if (catalogDbResponse != null) {
152 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
156 catch (Exception e) {
157 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
163 public JSONArray getAllVnfsByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
164 JSONArray vnfsList = null
165 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
167 msoLogger.debug("ENDPOINT: " + endPoint)
168 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
170 if (catalogDbResponse != null) {
171 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
175 catch (Exception e) {
176 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
182 public JSONArray getAllVnfsByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
183 JSONArray vnfsList = null
184 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
186 msoLogger.debug("ENDPOINT: " + endPoint)
187 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
189 if (catalogDbResponse != null) {
190 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
194 catch (Exception e) {
195 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
201 public JSONArray getAllVnfsByVnfModelCustomizationUuid(String catalogDbEndpoint, String vnfModelCustomizationUuid) {
202 JSONArray vnfsList = null
203 String endPoint = catalogDbEndpoint + "/v1/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
205 msoLogger.debug("ENDPOINT: " + endPoint)
206 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
208 if (catalogDbResponse != null) {
209 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs")
213 catch (Exception e) {
214 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
221 public JSONArray getAllottedResourcesByServiceModelUuid(String catalogDbEndpoint, String serviceModelUuid) {
222 JSONArray vnfsList = null
223 String endPoint = catalogDbEndpoint + "/v1/ServiceAllottedResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
225 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
227 if (catalogDbResponse != null) {
228 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
232 catch (Exception e) {
233 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
239 public JSONArray getAllottedResourcesByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
240 JSONArray vnfsList = null
241 String endPoint = catalogDbEndpoint + "/v1/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
243 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
245 if (catalogDbResponse != null) {
246 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
250 catch (Exception e) {
251 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
257 public JSONArray getAllottedResourcesByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
258 JSONArray vnfsList = null
259 String endPoint = catalogDbEndpoint + "/v1/serviceAllottedResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
261 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
263 if (catalogDbResponse != null) {
264 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
268 catch (Exception e) {
269 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
275 public JSONArray getAllottedResourcesByArModelCustomizationUuid(String catalogDbEndpoint, String arModelCustomizationUuid) {
276 JSONArray vnfsList = null
277 String endPoint = catalogDbEndpoint + "/v1/serviceAllottedResources?serviceModelCustomizationUuid=" + UriUtils.encode(arModelCustomizationUuid, "UTF-8")
279 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
281 if (catalogDbResponse != null) {
282 vnfsList = parseAllottedResourcesJson(catalogDbResponse, "serviceAllottedResources")
286 catch (Exception e) {
287 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
293 public JSONObject getServiceResourcesByServiceModelInvariantUuid(String catalogDbEndpoint, String serviceModelInvariantUuid) {
294 JSONObject resources = null
295 String endPoint = catalogDbEndpoint + "/v1/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
297 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
299 if (catalogDbResponse != null) {
300 resources = parseServiceResourcesJson(catalogDbResponse)
304 catch (Exception e) {
305 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
311 public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(String catalogDbEndpoint, String serviceModelInvariantUuid, String serviceModelVersion) {
312 JSONObject resources = null
313 String endPoint = catalogDbEndpoint + "/v1/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
315 String catalogDbResponse = getResponseFromCatalogDb(endPoint)
317 if (catalogDbResponse != null) {
318 resources = parseServiceResourcesJson(catalogDbResponse)
322 catch (Exception e) {
323 utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
331 private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName) {
332 JSONArray modelInfos = null
335 // Create array of jsons
337 JSONObject responseJson = new JSONObject(catalogDbResponse)
338 JSONArray networks = responseJson.getJSONArray(arrayName)
339 modelInfos = new JSONArray()
341 for (int i = 0; i < networks.length(); i++) {
343 JSONObject network = networks.getJSONObject(i)
344 JSONObject modelInfo = buildModelInfo("network", network)
345 JSONObject modelJson = new JSONObject()
346 modelJson.put("modelInfo", modelInfo)
347 String networkType = jsonUtils.getJsonValueForKey(network, "networkType")
348 modelJson.put("networkType", networkType)
349 modelInfos.put(modelJson)
352 String modelInfosString = modelInfos.toString()
353 msoLogger.debug("Returning networks JSON: " + modelInfosString)
355 } catch (Exception e) {
356 utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
362 private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName) {
363 JSONArray modelInfos = null
366 // Create array of jsons
368 JSONObject responseJson = new JSONObject(catalogDbResponse)
369 JSONArray vnfs = responseJson.getJSONArray(arrayName)
370 modelInfos = new JSONArray()
372 for (int i = 0; i < vnfs.length(); i++) {
373 JSONObject vnf = vnfs.getJSONObject(i)
375 msoLogger.debug(vnf.toString(2))
376 JSONObject modelInfo = buildModelInfo("vnf", vnf)
377 JSONObject modelJson = new JSONObject()
378 modelJson.put("modelInfo", modelInfo)
380 JSONArray vfModules = null
382 vfModules = vnf.getJSONArray("vfModules")
383 } catch (Exception e)
385 msoLogger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
388 if (vfModules != null) {
389 JSONArray vfModuleInfo = new JSONArray()
390 for (int j = 0; j < vfModules.length(); j++) {
391 JSONObject vfModule = vfModules.getJSONObject(j)
392 JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule)
393 JSONObject vfModuleModelJson = new JSONObject()
394 vfModuleModelJson.put("modelInfo", vfModuleModelInfo)
395 String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
396 vfModuleModelJson.put("vfModuleType", vfModuleType)
397 Integer isBase = jsonUtils.getJsonIntValueForKey(vfModule, "isBase")
398 if (isBase.intValue() == 1) {
399 vfModuleModelJson.put("isBase", "true")
402 vfModuleModelJson.put("isBase", "false")
404 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
405 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
406 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
407 vfModuleModelJson.put("initialCount", initialCount.intValue())
408 vfModuleInfo.put(vfModuleModelJson)
410 modelJson.put("vfModules", vfModuleInfo)
412 modelInfos.put(modelJson)
415 String modelInfosString = modelInfos.toString()
416 msoLogger.debug("Returning vnfs JSON: " + modelInfosString)
418 } catch (Exception e) {
419 utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
425 private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName) {
426 JSONArray modelInfos = null
429 // Create array of jsons
431 JSONObject responseJson = new JSONObject(catalogDbResponse)
432 JSONArray allottedResources = responseJson.getJSONArray(arrayName)
433 modelInfos = new JSONArray()
435 for (int i = 0; i < allottedResources.length(); i++) {
436 JSONObject allottedResource = allottedResources.getJSONObject(i)
437 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource)
438 JSONObject modelJson = new JSONObject()
439 modelJson.put("modelInfo", modelInfo)
440 modelInfos.put(modelJson)
443 String modelInfosString = modelInfos.toString()
444 msoLogger.debug("Returning allottedResources JSON: " + modelInfosString)
446 } catch (Exception e) {
447 utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
453 private JSONObject parseServiceResourcesJson (String catalogDbResponse) {
454 JSONObject serviceResources = new JSONObject()
457 // Create array of jsons
459 JSONObject responseJson = new JSONObject(catalogDbResponse)
460 JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
461 JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "vnfResources")
462 serviceResources.put("vnfs", vnfsArray)
463 JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "networkResourceCustomization")
464 serviceResources.put("networks", networksArray)
465 JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "allottedResourceCustomization")
466 serviceResources.put("allottedResources", allottedResourcesArray)
468 String serviceResourcesString = serviceResources.toString()
469 msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesString)
471 } catch (Exception e) {
472 utils.log("ERROR", "Exception in parsing Catalog DB Response: " + e.message)
475 return serviceResources
478 private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb) {
479 JSONObject modelInfo = null
481 modelInfo = new JSONObject()
482 modelInfo.put("modelType", modelType)
483 String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
484 modelInfo.put("modelInvariantId", modelInvariantId)
485 if(modelType.equalsIgnoreCase("allottedResource")){
486 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
487 modelInfo.put("modelInstanceName", modelInstanceName)
489 if (!"vfModule".equals(modelType) && !"vnf".equals(modelType)) {
490 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
491 modelInfo.put("modelVersionId", modelVersionId)
494 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
495 modelInfo.put("modelVersionId", modelVersionId)
497 String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
498 modelInfo.put("modelName", modelName)
499 String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
500 modelInfo.put("modelVersion", modelVersion)
501 if (!"vfModule".equals(modelType)) {
502 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
503 modelInfo.put("modelCustomizationName", modelCustomizationName)
505 String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
506 modelInfo.put("modelCustomizationId", modelCustomizationId)
507 JSONObject modelJson = new JSONObject()
508 modelJson.put("modelInfo", modelInfo)
510 catch (Exception e) {
511 utils.log("ERROR", "Exception while parsing model information: " + e.message)
516 private String getResponseFromCatalogDb (String endPoint) {
518 RESTConfig config = new RESTConfig(endPoint);
519 def responseData = ''
520 def bpmnRequestId = UUID.randomUUID().toString()
521 RESTClient client = new RESTClient(config).
522 addHeader('X-TransactionId', bpmnRequestId).
523 addHeader('X-FromAppId', 'BPMN').
524 addHeader('Content-Type', 'application/json').
525 addHeader('Accept','application/json');
526 msoLogger.debug('sending GET to Catalog DB endpoint' + endPoint)
527 APIResponse response = client.httpGet()
529 responseData = response.getResponseBodyAsString()
530 if (responseData != null) {
531 msoLogger.debug("Received data from Catalog DB: " + responseData)
534 msoLogger.debug('Response code:' + response.getStatusCode())
535 msoLogger.debug('Response:' + System.lineSeparator() + responseData)
536 if (response.getStatusCode() == 200) {
537 // parse response as needed
544 catch (Exception e) {
545 msoLogger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)