SO catalogDB updates
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / CatalogDbUtils.groovy
index 103326a..91f73e2 100644 (file)
@@ -4,6 +4,8 @@
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 
 package org.onap.so.bpmn.common.scripts
 
-import org.json.JSONObject;
-import org.json.JSONArray;
+import org.onap.so.logger.LoggingAnchor
+import org.apache.commons.lang3.StringUtils
+import org.camunda.bpm.engine.delegate.DelegateExecution
+import org.json.JSONArray
+import org.json.JSONObject
 import org.onap.logging.ref.slf4j.ONAPLogConstants
-import org.onap.so.bpmn.core.UrnPropertiesReader;
-import org.springframework.web.util.UriUtils;
-
+import org.onap.so.bpmn.core.UrnPropertiesReader
 import org.onap.so.bpmn.core.json.JsonUtils
 import org.onap.so.client.HttpClient
-
-import javax.ws.rs.core.MediaType
-import javax.ws.rs.core.Response
-import org.camunda.bpm.engine.delegate.DelegateExecution
-
-import org.onap.so.logger.MsoLogger;
-import org.onap.so.utils.TargetEntity
+import org.onap.so.client.HttpClientFactory
+import org.onap.logging.filter.base.ErrorCode
 import org.onap.so.logger.MessageEnum
 
+import org.slf4j.Logger
+import org.slf4j.LoggerFactory
+import org.onap.logging.filter.base.ONAPComponents;
+import org.springframework.web.util.UriUtils
 
+import javax.ws.rs.core.MediaType
+import javax.ws.rs.core.Response
 
 /***
  * Utilities for accessing Catalog DB Adapter to retrieve Networks, VNF/VFModules, AllottedResources and complete ServiceResources information
@@ -45,38 +49,24 @@ import org.onap.so.logger.MessageEnum
  */
 
 class CatalogDbUtils {
-       private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CatalogDbUtils.class);
-
+    private static final Logger logger = LoggerFactory.getLogger( CatalogDbUtils.class);
 
-       MsoUtils utils = new MsoUtils()
-       JsonUtils jsonUtils = new JsonUtils()
+       private HttpClientFactory httpClientFactory
+       private MsoUtils utils
+       private JsonUtils jsonUtils
        static private String defaultDbAdapterVersion = "v2"
 
-       public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid) {
-               JSONArray vnfsList = null
-               String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
-               try {
-                       msoLogger.debug("ENDPOINT: " + endPoint)
-                       String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
-
-                       if (catalogDbResponse != null) {
-                               vnfsList = parseVnfsJson(catalogDbResponse, "serviceVnfs", "v1")
-                       }
-
-               }
-               catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
-                       throw e
-               }
-
-               return vnfsList
+       CatalogDbUtils(HttpClientFactory httpClientFactory, JsonUtils jsonUtils) {
+               this.httpClientFactory = httpClientFactory
+               this.utils = new MsoUtils()
+               this.jsonUtils = jsonUtils
        }
 
        public JSONArray getAllVnfsByVnfModelCustomizationUuid(DelegateExecution execution, String vnfModelCustomizationUuid, String catalogUtilsVersion) {
                JSONArray vnfsList = null
                String endPoint = "/serviceVnfs?vnfModelCustomizationUuid=" + UriUtils.encode(vnfModelCustomizationUuid, "UTF-8")
                try {
-                       msoLogger.debug("ENDPOINT: " + endPoint)
+                       logger.debug("ENDPOINT: " + endPoint)
                        String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
 
                        if (catalogDbResponse != null) {
@@ -91,7 +81,9 @@ class CatalogDbUtils {
 
                }
                catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in Querying Catalog DB", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                        throw e
                }
 
@@ -114,7 +106,7 @@ class CatalogDbUtils {
                        }
                }
                catch (Exception e) {
-                       utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
+                       logger.error("Exception in Querying Catalog DB: " + e.message)
                        throw e
                }
 
@@ -122,23 +114,16 @@ class CatalogDbUtils {
        }
 
        public String getServiceResourcesByServiceModelInvariantUuidString(DelegateExecution execution, String serviceModelInvariantUuid) {
-               String resources = null
                String endPoint = "/serviceResources?serviceModelInvariantUuid=" + UriUtils.encode(serviceModelInvariantUuid, "UTF-8")
                try {
-                       String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
-
-                       if (catalogDbResponse != null) {
-
-                               resources = catalogDbResponse
-                       }
-
+                       return getResponseFromCatalogDb(execution, endPoint)
                }
                catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in Querying Catalog DB", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                        throw e
                }
-
-               return resources
        }
 
        public JSONObject getServiceResourcesByServiceModelInvariantUuid(DelegateExecution execution, String serviceModelInvariantUuid, String catalogUtilsVersion) {
@@ -158,7 +143,9 @@ class CatalogDbUtils {
 
                }
                catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in Querying Catalog DB", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                        throw e
                }
 
@@ -182,7 +169,9 @@ class CatalogDbUtils {
 
                }
                catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in Querying Catalog DB", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in Querying Catalog DB", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                        throw e
                }
 
@@ -192,7 +181,7 @@ class CatalogDbUtils {
        private JSONArray parseNetworksJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
                JSONArray modelInfos = null
 
-               msoLogger.debug("parseNetworksJson - catalogUtilsVersion is " + catalogUtilsVersion)
+               logger.debug("parseNetworksJson - catalogUtilsVersion is " + catalogUtilsVersion)
                try {
                        // Create array of jsons
 
@@ -227,10 +216,12 @@ class CatalogDbUtils {
                        }
 
                        String modelInfosString = modelInfos.toString()
-                       msoLogger.debug("Returning networks JSON: " + modelInfosString)
+                       logger.debug("Returning networks JSON: " + modelInfosString)
 
                } catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in parsing Catalog DB Response", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                }
 
                return modelInfos
@@ -239,7 +230,7 @@ class CatalogDbUtils {
        private JSONArray parseVnfsJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
                JSONArray modelInfos = null
 
-               msoLogger.debug("parseVnfsJson - catalogUtilsVersion is " + catalogUtilsVersion)
+               logger.debug("parseVnfsJson - catalogUtilsVersion is " + catalogUtilsVersion)
 
                try {
                        // Create array of jsons
@@ -251,7 +242,7 @@ class CatalogDbUtils {
                        for (int i = 0; i < vnfs.length(); i++) {
                                JSONObject vnf = vnfs.getJSONObject(i)
 
-                               msoLogger.debug(vnf.toString(2))
+                               logger.debug(vnf.toString(2))
                                JSONObject modelInfo = buildModelInfo("vnf", vnf, catalogUtilsVersion)
                                JSONObject modelJson = new JSONObject()
                                modelJson.put("modelInfo", modelInfo)
@@ -279,7 +270,7 @@ class CatalogDbUtils {
                                        vfModules = vnf.getJSONArray("vfModules")
                                } catch (Exception e)
                                {
-                                       msoLogger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
+                                       logger.debug("Cannot find VF MODULE ARRAY: " + i + ", exception is " + e.message)
                                }
 
                                if (vfModules != null) {
@@ -304,10 +295,12 @@ class CatalogDbUtils {
                        }
 
                        String modelInfosString = modelInfos.toString()
-                       msoLogger.debug("Returning vnfs JSON: " + modelInfosString)
+                       logger.debug("Returning vnfs JSON: " + modelInfosString)
 
                } catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in parsing Catalog DB Response", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                }
 
                return modelInfos
@@ -316,7 +309,7 @@ class CatalogDbUtils {
        private JSONArray parseAllottedResourcesJson (String catalogDbResponse, String arrayName, String catalogUtilsVersion) {
                JSONArray modelInfos = null
 
-               msoLogger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
+               logger.debug("parseAllottedResourcesJson - catalogUtilsVersion is " + catalogUtilsVersion)
 
                try {
                        // Create array of jsons
@@ -356,10 +349,12 @@ class CatalogDbUtils {
                        }
 
                        String modelInfosString = modelInfos.toString()
-                       msoLogger.debug("Returning allottedResources JSON: " + modelInfosString)
+                       logger.debug("Returning allottedResources JSON: " + modelInfosString)
 
                } catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in parsing Catalog DB Response", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                }
 
                return modelInfos
@@ -383,10 +378,12 @@ class CatalogDbUtils {
                        JSONArray allottedResourcesArray = parseAllottedResourcesJson(serviceResourcesRoot.toString(), "serviceAllottedResources", catalogUtilsVersion)
                        serviceResources.put("serviceAllottedResources", allottedResourcesArray)
                        serviceResourcesObject.put("serviceResources", serviceResources)
-                       msoLogger.debug("Returning serviceResources JSON: " + serviceResourcesObject.toString())
+                       logger.debug("Returning serviceResources JSON: " + serviceResourcesObject.toString())
 
                } catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception in parsing Catalog DB Response", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception in parsing Catalog DB Response", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                }
 
                return serviceResourcesObject
@@ -432,7 +429,9 @@ class CatalogDbUtils {
                        modelJson.put("modelInfo", modelInfo)
                }
                catch (Exception e) {
-                       msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception while parsing model information", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, e.message);
+                       logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
+                                       "Exception while parsing model information", "BPMN",
+                                       ErrorCode.UnknownError.getValue(), e.message);
                }
                return modelInfo
        }
@@ -443,30 +442,22 @@ class CatalogDbUtils {
                        String catalogDbEndpoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint",execution)
                        String queryEndpoint = catalogDbEndpoint + "/" + defaultDbAdapterVersion + endPoint
                        def responseData = ''
-                       def bpmnRequestId = UUID.randomUUID().toString()
-
-                       URL url = new URL(queryEndpoint)
-                       HttpClient client = new HttpClient(url, MediaType.APPLICATION_JSON, TargetEntity.CATALOG_DB)
-                       client.addAdditionalHeader(ONAPLogConstants.Headers.REQUEST_ID, bpmnRequestId)
+                       HttpClient client = httpClientFactory.newJsonClient(new URL(queryEndpoint), ONAPComponents.CATALOG_DB)
                        client.addAdditionalHeader('X-FromAppId', "BPMN")
                        client.addAdditionalHeader('Accept', MediaType.APPLICATION_JSON)
                        String basicAuthCred = execution.getVariable("BasicAuthHeaderValueDB")
-                       if (basicAuthCred != null && !"".equals(basicAuthCred)) {
-                               client.addAdditionalHeader("Authorization", basicAuthCred)
-                       }else {
-                               client.addAdditionalHeader("Authorization", getBasicDBAuthHeader(execution))
-                       }
+                       client.addAdditionalHeader("Authorization", StringUtils.defaultIfEmpty(basicAuthCred, getBasicDBAuthHeader(execution)))
 
-                       msoLogger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
+                       logger.debug('sending GET to Catalog DB endpoint: ' + endPoint)
                        Response response = client.get()
 
                        responseData = response.readEntity(String.class)
                        if (responseData != null) {
-                               msoLogger.debug("Received data from Catalog DB: " + responseData)
+                               logger.debug("Received data from Catalog DB: " + responseData)
                        }
 
-                       msoLogger.debug('Response code:' + response.getStatus())
-                       msoLogger.debug('Response:' + System.lineSeparator() + responseData)
+                       logger.debug('Response code:' + response.getStatus())
+                       logger.debug('Response:' + System.lineSeparator() + responseData)
                        if (response.getStatus() == 200) {
                                // parse response as needed
                                return responseData
@@ -476,7 +467,7 @@ class CatalogDbUtils {
                        }
                }
                catch (Exception e) {
-                       msoLogger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
+                       logger.debug("ERROR WHILE QUERYING CATALOG DB: " + e.message)
                        throw e
                }
 
@@ -489,7 +480,7 @@ class CatalogDbUtils {
                String endPoint = "/resourceRecipe?resourceModelUuid=" + UriUtils.encode(resourceModelUuid, "UTF-8")+ "&action=" + UriUtils.encode(action, "UTF-8")
                JSONObject responseJson = null
                try {
-                       msoLogger.debug("ENDPOINT: " + endPoint)
+                       logger.debug("ENDPOINT: " + endPoint)
                        String catalogDbResponse = getResponseFromCatalogDb(execution, endPoint)
 
                        if (catalogDbResponse != null) {
@@ -497,7 +488,7 @@ class CatalogDbUtils {
                        }
                }
                catch (Exception e) {
-                       utils.log("ERROR", "Exception in Querying Catalog DB: " + e.message)
+                       logger.error("Exception in Querying Catalog DB: " + e.message)
                        throw e
                }
 
@@ -509,15 +500,15 @@ class CatalogDbUtils {
                String encodedString = null
                try {
                        String basicAuthValueDB = UrnPropertiesReader.getVariable("mso.adapters.db.auth", execution)
-                       utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
+                       logger.debug("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB)
 
                        encodedString = utils.getBasicAuth(basicAuthValueDB, UrnPropertiesReader.getVariable("mso.msoKey", execution))
                        execution.setVariable("BasicAuthHeaderValueDB",encodedString)
                } catch (IOException ex) {
                        String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
-                       utils.log("ERROR", dataErrorMessage)
+                       logger.error(dataErrorMessage)
                }
                return encodedString
        }
 
-}
\ No newline at end of file
+}