Minor Improvement
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / policy / PolicyClient.java
index 9c72d2e..cc97a7c 100644 (file)
@@ -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;
@@ -56,14 +56,17 @@ import org.onap.policy.api.PolicyType;
 import org.onap.policy.api.PushPolicyParameters;
 import org.springframework.beans.factory.annotation.Autowired;
 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 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();
@@ -330,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<PolicyConfig> 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());
@@ -352,10 +359,11 @@ public class PolicyClient {
      * 
      * @return A new policy engine
      */
-    private PolicyEngine getPolicyEngine() {
-        PolicyEngine policyEngine;
+    private synchronized PolicyEngine getPolicyEngine() {
         try {
-            policyEngine = new PolicyEngine(policyConfiguration.getProperties());
+            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<Integer> 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_PROPERTY_NAME);
-        return deletePolicy(prop, policyType);
+        String deletePolicyResponse = "";
+        try {
+            String policyNamePrefix = refProp.getStringValue(POLICY_OP_NAME_PREFIX_PROPERTY_NAME);
+            List<Integer> 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;
     }
 
     /**