X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FSdcCatalogServices.java;h=e3d10c756c619cb48cad0720b12f023f0c22a73a;hb=2b15edd224cc6dcff90e170959102eb1a1bb1a50;hp=a4e332a39d8e542d4ae5d6067587741bc7aebd71;hpb=081886e65389c4d513033c7722918cda98bed7dc;p=clamp.git diff --git a/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java b/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java index a4e332a3..e3d10c75 100644 --- a/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java +++ b/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java @@ -25,7 +25,6 @@ package org.onap.clamp.clds.client; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -53,6 +52,7 @@ import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVRecord; import org.apache.commons.lang3.StringUtils; import org.onap.clamp.clds.client.req.SdcReq; +import org.onap.clamp.clds.exception.SdcCommunicationException; import org.onap.clamp.clds.model.CldsAlarmCondition; import org.onap.clamp.clds.model.CldsDBServiceCache; import org.onap.clamp.clds.model.CldsSdcArtifact; @@ -137,7 +137,7 @@ public class SdcCatalogServices { */ public List removeDuplicateServices(List rawCldsSdcServiceList) { List cldsSdcServiceInfoList = null; - if (rawCldsSdcServiceList != null && rawCldsSdcServiceList.size() > 0) { + if (rawCldsSdcServiceList != null && !rawCldsSdcServiceList.isEmpty()) { // sort list Collections.sort(rawCldsSdcServiceList); // and then take only the services with the max version (last in the @@ -165,7 +165,7 @@ public class SdcCatalogServices { */ public List removeDuplicateSdcResourceInstances(List rawCldsSdcResourceList) { List cldsSdcResourceList = null; - if (rawCldsSdcResourceList != null && rawCldsSdcResourceList.size() > 0) { + if (rawCldsSdcResourceList != null && !rawCldsSdcResourceList.isEmpty()) { // sort list Collections.sort(rawCldsSdcResourceList); // and then take only the resources with the max version (last in @@ -194,7 +194,7 @@ public class SdcCatalogServices { public List removeDuplicateSdcResourceBasicInfo( List rawCldsSdcResourceListBasicList) { List cldsSdcResourceBasicInfoList = null; - if (rawCldsSdcResourceListBasicList != null && rawCldsSdcResourceListBasicList.size() > 0) { + if (rawCldsSdcResourceListBasicList != null && !rawCldsSdcResourceListBasicList.isEmpty()) { // sort list Collections.sort(rawCldsSdcResourceListBasicList); // and then take only the resources with the max version (last in @@ -221,15 +221,13 @@ public class SdcCatalogServices { * @param invariantId * The invariant ID * @return The service UUID - * @throws IOException - * In case of issues with the JSON decoder */ - public String getServiceUuidFromServiceInvariantId(String invariantId) throws IOException { + public String getServiceUuidFromServiceInvariantId(String invariantId) { String serviceUuid = ""; String responseStr = getSdcServicesInformation(null); List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); - if (cldsSdcServicesList != null && cldsSdcServicesList.size() > 0) { + if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) { for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) { if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) { @@ -245,32 +243,45 @@ public class SdcCatalogServices { * To get CldsAsdsServiceInfo class by parsing json string. * * @param jsonStr - * @return - * @throws IOException + * The Json string that must be decoded + * @return The list of CldsSdcServiceInfo, if there is a failure it return + * an empty list */ - public List getCldsSdcServicesListFromJson(String jsonStr) throws IOException { + public List getCldsSdcServicesListFromJson(String jsonStr) { ObjectMapper objectMapper = new ObjectMapper(); if (StringUtils.isBlank(jsonStr)) { - return null; + return new ArrayList<>(); + } + try { + return objectMapper.readValue(jsonStr, + objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class)); + } catch (IOException e) { + logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e); + return new ArrayList<>(); } - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class)); } /** * To get List of CldsSdcResourceBasicInfo class by parsing json string. * * @param jsonStr - * @return - * @throws IOException + * The JSOn string that must be decoded + * @return The list of CldsSdcResourceBasicInfo, an empty list in case of + * issues */ - public List getAllSdcResourcesListFromJson(String jsonStr) throws IOException { + public List getAllSdcResourcesListFromJson(String jsonStr) { ObjectMapper objectMapper = new ObjectMapper(); if (StringUtils.isBlank(jsonStr)) { - return null; + return new ArrayList<>(); + } + + try { + return objectMapper.readValue(jsonStr, + objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); + } catch (IOException e) { + logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e); + return new ArrayList<>(); } - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); } /** @@ -290,76 +301,86 @@ public class SdcCatalogServices { * * @param jsonStr * @return - * @throws IOException */ - public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) throws IOException { + public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) { ObjectMapper objectMapper = new ObjectMapper(); - return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); + try { + return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); + } catch (IOException e) { + logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e); + return null; + } } /** - * To upload artifact to sdc based on serviceUUID and resourcename on url. + * To upload artifact to sdc based on serviceUUID and resource name on url. * * @param prop * @param userid * @param url - * @param formatttedSdcReq + * @param formattedSdcReq * @return - * @throws IOException */ - public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatttedSdcReq) - throws IOException { + public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatttedSdcReq) { // Verify whether it is triggered by Validation Test button from UI if (prop.isTest()) { return "sdc artifact upload not executed for test action"; } - logger.info("userid=" + userid); - String md5Text = SdcReq.calculateMD5ByString(formatttedSdcReq); - byte[] postData = SdcReq.stringToByteArray(formatttedSdcReq); - int postDataLength = postData.length; - HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, md5Text); - try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { - wr.write(postData); - } - boolean requestFailed = true; - int responseCode = conn.getResponseCode(); - logger.info("responseCode=" + responseCode); - if (responseCode == 200) { - requestFailed = false; - } + try { + logger.info("userid=" + userid); + String md5Text = SdcReq.calculateMD5ByString(formatttedSdcReq); + byte[] postData = SdcReq.stringToByteArray(formatttedSdcReq); + int postDataLength = postData.length; + HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, md5Text); + try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { + wr.write(postData); + } + boolean requestFailed = true; + int responseCode = conn.getResponseCode(); + logger.info("responseCode=" + responseCode); + if (responseCode == 200) { + requestFailed = false; + } - String responseStr = getResponse(conn); - if (responseStr != null) { - if (requestFailed) { + String responseStr = getResponse(conn); + if (responseStr != null && requestFailed) { logger.error("requestFailed - responseStr=" + responseStr); throw new BadRequestException(responseStr); } + return responseStr; + } catch (IOException e) { + logger.error("Exception when attempting to communicate with SDC", e); + throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e); } - return responseStr; + } - private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text) - throws IOException { - logger.info("userid=" + userid); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); - String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID"); - URL urlObj = new URL(url); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - conn.setDoOutput(true); - conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), sdcXonapInstanceId); - conn.setRequestProperty("Authorization", basicAuth); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Content-MD5", md5Text); - conn.setRequestProperty("USER_ID", userid); - conn.setRequestMethod("POST"); - conn.setRequestProperty("charset", "utf-8"); - conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); - conn.setUseCaches(false); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - return conn; + private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text) { + try { + logger.info("userid=" + userid); + String basicAuth = SdcReq.getSdcBasicAuth(refProp); + String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID"); + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); + conn.setDoOutput(true); + conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), sdcXonapInstanceId); + conn.setRequestProperty("Authorization", basicAuth); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("Content-MD5", md5Text); + conn.setRequestProperty("USER_ID", userid); + conn.setRequestMethod("POST"); + conn.setRequestProperty("charset", "utf-8"); + conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); + conn.setUseCaches(false); + conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); + return conn; + } catch (IOException e) { + logger.error("Exception when attempting to open connection with SDC", e); + throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e); + } } - private String getResponse(HttpURLConnection conn) throws IOException { + private String getResponse(HttpURLConnection conn) { try (InputStream is = getInputStream(conn)) { if (is != null) { try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) { @@ -370,29 +391,42 @@ public class SdcCatalogServices { } return response.toString(); } + } else { + return null; } + } catch (IOException e) { + logger.error("Exception when attempting to open SDC response", e); + throw new SdcCommunicationException("Exception when attempting to open SDC response", e); } - return null; } - private InputStream getInputStream(HttpURLConnection conn) throws IOException { - InputStream inStream = conn.getErrorStream(); - if (inStream == null) { - inStream = conn.getInputStream(); + private InputStream getInputStream(HttpURLConnection conn) { + try { + InputStream inStream = conn.getErrorStream(); + if (inStream == null) { + inStream = conn.getInputStream(); + } + return inStream; + } catch (IOException e) { + logger.error("Exception when attempting to open SDC error stream", e); + throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e); } - return inStream; } - public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) - throws IOException { - CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache(); - cldsDbServiceCache.setCldsDataInstream(cldsServiceData); - cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID()); - cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID()); - return cldsDbServiceCache; + 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); + } } - public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws IOException { + public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) { boolean expired = false; if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { String cachedServiceUuid = cldsServiceData.getServiceUUID(); @@ -408,7 +442,7 @@ public class SdcCatalogServices { return expired; } - public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) throws IOException { + public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) { String url = refProp.getStringValue("sdc.serviceUrl"); String catalogUrl = refProp.getStringValue("sdc.catalog.url"); String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); @@ -417,7 +451,13 @@ public class SdcCatalogServices { ObjectMapper objectMapper = new ObjectMapper(); CldsServiceData cldsServiceData = new CldsServiceData(); if (responseStr != null) { - CldsSdcServiceDetail cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class); + CldsSdcServiceDetail cldsSdcServiceDetail; + try { + cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.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); + } cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid()); cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID()); @@ -426,11 +466,11 @@ public class SdcCatalogServices { if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { List cldsSdcResourceList = removeDuplicateSdcResourceInstances( cldsSdcServiceDetail.getResources()); - if (cldsSdcResourceList != null && cldsSdcResourceList.size() > 0) { + if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) { List cldsVfDataList = new ArrayList<>(); for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) { if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null - && currCldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) { + && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) { CldsVfData currCldsVfData = new CldsVfData(); currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName()); currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID()); @@ -448,13 +488,9 @@ public class SdcCatalogServices { return cldsServiceData; } - /** - * @param cldsVfDataList - * @throws IOException - */ - private void getAllVfcForVfList(List cldsVfDataList, String catalogUrl) throws IOException { + private void getAllVfcForVfList(List cldsVfDataList, String catalogUrl) { // todo : refact this.. - if (cldsVfDataList != null && cldsVfDataList.size() > 0) { + if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType( RESOURCE_VF_TYPE); List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( @@ -475,7 +511,7 @@ public class SdcCatalogServices { List vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse); if (vfcDataListFromVfResponse != null) { currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse); - if (vfcDataListFromVfResponse.size() > 0) { + if (!vfcDataListFromVfResponse.isEmpty()) { // To get artifacts for every VFC and get // alarm conditions from artifact for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) { @@ -511,9 +547,15 @@ public class SdcCatalogServices { } } - private List getVfcDataListFromVfResponse(String vfResponse) throws IOException { + private List getVfcDataListFromVfResponse(String vfResponse) { ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + ObjectNode vfResponseNode; + try { + vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + } catch (IOException e) { + logger.error("Exception when decoding the JSON list of CldsVfcData", e); + return new ArrayList<>(); + } ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); List cldsVfcDataList = new ArrayList<>(); if (vfcArrayNode != null) { @@ -537,7 +579,7 @@ public class SdcCatalogServices { private List getVFCfromCVFC(String resourceUUID) { String catalogUrl = refProp.getStringValue("sdc.catalog.url"); - List cldsVfcDataList = new ArrayList(); + List cldsVfcDataList = new ArrayList<>(); if (resourceUUID != null) { String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata"; @@ -569,16 +611,19 @@ public class SdcCatalogServices { } private String removeUnwantedBracesFromString(String id) { - if (id != null && id.contains("\"")) { - id = id.replaceAll("\"", ""); - } - return id; + return (id != null) ? id.replaceAll("\"", "") : ""; } - private List getAlarmCondtionsFromVfc(String vfcResponse) throws IOException { + private List getAlarmCondtionsFromVfc(String vfcResponse) { List cldsAlarmConditionList = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); + ObjectNode vfcResponseNode; + try { + vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); + } catch (IOException e) { + logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e); + return cldsAlarmConditionList; + } ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts"); if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) { @@ -595,25 +640,36 @@ public class SdcCatalogServices { return cldsAlarmConditionList; } - private List parseCsvToGetAlarmConditions(String allAlarmCondsValues) throws IOException { - List cldsAlarmConditionList = new ArrayList<>(); - Reader alarmReader = new StringReader(allAlarmCondsValues); - Iterable records = CSVFormat.RFC4180.parse(alarmReader); - if (records != null) { - Iterator it = records.iterator(); - if (it.hasNext()) { - it.next(); + private List parseCsvToGetAlarmConditions(String allAlarmCondsValues) { + try { + List cldsAlarmConditionList = new ArrayList<>(); + Reader alarmReader = new StringReader(allAlarmCondsValues); + Iterable records = CSVFormat.RFC4180.parse(alarmReader); + if (records != null) { + Iterator it = records.iterator(); + if (it.hasNext()) { + it.next(); + } + it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record)); } - it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record)); + return cldsAlarmConditionList; + } catch (IOException e) { + logger.error("Exception when attempting to parse the CSV containing the alarm", e); + return new ArrayList<>(); } - return cldsAlarmConditionList; } // Method to get the artifact for any particular VF - private List getFieldPathFromVF(String vfResponse) throws JsonProcessingException, IOException { - List cldsVfKPIDataList = new ArrayList(); + private List getFieldPathFromVF(String vfResponse) { + List cldsVfKPIDataList = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + ObjectNode vfResponseNode; + try { + vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + } catch (IOException e) { + logger.error("Exception when decoding the JSON list of CldsVfKPIData", e); + return cldsVfKPIDataList; + } ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts"); if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) { @@ -624,7 +680,7 @@ public class SdcCatalogServices { String artifactName = ""; if (artifactNameNode != null) { artifactName = artifactNameNode.textValue(); - artifactName = artifactName.substring(artifactName.lastIndexOf(".") + 1); + artifactName = artifactName.substring(artifactName.lastIndexOf('.') + 1); } if (artifactUrlNode != null && artifactName != null && !artifactName.isEmpty() && artifactName.equalsIgnoreCase("csv")) { @@ -663,19 +719,24 @@ public class SdcCatalogServices { } // Method to get the artifactURL Data and set the CldsVfKPIData node - private List parseCsvToGetFieldPath(String allFieldPathValues) throws IOException { - List cldsVfKPIDataList = new ArrayList(); - Reader alarmReader = new StringReader(allFieldPathValues); - Iterable records = CSVFormat.RFC4180.parse(alarmReader); - if (records != null) { - for (CSVRecord record : records) { - CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record); - if (kpiData != null) { - cldsVfKPIDataList.add(kpiData); + private List parseCsvToGetFieldPath(String allFieldPathValues) { + try { + List cldsVfKPIDataList = new ArrayList<>(); + Reader alarmReader = new StringReader(allFieldPathValues); + Iterable records = CSVFormat.RFC4180.parse(alarmReader); + if (records != null) { + for (CSVRecord record : records) { + CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record); + if (kpiData != null) { + cldsVfKPIDataList.add(kpiData); + } } } + return cldsVfKPIDataList; + } catch (IOException e) { + logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e); + return new ArrayList<>(); } - return cldsVfKPIDataList; } private void processRecord(List cldsAlarmConditionList, CSVRecord record) { @@ -701,8 +762,8 @@ public class SdcCatalogServices { public String getResponsesFromArtifactUrl(String artifactsUrl) { String hostUrl = refProp.getStringValue("sdc.hostUrl"); - artifactsUrl = artifactsUrl.replaceAll("\"", ""); - String artifactUrl = hostUrl + artifactsUrl; + String artifactsUrlReworked = artifactsUrl.replaceAll("\"", ""); + String artifactUrl = hostUrl + artifactsUrlReworked; logger.info("value of artifactURl:" + artifactUrl); String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true); logger.info("value of artifactResponse:" + currArtifactResponse); @@ -716,12 +777,13 @@ public class SdcCatalogServices { * @param url * @param alarmConditions * @return - * @throws IOException */ public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) { + Date startTime = new Date(); try { - url = removeUnwantedBracesFromString(url); - URL urlObj = new URL(url); + LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL"); + String urlReworked = removeUnwantedBracesFromString(url); + URL urlObj = new URL(urlReworked); HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); String basicAuth = SdcReq.getSdcBasicAuth(refProp); @@ -732,22 +794,30 @@ public class SdcCatalogServices { conn.setRequestMethod("GET"); int responseCode = conn.getResponseCode(); - logger.info("responseCode=" + responseCode); + logger.info("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode); StringBuilder response; try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { response = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { - response.append(inputLine); + if (!inputLine.isEmpty()) { + response.append(inputLine); + } if (alarmConditions) { response.append("\n"); } } } + LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName()); return response.toString(); } catch (IOException e) { + LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Get sdc resources error"); logger.error("Exception occurred during query to SDC", e); return ""; + } finally { + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed"); } } @@ -758,9 +828,8 @@ public class SdcCatalogServices { * @param globalProps * @param cldsServiceData * @return - * @throws IOException */ - public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) throws IOException { + public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) { String totalPropsStr; ObjectMapper mapper = new ObjectMapper(); ObjectNode globalPropsJson; @@ -780,7 +849,7 @@ public class SdcCatalogServices { // To create byKpi ObjectNode kpiObjectNode = mapper.createObjectNode(); - if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) { + if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { if (currCldsVfData != null) { createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList()); @@ -791,7 +860,7 @@ public class SdcCatalogServices { // To create byVfc and alarmCondition with vfcResourceUUID ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode(); - if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) { + if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { if (currCldsVfData != null) { createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode, @@ -815,20 +884,29 @@ public class SdcCatalogServices { byIdObjectNode.putPOJO("byAlertDescription", alertDescObjectNodeByAlert); - globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class); + globalPropsJson = decodeGlobalProp(globalProps, mapper); globalPropsJson.putPOJO("shared", byIdObjectNode); - logger.info("valuie of objNode:" + globalPropsJson); + logger.info("value of objNode:" + globalPropsJson); } else { /** * to create json with total properties when no serviceUUID passed */ - globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class); + globalPropsJson = decodeGlobalProp(globalProps, mapper); } 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. * @@ -902,7 +980,7 @@ public class SdcCatalogServices { List cldsAlarmCondList) { ObjectNode alarmCondKeyNode = mapper.createObjectNode(); - if (cldsAlarmCondList != null && cldsAlarmCondList.size() > 0) { + if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) { for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) { if (currCldsAlarmCondition != null) { ObjectNode alarmCondNode = mapper.createObjectNode(); @@ -925,7 +1003,7 @@ public class SdcCatalogServices { ObjectNode vfObjectNode = mapper.createObjectNode(); ObjectNode vfUuidNode = mapper.createObjectNode(); List cldsVfsList = cldsServiceData.getCldsVfs(); - if (cldsVfsList != null && cldsVfsList.size() > 0) { + if (cldsVfsList != null && !cldsVfsList.isEmpty()) { for (CldsVfData currCldsVfData : cldsVfsList) { if (currCldsVfData != null) { vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName()); @@ -941,7 +1019,7 @@ public class SdcCatalogServices { private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode, List cldsVfKpiDataList) { - if (cldsVfKpiDataList != null && cldsVfKpiDataList.size() > 0) { + if (cldsVfKpiDataList != null && !cldsVfKpiDataList.isEmpty()) { for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) { if (currCldsVfKpiData != null) { ObjectNode thresholdNameObjectNode = mapper.createObjectNode(); @@ -967,11 +1045,11 @@ public class SdcCatalogServices { ObjectNode vfcObjectNode = mapper.createObjectNode(); ObjectNode alarmCondNode = mapper.createObjectNode(); ObjectNode alertDescNode = mapper.createObjectNode(); - if (cldsVfcDataList != null && cldsVfcDataList.size() > 0) { + if (cldsVfcDataList != null && !cldsVfcDataList.isEmpty()) { for (CldsVfcData currCldsVfcData : cldsVfcDataList) { if (currCldsVfcData != null) { if (currCldsVfcData.getCldsAlarmConditions() != null - && currCldsVfcData.getCldsAlarmConditions().size() > 0) { + && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), currCldsAlarmCondition.getAlarmConditionKey()); @@ -984,6 +1062,7 @@ public class SdcCatalogServices { } } } + vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); vfcObjectNode.putPOJO("alertDescription", alertDescNode); vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode); @@ -998,15 +1077,23 @@ 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) { ObjectNode vfUuidObjectNode = mapper.createObjectNode(); - if (cldsVfDataList != null && cldsVfDataList.size() > 0) { + if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { for (CldsVfData currCldsVfData : cldsVfDataList) { if (currCldsVfData != null) { - ObjectNode vfcObjectNode = mapper.createObjectNode(); + ObjectNode vfObjectNode = mapper.createObjectNode(); ObjectNode vfcUuidNode = mapper.createObjectNode(); - if (currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) { + ObjectNode kpiObjectNode = mapper.createObjectNode(); + if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) { for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), currCldsVfcData.getVfcName()); @@ -1014,8 +1101,17 @@ public class SdcCatalogServices { } else { vfcUuidNode.put("", ""); } - vfcObjectNode.putPOJO("vfc", vfcUuidNode); - vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfcObjectNode); + if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) { + for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) { + kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(), + currCldsVfKPIData.getThresholdValue()); + } + } else { + kpiObjectNode.put("", ""); + } + vfObjectNode.putPOJO("vfc", vfcUuidNode); + vfObjectNode.putPOJO("kpi", kpiObjectNode); + vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode); } } } else { @@ -1050,12 +1146,11 @@ public class SdcCatalogServices { if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null && !currCldsSdcResource.getArtifacts().isEmpty()) { for (CldsSdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) { - if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null) { - if (currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) { - artifactUuid = currCldsSdcArtifact.getArtifactUUID(); - artifactExists = true; - break; - } + if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null + && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) { + artifactUuid = currCldsSdcArtifact.getArtifactUUID(); + artifactExists = true; + break; } } } @@ -1109,24 +1204,18 @@ public class SdcCatalogServices { * @param resourceType * The resourceType * @return The list of CldsSdcResourceBasicInfo - * @throws IOException - * In case of issues with the Streams */ - private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) - throws IOException { - List allSdcResourceVfcBasicInfo = new ArrayList(); + private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) { String catalogUrl = refProp.getStringValue("sdc.catalog.url"); String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType; String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false); - - allSdcResourceVfcBasicInfo = getAllSdcResourcesListFromJson(allSdcVfcResources); - return removeDuplicateSdcResourceBasicInfo(allSdcResourceVfcBasicInfo); + return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources)); } private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid, - List resourceInfoList) throws IOException { + List resourceInfoList) { String resourceUuid = null; - if (resourceInfoList != null && resourceInfoList.size() > 0) { + if (resourceInfoList != null && !resourceInfoList.isEmpty()) { for (CldsSdcResourceBasicInfo currResource : resourceInfoList) { if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) { @@ -1172,12 +1261,10 @@ public class SdcCatalogServices { * @param locationArtifactName * The location artifact name from where we can get the Artifact * UUID - * @throws IOException - * In case of issues with the streams * */ public void uploadToSdc(ModelProperties prop, String userid, List sdcReqUrlsList, String formattedSdcReq, - String formattedSdcLocationReq, String artifactName, String locationArtifactName) throws IOException { + String formattedSdcLocationReq, String artifactName, String locationArtifactName) { logger.info("userid=" + userid); String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop); if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) {