X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fclamp%2Fclds%2Fclient%2Freq%2Fpolicy%2FPolicyClient.java;h=cc97a7ce0a197aade39c1edccdb10dc13cf84dba;hb=7a58af870eb9934dfec4b5353672d7c428208116;hp=144a48c92c0a072b46f960a0c746b4ae78060c0d;hpb=02490407e31862b68832185cc7e299f6d7dece96;p=clamp.git 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 index 144a48c9..cc97a7ce 100644 --- 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 @@ -18,7 +18,7 @@ * 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; @@ -26,7 +26,6 @@ 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; @@ -38,6 +37,7 @@ import java.util.UUID; import javax.ws.rs.BadRequestException; import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.clds.config.PolicyConfiguration; import org.onap.clamp.clds.exception.policy.PolicyClientException; import org.onap.clamp.clds.model.properties.ModelProperties; import org.onap.clamp.clds.util.LoggingUtils; @@ -55,28 +55,33 @@ 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; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; /** * Policy utility methods - specifically, send the policy. */ @Component +@Primary public class PolicyClient { - protected static final String POLICY_PREFIX_BASE = "Config_"; + protected PolicyEngine policyEngine; 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("${clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}") - protected String cldsPolicyConfigFile; + public static final String POLICY_MSTYPE_PROPERTY_NAME = "policy.ms.type"; + public static final String POLICY_ONAPNAME_PROPERTY_NAME = "policy.onap.name"; + public static final String POLICY_BASENAME_PREFIX_PROPERTY_NAME = "policy.base.policyNamePrefix"; + public static final String POLICY_OP_NAME_PREFIX_PROPERTY_NAME = "policy.op.policyNamePrefix"; + public static final String POLICY_MS_NAME_PREFIX_PROPERTY_NAME = "policy.ms.policyNamePrefix"; + public static final String POLICY_OP_TYPE_PROPERTY_NAME = "policy.op.type"; @Autowired protected ApplicationContext appContext; @Autowired protected ClampProperties refProp; + @Autowired + private PolicyConfiguration policyConfiguration; /** * Perform BRMS policy type. @@ -103,9 +108,9 @@ public class PolicyClient { policyParameters.setAttributes(attributes); // Set a random UUID(Mandatory) policyParameters.setRequestID(UUID.fromString(policyRequestUuid)); - String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix"); + String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME); String rtnMsg = send(policyParameters, prop, policyNamePrefix); - String policyType = refProp.getStringValue("policy.op.type"); + String policyType = refProp.getStringValue(POLICY_OP_TYPE_PROPERTY_NAME); push(policyType, prop); return rtnMsg; } @@ -125,12 +130,12 @@ public class PolicyClient { PolicyParameters policyParameters = new PolicyParameters(); // Set Policy Type policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); - policyParameters.setEcompName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setOnapName(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"); + String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME); // Adding this line to clear the policy id from policy name while // pushing to policy engine prop.setPolicyUniqueId(""); @@ -159,7 +164,7 @@ public class PolicyClient { PolicyParameters policyParameters = new PolicyParameters(); // Set Policy Type policyParameters.setPolicyConfigType(PolicyConfigType.Base); - policyParameters.setEcompName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); policyParameters.setConfigBody(configBody); policyParameters.setConfigBodyType(PolicyType.OTHER); @@ -169,7 +174,7 @@ public class PolicyClient { // 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); + String rtnMsg = send(policyParameters, prop, refProp.getStringValue(POLICY_BASENAME_PREFIX_PROPERTY_NAME)); push(PolicyConfigType.Base.toString(), prop); return rtnMsg; } @@ -187,10 +192,10 @@ public class PolicyClient { PolicyParameters policyParameters = new PolicyParameters(); // Set Policy Type policyParameters.setPolicyConfigType(PolicyConfigType.MicroService); - policyParameters.setEcompName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); + policyParameters.setOnapName(refProp.getStringValue(POLICY_ONAPNAME_PROPERTY_NAME)); policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName()); policyParameters.setConfigBody(configBody); - String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix"); + String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME); // Adding this line to clear the policy id from policy name while // pushing to policy engine prop.setPolicyUniqueId(""); @@ -328,13 +333,17 @@ public class PolicyClient { } else { policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix); } - logger.info("policyName=" + policyName); + logger.info("Search in Policy Engine for policyName=" + policyName); configRequestParameters.setPolicyName(policyName); try { Collection response = getPolicyEngine().getConfig(configRequestParameters); for (PolicyConfig policyConfig : response) { - Integer version = Integer.valueOf(policyConfig.getPolicyVersion()); - versions.add(version); + if (policyConfig.getPolicyVersion() != null) { + Integer version = Integer.valueOf(policyConfig.getPolicyVersion()); + versions.add(version); + } else { + logger.warn("Policy version was null, unable to convert it to Integer"); + } } Collections.sort(versions); logger.info("Policy versions.size()=" + versions.size()); @@ -350,12 +359,11 @@ public class PolicyClient { * * @return A new policy engine */ - private PolicyEngine getPolicyEngine() { - PolicyEngine policyEngine; + private synchronized PolicyEngine getPolicyEngine() { try { - policyEngine = new PolicyEngine(appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath()); - } catch (IOException e1) { - throw new PolicyClientException("Exception when opening policy config file", e1); + if (policyEngine == null) { + policyEngine = new PolicyEngine(policyConfiguration.getProperties()); + } } catch (PolicyEngineException e) { throw new PolicyClientException("Exception when creating a new policy engine", e); } @@ -370,8 +378,19 @@ public class PolicyClient { * @return The response message from Policy */ public String deleteMicrosService(ModelProperties prop) { - String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME); - return deletePolicy(prop, policyType); + String deletePolicyResponse = ""; + try { + String policyNamePrefix = refProp.getStringValue(POLICY_MS_NAME_PREFIX_PROPERTY_NAME); + List versions = getVersions(policyNamePrefix, prop); + if (!versions.isEmpty()) { + String policyType = refProp.getStringValue(POLICY_MSTYPE_PROPERTY_NAME); + deletePolicyResponse = deletePolicy(prop, policyType); + } + } catch (Exception e) { + logger.error("Exception occurred during policy communication", e); + throw new PolicyClientException("Exception while communicating with Policy", e); + } + return deletePolicyResponse; } /** @@ -393,8 +412,19 @@ public class PolicyClient { * @return The response message from policy */ public String deleteBrms(ModelProperties prop) { - String policyType = refProp.getStringValue("policy.op.type"); - return deletePolicy(prop, policyType); + String deletePolicyResponse = ""; + try { + String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME); + List versions = getVersions(policyNamePrefix, prop); + if (!versions.isEmpty()) { + String policyType = refProp.getStringValue(POLICY_OP_TYPE_PROPERTY_NAME); + deletePolicyResponse = deletePolicy(prop, policyType); + } + } catch (Exception e) { + logger.error("Exception occurred during policy communication", e); + throw new PolicyClientException("Exception while communicating with Policy", e); + } + return deletePolicyResponse; } /**