Fix potential NPE
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / policy / PolicyClient.java
index 5c68f00..1e423ff 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;
@@ -64,12 +64,16 @@ import org.springframework.stereotype.Component;
 @Component
 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";
+    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
@@ -102,9 +106,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;
     }
@@ -129,7 +133,7 @@ public class PolicyClient {
         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("");
@@ -168,7 +172,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;
     }
@@ -189,7 +193,7 @@ public class PolicyClient {
         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("");
@@ -327,13 +331,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());
@@ -349,10 +357,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);
         }
@@ -390,7 +399,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);
     }