From e8bb2818b84918e5f1c985ae0f010e02bd3f1449 Mon Sep 17 00:00:00 2001 From: "Determe, Sebastien (sd378r)" Date: Tue, 29 Aug 2017 04:40:12 -0700 Subject: [PATCH] Add Request ID in Common Logging Add the requestId method + align the others classes using that + Rework the Exceptions thrown and the Javadocs Change-Id: I15bccf1d46ced15aa85d88287f05f1c14b6017fd Issue-Id: CLAMP-43 Signed-off-by: Determe, Sebastien (sd378r) --- .../clamp/clds/client/DcaeDispatcherServices.java | 123 +- .../clamp/clds/client/DcaeInventoryServices.java | 112 +- .../clds/client/OperationalPolicyDelegate.java | 53 +- .../onap/clamp/clds/client/SdcCatalogServices.java | 2235 +++++++++++--------- .../org/onap/clamp/clds/util/LoggingUtils.java | 37 +- 5 files changed, 1443 insertions(+), 1117 deletions(-) diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java index e5144bfe..3d8d5d53 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeDispatcherServices.java @@ -23,25 +23,31 @@ package org.onap.clamp.clds.client; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + import java.io.BufferedReader; import java.io.DataOutputStream; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; +import java.util.Date; import java.util.stream.Collectors; import javax.net.ssl.HttpsURLConnection; +import javax.ws.rs.BadRequestException; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; +import org.onap.clamp.clds.exception.DcaeDeploymentException; import org.onap.clamp.clds.model.refprop.RefProp; +import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.beans.factory.annotation.Autowired; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - /** - * + * This class implements the communication with DCAE for the service + * deployments. * */ public class DcaeDispatcherServices { @@ -49,23 +55,29 @@ public class DcaeDispatcherServices { protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); @Autowired - private RefProp refProp; + private RefProp refProp; /** - * + * Delete the deployment on DCAE. + * * @param deploymentId - * @return - * @throws Exception + * The deployment ID + * @return Return the URL Status + * @throws IOException + * In case of issues with the Stream */ - public String deleteDeployment(String deploymentId) throws Exception { + public String deleteDeployment(String deploymentId) throws IOException { String statusUrl = null; InputStream in = null; + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "deleteDeployment"); try { String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId; logger.info("Dcae Dispatcher url - " + url); URL obj = new URL(url); HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); + conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); conn.setRequestMethod("DELETE"); int responseCode = conn.getResponseCode(); @@ -94,7 +106,7 @@ public class DcaeDispatcherServices { if (responseStr != null) { if (requestFailed) { logger.error("requestFailed - responseStr=" + responseStr); - throw new Exception(responseStr); + throw new BadRequestException(responseStr); } } @@ -109,12 +121,14 @@ public class DcaeDispatcherServices { logger.debug("Status URL: " + statusUrl); } catch (Exception e) { - logger.error(e.getClass().getName() + " " + e.getMessage()); - throw e; + logger.error("Exception occurred during Delete Deployment Operation with DCAE", e); + throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e); } finally { if (in != null) { in.close(); } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("deleteDeployment complete"); } return statusUrl; @@ -122,23 +136,30 @@ public class DcaeDispatcherServices { } /** - * + * Get the Operation Status from a specified URL. + * * @param statusUrl - * @return - * @throws Exception + * The URL provided by a previous DCAE Query + * @return The status + * @throws IOException + * In case of issues with the Stream + * */ - public String getOperationStatus(String statusUrl) throws Exception { + public String getOperationStatus(String statusUrl) throws IOException { - //Assigning processing status to monitor operation status further + // Assigning processing status to monitor operation status further String opStatus = "processing"; InputStream in = null; + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "getOperationStatus"); try { URL obj = new URL(statusUrl); HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); conn.setRequestMethod("GET"); + conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); int responseCode = conn.getResponseCode(); logger.debug("Deployment operation status response code - " + responseCode); - if(responseCode == 200){ + if (responseCode == 200) { in = conn.getInputStream(); String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")); JSONParser parser = new JSONParser(); @@ -150,58 +171,71 @@ public class DcaeDispatcherServices { opStatus = status; } } catch (Exception e) { - logger.debug(e.getClass().getName() + " " + e.getMessage()); + logger.error("Exception occurred during getOperationStatus Operation with DCAE", e); logger.debug(e.getMessage() + " : got exception while retrieving status, trying again until we get 200 response code"); } finally { if (in != null) { in.close(); } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("getOperationStatus complete"); } - return opStatus; } /** - * - * @throws Exception + * This method send a getDeployments operation to DCAE. + * + * @throws IOException + * In case of issues with the Stream */ - public void getDeployments() throws Exception { + public void getDeployments() throws IOException { InputStream in = null; + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "getDeployments"); try { String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments"; logger.info("Dcae Dispatcher deployments url - " + url); URL obj = new URL(url); HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); conn.setRequestMethod("GET"); + conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); int responseCode = conn.getResponseCode(); logger.debug("response code " + responseCode); in = conn.getInputStream(); String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n")); logger.debug("res:" + res); } catch (Exception e) { - logger.error("Exception occurred during DCAE communication", e); - throw e; + logger.error("Exception occurred during getDeployments Operation with DCAE", e); + throw new DcaeDeploymentException("Exception occurred during getDeployments Operation with DCAE", e); } finally { if (in != null) { in.close(); } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("getDeployments complete"); } } /** - * Returns status URL for deployment operation + * Returns status URL for createNewDeployment operation. * * @param deploymentId + * The deployment ID * @param serviceTypeId - * @return - * @throws Exception + * Service type ID + * @return The status URL + * @throws IOException + * In case of issues with the Stream */ - public String createNewDeployment(String deploymentId, String serviceTypeId) throws Exception { + public String createNewDeployment(String deploymentId, String serviceTypeId) throws IOException { String statusUrl = null; InputStream inStream = null; BufferedReader in = null; + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "createNewDeployment"); try { String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}"; logger.info("Dcae api Body String - " + apiBodyString); @@ -210,6 +244,7 @@ public class DcaeDispatcherServices { URL obj = new URL(url); HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); conn.setRequestMethod("PUT"); + conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { @@ -247,7 +282,7 @@ public class DcaeDispatcherServices { if (responseStr != null) { if (requestFailed) { logger.error("requestFailed - responseStr=" + responseStr); - throw new Exception(responseStr); + throw new BadRequestException(responseStr); } } @@ -259,8 +294,8 @@ public class DcaeDispatcherServices { statusUrl = (String) linksObj.get("status"); logger.debug("Status URL: " + statusUrl); } catch (Exception e) { - logger.error("Exception occurred during the DCAE communication", e); - throw e; + logger.error("Exception occurred during createNewDeployment Operation with DCAE", e); + throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e); } finally { if (inStream != null) { inStream.close(); @@ -268,21 +303,29 @@ public class DcaeDispatcherServices { if (in != null) { in.close(); } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("createNewDeployment complete"); } return statusUrl; } /** - * + * Returns status URL for deleteExistingDeployment operation. + * * @param deploymentId + * The deployment ID * @param serviceTypeId - * @return - * @throws Exception + * The service Type ID + * @return The status URL + * @throws IOException + * In case of issues with the Stream */ - public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws Exception { + public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws IOException { String statusUrl = null; InputStream in = null; + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment"); try { String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}"; logger.debug(apiBodyString); @@ -291,6 +334,7 @@ public class DcaeDispatcherServices { URL obj = new URL(url); HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection(); conn.setRequestMethod("DELETE"); + conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); conn.setRequestProperty("Content-Type", "application/json"); conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); @@ -309,12 +353,15 @@ public class DcaeDispatcherServices { statusUrl = (String) linksObj.get("status"); logger.debug("Status URL: " + statusUrl); } catch (Exception e) { - logger.error("Exception occurred during DCAE communication", e); - throw e; + logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e); + throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE", + e); } finally { if (in != null) { in.close(); } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("deleteExistingDeployment complete"); } return statusUrl; } diff --git a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java index 3dfe9fec..c35eb0da 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -23,11 +23,16 @@ 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 java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Date; import java.util.List; import javax.ws.rs.BadRequestException; @@ -43,29 +48,43 @@ import org.onap.clamp.clds.model.DcaeEvent; 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.util.LoggingUtils; import org.springframework.beans.factory.annotation.Autowired; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonProcessingException; - +/** + * This class implements the communication with DCAE for the service inventory. + * + */ public class DcaeInventoryServices { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class); - protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeInventoryServices.class); + protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); @Autowired - private RefProp refProp; + private RefProp refProp; @Autowired - private CldsDao cldsDao; + private CldsDao cldsDao; @Autowired - private SdcCatalogServices sdcCatalogServices; - - public void setEventInventory(CldsModel cldsModel, String userId) throws Exception { + private SdcCatalogServices sdcCatalogServices; + + /** + * Set the event inventory. + * + * @param cldsModel + * The CldsModel + * @param userId + * The user ID + * @throws ParseException + * In case of issues during the parsing of DCAE answer + */ + public void setEventInventory(CldsModel cldsModel, String userId) throws ParseException { String artifactName = cldsModel.getControlName(); DcaeEvent dcaeEvent = new DcaeEvent(); String isDcaeInfoAvailable = null; + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "setEventInventory"); if (artifactName != null) { artifactName = artifactName + ".yml"; } @@ -74,8 +93,8 @@ public class DcaeInventoryServices { * Below are the properties required for calling the dcae inventory * url call */ - ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false, "{}", - cldsModel.getPropText()); + ModelProperties prop = new ModelProperties(cldsModel.getName(), cldsModel.getControlName(), null, false, + "{}", cldsModel.getPropText()); Global global = prop.getGlobal(); String invariantServiceUuid = global.getService(); List resourceUuidList = global.getResourceVf(); @@ -92,12 +111,12 @@ public class DcaeInventoryServices { dcaeEvent.setEvent(DcaeEvent.EVENT_DISTRIBUTION); } catch (JsonProcessingException e) { - // exception - logger.error("JsonProcessingException" + e); - } catch (IOException e) { - - // exception - logger.error("IOException :" + e); + logger.error("Error during JSON decoding", e); + } catch (IOException ex) { + logger.error("Error during JSON decoding", ex); + } finally { + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("setEventInventory complete"); } /* Null whether the DCAE has items lenght or not */ if (isDcaeInfoAvailable != null) { @@ -107,18 +126,18 @@ public class DcaeInventoryServices { Object obj0 = parser.parse(isDcaeInfoAvailable); JSONObject jsonObj = (JSONObject) obj0; String oldTypeId = cldsModel.getTypeId(); - String newTypeId = ""; + String newTypeId = ""; if (jsonObj.get("typeId") != null) { - newTypeId = jsonObj.get("typeId").toString(); + newTypeId = jsonObj.get("typeId").toString(); cldsModel.setTypeId(jsonObj.get("typeId").toString()); } // cldsModel.setTypeName(cldsModel.getControlName().toString()+".yml"); if (jsonObj.get("typeName") != null) { cldsModel.setTypeName(jsonObj.get("typeName").toString()); } - if(oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)){ - CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(), - CldsEvent.ACTION_STATE_RECEIVED, null); + if (oldTypeId == null || !oldTypeId.equalsIgnoreCase(newTypeId)) { + CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), userId, dcaeEvent.getCldsActionCd(), + CldsEvent.ACTION_STATE_RECEIVED, null); } cldsModel.save(cldsDao, userId); } else { @@ -126,32 +145,55 @@ public class DcaeInventoryServices { } } - public String getDcaeInformation(String artifactName, String serviceUUID, String resourceUUID) + /** + * DO a query to DCAE to get some Information. + * + * @param artifactName + * The artifact Name + * @param serviceUuid + * The service UUID + * @param resourceUuid + * The resource UUID + * @return The DCAE inventory for the artifact + * @throws IOException + * In case of issues with the stream + * @throws ParseException + * In case of issues with the Json parsing + */ + public String getDcaeInformation(String artifactName, String serviceUuid, String resourceUuid) throws IOException, ParseException { - String queryString = "?sdcResourceId=" + resourceUUID + "&sdcServiceId=" + serviceUUID + "&typeName=" + Date startTime = new Date(); + LoggingUtils.setTargetContext("DCAE", "getDcaeInformation"); + String queryString = "?sdcResourceId=" + resourceUuid + "&sdcServiceId=" + serviceUuid + "&typeName=" + artifactName; String fullUrl = refProp.getStringValue("DCAE_INVENTORY_URL") + "/dcae-service-types" + queryString; + logger.info("Dcae Inventory Service full url - " + fullUrl); String daceInventoryResponse = null; URL inventoryUrl = new URL(fullUrl); HttpURLConnection conn = (HttpURLConnection) inventoryUrl.openConnection(); conn.setRequestMethod("GET"); + String reqid = LoggingUtils.getRequestId(); + logger.info("reqid set to " + reqid); + conn.setRequestProperty("X-ECOMP-RequestID", reqid); + boolean requestFailed = true; int responseCode = conn.getResponseCode(); if (responseCode == 200) { requestFailed = false; } - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - String inputLine = null; - StringBuffer response = new StringBuffer(); - String responseStr = null; - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); + StringBuilder response = new StringBuilder(); + + try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + String inputLine = null; + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } } - in.close(); - responseStr = response.toString(); + String responseStr = response.toString(); if (responseStr != null) { if (requestFailed) { logger.error("requestFailed - responseStr=" + response); @@ -175,6 +217,8 @@ public class DcaeInventoryServices { daceInventoryResponse = dcaeServiceType0.toString(); logger.info(daceInventoryResponse.toString()); } + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("getDcaeInformation complete: number services returned=" + numServices); return daceInventoryResponse; } diff --git a/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java b/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java index ed972d4b..566d11a2 100644 --- a/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java @@ -23,8 +23,11 @@ package org.onap.clamp.clds.client; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.io.IOException; import java.util.Map; -import java.util.UUID; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; @@ -33,47 +36,65 @@ import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.prop.Policy; import org.onap.clamp.clds.model.prop.PolicyChain; import org.onap.clamp.clds.model.refprop.RefProp; +import org.onap.clamp.clds.util.LoggingUtils; import org.onap.policy.api.AttributeType; +import org.onap.policy.api.PolicyEngineException; +import org.onap.policy.controlloop.policy.builder.BuilderException; import org.springframework.beans.factory.annotation.Autowired; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - /** - * Send Operational Policy info to policy api. + * Send Operational Policy info to policy api. It uses the policy code to define + * the model and communicate with it. See also the PolicyClient class. + * */ public class OperationalPolicyDelegate implements JavaDelegate { protected static final EELFLogger logger = EELFManager.getInstance() .getLogger(OperationalPolicyDelegate.class); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + /** + * Automatically injected by Spring, define in CldsConfiguration as a bean. + */ @Autowired - private PolicyClient policyClient; + private PolicyClient policyClient; + /** + * Automatically injected by Spring, define in CldsConfiguration as a bean. + */ @Autowired - private RefProp refProp; + private RefProp refProp; /** * Perform activity. Send Operational Policy info to policy api. * * @param execution + * The DelegateExecution + * @throws BuilderException + * In case of issues with OperationalPolicyReq + * @throws IOException + * In case of issues with the stream + * @throws PolicyEngineException + * In case of issues with the PolicyEngine creation */ @Override - public void execute(DelegateExecution execution) throws Exception { + public void execute(DelegateExecution execution) throws IOException, BuilderException, PolicyEngineException { // execution.setVariable("operationalPolicyRequestUuid", // operationalPolicyRequestUuid); String responseMessage = null; String operationalPolicyRequestUuid = null; ModelProperties prop = ModelProperties.create(execution); - for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) { - operationalPolicyRequestUuid = UUID.randomUUID().toString(); - Map> attributes = OperationalPolicyReq.formatAttributes(refProp, prop, - prop.getType(Policy.class).getId(), policyChain); - responseMessage = policyClient.sendBrms(attributes, prop, operationalPolicyRequestUuid); - } - if (responseMessage != null) { - execution.setVariable("operationalPolicyResponseMessage", responseMessage.getBytes()); + Policy policy = prop.getType(Policy.class); + if (policy.isFound()) { + for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) { + operationalPolicyRequestUuid = LoggingUtils.getRequestId(); + Map> attributes = OperationalPolicyReq.formatAttributes(refProp, + prop, prop.getType(Policy.class).getId(), policyChain); + responseMessage = policyClient.sendBrms(attributes, prop, operationalPolicyRequestUuid); + } + if (responseMessage != null) { + execution.setVariable("operationalPolicyResponseMessage", responseMessage.getBytes()); + } } } 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 df86977c..a4e332a3 100644 --- a/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java +++ b/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java @@ -1,1022 +1,1213 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP CLAMP - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights - * reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END============================================ - * =================================================================== - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - */ - -package org.onap.clamp.clds.client; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringReader; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.Iterator; -import java.util.List; - -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.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.ModelProperties; -import org.onap.clamp.clds.model.refprop.RefProp; -import org.onap.clamp.clds.util.LoggingUtils; -import org.springframework.beans.factory.annotation.Autowired; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.databind.node.TextNode; - -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"; - - @Autowired - private RefProp refProp; - - public String getSdcServicesInformation(String uuid) throws Exception { - Date startTime = new Date(); - String baseUrl = refProp.getStringValue("sdc.serviceUrl"); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); - 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.setRequestMethod("GET"); - - String resp = getResponse(conn); - if (resp != null) { - logger.info(resp.toString()); - return resp; - } - // metrics log - LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName()); - - } catch (Exception e) { - LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName()); - LoggingUtils.setErrorContext("900", "Get sdc services error"); - logger.error("not able to get any service information from sdc for uuid:" + uuid + " , exception is - " + e.getMessage()); - } - LoggingUtils.setTimeContext(startTime, new Date()); - LoggingUtils.setTargetContext("SDC", "Get Services"); - metricsLogger.info("Get sdc services information"); - - return ""; - } - - /** - * To remove duplicate serviceUUIDs from sdc services List - * - * @param rawCldsSdcServiceList - * @return - */ - public List removeDuplicateServices(List rawCldsSdcServiceList) { - List cldsSdcServiceInfoList = null; - if (rawCldsSdcServiceList != null && rawCldsSdcServiceList.size() > 0) { - // sort list - Collections.sort(rawCldsSdcServiceList); - // and then take only the services with the max version (last in the - // list with the same name) - cldsSdcServiceInfoList = new ArrayList<>(); - 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); - if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) { - cldsSdcServiceInfoList.add(prev); - } - } - // add the last in the list - cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1)); - } - return cldsSdcServiceInfoList; - } - - /** - * 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) { - // sort list - Collections.sort(rawCldsSdcResourceList); - // and then take only the resources with the max version (last in - // the list with the same name) - cldsSdcResourceList = new ArrayList<>(); - 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); - if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) { - cldsSdcResourceList.add(prev); - } - } - // add the last in the list - cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1)); - } - return cldsSdcResourceList; - } - - /** - * 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) { - // 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); - if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) { - cldsSdcResourceBasicInfoList.add(prev); - } - } - // add the last in the list - cldsSdcResourceBasicInfoList - .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1)); - } - return cldsSdcResourceBasicInfoList; - } - - /** - * To get ServiceUUID by using serviceInvariantUUID - * - * @param invariantId - * @return - * @throws Exception - */ - public String getServiceUuidFromServiceInvariantId(String invariantId) throws Exception { - String serviceUuid = ""; - String responseStr = getSdcServicesInformation(null); - List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); - List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); - if (cldsSdcServicesList != null && cldsSdcServicesList.size() > 0) { - for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) { - if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null - && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) { - serviceUuid = currCldsSdcServiceInfo.getUuid(); - break; - } - } - } - return serviceUuid; - } - - /** - * To get CldsAsdsServiceInfo class by parsing json string - * - * @param jsonStr - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public List getCldsSdcServicesListFromJson(String jsonStr) throws IOException { - ObjectMapper objectMapper = new ObjectMapper(); - if (StringUtils.isBlank(jsonStr)) { - return null; - } - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class)); - } - - /** - * To get List class by parsing json string - * - * @param jsonStr - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public List getAllSdcResourcesListFromJson(String jsonStr) throws IOException { - ObjectMapper objectMapper = new ObjectMapper(); - if (StringUtils.isBlank(jsonStr)) { - return null; - } - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); - } - - /** - * To get CldsAsdsResource class by parsing json string - * - * @param jsonStr - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public CldsSdcResource getCldsSdcResourceFromJson(String jsonStr) throws IOException { - ObjectMapper objectMapper = new ObjectMapper(); - return objectMapper.readValue(jsonStr, CldsSdcResource.class); - } - - /** - * To get CldsSdcServiceDetail by parsing json string - * - * @param jsonStr - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) throws IOException { - ObjectMapper objectMapper = new ObjectMapper(); - return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); - } - - /** - * To upload artifact to sdc based on serviceUUID and resourcename on url - * - * @param prop - * @param userid - * @param url - * @param formatttedSdcReq - * @return - * @throws Exception - */ - public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatttedSdcReq) - throws Exception { - // 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) { - logger.error("requestFailed - responseStr=" + responseStr); - throw new Exception(responseStr); - } - } - 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); - return conn; - } - - private String getResponse(HttpURLConnection conn) throws IOException { - try (InputStream is = getInputStream(conn)) { - if (is != null) { - try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) { - StringBuffer response = new StringBuffer(); - String inputLine; - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - } - return response.toString(); - } - } - } - return null; - } - - private InputStream getInputStream(HttpURLConnection conn) throws IOException { - InputStream inStream = conn.getErrorStream(); - if (inStream == null) { - inStream = conn.getInputStream(); - } - 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 boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws Exception { - 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; - } - } else { - expired = true; - } - return expired; - } - - public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) throws Exception { - String url = refProp.getStringValue("sdc.serviceUrl"); - String catalogUrl = refProp.getStringValue("sdc.catalog.url"); - String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); - String serviceDetailUrl = url + "/" + serviceUuid + "/metadata"; - String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false); - ObjectMapper objectMapper = new ObjectMapper(); - CldsServiceData cldsServiceData = new CldsServiceData(); - if (responseStr != null) { - CldsSdcServiceDetail cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class); - cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid()); - cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID()); - - // To remove duplicate resources from serviceDetail and add valid - // vfs to service - if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { - List cldsSdcResourceList = removeDuplicateSdcResourceInstances( - cldsSdcServiceDetail.getResources()); - if (cldsSdcResourceList != null && cldsSdcResourceList.size() > 0) { - List cldsVfDataList = new ArrayList<>(); - for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) { - if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null - && currCldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) { - CldsVfData currCldsVfData = new CldsVfData(); - currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName()); - currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID()); - cldsVfDataList.add(currCldsVfData); - } - } - 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()); - } - } - } - return cldsServiceData; - } - - /** - * @param cldsVfDataList - * @throws IOException - */ - private void getAllVfcForVfList(List cldsVfDataList, String catalogUrl) throws IOException { - // todo : refact this.. - if (cldsVfDataList != null && cldsVfDataList.size() > 0) { - List allVfResources = getAllSdcVForVFCResourcesBasedOnResourceType( - RESOURCE_VF_TYPE); - List allVfcResources = getAllSdcVForVFCResourcesBasedOnResourceType( - RESOURCE_VFC_TYPE); - for (CldsVfData currCldsVfData : cldsVfDataList) { - if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) { - String resourceUuid = getResourceUuidFromResourceInvariantUuid( - currCldsVfData.getVfInvariantResourceUUID(), allVfResources); - if (resourceUuid != null) { - String vfResourceUuidUrl = catalogUrl + "resources" + "/" + resourceUuid + "/metadata"; - String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl, false); - if (vfResponse != null) { - // Below 2 line are to get the KPI(field path) data - // associated with the VF's - List cldsVfKPIDataList = getFieldPathFromVF(vfResponse); - currCldsVfData.setCldsKPIList(cldsVfKPIDataList); - - List vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse); - if (vfcDataListFromVfResponse != null) { - currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse); - if (vfcDataListFromVfResponse.size() > 0) { - // To get artifacts for every VFC and get - // alarm conditions from artifact - for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) { - if (currCldsVfcData != null - && currCldsVfcData.getVfcInvariantResourceUUID() != null) { - String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid( - currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources); - if (resourceVfcUuid != null) { - String vfcResourceUuidUrl = catalogUrl + "resources" + "/" - + resourceVfcUuid + "/metadata"; - String vfcResponse = getCldsServicesOrResourcesBasedOnURL( - vfcResourceUuidUrl, false); - if (vfcResponse != null) { - List alarmCondtionsFromVfc = getAlarmCondtionsFromVfc( - vfcResponse); - currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc); - } - } else { - logger.info("No resourceVFC UUID found for given invariantID:" - + currCldsVfcData.getVfcInvariantResourceUUID()); - } - } - } - } - } - } - } else { - logger.info("No resourceUUID found for given invariantREsourceUUID:" - + currCldsVfData.getVfInvariantResourceUUID()); - } - } - } - } - } - - private List getVfcDataListFromVfResponse(String vfResponse) throws IOException { - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); - ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); - List cldsVfcDataList = new ArrayList<>(); - if (vfcArrayNode != null && vfcArrayNode.size() > 0) { - for (int index = 0; index < vfcArrayNode.size(); index++) { - CldsVfcData currCldsVfcData = new CldsVfcData(); - ObjectNode currVfcNode = (ObjectNode) vfcArrayNode.get(index); - TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType"); - if (resourceTypeNode != null && resourceTypeNode.textValue().equalsIgnoreCase("VFC")) { - TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName"); - TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID"); - currCldsVfcData.setVfcName(vfcResourceName.textValue()); - currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue()); - cldsVfcDataList.add(currCldsVfcData); - } - } - } - return cldsVfcDataList; - } - - private String removeUnwantedBracesFromString(String id) { - if (id != null && id.contains("\"")) { - id = id.replaceAll("\"", ""); - } - return id; - } - - private List getAlarmCondtionsFromVfc(String vfcResponse) throws IOException { - List cldsAlarmConditionList = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); - 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); - TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL"); - if (artifactUrlNode != null) { - String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue()); - cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl)); - logger.info(responsesFromArtifactUrl); - } - } - } - 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(); - } - it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record)); - } - return cldsAlarmConditionList; - } - - // Method to get the artifact for any particular VF - private List getFieldPathFromVF(String vfResponse) throws JsonProcessingException, IOException { - List cldsVfKPIDataList = new ArrayList(); - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); - 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); - TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL"); - TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName"); - String artifactName = ""; - if (artifactNameNode != null) { - artifactName = artifactNameNode.textValue(); - artifactName = artifactName.substring(artifactName.lastIndexOf(".") + 1); - } - if (artifactUrlNode != null && artifactName != null && !artifactName.isEmpty() - && artifactName.equalsIgnoreCase("csv")) { - String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue()); - cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl)); - logger.info(responsesFromArtifactUrl); - } - } - } - return cldsVfKPIDataList; - } - - private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) { - if (record.size() < 6) { - 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); - } - } - } - return cldsVfKPIDataList; - } - - private void processRecord(List cldsAlarmConditionList, CSVRecord record) { - if (record == null) { - return; - } - if (record.size() < 5) { - logger.debug("invalid csv alarm Record,total columns less than 5: " + record); - return; - } - if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3)) - || StringUtils.isBlank(record.get(4))) { - logger.debug("invalid csv alarm Record,one of column is having blank value : " + record); - return; - } - CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition(); - cldsAlarmCondition.setEventSourceType(record.get(1)); - cldsAlarmCondition.setAlarmConditionKey(record.get(3)); - cldsAlarmCondition.setSeverity(record.get(4)); - cldsAlarmConditionList.add(cldsAlarmCondition); - } - - public String getResponsesFromArtifactUrl(String artifactsUrl) throws IOException { - String hostUrl = refProp.getStringValue("sdc.hostUrl"); - artifactsUrl = artifactsUrl.replaceAll("\"", ""); - String artifactUrl = hostUrl + artifactsUrl; - logger.info("value of artifactURl:" + artifactUrl); - String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true); - logger.info("value of artifactResponse:" + currArtifactResponse); - return currArtifactResponse; - } - - /** - * Service to services/resources/artifacts from sdc.Pass alarmConditions as - * true to get alarmconditons from artifact url and else it is false - * - * @param url - * @param alarmConditions - * @return - * @throws IOException - */ - public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) { - String responseStr; - try { - url = removeUnwantedBracesFromString(url); - URL urlObj = new URL(url); - - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); - conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool"); - conn.setRequestProperty("Authorization", basicAuth); - conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); - conn.setRequestMethod("GET"); - - int responseCode = conn.getResponseCode(); - logger.info("responseCode=" + responseCode); - - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - StringBuffer response = new StringBuffer(); - String inputLine; - while ((inputLine = in.readLine()) != null) { - response.append(inputLine); - if (alarmConditions) { - response.append("\n"); - } - } - responseStr = response.toString(); - in.close(); - return responseStr; - } catch (Exception e) { - logger.error("Exception occurred :", e); - return ""; - } - - } - - /** - * To create properties object by using cldsServicedata - * - * @param globalProps - * @param cldsServiceData - * @return - * @throws IOException - */ - public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) throws IOException { - 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().size() > 0) { - for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { - if (currCldsVfData != null) { - createKPIObjectNodeByVfUUID(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList()); - } - } - } - byIdObjectNode.putPOJO("byKpi", kpiObjectNode); - - // To create byVfc and alarmCondition with vfcResourceUUID - ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode(); - if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) { - for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { - if (currCldsVfData != null) { - createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode, - currCldsVfData.getCldsVfcs()); - } - } - } - byIdObjectNode.putPOJO("byVfc", vfcResourceUuidObjectNode); - - // To create byAlarmCondition with alarmConditionKey - List allAlarmConditions = getAllAlarmConditionsFromCldsServiceData(cldsServiceData); - ObjectNode alarmCondObjectNodeByAlarmKey = createAlarmCondObjectNodeByAlarmKey(mapper, allAlarmConditions); - - byIdObjectNode.putPOJO("byAlarmCondition", alarmCondObjectNodeByAlarmKey); - - globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class); - - globalPropsJson.putPOJO("shared", byIdObjectNode); - logger.info("valuie of objNode:" + globalPropsJson); - } else { - /** - * to create json with total properties when no serviceUUID passed - */ - globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class); - } - totalPropsStr = globalPropsJson.toString(); - return totalPropsStr; - } - - public List getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData) { - List alarmCondList = new ArrayList<>(); - if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null - && cldsServiceData.getCldsVfs().size() > 0) { - for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { - if (currCldsVfData != null && currCldsVfData.getCldsVfcs() != null - && currCldsVfData.getCldsVfcs().size() > 0) { - for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { - if (currCldsVfcData != null && currCldsVfcData.getCldsAlarmConditions() != null - && currCldsVfcData.getCldsAlarmConditions().size() > 0) { - for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { - if (currCldsAlarmCondition != null) { - alarmCondList.add(currCldsAlarmCondition); - } - } - } - } - } - } - } - return alarmCondList; - } - - private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper, - List cldsAlarmCondList) { - ObjectNode alarmCondKeyNode = mapper.createObjectNode(); - - if (cldsAlarmCondList != null && cldsAlarmCondList.size() > 0) { - for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) { - if (currCldsAlarmCondition != null) { - ObjectNode alarmCondNode = mapper.createObjectNode(); - alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType()); - alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity()); - alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode); - } - } - } else { - ObjectNode alarmCondNode = mapper.createObjectNode(); - alarmCondNode.put("eventSourceType", ""); - alarmCondNode.put("eventSeverity", ""); - alarmCondKeyNode.putPOJO("", alarmCondNode); - } - return alarmCondKeyNode; - } - - private ObjectNode createVFObjectNodeByServiceInvariantUUID(ObjectMapper mapper, CldsServiceData cldsServiceData) { - ObjectNode invariantUuidObjectNode = mapper.createObjectNode(); - ObjectNode vfObjectNode = mapper.createObjectNode(); - ObjectNode vfUuidNode = mapper.createObjectNode(); - List cldsVfsList = cldsServiceData.getCldsVfs(); - if (cldsVfsList != null && cldsVfsList.size() > 0) { - for (CldsVfData currCldsVfData : cldsVfsList) { - if (currCldsVfData != null) { - vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName()); - } - } - } else { - vfUuidNode.put("", ""); - } - vfObjectNode.putPOJO("vf", vfUuidNode); - invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode); - return invariantUuidObjectNode; - } - - private void createKPIObjectNodeByVfUUID(ObjectMapper mapper, ObjectNode vfResourceUUIDObjectNode, - List cldsVfKPIDataList) { - if (cldsVfKPIDataList != null && cldsVfKPIDataList.size() > 0) { - 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); - } - } - } - } - - private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUUIDObjectNode, - List cldsVfcDataList) { - ObjectNode alarmCondContsObjectNode = mapper.createObjectNode(); - ObjectNode alarmCondNode = mapper.createObjectNode(); - // alarmCondNode.put("", ""); - if (cldsVfcDataList != null && cldsVfcDataList.size() > 0) { - for (CldsVfcData currCldsVfcData : cldsVfcDataList) { - if (currCldsVfcData != null) { - if (currCldsVfcData.getCldsAlarmConditions() != null - && currCldsVfcData.getCldsAlarmConditions().size() > 0) { - for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { - alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), - currCldsAlarmCondition.getAlarmConditionKey()); - } - alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode); - } - alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode); - vfcResourceUUIDObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), - alarmCondContsObjectNode); - } - } - } else { - alarmCondNode.put("", ""); - alarmCondContsObjectNode.putPOJO("alarmCondition", alarmCondNode); - vfcResourceUUIDObjectNode.putPOJO("", alarmCondContsObjectNode); - } - } - - private ObjectNode createVFCObjectNodeByVfUuid(ObjectMapper mapper, List cldsVfDataList) { - ObjectNode vfUUIDObjectNode = mapper.createObjectNode(); - - if (cldsVfDataList != null && cldsVfDataList.size() > 0) { - for (CldsVfData currCldsVfData : cldsVfDataList) { - if (currCldsVfData != null) { - ObjectNode vfcObjectNode = mapper.createObjectNode(); - ObjectNode vfcUuidNode = mapper.createObjectNode(); - if (currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) { - for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { - vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), - currCldsVfcData.getVfcName()); - } - } else { - vfcUuidNode.put("", ""); - } - vfcObjectNode.putPOJO("vfc", vfcUuidNode); - vfUUIDObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfcObjectNode); - } - } - } else { - ObjectNode vfcUuidNode = mapper.createObjectNode(); - vfcUuidNode.put("", ""); - ObjectNode vfcObjectNode = mapper.createObjectNode(); - vfcObjectNode.putPOJO("vfc", vfcUuidNode); - vfUUIDObjectNode.putPOJO("", vfcObjectNode); - } - return vfUUIDObjectNode; - } - - public String getArtifactIdIfArtifactAlreadyExists(CldsSdcServiceDetail CldsSdcServiceDetail, String artifactName) { - String artifactUuid = null; - boolean artifactxists = false; - if (CldsSdcServiceDetail != null && CldsSdcServiceDetail.getResources() != null - && CldsSdcServiceDetail.getResources().size() > 0) { - for (CldsSdcResource currCldsSdcResource : CldsSdcServiceDetail.getResources()) { - if (artifactxists) { - break; - } - if (currCldsSdcResource != null && currCldsSdcResource.getArtifacts() != null - && currCldsSdcResource.getArtifacts().size() > 0) { - for (CldsSdcArtifact currCldsSdcArtifact : currCldsSdcResource.getArtifacts()) { - if (currCldsSdcArtifact != null && currCldsSdcArtifact.getArtifactName() != null) { - if (currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) { - artifactUuid = currCldsSdcArtifact.getArtifactUUID(); - artifactxists = true; - break; - } - } - } - } - } - } - return artifactUuid; - } - - public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid, - String invariantServiceUuid, String artifactName) { - String baseUrl = refProp.getStringValue("sdc.serviceUrl"); - String basicAuth = SdcReq.getSdcBasicAuth(refProp); - String postStatusData = "{ \n" + "\"event\" : \"" + "Created" + "\",\n" + "\"serviceUUID\" : \"" - + invariantServiceUuid + "\",\n" + "\"resourceUUID\" :\"" + invariantResourceUuid + "\",\n" - + "\"artifactName\" : \"" + artifactName + "\",\n" + "} \n"; - try { - String url = baseUrl; - if (invariantServiceUuid != null) { - 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.setRequestMethod("POST"); - - 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; - } - } catch (Exception e) { - logger.error("not able to ger any service information from sdc for uuid:" + invariantServiceUuid); - } - return ""; - } - - /** - * To get all sdc VF/VFC Resources basic info - * - * @return - * @throws IOException - */ - private List getAllSdcVForVFCResourcesBasedOnResourceType(String resourceType) - throws IOException { - List allSdcResourceVFCBasicInfo = new ArrayList(); - String catalogUrl = refProp.getStringValue("sdc.catalog.url"); - String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType; - String allSdcVFCResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false); - - allSdcResourceVFCBasicInfo = getAllSdcResourcesListFromJson(allSdcVFCResources); - return removeDuplicateSdcResourceBasicInfo(allSdcResourceVFCBasicInfo); - } - - private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUUID, - List resourceInfoList) throws IOException { - String resourceUuid = null; - if (resourceInfoList != null && resourceInfoList.size() > 0) { - for (CldsSdcResourceBasicInfo currResource : resourceInfoList) { - if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null - && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUUID)) { - resourceUuid = currResource.getUuid(); - break; - } - } - } - return resourceUuid; - } -} +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END============================================ + * =================================================================== + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ + +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; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.node.TextNode; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.List; + +import javax.ws.rs.BadRequestException; + +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.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.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"; + + @Autowired + private RefProp refProp; + + /** + * 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 + */ + public String getSdcServicesInformation(String uuid) { + Date startTime = new Date(); + String baseUrl = refProp.getStringValue("sdc.serviceUrl"); + String basicAuth = SdcReq.getSdcBasicAuth(refProp); + 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()); + // metrics log + LoggingUtils.setResponseContext("0", "Get sdc services success", this.getClass().getName()); + return resp; + } + } catch (IOException e) { + LoggingUtils.setResponseContext("900", "Get sdc services failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Get sdc services error"); + logger.error("not able to get any service information from sdc for uuid:" + uuid, e); + } finally { + 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) { + // sort list + Collections.sort(rawCldsSdcServiceList); + // and then take only the services with the max version (last in the + // list with the same name) + cldsSdcServiceInfoList = new ArrayList<>(); + 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); + if (!rawCldsSdcServiceList.get(i).getName().equals(prev.getName())) { + cldsSdcServiceInfoList.add(prev); + } + } + // add the last in the list + cldsSdcServiceInfoList.add(rawCldsSdcServiceList.get(rawCldsSdcServiceList.size() - 1)); + } + return cldsSdcServiceInfoList; + } + + /** + * 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) { + // sort list + Collections.sort(rawCldsSdcResourceList); + // and then take only the resources with the max version (last in + // the list with the same name) + cldsSdcResourceList = new ArrayList<>(); + 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); + if (!rawCldsSdcResourceList.get(i).getResourceInstanceName().equals(prev.getResourceInstanceName())) { + cldsSdcResourceList.add(prev); + } + } + // add the last in the list + cldsSdcResourceList.add(rawCldsSdcResourceList.get(rawCldsSdcResourceList.size() - 1)); + } + return cldsSdcResourceList; + } + + /** + * 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) { + // 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); + if (!rawCldsSdcResourceListBasicList.get(i).getName().equals(prev.getName())) { + cldsSdcResourceBasicInfoList.add(prev); + } + } + // add the last in the list + cldsSdcResourceBasicInfoList + .add(rawCldsSdcResourceListBasicList.get(rawCldsSdcResourceListBasicList.size() - 1)); + } + return cldsSdcResourceBasicInfoList; + } + + /** + * 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 + */ + public String getServiceUuidFromServiceInvariantId(String invariantId) throws IOException { + String serviceUuid = ""; + String responseStr = getSdcServicesInformation(null); + List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); + List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); + if (cldsSdcServicesList != null && cldsSdcServicesList.size() > 0) { + for (CldsSdcServiceInfo currCldsSdcServiceInfo : cldsSdcServicesList) { + if (currCldsSdcServiceInfo != null && currCldsSdcServiceInfo.getInvariantUUID() != null + && currCldsSdcServiceInfo.getInvariantUUID().equalsIgnoreCase(invariantId)) { + serviceUuid = currCldsSdcServiceInfo.getUuid(); + break; + } + } + } + return serviceUuid; + } + + /** + * To get CldsAsdsServiceInfo class by parsing json string. + * + * @param jsonStr + * @return + * @throws IOException + */ + public List getCldsSdcServicesListFromJson(String jsonStr) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + if (StringUtils.isBlank(jsonStr)) { + return null; + } + 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 + */ + public List getAllSdcResourcesListFromJson(String jsonStr) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + if (StringUtils.isBlank(jsonStr)) { + return null; + } + return objectMapper.readValue(jsonStr, + objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); + } + + /** + * To get CldsAsdsResource class by parsing json string. + * + * @param jsonStr + * @return + * @throws IOException + */ + public CldsSdcResource getCldsSdcResourceFromJson(String jsonStr) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(jsonStr, CldsSdcResource.class); + } + + /** + * To get CldsSdcServiceDetail by parsing json string. + * + * @param jsonStr + * @return + * @throws IOException + */ + public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); + } + + /** + * To upload artifact to sdc based on serviceUUID and resourcename on url. + * + * @param prop + * @param userid + * @param url + * @param formatttedSdcReq + * @return + * @throws IOException + */ + public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formatttedSdcReq) + throws IOException { + // 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) { + logger.error("requestFailed - responseStr=" + responseStr); + throw new BadRequestException(responseStr); + } + } + 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 String getResponse(HttpURLConnection conn) throws IOException { + try (InputStream is = getInputStream(conn)) { + if (is != null) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) { + StringBuilder response = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + return response.toString(); + } + } + } + return null; + } + + private InputStream getInputStream(HttpURLConnection conn) throws IOException { + InputStream inStream = conn.getErrorStream(); + if (inStream == null) { + inStream = conn.getInputStream(); + } + 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 boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws IOException { + 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; + } + } else { + expired = true; + } + return expired; + } + + public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) throws IOException { + String url = refProp.getStringValue("sdc.serviceUrl"); + String catalogUrl = refProp.getStringValue("sdc.catalog.url"); + String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); + String serviceDetailUrl = url + "/" + serviceUuid + "/metadata"; + String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false); + ObjectMapper objectMapper = new ObjectMapper(); + CldsServiceData cldsServiceData = new CldsServiceData(); + if (responseStr != null) { + CldsSdcServiceDetail cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class); + cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid()); + cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID()); + + // To remove duplicate resources from serviceDetail and add valid + // vfs to service + if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { + List cldsSdcResourceList = removeDuplicateSdcResourceInstances( + cldsSdcServiceDetail.getResources()); + if (cldsSdcResourceList != null && cldsSdcResourceList.size() > 0) { + List cldsVfDataList = new ArrayList<>(); + for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) { + if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null + && currCldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) { + CldsVfData currCldsVfData = new CldsVfData(); + currCldsVfData.setVfName(currCldsSdcResource.getResourceInstanceName()); + currCldsVfData.setVfInvariantResourceUUID(currCldsSdcResource.getResourceInvariantUUID()); + cldsVfDataList.add(currCldsVfData); + } + } + 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()); + } + } + } + return cldsServiceData; + } + + /** + * @param cldsVfDataList + * @throws IOException + */ + private void getAllVfcForVfList(List cldsVfDataList, String catalogUrl) throws IOException { + // todo : refact this.. + if (cldsVfDataList != null && cldsVfDataList.size() > 0) { + List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType( + RESOURCE_VF_TYPE); + List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( + RESOURCE_VFC_TYPE); + for (CldsVfData currCldsVfData : cldsVfDataList) { + if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) { + String resourceUuid = getResourceUuidFromResourceInvariantUuid( + currCldsVfData.getVfInvariantResourceUUID(), allVfResources); + if (resourceUuid != null) { + String vfResourceUuidUrl = catalogUrl + "resources" + "/" + resourceUuid + "/metadata"; + String vfResponse = getCldsServicesOrResourcesBasedOnURL(vfResourceUuidUrl, false); + if (vfResponse != null) { + // Below 2 line are to get the KPI(field path) data + // associated with the VF's + List cldsVfKPIDataList = getFieldPathFromVF(vfResponse); + currCldsVfData.setCldsKPIList(cldsVfKPIDataList); + + List vfcDataListFromVfResponse = getVfcDataListFromVfResponse(vfResponse); + if (vfcDataListFromVfResponse != null) { + currCldsVfData.setCldsVfcs(vfcDataListFromVfResponse); + if (vfcDataListFromVfResponse.size() > 0) { + // To get artifacts for every VFC and get + // alarm conditions from artifact + for (CldsVfcData currCldsVfcData : vfcDataListFromVfResponse) { + if (currCldsVfcData != null + && currCldsVfcData.getVfcInvariantResourceUUID() != null) { + String resourceVfcUuid = getResourceUuidFromResourceInvariantUuid( + currCldsVfcData.getVfcInvariantResourceUUID(), allVfcResources); + if (resourceVfcUuid != null) { + String vfcResourceUuidUrl = catalogUrl + "resources" + "/" + + resourceVfcUuid + "/metadata"; + String vfcResponse = getCldsServicesOrResourcesBasedOnURL( + vfcResourceUuidUrl, false); + if (vfcResponse != null) { + List alarmCondtionsFromVfc = getAlarmCondtionsFromVfc( + vfcResponse); + currCldsVfcData.setCldsAlarmConditions(alarmCondtionsFromVfc); + } + } else { + logger.info("No resourceVFC UUID found for given invariantID:" + + currCldsVfcData.getVfcInvariantResourceUUID()); + } + } + } + } + } + } + } else { + logger.info("No resourceUUID found for given invariantREsourceUUID:" + + currCldsVfData.getVfInvariantResourceUUID()); + } + } + } + } + } + + private List getVfcDataListFromVfResponse(String vfResponse) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); + List cldsVfcDataList = new ArrayList<>(); + if (vfcArrayNode != null) { + for (JsonNode vfcjsonNode : vfcArrayNode) { + CldsVfcData currCldsVfcData = new CldsVfcData(); + ObjectNode currVfcNode = (ObjectNode) vfcjsonNode; + TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType"); + if (resourceTypeNode != null && "VFC".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); + } else if (resourceTypeNode != null && "CVFC".equalsIgnoreCase(resourceTypeNode.textValue())) { + cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue())); + } + } + } + return cldsVfcDataList; + } + + private List getVFCfromCVFC(String resourceUUID) { + String catalogUrl = refProp.getStringValue("sdc.catalog.url"); + List cldsVfcDataList = new ArrayList(); + + if (resourceUUID != null) { + String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata"; + try { + String vfcResponse = getCldsServicesOrResourcesBasedOnURL(vfcResourceUUIDUrl, false); + 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(); + ObjectNode currVfcNode = (ObjectNode) vfcjsonNode; + TextNode resourceTypeNode = (TextNode) currVfcNode.get("resoucreType"); + if (resourceTypeNode != null && "VFC".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); + } + } + } + } catch (IOException e) { + logger.error("Exception during JSON analyzis", e); + } + } + return cldsVfcDataList; + } + + private String removeUnwantedBracesFromString(String id) { + if (id != null && id.contains("\"")) { + id = id.replaceAll("\"", ""); + } + return id; + } + + private List getAlarmCondtionsFromVfc(String vfcResponse) throws IOException { + List cldsAlarmConditionList = new ArrayList<>(); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); + 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); + TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL"); + if (artifactUrlNode != null) { + String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue()); + cldsAlarmConditionList.addAll(parseCsvToGetAlarmConditions(responsesFromArtifactUrl)); + logger.info(responsesFromArtifactUrl); + } + } + } + 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(); + } + it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record)); + } + return cldsAlarmConditionList; + } + + // Method to get the artifact for any particular VF + private List getFieldPathFromVF(String vfResponse) throws JsonProcessingException, IOException { + List cldsVfKPIDataList = new ArrayList(); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + 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); + TextNode artifactUrlNode = (TextNode) currArtifactNode.get("artifactURL"); + TextNode artifactNameNode = (TextNode) currArtifactNode.get("artifactName"); + String artifactName = ""; + if (artifactNameNode != null) { + artifactName = artifactNameNode.textValue(); + artifactName = artifactName.substring(artifactName.lastIndexOf(".") + 1); + } + if (artifactUrlNode != null && artifactName != null && !artifactName.isEmpty() + && artifactName.equalsIgnoreCase("csv")) { + String responsesFromArtifactUrl = getResponsesFromArtifactUrl(artifactUrlNode.textValue()); + cldsVfKPIDataList.addAll(parseCsvToGetFieldPath(responsesFromArtifactUrl)); + logger.info(responsesFromArtifactUrl); + } + } + } + return cldsVfKPIDataList; + } + + private CldsVfKPIData convertCsvRecordToKpiData(CSVRecord record) { + if (record.size() < 6) { + 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); + } + } + } + return cldsVfKPIDataList; + } + + private void processRecord(List cldsAlarmConditionList, CSVRecord record) { + if (record == null) { + return; + } + if (record.size() < 5) { + logger.debug("invalid csv alarm Record,total columns less than 5: " + record); + return; + } + if (StringUtils.isBlank(record.get(1)) || StringUtils.isBlank(record.get(3)) + || StringUtils.isBlank(record.get(4))) { + logger.debug("invalid csv alarm Record,one of column is having blank value : " + record); + return; + } + CldsAlarmCondition cldsAlarmCondition = new CldsAlarmCondition(); + cldsAlarmCondition.setEventSourceType(record.get(1)); + cldsAlarmCondition.setEventName(record.get(2)); + cldsAlarmCondition.setAlarmConditionKey(record.get(3)); + cldsAlarmCondition.setSeverity(record.get(4)); + cldsAlarmConditionList.add(cldsAlarmCondition); + } + + public String getResponsesFromArtifactUrl(String artifactsUrl) { + String hostUrl = refProp.getStringValue("sdc.hostUrl"); + artifactsUrl = artifactsUrl.replaceAll("\"", ""); + String artifactUrl = hostUrl + artifactsUrl; + logger.info("value of artifactURl:" + artifactUrl); + String currArtifactResponse = getCldsServicesOrResourcesBasedOnURL(artifactUrl, true); + logger.info("value of artifactResponse:" + currArtifactResponse); + return currArtifactResponse; + } + + /** + * Service to services/resources/artifacts from sdc.Pass alarmConditions as + * true to get alarmconditons from artifact url and else it is false + * + * @param url + * @param alarmConditions + * @return + * @throws IOException + */ + public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) { + try { + url = removeUnwantedBracesFromString(url); + URL urlObj = new URL(url); + + HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); + String basicAuth = SdcReq.getSdcBasicAuth(refProp); + 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); + 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 (alarmConditions) { + response.append("\n"); + } + } + } + return response.toString(); + } catch (IOException e) { + logger.error("Exception occurred during query to SDC", e); + return ""; + } + + } + + /** + * To create properties object by using cldsServicedata. + * + * @param globalProps + * @param cldsServiceData + * @return + * @throws IOException + */ + public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) throws IOException { + 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().size() > 0) { + for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { + if (currCldsVfData != null) { + createKpiObjectNodeByVfUuid(mapper, kpiObjectNode, currCldsVfData.getCldsKPIList()); + } + } + } + byIdObjectNode.putPOJO("byKpi", kpiObjectNode); + + // To create byVfc and alarmCondition with vfcResourceUUID + ObjectNode vfcResourceUuidObjectNode = mapper.createObjectNode(); + if (cldsServiceData.getCldsVfs() != null && cldsServiceData.getCldsVfs().size() > 0) { + for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { + if (currCldsVfData != null) { + createAlarmCondObjectNodeByVfcUuid(mapper, vfcResourceUuidObjectNode, + currCldsVfData.getCldsVfcs()); + } + } + } + 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.putPOJO("shared", byIdObjectNode); + logger.info("valuie of objNode:" + globalPropsJson); + } else { + /** + * to create json with total properties when no serviceUUID passed + */ + globalPropsJson = (ObjectNode) mapper.readValue(globalProps, JsonNode.class); + } + totalPropsStr = globalPropsJson.toString(); + return totalPropsStr; + } + + /** + * Method to get alarm conditions/alert description from Service Data. + * + * @param cldsServiceData + * CldsServiceData the Service Data to analyze + * @param eventName + * The String event name that will be used to filter the alarm + * list + * @return The list of CldsAlarmCondition for the event name specified + */ + public List getAllAlarmConditionsFromCldsServiceData(CldsServiceData cldsServiceData, + String eventName) { + List alarmCondList = new ArrayList<>(); + if (cldsServiceData != null && cldsServiceData.getCldsVfs() != null + && !cldsServiceData.getCldsVfs().isEmpty()) { + for (CldsVfData currCldsVfData : cldsServiceData.getCldsVfs()) { + alarmCondList.addAll(getAllAlarmConditionsFromCldsVfData(currCldsVfData, eventName)); + } + } + return alarmCondList; + } + + /** + * Method to get alarm conditions/alert description from VF Data. + * + * @param currCldsVfData + * The Vf Data to analyze + * @param eventName + * The String event name that will be used to filter the alarm + * list + * @return The list of CldsAlarmCondition for the event name specified + */ + 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)); + } + } + return alarmCondList; + } + + /** + * Method to get alarm conditions/alert description from VFC Data. + * + * @param currCldsVfcData + * The VfC Data to analyze + * @param eventName + * The String event name that will be used to filter the alarm + * list + * @return The list of CldsAlarmCondition for the event name specified + */ + 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()) { + if (currCldsAlarmCondition != null + && currCldsAlarmCondition.getEventName().equalsIgnoreCase(eventName)) { + alarmCondList.add(currCldsAlarmCondition); + } + } + } + return alarmCondList; + } + + private ObjectNode createAlarmCondObjectNodeByAlarmKey(ObjectMapper mapper, + List cldsAlarmCondList) { + ObjectNode alarmCondKeyNode = mapper.createObjectNode(); + + if (cldsAlarmCondList != null && cldsAlarmCondList.size() > 0) { + for (CldsAlarmCondition currCldsAlarmCondition : cldsAlarmCondList) { + if (currCldsAlarmCondition != null) { + ObjectNode alarmCondNode = mapper.createObjectNode(); + alarmCondNode.put("eventSourceType", currCldsAlarmCondition.getEventSourceType()); + alarmCondNode.put("eventSeverity", currCldsAlarmCondition.getSeverity()); + alarmCondKeyNode.putPOJO(currCldsAlarmCondition.getAlarmConditionKey(), alarmCondNode); + } + } + } else { + ObjectNode alarmCondNode = mapper.createObjectNode(); + alarmCondNode.put("eventSourceType", ""); + alarmCondNode.put("eventSeverity", ""); + alarmCondKeyNode.putPOJO("", alarmCondNode); + } + return alarmCondKeyNode; + } + + private ObjectNode createVfObjectNodeByServiceInvariantUuid(ObjectMapper mapper, CldsServiceData cldsServiceData) { + ObjectNode invariantUuidObjectNode = mapper.createObjectNode(); + ObjectNode vfObjectNode = mapper.createObjectNode(); + ObjectNode vfUuidNode = mapper.createObjectNode(); + List cldsVfsList = cldsServiceData.getCldsVfs(); + if (cldsVfsList != null && cldsVfsList.size() > 0) { + for (CldsVfData currCldsVfData : cldsVfsList) { + if (currCldsVfData != null) { + vfUuidNode.put(currCldsVfData.getVfInvariantResourceUUID(), currCldsVfData.getVfName()); + } + } + } else { + vfUuidNode.put("", ""); + } + vfObjectNode.putPOJO("vf", vfUuidNode); + invariantUuidObjectNode.putPOJO(cldsServiceData.getServiceInvariantUUID(), vfObjectNode); + return invariantUuidObjectNode; + } + + private void createKpiObjectNodeByVfUuid(ObjectMapper mapper, ObjectNode vfResourceUuidObjectNode, + List cldsVfKpiDataList) { + if (cldsVfKpiDataList != null && cldsVfKpiDataList.size() > 0) { + 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); + } + } + } + } + + private void createAlarmCondObjectNodeByVfcUuid(ObjectMapper mapper, ObjectNode vfcResourceUuidObjectNode, + List cldsVfcDataList) { + ObjectNode vfcObjectNode = mapper.createObjectNode(); + ObjectNode alarmCondNode = mapper.createObjectNode(); + ObjectNode alertDescNode = mapper.createObjectNode(); + if (cldsVfcDataList != null && cldsVfcDataList.size() > 0) { + for (CldsVfcData currCldsVfcData : cldsVfcDataList) { + if (currCldsVfcData != null) { + if (currCldsVfcData.getCldsAlarmConditions() != null + && currCldsVfcData.getCldsAlarmConditions().size() > 0) { + for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { + alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), + currCldsAlarmCondition.getAlarmConditionKey()); + if (currCldsAlarmCondition.getEventName().equalsIgnoreCase("alarmCondition")) { + 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("", ""); + vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); + alertDescNode.put("", ""); + vfcObjectNode.putPOJO("alertDescription", alarmCondNode); + vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode); + } + } + + private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List cldsVfDataList) { + ObjectNode vfUuidObjectNode = mapper.createObjectNode(); + + if (cldsVfDataList != null && cldsVfDataList.size() > 0) { + for (CldsVfData currCldsVfData : cldsVfDataList) { + if (currCldsVfData != null) { + ObjectNode vfcObjectNode = mapper.createObjectNode(); + ObjectNode vfcUuidNode = mapper.createObjectNode(); + if (currCldsVfData.getCldsVfcs() != null && currCldsVfData.getCldsVfcs().size() > 0) { + for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { + vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), + currCldsVfcData.getVfcName()); + } + } else { + vfcUuidNode.put("", ""); + } + vfcObjectNode.putPOJO("vfc", vfcUuidNode); + vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfcObjectNode); + } + } + } else { + ObjectNode vfcUuidNode = mapper.createObjectNode(); + vfcUuidNode.put("", ""); + ObjectNode vfcObjectNode = mapper.createObjectNode(); + vfcObjectNode.putPOJO("vfc", vfcUuidNode); + vfUuidObjectNode.putPOJO("", vfcObjectNode); + } + return vfUuidObjectNode; + } + + /** + * 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) { + String artifactUuid = null; + boolean artifactExists = false; + if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null + && !cldsSdcServiceDetail.getResources().isEmpty()) { + for (CldsSdcResource currCldsSdcResource : cldsSdcServiceDetail.getResources()) { + if (artifactExists) { + break; + } + 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; + } + } + } + } + } + } + return artifactUuid; + } + + public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid, + String invariantServiceUuid, String artifactName) { + String baseUrl = refProp.getStringValue("sdc.serviceUrl"); + String basicAuth = SdcReq.getSdcBasicAuth(refProp); + String postStatusData = "{ \n" + "\"event\" : \"" + "Created" + "\",\n" + "\"serviceUUID\" : \"" + + invariantServiceUuid + "\",\n" + "\"resourceUUID\" :\"" + invariantResourceUuid + "\",\n" + + "\"artifactName\" : \"" + artifactName + "\",\n" + "} \n"; + try { + String url = baseUrl; + if (invariantServiceUuid != null) { + 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); + 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; + } + } catch (IOException e) { + logger.error("Not able to get any service information from sdc for uuid:" + invariantServiceUuid, e); + } + return ""; + } + + /** + * To get all sdc VF/VFC Resources basic info. + * + * @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(); + String catalogUrl = refProp.getStringValue("sdc.catalog.url"); + String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType; + String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false); + + allSdcResourceVfcBasicInfo = getAllSdcResourcesListFromJson(allSdcVfcResources); + return removeDuplicateSdcResourceBasicInfo(allSdcResourceVfcBasicInfo); + } + + private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid, + List resourceInfoList) throws IOException { + String resourceUuid = null; + if (resourceInfoList != null && resourceInfoList.size() > 0) { + for (CldsSdcResourceBasicInfo currResource : resourceInfoList) { + if (currResource != null && currResource.getInvariantUUID() != null && currResource.getUuid() != null + && currResource.getInvariantUUID().equalsIgnoreCase(resourceInvariantUuid)) { + resourceUuid = currResource.getUuid(); + break; + } + } + } + return resourceUuid; + } + + /** + * Method to get service invariant uuid from model properties. + * + * @param props + * The Clds model properties + * @return The Service Id + */ + private String getServiceInvariantUuidFromProps(ModelProperties props) { + String invariantUuid = ""; + Global globalProps = props.getGlobal(); + if (globalProps != null && globalProps.getService() != null) { + invariantUuid = globalProps.getService(); + } + return invariantUuid; + } + + /** + * This method upload the BluePrint to SDC. + * + * @param prop + * The Clds model Properties + * @param userid + * The user id for SDC + * @param sdcReqUrlsList + * The list of SDC URL to try + * @param formattedSdcReq + * The blueprint to upload + * @param formattedSdcLocationReq + * THe location Blueprint to upload + * @param artifactName + * The artifact name from where we can get the Artifact UUID + * @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 { + logger.info("userid=" + userid); + String serviceInvariantUuid = getServiceInvariantUuidFromProps(prop); + if (sdcReqUrlsList != null && !sdcReqUrlsList.isEmpty()) { + for (String url : sdcReqUrlsList) { + if (url != null) { + String originalServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid); + logger.info("ServiceUUID used before upload in url:" + originalServiceUuid); + String sdcServicesInformation = getSdcServicesInformation(originalServiceUuid); + CldsSdcServiceDetail cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation); + String uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, + artifactName); + // Upload artifacts to sdc + String updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url; + String responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcReq); + logger.info("value of sdc Response of uploading to sdc :" + responseStr); + String updatedServiceUuid = getServiceUuidFromServiceInvariantId(serviceInvariantUuid); + if (!originalServiceUuid.equalsIgnoreCase(updatedServiceUuid)) { + url = url.replace(originalServiceUuid, updatedServiceUuid); + } + logger.info("ServiceUUID used after upload in ulr:" + updatedServiceUuid); + sdcServicesInformation = getSdcServicesInformation(updatedServiceUuid); + cldsSdcServiceDetail = getCldsSdcServiceDetailFromJson(sdcServicesInformation); + uploadedArtifactUuid = getArtifactIdIfArtifactAlreadyExists(cldsSdcServiceDetail, + locationArtifactName); + // To send location information also to sdc + updateUrl = uploadedArtifactUuid != null ? url + "/" + uploadedArtifactUuid : url; + responseStr = uploadArtifactToSdc(prop, userid, updateUrl, formattedSdcLocationReq); + logger.info("value of sdc Response of uploading location to sdc :" + responseStr); + } + } + } + } +} diff --git a/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java b/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java index 8ceaa3d8..b501b2df 100644 --- a/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java +++ b/src/main/java/org/onap/clamp/clds/util/LoggingUtils.java @@ -29,27 +29,32 @@ import java.util.Date; import java.util.TimeZone; import java.util.UUID; -import org.jboss.logging.MDC; +import org.apache.log4j.MDC; +/** + * This class handles the special info that appear in the log, like RequestID, + * time context, ... + * + */ public class LoggingUtils { /** * Set request related logging variables in thread local data via MDC - * + * * @param service * Service Name of API (ex. "PUT template") * @param partner * Partner name (client or user invoking API) */ public static void setRequestContext(String service, String partner) { - MDC.put("RequestId", UUID.randomUUID().toString()); + MDC.put("RequestId", UUID.randomUUID().toString()); MDC.put("ServiceName", service); MDC.put("PartnerName", partner); } /** - * Set time related logging variables in thread local data via MDC - * + * Set time related logging variables in thread local data via MDC. + * * @param beginTimeStamp * Start time * @param endTimeStamp @@ -72,7 +77,7 @@ public class LoggingUtils { } /** - * Set response related logging variables in thread local data via MDC + * Set response related logging variables in thread local data via MDC. * * @param code * Response code ("0" indicates success) @@ -102,7 +107,7 @@ public class LoggingUtils { } /** - * Set error related logging variables in thread local data via MDC + * Set error related logging variables in thread local data via MDC. * * @param code * Error code @@ -121,4 +126,22 @@ public class LoggingUtils { return df.format(timeStamp); } + /** + * Get a previously stored RequestID for the thread local data via MDC. If + * one was not previously stored, generate one, store it, and return that + * one. + * + * @return A string with the request ID + */ + public static String getRequestId() { + String reqid; + + reqid = (String) MDC.get("RequestID"); + if (reqid == null || reqid.isEmpty()) { + reqid = UUID.randomUUID().toString(); + MDC.put("RequestId", reqid); + } + return reqid; + } + } -- 2.16.6