X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2FSdcCatalogServices.java;h=36265e83790b5abf3bb4c06f92f362f6e4914de8;hb=434170f50621917a7fb2cbe7c7b01c4b29a8211e;hp=d418ea55b8158096906e39ee18362d301d118962;hpb=7104181e0ebd97bbd034b5c221561d42e4241fe5;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 d418ea55..36265e83 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; @@ -41,6 +40,7 @@ import java.io.Reader; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URL; +import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -53,6 +53,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; @@ -71,14 +72,16 @@ import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.beans.factory.annotation.Autowired; public class SdcCatalogServices { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCatalogServices.class); - protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - - private static final String RESOURCE_VF_TYPE = "VF"; - private static final String RESOURCE_VFC_TYPE = "VFC"; - + protected static final EELFLogger logger = EELFManager.getInstance() + .getLogger(SdcCatalogServices.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + private static final String RESOURCE_VF_TYPE = "VF"; + private static final String RESOURCE_VFC_TYPE = "VFC"; + private static final String RESOURCE_CVFC_TYPE = "CVFC"; @Autowired private RefProp refProp; + @Autowired + private SdcReq sdcReq; /** * This method get the SDC services Information with the corresponding @@ -87,31 +90,29 @@ public class SdcCatalogServices { * @param uuid * The service UUID * @return A Json String with all the service list + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password */ - public String getSdcServicesInformation(String uuid) { + public String getSdcServicesInformation(String uuid) throws GeneralSecurityException { Date startTime = new Date(); String baseUrl = refProp.getStringValue("sdc.serviceUrl"); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); + String basicAuth = sdcReq.getSdcBasicAuth(); LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation"); - try { String url = baseUrl; if (uuid != null) { url = baseUrl + "/" + uuid + "/metadata"; } URL urlObj = new URL(url); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool"); conn.setRequestProperty("Authorization", basicAuth); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); conn.setRequestMethod("GET"); - String resp = getResponse(conn); if (resp != null) { - logger.info(resp.toString()); + logger.info(resp); // metrics log LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName()); return resp; @@ -124,20 +125,19 @@ public class SdcCatalogServices { LoggingUtils.setTimeContext(startTime, new Date()); metricsLogger.info("getSdcServicesInformation complete"); } - return ""; } /** * 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; - 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 @@ -159,13 +159,13 @@ public class SdcCatalogServices { /** * To remove duplicate serviceUUIDs from sdc resources List. - * + * * @param rawCldsSdcResourceList * @return */ 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 @@ -187,14 +187,14 @@ public class SdcCatalogServices { /** * To remove duplicate basic resources with same resourceUUIDs. - * + * * @param rawCldsSdcResourceListBasicList * @return */ 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 @@ -217,19 +217,19 @@ public class SdcCatalogServices { /** * To get ServiceUUID by using serviceInvariantUUID. - * + * * @param invariantId * The invariant ID * @return The service UUID - * @throws IOException - * In case of issues with the JSON decoder + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password */ - public String getServiceUuidFromServiceInvariantId(String invariantId) throws IOException { + public String getServiceUuidFromServiceInvariantId(String invariantId) throws GeneralSecurityException { 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)) { @@ -243,39 +243,51 @@ 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)); } /** * To get CldsAsdsResource class by parsing json string. - * + * * @param jsonStr * @return * @throws IOException @@ -287,79 +299,90 @@ public class SdcCatalogServices { /** * To get CldsSdcServiceDetail by parsing json string. - * + * * @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 + * @throws GeneralSecurityException */ - public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatttedSdcReq) - throws IOException { + public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formattedSdcReq) + throws GeneralSecurityException { // 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; - } - - String responseStr = getResponse(conn); - if (responseStr != null) { - if (requestFailed) { + try { + logger.info("userid=" + userid); + String md5Text = sdcReq.calculateMD5ByString(formattedSdcReq); + byte[] postData = sdcReq.stringToByteArray(formattedSdcReq); + 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 && 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; + throws GeneralSecurityException { + try { + logger.info("userid=" + userid); + String basicAuth = sdcReq.getSdcBasicAuth(); + 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 +393,49 @@ 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 { + /** + * Check if the SDC Info in cache has expired. + * + * @param cldsServiceData + * @return + * @throws GeneralSecurityException + */ + public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws GeneralSecurityException { boolean expired = false; if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { String cachedServiceUuid = cldsServiceData.getServiceUUID(); @@ -408,7 +451,16 @@ public class SdcCatalogServices { return expired; } - public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) throws IOException { + /** + * Get the Service Data with Alarm Conditions for a given + * invariantServiceUuid. + * + * @param invariantServiceUuid + * @return + * @throws GeneralSecurityException + */ + public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) + throws GeneralSecurityException { String url = refProp.getStringValue("sdc.serviceUrl"); String catalogUrl = refProp.getStringValue("sdc.catalog.url"); String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); @@ -417,13 +469,18 @@ 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()); - // To remove duplicate resources from serviceDetail and add valid // vfs to service - if (cldsSdcServiceDetail.getResources() != null) { + if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { List cldsSdcResourceList = removeDuplicateSdcResourceInstances( cldsSdcServiceDetail.getResources()); if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) { @@ -448,17 +505,15 @@ 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) + throws GeneralSecurityException { // todo : refact this.. if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType( RESOURCE_VF_TYPE); List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( RESOURCE_VFC_TYPE); + allVfcResources.addAll(getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_CVFC_TYPE)); for (CldsVfData currCldsVfData : cldsVfDataList) { if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) { String resourceUuid = getResourceUuidFromResourceInvariantUuid( @@ -471,7 +526,6 @@ public class SdcCatalogServices { // associated with the VF's List cldsVfKPIDataList = getFieldPathFromVF(vfResponse); currCldsVfData.setCldsKPIList(cldsVfKPIDataList); - List vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse); if (vfcDataListFromVfResponse != null) { currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse); @@ -511,9 +565,15 @@ public class SdcCatalogServices { } } - private List getVfcDataListFromVfResponse(String vfResponse) throws IOException { + private List getVfcDataListFromVfResponse(String vfResponse) throws GeneralSecurityException { 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) { @@ -528,6 +588,11 @@ public class SdcCatalogServices { currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue()); cldsVfcDataList.add(currCldsVfcData); } else if (resourceTypeNode != null && "CVFC".equalsIgnoreCase(resourceTypeNode.textValue())) { + TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName"); + TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID"); + currCldsVfcData.setVfcName(vfcResourceName.textValue()); + currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue()); + cldsVfcDataList.add(currCldsVfcData); cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue())); } } @@ -535,10 +600,9 @@ public class SdcCatalogServices { return cldsVfcDataList; } - private List getVFCfromCVFC(String resourceUUID) { + private List getVFCfromCVFC(String resourceUUID) throws GeneralSecurityException { String catalogUrl = refProp.getStringValue("sdc.catalog.url"); List cldsVfcDataList = new ArrayList<>(); - if (resourceUUID != null) { String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata"; try { @@ -546,7 +610,6 @@ public class SdcCatalogServices { ObjectMapper mapper = new ObjectMapper(); ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfcResponse); ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); - if (vfcArrayNode != null) { for (JsonNode vfcjsonNode : vfcArrayNode) { CldsVfcData currCldsVfcData = new CldsVfcData(); @@ -572,12 +635,17 @@ public class SdcCatalogServices { return (id != null) ? id.replaceAll("\"", "") : ""; } - private List getAlarmCondtionsFromVfc(String vfcResponse) throws IOException { + private List getAlarmCondtionsFromVfc(String vfcResponse) throws GeneralSecurityException { 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) { for (int index = 0; index < artifactsArrayNode.size(); index++) { ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index); @@ -592,27 +660,37 @@ 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 { + private List getFieldPathFromVF(String vfResponse) throws GeneralSecurityException { 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) { for (int index = 0; index < artifactsArrayNode.size(); index++) { ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index); @@ -621,10 +699,9 @@ 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")) { + if (artifactUrlNode != null && "csv".equalsIgnoreCase(artifactName)) { String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue()); cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl)); logger.info(responsesFromArtifactUrl); @@ -639,40 +716,40 @@ public class SdcCatalogServices { logger.debug("invalid csv field path Record,total columns less than 6: " + record); return null; } - if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3)) || StringUtils.isBlank(record.get(5))) { logger.debug("Invalid csv field path Record,one of column is having blank value : " + record); return null; } - CldsVfKPIData cldsVfKPIData = new CldsVfKPIData(); cldsVfKPIData.setNfNamingCode(record.get(0).trim()); cldsVfKPIData.setNfNamingValue(record.get(1).trim()); - cldsVfKPIData.setFieldPath(record.get(2).trim()); cldsVfKPIData.setFieldPathValue(record.get(3).trim()); - cldsVfKPIData.setThresholdName(record.get(4).trim()); cldsVfKPIData.setThresholdValue(record.get(5).trim()); return cldsVfKPIData; - } // 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) { @@ -696,7 +773,14 @@ public class SdcCatalogServices { cldsAlarmConditionList.add(cldsAlarmCondition); } - public String getResponsesFromArtifactUrl(String artifactsUrl) { + /** + * Get the responses for the current artifact from the artifacts URL. + * + * @param artifactsUrl + * @return + * @throws GeneralSecurityException + */ + public String getResponsesFromArtifactUrl(String artifactsUrl) throws GeneralSecurityException { String hostUrl = refProp.getStringValue("sdc.hostUrl"); String artifactsUrlReworked = artifactsUrl.replaceAll("\"", ""); String artifactUrl = hostUrl + artifactsUrlReworked; @@ -708,47 +792,55 @@ public class SdcCatalogServices { /** * Service to services/resources/artifacts from sdc.Pass alarmConditions as - * true to get alarmconditons from artifact url and else it is false - * + * true to get alarm conditons from artifact url and else it is false + * * @param url * @param alarmConditions * @return - * @throws IOException + * @throws GeneralSecurityException + * In case of issue when decrypting the SDC password + * */ - public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) { + public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) + throws GeneralSecurityException { + Date startTime = new Date(); try { + LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL"); String urlReworked = removeUnwantedBracesFromString(url); URL urlObj = new URL(urlReworked); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); + String basicAuth = sdcReq.getSdcBasicAuth(); conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool"); conn.setRequestProperty("Authorization", basicAuth); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); 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) { if (!inputLine.isEmpty()) { - response.append(inputLine); + 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"); } - } /** @@ -757,26 +849,21 @@ 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; 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); byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf); - // To create byVf and vfcResourceNode with vfResourceUUID ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs()); byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid); - // To create byKpi ObjectNode kpiObjectNode = mapper.createObjectNode(); if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { @@ -787,7 +874,6 @@ public class SdcCatalogServices { } } byIdObjectNode.putPOJO("byKpi", kpiObjectNode); - // To create byVfc and alarmCondition with vfcResourceUUID ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode(); if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { @@ -799,35 +885,38 @@ public class SdcCatalogServices { } } byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode); - // To create byAlarmCondition with alarmConditionKey List allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData, "alarmCondition"); ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions); - byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey); - // To create byAlertDescription with AlertDescription List allAlertDescriptions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData, "alertDescription"); ObjectNode alertDescObjectNodeByAlert = createAlarmCondObjectNodeByAlarmKey(mapper, allAlertDescriptions); - 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. * @@ -862,7 +951,6 @@ public class SdcCatalogServices { */ private List getAllAlarmConditionsFromCldsVfData(CldsVfData currCldsVfData, String eventName) { List alarmCondList = new ArrayList<>(); - if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) { for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { alarmCondList.addAll(getAllAlarmConditionsFromCldsVfcData(currCldsVfcData, eventName)); @@ -884,7 +972,6 @@ public class SdcCatalogServices { private List getAllAlarmConditionsFromCldsVfcData(CldsVfcData currCldsVfcData, String eventName) { List alarmCondList = new ArrayList<>(); - if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { @@ -900,7 +987,6 @@ public class SdcCatalogServices { private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper, List cldsAlarmCondList) { ObjectNode alarmCondKeyNode = mapper.createObjectNode(); - if (cldsAlarmCondList != null && !cldsAlarmCondList.isEmpty()) { for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) { if (currCldsAlarmCondition != null) { @@ -944,17 +1030,13 @@ public class SdcCatalogServices { for (CldsVfKPIData currCldsVfKpiData : cldsVfKpiDataList) { if (currCldsVfKpiData != null) { ObjectNode thresholdNameObjectNode = mapper.createObjectNode(); - ObjectNode fieldPathObjectNode = mapper.createObjectNode(); ObjectNode nfNamingCodeNode = mapper.createObjectNode(); - fieldPathObjectNode.put(currCldsVfKpiData.getFieldPathValue(), currCldsVfKpiData.getFieldPathValue()); nfNamingCodeNode.put(currCldsVfKpiData.getNfNamingValue(), currCldsVfKpiData.getNfNamingValue()); - thresholdNameObjectNode.putPOJO("fieldPath", fieldPathObjectNode); thresholdNameObjectNode.putPOJO("nfNamingCode", nfNamingCodeNode); - vfResourceUuidObjectNode.putPOJO(currCldsVfKpiData.getThresholdValue(), thresholdNameObjectNode); } } @@ -972,22 +1054,19 @@ public class SdcCatalogServices { if (currCldsVfcData.getCldsAlarmConditions() != null && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { - alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), - currCldsAlarmCondition.getAlarmConditionKey()); - if (currCldsAlarmCondition.getEventName().equalsIgnoreCase("alarmCondition")) { + if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) { alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), currCldsAlarmCondition.getAlarmConditionKey()); } else { alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(), currCldsAlarmCondition.getAlarmConditionKey()); } - } } - + } vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); vfcObjectNode.putPOJO("alertDescription", alertDescNode); vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode); - } + } } } else { alarmCondNode.put("", ""); @@ -998,24 +1077,43 @@ 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.isEmpty()) { for (CldsVfData currCldsVfData : cldsVfDataList) { if (currCldsVfData != null) { - ObjectNode vfcObjectNode = mapper.createObjectNode(); + ObjectNode vfObjectNode = mapper.createObjectNode(); ObjectNode vfcUuidNode = mapper.createObjectNode(); + ObjectNode kpiObjectNode = mapper.createObjectNode(); if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) { for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { - vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), - currCldsVfcData.getVfcName()); + if (currCldsVfcData.getCldsAlarmConditions() != null + && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { + vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), + currCldsVfcData.getVfcName()); + } } } 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 +1148,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; } } } @@ -1065,9 +1162,9 @@ public class SdcCatalogServices { } public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid, - String invariantServiceUuid, String artifactName) { + String invariantServiceUuid, String artifactName) throws GeneralSecurityException { String baseUrl = refProp.getStringValue("sdc.serviceUrl"); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); + String basicAuth = sdcReq.getSdcBasicAuth(); String postStatusData = "{ \n" + "\"event\" : \"" + "Created" + "\",\n" + "\"serviceUUID\" : \"" + invariantServiceUuid + "\",\n" + "\"resourceUUID\" :\"" + invariantResourceUuid + "\",\n" + "\"artifactName\" : \"" + artifactName + "\",\n" + "} \n"; @@ -1077,22 +1174,18 @@ public class SdcCatalogServices { url = dcaeUrl + "/closed-loops"; } URL urlObj = new URL(url); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool"); conn.setRequestProperty("Authorization", basicAuth); conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); conn.setRequestMethod("POST"); - - byte[] postData = SdcReq.stringToByteArray(postStatusData); + byte[] postData = sdcReq.stringToByteArray(postStatusData); try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { wr.write(postData); } - int responseCode = conn.getResponseCode(); logger.info("responseCode=" + responseCode); - String resp = getResponse(conn); if (resp != null) { return resp; @@ -1109,22 +1202,20 @@ public class SdcCatalogServices { * @param resourceType * The resourceType * @return The list of CldsSdcResourceBasicInfo - * @throws IOException - * In case of issues with the Streams + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password + * */ private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) - throws IOException { - List allSdcResourceVfcBasicInfo = new ArrayList(); + throws GeneralSecurityException { 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.isEmpty()) { for (CldsSdcResourceBasicInfo currResource : resourceInfoList) { @@ -1172,12 +1263,12 @@ 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 - * + * @throws GeneralSecurityException + * In case of issues to decrypt the SDC password */ 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) + throws GeneralSecurityException { logger.info("userid=" + userid); String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop); if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) {