More sonar cleanup and line consolidation
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / DecisionPolicyController.java
index fdbee2a..d12cbb2 100644 (file)
@@ -2,14 +2,14 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Engine
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,19 +30,9 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+
 import javax.xml.bind.JAXBElement;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.onap.policy.common.logging.flexlogger.FlexLogger;
-import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.rest.adapter.PolicyRestAdapter;
-import org.onap.policy.rest.adapter.RainyDayParams;
-import org.onap.policy.rest.adapter.YAMLParams;
-import org.onap.policy.rest.jpa.PolicyEntity;
-import org.onap.policy.xacml.util.XACMLPolicyWriter;
-import org.onap.portalsdk.core.controller.RestrictedBaseController;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
+
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
@@ -59,52 +49,79 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.adapter.RainyDayParams;
+import org.onap.policy.rest.adapter.YAMLParams;
+import org.onap.policy.rest.jpa.PolicyEntity;
+import org.onap.policy.xacml.util.XACMLPolicyWriter;
+import org.onap.portalsdk.core.controller.RestrictedBaseController;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+
 @Controller
 @RequestMapping("/")
 public class DecisionPolicyController extends RestrictedBaseController {
     private static final Logger policyLogger = FlexLogger.getLogger(DecisionPolicyController.class);
 
-    public DecisionPolicyController() {
-        // This constructor is empty
-    }
+    public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
+    private static final String BLENTRY = "@blEntry@";
+    private static final String DECISIONRAWTYPE = "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
+    private static final String GUARD_YAML = "GUARD_YAML";
+    private static final String GUARD_BL_YAML = "GUARD_BL_YAML";
+    private static final String GUARD_MIN_MAX = "GUARD_MIN_MAX";
 
     protected PolicyRestAdapter policyAdapter = null;
-    private ArrayList<Object> attributeList;
-    private ArrayList<Object> decisionList;
     private ArrayList<Object> ruleAlgorithmList;
     private ArrayList<Object> treatmentList = null;
     protected LinkedList<Integer> ruleAlgoirthmTracker;
-    public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
-    private static final String blEntry = "@blEntry@";
-    private static final String decisionRawType = "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
 
-    public void rawXACMLPolicy(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
+    public DecisionPolicyController() {
+        // This constructor is empty
+    }
+
+    /**
+     * rawXacmlPolicy. Should this method be private?
+     *
+     * @param policyAdapter PolicyRestAdapter
+     * @param entity PolicyEntity
+     */
+    public void rawXacmlPolicy(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
         try (InputStream policyXmlStream = XACMLPolicyWriter.getXmlAsInputStream(policyAdapter.getPolicyData())) {
             String name = StringUtils.substringAfter(entity.getPolicyName(), "Decision_");
             policyAdapter.setPolicyName(name.substring(0, name.indexOf('.')));
             policyAdapter.setRuleProvider("Raw");
-            policyAdapter.setRawXacmlPolicy(IOUtils.toString(policyXmlStream).replaceAll(decisionRawType, ""));
+            policyAdapter.setRawXacmlPolicy(IOUtils.toString(policyXmlStream).replaceAll(DECISIONRAWTYPE, ""));
         } catch (IOException e) {
             policyLogger.error("Exception Occured while setting XACML Raw Object" + e);
         }
     }
 
+    /**
+     * prePopulateDecisionPolicyData.
+     *
+     * @param policyAdapter PolicyRestAdapter
+     * @param entity PolicyEntity
+     */
     @SuppressWarnings("unchecked")
     public void prePopulateDecisionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
-        attributeList = new ArrayList<>();
-        decisionList = new ArrayList<>();
+        List<Object> attributeList = new ArrayList<>();
+        List<Object> decisionList = new ArrayList<>();
         ruleAlgorithmList = new ArrayList<>();
         treatmentList = new ArrayList<>();
 
         boolean rawPolicyCheck = false;
         if (policyAdapter.getPolicyData() instanceof PolicySetType) {
-            rawPolicyCheck = ((PolicySetType) policyAdapter.getPolicyData()).getDescription().contains(decisionRawType);
+            rawPolicyCheck = ((PolicySetType) policyAdapter.getPolicyData()).getDescription().contains(DECISIONRAWTYPE);
         } else {
-            rawPolicyCheck = ((PolicyType) policyAdapter.getPolicyData()).getDescription().contains(decisionRawType);
+            rawPolicyCheck = ((PolicyType) policyAdapter.getPolicyData()).getDescription().contains(DECISIONRAWTYPE);
         }
 
         if (rawPolicyCheck) {
-            rawXACMLPolicy(policyAdapter, entity);
+            rawXacmlPolicy(policyAdapter, entity);
         } else {
             RainyDayParams rainydayParams = new RainyDayParams();
             Object policyData = policyAdapter.getPolicyData();
@@ -115,9 +132,9 @@ public class DecisionPolicyController extends RestrictedBaseController {
             String description = "";
             String blackListEntryType = "Use Manual Entry";
             try {
-                if (policy.getDescription().contains(blEntry)) {
-                    blackListEntryType = policy.getDescription().substring(policy.getDescription().indexOf(blEntry) + 9,
-                            policy.getDescription().lastIndexOf(blEntry));
+                if (policy.getDescription().contains(BLENTRY)) {
+                    blackListEntryType = policy.getDescription().substring(policy.getDescription().indexOf(BLENTRY) + 9,
+                            policy.getDescription().lastIndexOf(BLENTRY));
                 }
                 policyAdapter.setBlackListEntryType(blackListEntryType);
                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
@@ -138,45 +155,46 @@ public class DecisionPolicyController extends RestrictedBaseController {
                         AnyOfType anyOf = iterAnyOf.next();
                         // Under AntOfType we have AllOfType
                         List<AllOfType> allOfList = anyOf.getAllOf();
-                        if (allOfList != null) {
-                            Iterator<AllOfType> iterAllOf = allOfList.iterator();
-                            while (iterAllOf.hasNext()) {
-                                AllOfType allOf = iterAllOf.next();
-                                // Under AllOfType we have Mathch.
-                                List<MatchType> matchList = allOf.getMatch();
-                                int index = 0;
-                                if (matchList != null) {
-                                    Iterator<MatchType> iterMatch = matchList.iterator();
-                                    while (iterMatch.hasNext()) {
-                                        MatchType match = iterMatch.next();
-                                        //
-                                        // Under the match we have attributevalue and
-                                        // attributeDesignator. So,finally down to the actual attribute.
-                                        //
-                                        AttributeValueType attributeValue = match.getAttributeValue();
-                                        String value = (String) attributeValue.getContent().get(0);
-                                        if (value != null) {
-                                            value = value.replaceAll("\\(\\?i\\)", "");
-                                        }
-                                        AttributeDesignatorType designator = match.getAttributeDesignator();
-                                        String attributeId = designator.getAttributeId();
-                                        // First match in the target is OnapName, so set that value.
-                                        if ("ONAPName".equals(attributeId)) {
-                                            policyAdapter.setOnapName(value);
-                                        }
-                                        // Component attributes are saved under Target here we are fetching them back.
-                                        // One row is default so we are not adding dynamic component at index 0.
-                                        if (index >= 1) {
-                                            Map<String, String> attribute = new HashMap<>();
-                                            attribute.put("key", attributeId);
-                                            attribute.put("value", value);
-                                            attributeList.add(attribute);
-                                        }
-                                        index++;
+                        if (allOfList == null) {
+                            continue;
+                        }
+                        Iterator<AllOfType> iterAllOf = allOfList.iterator();
+                        while (iterAllOf.hasNext()) {
+                            AllOfType allOf = iterAllOf.next();
+                            // Under AllOfType we have Mathch.
+                            List<MatchType> matchList = allOf.getMatch();
+                            int index = 0;
+                            if (matchList != null) {
+                                Iterator<MatchType> iterMatch = matchList.iterator();
+                                while (iterMatch.hasNext()) {
+                                    MatchType match = iterMatch.next();
+                                    //
+                                    // Under the match we have attributevalue and
+                                    // attributeDesignator. So,finally down to the actual attribute.
+                                    //
+                                    AttributeValueType attributeValue = match.getAttributeValue();
+                                    String value = (String) attributeValue.getContent().get(0);
+                                    if (value != null) {
+                                        value = value.replaceAll("\\(\\?i\\)", "");
+                                    }
+                                    AttributeDesignatorType designator = match.getAttributeDesignator();
+                                    String attributeId = designator.getAttributeId();
+                                    // First match in the target is OnapName, so set that value.
+                                    if ("ONAPName".equals(attributeId)) {
+                                        policyAdapter.setOnapName(value);
+                                    }
+                                    // Component attributes are saved under Target here we are fetching them back.
+                                    // One row is default so we are not adding dynamic component at index 0.
+                                    if (index >= 1) {
+                                        Map<String, String> attribute = new HashMap<>();
+                                        attribute.put("key", attributeId);
+                                        attribute.put("value", value);
+                                        attributeList.add(attribute);
                                     }
+                                    index++;
                                 }
-                                policyAdapter.setAttributes(attributeList);
                             }
+                            policyAdapter.setAttributes(attributeList);
                         }
                     }
                     // Setting rainy day attributes to the parameters object if they exist
@@ -226,12 +244,15 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                         .getAdviceExpression().get(0).getAdviceId())) {
                                     policyAdapter.setRuleProvider("AAF");
                                     break;
-                                } else if ("GUARD_YAML".equalsIgnoreCase(((RuleType) object).getAdviceExpressions()
+                                } else if (GUARD_YAML.equalsIgnoreCase(((RuleType) object).getAdviceExpressions()
                                         .getAdviceExpression().get(0).getAdviceId())) {
-                                    policyAdapter.setRuleProvider("GUARD_YAML");
-                                } else if ("GUARD_BL_YAML".equalsIgnoreCase(((RuleType) object).getAdviceExpressions()
+                                    policyAdapter.setRuleProvider(GUARD_YAML);
+                                } else if (GUARD_BL_YAML.equalsIgnoreCase(((RuleType) object).getAdviceExpressions()
                                         .getAdviceExpression().get(0).getAdviceId())) {
-                                    policyAdapter.setRuleProvider("GUARD_BL_YAML");
+                                    policyAdapter.setRuleProvider(GUARD_BL_YAML);
+                                } else if (GUARD_MIN_MAX.equalsIgnoreCase(((RuleType) object).getAdviceExpressions()
+                                        .getAdviceExpression().get(0).getAdviceId())) {
+                                    policyAdapter.setRuleProvider(GUARD_MIN_MAX);
                                 }
                             } else {
                                 policyAdapter.setRuleProvider("Custom");
@@ -242,8 +263,9 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                 decisionApply = (ApplyType) decisionApply.getExpression().get(0).getValue();
                                 ruleAlgoirthmTracker = new LinkedList<>();
                                 if (policyAdapter.getRuleProvider() != null
-                                        && ("GUARD_YAML".equals(policyAdapter.getRuleProvider())
-                                                || ("GUARD_BL_YAML".equals(policyAdapter.getRuleProvider())))) {
+                                        && (GUARD_YAML.equals(policyAdapter.getRuleProvider())
+                                                || (GUARD_BL_YAML.equals(policyAdapter.getRuleProvider()))
+                                                || (GUARD_MIN_MAX.equals(policyAdapter.getRuleProvider())))) {
                                     YAMLParams yamlParams = new YAMLParams();
                                     for (int i = 0; i < attributeList.size(); i++) {
                                         Map<String, String> map = (Map<String, String>) attributeList.get(i);
@@ -255,6 +277,10 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                             yamlParams.setTargets(Arrays.asList(map.get("value").split("\\|")));
                                         } else if ("clname".equals(map.get("key"))) {
                                             yamlParams.setClname(map.get("value"));
+                                        } else if ("min".equals(map.get("key"))) {
+                                            yamlParams.setMin(map.get("value"));
+                                        } else if ("max".equals(map.get("key"))) {
+                                            yamlParams.setMax(map.get("value"));
                                         }
                                     }
                                     ApplyType apply =
@@ -266,7 +292,7 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                     yamlParams.setGuardActiveEnd(
                                             ((AttributeValueType) apply.getExpression().get(2).getValue()).getContent()
                                                     .get(0).toString());
-                                    if ("GUARD_BL_YAML".equals(policyAdapter.getRuleProvider())) {
+                                    if (GUARD_BL_YAML.equals(policyAdapter.getRuleProvider())) {
                                         apply = (ApplyType) ((ApplyType) ((ApplyType) decisionApply.getExpression()
                                                 .get(0).getValue()).getExpression().get(1).getValue()).getExpression()
                                                         .get(2).getValue();
@@ -367,7 +393,6 @@ public class DecisionPolicyController extends RestrictedBaseController {
             VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();
             ruleMap.put("dynamicRuleAlgorithmField1", "S_" + variableReference.getVariableId());
 
-
             // Get from Attribute Value
             AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
             String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);