Remove ECOMP in headers
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / policy / PolicyClient.java
index e01a68f..7908896 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP CLAMP
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -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;
@@ -37,9 +36,10 @@ 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.prop.ModelProperties;
-import org.onap.clamp.clds.model.refprop.RefProp;
+import org.onap.clamp.clds.model.properties.ModelProperties;
 import org.onap.clamp.clds.util.LoggingUtils;
 import org.onap.policy.api.AttributeType;
 import org.onap.policy.api.ConfigRequestParameters;
@@ -55,28 +55,30 @@ 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.stereotype.Component;
 
 /**
  * Policy utility methods - specifically, send the policy.
  */
+@Component
 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;
+
+    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();
+    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 ApplicationContext      appContext;
+    protected ClampProperties refProp;
     @Autowired
-    protected RefProp                 refProp;
+    private PolicyConfiguration policyConfiguration;
 
     /**
      * Perform BRMS policy type.
@@ -88,7 +90,6 @@ public class PolicyClient {
      * @param policyRequestUuid
      *            PolicyRequest UUID
      * @return The response message of policy
-     * 
      */
     public String sendBrmsPolicy(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
             String policyRequestUuid) {
@@ -104,9 +105,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;
     }
@@ -126,12 +127,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("");
@@ -160,7 +161,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);
@@ -170,7 +171,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;
     }
@@ -188,11 +189,10 @@ public class PolicyClient {
         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.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("");
@@ -234,15 +234,19 @@ public class PolicyClient {
                 responseMessage = response.getResponseMessage();
             }
         } catch (Exception e) {
+            LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
+            LoggingUtils.setErrorContext("900", "Policy send error");
             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) {
+            LoggingUtils.setResponseContext("0", "Policy send success", this.getClass().getName());
             logger.info("Policy send successful");
             metricsLogger.info("Policy send success");
         } else {
+            LoggingUtils.setResponseContext("900", "Policy send failed", this.getClass().getName());
             logger.warn("Policy send failed: " + responseMessage);
             metricsLogger.info("Policy send failure");
             throw new BadRequestException("Policy send failed: " + responseMessage);
@@ -279,20 +283,27 @@ public class PolicyClient {
         PolicyChangeResponse response;
         String responseMessage = "";
         try {
+            LoggingUtils.setTargetContext("Policy", "pushPolicy");
             logger.info("Attempting to push policy...");
             response = getPolicyEngine().pushPolicy(pushPolicyParameters);
             if (response != null) {
                 responseMessage = response.getResponseMessage();
             }
         } catch (Exception e) {
+            LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
+            LoggingUtils.setErrorContext("900", "Policy push error");
             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)) {
+            LoggingUtils.setResponseContext("0", "Policy push success", this.getClass().getName());
             logger.info("Policy push successful");
+            metricsLogger.info("Policy push success");
         } else {
+            LoggingUtils.setResponseContext("900", "Policy push failed", this.getClass().getName());
             logger.warn("Policy push failed: " + responseMessage);
+            metricsLogger.info("Policy push failure");
             throw new BadRequestException("Policy push failed: " + responseMessage);
         }
         return responseMessage;
@@ -331,7 +342,7 @@ public class PolicyClient {
             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());
+            logger.warn("policy not found...policy name - " + policyName, e);
         }
         return versions;
     }
@@ -344,9 +355,7 @@ public class PolicyClient {
     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);
+            policyEngine = new PolicyEngine(policyConfiguration.getProperties());
         } catch (PolicyEngineException e) {
             throw new PolicyClientException("Exception when creating a new policy engine", e);
         }
@@ -384,7 +393,7 @@ public class PolicyClient {
      * @return The response message from policy
      */
     public String deleteBrms(ModelProperties prop) {
-        String policyType = refProp.getStringValue("policy.op.type");
+        String policyType = refProp.getStringValue(POLICY_OP_TYPE_PROPERTY_NAME);
         return deletePolicy(prop, policyType);
     }
 
@@ -393,7 +402,6 @@ public class PolicyClient {
      *
      * @param prop
      *            The ModelProperties
-     *
      * @return The response message from policy
      */
     protected String deletePolicy(ModelProperties prop, String policyType) {