From 98344de96ed80b839929be0bd272753675009a5d Mon Sep 17 00:00:00 2001 From: "Determe, Sebastien (sd378r)" Date: Thu, 16 Nov 2017 13:40:22 +0100 Subject: Refactor the java packages Refactor the java packages, moving classes to others packages Change-Id: I5cd2a6477876ce187502afff37a862029a081ab3 Issue-ID: CLAMP-74 Signed-off-by: Determe, Sebastien (sd378r) --- src/main/java/org/onap/clamp/clds/Application.java | 15 +- .../clamp/clds/client/DcaeInventoryServices.java | 8 +- .../clamp/clds/client/HolmesPolicyDelegate.java | 21 +- .../clds/client/HolmesPolicyDeleteDelegate.java | 9 +- .../clds/client/OperationalPolicyDelegate.java | 14 +- .../client/OperationalPolicyDeleteDelegate.java | 35 +- .../org/onap/clamp/clds/client/PolicyClient.java | 501 -------- .../onap/clamp/clds/client/SdcCatalogServices.java | 1304 ------------------- .../onap/clamp/clds/client/SdcSendReqDelegate.java | 3 +- .../onap/clamp/clds/client/TcaPolicyDelegate.java | 9 +- .../clamp/clds/client/TcaPolicyDeleteDelegate.java | 1 + .../clds/client/req/OperationalPolicyReq.java | 451 ------- .../org/onap/clamp/clds/client/req/SdcReq.java | 317 ----- .../clamp/clds/client/req/TcaRequestFormatter.java | 204 --- .../client/req/policy/OperationalPolicyReq.java | 424 +++++++ .../clamp/clds/client/req/policy/PolicyClient.java | 454 +++++++ .../clds/client/req/sdc/SdcCatalogServices.java | 1305 ++++++++++++++++++++ .../org/onap/clamp/clds/client/req/sdc/SdcReq.java | 232 ++++ .../clds/client/req/tca/TcaRequestFormatter.java | 203 +++ .../onap/clamp/clds/config/CldsConfiguration.java | 6 +- src/main/java/org/onap/clamp/clds/dao/CldsDao.java | 55 +- .../onap/clamp/clds/dao/CldsServiceDataMapper.java | 62 + .../org/onap/clamp/clds/dao/ValueItemMapper.java | 42 + .../clds/exception/ModelPropertiesException.java | 61 + .../org/onap/clamp/clds/model/prop/ModelBpmn.java | 68 +- .../org/onap/clamp/clds/service/CldsService.java | 42 +- .../clamp/clds/service/CldsTemplateService.java | 17 +- .../java/org/onap/clamp/clds/AbstractItCase.java | 2 +- .../clds/client/req/OperationalPolicyReqTest.java | 1 + .../java/org/onap/clamp/clds/it/CldsDaoItCase.java | 27 +- .../org/onap/clamp/clds/it/CldsServiceItCase.java | 90 +- .../clamp/clds/it/OperationPolicyReqItCase.java | 3 +- .../org/onap/clamp/clds/it/PolicyClientItCase.java | 15 +- .../java/org/onap/clamp/clds/it/RefPropItCase.java | 8 +- .../clamp/clds/it/SdcCatalogServicesItCase.java | 2 +- .../java/org/onap/clamp/clds/it/SdcReqItCase.java | 7 +- .../clamp/clds/it/TcaRequestFormatterItCase.java | 2 +- 37 files changed, 3013 insertions(+), 3007 deletions(-) delete mode 100644 src/main/java/org/onap/clamp/clds/client/PolicyClient.java delete mode 100644 src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java delete mode 100644 src/main/java/org/onap/clamp/clds/client/req/OperationalPolicyReq.java delete mode 100644 src/main/java/org/onap/clamp/clds/client/req/SdcReq.java delete mode 100644 src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java create mode 100644 src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReq.java create mode 100644 src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java create mode 100644 src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java create mode 100644 src/main/java/org/onap/clamp/clds/client/req/sdc/SdcReq.java create mode 100644 src/main/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatter.java create mode 100644 src/main/java/org/onap/clamp/clds/dao/CldsServiceDataMapper.java create mode 100644 src/main/java/org/onap/clamp/clds/dao/ValueItemMapper.java create mode 100644 src/main/java/org/onap/clamp/clds/exception/ModelPropertiesException.java (limited to 'src') diff --git a/src/main/java/org/onap/clamp/clds/Application.java b/src/main/java/org/onap/clamp/clds/Application.java index 0304a680..e2bfbd77 100644 --- a/src/main/java/org/onap/clamp/clds/Application.java +++ b/src/main/java/org/onap/clamp/clds/Application.java @@ -65,28 +65,22 @@ import org.springframework.scheduling.annotation.EnableAsync; ManagementWebSecurityAutoConfiguration.class }) @EnableAsync public class Application extends SpringBootServletInitializer { - - protected static final EELFLogger eelfLogger = EELFManager.getInstance().getLogger(Application.class); - + protected static final EELFLogger EELF_LOGGER = EELFManager.getInstance().getLogger(Application.class); @Autowired - protected ApplicationContext appContext; - + private ApplicationContext appContext; private static final String CAMEL_SERVLET_NAME = "CamelServlet"; private static final String CAMEL_URL_MAPPING = "/restservices/clds/v1/*"; - // This settings is an additional one to Spring config, // only if we want to have an additional port automatically redirected to // HTTPS @Value("${server.http-to-https-redirection.port:none}") private String httpRedirectedPort; - /** * This 8080 is the default port used by spring if this parameter is not * specified in application.properties. */ @Value("${server.port:8080}") private String springServerPort; - @Value("${server.ssl.key-store:none}") private String sslKeystoreFile; @@ -139,15 +133,13 @@ public class Application extends SpringBootServletInitializer { if (newConnector != null) { tomcat.addAdditionalTomcatConnectors(newConnector); } - } return tomcat; - } private Connector createRedirectConnector(int redirectSecuredPort) { if (redirectSecuredPort <= 0) { - eelfLogger.warn( + EELF_LOGGER.warn( "HTTP port redirection to HTTPS is disabled because the HTTPS port is 0 (random port) or -1 (Connector disabled)"); return null; } @@ -158,5 +150,4 @@ public class Application extends SpringBootServletInitializer { connector.setRedirectPort(redirectSecuredPort); return connector; } - } 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 7662a9fc..53d17aaf 100644 --- a/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java +++ b/src/main/java/org/onap/clamp/clds/client/DcaeInventoryServices.java @@ -32,6 +32,7 @@ import java.security.GeneralSecurityException; import java.util.Date; import java.util.List; +import org.apache.commons.codec.DecoderException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -74,8 +75,11 @@ public class DcaeInventoryServices { * In case of issue when decryting the DCAE password * @throws ParseException * In case of DCAE Json parse exception + * @throws DecoderException + * In case of issues with HexString decoding */ - public void setEventInventory(CldsModel cldsModel, String userId) throws GeneralSecurityException, ParseException { + public void setEventInventory(CldsModel cldsModel, String userId) + throws GeneralSecurityException, ParseException, DecoderException { String artifactName = cldsModel.getControlName(); DcaeEvent dcaeEvent = new DcaeEvent(); String isDcaeInfoAvailable = null; @@ -152,7 +156,9 @@ public class DcaeInventoryServices { * 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) diff --git a/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDelegate.java b/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDelegate.java index 5e0d470e..aaa4a2c0 100644 --- a/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDelegate.java @@ -30,6 +30,7 @@ import java.util.UUID; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; +import org.onap.clamp.clds.client.req.policy.PolicyClient; import org.onap.clamp.clds.model.prop.Holmes; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.refprop.RefProp; @@ -41,10 +42,8 @@ import org.springframework.beans.factory.annotation.Autowired; public class HolmesPolicyDelegate implements JavaDelegate { protected static final EELFLogger logger = EELFManager.getInstance().getLogger(HolmesPolicyDelegate.class); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - @Autowired private PolicyClient policyClient; - @Autowired private RefProp refProp; @@ -54,22 +53,30 @@ public class HolmesPolicyDelegate implements JavaDelegate { * @param execution */ @Override - public void execute(DelegateExecution execution) throws Exception { + public void execute(DelegateExecution execution) { String holmesPolicyRequestUuid = UUID.randomUUID().toString(); execution.setVariable("holmesPolicyRequestUuid", holmesPolicyRequestUuid); - ModelProperties prop = ModelProperties.create(execution); Holmes holmes = prop.getType(Holmes.class); if (holmes.isFound()) { - String responseMessage = policyClient.sendBasePolicyInOther(formatHolmesConfigBody(prop, holmes), holmes.getConfigPolicyName(), prop, - holmesPolicyRequestUuid); + String responseMessage = policyClient.sendBasePolicyInOther(formatHolmesConfigBody(prop, holmes), + holmes.getConfigPolicyName(), prop, holmesPolicyRequestUuid); if (responseMessage != null) { execution.setVariable("holmesPolicyResponseMessage", responseMessage.getBytes()); } } } - public static String formatHolmesConfigBody (ModelProperties prop, Holmes holmes) { + /** + * This method is used to create the Payload that must be sent to Holmes. + * + * @param prop + * The ModelProperties containing all the closed loop props + * @param holmes + * The holmes object extracted from the closed loop + * @return The String that must be sent to policy for holmes + */ + public static String formatHolmesConfigBody(ModelProperties prop, Holmes holmes) { return prop.getControlName() + "$$$" + holmes.getCorrelationLogic(); } } diff --git a/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDeleteDelegate.java b/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDeleteDelegate.java index c3c91fa8..72db3285 100644 --- a/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDeleteDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/HolmesPolicyDeleteDelegate.java @@ -26,8 +26,11 @@ package org.onap.clamp.clds.client; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; +import java.io.IOException; + import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; +import org.onap.clamp.clds.client.req.policy.PolicyClient; import org.onap.clamp.clds.model.prop.Holmes; import org.onap.clamp.clds.model.prop.ModelProperties; import org.springframework.beans.factory.annotation.Autowired; @@ -39,7 +42,6 @@ public class HolmesPolicyDeleteDelegate implements JavaDelegate { protected static final EELFLogger logger = EELFManager.getInstance() .getLogger(HolmesPolicyDeleteDelegate.class); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - @Autowired private PolicyClient policyClient; @@ -47,16 +49,15 @@ public class HolmesPolicyDeleteDelegate implements JavaDelegate { * Perform activity. Delete Holmes Policy via policy api. * * @param execution + * @throws IOException */ @Override - public void execute(DelegateExecution execution) throws Exception { + public void execute(DelegateExecution execution) { ModelProperties prop = ModelProperties.create(execution); Holmes holmes = prop.getType(Holmes.class); if (holmes.isFound()) { prop.setCurrentModelElementId(holmes.getId()); - policyClient.deleteBasePolicy(prop); } } - } 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 493b3b3c..e6af4336 100644 --- a/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java @@ -26,12 +26,13 @@ package org.onap.clamp.clds.client; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.util.Map; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.clamp.clds.client.req.OperationalPolicyReq; +import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; +import org.onap.clamp.clds.client.req.policy.PolicyClient; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.prop.Policy; import org.onap.clamp.clds.model.prop.PolicyChain; @@ -51,13 +52,11 @@ 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; - /** * Automatically injected by Spring, define in CldsConfiguration as a bean. */ @@ -71,13 +70,13 @@ public class OperationalPolicyDelegate implements JavaDelegate { * 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 + * @throws UnsupportedEncodingException */ @Override - public void execute(DelegateExecution execution) throws IOException, BuilderException, PolicyEngineException { + public void execute(DelegateExecution execution) + throws BuilderException, PolicyEngineException, UnsupportedEncodingException { String responseMessage = null; String operationalPolicyRequestUuid = null; ModelProperties prop = ModelProperties.create(execution); @@ -94,5 +93,4 @@ public class OperationalPolicyDelegate implements JavaDelegate { } } } - } diff --git a/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDeleteDelegate.java b/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDeleteDelegate.java index 2c60941c..2bca639e 100644 --- a/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDeleteDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/OperationalPolicyDeleteDelegate.java @@ -23,16 +23,19 @@ package org.onap.clamp.clds.client; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.io.IOException; + import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; +import org.onap.clamp.clds.client.req.policy.PolicyClient; 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.springframework.beans.factory.annotation.Autowired; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - /** * Delete Operational Policy via policy api. */ @@ -40,31 +43,29 @@ public class OperationalPolicyDeleteDelegate implements JavaDelegate { protected static final EELFLogger logger = EELFManager.getInstance() .getLogger(OperationalPolicyDeleteDelegate.class); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - @Autowired - private PolicyClient policyClient; + private PolicyClient policyClient; /** * Perform activity. Delete Operational Policy via policy api. * * @param execution + * @throws IOException */ @Override - public void execute(DelegateExecution execution) throws Exception { + public void execute(DelegateExecution execution) { ModelProperties prop = ModelProperties.create(execution); Policy policy = prop.getType(Policy.class); prop.setCurrentModelElementId(policy.getId()); - String responseMessage = ""; - if(policy.isFound()){ - for (PolicyChain policyChain : policy.getPolicyChains()) { - prop.setPolicyUniqueId(policyChain.getPolicyId()); - responseMessage = policyClient.deleteBrms(prop); - } - if (responseMessage != null) { - execution.setVariable("operationalPolicyDeleteResponseMessage", responseMessage.getBytes()); - } - } + if (policy.isFound()) { + for (PolicyChain policyChain : policy.getPolicyChains()) { + prop.setPolicyUniqueId(policyChain.getPolicyId()); + responseMessage = policyClient.deleteBrms(prop); + } + if (responseMessage != null) { + execution.setVariable("operationalPolicyDeleteResponseMessage", responseMessage.getBytes()); + } + } } - } diff --git a/src/main/java/org/onap/clamp/clds/client/PolicyClient.java b/src/main/java/org/onap/clamp/clds/client/PolicyClient.java deleted file mode 100644 index ed29afe7..00000000 --- a/src/main/java/org/onap/clamp/clds/client/PolicyClient.java +++ /dev/null @@ -1,501 +0,0 @@ -/*- - * ============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 java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import javax.ws.rs.BadRequestException; - -import org.onap.clamp.clds.exception.policy.PolicyClientException; -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.onap.policy.api.AttributeType; -import org.onap.policy.api.ConfigRequestParameters; -import org.onap.policy.api.DeletePolicyCondition; -import org.onap.policy.api.DeletePolicyParameters; -import org.onap.policy.api.PolicyChangeResponse; -import org.onap.policy.api.PolicyConfig; -import org.onap.policy.api.PolicyConfigException; -import org.onap.policy.api.PolicyConfigType; -import org.onap.policy.api.PolicyEngine; -import org.onap.policy.api.PolicyEngineException; -import org.onap.policy.api.PolicyParameters; -import org.onap.policy.api.PolicyType; -import org.onap.policy.api.PushPolicyParameters; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationContext; - -/** - * Policy utility methods - specifically, send the policy. - */ -public class PolicyClient { - protected static final String POLICY_PREFIX_BASE = "Config_"; - protected static final String POLICY_PREFIX_BRMS_PARAM = "Config_BRMS_Param_"; - protected static final String POLICY_PREFIX_MICROSERVICE = "Config_MS_"; - - protected static final String LOG_POLICY_PREFIX = "Response is "; - - protected static final EELFLogger logger = EELFManager.getInstance() - .getLogger(PolicyClient.class); - protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - - @Value("${org.onap.clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}") - protected String cldsPolicyConfigFile; - - @Autowired - protected ApplicationContext appContext; - - @Autowired - protected RefProp refProp; - - /** - * Perform BRMS policy type. - * - * @param attributes - * A map of attributes - * @param prop - * The ModelProperties - * @param policyRequestUuid - * PolicyRequest UUID - * @return The response message of policy - * - */ - public String sendBrmsPolicy(Map> attributes, ModelProperties prop, - String policyRequestUuid) { - - PolicyParameters policyParameters = new PolicyParameters(); - - // Set Policy Type(Mandatory) - policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM); - - // Set Policy Name(Mandatory) - policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()); - - // documentation says this is options, but when tested, got the - // following failure: java.lang.Exception: Policy send failed: PE300 - - // Data Issue: No policyDescription given. - policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription")); - policyParameters.setAttributes(attributes); - // Set a random UUID(Mandatory) - policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); - String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix"); - String rtnMsg = send(policyParameters, prop, policyNamePrefix); - - String policyType = refProp.getStringValue("policy.op.type"); - push(policyType, prop); - - return rtnMsg; - } - - /** - * Perform send of microservice policy in JSON. - * - * @param policyJson - * The policy JSON - * @param prop - * The ModelProperties - * @param policyRequestUuid - * The policy Request UUID - * @return The response message of policy - */ - public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) { - - PolicyParameters policyParameters = new PolicyParameters(); - - // Set Policy Type - policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); - policyParameters.setEcompName(refProp.getStringValue("policy.onap.name")); - policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); - - policyParameters.setConfigBody(policyJson); - policyParameters.setConfigBodyType(PolicyType.JSON); - - policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); - String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix"); - - // Adding this line to clear the policy id from policy name while - // pushing to policy engine - prop.setPolicyUniqueId(""); - - String rtnMsg = send(policyParameters, prop, policyNamePrefix); - String policyType = refProp.getStringValue("policy.ms.type"); - push(policyType, prop); - - return rtnMsg; - } - - /** - * Perform send of base policy in OTHER type. - * - * @param configBody - * The config policy string body - * @param configPolicyName - * The config policy name of the component that has been - * pre-deployed in DCAE - * @param prop - * The ModelProperties - * @param policyRequestUuid - * The policy request UUID - * @return The answer from policy call - */ - public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop, - String policyRequestUuid) { - PolicyParameters policyParameters = new PolicyParameters(); - - // Set Policy Type - policyParameters.setPolicyConfigType(PolicyConfigType.Base); - policyParameters.setEcompName(refProp.getStringValue("policy.onap.name")); - policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); - - policyParameters.setConfigBody(configBody); - policyParameters.setConfigBodyType(PolicyType.OTHER); - policyParameters.setConfigName("HolmesPolicy"); - policyParameters.setPolicyName(configPolicyName); - - policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); - - // Adding this line to clear the policy id from policy name while - // pushing to policy engine - prop.setPolicyUniqueId(""); - - String rtnMsg = send(policyParameters, prop, POLICY_PREFIX_BASE); - push(PolicyConfigType.Base.toString(), prop); - - return rtnMsg; - } - - /** - * Perform send of Microservice policy in OTHER type. - * - * @param configBody - * The config policy string body - * @param prop - * The ModelProperties - * @param policyRequestUuid - * The policy request UUID - * @return The answer from policy call - */ - public String sendMicroServiceInOther(String configBody, ModelProperties prop, String policyRequestUuid) { - - PolicyParameters policyParameters = new PolicyParameters(); - // Set Policy Type - policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); - policyParameters.setOnapName("DCAE"); - policyParameters.setEcompName(refProp.getStringValue("policy.onap.name")); - policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); - policyParameters.setConfigBody(configBody); - String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix"); - - // Adding this line to clear the policy id from policy name while - // pushing to policy engine - prop.setPolicyUniqueId(""); - - String rtnMsg = send(policyParameters, prop, policyNamePrefix); - String policyType = refProp.getStringValue("policy.ms.type"); - push(policyType, prop); - - return rtnMsg; - } - - /** - * Perform send of policy. - * - * @param policyParameters - * The PolicyParameters - * @param prop - * The ModelProperties - * @return The response message of Policy - */ - protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) { - // Verify whether it is triggered by Validation Test button from UI - if (prop.isTest()) { - return "send not executed for test action"; - } - - // API method to create or update Policy. - PolicyChangeResponse response = null; - String responseMessage = ""; - Date startTime = new Date(); - try { - List versions = getVersions(policyNamePrefix, prop); - if (versions.isEmpty()) { - LoggingUtils.setTargetContext("Policy", "createPolicy"); - logger.info("Attempting to create policy for action=" + prop.getActionCd()); - response = getPolicyEngine().createPolicy(policyParameters); - responseMessage = response.getResponseMessage(); - } else { - LoggingUtils.setTargetContext("Policy", "updatePolicy"); - logger.info("Attempting to update policy for action=" + prop.getActionCd()); - response = getPolicyEngine().updatePolicy(policyParameters); - responseMessage = response.getResponseMessage(); - } - } catch (Exception e) { - logger.error("Exception occurred during policy communication", e); - throw new PolicyClientException("Exception while communicating with Policy", e); - } - logger.info(LOG_POLICY_PREFIX + responseMessage); - - LoggingUtils.setTimeContext(startTime, new Date()); - - if (response.getResponseCode() == 200) { - logger.info("Policy send successful"); - metricsLogger.info("Policy send success"); - } else { - logger.warn("Policy send failed: " + responseMessage); - metricsLogger.info("Policy send failure"); - throw new BadRequestException("Policy send failed: " + responseMessage); - } - - return responseMessage; - } - - /** - * Format and send push of policy. - * - * @param policyType - * The policy Type - * @param prop - * The ModelProperties - * @return The response message of policy - */ - protected String push(String policyType, ModelProperties prop) { - // Verify whether it is triggered by Validation Test button from UI - if (prop.isTest()) { - return "push not executed for test action"; - } - - PushPolicyParameters pushPolicyParameters = new PushPolicyParameters(); - - // Parameter arguments - if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) { - pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()); - } else { - pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); - } - logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName()); - - pushPolicyParameters.setPolicyType(policyType); - pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group")); - pushPolicyParameters.setRequestID(null); - - // API method to create or update Policy. - PolicyChangeResponse response = null; - String responseMessage = ""; - try { - logger.info("Attempting to push policy..."); - response = getPolicyEngine().pushPolicy(pushPolicyParameters); - responseMessage = response.getResponseMessage(); - } catch (Exception e) { - logger.error("Exception occurred during policy communication", e); - throw new PolicyClientException("Exception while communicating with Policy", e); - } - logger.info(LOG_POLICY_PREFIX + responseMessage); - - if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) { - logger.info("Policy push successful"); - } else { - logger.warn("Policy push failed: " + responseMessage); - throw new BadRequestException("Policy push failed: " + responseMessage); - } - - return responseMessage; - } - - /** - * Use Get Config Policy API to retrieve the versions for a policy. Return - * versions in sorted order. Return empty list if none found. - * - * @param policyNamePrefix - * The Policy Name Prefix - * @param prop - * The ModelProperties - * @return The response message from policy - * @throws PolicyConfigException - * In case of issues with policy engine - */ - protected List getVersions(String policyNamePrefix, ModelProperties prop) throws PolicyConfigException { - - ArrayList versions = new ArrayList<>(); - ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); - String policyName = ""; - - if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) { - policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId(); - } else { - policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix); - } - - logger.info("policyName=" + policyName); - configRequestParameters.setPolicyName(policyName); - try { - Collection response = getPolicyEngine().getConfig(configRequestParameters); - for (PolicyConfig policyConfig : response) { - Integer version = Integer.valueOf(policyConfig.getPolicyVersion()); - versions.add(version); - } - Collections.sort(versions); - logger.info("Policy versions.size()=" + versions.size()); - } catch (PolicyConfigException e) { - // just print warning - if no policy version found - logger.warn("warning: policy not found...policy name - " + policyName, e.getMessage()); - } - return versions; - - } - - /** - * This method create a new policy engine. - * - * @return A new policy engine - */ - private PolicyEngine getPolicyEngine() { - PolicyEngine policyEngine; - try { - policyEngine = new PolicyEngine(appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath()); - } catch (IOException e1) { - throw new PolicyClientException("Exception when opening policy config file", e1); - } catch (PolicyEngineException e) { - throw new PolicyClientException("Exception when creating a new policy engine", e); - } - return policyEngine; - } - - /** - * Format and send delete Micro Service requests to Policy. - * - * @param prop - * The ModelProperties - * @return The response message from Policy - */ - public String deleteMicrosService(ModelProperties prop) { - String policyType = refProp.getStringValue("policy.ms.type"); - return deletePolicy(prop, policyType); - } - - /** - * This method delete the Base policy. - * - * @param prop - * The model Properties - * @return A string with the answer from policy - */ - public String deleteBasePolicy(ModelProperties prop) { - return deletePolicy(prop, PolicyConfigType.Base.toString()); - } - - /** - * Format and send delete BRMS requests to Policy. - * - * @param prop - * The ModelProperties - * @return The response message from policy - */ - public String deleteBrms(ModelProperties prop) { - String policyType = refProp.getStringValue("policy.op.type"); - return deletePolicy(prop, policyType); - } - - /** - * Format and send delete PAP and PDP requests to Policy. - * - * @param prop - * The ModelProperties - * - * @return The response message from policy - */ - protected String deletePolicy(ModelProperties prop, String policyType) { - DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters(); - - if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) { - deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()); - } else { - deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); - } - logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName()); - deletePolicyParameters.setPolicyComponent("PDP"); - deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL); - deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group")); - deletePolicyParameters.setPolicyType(policyType); - // send delete request - StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop)); - - logger.info("Deleting policy from PAP..."); - deletePolicyParameters.setPolicyComponent("PAP"); - deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL); - - // send delete request - responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop)); - - return responseMessage.toString(); - } - - /** - * Send delete request to Policy. - * - * @param deletePolicyParameters - * The DeletePolicyParameters - * @param prop - * The ModelProperties - * @return The response message from policy - */ - protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) { - // Verify whether it is triggered by Validation Test button from UI - if (prop.isTest()) { - return "delete not executed for test action"; - } - - // API method to create or update Policy. - PolicyChangeResponse response = null; - String responseMessage = ""; - try { - logger.info("Attempting to delete policy..."); - response = getPolicyEngine().deletePolicy(deletePolicyParameters); - responseMessage = response.getResponseMessage(); - } catch (Exception e) { - logger.error("Exception occurred during policy communnication", e); - } - logger.info(LOG_POLICY_PREFIX + responseMessage); - - if (response != null && response.getResponseCode() == 200) { - logger.info("Policy delete successful"); - } else { - logger.warn("Policy delete failed: " + responseMessage); - throw new BadRequestException("Policy delete failed: " + responseMessage); - } - - return responseMessage; - } -} \ No newline at end of file diff --git a/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java b/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java deleted file mode 100644 index 36265e83..00000000 --- a/src/main/java/org/onap/clamp/clds/client/SdcCatalogServices.java +++ /dev/null @@ -1,1304 +0,0 @@ -/*- - * ============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.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.security.GeneralSecurityException; -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.exception.SdcCommunicationException; -import org.onap.clamp.clds.model.CldsAlarmCondition; -import org.onap.clamp.clds.model.CldsDBServiceCache; -import org.onap.clamp.clds.model.CldsSdcArtifact; -import org.onap.clamp.clds.model.CldsSdcResource; -import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo; -import org.onap.clamp.clds.model.CldsSdcServiceDetail; -import org.onap.clamp.clds.model.CldsSdcServiceInfo; -import org.onap.clamp.clds.model.CldsServiceData; -import org.onap.clamp.clds.model.CldsVfData; -import org.onap.clamp.clds.model.CldsVfKPIData; -import org.onap.clamp.clds.model.CldsVfcData; -import org.onap.clamp.clds.model.prop.Global; -import org.onap.clamp.clds.model.prop.ModelProperties; -import org.onap.clamp.clds.model.refprop.RefProp; -import org.onap.clamp.clds.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"; - private static final String RESOURCE_CVFC_TYPE = "CVFC"; - @Autowired - private RefProp refProp; - @Autowired - private SdcReq sdcReq; - - /** - * This method get the SDC services Information with the corresponding - * Service UUID. - * - * @param uuid - * The service UUID - * @return A Json String with all the service list - * @throws GeneralSecurityException - * In case of issue when decryting the SDC password - */ - public String getSdcServicesInformation(String uuid) throws GeneralSecurityException { - Date startTime = new Date(); - String baseUrl = refProp.getStringValue("sdc.serviceUrl"); - String basicAuth = sdcReq.getSdcBasicAuth(); - LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation"); - try { - String url = baseUrl; - if (uuid != null) { - url = baseUrl + "/" + uuid + "/metadata"; - } - URL urlObj = new URL(url); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), "CLAMP-Tool"); - conn.setRequestProperty("Authorization", basicAuth); - conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - conn.setRequestMethod("GET"); - String resp = getResponse(conn); - if (resp != null) { - logger.info(resp); - // 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.isEmpty()) { - // 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.isEmpty()) { - // 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.isEmpty()) { - // sort list - Collections.sort(rawCldsSdcResourceListBasicList); - // and then take only the resources with the max version (last in - // the list with the same name) - cldsSdcResourceBasicInfoList = new ArrayList<>(); - for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) { - // compare name with previous - if not equal, then keep the - // previous (it's the last with that name) - CldsSdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1); - 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 GeneralSecurityException - * In case of issue when decryting the SDC password - */ - public String getServiceUuidFromServiceInvariantId(String invariantId) throws GeneralSecurityException { - String serviceUuid = ""; - String responseStr = getSdcServicesInformation(null); - List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); - List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); - if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) { - 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 - * The Json string that must be decoded - * @return The list of CldsSdcServiceInfo, if there is a failure it return - * an empty list - */ - public List getCldsSdcServicesListFromJson(String jsonStr) { - ObjectMapper objectMapper = new ObjectMapper(); - if (StringUtils.isBlank(jsonStr)) { - return new ArrayList<>(); - } - try { - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class)); - } catch (IOException e) { - logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e); - return new ArrayList<>(); - } - } - - /** - * To get List of CldsSdcResourceBasicInfo class by parsing json string. - * - * @param jsonStr - * The JSOn string that must be decoded - * @return The list of CldsSdcResourceBasicInfo, an empty list in case of - * issues - */ - public List getAllSdcResourcesListFromJson(String jsonStr) { - ObjectMapper objectMapper = new ObjectMapper(); - if (StringUtils.isBlank(jsonStr)) { - return new ArrayList<>(); - } - try { - return objectMapper.readValue(jsonStr, - objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); - } catch (IOException e) { - logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e); - return new ArrayList<>(); - } - } - - /** - * 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 - */ - public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) { - ObjectMapper objectMapper = new ObjectMapper(); - try { - return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); - } catch (IOException e) { - logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e); - return null; - } - } - - /** - * To upload artifact to sdc based on serviceUUID and resource name on url. - * - * @param prop - * @param userid - * @param url - * @param formattedSdcReq - * @return - * @throws GeneralSecurityException - */ - public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formattedSdcReq) - throws GeneralSecurityException { - // Verify whether it is triggered by Validation Test button from UI - if (prop.isTest()) { - return "sdc artifact upload not executed for test action"; - } - try { - logger.info("userid=" + userid); - String md5Text = sdcReq.calculateMD5ByString(formattedSdcReq); - byte[] postData = sdcReq.stringToByteArray(formattedSdcReq); - int postDataLength = postData.length; - HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, md5Text); - try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { - wr.write(postData); - } - boolean requestFailed = true; - int responseCode = conn.getResponseCode(); - logger.info("responseCode=" + responseCode); - if (responseCode == 200) { - requestFailed = false; - } - String responseStr = getResponse(conn); - if (responseStr != null && requestFailed) { - logger.error("requestFailed - responseStr=" + responseStr); - throw new BadRequestException(responseStr); - } - return responseStr; - } catch (IOException e) { - logger.error("Exception when attempting to communicate with SDC", e); - throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e); - } - } - - private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String md5Text) - throws GeneralSecurityException { - try { - logger.info("userid=" + userid); - String basicAuth = sdcReq.getSdcBasicAuth(); - String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID"); - URL urlObj = new URL(url); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - conn.setDoOutput(true); - conn.setRequestProperty(refProp.getStringValue("sdc.InstanceID"), sdcXonapInstanceId); - conn.setRequestProperty("Authorization", basicAuth); - conn.setRequestProperty("Content-Type", "application/json"); - conn.setRequestProperty("Content-MD5", md5Text); - conn.setRequestProperty("USER_ID", userid); - conn.setRequestMethod("POST"); - conn.setRequestProperty("charset", "utf-8"); - conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); - conn.setUseCaches(false); - conn.setRequestProperty("X-ECOMP-RequestID", LoggingUtils.getRequestId()); - return conn; - } catch (IOException e) { - logger.error("Exception when attempting to open connection with SDC", e); - throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e); - } - } - - private String getResponse(HttpURLConnection conn) { - 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(); - } - } else { - return null; - } - } catch (IOException e) { - logger.error("Exception when attempting to open SDC response", e); - throw new SdcCommunicationException("Exception when attempting to open SDC response", e); - } - } - - private InputStream getInputStream(HttpURLConnection conn) { - try { - InputStream inStream = conn.getErrorStream(); - if (inStream == null) { - inStream = conn.getInputStream(); - } - return inStream; - } catch (IOException e) { - logger.error("Exception when attempting to open SDC error stream", e); - throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e); - } - } - - public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) { - try { - CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache(); - cldsDbServiceCache.setCldsDataInstream(cldsServiceData); - cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID()); - cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID()); - return cldsDbServiceCache; - } catch (IOException e) { - logger.error("Exception when getting service in cache", e); - throw new SdcCommunicationException("Exception when getting service in cache", e); - } - } - - /** - * Check if the SDC Info in cache has expired. - * - * @param cldsServiceData - * @return - * @throws GeneralSecurityException - */ - public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) throws GeneralSecurityException { - 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; - } - - /** - * Get the Service Data with Alarm Conditions for a given - * invariantServiceUuid. - * - * @param invariantServiceUuid - * @return - * @throws GeneralSecurityException - */ - public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) - throws GeneralSecurityException { - String url = refProp.getStringValue("sdc.serviceUrl"); - String catalogUrl = refProp.getStringValue("sdc.catalog.url"); - String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); - String serviceDetailUrl = url + "/" + serviceUuid + "/metadata"; - String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false); - ObjectMapper objectMapper = new ObjectMapper(); - CldsServiceData cldsServiceData = new CldsServiceData(); - if (responseStr != null) { - CldsSdcServiceDetail cldsSdcServiceDetail; - try { - cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class); - } catch (IOException e) { - logger.error("Exception when decoding the CldsServiceData JSON from SDC", e); - throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e); - } - cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid()); - cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID()); - // To remove duplicate resources from serviceDetail and add valid - // vfs to service - if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { - List cldsSdcResourceList = removeDuplicateSdcResourceInstances( - cldsSdcServiceDetail.getResources()); - if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) { - List cldsVfDataList = new ArrayList<>(); - for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) { - if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null - && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) { - 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; - } - - private void getAllVfcForVfList(List cldsVfDataList, String catalogUrl) - throws GeneralSecurityException { - // todo : refact this.. - if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { - List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType( - RESOURCE_VF_TYPE); - List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( - RESOURCE_VFC_TYPE); - allVfcResources.addAll(getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_CVFC_TYPE)); - for (CldsVfData currCldsVfData : cldsVfDataList) { - if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) { - String resourceUuid = getResourceUuidFromResourceInvariantUuid( - 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.isEmpty()) { - // 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 GeneralSecurityException { - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode; - try { - vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); - } catch (IOException e) { - logger.error("Exception when decoding the JSON list of CldsVfcData", e); - return new ArrayList<>(); - } - ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); - List cldsVfcDataList = new ArrayList<>(); - if (vfcArrayNode != null) { - 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())) { - TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName"); - TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID"); - currCldsVfcData.setVfcName(vfcResourceName.textValue()); - currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue()); - cldsVfcDataList.add(currCldsVfcData); - cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue())); - } - } - } - return cldsVfcDataList; - } - - private List getVFCfromCVFC(String resourceUUID) throws GeneralSecurityException { - String catalogUrl = refProp.getStringValue("sdc.catalog.url"); - List cldsVfcDataList = new ArrayList<>(); - if (resourceUUID != null) { - String vfcResourceUUIDUrl = catalogUrl + "resources" + "/" + resourceUUID + "/metadata"; - try { - 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) { - return (id != null) ? id.replaceAll("\"", "") : ""; - } - - private List getAlarmCondtionsFromVfc(String vfcResponse) throws GeneralSecurityException { - List cldsAlarmConditionList = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfcResponseNode; - try { - vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); - } catch (IOException e) { - logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e); - return cldsAlarmConditionList; - } - ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts"); - if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) { - for (int index = 0; index < artifactsArrayNode.size(); index++) { - ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index); - 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) { - try { - List cldsAlarmConditionList = new ArrayList<>(); - Reader alarmReader = new StringReader(allAlarmCondsValues); - Iterable records = CSVFormat.RFC4180.parse(alarmReader); - if (records != null) { - Iterator it = records.iterator(); - if (it.hasNext()) { - it.next(); - } - it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record)); - } - return cldsAlarmConditionList; - } catch (IOException e) { - logger.error("Exception when attempting to parse the CSV containing the alarm", e); - return new ArrayList<>(); - } - } - - // Method to get the artifact for any particular VF - private List getFieldPathFromVF(String vfResponse) throws GeneralSecurityException { - List cldsVfKPIDataList = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); - ObjectNode vfResponseNode; - try { - vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); - } catch (IOException e) { - logger.error("Exception when decoding the JSON list of CldsVfKPIData", e); - return cldsVfKPIDataList; - } - ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts"); - if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) { - for (int index = 0; index < artifactsArrayNode.size(); index++) { - ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index); - 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 && "csv".equalsIgnoreCase(artifactName)) { - 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) { - try { - List cldsVfKPIDataList = new ArrayList<>(); - Reader alarmReader = new StringReader(allFieldPathValues); - Iterable records = CSVFormat.RFC4180.parse(alarmReader); - if (records != null) { - for (CSVRecord record : records) { - CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record); - if (kpiData != null) { - cldsVfKPIDataList.add(kpiData); - } - } - } - return cldsVfKPIDataList; - } catch (IOException e) { - logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e); - return new ArrayList<>(); - } - } - - 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); - } - - /** - * Get the responses for the current artifact from the artifacts URL. - * - * @param artifactsUrl - * @return - * @throws GeneralSecurityException - */ - public String getResponsesFromArtifactUrl(String artifactsUrl) throws GeneralSecurityException { - String hostUrl = refProp.getStringValue("sdc.hostUrl"); - String artifactsUrlReworked = artifactsUrl.replaceAll("\"", ""); - String artifactUrl = hostUrl + artifactsUrlReworked; - 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 alarm conditons from artifact url and else it is false - * - * @param url - * @param alarmConditions - * @return - * @throws GeneralSecurityException - * In case of issue when decrypting the SDC password - * - */ - public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) - throws GeneralSecurityException { - Date startTime = new Date(); - try { - LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL"); - String urlReworked = removeUnwantedBracesFromString(url); - URL urlObj = new URL(urlReworked); - HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); - String basicAuth = sdcReq.getSdcBasicAuth(); - 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("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode); - StringBuilder response; - try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { - response = new StringBuilder(); - String inputLine; - while ((inputLine = in.readLine()) != null) { - if (!inputLine.isEmpty()) { - response.append(inputLine); - } - if (alarmConditions) { - response.append("\n"); - } - } - } - LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName()); - return response.toString(); - } catch (IOException e) { - LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName()); - LoggingUtils.setErrorContext("900", "Get sdc resources error"); - logger.error("Exception occurred during query to SDC", e); - return ""; - } finally { - LoggingUtils.setTimeContext(startTime, new Date()); - metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed"); - } - } - - /** - * To create properties object by using cldsServicedata. - * - * @param globalProps - * @param cldsServiceData - * @return - */ - public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) { - String totalPropsStr; - ObjectMapper mapper = new ObjectMapper(); - ObjectNode globalPropsJson; - if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { - // Objectnode to save all byservice, byvf , byvfc and byalarm nodes - ObjectNode byIdObjectNode = mapper.createObjectNode(); - // To create vf ResourceUUID node with serviceInvariantUUID - ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(mapper, - cldsServiceData); - byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf); - // To create byVf and vfcResourceNode with vfResourceUUID - ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs()); - byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid); - // To create byKpi - ObjectNode kpiObjectNode = mapper.createObjectNode(); - if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { - 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().isEmpty()) { - 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 = decodeGlobalProp(globalProps, mapper); - globalPropsJson.putPOJO("shared", byIdObjectNode); - logger.info("value of objNode:" + globalPropsJson); - } else { - /** - * to create json with total properties when no serviceUUID passed - */ - globalPropsJson = decodeGlobalProp(globalProps, mapper); - } - totalPropsStr = globalPropsJson.toString(); - return totalPropsStr; - } - - private ObjectNode decodeGlobalProp(String globalProps, ObjectMapper mapper) { - try { - return (ObjectNode) mapper.readValue(globalProps, JsonNode.class); - } catch (IOException e) { - logger.error("Exception occurred during decoding of the global props, returning an empty objectNode", e); - return mapper.createObjectNode(); - } - } - - /** - * Method to get alarm conditions/alert description from Service Data. - * - * @param cldsServiceData - * CldsServiceData the Service Data to analyze - * @param eventName - * 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.isEmpty()) { - 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.isEmpty()) { - 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.isEmpty()) { - 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.isEmpty()) { - for (CldsVfcData currCldsVfcData : cldsVfcDataList) { - if (currCldsVfcData != null) { - if (currCldsVfcData.getCldsAlarmConditions() != null - && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { - for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { - if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) { - alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), - currCldsAlarmCondition.getAlarmConditionKey()); - } else { - alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(), - currCldsAlarmCondition.getAlarmConditionKey()); - } - } - } - vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); - vfcObjectNode.putPOJO("alertDescription", alertDescNode); - vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode); - } - } - } else { - alarmCondNode.put("", ""); - vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); - alertDescNode.put("", ""); - vfcObjectNode.putPOJO("alertDescription", alarmCondNode); - vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode); - } - } - - /** - * Method to create vfc and kpi nodes inside vf node - * - * @param mapper - * @param cldsVfDataList - * @return - */ - private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List cldsVfDataList) { - ObjectNode vfUuidObjectNode = mapper.createObjectNode(); - if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { - for (CldsVfData currCldsVfData : cldsVfDataList) { - if (currCldsVfData != null) { - ObjectNode vfObjectNode = mapper.createObjectNode(); - ObjectNode vfcUuidNode = mapper.createObjectNode(); - ObjectNode kpiObjectNode = mapper.createObjectNode(); - if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) { - for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { - if (currCldsVfcData.getCldsAlarmConditions() != null - && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { - vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), - currCldsVfcData.getVfcName()); - } - } - } else { - vfcUuidNode.put("", ""); - } - if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) { - for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) { - kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(), - currCldsVfKPIData.getThresholdValue()); - } - } else { - kpiObjectNode.put("", ""); - } - vfObjectNode.putPOJO("vfc", vfcUuidNode); - vfObjectNode.putPOJO("kpi", kpiObjectNode); - vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode); - } - } - } else { - 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 - && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) { - artifactUuid = currCldsSdcArtifact.getArtifactUUID(); - artifactExists = true; - break; - } - } - } - } - } - return artifactUuid; - } - - public String updateControlLoopStatusToDcae(String dcaeUrl, String invariantResourceUuid, - String invariantServiceUuid, String artifactName) throws GeneralSecurityException { - String baseUrl = refProp.getStringValue("sdc.serviceUrl"); - String basicAuth = sdcReq.getSdcBasicAuth(); - 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 GeneralSecurityException - * In case of issue when decryting the SDC password - * - */ - private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) - throws GeneralSecurityException { - String catalogUrl = refProp.getStringValue("sdc.catalog.url"); - String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType; - String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false); - return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources)); - } - - private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid, - List resourceInfoList) { - String resourceUuid = null; - if (resourceInfoList != null && !resourceInfoList.isEmpty()) { - 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 GeneralSecurityException - * In case of issues to decrypt the SDC password - */ - public void uploadToSdc(ModelProperties prop, String userid, List sdcReqUrlsList, String formattedSdcReq, - String formattedSdcLocationReq, String artifactName, String locationArtifactName) - throws GeneralSecurityException { - 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/client/SdcSendReqDelegate.java b/src/main/java/org/onap/clamp/clds/client/SdcSendReqDelegate.java index 90bdcb3a..b582349f 100644 --- a/src/main/java/org/onap/clamp/clds/client/SdcSendReqDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/SdcSendReqDelegate.java @@ -30,7 +30,8 @@ import java.util.List; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.clamp.clds.client.req.SdcReq; +import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices; +import org.onap.clamp.clds.client.req.sdc.SdcReq; import org.onap.clamp.clds.model.DcaeEvent; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.refprop.RefProp; diff --git a/src/main/java/org/onap/clamp/clds/client/TcaPolicyDelegate.java b/src/main/java/org/onap/clamp/clds/client/TcaPolicyDelegate.java index 7de12683..2d7df844 100644 --- a/src/main/java/org/onap/clamp/clds/client/TcaPolicyDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/TcaPolicyDelegate.java @@ -30,7 +30,8 @@ import java.util.UUID; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; -import org.onap.clamp.clds.client.req.TcaRequestFormatter; +import org.onap.clamp.clds.client.req.policy.PolicyClient; +import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.prop.Tca; import org.onap.clamp.clds.model.refprop.RefProp; @@ -44,10 +45,8 @@ import org.springframework.beans.factory.annotation.Autowired; public class TcaPolicyDelegate implements JavaDelegate { protected static final EELFLogger logger = EELFManager.getInstance().getLogger(TcaPolicyDelegate.class); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - @Autowired private RefProp refProp; - @Autowired PolicyClient policyClient; @@ -60,16 +59,14 @@ public class TcaPolicyDelegate implements JavaDelegate { public void execute(DelegateExecution execution) throws Exception { String tcaPolicyRequestUuid = UUID.randomUUID().toString(); execution.setVariable("tcaPolicyRequestUuid", tcaPolicyRequestUuid); - ModelProperties prop = ModelProperties.create(execution); Tca tca = prop.getType(Tca.class); if (tca.isFound()) { String policyJson = TcaRequestFormatter.createPolicyJson(refProp, prop); - String responseMessage = policyClient.sendMicroServiceInOther(policyJson, prop, tcaPolicyRequestUuid); + String responseMessage = policyClient.sendMicroServiceInOther(policyJson, prop); if (responseMessage != null) { execution.setVariable("tcaPolicyResponseMessage", responseMessage.getBytes()); } } } - } diff --git a/src/main/java/org/onap/clamp/clds/client/TcaPolicyDeleteDelegate.java b/src/main/java/org/onap/clamp/clds/client/TcaPolicyDeleteDelegate.java index d3fb3f73..e88b720a 100644 --- a/src/main/java/org/onap/clamp/clds/client/TcaPolicyDeleteDelegate.java +++ b/src/main/java/org/onap/clamp/clds/client/TcaPolicyDeleteDelegate.java @@ -25,6 +25,7 @@ package org.onap.clamp.clds.client; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.camunda.bpm.engine.delegate.JavaDelegate; +import org.onap.clamp.clds.client.req.policy.PolicyClient; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.prop.Tca; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/org/onap/clamp/clds/client/req/OperationalPolicyReq.java b/src/main/java/org/onap/clamp/clds/client/req/OperationalPolicyReq.java deleted file mode 100644 index a5506da9..00000000 --- a/src/main/java/org/onap/clamp/clds/client/req/OperationalPolicyReq.java +++ /dev/null @@ -1,451 +0,0 @@ -/*- - * ============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.req; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFLogger.Level; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ObjectNode; - -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.ws.rs.BadRequestException; - -import org.onap.clamp.clds.model.prop.Global; -import org.onap.clamp.clds.model.prop.ModelProperties; -import org.onap.clamp.clds.model.prop.PolicyChain; -import org.onap.clamp.clds.model.prop.PolicyItem; -import org.onap.clamp.clds.model.prop.Tca; -import org.onap.clamp.clds.model.refprop.RefProp; -import org.onap.policy.api.AttributeType; -import org.onap.policy.sdc.Resource; -import org.onap.policy.sdc.ResourceType; -import org.onap.policy.sdc.Service; -import org.onap.policy.controlloop.policy.OperationsAccumulateParams; -import org.onap.policy.controlloop.policy.Policy; -import org.onap.policy.controlloop.policy.PolicyResult; -import org.onap.policy.controlloop.policy.Target; -import org.onap.policy.controlloop.policy.TargetType; -import org.onap.policy.controlloop.policy.builder.BuilderException; -import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; -import org.onap.policy.controlloop.policy.builder.Message; -import org.onap.policy.controlloop.policy.builder.Results; - -/** - * Construct an Operational Policy request given CLDS objects. - */ -public class OperationalPolicyReq { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyReq.class); - protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - - private OperationalPolicyReq() { - - } - - /** - * Format Operational Policy attributes. - * - * @param refProp - * @param prop - * @return - * @throws BuilderException - * @throws UnsupportedEncodingException - */ - public static Map> formatAttributes(RefProp refProp, ModelProperties prop, - String modelElementId, PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { - Global global = prop.getGlobal(); - prop.setCurrentModelElementId(modelElementId); - prop.setPolicyUniqueId(policyChain.getPolicyId()); - - String templateName = ""; - String operationTopic = ""; - String notificationTopic = ""; - String controller = ""; - Tca tca = prop.getType(Tca.class); - if (tca != null && tca.isFound()) { - if (!global.getActionSet().equalsIgnoreCase("enbRecipe")) { - throw new BadRequestException( - "Operation Policy validation problem: action set is not selected properly."); - } - templateName = refProp.getStringValue("op.eNodeB.templateName", global.getService()); - operationTopic = refProp.getStringValue("op.eNodeB.operationTopic", global.getService()); - notificationTopic = refProp.getStringValue("op.eNodeB.notificationTopic", global.getService()); - controller = refProp.getStringValue("op.eNodeB.controller", global.getService()); - } else { - if (!global.getActionSet().equalsIgnoreCase("vnfRecipe")) { - throw new BadRequestException( - "Operation Policy validation problem: Action set is not selected properly."); - } - templateName = refProp.getStringValue("op.templateName", global.getService()); - operationTopic = refProp.getStringValue("op.operationTopic", global.getService()); - notificationTopic = refProp.getStringValue("op.notificationTopic", global.getService()); - controller = refProp.getStringValue("op.controller", global.getService()); - } - String recipeTopic = refProp.getStringValue("op.recipeTopic", global.getService()); - - // ruleAttributes - Map ruleAttributes = new HashMap<>(); - - if (operationTopic == null || operationTopic.isEmpty()) { - logger.info("templateName=" + templateName); - logger.info("recipeTopic=" + recipeTopic); - logger.info("notificationTopic=" + notificationTopic); - - // if no operationTopic, then don't format yaml - use first policy - // from list - PolicyItem policyItem = policyChain.getPolicyItems().get(0); - - ruleAttributes.put("templateName", templateName); - ruleAttributes.put("ClosedLoopControlName", prop.getControlNameAndPolicyUniqueId()); - ruleAttributes.put("RecipeTopic", recipeTopic); - ruleAttributes.put("NotificationTopic", notificationTopic); - - String recipe = policyItem.getRecipe(); - String maxRetries = String.valueOf(policyItem.getMaxRetries()); - String retryTimeLimit = String.valueOf(policyItem.getRetryTimeLimit()); - String targetResourceId = String.valueOf(policyItem.getTargetResourceId()); - logger.info("recipe=" + recipe); - logger.info("maxRetries=" + maxRetries); - logger.info("retryTimeLimit=" + retryTimeLimit); - logger.info("targetResourceId=" + targetResourceId); - ruleAttributes.put("Recipe", recipe); - ruleAttributes.put("MaxRetries", maxRetries); - ruleAttributes.put("RetryTimeLimit", retryTimeLimit); - ruleAttributes.put("ResourceId", targetResourceId); - - } else { - logger.info("templateName=" + templateName); - logger.info("operationTopic=" + operationTopic); - logger.info("notificationTopic=" + notificationTopic); - - // format yaml - String yaml = (tca != null && tca.isFound()) ? formateNodeBYaml(refProp, prop, modelElementId, policyChain) - : formatYaml(refProp, prop, modelElementId, policyChain); - - ruleAttributes.put("templateName", templateName); - ruleAttributes.put("ClosedLoopControlName", prop.getControlNameAndPolicyUniqueId()); - ruleAttributes.put("OperationTopic", operationTopic); - ruleAttributes.put("NotificationTopic", notificationTopic); - - ruleAttributes.put("ControlLoopYaml", yaml); - } - - // matchingAttributes - Map matchingAttributes = new HashMap<>(); - matchingAttributes.put("controller", controller); - - Map> attributes = new HashMap<>(); - attributes.put(AttributeType.RULE, ruleAttributes); - attributes.put(AttributeType.MATCHING, matchingAttributes); - - return attributes; - } - - /** - * Format Operational Policy yaml. - * - * @param refProp - * @param prop - * @return - * @throws BuilderException - * @throws UnsupportedEncodingException - */ - public static String formatYaml(RefProp refProp, ModelProperties prop, String modelElementId, - PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { - - // get property objects - Global global = prop.getGlobal(); - prop.setCurrentModelElementId(modelElementId); - prop.setPolicyUniqueId(policyChain.getPolicyId()); - - // convert values to SDC objects - Service service = new Service(global.getService()); - Resource[] vfResources = convertToResource(global.getResourceVf(), ResourceType.VF); - Resource[] vfcResources = convertToResource(global.getResourceVfc(), ResourceType.VFC); - - // create builder - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(), - policyChain.getTimeout(), service, vfResources); - builder.addResource(vfcResources); - - // process each policy - Map policyObjMap = new HashMap<>(); - List policyItemList = orderParentFirst(policyChain.getPolicyItems()); - for (PolicyItem policyItem : policyItemList) { - String policyName = policyItem.getRecipe() + " Policy"; - Target target = new Target(); - target.setType(TargetType.VM); - target.setResourceID(policyItem.getTargetResourceId()); - Policy policyObj; - if (policyItemList.indexOf(policyItem) == 0) { - String policyDescription = policyItem.getRecipe() - + " Policy - the trigger (no parent) policy - created by CLDS"; - policyObj = builder.setTriggerPolicy(policyName, policyDescription, - refProp.getStringValue("op.policy.appc"), target, policyItem.getRecipe(), null, - policyItem.getMaxRetries(), policyItem.getRetryTimeLimit()); - } else { - Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy()); - String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by " - + parentPolicyObj.getName() + " - created by CLDS"; - policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription, - refProp.getStringValue("op.policy.appc"), target, policyItem.getRecipe(), null, - policyItem.getMaxRetries(), policyItem.getRetryTimeLimit(), parentPolicyObj.getId(), - convertToPolicyResult(policyItem.getParentPolicyConditions())); - - logger.info("policyObj.id=" + policyObj.getId() + "; parentPolicyObj.id=" + parentPolicyObj.getId()); - } - policyObjMap.put(policyItem.getId(), policyObj); - } - - // - // Build the specification - // - Results results = builder.buildSpecification(); - if (results.isValid()) { - logger.info("results.getSpecification()=" + results.getSpecification()); - } else { - // throw exception with error info - StringBuilder sb = new StringBuilder(); - sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: "); - for (Message message : results.getMessages()) { - sb.append(message.getMessage()); - sb.append("; "); - } - throw new BadRequestException(sb.toString()); - } - return URLEncoder.encode(results.getSpecification(), "UTF-8"); - } - - /** - * Format Operational Policy yaml. - * - * @param refProp - * @param prop - * @return - * @throws BuilderException - * @throws UnsupportedEncodingException - */ - public static String formateNodeBYaml(RefProp refProp, ModelProperties prop, String modelElementId, - PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { - - // get property objects - Global global = prop.getGlobal(); - prop.setCurrentModelElementId(modelElementId); - prop.setPolicyUniqueId(policyChain.getPolicyId()); - - // convert values to SDC objects - Service service = new Service(global.getService()); - Resource[] vfResources = convertToResource(global.getResourceVf(), ResourceType.VF); - Resource[] vfcResources = convertToResource(global.getResourceVfc(), ResourceType.VFC); - - // create builder - ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(), - policyChain.getTimeout(), service, vfResources); - builder.addResource(vfcResources); - - // process each policy - Map policyObjMap = new HashMap<>(); - List policyItemList = addAOTSActorRecipe(refProp, global.getService(), - policyChain.getPolicyItems()); - - Policy lastPolicyObj = new Policy(); - for (PolicyItem policyItem : policyItemList) { - Target target = new Target(); - target.setType(TargetType.VM); - target.setResourceID(policyItem.getTargetResourceId()); - String policyName = policyItem.getRecipe() + " Policy"; - Policy policyObj; - if (policyItemList.indexOf(policyItem) == 0) { - // To set up time window payload for trigger policy - Map payloadMap = new HashMap<>(); - payloadMap.put("timeWindow", refProp.getStringValue("op.eNodeB.timeWindow")); - String policyDescription = policyItem.getRecipe() - + " Policy - the trigger (no parent) policy - created by CLDS"; - policyObj = builder.setTriggerPolicy(policyName, policyDescription, policyItem.getActor(), target, - policyItem.getRecipe(), payloadMap, policyItem.getMaxRetries(), policyItem.getRetryTimeLimit()); - } else { - Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy()); - String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by " - + parentPolicyObj.getName() + " - created by CLDS"; - policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription, policyItem.getActor(), - target, policyItem.getRecipe(), null, policyItem.getMaxRetries(), - policyItem.getRetryTimeLimit(), parentPolicyObj.getId(), - convertToPolicyResult(policyItem.getParentPolicyConditions())); - lastPolicyObj = policyObj; - logger.info("policyObj.id=" + policyObj.getId() + "; parentPolicyObj.id=" + parentPolicyObj.getId()); - } - policyObjMap.put(policyItem.getId(), policyObj); - } - // To set up operations accumulate params - OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); - operationsAccumulateParams.setLimit(Integer.valueOf(refProp.getStringValue("op.eNodeB.limit"))); - operationsAccumulateParams.setPeriod(refProp.getStringValue("op.eNodeB.period")); - builder.addOperationsAccumulateParams(lastPolicyObj.getId(), operationsAccumulateParams); - - // - // Build the specification - // - Results results = builder.buildSpecification(); - if (results.isValid()) { - logger.info("results.getSpecification()=" + results.getSpecification()); - } else { - // throw exception with error info - StringBuilder sb = new StringBuilder(); - sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: "); - for (Message message : results.getMessages()) { - sb.append(message.getMessage()); - sb.append("; "); - } - throw new BadRequestException(sb.toString()); - } - return URLEncoder.encode(results.getSpecification(), "UTF-8"); - } - - /** - * Adding AOTS actor and other recipe for yaml - * - * @param inOrigList - * @return - */ - private static List addAOTSActorRecipe(RefProp refProp, String service, List inOrigList) { - List outList = new ArrayList<>(); - try { - PolicyItem policyItem = inOrigList.get(0); - ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("op.eNodeB.recipe", service); - Iterator itr = rootNode.get("eNodeBRecipes").elements(); - while (itr.hasNext()) { - PolicyItem policyItemObj = (PolicyItem) policyItem.clone(); - JsonNode recipeNode = itr.next(); - policyItemObj.setId(recipeNode.path("Recipe").asText()); - policyItemObj.setActor(recipeNode.path("Actor").asText()); - policyItemObj.setRecipe(recipeNode.path("Recipe").asText()); - policyItemObj.setParentPolicy(recipeNode.path("ParentPolicy").asText()); - if (!recipeNode.path("Retry").asText().isEmpty()) { - policyItemObj.setMaxRetries(Integer.parseInt(recipeNode.path("Retry").asText())); - } - if (!recipeNode.path("TimeLimit").asText().isEmpty()) { - policyItemObj.setRetryTimeLimit(Integer.parseInt(recipeNode.path("TimeLimit").asText())); - } - if (!recipeNode.path("PPConditions").asText().isEmpty()) { - List parentPolicyConditions = new ArrayList<>(); - for (String ppCondition : recipeNode.path("PPConditions").asText().split(",")) { - parentPolicyConditions.add(ppCondition); - } - policyItemObj.setParentPolicyConditions(parentPolicyConditions); - } - outList.add(policyItemObj); - } - } catch (Exception e) { - logger.log(Level.ERROR, "Error", e); - } - return outList; - } - - /** - * Order list of PolicyItems so that parents come before any of their - * children - * - * @param inOrigList - * @return - */ - private static List orderParentFirst(List inOrigList) { - List inList = new ArrayList<>(); - inList.addAll(inOrigList); - List outList = new ArrayList<>(); - int prevSize = 0; - while (!inList.isEmpty()) { - // check if there's a loop in the policy chain (the inList should - // have been reduced by at least one) - if (inList.size() == prevSize) { - throw new BadRequestException("Operation Policy validation problem: loop in Operation Policy chain"); - } - prevSize = inList.size(); - // the following loop should remove at least one PolicyItem from the - // inList - Iterator inListItr = inList.iterator(); - while (inListItr.hasNext()) { - PolicyItem inItem = inListItr.next(); - // check for trigger policy (no parent) - String parent = inItem.getParentPolicy(); - if (parent == null || parent.length() == 0) { - if (!outList.isEmpty()) { - throw new BadRequestException( - "Operation Policy validation problem: more than one trigger policy"); - } else { - outList.add(inItem); - inListItr.remove(); - } - } else { - // check if this PolicyItem's parent has been processed - for (PolicyItem outItem : outList) { - if (outItem.getId().equals(parent)) { - // if the inItem parent is already in the outList, - // then add inItem to outList and remove from inList - outList.add(inItem); - inListItr.remove(); - break; - } - } - } - } - } - return outList; - } - - /** - * Convert a List of resource strings to an array of Resource objects. - * - * @param stringList - * @param resourceType - * @return - */ - private static Resource[] convertToResource(List stringList, ResourceType resourceType) { - if (stringList == null || stringList.isEmpty()) { - return new Resource[0]; - } - return stringList.stream().map(stringElem -> new Resource(stringElem, resourceType)).toArray(Resource[]::new); - } - - /** - * Convert a List of policy result strings to an array of PolicyResult - * objects. - * - * @param prList - * @return - */ - private static PolicyResult[] convertToPolicyResult(List prList) { - if (prList == null || prList.isEmpty()) { - return new PolicyResult[0]; - } - return prList.stream().map(stringElem -> PolicyResult.toResult(stringElem)).toArray(PolicyResult[]::new); - } - -} \ No newline at end of file diff --git a/src/main/java/org/onap/clamp/clds/client/req/SdcReq.java b/src/main/java/org/onap/clamp/clds/client/req/SdcReq.java deleted file mode 100644 index 38e3b15a..00000000 --- a/src/main/java/org/onap/clamp/clds/client/req/SdcReq.java +++ /dev/null @@ -1,317 +0,0 @@ -/*- - * ============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.req; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonParseException; -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 java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.security.GeneralSecurityException; -import java.util.ArrayList; -import java.util.Base64; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; - -import org.apache.commons.codec.digest.DigestUtils; -import org.camunda.bpm.engine.delegate.DelegateExecution; -import org.onap.clamp.clds.client.SdcCatalogServices; -import org.onap.clamp.clds.model.CldsSdcResource; -import org.onap.clamp.clds.model.CldsSdcServiceDetail; -import org.onap.clamp.clds.model.prop.Global; -import org.onap.clamp.clds.model.prop.ModelProperties; -import org.onap.clamp.clds.model.prop.Tca; -import org.onap.clamp.clds.model.refprop.RefProp; -import org.onap.clamp.clds.util.CryptoUtils; -import org.springframework.beans.factory.annotation.Autowired; - -/** - * Construct a Sdc request given CLDS objects. - */ -public class SdcReq { - @Autowired - protected CryptoUtils cryptoUtils; - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcReq.class); - protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - @Autowired - protected RefProp refProp; - - /** - * Format the Blueprint from a Yaml - * - * @param prop - * The ModelProperties describing the clds model - * @param docText - * The Yaml file that must be converted - * - * @return A String containing the BluePrint - * @throws JsonParseException - * In case of issues - * @throws JsonMappingException - * In case of issues - * @throws IOException - * In case of issues - */ - public String formatBlueprint(ModelProperties prop, String docText) - throws JsonParseException, JsonMappingException, IOException { - String yamlvalue = getYamlvalue(docText); - String updatedBlueprint = ""; - Tca tca = prop.getType(Tca.class); - if (tca.isFound()) { - updatedBlueprint = TcaRequestFormatter.updatedBlueprintWithConfiguration(refProp, prop, yamlvalue); - } - logger.info("value of blueprint:" + updatedBlueprint); - return updatedBlueprint; - } - - /** - * Format the SDC Locations Request in the JSON Format - * - * @param prop - * The ModelProperties describing the clds model - * @param artifactName - * The name of the artifact - * - * @return SDC Locations request in the JSON Format - */ - public String formatSdcLocationsReq(ModelProperties prop, String artifactName) { - ObjectMapper objectMapper = new ObjectMapper(); - Global global = prop.getGlobal(); - List locationsList = global.getLocation(); - ArrayNode locationsArrayNode = objectMapper.createArrayNode(); - ObjectNode locationObject = objectMapper.createObjectNode(); - for (String currLocation : locationsList) { - locationsArrayNode.add(currLocation); - } - locationObject.put("artifactName", artifactName); - locationObject.putPOJO("locations", locationsArrayNode); - String locationJsonFormat = locationObject.toString(); - logger.info("Value of location Json Artifact:" + locationsArrayNode); - return locationJsonFormat; - } - - /** - * Format the SDC Request - * - * @param payloadData - * The ModelProperties describing the clds model - * @param artifactName - * The name of the artifact - * @param artifactLabel - * The Label of the artifact - * @param artifactType - * The type of the artifact - * @return formatted SDC Request - * @throws IOException - * In case of issues - */ - public String formatSdcReq(String payloadData, String artifactName, String artifactLabel, String artifactType) - throws IOException { - logger.info("artifact=" + payloadData); - String base64Artifact = base64Encode(payloadData); - return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel - + "\",\n" + "\"artifactName\" :\"" + artifactName + "\",\n" + "\"artifactType\" : \"" + artifactType - + "\",\n" + "\"artifactGroupType\" : \"DEPLOYMENT\",\n" + "\"description\" : \"from CLAMP Cockpit\"\n" - + "} \n"; - } - - /** - * Get the SDC Request URL - * - * @param prop - * The ModelProperties describing the clds model - * @param url - * url - * @return SDC Request URL - */ - public String getSdcReqUrl(ModelProperties prop, String url) { - Global globalProps = prop.getGlobal(); - String serviceUUID = ""; - String resourceInstanceName = ""; - if (globalProps != null) { - List resourceVf = globalProps.getResourceVf(); - if (resourceVf != null && !resourceVf.isEmpty()) { - resourceInstanceName = resourceVf.get(0); - } - if (globalProps.getService() != null) { - serviceUUID = globalProps.getService(); - } - } - String normalizedResourceInstanceName = normalizeResourceInstanceName(resourceInstanceName); - return url + "/" + serviceUUID + "/resourceInstances/" + normalizedResourceInstanceName + "/artifacts"; - } - - /** - * To get List of urls for all vfresources - * - * @param prop - * @param baseUrl - * @param sdcCatalogServices - * @param execution - * @return - * @throws GeneralSecurityException - */ - public List getSdcReqUrlsList(ModelProperties prop, String baseUrl, SdcCatalogServices sdcCatalogServices, - DelegateExecution execution) throws GeneralSecurityException { - // TODO : refact and regroup with very similar code - List urlList = new ArrayList<>(); - Global globalProps = prop.getGlobal(); - if (globalProps != null) { - if (globalProps.getService() != null) { - String serviceInvariantUUID = globalProps.getService(); - execution.setVariable("serviceInvariantUUID", serviceInvariantUUID); - List resourceVfList = globalProps.getResourceVf(); - String serviceUUID = sdcCatalogServices.getServiceUuidFromServiceInvariantId(serviceInvariantUUID); - String sdcServicesInformation = sdcCatalogServices.getSdcServicesInformation(serviceUUID); - CldsSdcServiceDetail cldsSdcServiceDetail = sdcCatalogServices - .getCldsSdcServiceDetailFromJson(sdcServicesInformation); - if (cldsSdcServiceDetail != null && resourceVfList != null) { - List cldsSdcResourcesList = cldsSdcServiceDetail.getResources(); - if (cldsSdcResourcesList != null && !cldsSdcResourcesList.isEmpty()) { - for (CldsSdcResource CldsSdcResource : cldsSdcResourcesList) { - if (CldsSdcResource != null && CldsSdcResource.getResoucreType() != null - && CldsSdcResource.getResoucreType().equalsIgnoreCase("VF") - && resourceVfList.contains(CldsSdcResource.getResourceInvariantUUID())) { - String normalizedResourceInstanceName = normalizeResourceInstanceName( - CldsSdcResource.getResourceInstanceName()); - String svcUrl = baseUrl + "/" + serviceUUID + "/resourceInstances/" - + normalizedResourceInstanceName + "/artifacts"; - urlList.add(svcUrl); - } - } - } - } - } - } - return urlList; - } - - /** - * "Normalize" the resource instance name: - Remove spaces, underscores, - * dashes, and periods. - make lower case This is required by SDC when using - * the resource instance name to upload an artifact. - * - * @param inText - * @return - */ - public String normalizeResourceInstanceName(String inText) { - return inText.replace(" ", "").replace("-", "").replace(".", "").toLowerCase(); - } - - /** - * from michael - * - * @param data - * @return - */ - public String calculateMD5ByString(String data) { - String calculatedMd5 = DigestUtils.md5Hex(data); - // encode base-64 result - return base64Encode(calculatedMd5.getBytes()); - } - - /** - * Base 64 encode a String. - * - * @param inText - * @return - */ - public String base64Encode(String inText) { - return base64Encode(stringToByteArray(inText)); - } - - /** - * Convert String to byte array. - * - * @param inText - * @return - */ - public byte[] stringToByteArray(String inText) { - return inText.getBytes(StandardCharsets.UTF_8); - } - - /** - * Base 64 encode a byte array. - * - * @param bytes - * @return - */ - public String base64Encode(byte[] bytes) { - Base64.Encoder encoder = Base64.getEncoder(); - return encoder.encodeToString(bytes); - } - - /** - * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic - * dGVzdDoxMjM0NTY=). - * - * @return The String with Basic Auth and password - * @throws GeneralSecurityException - * In case of issue when decryting the SDC password - */ - public String getSdcBasicAuth() throws GeneralSecurityException { - String sdcId = refProp.getStringValue("sdc.serviceUsername"); - String sdcPw = refProp.getStringValue("sdc.servicePassword"); - String password = cryptoUtils.decrypt(sdcPw); - String idPw = base64Encode(sdcId + ":" + password); - return "Basic " + idPw; - } - - /** - * Method to get yaml/template properties value from json - * - * @param docText - * @return - * @throws IOException - */ - public String getYamlvalue(String docText) throws IOException { - ObjectMapper objectMapper = new ObjectMapper(); - String yamlFileValue = ""; - ObjectNode root = objectMapper.readValue(docText, ObjectNode.class); - Iterator> entryItr = root.fields(); - while (entryItr.hasNext()) { - Entry entry = entryItr.next(); - String key = entry.getKey(); - if (key != null && key.equalsIgnoreCase("global")) { - ArrayNode arrayNode = (ArrayNode) entry.getValue(); - for (JsonNode anArrayNode : arrayNode) { - ObjectNode node = (ObjectNode) anArrayNode; - ArrayNode arrayValueNode = (ArrayNode) node.get("value"); - JsonNode jsonNode = arrayValueNode.get(0); - yamlFileValue = jsonNode.asText(); - logger.info("value:" + yamlFileValue); - } - break; - } - } - return yamlFileValue; - } -} diff --git a/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java b/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java deleted file mode 100644 index 3646e574..00000000 --- a/src/main/java/org/onap/clamp/clds/client/req/TcaRequestFormatter.java +++ /dev/null @@ -1,204 +0,0 @@ -/*- - * ============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.req; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; - -import java.util.Map; - -import org.onap.clamp.clds.exception.TcaRequestFormatterException; -import org.onap.clamp.clds.model.prop.ModelProperties; -import org.onap.clamp.clds.model.prop.Tca; -import org.onap.clamp.clds.model.prop.TcaItem; -import org.onap.clamp.clds.model.prop.TcaThreshold; -import org.onap.clamp.clds.model.refprop.RefProp; - -/** - * Construct the requests for TCA policy and SDC. - * - */ -public class TcaRequestFormatter { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(TcaRequestFormatter.class); - protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - - /** - * Hide the default constructor. - */ - private TcaRequestFormatter() { - } - - /** - * Format Tca Policy JSON request. - * - * @param refProp - * The refProp generally created by Spring, it's an access on the - * clds-references.properties file - * @param modelProperties - * The Model Prop created from BPMN JSON and BPMN properties JSON - * @return The Json string containing that should be sent to policy - */ - public static String createPolicyJson(RefProp refProp, ModelProperties modelProperties) { - try { - String service = modelProperties.getGlobal().getService(); - Tca tca = modelProperties.getType(Tca.class); - modelProperties.setCurrentModelElementId(tca.getId()); - ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.policy.template", service); - String policyName = modelProperties.getCurrentPolicyScopeAndPolicyName(); - rootNode.put("policyName", policyName); - rootNode.put("description", "MicroService vCPE Policy"); - ((ObjectNode) rootNode.get("content")).replace("tca_policy", - createPolicyContent(refProp, modelProperties, service, policyName, tca)); - String tcaPolicyReq = rootNode.toString(); - logger.info("tcaPolicyReq=" + tcaPolicyReq); - return tcaPolicyReq; - } catch (Exception e) { - throw new TcaRequestFormatterException("Exception caught when attempting to create the policy JSON", e); - } - } - - /** - * Format Tca Policy Content JSON - * - * @param refProp - * The refProp generally created by Spring, it's an access on the - * clds-references.properties file - * @param modelProperties - * The Model Prop created from BPMN JSON and BPMN properties JSON - * @return The Json string containing that should be sent to policy - */ - public static JsonNode createPolicyContent(RefProp refProp, ModelProperties modelProperties, String service, - String policyName, Tca tca) { - try { - if (null == service) { - service = modelProperties.getGlobal().getService(); - } - if (null == tca) { - tca = modelProperties.getType(Tca.class); - modelProperties.setCurrentModelElementId(tca.getId()); - } - if (null == policyName) { - policyName = modelProperties.getCurrentPolicyScopeAndPolicyName(); - } - ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", service); - ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("eventName", tca.getTcaItem().getEventName()); - ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("policyName", policyName); - ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("controlLoopSchemaType", - tca.getTcaItem().getControlLoopSchemaType()); - ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("metricsPerEventName").get(0)); - addThresholds(refProp, service, thresholdsParent, tca.getTcaItem(), modelProperties); - logger.info("tcaPolicyContent=" + rootNode.toString()); - return rootNode; - } catch (Exception e) { - throw new TcaRequestFormatterException("Exception caught when attempting to create the policy content JSON", - e); - } - } - - /** - * Add threshold values to the existing policy JSON. - * - * @param refProp - * The refProp generally created by Spring, it's an access on the - * clds-references.properties file - * @param service - * The Service value extracted from Global section of the Bpmn - * Properties JSON - * @param appendToNode - * The JSON structure from where the thresholds section must be - * added - * @param tcaItem - * The TCA item contained in the Tca object - * @param modelProperties - * The Model Properties created from BPMN JSON and BPMN - * properties JSON - */ - private static void addThresholds(RefProp refProp, String service, ObjectNode appendToNode, TcaItem tcaItem, - ModelProperties modelProperties) { - try { - ArrayNode tcaNodes = appendToNode.withArray("thresholds"); - ObjectNode tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.thresholds.template", service); - for (TcaThreshold tcaThreshold : tcaItem.getTcaThresholds()) { - tcaNode.put("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId()); - tcaNode.put("fieldPath", tcaThreshold.getFieldPath()); - tcaNode.put("thresholdValue", tcaThreshold.getThreshold()); - tcaNode.put("direction", tcaThreshold.getOperator()); - tcaNode.put("closedLoopEventStatus", tcaThreshold.getClosedLoopEventStatus()); - tcaNodes.add(tcaNode); - } - } catch (Exception e) { - throw new TcaRequestFormatterException("Exception caught when attempting to create the thresholds JSON", e); - } - } - - /** - * This method updates the blueprint that is received in the UI with the TCA - * Json. - * - * @param refProp - * * The refProp generally created by Spring, it's an access on - * the clds-references.properties file - * @param modelProperties - * The Model Prop created from BPMN JSON and BPMN properties JSON - * @param yamlValue - * The yaml string received from the UI - * @return The updated YAML as a string - */ - public static String updatedBlueprintWithConfiguration(RefProp refProp, ModelProperties modelProperties, - String yamlValue) { - try { - String jsonPolicy = ((ObjectNode) createPolicyContent(refProp, modelProperties, null, null, null)) - .toString(); - logger.info("Yaml that will be updated:" + yamlValue); - Yaml yaml = new Yaml(); - Map loadedYaml = (Map) yaml.load(yamlValue); - Map nodeTemplates = (Map) loadedYaml.get("node_templates"); - // add policy_0 section in blueprint - /* - * Map policyObject = new HashMap - * (); Map policyIdObject = new HashMap (); String policyPrefix = - * refProp.getStringValue("tca.policyid.prefix"); - * policyIdObject.put("policy_id", policyPrefix + - * modelProperties.getCurrentPolicyScopeAndPolicyName()); - * policyObject.put("type", "dcae.nodes.policy"); - * policyObject.put("properties", policyIdObject); - * nodeTemplates.put("policy_0", policyObject); - */ - Map tcaObject = (Map) nodeTemplates.get("tca_tca"); - Map propsObject = (Map) tcaObject.get("properties"); - Map appPreferences = (Map) propsObject.get("app_preferences"); - appPreferences.put("tca_policy", jsonPolicy); - String blueprint = yaml.dump(loadedYaml); - logger.info("Yaml updated:" + blueprint); - return blueprint; - } catch (Exception e) { - throw new TcaRequestFormatterException("Exception caught when attempting to update the blueprint", e); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReq.java b/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReq.java new file mode 100644 index 00000000..fcf6cda5 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReq.java @@ -0,0 +1,424 @@ +/*- + * ============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.req.policy; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFLogger.Level; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.ws.rs.BadRequestException; + +import org.onap.clamp.clds.model.prop.Global; +import org.onap.clamp.clds.model.prop.ModelProperties; +import org.onap.clamp.clds.model.prop.PolicyChain; +import org.onap.clamp.clds.model.prop.PolicyItem; +import org.onap.clamp.clds.model.prop.Tca; +import org.onap.clamp.clds.model.refprop.RefProp; +import org.onap.policy.api.AttributeType; +import org.onap.policy.sdc.Resource; +import org.onap.policy.sdc.ResourceType; +import org.onap.policy.sdc.Service; +import org.onap.policy.controlloop.policy.OperationsAccumulateParams; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.controlloop.policy.PolicyResult; +import org.onap.policy.controlloop.policy.Target; +import org.onap.policy.controlloop.policy.TargetType; +import org.onap.policy.controlloop.policy.builder.BuilderException; +import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder; +import org.onap.policy.controlloop.policy.builder.Message; +import org.onap.policy.controlloop.policy.builder.Results; + +/** + * Construct an Operational Policy request given CLDS objects. + */ +public class OperationalPolicyReq { + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyReq.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + + private OperationalPolicyReq() { + } + + /** + * Format Operational Policy attributes. + * + * @param refProp + * @param prop + * @return + * @throws BuilderException + * @throws UnsupportedEncodingException + */ + public static Map> formatAttributes(RefProp refProp, ModelProperties prop, + String modelElementId, PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { + Global global = prop.getGlobal(); + prop.setCurrentModelElementId(modelElementId); + prop.setPolicyUniqueId(policyChain.getPolicyId()); + String templateName = ""; + String operationTopic = ""; + String notificationTopic = ""; + String controller = ""; + Tca tca = prop.getType(Tca.class); + if (tca != null && tca.isFound()) { + if (!global.getActionSet().equalsIgnoreCase("enbRecipe")) { + throw new BadRequestException( + "Operation Policy validation problem: action set is not selected properly."); + } + templateName = refProp.getStringValue("op.eNodeB.templateName", global.getService()); + operationTopic = refProp.getStringValue("op.eNodeB.operationTopic", global.getService()); + notificationTopic = refProp.getStringValue("op.eNodeB.notificationTopic", global.getService()); + controller = refProp.getStringValue("op.eNodeB.controller", global.getService()); + } else { + if (!global.getActionSet().equalsIgnoreCase("vnfRecipe")) { + throw new BadRequestException( + "Operation Policy validation problem: Action set is not selected properly."); + } + templateName = refProp.getStringValue("op.templateName", global.getService()); + operationTopic = refProp.getStringValue("op.operationTopic", global.getService()); + notificationTopic = refProp.getStringValue("op.notificationTopic", global.getService()); + controller = refProp.getStringValue("op.controller", global.getService()); + } + String recipeTopic = refProp.getStringValue("op.recipeTopic", global.getService()); + // ruleAttributes + Map ruleAttributes = new HashMap<>(); + if (operationTopic == null || operationTopic.isEmpty()) { + logger.info("templateName=" + templateName); + logger.info("recipeTopic=" + recipeTopic); + logger.info("notificationTopic=" + notificationTopic); + // if no operationTopic, then don't format yaml - use first policy + // from list + PolicyItem policyItem = policyChain.getPolicyItems().get(0); + ruleAttributes.put("templateName", templateName); + ruleAttributes.put("ClosedLoopControlName", prop.getControlNameAndPolicyUniqueId()); + ruleAttributes.put("RecipeTopic", recipeTopic); + ruleAttributes.put("NotificationTopic", notificationTopic); + String recipe = policyItem.getRecipe(); + String maxRetries = String.valueOf(policyItem.getMaxRetries()); + String retryTimeLimit = String.valueOf(policyItem.getRetryTimeLimit()); + String targetResourceId = String.valueOf(policyItem.getTargetResourceId()); + logger.info("recipe=" + recipe); + logger.info("maxRetries=" + maxRetries); + logger.info("retryTimeLimit=" + retryTimeLimit); + logger.info("targetResourceId=" + targetResourceId); + ruleAttributes.put("Recipe", recipe); + ruleAttributes.put("MaxRetries", maxRetries); + ruleAttributes.put("RetryTimeLimit", retryTimeLimit); + ruleAttributes.put("ResourceId", targetResourceId); + } else { + logger.info("templateName=" + templateName); + logger.info("operationTopic=" + operationTopic); + logger.info("notificationTopic=" + notificationTopic); + // format yaml + String yaml = (tca != null && tca.isFound()) ? formateNodeBYaml(refProp, prop, modelElementId, policyChain) + : formatYaml(refProp, prop, modelElementId, policyChain); + ruleAttributes.put("templateName", templateName); + ruleAttributes.put("ClosedLoopControlName", prop.getControlNameAndPolicyUniqueId()); + ruleAttributes.put("OperationTopic", operationTopic); + ruleAttributes.put("NotificationTopic", notificationTopic); + ruleAttributes.put("ControlLoopYaml", yaml); + } + // matchingAttributes + Map matchingAttributes = new HashMap<>(); + matchingAttributes.put("controller", controller); + Map> attributes = new HashMap<>(); + attributes.put(AttributeType.RULE, ruleAttributes); + attributes.put(AttributeType.MATCHING, matchingAttributes); + return attributes; + } + + /** + * Format Operational Policy yaml. + * + * @param refProp + * @param prop + * @return + * @throws BuilderException + * @throws UnsupportedEncodingException + */ + public static String formatYaml(RefProp refProp, ModelProperties prop, String modelElementId, + PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { + // get property objects + Global global = prop.getGlobal(); + prop.setCurrentModelElementId(modelElementId); + prop.setPolicyUniqueId(policyChain.getPolicyId()); + // convert values to SDC objects + Service service = new Service(global.getService()); + Resource[] vfResources = convertToResource(global.getResourceVf(), ResourceType.VF); + Resource[] vfcResources = convertToResource(global.getResourceVfc(), ResourceType.VFC); + // create builder + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(), + policyChain.getTimeout(), service, vfResources); + builder.addResource(vfcResources); + // process each policy + Map policyObjMap = new HashMap<>(); + List policyItemList = orderParentFirst(policyChain.getPolicyItems()); + for (PolicyItem policyItem : policyItemList) { + String policyName = policyItem.getRecipe() + " Policy"; + Target target = new Target(); + target.setType(TargetType.VM); + target.setResourceID(policyItem.getTargetResourceId()); + Policy policyObj; + if (policyItemList.indexOf(policyItem) == 0) { + String policyDescription = policyItem.getRecipe() + + " Policy - the trigger (no parent) policy - created by CLDS"; + policyObj = builder.setTriggerPolicy(policyName, policyDescription, + refProp.getStringValue("op.policy.appc"), target, policyItem.getRecipe(), null, + policyItem.getMaxRetries(), policyItem.getRetryTimeLimit()); + } else { + Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy()); + String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by " + + parentPolicyObj.getName() + " - created by CLDS"; + policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription, + refProp.getStringValue("op.policy.appc"), target, policyItem.getRecipe(), null, + policyItem.getMaxRetries(), policyItem.getRetryTimeLimit(), parentPolicyObj.getId(), + convertToPolicyResult(policyItem.getParentPolicyConditions())); + logger.info("policyObj.id=" + policyObj.getId() + "; parentPolicyObj.id=" + parentPolicyObj.getId()); + } + policyObjMap.put(policyItem.getId(), policyObj); + } + // + // Build the specification + // + Results results = builder.buildSpecification(); + if (results.isValid()) { + logger.info("results.getSpecification()=" + results.getSpecification()); + } else { + // throw exception with error info + StringBuilder sb = new StringBuilder(); + sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: "); + for (Message message : results.getMessages()) { + sb.append(message.getMessage()); + sb.append("; "); + } + throw new BadRequestException(sb.toString()); + } + return URLEncoder.encode(results.getSpecification(), "UTF-8"); + } + + /** + * Format Operational Policy yaml. + * + * @param refProp + * @param prop + * @return + * @throws BuilderException + * @throws UnsupportedEncodingException + */ + public static String formateNodeBYaml(RefProp refProp, ModelProperties prop, String modelElementId, + PolicyChain policyChain) throws BuilderException, UnsupportedEncodingException { + // get property objects + Global global = prop.getGlobal(); + prop.setCurrentModelElementId(modelElementId); + prop.setPolicyUniqueId(policyChain.getPolicyId()); + // convert values to SDC objects + Service service = new Service(global.getService()); + Resource[] vfResources = convertToResource(global.getResourceVf(), ResourceType.VF); + Resource[] vfcResources = convertToResource(global.getResourceVfc(), ResourceType.VFC); + // create builder + ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(prop.getControlName(), + policyChain.getTimeout(), service, vfResources); + builder.addResource(vfcResources); + // process each policy + Map policyObjMap = new HashMap<>(); + List policyItemList = addAOTSActorRecipe(refProp, global.getService(), + policyChain.getPolicyItems()); + Policy lastPolicyObj = new Policy(); + for (PolicyItem policyItem : policyItemList) { + Target target = new Target(); + target.setType(TargetType.VM); + target.setResourceID(policyItem.getTargetResourceId()); + String policyName = policyItem.getRecipe() + " Policy"; + Policy policyObj; + if (policyItemList.indexOf(policyItem) == 0) { + // To set up time window payload for trigger policy + Map payloadMap = new HashMap<>(); + payloadMap.put("timeWindow", refProp.getStringValue("op.eNodeB.timeWindow")); + String policyDescription = policyItem.getRecipe() + + " Policy - the trigger (no parent) policy - created by CLDS"; + policyObj = builder.setTriggerPolicy(policyName, policyDescription, policyItem.getActor(), target, + policyItem.getRecipe(), payloadMap, policyItem.getMaxRetries(), policyItem.getRetryTimeLimit()); + } else { + Policy parentPolicyObj = policyObjMap.get(policyItem.getParentPolicy()); + String policyDescription = policyItem.getRecipe() + " Policy - triggered conditionally by " + + parentPolicyObj.getName() + " - created by CLDS"; + policyObj = builder.setPolicyForPolicyResult(policyName, policyDescription, policyItem.getActor(), + target, policyItem.getRecipe(), null, policyItem.getMaxRetries(), + policyItem.getRetryTimeLimit(), parentPolicyObj.getId(), + convertToPolicyResult(policyItem.getParentPolicyConditions())); + lastPolicyObj = policyObj; + logger.info("policyObj.id=" + policyObj.getId() + "; parentPolicyObj.id=" + parentPolicyObj.getId()); + } + policyObjMap.put(policyItem.getId(), policyObj); + } + // To set up operations accumulate params + OperationsAccumulateParams operationsAccumulateParams = new OperationsAccumulateParams(); + operationsAccumulateParams.setLimit(Integer.valueOf(refProp.getStringValue("op.eNodeB.limit"))); + operationsAccumulateParams.setPeriod(refProp.getStringValue("op.eNodeB.period")); + builder.addOperationsAccumulateParams(lastPolicyObj.getId(), operationsAccumulateParams); + // + // Build the specification + // + Results results = builder.buildSpecification(); + if (results.isValid()) { + logger.info("results.getSpecification()=" + results.getSpecification()); + } else { + // throw exception with error info + StringBuilder sb = new StringBuilder(); + sb.append("Operation Policy validation problem: ControlLoopPolicyBuilder failed with following messages: "); + for (Message message : results.getMessages()) { + sb.append(message.getMessage()); + sb.append("; "); + } + throw new BadRequestException(sb.toString()); + } + return URLEncoder.encode(results.getSpecification(), "UTF-8"); + } + + /** + * Adding AOTS actor and other recipe for yaml + * + * @param inOrigList + * @return + */ + private static List addAOTSActorRecipe(RefProp refProp, String service, List inOrigList) { + List outList = new ArrayList<>(); + try { + PolicyItem policyItem = inOrigList.get(0); + ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("op.eNodeB.recipe", service); + Iterator itr = rootNode.get("eNodeBRecipes").elements(); + while (itr.hasNext()) { + PolicyItem policyItemObj = (PolicyItem) policyItem.clone(); + JsonNode recipeNode = itr.next(); + policyItemObj.setId(recipeNode.path("Recipe").asText()); + policyItemObj.setActor(recipeNode.path("Actor").asText()); + policyItemObj.setRecipe(recipeNode.path("Recipe").asText()); + policyItemObj.setParentPolicy(recipeNode.path("ParentPolicy").asText()); + if (!recipeNode.path("Retry").asText().isEmpty()) { + policyItemObj.setMaxRetries(Integer.parseInt(recipeNode.path("Retry").asText())); + } + if (!recipeNode.path("TimeLimit").asText().isEmpty()) { + policyItemObj.setRetryTimeLimit(Integer.parseInt(recipeNode.path("TimeLimit").asText())); + } + if (!recipeNode.path("PPConditions").asText().isEmpty()) { + List parentPolicyConditions = new ArrayList<>(); + for (String ppCondition : recipeNode.path("PPConditions").asText().split(",")) { + parentPolicyConditions.add(ppCondition); + } + policyItemObj.setParentPolicyConditions(parentPolicyConditions); + } + outList.add(policyItemObj); + } + } catch (Exception e) { + logger.log(Level.ERROR, "Error", e); + } + return outList; + } + + /** + * Order list of PolicyItems so that parents come before any of their + * children + * + * @param inOrigList + * @return + */ + private static List orderParentFirst(List inOrigList) { + List inList = new ArrayList<>(); + inList.addAll(inOrigList); + List outList = new ArrayList<>(); + int prevSize = 0; + while (!inList.isEmpty()) { + // check if there's a loop in the policy chain (the inList should + // have been reduced by at least one) + if (inList.size() == prevSize) { + throw new BadRequestException("Operation Policy validation problem: loop in Operation Policy chain"); + } + prevSize = inList.size(); + // the following loop should remove at least one PolicyItem from the + // inList + Iterator inListItr = inList.iterator(); + while (inListItr.hasNext()) { + PolicyItem inItem = inListItr.next(); + // check for trigger policy (no parent) + String parent = inItem.getParentPolicy(); + if (parent == null || parent.length() == 0) { + if (!outList.isEmpty()) { + throw new BadRequestException( + "Operation Policy validation problem: more than one trigger policy"); + } else { + outList.add(inItem); + inListItr.remove(); + } + } else { + // check if this PolicyItem's parent has been processed + for (PolicyItem outItem : outList) { + if (outItem.getId().equals(parent)) { + // if the inItem parent is already in the outList, + // then add inItem to outList and remove from inList + outList.add(inItem); + inListItr.remove(); + break; + } + } + } + } + } + return outList; + } + + /** + * Convert a List of resource strings to an array of Resource objects. + * + * @param stringList + * @param resourceType + * @return + */ + private static Resource[] convertToResource(List stringList, ResourceType resourceType) { + if (stringList == null || stringList.isEmpty()) { + return new Resource[0]; + } + return stringList.stream().map(stringElem -> new Resource(stringElem, resourceType)).toArray(Resource[]::new); + } + + /** + * Convert a List of policy result strings to an array of PolicyResult + * objects. + * + * @param prList + * @return + */ + private static PolicyResult[] convertToPolicyResult(List prList) { + if (prList == null || prList.isEmpty()) { + return new PolicyResult[0]; + } + return prList.stream().map(stringElem -> PolicyResult.toResult(stringElem)).toArray(PolicyResult[]::new); + } +} \ No newline at end of file diff --git a/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java b/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java new file mode 100644 index 00000000..e01a68fa --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/req/policy/PolicyClient.java @@ -0,0 +1,454 @@ +/*- + * ============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.req.policy; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.ws.rs.BadRequestException; + +import org.onap.clamp.clds.exception.policy.PolicyClientException; +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.onap.policy.api.AttributeType; +import org.onap.policy.api.ConfigRequestParameters; +import org.onap.policy.api.DeletePolicyCondition; +import org.onap.policy.api.DeletePolicyParameters; +import org.onap.policy.api.PolicyChangeResponse; +import org.onap.policy.api.PolicyConfig; +import org.onap.policy.api.PolicyConfigException; +import org.onap.policy.api.PolicyConfigType; +import org.onap.policy.api.PolicyEngine; +import org.onap.policy.api.PolicyEngineException; +import org.onap.policy.api.PolicyParameters; +import org.onap.policy.api.PolicyType; +import org.onap.policy.api.PushPolicyParameters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; + +/** + * Policy utility methods - specifically, send the policy. + */ +public class PolicyClient { + protected static final String POLICY_PREFIX_BASE = "Config_"; + protected static final String POLICY_PREFIX_BRMS_PARAM = "Config_BRMS_Param_"; + protected static final String POLICY_PREFIX_MICROSERVICE = "Config_MS_"; + protected static final String LOG_POLICY_PREFIX = "Response is "; + protected static final EELFLogger logger = EELFManager.getInstance() + .getLogger(PolicyClient.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + protected static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type"; + protected static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name"; + @Value("${org.onap.clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}") + protected String cldsPolicyConfigFile; + @Autowired + protected ApplicationContext appContext; + @Autowired + protected RefProp refProp; + + /** + * Perform BRMS policy type. + * + * @param attributes + * A map of attributes + * @param prop + * The ModelProperties + * @param policyRequestUuid + * PolicyRequest UUID + * @return The response message of policy + * + */ + public String sendBrmsPolicy(Map> attributes, ModelProperties prop, + String policyRequestUuid) { + PolicyParameters policyParameters = new PolicyParameters(); + // Set Policy Type(Mandatory) + policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM); + // Set Policy Name(Mandatory) + policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()); + // documentation says this is options, but when tested, got the + // following failure: java.lang.Exception: Policy send failed: PE300 - + // Data Issue: No policyDescription given. + policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription")); + policyParameters.setAttributes(attributes); + // Set a random UUID(Mandatory) + policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); + String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix"); + String rtnMsg = send(policyParameters, prop, policyNamePrefix); + String policyType = refProp.getStringValue("policy.op.type"); + push(policyType, prop); + return rtnMsg; + } + + /** + * Perform send of microservice policy in JSON. + * + * @param policyJson + * The policy JSON + * @param prop + * The ModelProperties + * @param policyRequestUuid + * The policy Request UUID + * @return The response message of policy + */ + public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) { + PolicyParameters policyParameters = new PolicyParameters(); + // Set Policy Type + policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); + policyParameters.setEcompName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); + policyParameters.setConfigBody(policyJson); + policyParameters.setConfigBodyType(PolicyType.JSON); + policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); + String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix"); + // Adding this line to clear the policy id from policy name while + // pushing to policy engine + prop.setPolicyUniqueId(""); + String rtnMsg = send(policyParameters, prop, policyNamePrefix); + String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME); + push(policyType, prop); + return rtnMsg; + } + + /** + * Perform send of base policy in OTHER type. + * + * @param configBody + * The config policy string body + * @param configPolicyName + * The config policy name of the component that has been + * pre-deployed in DCAE + * @param prop + * The ModelProperties + * @param policyRequestUuid + * The policy request UUID + * @return The answer from policy call + */ + public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop, + String policyRequestUuid) { + PolicyParameters policyParameters = new PolicyParameters(); + // Set Policy Type + policyParameters.setPolicyConfigType(PolicyConfigType.Base); + policyParameters.setEcompName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); + policyParameters.setConfigBody(configBody); + policyParameters.setConfigBodyType(PolicyType.OTHER); + policyParameters.setConfigName("HolmesPolicy"); + policyParameters.setPolicyName(configPolicyName); + policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); + // Adding this line to clear the policy id from policy name while + // pushing to policy engine + prop.setPolicyUniqueId(""); + String rtnMsg = send(policyParameters, prop, POLICY_PREFIX_BASE); + push(PolicyConfigType.Base.toString(), prop); + return rtnMsg; + } + + /** + * Perform send of Microservice policy in OTHER type. + * + * @param configBody + * The config policy string body + * @param prop + * The ModelProperties + * @return The answer from policy call + */ + public String sendMicroServiceInOther(String configBody, ModelProperties prop) { + PolicyParameters policyParameters = new PolicyParameters(); + // Set Policy Type + policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); + // policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setEcompName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); + policyParameters.setConfigBody(configBody); + String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix"); + // Adding this line to clear the policy id from policy name while + // pushing to policy engine + prop.setPolicyUniqueId(""); + String rtnMsg = send(policyParameters, prop, policyNamePrefix); + String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME); + push(policyType, prop); + return rtnMsg; + } + + /** + * Perform send of policy. + * + * @param policyParameters + * The PolicyParameters + * @param prop + * The ModelProperties + * @return The response message of Policy + */ + protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) { + // Verify whether it is triggered by Validation Test button from UI + if (prop.isTestOnly()) { + return "send not executed for test action"; + } + // API method to create or update Policy. + PolicyChangeResponse response = null; + String responseMessage = ""; + Date startTime = new Date(); + try { + List versions = getVersions(policyNamePrefix, prop); + if (versions.isEmpty()) { + LoggingUtils.setTargetContext("Policy", "createPolicy"); + logger.info("Attempting to create policy for action=" + prop.getActionCd()); + response = getPolicyEngine().createPolicy(policyParameters); + responseMessage = response.getResponseMessage(); + } else { + LoggingUtils.setTargetContext("Policy", "updatePolicy"); + logger.info("Attempting to update policy for action=" + prop.getActionCd()); + response = getPolicyEngine().updatePolicy(policyParameters); + responseMessage = response.getResponseMessage(); + } + } catch (Exception e) { + logger.error("Exception occurred during policy communication", e); + throw new PolicyClientException("Exception while communicating with Policy", e); + } + logger.info(LOG_POLICY_PREFIX + responseMessage); + LoggingUtils.setTimeContext(startTime, new Date()); + if (response.getResponseCode() == 200) { + logger.info("Policy send successful"); + metricsLogger.info("Policy send success"); + } else { + logger.warn("Policy send failed: " + responseMessage); + metricsLogger.info("Policy send failure"); + throw new BadRequestException("Policy send failed: " + responseMessage); + } + return responseMessage; + } + + /** + * Format and send push of policy. + * + * @param policyType + * The policy Type + * @param prop + * The ModelProperties + * @return The response message of policy + */ + protected String push(String policyType, ModelProperties prop) { + // Verify whether it is triggered by Validation Test button from UI + if (prop.isTestOnly()) { + return "push not executed for test action"; + } + PushPolicyParameters pushPolicyParameters = new PushPolicyParameters(); + // Parameter arguments + if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) { + pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()); + } else { + pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); + } + logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName()); + pushPolicyParameters.setPolicyType(policyType); + pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group")); + pushPolicyParameters.setRequestID(null); + // API method to create or update Policy. + PolicyChangeResponse response; + String responseMessage = ""; + try { + logger.info("Attempting to push policy..."); + response = getPolicyEngine().pushPolicy(pushPolicyParameters); + if (response != null) { + responseMessage = response.getResponseMessage(); + } + } catch (Exception e) { + logger.error("Exception occurred during policy communication", e); + throw new PolicyClientException("Exception while communicating with Policy", e); + } + logger.info(LOG_POLICY_PREFIX + responseMessage); + if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) { + logger.info("Policy push successful"); + } else { + logger.warn("Policy push failed: " + responseMessage); + throw new BadRequestException("Policy push failed: " + responseMessage); + } + return responseMessage; + } + + /** + * Use Get Config Policy API to retrieve the versions for a policy. Return + * versions in sorted order. Return empty list if none found. + * + * @param policyNamePrefix + * The Policy Name Prefix + * @param prop + * The ModelProperties + * @return The response message from policy + * @throws PolicyConfigException + * In case of issues with policy engine + */ + protected List getVersions(String policyNamePrefix, ModelProperties prop) throws PolicyConfigException { + ArrayList versions = new ArrayList<>(); + ConfigRequestParameters configRequestParameters = new ConfigRequestParameters(); + String policyName = ""; + if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) { + policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId(); + } else { + policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix); + } + logger.info("policyName=" + policyName); + configRequestParameters.setPolicyName(policyName); + try { + Collection response = getPolicyEngine().getConfig(configRequestParameters); + for (PolicyConfig policyConfig : response) { + Integer version = Integer.valueOf(policyConfig.getPolicyVersion()); + versions.add(version); + } + Collections.sort(versions); + logger.info("Policy versions.size()=" + versions.size()); + } catch (PolicyConfigException e) { + // just print warning - if no policy version found + logger.warn("warning: policy not found...policy name - " + policyName, e.getMessage()); + } + return versions; + } + + /** + * This method create a new policy engine. + * + * @return A new policy engine + */ + private PolicyEngine getPolicyEngine() { + PolicyEngine policyEngine; + try { + policyEngine = new PolicyEngine(appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath()); + } catch (IOException e1) { + throw new PolicyClientException("Exception when opening policy config file", e1); + } catch (PolicyEngineException e) { + throw new PolicyClientException("Exception when creating a new policy engine", e); + } + return policyEngine; + } + + /** + * Format and send delete Micro Service requests to Policy. + * + * @param prop + * The ModelProperties + * @return The response message from Policy + */ + public String deleteMicrosService(ModelProperties prop) { + String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME); + return deletePolicy(prop, policyType); + } + + /** + * This method delete the Base policy. + * + * @param prop + * The model Properties + * @return A string with the answer from policy + */ + public String deleteBasePolicy(ModelProperties prop) { + return deletePolicy(prop, PolicyConfigType.Base.toString()); + } + + /** + * Format and send delete BRMS requests to Policy. + * + * @param prop + * The ModelProperties + * @return The response message from policy + */ + public String deleteBrms(ModelProperties prop) { + String policyType = refProp.getStringValue("policy.op.type"); + return deletePolicy(prop, policyType); + } + + /** + * Format and send delete PAP and PDP requests to Policy. + * + * @param prop + * The ModelProperties + * + * @return The response message from policy + */ + protected String deletePolicy(ModelProperties prop, String policyType) { + DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters(); + if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) { + deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId()); + } else { + deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); + } + logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName()); + deletePolicyParameters.setPolicyComponent("PDP"); + deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL); + deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group")); + deletePolicyParameters.setPolicyType(policyType); + // send delete request + StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop)); + logger.info("Deleting policy from PAP..."); + deletePolicyParameters.setPolicyComponent("PAP"); + deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL); + // send delete request + responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop)); + return responseMessage.toString(); + } + + /** + * Send delete request to Policy. + * + * @param deletePolicyParameters + * The DeletePolicyParameters + * @param prop + * The ModelProperties + * @return The response message from policy + */ + protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) { + // Verify whether it is triggered by Validation Test button from UI + if (prop.isTestOnly()) { + return "delete not executed for test action"; + } + // API method to create or update Policy. + PolicyChangeResponse response = null; + String responseMessage = ""; + try { + logger.info("Attempting to delete policy..."); + response = getPolicyEngine().deletePolicy(deletePolicyParameters); + responseMessage = response.getResponseMessage(); + } catch (Exception e) { + logger.error("Exception occurred during policy communnication", e); + } + logger.info(LOG_POLICY_PREFIX + responseMessage); + if (response != null && response.getResponseCode() == 200) { + logger.info("Policy delete successful"); + } else { + logger.warn("Policy delete failed: " + responseMessage); + throw new BadRequestException("Policy delete failed: " + responseMessage); + } + return responseMessage; + } +} \ No newline at end of file diff --git a/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java b/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java new file mode 100644 index 00000000..c725be2b --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcCatalogServices.java @@ -0,0 +1,1305 @@ +/*- + * ============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.req.sdc; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +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.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Base64; +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.codec.DecoderException; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHeaders; +import org.onap.clamp.clds.exception.SdcCommunicationException; +import org.onap.clamp.clds.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.CryptoUtils; +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"; + private static final String RESOURCE_CVFC_TYPE = "CVFC"; + public static final String SDC_REQUESTID_PROPERTY_NAME = "sdc.header.requestId"; + private static final String SDC_METADATA_URL_PREFIX = "/metadata"; + private static final String SDC_INSTANCE_ID_PROPERTY_NAME = "sdc.InstanceID"; + private static final String SDC_CATALOG_URL_PROPERTY_NAME = "sdc.catalog.url"; + private static final String SDC_SERVICE_URL_PROPERTY_NAME = "sdc.serviceUrl"; + private static final String SDC_INSTANCE_ID_CLAMP = "CLAMP-Tool"; + private static final String RESOURCE_URL_PREFIX = "resources"; + @Autowired + private RefProp refProp; + protected CryptoUtils cryptoUtils = new CryptoUtils(); + + /** + * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic + * dGVzdDoxMjM0NTY=). + * + * @return The String with Basic Auth and password + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password + * @throws DecoderException + * In case of issues with the decoding of the HexString message + */ + private String getSdcBasicAuth() throws GeneralSecurityException, DecoderException { + String sdcId = refProp.getStringValue("sdc.serviceUsername"); + String sdcPw = refProp.getStringValue("sdc.servicePassword"); + String password = cryptoUtils.decrypt(sdcPw); + String idPw = Base64.getEncoder().encodeToString((sdcId + ":" + password).getBytes(StandardCharsets.UTF_8)); + return "Basic " + idPw; + } + + /** + * 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 + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password + * @throws DecoderException + * In case of issues with the decoding of the Hex String + */ + public String getSdcServicesInformation(String uuid) throws GeneralSecurityException, DecoderException { + Date startTime = new Date(); + String baseUrl = refProp.getStringValue(SDC_SERVICE_URL_PROPERTY_NAME); + String basicAuth = getSdcBasicAuth(); + LoggingUtils.setTargetContext("SDC", "getSdcServicesInformation"); + try { + String url = baseUrl; + if (uuid != null) { + url = baseUrl + "/" + uuid + SDC_METADATA_URL_PREFIX; + } + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); + conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), SDC_INSTANCE_ID_CLAMP); + conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth); + conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8"); + conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId()); + conn.setRequestMethod("GET"); + String resp = getResponse(conn); + logger.info(resp); + // 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.isEmpty()) { + // 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.isEmpty()) { + // 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.isEmpty()) { + // sort list + Collections.sort(rawCldsSdcResourceListBasicList); + // and then take only the resources with the max version (last in + // the list with the same name) + cldsSdcResourceBasicInfoList = new ArrayList<>(); + for (int i = 1; i < rawCldsSdcResourceListBasicList.size(); i++) { + // compare name with previous - if not equal, then keep the + // previous (it's the last with that name) + CldsSdcResourceBasicInfo prev = rawCldsSdcResourceListBasicList.get(i - 1); + 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 GeneralSecurityException + * In case of issue when decryting the SDC password + * @throws DecoderException + * In case of issues with the decoding of the Hex String + */ + public String getServiceUuidFromServiceInvariantId(String invariantId) + throws GeneralSecurityException, DecoderException { + String serviceUuid = ""; + String responseStr = getSdcServicesInformation(null); + List rawCldsSdcServicesList = getCldsSdcServicesListFromJson(responseStr); + List cldsSdcServicesList = removeDuplicateServices(rawCldsSdcServicesList); + if (cldsSdcServicesList != null && !cldsSdcServicesList.isEmpty()) { + 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 + * The Json string that must be decoded + * @return The list of CldsSdcServiceInfo, if there is a failure it return + * an empty list + */ + public List getCldsSdcServicesListFromJson(String jsonStr) { + ObjectMapper objectMapper = new ObjectMapper(); + if (StringUtils.isBlank(jsonStr)) { + return new ArrayList<>(); + } + try { + return objectMapper.readValue(jsonStr, + objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcServiceInfo.class)); + } catch (IOException e) { + logger.error("Error when attempting to decode the JSON containing CldsSdcServiceInfo", e); + return new ArrayList<>(); + } + } + + /** + * To get List of CldsSdcResourceBasicInfo class by parsing json string. + * + * @param jsonStr + * The JSOn string that must be decoded + * @return The list of CldsSdcResourceBasicInfo, an empty list in case of + * issues + */ + public List getAllSdcResourcesListFromJson(String jsonStr) { + ObjectMapper objectMapper = new ObjectMapper(); + if (StringUtils.isBlank(jsonStr)) { + return new ArrayList<>(); + } + try { + return objectMapper.readValue(jsonStr, + objectMapper.getTypeFactory().constructCollectionType(List.class, CldsSdcResourceBasicInfo.class)); + } catch (IOException e) { + logger.error("Exception occurred when attempting to decode the list of CldsSdcResourceBasicInfo JSON", e); + return new ArrayList<>(); + } + } + + /** + * 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 + */ + public CldsSdcServiceDetail getCldsSdcServiceDetailFromJson(String jsonStr) { + ObjectMapper objectMapper = new ObjectMapper(); + try { + return objectMapper.readValue(jsonStr, CldsSdcServiceDetail.class); + } catch (IOException e) { + logger.error("Exception when attempting to decode the CldsSdcServiceDetail JSON", e); + return null; + } + } + + /** + * To upload artifact to sdc based on serviceUUID and resource name on url. + * + * @param prop + * @param userid + * @param url + * @param formattedSdcReq + * @return + * @throws GeneralSecurityException + */ + public String uploadArtifactToSdc(ModelProperties prop, String userid, String url, String formattedSdcReq) + throws GeneralSecurityException { + // Verify whether it is triggered by Validation Test button from UI + if (prop.isTestOnly()) { + return "sdc artifact upload not executed for test action"; + } + try { + logger.info("userid=" + userid); + byte[] postData = formattedSdcReq.getBytes(StandardCharsets.UTF_8); + int postDataLength = postData.length; + HttpURLConnection conn = getSdcHttpUrlConnection(userid, postDataLength, url, formattedSdcReq); + try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { + wr.write(postData); + } + boolean requestFailed = true; + int responseCode = conn.getResponseCode(); + logger.info("responseCode=" + responseCode); + if (responseCode == 200) { + requestFailed = false; + } + String responseStr = getResponse(conn); + if (responseStr != null && requestFailed) { + logger.error("requestFailed - responseStr=" + responseStr); + throw new BadRequestException(responseStr); + } + return responseStr; + } catch (IOException e) { + logger.error("Exception when attempting to communicate with SDC", e); + throw new SdcCommunicationException("Exception when attempting to communicate with SDC", e); + } + } + + private HttpURLConnection getSdcHttpUrlConnection(String userid, int postDataLength, String url, String content) + throws GeneralSecurityException { + try { + logger.info("userid=" + userid); + String basicAuth = getSdcBasicAuth(); + String sdcXonapInstanceId = refProp.getStringValue("sdc.sdcX-InstanceID"); + URL urlObj = new URL(url); + HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); + conn.setDoOutput(true); + conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), sdcXonapInstanceId); + conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth); + conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json"); + conn.setRequestProperty(HttpHeaders.CONTENT_MD5, + Base64.getEncoder().encodeToString(DigestUtils.md5Hex(content).getBytes("UTF-8"))); + conn.setRequestProperty("USER_ID", userid); + conn.setRequestMethod("POST"); + conn.setRequestProperty("charset", "utf-8"); + conn.setRequestProperty(HttpHeaders.CONTENT_LENGTH, Integer.toString(postDataLength)); + conn.setUseCaches(false); + conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId()); + return conn; + } catch (IOException e) { + logger.error("Exception when attempting to open connection with SDC", e); + throw new SdcCommunicationException("Exception when attempting to open connection with SDC", e); + } catch (DecoderException e) { + logger.error("Exception when attempting to decode the Hex string", e); + throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e); + } catch (GeneralSecurityException e) { + logger.error("Exception when attempting to decrypt the encrypted password", e); + throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e); + } + } + + private String getResponse(HttpURLConnection conn) { + try (InputStream is = getInputStream(conn)) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) { + return IOUtils.toString(in); + } + } catch (IOException e) { + logger.error("Exception when attempting to open SDC response", e); + throw new SdcCommunicationException("Exception when attempting to open SDC response", e); + } + } + + private InputStream getInputStream(HttpURLConnection conn) { + try { + InputStream inStream = conn.getErrorStream(); + if (inStream == null) { + inStream = conn.getInputStream(); + } + return inStream; + } catch (IOException e) { + logger.error("Exception when attempting to open SDC error stream", e); + throw new SdcCommunicationException("Exception when attempting to open SDC error stream", e); + } + } + + public CldsDBServiceCache getCldsDbServiceCacheUsingCldsServiceData(CldsServiceData cldsServiceData) { + try { + CldsDBServiceCache cldsDbServiceCache = new CldsDBServiceCache(); + cldsDbServiceCache.setCldsDataInstream(cldsServiceData); + cldsDbServiceCache.setInvariantId(cldsServiceData.getServiceInvariantUUID()); + cldsDbServiceCache.setServiceId(cldsServiceData.getServiceUUID()); + return cldsDbServiceCache; + } catch (IOException e) { + logger.error("Exception when getting service in cache", e); + throw new SdcCommunicationException("Exception when getting service in cache", e); + } + } + + /** + * Check if the SDC Info in cache has expired. + * + * @param cldsServiceData + * @return + * @throws GeneralSecurityException + * In case of issues with the decryting the encrypted password + * @throws DecoderException + * In case of issues with the decoding of the Hex String + */ + public boolean isCldsSdcCacheDataExpired(CldsServiceData cldsServiceData) + throws GeneralSecurityException, DecoderException { + boolean expired = false; + if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { + String cachedServiceUuid = cldsServiceData.getServiceUUID(); + String latestServiceUuid = getServiceUuidFromServiceInvariantId(cldsServiceData.getServiceInvariantUUID()); + String defaultRecordAge = refProp.getStringValue("CLDS_SERVICE_CACHE_MAX_SECONDS"); + if ((!cachedServiceUuid.equalsIgnoreCase(latestServiceUuid)) || (cldsServiceData.getAgeOfRecord() != null + && cldsServiceData.getAgeOfRecord() > Long.parseLong(defaultRecordAge))) { + expired = true; + } + } else { + expired = true; + } + return expired; + } + + /** + * Get the Service Data with Alarm Conditions for a given + * invariantServiceUuid. + * + * @param invariantServiceUuid + * @return + * @throws GeneralSecurityException + * In case of issues with the decryting the encrypted password + * @throws DecoderException + * In case of issues with the decoding of the Hex String + */ + public CldsServiceData getCldsServiceDataWithAlarmConditions(String invariantServiceUuid) + throws GeneralSecurityException, DecoderException { + String url = refProp.getStringValue(SDC_SERVICE_URL_PROPERTY_NAME); + String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME); + String serviceUuid = getServiceUuidFromServiceInvariantId(invariantServiceUuid); + String serviceDetailUrl = url + "/" + serviceUuid + SDC_METADATA_URL_PREFIX; + String responseStr = getCldsServicesOrResourcesBasedOnURL(serviceDetailUrl, false); + ObjectMapper objectMapper = new ObjectMapper(); + CldsServiceData cldsServiceData = new CldsServiceData(); + if (responseStr != null) { + CldsSdcServiceDetail cldsSdcServiceDetail; + try { + cldsSdcServiceDetail = objectMapper.readValue(responseStr, CldsSdcServiceDetail.class); + } catch (IOException e) { + logger.error("Exception when decoding the CldsServiceData JSON from SDC", e); + throw new SdcCommunicationException("Exception when decoding the CldsServiceData JSON from SDC", e); + } + // To remove duplicate resources from serviceDetail and add valid + // vfs to service + if (cldsSdcServiceDetail != null && cldsSdcServiceDetail.getResources() != null) { + cldsServiceData.setServiceUUID(cldsSdcServiceDetail.getUuid()); + cldsServiceData.setServiceInvariantUUID(cldsSdcServiceDetail.getInvariantUUID()); + List cldsSdcResourceList = removeDuplicateSdcResourceInstances( + cldsSdcServiceDetail.getResources()); + if (cldsSdcResourceList != null && !cldsSdcResourceList.isEmpty()) { + List cldsVfDataList = new ArrayList<>(); + for (CldsSdcResource currCldsSdcResource : cldsSdcResourceList) { + if (currCldsSdcResource != null && currCldsSdcResource.getResoucreType() != null + && "VF".equalsIgnoreCase(currCldsSdcResource.getResoucreType())) { + 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; + } + + private void getAllVfcForVfList(List cldsVfDataList, String catalogUrl) + throws GeneralSecurityException { + // todo : refact this.. + if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { + List allVfResources = getAllSdcVForVfcResourcesBasedOnResourceType( + RESOURCE_VF_TYPE); + List allVfcResources = getAllSdcVForVfcResourcesBasedOnResourceType( + RESOURCE_VFC_TYPE); + allVfcResources.addAll(getAllSdcVForVfcResourcesBasedOnResourceType(RESOURCE_CVFC_TYPE)); + for (CldsVfData currCldsVfData : cldsVfDataList) { + if (currCldsVfData != null && currCldsVfData.getVfInvariantResourceUUID() != null) { + String resourceUuid = getResourceUuidFromResourceInvariantUuid( + currCldsVfData.getVfInvariantResourceUUID(), allVfResources); + if (resourceUuid != null) { + String vfResourceUuidUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUuid + + SDC_METADATA_URL_PREFIX; + 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.isEmpty()) { + // 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 + RESOURCE_URL_PREFIX + "/" + + resourceVfcUuid + SDC_METADATA_URL_PREFIX; + 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 GeneralSecurityException { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode vfResponseNode; + try { + vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + } catch (IOException e) { + logger.error("Exception when decoding the JSON list of CldsVfcData", e); + return new ArrayList<>(); + } + ArrayNode vfcArrayNode = (ArrayNode) vfResponseNode.get("resources"); + List cldsVfcDataList = new ArrayList<>(); + if (vfcArrayNode != null) { + 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())) { + TextNode vfcResourceName = (TextNode) currVfcNode.get("resourceInstanceName"); + TextNode vfcInvariantResourceUuid = (TextNode) currVfcNode.get("resourceInvariantUUID"); + currCldsVfcData.setVfcName(vfcResourceName.textValue()); + currCldsVfcData.setVfcInvariantResourceUUID(vfcInvariantResourceUuid.textValue()); + cldsVfcDataList.add(currCldsVfcData); + cldsVfcDataList.addAll(getVFCfromCVFC(currVfcNode.get("resourceUUID").textValue())); + } + } + } + return cldsVfcDataList; + } + + private List getVFCfromCVFC(String resourceUUID) throws GeneralSecurityException { + String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME); + List cldsVfcDataList = new ArrayList<>(); + if (resourceUUID != null) { + String vfcResourceUUIDUrl = catalogUrl + RESOURCE_URL_PREFIX + "/" + resourceUUID + SDC_METADATA_URL_PREFIX; + 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) { + return (id != null) ? id.replaceAll("\"", "") : ""; + } + + private List getAlarmCondtionsFromVfc(String vfcResponse) throws GeneralSecurityException { + List cldsAlarmConditionList = new ArrayList<>(); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode vfcResponseNode; + try { + vfcResponseNode = (ObjectNode) mapper.readTree(vfcResponse); + } catch (IOException e) { + logger.error("Exception when decoding the JSON list of CldsAlarmCondition", e); + return cldsAlarmConditionList; + } + ArrayNode artifactsArrayNode = (ArrayNode) vfcResponseNode.get("artifacts"); + if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) { + for (int index = 0; index < artifactsArrayNode.size(); index++) { + ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index); + 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) { + try { + List cldsAlarmConditionList = new ArrayList<>(); + Reader alarmReader = new StringReader(allAlarmCondsValues); + Iterable records = CSVFormat.RFC4180.parse(alarmReader); + if (records != null) { + Iterator it = records.iterator(); + if (it.hasNext()) { + it.next(); + } + it.forEachRemaining(record -> processRecord(cldsAlarmConditionList, record)); + } + return cldsAlarmConditionList; + } catch (IOException e) { + logger.error("Exception when attempting to parse the CSV containing the alarm", e); + return new ArrayList<>(); + } + } + + // Method to get the artifact for any particular VF + private List getFieldPathFromVF(String vfResponse) throws GeneralSecurityException { + List cldsVfKPIDataList = new ArrayList<>(); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode vfResponseNode; + try { + vfResponseNode = (ObjectNode) mapper.readTree(vfResponse); + } catch (IOException e) { + logger.error("Exception when decoding the JSON list of CldsVfKPIData", e); + return cldsVfKPIDataList; + } + ArrayNode artifactsArrayNode = (ArrayNode) vfResponseNode.get("artifacts"); + if (artifactsArrayNode != null && artifactsArrayNode.size() > 0) { + for (int index = 0; index < artifactsArrayNode.size(); index++) { + ObjectNode currArtifactNode = (ObjectNode) artifactsArrayNode.get(index); + 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 && "csv".equalsIgnoreCase(artifactName)) { + 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) { + try { + List cldsVfKPIDataList = new ArrayList<>(); + Reader alarmReader = new StringReader(allFieldPathValues); + Iterable records = CSVFormat.RFC4180.parse(alarmReader); + if (records != null) { + for (CSVRecord record : records) { + CldsVfKPIData kpiData = this.convertCsvRecordToKpiData(record); + if (kpiData != null) { + cldsVfKPIDataList.add(kpiData); + } + } + } + return cldsVfKPIDataList; + } catch (IOException e) { + logger.error("Exception when attempting to parse the CSV containing the alarm kpi data", e); + return new ArrayList<>(); + } + } + + 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); + } + + /** + * Get the responses for the current artifact from the artifacts URL. + * + * @param artifactsUrl + * @return + * @throws GeneralSecurityException + */ + public String getResponsesFromArtifactUrl(String artifactsUrl) throws GeneralSecurityException { + String hostUrl = refProp.getStringValue("sdc.hostUrl"); + String artifactsUrlReworked = artifactsUrl.replaceAll("\"", ""); + String artifactUrl = hostUrl + artifactsUrlReworked; + 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 alarm conditons from artifact url and else it is false + * + * @param url + * @param alarmConditions + * @return + * @throws GeneralSecurityException + * In case of issue when decrypting the SDC password + * + */ + public String getCldsServicesOrResourcesBasedOnURL(String url, boolean alarmConditions) + throws GeneralSecurityException { + Date startTime = new Date(); + try { + LoggingUtils.setTargetContext("SDC", "getCldsServicesOrResourcesBasedOnURL"); + String urlReworked = removeUnwantedBracesFromString(url); + URL urlObj = new URL(urlReworked); + HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection(); + String basicAuth = getSdcBasicAuth(); + conn.setRequestProperty(refProp.getStringValue(SDC_INSTANCE_ID_PROPERTY_NAME), SDC_INSTANCE_ID_CLAMP); + conn.setRequestProperty(HttpHeaders.AUTHORIZATION, basicAuth); + conn.setRequestProperty(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8"); + conn.setRequestProperty(refProp.getStringValue(SDC_REQUESTID_PROPERTY_NAME), LoggingUtils.getRequestId()); + conn.setRequestMethod("GET"); + int responseCode = conn.getResponseCode(); + logger.info("Sdc resource url - " + urlReworked + " , responseCode=" + responseCode); + try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + String response = IOUtils.toString(in); + LoggingUtils.setResponseContext("0", "Get sdc resources success", this.getClass().getName()); + return response; + } + } catch (IOException e) { + LoggingUtils.setResponseContext("900", "Get sdc resources failed", this.getClass().getName()); + LoggingUtils.setErrorContext("900", "Get sdc resources error"); + logger.error("Exception occurred during query to SDC", e); + return ""; + } catch (DecoderException e) { + logger.error("Exception when attempting to decode the Hex string", e); + throw new SdcCommunicationException("Exception when attempting to decode the Hex string", e); + } catch (GeneralSecurityException e) { + logger.error("Exception when attempting to decrypt the encrypted password", e); + throw new SdcCommunicationException("Exception when attempting to decrypt the encrypted password", e); + } finally { + LoggingUtils.setTimeContext(startTime, new Date()); + metricsLogger.info("getCldsServicesOrResourcesBasedOnURL completed"); + } + } + + /** + * To create properties object by using cldsServicedata. + * + * @param globalProps + * @param cldsServiceData + * @return + */ + public String createPropertiesObjectByUUID(String globalProps, CldsServiceData cldsServiceData) { + String totalPropsStr; + ObjectMapper mapper = new ObjectMapper(); + ObjectNode globalPropsJson; + if (cldsServiceData != null && cldsServiceData.getServiceUUID() != null) { + // Objectnode to save all byservice, byvf , byvfc and byalarm nodes + ObjectNode byIdObjectNode = mapper.createObjectNode(); + // To create vf ResourceUUID node with serviceInvariantUUID + ObjectNode invariantUuidObjectNodeWithVf = createVfObjectNodeByServiceInvariantUuid(mapper, + cldsServiceData); + byIdObjectNode.putPOJO("byService", invariantUuidObjectNodeWithVf); + // To create byVf and vfcResourceNode with vfResourceUUID + ObjectNode vfcObjectNodeByVfUuid = createVfcObjectNodeByVfUuid(mapper, cldsServiceData.getCldsVfs()); + byIdObjectNode.putPOJO("byVf", vfcObjectNodeByVfUuid); + // To create byKpi + ObjectNode kpiObjectNode = mapper.createObjectNode(); + if (cldsServiceData.getCldsVfs() != null && !cldsServiceData.getCldsVfs().isEmpty()) { + 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().isEmpty()) { + 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 = decodeGlobalProp(globalProps, mapper); + globalPropsJson.putPOJO("shared", byIdObjectNode); + logger.info("value of objNode:" + globalPropsJson); + } else { + /** + * to create json with total properties when no serviceUUID passed + */ + globalPropsJson = decodeGlobalProp(globalProps, mapper); + } + totalPropsStr = globalPropsJson.toString(); + return totalPropsStr; + } + + private ObjectNode decodeGlobalProp(String globalProps, ObjectMapper mapper) { + try { + return (ObjectNode) mapper.readValue(globalProps, JsonNode.class); + } catch (IOException e) { + logger.error("Exception occurred during decoding of the global props, returning an empty objectNode", e); + return mapper.createObjectNode(); + } + } + + /** + * Method to get alarm conditions/alert description from Service Data. + * + * @param cldsServiceData + * CldsServiceData the Service Data to analyze + * @param eventName + * 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.isEmpty()) { + 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.isEmpty()) { + 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.isEmpty()) { + 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.isEmpty()) { + for (CldsVfcData currCldsVfcData : cldsVfcDataList) { + if (currCldsVfcData != null) { + if (currCldsVfcData.getCldsAlarmConditions() != null + && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { + for (CldsAlarmCondition currCldsAlarmCondition : currCldsVfcData.getCldsAlarmConditions()) { + if ("alarmCondition".equalsIgnoreCase(currCldsAlarmCondition.getEventName())) { + alarmCondNode.put(currCldsAlarmCondition.getAlarmConditionKey(), + currCldsAlarmCondition.getAlarmConditionKey()); + } else { + alertDescNode.put(currCldsAlarmCondition.getAlarmConditionKey(), + currCldsAlarmCondition.getAlarmConditionKey()); + } + } + } + vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); + vfcObjectNode.putPOJO("alertDescription", alertDescNode); + vfcResourceUuidObjectNode.putPOJO(currCldsVfcData.getVfcInvariantResourceUUID(), vfcObjectNode); + } + } + } else { + alarmCondNode.put("", ""); + vfcObjectNode.putPOJO("alarmCondition", alarmCondNode); + alertDescNode.put("", ""); + vfcObjectNode.putPOJO("alertDescription", alarmCondNode); + vfcResourceUuidObjectNode.putPOJO("", vfcObjectNode); + } + } + + /** + * Method to create vfc and kpi nodes inside vf node + * + * @param mapper + * @param cldsVfDataList + * @return + */ + private ObjectNode createVfcObjectNodeByVfUuid(ObjectMapper mapper, List cldsVfDataList) { + ObjectNode vfUuidObjectNode = mapper.createObjectNode(); + if (cldsVfDataList != null && !cldsVfDataList.isEmpty()) { + for (CldsVfData currCldsVfData : cldsVfDataList) { + if (currCldsVfData != null) { + ObjectNode vfObjectNode = mapper.createObjectNode(); + ObjectNode vfcUuidNode = mapper.createObjectNode(); + ObjectNode kpiObjectNode = mapper.createObjectNode(); + if (currCldsVfData.getCldsVfcs() != null && !currCldsVfData.getCldsVfcs().isEmpty()) { + for (CldsVfcData currCldsVfcData : currCldsVfData.getCldsVfcs()) { + if (currCldsVfcData.getCldsAlarmConditions() != null + && !currCldsVfcData.getCldsAlarmConditions().isEmpty()) { + vfcUuidNode.put(currCldsVfcData.getVfcInvariantResourceUUID(), + currCldsVfcData.getVfcName()); + } + } + } else { + vfcUuidNode.put("", ""); + } + if (currCldsVfData.getCldsKPIList() != null && !currCldsVfData.getCldsKPIList().isEmpty()) { + for (CldsVfKPIData currCldsVfKPIData : currCldsVfData.getCldsKPIList()) { + kpiObjectNode.put(currCldsVfKPIData.getThresholdValue(), + currCldsVfKPIData.getThresholdValue()); + } + } else { + kpiObjectNode.put("", ""); + } + vfObjectNode.putPOJO("vfc", vfcUuidNode); + vfObjectNode.putPOJO("kpi", kpiObjectNode); + vfUuidObjectNode.putPOJO(currCldsVfData.getVfInvariantResourceUUID(), vfObjectNode); + } + } + } else { + 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 + && currCldsSdcArtifact.getArtifactName().equalsIgnoreCase(artifactName)) { + artifactUuid = currCldsSdcArtifact.getArtifactUUID(); + artifactExists = true; + break; + } + } + } + } + } + return artifactUuid; + } + + /** + * To get all sdc VF/VFC Resources basic info. + * + * @param resourceType + * The resourceType + * @return The list of CldsSdcResourceBasicInfo + * @throws GeneralSecurityException + * In case of issue when decryting the SDC password + * + */ + private List getAllSdcVForVfcResourcesBasedOnResourceType(String resourceType) + throws GeneralSecurityException { + String catalogUrl = refProp.getStringValue(SDC_CATALOG_URL_PROPERTY_NAME); + String resourceUrl = catalogUrl + "resources?resourceType=" + resourceType; + String allSdcVfcResources = getCldsServicesOrResourcesBasedOnURL(resourceUrl, false); + return removeDuplicateSdcResourceBasicInfo(getAllSdcResourcesListFromJson(allSdcVfcResources)); + } + + private String getResourceUuidFromResourceInvariantUuid(String resourceInvariantUuid, + List resourceInfoList) { + String resourceUuid = null; + if (resourceInfoList != null && !resourceInfoList.isEmpty()) { + 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 GeneralSecurityException + * In case of issues with the decryting the encrypted password + * @throws DecoderException + * In case of issues with the decoding of the Hex String + */ + public void uploadToSdc(ModelProperties prop, String userid, List sdcReqUrlsList, String formattedSdcReq, + String formattedSdcLocationReq, String artifactName, String locationArtifactName) + throws GeneralSecurityException, DecoderException { + 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/client/req/sdc/SdcReq.java b/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcReq.java new file mode 100644 index 00000000..d4b5b086 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/req/sdc/SdcReq.java @@ -0,0 +1,232 @@ +/*- + * ============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.req.sdc; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.core.JsonParseException; +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 java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.Iterator; +import java.util.List; +import java.util.Map.Entry; + +import org.apache.commons.codec.DecoderException; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter; +import org.onap.clamp.clds.model.CldsSdcResource; +import org.onap.clamp.clds.model.CldsSdcServiceDetail; +import org.onap.clamp.clds.model.prop.Global; +import org.onap.clamp.clds.model.prop.ModelProperties; +import org.onap.clamp.clds.model.prop.Tca; +import org.onap.clamp.clds.model.refprop.RefProp; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Construct a Sdc request given CLDS objects. + */ +public class SdcReq { + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcReq.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + @Autowired + protected RefProp refProp; + + /** + * Format the Blueprint from a Yaml + * + * @param prop + * The ModelProperties describing the clds model + * @param docText + * The Yaml file that must be converted + * + * @return A String containing the BluePrint + * @throws JsonParseException + * In case of issues + * @throws JsonMappingException + * In case of issues + * @throws IOException + * In case of issues + */ + public String formatBlueprint(ModelProperties prop, String docText) + throws JsonParseException, JsonMappingException, IOException { + String yamlvalue = getYamlvalue(docText); + String updatedBlueprint = ""; + Tca tca = prop.getType(Tca.class); + if (tca.isFound()) { + updatedBlueprint = TcaRequestFormatter.updatedBlueprintWithConfiguration(refProp, prop, yamlvalue); + } + logger.info("value of blueprint:" + updatedBlueprint); + return updatedBlueprint; + } + + /** + * Format the SDC Locations Request in the JSON Format + * + * @param prop + * The ModelProperties describing the clds model + * @param artifactName + * The name of the artifact + * + * @return SDC Locations request in the JSON Format + */ + public String formatSdcLocationsReq(ModelProperties prop, String artifactName) { + ObjectMapper objectMapper = new ObjectMapper(); + Global global = prop.getGlobal(); + List locationsList = global.getLocation(); + ArrayNode locationsArrayNode = objectMapper.createArrayNode(); + ObjectNode locationObject = objectMapper.createObjectNode(); + for (String currLocation : locationsList) { + locationsArrayNode.add(currLocation); + } + locationObject.put("artifactName", artifactName); + locationObject.putPOJO("locations", locationsArrayNode); + String locationJsonFormat = locationObject.toString(); + logger.info("Value of location Json Artifact:" + locationsArrayNode); + return locationJsonFormat; + } + + /** + * Format the SDC Request + * + * @param payloadData + * The ModelProperties describing the clds model + * @param artifactName + * The name of the artifact + * @param artifactLabel + * The Label of the artifact + * @param artifactType + * The type of the artifact + * @return formatted SDC Request + * @throws IOException + * In case of issues + */ + public String formatSdcReq(String payloadData, String artifactName, String artifactLabel, String artifactType) + throws IOException { + logger.info("artifact=" + payloadData); + String base64Artifact = Base64.getEncoder().encodeToString(payloadData.getBytes(StandardCharsets.UTF_8)); + return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel + + "\",\n" + "\"artifactName\" :\"" + artifactName + "\",\n" + "\"artifactType\" : \"" + artifactType + + "\",\n" + "\"artifactGroupType\" : \"DEPLOYMENT\",\n" + "\"description\" : \"from CLAMP Cockpit\"\n" + + "} \n"; + } + + /** + * To get List of urls for all vfresources + * + * @param prop + * @param baseUrl + * @param sdcCatalogServices + * @param execution + * @return + * @throws GeneralSecurityException + * In case of issues when decrypting the password + * @throws DecoderException + * In case of issues when decoding the Hex String + */ + public List getSdcReqUrlsList(ModelProperties prop, String baseUrl, SdcCatalogServices sdcCatalogServices, + DelegateExecution execution) throws GeneralSecurityException, DecoderException { + // TODO : refact and regroup with very similar code + List urlList = new ArrayList<>(); + Global globalProps = prop.getGlobal(); + if (globalProps != null) { + if (globalProps.getService() != null) { + String serviceInvariantUUID = globalProps.getService(); + execution.setVariable("serviceInvariantUUID", serviceInvariantUUID); + List resourceVfList = globalProps.getResourceVf(); + String serviceUUID = sdcCatalogServices.getServiceUuidFromServiceInvariantId(serviceInvariantUUID); + String sdcServicesInformation = sdcCatalogServices.getSdcServicesInformation(serviceUUID); + CldsSdcServiceDetail cldsSdcServiceDetail = sdcCatalogServices + .getCldsSdcServiceDetailFromJson(sdcServicesInformation); + if (cldsSdcServiceDetail != null && resourceVfList != null) { + List cldsSdcResourcesList = cldsSdcServiceDetail.getResources(); + if (cldsSdcResourcesList != null && !cldsSdcResourcesList.isEmpty()) { + for (CldsSdcResource CldsSdcResource : cldsSdcResourcesList) { + if (CldsSdcResource != null && CldsSdcResource.getResoucreType() != null + && CldsSdcResource.getResoucreType().equalsIgnoreCase("VF") + && resourceVfList.contains(CldsSdcResource.getResourceInvariantUUID())) { + String normalizedResourceInstanceName = normalizeResourceInstanceName( + CldsSdcResource.getResourceInstanceName()); + String svcUrl = baseUrl + "/" + serviceUUID + "/resourceInstances/" + + normalizedResourceInstanceName + "/artifacts"; + urlList.add(svcUrl); + } + } + } + } + } + } + return urlList; + } + + /** + * "Normalize" the resource instance name: - Remove spaces, underscores, + * dashes, and periods. - make lower case This is required by SDC when using + * the resource instance name to upload an artifact. + * + * @param inText + * @return + */ + public String normalizeResourceInstanceName(String inText) { + return inText.replace(" ", "").replace("-", "").replace(".", "").toLowerCase(); + } + + /** + * Method to get yaml/template properties value from json + * + * @param docText + * @return + * @throws IOException + */ + public String getYamlvalue(String docText) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + String yamlFileValue = ""; + ObjectNode root = objectMapper.readValue(docText, ObjectNode.class); + Iterator> entryItr = root.fields(); + while (entryItr.hasNext()) { + Entry entry = entryItr.next(); + String key = entry.getKey(); + if (key != null && key.equalsIgnoreCase("global")) { + ArrayNode arrayNode = (ArrayNode) entry.getValue(); + for (JsonNode anArrayNode : arrayNode) { + ObjectNode node = (ObjectNode) anArrayNode; + ArrayNode arrayValueNode = (ArrayNode) node.get("value"); + JsonNode jsonNode = arrayValueNode.get(0); + yamlFileValue = jsonNode.asText(); + logger.info("value:" + yamlFileValue); + } + break; + } + } + return yamlFileValue; + } +} diff --git a/src/main/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatter.java b/src/main/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatter.java new file mode 100644 index 00000000..385a4e41 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/client/req/tca/TcaRequestFormatter.java @@ -0,0 +1,203 @@ +/*- + * ============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.req.tca; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; + +import java.io.IOException; +import java.util.Map; + +import org.onap.clamp.clds.exception.TcaRequestFormatterException; +import org.onap.clamp.clds.model.prop.ModelProperties; +import org.onap.clamp.clds.model.prop.Tca; +import org.onap.clamp.clds.model.prop.TcaItem; +import org.onap.clamp.clds.model.prop.TcaThreshold; +import org.onap.clamp.clds.model.refprop.RefProp; + +/** + * Construct the requests for TCA policy and SDC. + * + */ +public class TcaRequestFormatter { + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(TcaRequestFormatter.class); + protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + + /** + * Hide the default constructor. + */ + private TcaRequestFormatter() { + } + + /** + * Format Tca Policy JSON request. + * + * @param refProp + * The refProp generally created by Spring, it's an access on the + * clds-references.properties file + * @param modelProperties + * The Model Prop created from BPMN JSON and BPMN properties JSON + * @return The Json string containing that should be sent to policy + */ + public static String createPolicyJson(RefProp refProp, ModelProperties modelProperties) { + try { + String service = modelProperties.getGlobal().getService(); + Tca tca = modelProperties.getType(Tca.class); + modelProperties.setCurrentModelElementId(tca.getId()); + ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.policy.template", service); + String policyName = modelProperties.getCurrentPolicyScopeAndPolicyName(); + rootNode.put("policyName", policyName); + rootNode.put("description", "MicroService vCPE Policy"); + ((ObjectNode) rootNode.get("content")).replace("tca_policy", + createPolicyContent(refProp, modelProperties, service, policyName, tca)); + String tcaPolicyReq = rootNode.toString(); + logger.info("tcaPolicyReq=" + tcaPolicyReq); + return tcaPolicyReq; + } catch (IOException e) { + throw new TcaRequestFormatterException("Exception caught when attempting to create the policy JSON", e); + } + } + + /** + * Format Tca Policy Content JSON + * + * @param refProp + * The refProp generally created by Spring, it's an access on the + * clds-references.properties file + * @param modelProperties + * The Model Prop created from BPMN JSON and BPMN properties JSON + * @param service + * The service ID, if not specified getGlobal.getService will be + * used + * @param policyName + * The policyName, if not specified the + * modelProperties.getCurrentPolicyScopeAndPolicyName will be + * used + * @param tca + * The Tca object, if not specified the + * modelProperties.setCurrentModelElementId will be used + * @return The Json node containing what should be sent to policy + */ + public static JsonNode createPolicyContent(RefProp refProp, ModelProperties modelProperties, String service, + String policyName, Tca tca) { + try { + String serviceToUse = service; + String policyNameToUse = policyName; + Tca tcaToUse = tca; + if (serviceToUse == null) { + serviceToUse = modelProperties.getGlobal().getService(); + } + if (tcaToUse == null) { + tcaToUse = modelProperties.getType(Tca.class); + modelProperties.setCurrentModelElementId(tcaToUse.getId()); + } + if (policyNameToUse == null) { + policyNameToUse = modelProperties.getCurrentPolicyScopeAndPolicyName(); + } + ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", serviceToUse); + ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("eventName", + tcaToUse.getTcaItem().getEventName()); + ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("policyName", policyNameToUse); + ((ObjectNode) rootNode.get("metricsPerEventName").get(0)).put("controlLoopSchemaType", + tcaToUse.getTcaItem().getControlLoopSchemaType()); + ObjectNode thresholdsParent = ((ObjectNode) rootNode.get("metricsPerEventName").get(0)); + addThresholds(refProp, serviceToUse, thresholdsParent, tcaToUse.getTcaItem(), modelProperties); + logger.info("tcaPolicyContent=" + rootNode.toString()); + return rootNode; + } catch (IOException e) { + throw new TcaRequestFormatterException("Exception caught when attempting to create the policy content JSON", + e); + } + } + + /** + * Add threshold values to the existing policy JSON. + * + * @param refProp + * The refProp generally created by Spring, it's an access on the + * clds-references.properties file + * @param service + * The Service value extracted from Global section of the Bpmn + * Properties JSON + * @param appendToNode + * The JSON structure from where the thresholds section must be + * added + * @param tcaItem + * The TCA item contained in the Tca object + * @param modelProperties + * The Model Properties created from BPMN JSON and BPMN + * properties JSON + */ + private static void addThresholds(RefProp refProp, String service, ObjectNode appendToNode, TcaItem tcaItem, + ModelProperties modelProperties) { + ArrayNode tcaNodes = appendToNode.withArray("thresholds"); + ObjectNode tcaNode; + try { + tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.thresholds.template", service); + for (TcaThreshold tcaThreshold : tcaItem.getTcaThresholds()) { + tcaNode.put("closedLoopControlName", modelProperties.getControlNameAndPolicyUniqueId()); + tcaNode.put("fieldPath", tcaThreshold.getFieldPath()); + tcaNode.put("thresholdValue", tcaThreshold.getThreshold()); + tcaNode.put("direction", tcaThreshold.getOperator()); + tcaNode.put("closedLoopEventStatus", tcaThreshold.getClosedLoopEventStatus()); + tcaNodes.add(tcaNode); + } + } catch (IOException e) { + throw new TcaRequestFormatterException("Exception caught when attempting to create the thresholds JSON", e); + } + } + + /** + * This method updates the blueprint that is received in the UI with the TCA + * Json. + * + * @param refProp + * * The refProp generally created by Spring, it's an access on + * the clds-references.properties file + * @param modelProperties + * The Model Prop created from BPMN JSON and BPMN properties JSON + * @param yamlValue + * The yaml string received from the UI + * @return The updated YAML as a string + */ + public static String updatedBlueprintWithConfiguration(RefProp refProp, ModelProperties modelProperties, + String yamlValue) { + String jsonPolicy = ((ObjectNode) createPolicyContent(refProp, modelProperties, null, null, null)).toString(); + logger.info("Yaml that will be updated:" + yamlValue); + Yaml yaml = new Yaml(); + Map loadedYaml = (Map) yaml.load(yamlValue); + Map nodeTemplates = (Map) loadedYaml.get("node_templates"); + Map tcaObject = (Map) nodeTemplates.get("tca_tca"); + Map propsObject = (Map) tcaObject.get("properties"); + Map appPreferences = (Map) propsObject.get("app_preferences"); + appPreferences.put("tca_policy", jsonPolicy); + String blueprint = yaml.dump(loadedYaml); + logger.info("Yaml updated:" + blueprint); + return blueprint; + } +} \ No newline at end of file diff --git a/src/main/java/org/onap/clamp/clds/config/CldsConfiguration.java b/src/main/java/org/onap/clamp/clds/config/CldsConfiguration.java index 0c73ac71..45d2786a 100644 --- a/src/main/java/org/onap/clamp/clds/config/CldsConfiguration.java +++ b/src/main/java/org/onap/clamp/clds/config/CldsConfiguration.java @@ -40,12 +40,12 @@ import org.onap.clamp.clds.client.HolmesPolicyDelegate; import org.onap.clamp.clds.client.HolmesPolicyDeleteDelegate; import org.onap.clamp.clds.client.OperationalPolicyDelegate; import org.onap.clamp.clds.client.OperationalPolicyDeleteDelegate; -import org.onap.clamp.clds.client.PolicyClient; -import org.onap.clamp.clds.client.SdcCatalogServices; import org.onap.clamp.clds.client.SdcSendReqDelegate; import org.onap.clamp.clds.client.TcaPolicyDelegate; import org.onap.clamp.clds.client.TcaPolicyDeleteDelegate; -import org.onap.clamp.clds.client.req.SdcReq; +import org.onap.clamp.clds.client.req.policy.PolicyClient; +import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices; +import org.onap.clamp.clds.client.req.sdc.SdcReq; import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.model.refprop.RefProp; import org.onap.clamp.clds.transform.XslTransformer; diff --git a/src/main/java/org/onap/clamp/clds/dao/CldsDao.java b/src/main/java/org/onap/clamp/clds/dao/CldsDao.java index c5332a6b..2c052c1a 100644 --- a/src/main/java/org/onap/clamp/clds/dao/CldsDao.java +++ b/src/main/java/org/onap/clamp/clds/dao/CldsDao.java @@ -26,12 +26,7 @@ package org.onap.clamp.clds.dao; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import java.io.IOException; import java.io.InputStream; -import java.io.ObjectInputStream; -import java.sql.Blob; -import java.sql.ResultSet; -import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -49,7 +44,6 @@ import org.onap.clamp.clds.model.CldsTemplate; import org.onap.clamp.clds.model.ValueItem; import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.SqlParameterSource; import org.springframework.jdbc.core.simple.SimpleJdbcCall; @@ -60,10 +54,8 @@ import org.springframework.stereotype.Repository; */ @Repository("cldsDao") public class CldsDao { - protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class); protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); - private JdbcTemplate jdbcTemplateObject; private SimpleJdbcCall procGetModel; private SimpleJdbcCall procGetModelTemplate; @@ -75,7 +67,6 @@ public class CldsDao { private SimpleJdbcCall procDelAllModelInstances; private SimpleJdbcCall procInsModelInstance; private SimpleJdbcCall procDelModelInstance; - private static final String HEALTHCHECK = "Select 1"; /** @@ -202,7 +193,6 @@ public class CldsDao { model.getEvent().setUserid((String) out.get("v_event_user_id")); model.setTypeId((String) out.get("v_service_type_id")); model.setDeploymentId((String) out.get("v_deployment_id")); - Map modelResults = logSqlExecution(procGetModel, in); Object modelResultObject = modelResults.get("#result-set-1"); if (modelResultObject != null && modelResultObject instanceof ArrayList) { @@ -266,7 +256,6 @@ public class CldsDao { // Delete all existing model instances for given controlNameUUID logger.debug("deleting instances for: {}", model.getControlNameUuid()); delAllModelInstances(model.getControlNameUuid()); - if (modelInstancesList == null) { logger.debug("modelInstancesList == null"); } else { @@ -358,41 +347,6 @@ public class CldsDao { logSqlExecution(procUpdEvent, in); } - /** - * Generic mapper for list of values - */ - private static final class ValueItemMapper implements RowMapper { - @Override - public ValueItem mapRow(ResultSet rs, int rowNum) throws SQLException { - ValueItem item = new ValueItem(); - item.setValue(rs.getString(1)); - return item; - } - } - - /** - * Generic mapper for CldsDBServiceCache - */ - private static final class CldsServiceDataMapper implements RowMapper { - @Override - public CldsServiceData mapRow(ResultSet rs, int rowNum) throws SQLException { - CldsServiceData cldsServiceData = new CldsServiceData(); - long age; - age = rs.getLong(5); - Blob blob = rs.getBlob(4); - InputStream is = blob.getBinaryStream(); - ObjectInputStream oip; - try { - oip = new ObjectInputStream(is); - cldsServiceData = (CldsServiceData) oip.readObject(); - cldsServiceData.setAgeOfRecord(age); - } catch (IOException | ClassNotFoundException e) { - logger.error("Error caught while retrieving cldsServiceData from database", e); - } - return cldsServiceData; - } - } - /** * Return list of model names * @@ -503,14 +457,14 @@ public class CldsDao { } /** - * Method to get all models with model properties. + * Method to get deployed/active models with model properties. * * @return list of CldsModelProp */ - public List getAllModelProperties() { + public List getDeployedModelProperties() { List cldsModelPropList = new ArrayList(); - String modelsSql = "select m.model_id, m.model_name, mp.model_prop_id, mp.model_prop_text FROM model m, model_properties mp" - + " WHERE m.model_prop_id = mp.model_prop_id"; + String modelsSql = "select m.model_id, m.model_name, mp.model_prop_id, mp.model_prop_text FROM model m, model_properties mp, event e " + + "WHERE m.model_prop_id = mp.model_prop_id and m.event_id = e.event_id and e.action_cd = 'DEPLOY'"; List> rows = jdbcTemplateObject.queryForList(modelsSql); CldsModelProp cldsModelProp = null; for (Map row : rows) { @@ -523,5 +477,4 @@ public class CldsDao { } return cldsModelPropList; } - } diff --git a/src/main/java/org/onap/clamp/clds/dao/CldsServiceDataMapper.java b/src/main/java/org/onap/clamp/clds/dao/CldsServiceDataMapper.java new file mode 100644 index 00000000..91d89505 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/dao/CldsServiceDataMapper.java @@ -0,0 +1,62 @@ +/*- + * ============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.dao; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import org.apache.commons.io.serialization.ValidatingObjectInputStream; +import java.sql.Blob; +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.onap.clamp.clds.model.CldsServiceData; +import org.springframework.jdbc.core.RowMapper; + +/** + * Generic mapper for CldsDBServiceCache + */ +public final class CldsServiceDataMapper implements RowMapper { + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsDao.class); + + @Override + public CldsServiceData mapRow(ResultSet rs, int rowNum) throws SQLException { + CldsServiceData cldsServiceData = new CldsServiceData(); + long age; + age = rs.getLong(5); + Blob blob = rs.getBlob(4); + InputStream is = blob.getBinaryStream(); + try (ValidatingObjectInputStream oip = new ValidatingObjectInputStream(is)) { + oip.accept(CldsServiceData.class); + cldsServiceData = (CldsServiceData) oip.readObject(); + cldsServiceData.setAgeOfRecord(age); + } catch (IOException | ClassNotFoundException e) { + logger.error("Error caught while retrieving cldsServiceData from database", e); + } + return cldsServiceData; + } +} diff --git a/src/main/java/org/onap/clamp/clds/dao/ValueItemMapper.java b/src/main/java/org/onap/clamp/clds/dao/ValueItemMapper.java new file mode 100644 index 00000000..12367284 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/dao/ValueItemMapper.java @@ -0,0 +1,42 @@ +/*- + * ============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.dao; + +import java.sql.ResultSet; +import java.sql.SQLException; + +import org.onap.clamp.clds.model.ValueItem; +import org.springframework.jdbc.core.RowMapper; + +/** + * Generic mapper for list of values + */ +public final class ValueItemMapper implements RowMapper { + @Override + public ValueItem mapRow(ResultSet rs, int rowNum) throws SQLException { + ValueItem item = new ValueItem(); + item.setValue(rs.getString(1)); + return item; + } +} diff --git a/src/main/java/org/onap/clamp/clds/exception/ModelPropertiesException.java b/src/main/java/org/onap/clamp/clds/exception/ModelPropertiesException.java new file mode 100644 index 00000000..09fb5dba --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/exception/ModelPropertiesException.java @@ -0,0 +1,61 @@ +/*- + * ============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.exception; + +/** + * The exception used in case of issues when decoding the ModelProperties JSON + * generated by the Designer UI. + * + * + */ +public class ModelPropertiesException extends RuntimeException { + /** + * Generated ID. + */ + private static final long serialVersionUID = 8452294782552680344L; + + /** + * This constructor can be used to create a new ModelPropertiesException. + * + * @param message + * A string message detailing the problem + * @param e + * The exception sent by the code + */ + public ModelPropertiesException(String message, Throwable e) { + super(message, e); + } + + /** + * This constructor can be used to create a new ModelPropertiesException. + * Use this constructor only if you are creating a new exception stack, not + * if an exception was already raised by another code. + * + * @param message + * A string message detailing the problem + */ + public ModelPropertiesException(String message) { + super(message); + } +} diff --git a/src/main/java/org/onap/clamp/clds/model/prop/ModelBpmn.java b/src/main/java/org/onap/clamp/clds/model/prop/ModelBpmn.java index f3d344a7..9738396a 100644 --- a/src/main/java/org/onap/clamp/clds/model/prop/ModelBpmn.java +++ b/src/main/java/org/onap/clamp/clds/model/prop/ModelBpmn.java @@ -25,8 +25,6 @@ package org.onap.clamp.clds.model.prop; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonParseException; -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; @@ -40,25 +38,22 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.onap.clamp.clds.exception.ModelBpmnException; import org.onap.clamp.clds.service.CldsService; /** * Parse Model BPMN properties. *

- * Example json: {"policy" - * :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]} + * Example json: {"policy" :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]} */ public class ModelBpmn { protected static final EELFLogger logger = EELFManager.getInstance() .getLogger(CldsService.class); protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger(); - // for each type, an array of entries private final Map> entriesByType = new HashMap<>(); - // for each id, an array of entries private final Map> entriesById = new HashMap<>(); - // List of all elementIds private List bpmnElementIds; @@ -67,36 +62,37 @@ public class ModelBpmn { * * @param modelBpmnPropText * @return - * @throws IOException - * @throws JsonMappingException - * @throws JsonParseException */ - public static ModelBpmn create(String modelBpmnPropText) throws IOException { - ModelBpmn modelBpmn = new ModelBpmn(); - ObjectMapper objectMapper = new ObjectMapper(); - ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class); - // iterate over each entry like: - // "Policy":[{"id":"Policy","from":"StartEvent_1"}] - Iterator> entryItr = root.fields(); - List bpmnElementIdList = new ArrayList<>(); - while (entryItr.hasNext()) { - // process the entry - Entry entry = entryItr.next(); - String type = entry.getKey(); - ArrayNode arrayNode = (ArrayNode) entry.getValue(); - // process each id/from object, like: - // {"id":"Policy","from":"StartEvent_1"} - for (JsonNode anArrayNode : arrayNode) { - ObjectNode node = (ObjectNode) anArrayNode; - String id = node.get("id").asText(); - String fromId = node.get("from").asText(); - ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId); - modelBpmn.addEntry(modelBpmnEntry); - bpmnElementIdList.add(id); + public static ModelBpmn create(String modelBpmnPropText) { + try { + ModelBpmn modelBpmn = new ModelBpmn(); + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class); + // iterate over each entry like: + // "Policy":[{"id":"Policy","from":"StartEvent_1"}] + Iterator> entryItr = root.fields(); + List bpmnElementIdList = new ArrayList<>(); + while (entryItr.hasNext()) { + // process the entry + Entry entry = entryItr.next(); + String type = entry.getKey(); + ArrayNode arrayNode = (ArrayNode) entry.getValue(); + // process each id/from object, like: + // {"id":"Policy","from":"StartEvent_1"} + for (JsonNode anArrayNode : arrayNode) { + ObjectNode node = (ObjectNode) anArrayNode; + String id = node.get("id").asText(); + String fromId = node.get("from").asText(); + ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId); + modelBpmn.addEntry(modelBpmnEntry); + bpmnElementIdList.add(id); + } + modelBpmn.setBpmnElementIds(bpmnElementIdList); } - modelBpmn.setBpmnElementIds(bpmnElementIdList); + return modelBpmn; + } catch (IOException e) { + throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e); } - return modelBpmn; } /** @@ -122,8 +118,8 @@ public class ModelBpmn { } /** - * This method verifies if the ModelElement Type (holmes, tca, - * ...) is in the list. + * This method verifies if the ModelElement Type (holmes, tca, ...) is in + * the list. * * @param type * A model Element type (tca, ...) diff --git a/src/main/java/org/onap/clamp/clds/service/CldsService.java b/src/main/java/org/onap/clamp/clds/service/CldsService.java index f9eeeb58..bbcfd5d2 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsService.java @@ -56,13 +56,14 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.xml.transform.TransformerException; +import org.apache.commons.codec.DecoderException; import org.apache.commons.lang3.StringUtils; import org.camunda.bpm.engine.RuntimeService; import org.camunda.bpm.engine.runtime.ProcessInstance; import org.json.simple.parser.ParseException; import org.onap.clamp.clds.client.DcaeDispatcherServices; import org.onap.clamp.clds.client.DcaeInventoryServices; -import org.onap.clamp.clds.client.SdcCatalogServices; +import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices; import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.exception.CldsConfigException; import org.onap.clamp.clds.exception.SdcCommunicationException; @@ -337,12 +338,6 @@ public class CldsService extends SecureServiceBase { logger.info("PUT propText={}", cldsModel.getPropText()); logger.info("PUT imageText={}", cldsModel.getImageText()); cldsModel.setName(modelName); - try { - duplicateCheckforServiceVf(modelName, cldsModel.getPropText()); - } catch (IOException | BadRequestException e) { - logger.error("Exception occured during duplicate check for service and VF", e); - throw new CldsConfigException(e.getMessage(), e); - } if (cldsModel.getTemplateName() != null) { CldsTemplate template = cldsDao.getTemplate(cldsModel.getTemplateName()); if (template != null) { @@ -390,8 +385,13 @@ public class CldsService extends SecureServiceBase { * @param model * @return * @throws TransformerException + * In case of issues when doing the XSLT of the BPMN flow * @throws ParseException + * In case of issues when parsing the JSON * @throws GeneralSecurityException + * In case of issues when decrypting the password + * @throws DecoderException + * In case of issues with the Hex String decoding */ @ApiOperation(value = "Saves and processes an action for a CLDS model by name", notes = "", response = String.class) @PUT @@ -400,7 +400,7 @@ public class CldsService extends SecureServiceBase { @Produces(MediaType.APPLICATION_JSON) public CldsModel putModelAndProcessAction(@PathParam("action") String action, @PathParam("modelName") String modelName, @QueryParam("test") String test, CldsModel model) - throws TransformerException, ParseException, GeneralSecurityException { + throws TransformerException, ParseException, GeneralSecurityException, DecoderException { Date startTime = new Date(); LoggingUtils.setRequestContext("CldsService: Process model action", getPrincipalName()); String actionCd = action.toUpperCase(); @@ -558,13 +558,15 @@ public class CldsService extends SecureServiceBase { * * @throws GeneralSecurityException * In case of issue when decryting the SDC password + * @throws DecoderException + * In case of issues with the decoding of the Hex String * */ @ApiOperation(value = "Retrieves sdc services", notes = "", response = String.class) @GET @Path("/sdc/services") @Produces(MediaType.APPLICATION_JSON) - public String getSdcServices() throws GeneralSecurityException { + public String getSdcServices() throws GeneralSecurityException, DecoderException { Date startTime = new Date(); LoggingUtils.setRequestContext("CldsService: GET sdc services", getPrincipalName()); String retStr; @@ -603,7 +605,9 @@ public class CldsService extends SecureServiceBase { * on refresh and non refresh * * @throws GeneralSecurityException - * In case of issue when decryting the SDC password + * In case of issues with the decryting the encrypted password + * @throws DecoderException + * In case of issues with the decoding of the Hex String * */ @ApiOperation(value = "Retrieves total properties by using invariantUUID based on refresh and non refresh", notes = "", response = String.class) @@ -612,7 +616,8 @@ public class CldsService extends SecureServiceBase { @Produces(MediaType.APPLICATION_JSON) public String getSdcPropertiesByServiceUUIDForRefresh( @PathParam("serviceInvariantUUID") String serviceInvariantUUID, - @DefaultValue("false") @QueryParam("refresh") String refresh) throws GeneralSecurityException { + @DefaultValue("false") @QueryParam("refresh") String refresh) + throws GeneralSecurityException, DecoderException { Date startTime = new Date(); LoggingUtils.setRequestContext("CldsService: GET sdc properties by uuid", getPrincipalName()); CldsServiceData cldsServiceData = new CldsServiceData(); @@ -831,6 +836,12 @@ public class CldsService extends SecureServiceBase { @QueryParam("test") String test, CldsModel model) throws IOException { Date startTime = new Date(); LoggingUtils.setRequestContext("CldsService: Deploy model", getPrincipalName()); + try { + checkForDuplicateServiceVf(modelName, model.getPropText()); + } catch (IOException | BadRequestException e) { + logger.error("Exception occured during duplicate check for service and VF", e); + throw new CldsConfigException(e.getMessage(), e); + } String deploymentId = ""; // If model is already deployed then pass same deployment id if (model.getDeploymentId() != null && !model.getDeploymentId().isEmpty()) { @@ -932,13 +943,13 @@ public class CldsService extends SecureServiceBase { } } - private void duplicateCheckforServiceVf(String modelName, String modelPropText) throws IOException { + private void checkForDuplicateServiceVf(String modelName, String modelPropText) throws IOException { JsonNode modelJson = new ObjectMapper().readTree(modelPropText); JsonNode globalNode = modelJson.get("global"); String service = AbstractModelElement.getValueByName(globalNode, "service"); List resourceVf = AbstractModelElement.getValuesByName(globalNode, "vf"); if (service != null && resourceVf != null && !resourceVf.isEmpty()) { - List cldsModelPropList = cldsDao.getAllModelProperties(); + List cldsModelPropList = cldsDao.getDeployedModelProperties(); for (CldsModelProp cldsModelProp : cldsModelPropList) { JsonNode currentJson = new ObjectMapper().readTree(cldsModelProp.getPropText()); JsonNode currentNode = currentJson.get("global"); @@ -947,8 +958,9 @@ public class CldsService extends SecureServiceBase { if (currentVf != null && !currentVf.isEmpty()) { if (!modelName.equalsIgnoreCase(cldsModelProp.getName()) && service.equalsIgnoreCase(currentService) && resourceVf.get(0).equalsIgnoreCase(currentVf.get(0))) { - throw new BadRequestException("Same service/VF already exists in " + cldsModelProp.getName() - + " model, please select different service/VF."); + throw new BadRequestException("Same Service/VF already exists in " + cldsModelProp.getName() + + " model, please select different Service/VF."); + } } } diff --git a/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java b/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java index c84f5efb..070c0667 100644 --- a/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java +++ b/src/main/java/org/onap/clamp/clds/service/CldsTemplateService.java @@ -44,7 +44,6 @@ import javax.xml.transform.TransformerException; import org.onap.clamp.clds.dao.CldsDao; import org.onap.clamp.clds.model.CldsTemplate; import org.onap.clamp.clds.model.ValueItem; -import org.onap.clamp.clds.transform.XslTransformer; import org.onap.clamp.clds.util.LoggingUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -55,15 +54,11 @@ import org.springframework.beans.factory.annotation.Value; @AjscService @Path("/cldsTempate") public class CldsTemplateService extends SecureServiceBase { - @Value("${CLDS_PERMISSION_TYPE_TEMPLATE:permission-type-template}") private String cldsPermissionTypeTemplate; - @Value("${CLDS_PERMISSION_INSTANCE:dev}") private String cldsPermissionInstance; - private SecureServicePermission permissionReadTemplate; - private SecureServicePermission permissionUpdateTemplate; @PostConstruct @@ -75,12 +70,7 @@ public class CldsTemplateService extends SecureServiceBase { } @Autowired - private CldsDao cldsDao; - - @Autowired - private XslTransformer cldsBpmnTransformer; - - private static String userid; + private CldsDao cldsDao; /** * REST service that retrieves BPMN for a CLDS template name from the @@ -170,7 +160,6 @@ public class CldsTemplateService extends SecureServiceBase { Date startTime = new Date(); LoggingUtils.setRequestContext("CldsTemplateService: PUT template", getPrincipalName()); isAuthorized(permissionUpdateTemplate); - logger.info("PUT Template for templateName=" + templateName); logger.info("PUT bpmnText=" + cldsTemplate.getBpmnText()); logger.info("PUT propText=" + cldsTemplate.getPropText()); @@ -185,13 +174,11 @@ public class CldsTemplateService extends SecureServiceBase { logger.info(" bpmnText : " + cldsTemplate.getBpmnText()); logger.info(" Image Text : " + cldsTemplate.getImageText()); logger.info(" Prop Text : " + cldsTemplate.getPropText()); - cldsTemplate.save(cldsDao, userid); - + cldsTemplate.save(cldsDao, null); // audit log LoggingUtils.setTimeContext(startTime, new Date()); LoggingUtils.setResponseContext("0", "Put template success", this.getClass().getName()); auditLogger.info("PUT template completed"); - return cldsTemplate; } diff --git a/src/test/java/org/onap/clamp/clds/AbstractItCase.java b/src/test/java/org/onap/clamp/clds/AbstractItCase.java index 9be8a13b..d3f64240 100644 --- a/src/test/java/org/onap/clamp/clds/AbstractItCase.java +++ b/src/test/java/org/onap/clamp/clds/AbstractItCase.java @@ -24,7 +24,7 @@ package org.onap.clamp.clds; import org.junit.BeforeClass; -import org.onap.clamp.clds.client.PolicyClient; +import org.onap.clamp.clds.client.req.policy.PolicyClient; import org.onap.clamp.clds.model.refprop.RefProp; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; diff --git a/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java b/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java index 734b6052..9b8f4aad 100644 --- a/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java +++ b/src/test/java/org/onap/clamp/clds/client/req/OperationalPolicyReqTest.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.List; import org.junit.Test; +import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; import org.onap.policy.controlloop.policy.PolicyResult; import org.onap.policy.sdc.Resource; import org.onap.policy.sdc.ResourceType; diff --git a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java index 1427c329..78b24013 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsDaoItCase.java @@ -25,6 +25,8 @@ package org.onap.clamp.clds.it; import static org.junit.Assert.assertEquals; +import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils; + import java.io.IOException; import javax.ws.rs.NotFoundException; @@ -51,10 +53,8 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @TestPropertySource(locations = "classpath:application-no-camunda.properties") public class CldsDaoItCase extends AbstractItCase { - @Autowired public CldsDao cldsDao; - private String bpmnText; private String imageText; private String bpmnPropText; @@ -74,40 +74,37 @@ public class CldsDaoItCase extends AbstractItCase { @Test public void testModelSave() throws IOException { + String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5); + String randomNameModel = RandomStringUtils.randomAlphanumeric(5); // Add the template first CldsTemplate newTemplate = new CldsTemplate(); - newTemplate.setName("test-template"); - + newTemplate.setName(randomNameTemplate); newTemplate.setBpmnText(bpmnText); newTemplate.setImageText(imageText); - // Save the template in DB cldsDao.setTemplate(newTemplate, "user"); // Test if it's well there - CldsTemplate newTemplateRead = cldsDao.getTemplate("test-template"); + CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate); assertEquals(bpmnText, newTemplateRead.getBpmnText()); assertEquals(imageText, newTemplateRead.getImageText()); - // Save the model CldsModel newModel = new CldsModel(); - newModel.setName("test-model"); - + newModel.setName(randomNameModel); newModel.setBpmnText(bpmnText); newModel.setImageText(imageText); newModel.setPropText(bpmnPropText); newModel.setControlNamePrefix("ClosedLoop-"); - newModel.setTemplateName("test-template"); + newModel.setTemplateName(randomNameTemplate); newModel.setTemplateId(newTemplate.getId()); newModel.setDocText(newTemplate.getPropText()); newModel.setDocId(newTemplate.getPropId()); // Save the model in DB cldsDao.setModel(newModel, "user"); // Test if the model can be retrieved - CldsModel newCldsModel = cldsDao.getModelTemplate("test-model"); + CldsModel newCldsModel = cldsDao.getModelTemplate(randomNameModel); assertEquals(bpmnText, newCldsModel.getBpmnText()); assertEquals(imageText, newCldsModel.getImageText()); assertEquals(bpmnPropText, newCldsModel.getPropText()); - } @Test(expected = NotFoundException.class) @@ -125,21 +122,16 @@ public class CldsDaoItCase extends AbstractItCase { // Add the template first CldsTemplate newTemplate = new CldsTemplate(); newTemplate.setName("test-template-for-event"); - newTemplate.setBpmnText(bpmnText); newTemplate.setImageText(imageText); - newTemplate.save(cldsDao, "user"); - // Test if it's well there CldsTemplate newTemplateRead = CldsTemplate.retrieve(cldsDao, "test-template-for-event", false); assertEquals(bpmnText, newTemplateRead.getBpmnText()); assertEquals(imageText, newTemplateRead.getImageText()); - // Save the model CldsModel newModel = new CldsModel(); newModel.setName("test-model-for-event"); - newModel.setBpmnText(bpmnText); newModel.setImageText(imageText); newModel.setPropText(bpmnPropText); @@ -148,7 +140,6 @@ public class CldsDaoItCase extends AbstractItCase { newModel.setTemplateId(newTemplate.getId()); newModel.setDocText(newTemplate.getPropText()); newModel.setDocId(newTemplate.getPropId()); - CldsEvent.insEvent(cldsDao, newModel, "user", CldsEvent.ACTION_RESTART, CldsEvent.ACTION_STATE_COMPLETED, "process-instance-id"); } diff --git a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java index a1882eaa..f6e9abdd 100644 --- a/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/CldsServiceItCase.java @@ -25,20 +25,30 @@ package org.onap.clamp.clds.it; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import com.att.aft.dme2.internal.apache.commons.lang.RandomStringUtils; + +import java.io.IOException; import java.io.InputStream; import java.security.Principal; import java.util.Properties; import javax.ws.rs.core.SecurityContext; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.clamp.clds.AbstractItCase; +import org.onap.clamp.clds.dao.CldsDao; +import org.onap.clamp.clds.model.CldsHealthCheck; import org.onap.clamp.clds.model.CldsInfo; +import org.onap.clamp.clds.model.CldsModel; +import org.onap.clamp.clds.model.CldsTemplate; import org.onap.clamp.clds.service.CldsService; +import org.onap.clamp.clds.util.ResourceFileUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; @@ -52,19 +62,34 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @TestPropertySource(locations = "classpath:application-no-camunda.properties") public class CldsServiceItCase extends AbstractItCase { - @Autowired - CldsService cldsService; + CldsService cldsService; + private String bpmnText; + private String imageText; + private String bpmnPropText; + @Autowired + public CldsDao cldsDao; + + /** + * Setup the variable before the tests execution. + * + * @throws IOException + * In case of issues when opening the files + */ + @Before + public void setupBefore() throws IOException { + bpmnText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-template.xml"); + imageText = ResourceFileUtil.getResourceAsString("example/dao/image-template.xml"); + bpmnPropText = ResourceFileUtil.getResourceAsString("example/dao/bpmn-prop.json"); + } @Test public void testCldsInfoNotAuthorized() throws Exception { SecurityContext securityContext = Mockito.mock(SecurityContext.class); Principal principal = Mockito.mock(Principal.class); Mockito.when(principal.getName()).thenReturn("admin"); - Mockito.when(securityContext.getUserPrincipal()).thenReturn(principal); cldsService.setSecurityContext(securityContext); - CldsInfo cldsInfo = cldsService.getCldsInfo(); assertFalse(cldsInfo.isPermissionReadCl()); assertFalse(cldsInfo.isPermissionReadTemplate()); @@ -74,32 +99,75 @@ public class CldsServiceItCase extends AbstractItCase { @Test public void testCldsInfoAuthorized() throws Exception { - SecurityContext securityContext = Mockito.mock(SecurityContext.class); Principal p = Mockito.mock(Principal.class); Mockito.when(p.getName()).thenReturn("admin"); - Mockito.when(securityContext.getUserPrincipal()).thenReturn(p); Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true); Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true); Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true); Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true); - cldsService.setSecurityContext(securityContext); - CldsInfo cldsInfo = cldsService.getCldsInfo(); assertTrue(cldsInfo.isPermissionReadCl()); assertTrue(cldsInfo.isPermissionReadTemplate()); assertTrue(cldsInfo.isPermissionUpdateCl()); assertTrue(cldsInfo.isPermissionUpdateTemplate()); - Properties prop = new Properties(); - InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("clds-version.properties"); prop.load(in); in.close(); - assertEquals(cldsInfo.getCldsVersion(), prop.getProperty("clds.version")); assertEquals(cldsInfo.getUserName(), "admin"); } + + @Test + public void testGetHealthCheck() throws Exception { + CldsHealthCheck cldsHealthCheck = cldsService.gethealthcheck(); + assertNotNull(cldsHealthCheck); + assertEquals("UP", cldsHealthCheck.getHealthCheckStatus()); + assertEquals("CLDS-APP", cldsHealthCheck.getHealthCheckComponent()); + assertEquals("OK", cldsHealthCheck.getDescription()); + } + + @Test + public void testPutModel() throws Exception { + String randomNameTemplate = RandomStringUtils.randomAlphanumeric(5); + String randomNameModel = RandomStringUtils.randomAlphanumeric(5); + SecurityContext securityContext = Mockito.mock(SecurityContext.class); + Principal p = Mockito.mock(Principal.class); + Mockito.when(p.getName()).thenReturn("admin"); + Mockito.when(securityContext.getUserPrincipal()).thenReturn(p); + Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|read")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-cl|dev|update")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-template|dev|read")).thenReturn(true); + Mockito.when(securityContext.isUserInRole("permission-type-template|dev|update")).thenReturn(true); + cldsService.setSecurityContext(securityContext); + // Add the template first + CldsTemplate newTemplate = new CldsTemplate(); + newTemplate.setName(randomNameTemplate); + newTemplate.setBpmnText(bpmnText); + newTemplate.setImageText(imageText); + // Save the template in DB + cldsDao.setTemplate(newTemplate, "user"); + // Test if it's well there + CldsTemplate newTemplateRead = cldsDao.getTemplate(randomNameTemplate); + assertEquals(bpmnText, newTemplateRead.getBpmnText()); + assertEquals(imageText, newTemplateRead.getImageText()); + // Save the model + CldsModel newModel = new CldsModel(); + newModel.setName(randomNameTemplate); + newModel.setBpmnText(bpmnText); + newModel.setImageText(imageText); + newModel.setPropText(bpmnPropText); + newModel.setControlNamePrefix("ClosedLoop-"); + newModel.setTemplateName("test-template"); + newModel.setTemplateId(newTemplate.getId()); + newModel.setDocText(newTemplate.getPropText()); + newModel.setDocId(newTemplate.getPropId()); + // Test the PutModel method + cldsService.putModel(randomNameModel, newModel); + // Verify whether it has been added properly or not + assertNotNull(cldsDao.getModel(randomNameModel)); + } } diff --git a/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java b/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java index c87bbf62..30091995 100644 --- a/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java @@ -36,7 +36,7 @@ import java.util.Map; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.AbstractItCase; -import org.onap.clamp.clds.client.req.OperationalPolicyReq; +import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; import org.onap.clamp.clds.model.CldsEvent; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.prop.Policy; @@ -52,7 +52,6 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @TestPropertySource(locations = "classpath:application-no-camunda.properties") public class OperationPolicyReqItCase extends AbstractItCase { - @Test public void formatAttributesTest() throws IOException, BuilderException { String modelProp = ResourceFileUtil.getResourceAsString("example/modelProp.json"); diff --git a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java index c129800d..27573776 100644 --- a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java @@ -34,8 +34,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.AbstractItCase; -import org.onap.clamp.clds.client.req.OperationalPolicyReq; -import org.onap.clamp.clds.client.req.TcaRequestFormatter; +import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq; +import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter; import org.onap.clamp.clds.model.CldsEvent; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.model.prop.Policy; @@ -77,7 +77,6 @@ public class PolicyClientItCase extends AbstractItCase { if (policy.isFound()) { for (PolicyChain policyChain : policy.getPolicyChains()) { String operationalPolicyRequestUuid = UUID.randomUUID().toString(); - Map> attributes = OperationalPolicyReq.formatAttributes(refProp, prop, policy.getId(), policyChain); policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid); @@ -91,7 +90,6 @@ public class PolicyClientItCase extends AbstractItCase { if (tca.isFound()) { String tcaPolicyRequestUuid = UUID.randomUUID().toString(); String policyJson = TcaRequestFormatter.createPolicyJson(refProp, prop); - try { policyClient.sendMicroServiceInJson(policyJson, prop, tcaPolicyRequestUuid); } catch (Exception e) { @@ -102,7 +100,6 @@ public class PolicyClientItCase extends AbstractItCase { private void deleteOperationalPolicy(String actionCd) throws Exception { ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp); - Policy policy = prop.getType(Policy.class); if (policy.isFound()) { prop.setCurrentModelElementId(policy.getId()); @@ -115,11 +112,9 @@ public class PolicyClientItCase extends AbstractItCase { private void deleteTcaPolicy(String actionCd) throws Exception { ModelProperties prop = new ModelProperties(modelName, controlName, actionCd, false, modelBpmnProp, modelProp); - Tca tca = prop.getType(Tca.class); if (tca.isFound()) { prop.setCurrentModelElementId(tca.getId()); - try { policyClient.deleteMicrosService(prop); } catch (Exception e) { @@ -133,21 +128,15 @@ public class PolicyClientItCase extends AbstractItCase { * Temporarily disabled Test. */ public void testCreateUpdateDeleteOperationalPolicy() throws Exception { - createUpdateOperationalPolicy(CldsEvent.ACTION_SUBMIT); - TimeUnit.SECONDS.sleep(20); - deleteOperationalPolicy(CldsEvent.ACTION_DELETE); } @Test public void testCreateUpdateDeleteTcaPolicy() throws Exception { - createUpdateTcaPolicy(CldsEvent.ACTION_SUBMIT); - TimeUnit.SECONDS.sleep(20); - deleteTcaPolicy(CldsEvent.ACTION_DELETE); } } diff --git a/src/test/java/org/onap/clamp/clds/it/RefPropItCase.java b/src/test/java/org/onap/clamp/clds/it/RefPropItCase.java index 478a7b38..7ccda5a8 100644 --- a/src/test/java/org/onap/clamp/clds/it/RefPropItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/RefPropItCase.java @@ -23,6 +23,8 @@ package org.onap.clamp.clds.it; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -42,6 +44,7 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest @TestPropertySource(locations = "classpath:application-no-camunda.properties") public class RefPropItCase extends AbstractItCase { + protected static final EELFLogger logger = EELFManager.getInstance().getLogger(RefPropItCase.class); /** * Test getting prop value as a JSON Node / template. @@ -53,12 +56,9 @@ public class RefPropItCase extends AbstractItCase { public void testJsonTemplate() throws IOException { ObjectNode root = (ObjectNode) refProp.getJsonTemplate("tca.template"); root.put("closedLoopControlName", "ClosedLoop-FRWL-SIG-1582f840-2881-11e6-b4ec-005056a9d756"); - ObjectMapper mapper = new ObjectMapper(); String jsonText = mapper.writeValueAsString(root); - System.out.println("jsonText=" + jsonText); - + logger.error("Exception caught IllegalArgumentException as expected"); // assertEquals(topicsJson, ref.getTopicsToJson()); } - } diff --git a/src/test/java/org/onap/clamp/clds/it/SdcCatalogServicesItCase.java b/src/test/java/org/onap/clamp/clds/it/SdcCatalogServicesItCase.java index b684a506..5d3b7703 100644 --- a/src/test/java/org/onap/clamp/clds/it/SdcCatalogServicesItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/SdcCatalogServicesItCase.java @@ -33,7 +33,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.clamp.clds.AbstractItCase; -import org.onap.clamp.clds.client.SdcCatalogServices; +import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices; import org.onap.clamp.clds.model.CldsAlarmCondition; import org.onap.clamp.clds.model.CldsSdcResource; import org.onap.clamp.clds.model.CldsSdcResourceBasicInfo; diff --git a/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java b/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java index faff2846..757d85b5 100644 --- a/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/SdcReqItCase.java @@ -30,12 +30,13 @@ import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.List; +import org.apache.commons.codec.DecoderException; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; -import org.onap.clamp.clds.client.SdcCatalogServices; -import org.onap.clamp.clds.client.req.SdcReq; +import org.onap.clamp.clds.client.req.sdc.SdcCatalogServices; +import org.onap.clamp.clds.client.req.sdc.SdcReq; import org.onap.clamp.clds.model.CldsSdcResource; import org.onap.clamp.clds.model.CldsSdcServiceDetail; import org.onap.clamp.clds.model.prop.Global; @@ -55,7 +56,7 @@ public class SdcReqItCase { private SdcReq sdcReq; @Test - public void getSdcReqUrlsListNoGlobalPropTest() throws GeneralSecurityException { + public void getSdcReqUrlsListNoGlobalPropTest() throws GeneralSecurityException, DecoderException { ModelProperties prop = mock(ModelProperties.class); SdcCatalogServices sdcCatalogServices = mock(SdcCatalogServices.class); DelegateExecution delegateExecution = mock(DelegateExecution.class); diff --git a/src/test/java/org/onap/clamp/clds/it/TcaRequestFormatterItCase.java b/src/test/java/org/onap/clamp/clds/it/TcaRequestFormatterItCase.java index a32743a6..ccbcb0ea 100644 --- a/src/test/java/org/onap/clamp/clds/it/TcaRequestFormatterItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/TcaRequestFormatterItCase.java @@ -32,7 +32,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.clamp.clds.AbstractItCase; -import org.onap.clamp.clds.client.req.TcaRequestFormatter; +import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter; import org.onap.clamp.clds.model.CldsEvent; import org.onap.clamp.clds.model.prop.ModelProperties; import org.onap.clamp.clds.util.ResourceFileUtil; -- cgit 1.2.3-korg