X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2Freq%2Fsdc%2FSdcCatalogServices.java;h=240094e61db48cf2b1cab4fcd094ca81edcb6659;hb=0cfec3c5d53ed995e66f135ccd584d7ab0950258;hp=14421da5f28ef9b2a16c56c10be420c0d0bbc3f7;hpb=a4096a47f893c1ed0a231303093c3fbba6865641;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java b/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java index 14421da5..240094e6 100644 --- a/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java +++ b/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP CLAMP * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights * reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,8 +17,9 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ + * Modifications copyright (c) 2018 Nokia * =================================================================== - * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * */ package org.onap.clamp.clds.client.req.sdc; @@ -58,25 +59,30 @@ import org.apache.commons.csv.CSVRecord; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHeaders; -import org.onap.clamp.clds.exception.SdcCommunicationException; +import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.exception.sdc.SdcCommunicationException; import org.onap.clamp.clds.model.CldsAlarmCondition; -import org.onap.clamp.clds.model.CldsDBServiceCache; -import org.onap.clamp.clds.model.CldsSdcArtifact; -import org.onap.clamp.clds.model.CldsSdcResource; -import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo; -import org.onap.clamp.clds.model.CldsSdcServiceDetail; -import org.onap.clamp.clds.model.CldsSdcServiceInfo; import org.onap.clamp.clds.model.CldsServiceData; import org.onap.clamp.clds.model.CldsVfData; import org.onap.clamp.clds.model.CldsVfKPIData; import org.onap.clamp.clds.model.CldsVfcData; -import org.onap.clamp.clds.model.prop.Global; -import org.onap.clamp.clds.model.prop.ModelProperties; -import org.onap.clamp.clds.model.refprop.RefProp; +import org.onap.clamp.clds.model.properties.Global; +import org.onap.clamp.clds.model.properties.ModelProperties; +import org.onap.clamp.clds.model.sdc.SdcArtifact; +import org.onap.clamp.clds.model.sdc.SdcResource; +import org.onap.clamp.clds.model.sdc.SdcResourceBasicInfo; +import org.onap.clamp.clds.model.sdc.SdcServiceDetail; +import org.onap.clamp.clds.model.sdc.SdcServiceInfo; +import org.onap.clamp.clds.service.CldsService; import org.onap.clamp.clds.util.CryptoUtils; +import org.onap.clamp.clds.util.JacksonUtils; import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Primary; +import org.springframework.stereotype.Component; +@Component +@Primary public class SdcCatalogServices { private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCatalogServices.class); @@ -92,14 +98,22 @@ public class SdcCatalogServices { private static final String SDC_INSTANCE_ID_CLAMP = "CLAMP-Tool"; private static final String RESOURCE_URL_PREFIX = "resources"; @Autowired - private RefProp refProp; - private CryptoUtils cryptoUtils = new CryptoUtils(); + private ClampProperties refProp; - // returns SDC id and password as a HTTP Basic Auth string (for example: Basic dGVzdDoxMjM0NTY=) + /** + * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic + * dGVzdDoxMjM0NTY=). + * + * @return The String with Basic Auth and password + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password + * @throws DecoderException + * In case of issues with the decoding of the HexString message + */ private String getSdcBasicAuth() throws GeneralSecurityException, DecoderException { String sdcId = refProp.getStringValue("sdc.serviceUsername"); String sdcPw = refProp.getStringValue("sdc.servicePassword"); - String password = cryptoUtils.decrypt(sdcPw); + String password = CryptoUtils.decrypt(sdcPw); String idPw = Base64.getEncoder().encodeToString((sdcId + ":" + password).getBytes(StandardCharsets.UTF_8)); return "Basic " + idPw; } @@ -107,7 +121,7 @@ public class SdcCatalogServices { /** * This method get the SDC services Information with the corresponding * Service UUID. - * + * * @param uuid * The service UUID * @return A Json String with all the service list @@ -123,7 +137,7 @@ public class SdcCatalogServices { LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation"); try { String url = baseUrl; - if (uuid != null) { + if (uuid != null && !uuid.isEmpty()) { url = baseUrl + "/" + uuid + SDC_METADATA_URL_PREFIX; } URL urlObj = new URL(url); @@ -134,7 +148,7 @@ public class SdcCatalogServices { conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId()); conn.setRequestMethod("GET"); String resp = getResponse(conn); - logger.info(resp); + logger.debug("Services list received from SDC:" + resp); // metrics log LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName()); return resp; @@ -151,13 +165,13 @@ public class SdcCatalogServices { /** * To remove duplicate serviceUUIDs from sdc services List. - * + * * @param rawCldsSdcServiceList * A list of CldsSdcServiceInfo * @return A list of CldsSdcServiceInfo without duplicate service UUID */ - public List removeDuplicateServices(List rawCldsSdcServiceList) { - List cldsSdcServiceInfoList = null; + public List removeDuplicateServices(List rawCldsSdcServiceList) { + List cldsSdcServiceInfoList = null; if (rawCldsSdcServiceList != null && !rawCldsSdcServiceList.isEmpty()) { // sort list Collections.sort(rawCldsSdcServiceList); @@ -167,7 +181,7 @@ public class SdcCatalogServices { for (int i = 1; i < rawCldsSdcServiceList.size(); i++) { // compare name with previous - if not equal, then keep the // previous (it's the last with that name) - CldsSdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1); + SdcServiceInfo prev = rawCldsSdcServiceList.get(i - 1); if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) { cldsSdcServiceInfoList.add(prev); } @@ -180,11 +194,12 @@ public class SdcCatalogServices { /** * To remove duplicate serviceUUIDs from sdc resources List. - * + * * @param rawCldsSdcResourceList + * @return List of CldsSdcResource */ - public List removeDuplicateSdcResourceInstances(List rawCldsSdcResourceList) { - List cldsSdcResourceList = null; + public List removeDuplicateSdcResourceInstances(List rawCldsSdcResourceList) { + List cldsSdcResourceList = null; if (rawCldsSdcResourceList != null && !rawCldsSdcResourceList.isEmpty()) { // sort list Collections.sort(rawCldsSdcResourceList); @@ -194,7 +209,7 @@ public class SdcCatalogServices { for (int i = 1; i < rawCldsSdcResourceList.size(); i++) { // compare name with previous - if not equal, then keep the // previous (it's the last with that name) - CldsSdcResource prev = rawCldsSdcResourceList.get(i - 1); + SdcResource prev = rawCldsSdcResourceList.get(i - 1); if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) { cldsSdcResourceList.add(prev); } @@ -207,22 +222,22 @@ public class SdcCatalogServices { /** * To remove duplicate basic resources with same resourceUUIDs. - * + * * @param rawCldsSdcResourceListBasicList + * @return List of CldsSdcResourceBasicInfo */ - public List removeDuplicateSdcResourceBasicInfo( - List rawCldsSdcResourceListBasicList) { - List cldsSdcResourceBasicInfoList = null; + public List removeDuplicateSdcResourceBasicInfo( + List rawCldsSdcResourceListBasicList) { + List cldsSdcResourceBasicInfoList = new ArrayList<>(); if (rawCldsSdcResourceListBasicList != null && !rawCldsSdcResourceListBasicList.isEmpty()) { // sort list Collections.sort(rawCldsSdcResourceListBasicList); // and then take only the resources with the max version (last in // the list with the same name) - cldsSdcResourceBasicInfoList = new ArrayList<>(); for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) { // compare name with previous - if not equal, then keep the // previous (it's the last with that name) - CldsSdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1); + SdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1); if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) { cldsSdcResourceBasicInfoList.add(prev); } @@ -236,7 +251,7 @@ public class SdcCatalogServices { /** * To get ServiceUUID by using serviceInvariantUUID. - * + * * @param invariantId * The invariant ID * @return The service UUID @@ -249,10 +264,10 @@ public class SdcCatalogServices { throws GeneralSecurityException, DecoderException { String serviceUuid = ""; String responseStr = getSdcServicesInformation(null); - List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); - List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); + List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); + List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) { - for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) { + for (SdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) { if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) { serviceUuid = currCldsSdcServiceInfo.getUuid(); @@ -263,28 +278,42 @@ public class SdcCatalogServices { return serviceUuid; } - private List getCldsSdcServicesListFromJson(String jsonStr) { - ObjectMapper objectMapper = new ObjectMapper(); + /** + * To get CldsAsdsServiceInfo class by parsing json string. + * + * @param jsonStr + * The Json string that must be decoded + * @return The list of CldsSdcServiceInfo, if there is a failure it return + * an empty list + */ + private List getCldsSdcServicesListFromJson(String jsonStr) { if (StringUtils.isBlank(jsonStr)) { return new ArrayList<>(); } try { - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class)); + return JacksonUtils.getObjectMapperInstance().readValue(jsonStr, JacksonUtils.getObjectMapperInstance() + .getTypeFactory().constructCollectionType(List.class, SdcServiceInfo.class)); } catch (IOException e) { logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e); return new ArrayList<>(); } } - private List getAllSdcResourcesListFromJson(String jsonStr) { - ObjectMapper objectMapper = new ObjectMapper(); + /** + * To get List of CldsSdcResourceBasicInfo class by parsing json string. + * + * @param jsonStr + * The JSOn string that must be decoded + * @return The list of CldsSdcResourceBasicInfo, an empty list in case of + * issues + */ + private List getAllSdcResourcesListFromJson(String jsonStr) { if (StringUtils.isBlank(jsonStr)) { return new ArrayList<>(); } try { - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); + return JacksonUtils.getObjectMapperInstance().readValue(jsonStr, JacksonUtils.getObjectMapperInstance() + .getTypeFactory().constructCollectionType(List.class, SdcResourceBasicInfo.class)); } catch (IOException e) { logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e); return new ArrayList<>(); @@ -293,21 +322,19 @@ public class SdcCatalogServices { /** * To get CldsSdcServiceDetail by parsing json string. - * + * * @param jsonStr * @return */ - public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) { - ObjectMapper objectMapper = new ObjectMapper(); + public SdcServiceDetail decodeCldsSdcServiceDetailFromJson(String jsonStr) { try { - return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); + return JacksonUtils.getObjectMapperInstance().readValue(jsonStr, SdcServiceDetail.class); } catch (IOException e) { logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e); return null; } } - // upload artifact to sdc based on serviceUUID and resource name on url private String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formattedSdcReq) { // Verify whether it is triggered by Validation Test button from UI @@ -396,23 +423,12 @@ public class SdcCatalogServices { } } - public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) { - try { - CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache(); - cldsDbServiceCache.setCldsDataInstream(cldsServiceData); - cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID()); - cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID()); - return cldsDbServiceCache; - } catch (IOException e) { - logger.error("Exception when getting service in cache", e); - throw new SdcCommunicationException("Exception when getting service in cache", e); - } - } - /** * Check if the SDC Info in cache has expired. - * + * * @param cldsServiceData + * The object representing the service data + * @return boolean flag * @throws GeneralSecurityException * In case of issues with the decryting the encrypted password * @throws DecoderException @@ -420,25 +436,28 @@ public class SdcCatalogServices { */ public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws GeneralSecurityException, DecoderException { - boolean expired = false; if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { String cachedServiceUuid = cldsServiceData.getServiceUUID(); String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID()); - String defaultRecordAge = refProp.getStringValue("CLDS_SERVICE_CACHE_MAX_SECONDS"); - if ((!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null - && cldsServiceData.getAgeOfRecord() > Long.parseLong(defaultRecordAge))) { - expired = true; + String configuredMaxAge = refProp.getStringValue("clds.service.cache.invalidate.after.seconds"); + if (configuredMaxAge == null) { + logger.warn( + "clds.service.cache.invalidate.after.seconds NOT set in clds-reference.properties file, taking 60s as default"); + configuredMaxAge = "60"; } + return (!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null + && cldsServiceData.getAgeOfRecord() > Long.parseLong(configuredMaxAge)); } else { - expired = true; + return true; } - return expired; } /** - * Get the Service Data with Alarm Conditions for a given invariantServiceUuid. - * + * Get the Service Data with Alarm Conditions for a given + * invariantServiceUuid. + * * @param invariantServiceUuid + * @return The CldsServiceData * @throws GeneralSecurityException * In case of issues with the decryting the encrypted password * @throws DecoderException @@ -451,12 +470,12 @@ public class SdcCatalogServices { String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); String serviceDetailUrl = url + "/" + serviceUuid + SDC_METADATA_URL_PREFIX; String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl); - ObjectMapper objectMapper = new ObjectMapper(); CldsServiceData cldsServiceData = new CldsServiceData(); if (responseStr != null) { - CldsSdcServiceDetail cldsSdcServiceDetail; + SdcServiceDetail cldsSdcServiceDetail; try { - cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class); + cldsSdcServiceDetail = JacksonUtils.getObjectMapperInstance().readValue(responseStr, + SdcServiceDetail.class); } catch (IOException e) { logger.error("Exception when decoding the CldsServiceData JSON from SDC", e); throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e); @@ -466,11 +485,11 @@ public class SdcCatalogServices { if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid()); cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID()); - List cldsSdcResourceList = removeDuplicateSdcResourceInstances( + List cldsSdcResourceList = removeDuplicateSdcResourceInstances( cldsSdcServiceDetail.getResources()); if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) { List cldsVfDataList = new ArrayList<>(); - for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) { + for (SdcResource currCldsSdcResource : cldsSdcResourceList) { if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) { CldsVfData currCldsVfData = new CldsVfData(); @@ -482,8 +501,7 @@ public class SdcCatalogServices { cldsServiceData.setCldsVfs(cldsVfDataList); // For each vf in the list , add all vfc's getAllVfcForVfList(cldsVfDataList, catalogUrl); - logger.info("value of cldsServiceData:" + cldsServiceData); - logger.info("value of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID()); + logger.info("Invariant Service ID of cldsServiceData:" + cldsServiceData.getServiceInvariantUUID()); } } } @@ -494,9 +512,8 @@ public class SdcCatalogServices { throws GeneralSecurityException { // todo : refact this.. if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { - List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType( - RESOURCE_VF_TYPE); - List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( + List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_VF_TYPE); + List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( RESOURCE_VFC_TYPE); allVfcResources.addAll(getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_CVFC_TYPE)); for (CldsVfData currCldsVfData : cldsVfDataList) { @@ -551,11 +568,10 @@ public class SdcCatalogServices { } } - private List getVfcDataListFromVfResponse(String vfResponse) throws GeneralSecurityException { - ObjectMapper mapper = new ObjectMapper(); + private List getVfcDataListFromVfResponse(String vfResponse) { ObjectNode vfResponseNode; try { - vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + vfResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfResponse); } catch (IOException e) { logger.error("Exception when decoding the JSON list of CldsVfcData", e); return new ArrayList<>(); @@ -597,8 +613,7 @@ public class SdcCatalogServices { String vfcResourceUUIDUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUUID + SDC_METADATA_URL_PREFIX; try { String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl); - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfcResponse); + ObjectNode vfResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfcResponse); ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); if (vfcArrayNode != null) { for (JsonNode vfcjsonNode : vfcArrayNode) { @@ -622,10 +637,9 @@ public class SdcCatalogServices { private List getAlarmCondtionsFromVfc(String vfcResponse) throws GeneralSecurityException { List cldsAlarmConditionList = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); ObjectNode vfcResponseNode; try { - vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); + vfcResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfcResponse); } catch (IOException e) { logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e); return cldsAlarmConditionList; @@ -667,10 +681,9 @@ public class SdcCatalogServices { // Method to get the artifact for any particular VF private List getFieldPathFromVF(String vfResponse) throws GeneralSecurityException { List cldsVfKPIDataList = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); ObjectNode vfResponseNode; try { - vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + vfResponseNode = (ObjectNode) JacksonUtils.getObjectMapperInstance().readTree(vfResponse); } catch (IOException e) { logger.error("Exception when decoding the JSON list of CldsVfKPIData", e); return cldsVfKPIDataList; @@ -758,6 +771,7 @@ public class SdcCatalogServices { cldsAlarmConditionList.add(cldsAlarmCondition); } + // Get the responses for the current artifact from the artifacts URL. private String getResponsesFromArtifactUrl(String artifactsUrl) { String hostUrl = refProp.getStringValue("sdc.hostUrl"); String artifactsUrlReworked = artifactsUrl.replaceAll("\"", ""); @@ -771,6 +785,10 @@ public class SdcCatalogServices { /** * Service to services/resources/artifacts from sdc.Pass alarmConditions as * true to get alarm conditons from artifact url and else it is false + * + * @param url + * The URL to trigger + * @return The String containing the payload */ public String getCldsServicesOrResourcesBasedOnURL(String url) { Date startTime = new Date(); @@ -798,9 +816,13 @@ public class SdcCatalogServices { logger.error("Exception occurred during query to SDC", e); return ""; } catch (DecoderException e) { + LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Get sdc resources error"); logger.error("Exception when attempting to decode the Hex string", e); throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e); } catch (GeneralSecurityException e) { + LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Get sdc resources error"); logger.error("Exception when attempting to decrypt the encrypted password", e); throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e); } finally { @@ -815,27 +837,28 @@ public class SdcCatalogServices { * @param globalProps * @param cldsServiceData * @return + * @throws IOException + * In case of issues during the parsing of the Global Properties */ - public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) { + public String createPropertiesObjectByUUID(CldsServiceData cldsServiceData) throws IOException { String totalPropsStr; - ObjectMapper mapper = new ObjectMapper(); - ObjectNode globalPropsJson; + ObjectMapper mapper = JacksonUtils.getObjectMapperInstance(); + ObjectNode globalPropsJson = (ObjectNode) refProp.getJsonTemplate(CldsService.GLOBAL_PROPERTIES_KEY); if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { // Objectnode to save all byservice, byvf , byvfc and byalarm nodes ObjectNode byIdObjectNode = mapper.createObjectNode(); // To create vf ResourceUUID node with serviceInvariantUUID - ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(mapper, - cldsServiceData); + ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(cldsServiceData); byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf); // To create byVf and vfcResourceNode with vfResourceUUID - ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs()); + ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(cldsServiceData.getCldsVfs()); byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid); // To create byKpi ObjectNode kpiObjectNode = mapper.createObjectNode(); if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { if (currCldsVfData != null) { - createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList()); + createKpiObjectNodeByVfUuid(kpiObjectNode, currCldsVfData.getCldsKPIList()); } } } @@ -845,8 +868,7 @@ public class SdcCatalogServices { if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { if (currCldsVfData != null) { - createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode, - currCldsVfData.getCldsVfcs()); + createAlarmCondObjectNodeByVfcUuid(vfcResourceUuidObjectNode, currCldsVfData.getCldsVfcs()); } } } @@ -854,38 +876,23 @@ public class SdcCatalogServices { // To create byAlarmCondition with alarmConditionKey List allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData, "alarmCondition"); - ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions); + ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(allAlarmConditions); byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey); // To create byAlertDescription with AlertDescription List allAlertDescriptions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData, "alertDescription"); - ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(mapper, allAlertDescriptions); + ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(allAlertDescriptions); byIdObjectNode.putPOJO("byAlertDescription", alertDescObjectNodeByAlert); - globalPropsJson = decodeGlobalProp(globalProps, mapper); globalPropsJson.putPOJO("shared", byIdObjectNode); - logger.info("value of objNode:" + globalPropsJson); - } else { - /** - * to create json with total properties when no serviceUUID passed - */ - globalPropsJson = decodeGlobalProp(globalProps, mapper); + logger.info("Global properties JSON created with SDC info:" + globalPropsJson); } totalPropsStr = globalPropsJson.toString(); return totalPropsStr; } - private ObjectNode decodeGlobalProp(String globalProps, ObjectMapper mapper) { - try { - return (ObjectNode) mapper.readValue(globalProps, JsonNode.class); - } catch (IOException e) { - logger.error("Exception occurred during decoding of the global props, returning an empty objectNode", e); - return mapper.createObjectNode(); - } - } - /** * Method to get alarm conditions/alert description from Service Data. - * + * * @param cldsServiceData * CldsServiceData the Service Data to analyze * @param eventName @@ -907,7 +914,7 @@ public class SdcCatalogServices { /** * Method to get alarm conditions/alert description from VF Data. - * + * * @param currCldsVfData * The Vf Data to analyze * @param eventName @@ -927,7 +934,7 @@ public class SdcCatalogServices { /** * Method to get alarm conditions/alert description from VFC Data. - * + * * @param currCldsVfcData * The VfC Data to analyze * @param eventName @@ -950,8 +957,8 @@ public class SdcCatalogServices { return alarmCondList; } - private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper, - List cldsAlarmCondList) { + private ObjectNode createAlarmCondObjectNodeByAlarmKey(List cldsAlarmCondList) { + ObjectMapper mapper = JacksonUtils.getObjectMapperInstance(); ObjectNode alarmCondKeyNode = mapper.createObjectNode(); if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) { for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) { @@ -971,7 +978,8 @@ public class SdcCatalogServices { return alarmCondKeyNode; } - private ObjectNode createVfObjectNodeByServiceInvariantUuid(ObjectMapper mapper, CldsServiceData cldsServiceData) { + private ObjectNode createVfObjectNodeByServiceInvariantUuid(CldsServiceData cldsServiceData) { + ObjectMapper mapper = JacksonUtils.getObjectMapperInstance(); ObjectNode invariantUuidObjectNode = mapper.createObjectNode(); ObjectNode vfObjectNode = mapper.createObjectNode(); ObjectNode vfUuidNode = mapper.createObjectNode(); @@ -990,8 +998,9 @@ public class SdcCatalogServices { return invariantUuidObjectNode; } - private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode, + private void createKpiObjectNodeByVfUuid(ObjectNode vfResourceUuidObjectNode, List cldsVfKpiDataList) { + ObjectMapper mapper = JacksonUtils.getObjectMapperInstance(); if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) { for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) { if (currCldsVfKpiData != null) { @@ -1009,8 +1018,9 @@ public class SdcCatalogServices { } } - private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUuidObjectNode, + private void createAlarmCondObjectNodeByVfcUuid(ObjectNode vfcResourceUuidObjectNode, List cldsVfcDataList) { + ObjectMapper mapper = JacksonUtils.getObjectMapperInstance(); ObjectNode vfcObjectNode = mapper.createObjectNode(); ObjectNode alarmCondNode = mapper.createObjectNode(); ObjectNode alertDescNode = mapper.createObjectNode(); @@ -1045,12 +1055,13 @@ public class SdcCatalogServices { /** * Method to create vfc and kpi nodes inside vf node - * + * * @param mapper * @param cldsVfDataList * @return */ - private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List cldsVfDataList) { + private ObjectNode createVfcObjectNodeByVfUuid(List cldsVfDataList) { + ObjectMapper mapper = JacksonUtils.getObjectMapperInstance(); ObjectNode vfUuidObjectNode = mapper.createObjectNode(); if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { for (CldsVfData currCldsVfData : cldsVfDataList) { @@ -1095,25 +1106,25 @@ public class SdcCatalogServices { /** * This method searches the equivalent artifact UUID for a specific * artifactName in a SdcServiceDetail. - * + * * @param cldsSdcServiceDetail * The SdcServiceDetail that will be analyzed * @param artifactName * The artifact name that will be searched * @return The artifact UUID found */ - public String getArtifactIdIfArtifactAlreadyExists(CldsSdcServiceDetail cldsSdcServiceDetail, String artifactName) { + public String getArtifactIdIfArtifactAlreadyExists(SdcServiceDetail cldsSdcServiceDetail, String artifactName) { String artifactUuid = null; boolean artifactExists = false; if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null && !cldsSdcServiceDetail.getResources().isEmpty()) { - for (CldsSdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) { + for (SdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) { if (artifactExists) { break; } if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null && !currCldsSdcResource.getArtifacts().isEmpty()) { - for (CldsSdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) { + for (SdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) { if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) { artifactUuid = currCldsSdcArtifact.getArtifactUUID(); @@ -1127,7 +1138,8 @@ public class SdcCatalogServices { return artifactUuid; } - private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) { + // To get all sdc VF/VFC Resources basic info. + private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) { String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME); String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType; String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl); @@ -1135,10 +1147,10 @@ public class SdcCatalogServices { } private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid, - List resourceInfoList) { + List resourceInfoList) { String resourceUuid = null; if (resourceInfoList != null && !resourceInfoList.isEmpty()) { - for (CldsSdcResourceBasicInfo currResource : resourceInfoList) { + for (SdcResourceBasicInfo currResource : resourceInfoList) { if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) { resourceUuid = currResource.getUuid(); @@ -1149,6 +1161,7 @@ public class SdcCatalogServices { return resourceUuid; } + // Method to get service invariant uuid from model properties. private String getServiceInvariantUuidFromProps(ModelProperties props) { String invariantUuid = ""; Global globalProps = props.getGlobal(); @@ -1160,7 +1173,7 @@ public class SdcCatalogServices { /** * This method upload the BluePrint to SDC. - * + * * @param prop * The Clds model Properties * @param userid @@ -1192,7 +1205,7 @@ public class SdcCatalogServices { String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid); logger.info("ServiceUUID used before upload in url:" + originalServiceUuid); String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid); - CldsSdcServiceDetail cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation); + SdcServiceDetail cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation); String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, artifactName); // Upload artifacts to sdc @@ -1205,7 +1218,7 @@ public class SdcCatalogServices { } logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUuid); sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid); - cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation); + cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation); uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, locationArtifactName); // To send location information also to sdc @@ -1216,4 +1229,77 @@ public class SdcCatalogServices { } } } + + /** + * Method to delete blueprint and location json artifacts from sdc + * + * @param prop + * @param userid + * @param sdcReqUrlsList + * @param artifactName + * @param locationArtifactName + * @throws GeneralSecurityException + * @throws DecoderException + */ + public void deleteArtifactsFromSdc(ModelProperties prop, String userid, List sdcReqUrlsList, + String artifactName, String locationArtifactName) throws GeneralSecurityException, DecoderException { + String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop); + for (String url : sdcReqUrlsList) { + String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid); + logger.info("ServiceUUID used before deleting in url:" + originalServiceUuid); + String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid); + SdcServiceDetail cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation); + String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, artifactName); + String responseStr = deleteArtifact(userid, url, uploadedArtifactUuid); + logger.info("value of sdc Response of deleting blueprint from sdc :" + responseStr); + String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid); + if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) { + url = url.replace(originalServiceUuid, updatedServiceUuid); + } + logger.info("ServiceUUID used after delete in ulr:" + updatedServiceUuid); + sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid); + cldsSdcServiceDetail = decodeCldsSdcServiceDetailFromJson(sdcServicesInformation); + uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, locationArtifactName); + responseStr = deleteArtifact(userid, url, uploadedArtifactUuid); + logger.info("value of sdc Response of deleting location json from sdc :" + responseStr); + } + } + + private String deleteArtifact(String userid, String url, String uploadedArtifactUuid) { + try { + String responseStr = ""; + if (uploadedArtifactUuid != null && !uploadedArtifactUuid.isEmpty()) { + logger.info("userid=" + userid); + String basicAuth = getSdcBasicAuth(); + String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID"); + url = url + "/" + uploadedArtifactUuid; + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); + conn.setDoOutput(true); + conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), sdcXonapInstanceId); + conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth); + conn.setRequestProperty("USER_ID", userid); + conn.setRequestMethod("DELETE"); + conn.setRequestProperty("charset", "utf-8"); + conn.setUseCaches(false); + conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), + LoggingUtils.getRequestId()); + boolean requestFailed = true; + int responseCode = conn.getResponseCode(); + logger.info("responseCode=" + responseCode); + if (responseCode == 200) { + requestFailed = false; + } + responseStr = getResponse(conn); + if (responseStr != null && requestFailed) { + logger.error("requestFailed - responseStr=" + responseStr); + throw new BadRequestException(responseStr); + } + } + return responseStr; + } catch (IOException | DecoderException | GeneralSecurityException e) { + logger.error("Exception when attempting to communicate with SDC", e); + throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e); + } + } }