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.MessageEnum
35 import org.onap.so.logger.MsoLogger
36 import org.slf4j.Logger
37 import org.slf4j.LoggerFactory
38 import org.onap.so.utils.TargetEntity
39 import org.springframework.web.util.UriUtils
41 import javax.ws.rs.core.MediaType
42 import javax.ws.rs.core.Response
45 * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
49 class CatalogDbUtils {
50 private static final Logger logger = LoggerFactory.getLogger( CatalogDbUtils.class);
52 private HttpClientFactory httpClientFactory
53 private MsoUtils msoUtils
54 private JsonUtils jsonUtils
55 static private String defaultDbAdapterVersion = "v2"
57 CatalogDbUtils(HttpClientFactory httpClientFactory, MsoUtils msoUtils, JsonUtils jsonUtils) {
58 this.httpClientFactory = httpClientFactory
59 this.msoUtils = msoUtils
60 this.jsonUtils = jsonUtils
63 public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid, String catalogUtilsVersion) {
64 JSONArray vnfsList = null
65 String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
67 logger.debug("ENDPOINT: " + endPoint)
68 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
70 if (catalogDbResponse != null) {
71 if (!catalogUtilsVersion.equals("v1")) {
72 JSONObject responseJson = new JSONObject(catalogDbResponse)
73 vnfsList = responseJson.getJSONArray("serviceVnfs")
76 vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", catalogUtilsVersion)
82 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
83 "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(),
84 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
91 public JSONObject getServiceResourcesByServiceModelUuid(DelegateExecution execution, String serviceModelUuid, String catalogUtilsVersion) {
92 JSONObject resources = null
93 String endPoint = "/serviceResources?serviceModelUuid=" + UriUtils.encode(serviceModelUuid, "UTF-8")
95 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
97 if (catalogDbResponse != null) {
98 if (!catalogUtilsVersion.equals("v1")) {
99 resources = new JSONObject(catalogDbResponse)
102 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
106 catch (Exception e) {
107 msoUtils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
114 public String getServiceResourcesByServiceModelInvariantUuidString(DelegateExecution execution, String serviceModelInvariantUuid) {
115 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
117 return getResponseFromCatalogDb(execution, endPoint)
119 catch (Exception e) {
120 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
121 "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(),
122 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
127 public JSONObject getServiceResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
128 JSONObject resources = null
129 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
131 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
133 if (catalogDbResponse != null) {
134 if (!catalogUtilsVersion.equals("v1")) {
135 resources = new JSONObject(catalogDbResponse)
138 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
143 catch (Exception e) {
144 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
145 "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(),
146 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
153 public JSONObject getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(DelegateExecution execution, String serviceModelInvariantUuid, String serviceModelVersion, String catalogUtilsVersion) {
154 JSONObject resources = null
155 String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8") + "&serviceModelVersion=" + UriUtils.encode(serviceModelVersion, "UTF-8")
157 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
159 if (catalogDbResponse != null) {
160 if (!catalogUtilsVersion.equals("v1")) {
161 resources = new JSONObject(catalogDbResponse)
164 resources = parseServiceResourcesJson(catalogDbResponse, catalogUtilsVersion)
169 catch (Exception e) {
170 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
171 "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(),
172 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
179 private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
180 JSONArray modelInfos = null
182 logger.debug("parseNetworksJson - catalogUtilsVersion is " + catalogUtilsVersion)
184 // Create array of jsons
186 JSONObject responseJson = new JSONObject(catalogDbResponse)
187 JSONArray networks = responseJson.getJSONArray(arrayName)
188 modelInfos = new JSONArray()
190 for (int i = 0; i < networks.length(); i++) {
192 JSONObject network = networks.getJSONObject(i)
193 JSONObject modelJson = new JSONObject()
194 JSONObject modelInfo = buildModelInfo("network", network, catalogUtilsVersion)
195 modelJson.put("modelInfo", modelInfo)
196 String networkType = jsonUtils.getJsonValueForKey(network, "networkType")
197 modelJson.put("networkType", networkType)
199 switch (catalogUtilsVersion) {
203 String toscaNodeType = jsonUtils.getJsonValueForKey(network, "toscaNodeType")
204 modelJson.put("toscaNodeType", toscaNodeType)
205 String networkTechnology = jsonUtils.getJsonValueForKey(network, "networkTechnology")
206 modelJson.put("networkTechnology", networkTechnology)
207 String networkRole = jsonUtils.getJsonValueForKey(network, "networkRole")
208 modelJson.put("networkRole", networkRole)
209 String networkScope = jsonUtils.getJsonValueForKey(network, "networkScope")
210 modelJson.put("networkScope", networkScope)
213 modelInfos.put(modelJson)
216 String modelInfosString = modelInfos.toString()
217 logger.debug("Returning networks JSON: " + modelInfosString)
219 } catch (Exception e) {
220 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
221 "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(),
222 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
228 private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
229 JSONArray modelInfos = null
231 logger.debug("parseVnfsJson - catalogUtilsVersion is " + catalogUtilsVersion)
234 // Create array of jsons
236 JSONObject responseJson = new JSONObject(catalogDbResponse)
237 JSONArray vnfs = responseJson.getJSONArray(arrayName)
238 modelInfos = new JSONArray()
240 for (int i = 0; i < vnfs.length(); i++) {
241 JSONObject vnf = vnfs.getJSONObject(i)
243 logger.debug(vnf.toString(2))
244 JSONObject modelInfo = buildModelInfo("vnf", vnf, catalogUtilsVersion)
245 JSONObject modelJson = new JSONObject()
246 modelJson.put("modelInfo", modelInfo)
247 switch(catalogUtilsVersion) {
251 String toscaNodeType = jsonUtils.getJsonValueForKey(vnf, "toscaNodeType")
252 modelJson.put("toscaNodeType", toscaNodeType)
253 String nfType = jsonUtils.getJsonValueForKey(vnf, "nfType")
254 modelJson.put("nfType", nfType)
255 String nfRole = jsonUtils.getJsonValueForKey(vnf, "nfRole")
256 modelJson.put("nfRole", nfRole)
257 String nfCode = jsonUtils.getJsonValueForKey(vnf, "nfCode")
258 modelJson.put("nfNamingCode", nfCode)
259 String nfFunction = jsonUtils.getJsonValueForKey(vnf, "nfFunction")
260 modelJson.put("nfFunction", nfFunction)
261 String multiStageDesign = jsonUtils.getJsonValueForKey(vnf, "multiStageDesign")
262 modelJson.put("multiStageDesign", multiStageDesign)
266 JSONArray vfModules = null
268 vfModules = vnf.getJSONArray("vfModules")
269 } catch (Exception e)
271 logger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
274 if (vfModules != null) {
275 JSONArray vfModuleInfo = new JSONArray()
276 for (int j = 0; j < vfModules.length(); j++) {
277 JSONObject vfModule = vfModules.getJSONObject(j)
278 JSONObject vfModuleModelInfo = buildModelInfo("vfModule", vfModule, catalogUtilsVersion)
279 JSONObject vfModuleModelJson = new JSONObject()
280 vfModuleModelJson.put("modelInfo", vfModuleModelInfo)
281 String vfModuleType = jsonUtils.getJsonValueForKey(vfModule, "type")
282 vfModuleModelJson.put("vfModuleType", vfModuleType)
283 vfModuleModelJson.put("isBase", jsonUtils.getJsonBooleanValueForKey(vfModule, "isBase"))
284 String vfModuleLabel = jsonUtils.getJsonValueForKey(vfModule, "label")
285 vfModuleModelJson.put("vfModuleLabel", vfModuleLabel)
286 Integer initialCount = jsonUtils.getJsonIntValueForKey(vfModule, "initialCount")
287 vfModuleModelJson.put("initialCount", initialCount.intValue())
288 vfModuleInfo.put(vfModuleModelJson)
290 modelJson.put("vfModules", vfModuleInfo)
292 modelInfos.put(modelJson)
295 String modelInfosString = modelInfos.toString()
296 logger.debug("Returning vnfs JSON: " + modelInfosString)
298 } catch (Exception e) {
299 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
300 "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(),
301 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
307 private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
308 JSONArray modelInfos = null
310 logger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
313 // Create array of jsons
315 JSONObject responseJson = new JSONObject(catalogDbResponse)
316 JSONArray allottedResources = responseJson.getJSONArray(arrayName)
317 modelInfos = new JSONArray()
319 for (int i = 0; i < allottedResources.length(); i++) {
320 JSONObject allottedResource = allottedResources.getJSONObject(i)
321 JSONObject modelInfo = buildModelInfo("allottedResource", allottedResource, catalogUtilsVersion)
322 JSONObject modelJson = new JSONObject()
323 modelJson.put("modelInfo", modelInfo)
324 switch(catalogUtilsVersion) {
328 String toscaNodeType = jsonUtils.getJsonValueForKey(allottedResource, "toscaNodeType")
329 modelJson.put("toscaNodeType", toscaNodeType)
330 String nfType = jsonUtils.getJsonValueForKey(allottedResource, "nfType")
331 modelJson.put("nfType", nfType)
332 String nfRole = jsonUtils.getJsonValueForKey(allottedResource, "nfRole")
333 modelJson.put("nfRole", nfRole)
334 String nfCode = jsonUtils.getJsonValueForKey(allottedResource, "nfCode")
335 modelJson.put("nfNamingCode", nfCode)
336 String nfFunction = jsonUtils.getJsonValueForKey(allottedResource, "nfFunction")
337 modelJson.put("nfFunction", nfFunction)
338 String providingServiceModelName = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelName")
339 modelJson.put("providingServiceModelName", providingServiceModelName)
340 String providingServiceModelUuid = jsonUtils.getJsonValueForKey(allottedResource, "providingServiceModelUuid")
341 modelJson.put("providingServiceModelUuid", providingServiceModelUuid)
346 modelInfos.put(modelJson)
349 String modelInfosString = modelInfos.toString()
350 logger.debug("Returning allottedResources JSON: " + modelInfosString)
352 } catch (Exception e) {
353 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
354 "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(),
355 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
361 private JSONObject parseServiceResourcesJson (String catalogDbResponse, String catalogUtilsVersion) {
362 JSONObject serviceResources = new JSONObject()
363 JSONObject serviceResourcesObject = new JSONObject()
366 // Create array of jsons
368 JSONObject responseJson = new JSONObject(catalogDbResponse)
369 JSONObject serviceResourcesRoot = responseJson.getJSONObject("serviceResources")
370 JSONObject modelInfo = buildModelInfo("", serviceResourcesRoot, catalogUtilsVersion)
371 serviceResources.put("modelInfo", modelInfo)
372 JSONArray vnfsArray = parseVnfsJson(serviceResourcesRoot.toString(), "serviceVnfs", catalogUtilsVersion)
373 serviceResources.put("serviceVnfs", vnfsArray)
374 JSONArray networksArray = parseNetworksJson(serviceResourcesRoot.toString(), "serviceNetworks", catalogUtilsVersion)
375 serviceResources.put("serviceNetworks", networksArray)
376 JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "serviceAllottedResources", catalogUtilsVersion)
377 serviceResources.put("serviceAllottedResources", allottedResourcesArray)
378 serviceResourcesObject.put("serviceResources", serviceResources)
379 logger.debug("Returning serviceResources JSON: " + serviceResourcesObject.toString())
381 } catch (Exception e) {
382 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
383 "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(),
384 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
387 return serviceResourcesObject
390 private JSONObject buildModelInfo(String modelType, JSONObject modelFromDb, String catalogUtilsVersion) {
391 JSONObject modelInfo = null
393 modelInfo = new JSONObject()
394 modelInfo.put("modelType", modelType)
395 String modelInvariantId = jsonUtils.getJsonValueForKey(modelFromDb, "modelInvariantUuid")
396 modelInfo.put("modelInvariantId", modelInvariantId)
397 if(modelType.equalsIgnoreCase("allottedResource") || modelType.equalsIgnoreCase("vnf")){
398 String modelInstanceName = jsonUtils.getJsonValueForKey(modelFromDb, "modelInstanceName")
399 modelInfo.put("modelInstanceName", modelInstanceName)
401 if ((!"vfModule".equals(modelType) && !"vnf".equals(modelType)) || !catalogUtilsVersion.equals("v1")) {
402 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "modelUuid")
403 modelInfo.put("modelVersionId", modelVersionId)
406 String modelVersionId = jsonUtils.getJsonValueForKey(modelFromDb, "asdcUuid")
407 modelInfo.put("modelVersionId", modelVersionId)
409 String modelName = jsonUtils.getJsonValueForKey(modelFromDb, "modelName")
410 modelInfo.put("modelName", modelName)
411 String modelVersion = jsonUtils.getJsonValueForKey(modelFromDb, "modelVersion")
412 modelInfo.put("modelVersion", modelVersion)
413 if (!"vfModule".equals(modelType)) {
414 String modelCustomizationName = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationName")
415 modelInfo.put("modelCustomizationName", modelCustomizationName)
417 String modelCustomizationId = jsonUtils.getJsonValueForKey(modelFromDb, "modelCustomizationUuid")
418 switch (catalogUtilsVersion) {
420 modelInfo.put("modelCustomizationId", modelCustomizationId)
423 modelInfo.put("modelCustomizationUuid", modelCustomizationId)
426 JSONObject modelJson = new JSONObject()
427 modelJson.put("modelInfo", modelInfo)
429 catch (Exception e) {
430 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
431 "Exception while parsing model information", "BPMN", MsoLogger.getServiceName(),
432 MsoLogger.ErrorCode.UnknownError.getValue(), e.message);
437 private String getResponseFromCatalogDb (DelegateExecution execution, String endPoint) {
440 String catalogDbEndpoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint",execution)
441 String queryEndpoint = catalogDbEndpoint + "/" + defaultDbAdapterVersion + endPoint
442 def responseData = ''
443 HttpClient client = httpClientFactory.newJsonClient(new URL(queryEndpoint), TargetEntity.CATALOG_DB)
444 client.addAdditionalHeader(ONAPLogConstants.Headers.REQUEST_ID, UUID.randomUUID().toString())
445 client.addAdditionalHeader('X-FromAppId', "BPMN")
446 client.addAdditionalHeader('Accept', MediaType.APPLICATION_JSON)
447 String basicAuthCred = execution.getVariable("BasicAuthHeaderValueDB")
448 client.addAdditionalHeader("Authorization", StringUtils.defaultIfEmpty(basicAuthCred, getBasicDBAuthHeader(execution)))
450 logger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
451 Response response = client.get()
453 responseData = response.readEntity(String.class)
454 if (responseData != null) {
455 logger.debug("Received data from Catalog DB: " + responseData)
458 logger.debug('Response code:' + response.getStatus())
459 logger.debug('Response:' + System.lineSeparator() + responseData)
460 if (response.getStatus() == 200) {
461 // parse response as needed
468 catch (Exception e) {
469 logger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
476 * get resource recipe by resource model uuid and action
478 public JSONObject getResourceRecipe(DelegateExecution execution, String resourceModelUuid, String action) {
479 String endPoint = "/resourceRecipe?resourceModelUuid=" + UriUtils.encode(resourceModelUuid, "UTF-8")+ "&action=" + UriUtils.encode(action, "UTF-8")
480 JSONObject responseJson = null
482 logger.debug("ENDPOINT: " + endPoint)
483 String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
485 if (catalogDbResponse != null) {
486 responseJson = new JSONObject(catalogDbResponse)
489 catch (Exception e) {
490 msoUtils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
497 private String getBasicDBAuthHeader(DelegateExecution execution) {
499 String encodedString = null
501 String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
502 msoUtils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
504 encodedString = msoUtils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
505 execution.setVariable("BasicAuthHeaderValueDB",encodedString)
506 } catch (IOException ex) {
507 String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
508 msoUtils.log("ERROR", dataErrorMessage)