Code refactoring 49/26049/2
authormuszkiet <lukasz.muszkieta@nokia.com>
Mon, 11 Dec 2017 15:15:07 +0000 (16:15 +0100)
committermuszkiet <lukasz.muszkieta@nokia.com>
Tue, 12 Dec 2017 08:22:23 +0000 (09:22 +0100)
remove duplication, methods improvements

Change-Id: Ifadb3ed931892f93ef6fb9ad128232c851828314
Issue-ID: CLAMP-93
Signed-off-by: muszkiet <lukasz.muszkieta@nokia.com>
src/main/java/org/onap/clamp/clds/client/OperationalPolicyDelegate.java
src/main/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReq.java

index e6af433..58f5e00 100644 (file)
@@ -39,14 +39,13 @@ import org.onap.clamp.clds.model.prop.PolicyChain;
 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.PolicyEngineException;
 import org.onap.policy.controlloop.policy.builder.BuilderException;
 import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * Send Operational Policy info to policy api. It uses the policy code to define
  * the model and communicate with it. See also the PolicyClient class.
- * 
+ *
  */
 public class OperationalPolicyDelegate implements JavaDelegate {
     protected static final EELFLogger logger        = EELFManager.getInstance()
@@ -70,20 +69,17 @@ public class OperationalPolicyDelegate implements JavaDelegate {
      *            The DelegateExecution
      * @throws BuilderException
      *             In case of issues with OperationalPolicyReq
-     * @throws PolicyEngineException
-     *             In case of issues with the PolicyEngine creation
      * @throws UnsupportedEncodingException
      */
     @Override
     public void execute(DelegateExecution execution)
-            throws BuilderException, PolicyEngineException, UnsupportedEncodingException {
+            throws BuilderException, UnsupportedEncodingException {
         String responseMessage = null;
-        String operationalPolicyRequestUuid = null;
         ModelProperties prop = ModelProperties.create(execution);
         Policy policy = prop.getType(Policy.class);
         if (policy.isFound()) {
             for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) {
-                operationalPolicyRequestUuid = LoggingUtils.getRequestId();
+                String operationalPolicyRequestUuid = LoggingUtils.getRequestId();
                 Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
                         prop, prop.getType(Policy.class).getId(), policyChain);
                 responseMessage = policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid);
index fcf6cda..db84a12 100644 (file)
@@ -107,20 +107,21 @@ public class OperationalPolicyReq {
             notificationTopic = refProp.getStringValue("op.notificationTopic", global.getService());
             controller = refProp.getStringValue("op.controller", global.getService());
         }
+
         String recipeTopic = refProp.getStringValue("op.recipeTopic", global.getService());
         // ruleAttributes
+        logger.info("templateName=" + templateName);
+        logger.info("notificationTopic=" + notificationTopic);
         Map<String, String> ruleAttributes = new HashMap<>();
+        ruleAttributes.put("templateName", templateName);
+        ruleAttributes.put("ClosedLoopControlName", prop.getControlNameAndPolicyUniqueId());
+        ruleAttributes.put("NotificationTopic", notificationTopic);
         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());
@@ -134,16 +135,11 @@ public class OperationalPolicyReq {
             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
@@ -205,22 +201,9 @@ public class OperationalPolicyReq {
             }
             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());
-        }
+        validate(results);
         return URLEncoder.encode(results.getSpecification(), "UTF-8");
     }
 
@@ -284,10 +267,13 @@ public class OperationalPolicyReq {
         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();
+        validate(results);
+        return URLEncoder.encode(results.getSpecification(), "UTF-8");
+    }
+
+    private static void validate (Results results) {
         if (results.isValid()) {
             logger.info("results.getSpecification()=" + results.getSpecification());
         } else {
@@ -300,7 +286,6 @@ public class OperationalPolicyReq {
             }
             throw new BadRequestException(sb.toString());
         }
-        return URLEncoder.encode(results.getSpecification(), "UTF-8");
     }
 
     /**