Consolidate PolicyRestAdapter setup
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreatePolicyController.java
index af4f8cf..a2cf208 100644 (file)
@@ -22,14 +22,11 @@ package org.onap.policy.controller;
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
@@ -80,58 +77,30 @@ public class CreatePolicyController extends RestrictedBaseController {
         policyAdapter.setPolicyDescription(description);
         // Get the target data under policy.
         TargetType target = policy.getTarget();
-        if (target != null && target.getAnyOf() != null) {
+        //
+        // NOTE: target.getAnyOf() will NEVER return null
+        //
+        if (target != null) {
             // Under target we have AnyOFType
-            List<AnyOfType> anyOfList = target.getAnyOf();
-            Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
-            while (iterAnyOf.hasNext()) {
-                AnyOfType anyOf = iterAnyOf.next();
+            for (AnyOfType anyOf : target.getAnyOf()) {
                 // Under AnyOFType we have AllOFType
-                List<AllOfType> allOfList = anyOf.getAllOf();
-                if (allOfList == null) {
-                    continue;
-                }
-                Iterator<AllOfType> iterAllOf = allOfList.iterator();
+                //
+                // NOTE: anyOf.getAllOf() will NEVER return null
+                //
                 int index = 0;
-                while (iterAllOf.hasNext()) {
-                    AllOfType allOf = iterAllOf.next();
+                for (AllOfType allOf : anyOf.getAllOf()) {
                     // Under AllOFType we have Match
-                    List<MatchType> matchList = allOf.getMatch();
-                    if (matchList == null) {
-                        continue;
-                    }
-                    Iterator<MatchType> iterMatch = matchList.iterator();
-                    while (iterMatch.hasNext()) {
-                        MatchType match = iterMatch.next();
+                    // NOTE: allOf.getMatch() will NEVER be NULL
+                    //
+                    for (MatchType match : allOf.getMatch()) {
                         //
                         // Under the match we have attribute value and
                         // attributeDesignator. So,finally down to the actual attribute.
                         //
-                        AttributeValueType attributeValue = match.getAttributeValue();
-                        String value = (String) attributeValue.getContent().get(0);
-                        AttributeDesignatorType designator = match.getAttributeDesignator();
-                        String attributeId = designator.getAttributeId();
+                        String value = (String) match.getAttributeValue().getContent().get(0);
+                        String attributeId = match.getAttributeDesignator().getAttributeId();
                         // First match in the target is OnapName, so set that value.
-                        if ("ONAPName".equals(attributeId)) {
-                            policyAdapter.setOnapName(value);
-                        }
-                        if ("RiskType".equals(attributeId)) {
-                            policyAdapter.setRiskType(value);
-                        }
-                        if ("RiskLevel".equals(attributeId)) {
-                            policyAdapter.setRiskLevel(value);
-                        }
-                        if ("guard".equals(attributeId)) {
-                            policyAdapter.setGuard(value);
-                        }
-                        if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
-                            PolicyController controller = new PolicyController();
-                            String newDate = controller.convertDate(value);
-                            policyAdapter.setTtlDate(newDate);
-                        }
-                        if ("ConfigName".equals(attributeId)) {
-                            policyAdapter.setConfigName(value);
-                        }
+                        policyAdapter.setupUsingAttribute(attributeId, value);
                         // After Onap and Config it is optional to have attributes, so
                         // check weather dynamic values or there or not.
                         if (index >= 7) {
@@ -147,10 +116,10 @@ public class CreatePolicyController extends RestrictedBaseController {
             policyAdapter.setAttributes(attributeList);
         }
         List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
-        for (Object o : ruleList) {
-            if (o instanceof RuleType) {
+        for (Object object : ruleList) {
+            if (object instanceof RuleType) {
                 // get the condition data under the rule for rule Algorithms.
-                policyAdapter.setRuleID(((RuleType) o).getRuleId());
+                policyAdapter.setRuleID(((RuleType) object).getRuleId());
             }
         }
     }