[Policy-52, Policy-92, Policy-93] Policy Enhancements and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / DecisionPolicyController.java
index 57e8821..f959495 100644 (file)
@@ -21,6 +21,7 @@
 package org.openecomp.policy.controller;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -32,12 +33,14 @@ import javax.xml.bind.JAXBElement;
 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
 import org.openecomp.policy.common.logging.flexlogger.Logger;
 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
+import org.openecomp.policy.rest.adapter.RainyDayParams;
 import org.openecomp.policy.rest.adapter.YAMLParams;
 import org.openecomp.policy.rest.jpa.PolicyEntity;
 import org.openecomp.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;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
@@ -55,7 +58,7 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
 @Controller
 @RequestMapping("/")
 public class DecisionPolicyController extends RestrictedBaseController {
-       private static final Logger logger = FlexLogger.getLogger(DecisionPolicyController.class);
+       private static final Logger policyLogger = FlexLogger.getLogger(DecisionPolicyController.class);
        
        public DecisionPolicyController(){}
 
@@ -63,15 +66,19 @@ public class DecisionPolicyController extends RestrictedBaseController {
        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";
 
        @SuppressWarnings("unchecked")
        public void prePopulateDecisionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
-               attributeList = new ArrayList<Object>();
-               decisionList = new ArrayList<Object>();
-               ruleAlgorithmList = new ArrayList<Object>();
+               attributeList = new ArrayList<>();
+               decisionList = new ArrayList<>();
+               ruleAlgorithmList = new ArrayList<>();
+               treatmentList = new ArrayList<>();
+               
                if (policyAdapter.getPolicyData() instanceof PolicyType) {
+                       RainyDayParams rainydayParams = new RainyDayParams();
                        Object policyData = policyAdapter.getPolicyData();
                        PolicyType policy = (PolicyType) policyData;
                        policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
@@ -81,6 +88,7 @@ public class DecisionPolicyController extends RestrictedBaseController {
                        try{
                                description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
                        }catch(Exception e){
+                               policyLogger.info("General error", e);
                                description = policy.getDescription();
                        }
                        policyAdapter.setPolicyDescription(description);
@@ -115,13 +123,13 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                                                                AttributeDesignatorType designator = match.getAttributeDesignator();
                                                                                String attributeId = designator.getAttributeId();
                                                                                // First match in the target is EcompName, so set that value.
-                                                                               if (index == 0) {
+                                                                               if (attributeId.equals("ECOMPName")) {
                                                                                        policyAdapter.setEcompName(value);
                                                                                }
                                                                                // Component attributes are saved under Target here we are fetching  them back.
-                                                                               // One row is default so we are not adding dynamic componet at index 0.
+                                                                               // One row is default so we are not adding dynamic component at index 0.
                                                                                if (index >= 1) {       
-                                                                                       Map<String, String> attribute = new HashMap<String, String>();
+                                                                                       Map<String, String> attribute = new HashMap<>();
                                                                                        attribute.put("key", attributeId);
                                                                                        attribute.put("value", value);
                                                                                        attributeList.add(attribute);   
@@ -133,6 +141,29 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                                        }
                                                }
                                        }
+                                       // Setting rainy day attributes to the parameters object if they exist 
+                                       boolean rainy = false;
+                                       if(!attributeList.isEmpty()) {
+                                               for(int i=0; i<attributeList.size() ; i++){
+                                                       Map<String, String> map = (Map<String,String>)attributeList.get(i);
+                                                       if(map.get("key").equals("WorkStep")){
+                                                               rainydayParams.setWorkstep(map.get("value"));
+                                                               rainy=true;
+                                                       }else if(map.get("key").equals("BB_ID")){
+                                                               rainydayParams.setBbid(map.get("value"));
+                                                               rainy=true;
+                                                       }else if(map.get("key").equals("ServiceType")){
+                                                               rainydayParams.setServiceType(map.get("value"));
+                                                               rainy=true;
+                                                       }else if(map.get("key").equals("VNFType")){
+                                                               rainydayParams.setVnfType(map.get("value"));
+                                                               rainy=true;
+                                                       }
+                                               }       
+                                       }
+                                       if(rainy){
+                                               policyAdapter.setRuleProvider("Rainy_Day");
+                                       }
                                }
 
                                List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
@@ -140,7 +171,7 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                for (Object object : ruleList) {
                                        if (object instanceof VariableDefinitionType) {
                                                VariableDefinitionType variableDefinitionType = (VariableDefinitionType) object;
-                                               Map<String, String> settings = new HashMap<String, String>();
+                                               Map<String, String> settings = new HashMap<>();
                                                settings.put("key", variableDefinitionType.getVariableId());
                                                JAXBElement<AttributeValueType> attributeValueTypeElement = (JAXBElement<AttributeValueType>) variableDefinitionType.getExpression();
                                                if (attributeValueTypeElement != null) {
@@ -157,6 +188,8 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                                                        break;
                                                                }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_YAML")){
                                                                        policyAdapter.setRuleProvider("GUARD_YAML");
+                                                               }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_BL_YAML")){
+                                                                       policyAdapter.setRuleProvider("GUARD_BL_YAML");
                                                                }
                                                        }else{
                                                                policyAdapter.setRuleProvider("Custom");
@@ -165,8 +198,8 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                                        if (condition != null) {
                                                                ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
                                                                decisionApply = (ApplyType) decisionApply.getExpression().get(0).getValue();
-                                                               ruleAlgoirthmTracker = new LinkedList<Integer>();
-                                                               if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals("GUARD_YAML")){
+                                                               ruleAlgoirthmTracker = new LinkedList<>();
+                                                               if(policyAdapter.getRuleProvider()!=null && (policyAdapter.getRuleProvider().equals("GUARD_YAML")||(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")))){
                                                                        YAMLParams yamlParams = new YAMLParams();
                                                                        for(int i=0; i<attributeList.size() ; i++){
                                                                                Map<String, String> map = (Map<String,String>)attributeList.get(i);
@@ -174,14 +207,30 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                                                                        yamlParams.setActor(map.get("value"));
                                                                                }else if(map.get("key").equals("recipe")){
                                                                                        yamlParams.setRecipe(map.get("value"));
+                                                                               }else if(map.get("key").equals("targets")){
+                                                                                       yamlParams.setTargets(Arrays.asList(map.get("value").split("\\|")));
+                                                                               }else if(map.get("key").equals("clname")){
+                                                                                       yamlParams.setClname(map.get("value"));
                                                                                }
                                                                        }
                                                                        ApplyType apply = ((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(0).getValue());
                                                                        yamlParams.setGuardActiveStart(((AttributeValueType)apply.getExpression().get(1).getValue()).getContent().get(0).toString());
                                                                        yamlParams.setGuardActiveEnd(((AttributeValueType)apply.getExpression().get(2).getValue()).getContent().get(0).toString());
-                                                                       yamlParams.setLimit(((AttributeValueType)((ApplyType)decisionApply.getExpression().get(1).getValue()).getExpression().get(1).getValue()).getContent().get(0).toString());
-                                                                       String timeWindow = ((AttributeDesignatorType)((ApplyType)((ApplyType)decisionApply.getExpression().get(1).getValue()).getExpression().get(0).getValue()).getExpression().get(0).getValue()).getIssuer();
-                                                                       yamlParams.setTimeWindow(timeWindow.substring(timeWindow.lastIndexOf(":")+1));
+                                                                       if(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")){
+                                                                               apply = (ApplyType)((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue()).getExpression().get(2).getValue();
+                                                                               Iterator<JAXBElement<?>> attributes = apply.getExpression().iterator();
+                                                                               List<String> blackList = new ArrayList<>();
+                                                                               while(attributes.hasNext()){
+                                                                                       blackList.add(((AttributeValueType)attributes.next().getValue()).getContent().get(0).toString());
+                                                                               }
+                                                                               yamlParams.setBlackList(blackList);
+                                                                       }else{
+                                                                               ApplyType timeWindowSection = (ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue();
+                                                                               yamlParams.setLimit(((AttributeValueType)timeWindowSection.getExpression().get(1).getValue()).getContent().get(0).toString());
+                                                                               String timeWindow = ((AttributeDesignatorType)((ApplyType)timeWindowSection.getExpression().get(0).getValue()).getExpression().get(0).getValue()).getIssuer();
+                                                                               yamlParams.setTimeUnits(timeWindow.substring(timeWindow.lastIndexOf(':')+1));
+                                                                               yamlParams.setTimeWindow(timeWindow.substring(timeWindow.indexOf(":tw:")+4,timeWindow.lastIndexOf(':')));
+                                                                       }
                                                                        policyAdapter.setYamlparams(yamlParams);
                                                                        policyAdapter.setAttributes(new ArrayList<Object>());
                                                                        policyAdapter.setRuleAlgorithmschoices(new ArrayList<Object>());
@@ -191,17 +240,42 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                                                prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
                                                                policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
                                                        }
+                                               } else if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals("Rainy_Day")&& ((RuleType) object).getEffect().equals(EffectType.PERMIT)) {
+                                                       
+                                                       TargetType ruleTarget = ((RuleType) object).getTarget();
+                                                       AdviceExpressionsType adviceExpression = ((RuleType) object).getAdviceExpressions();
+                                                       
+                                                       String errorcode = ruleTarget.getAnyOf().get(0).getAllOf().get(0).getMatch().
+                                                                       get(1).getAttributeValue().getContent().get(0).toString();
+                                                       JAXBElement<AttributeValueType> tempTreatmentObj = (JAXBElement<AttributeValueType>) adviceExpression.getAdviceExpression().
+                                                                       get(0).getAttributeAssignmentExpression().get(0).getExpression();
+                                                       String treatment = tempTreatmentObj.getValue().getContent().get(0).toString();
+                                                       
+                                                       prePopulateRainyDayTreatments(errorcode, treatment);                                            
+
                                                }
                                        }
                                }
                        }
+                       
+                       rainydayParams.setTreatmentTableChoices(treatmentList);
+                       policyAdapter.setRainyday(rainydayParams);
                        policyAdapter.setSettings(decisionList);        
                }       
 
        }
 
+       private void prePopulateRainyDayTreatments(String errorcode, String treatment) {
+               Map<String, String> ruleMap = new HashMap<>();
+               
+               ruleMap.put("errorcode", errorcode);
+               ruleMap.put("treatment", treatment);
+               treatmentList.add(ruleMap);
+               
+       }
+       
        private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
-               Map<String, String> ruleMap = new HashMap<String, String>();
+               Map<String, String> ruleMap = new HashMap<>();
                ruleMap.put("id", "A" + (index +1));
                Map<String, String> dropDownMap = PolicyController.getDropDownMap();
                for (String key : dropDownMap.keySet()) {
@@ -241,8 +315,8 @@ public class DecisionPolicyController extends RestrictedBaseController {
                List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
                for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
                        // If There is Attribute Value under Decision Type that means we came to the final child
-                       if (logger.isDebugEnabled()) {
-                               logger.debug("Prepopulating rule algoirthm: " + index);
+                       if (policyLogger.isDebugEnabled()) {
+                               policyLogger.debug("Prepopulating rule algoirthm: " + index);
                        }
                        // Check to see if Attribute Value exists, if yes then it is not a compound rule
                        if(jaxbElement.getValue() instanceof AttributeValueType) {
@@ -259,10 +333,10 @@ public class DecisionPolicyController extends RestrictedBaseController {
                                index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
                        }
                        // Populate combo box
-                       if (logger.isDebugEnabled()) {
-                               logger.debug("Prepopulating Compound rule algorithm: " + index);
+                       if (policyLogger.isDebugEnabled()) {
+                               policyLogger.debug("Prepopulating Compound rule algorithm: " + index);
                        }
-                       Map<String, String> rule = new HashMap<String, String>();
+                       Map<String, String> rule = new HashMap<>();
                        for (String key : PolicyController.getDropDownMap().keySet()) {
                                String keyValue = PolicyController.getDropDownMap().get(key);
                                if (keyValue.equals(decisionApply.getFunctionId())) {
@@ -281,6 +355,7 @@ public class DecisionPolicyController extends RestrictedBaseController {
                        ruleAlgorithmList.add(rule);
                        index++;
                }
+               
                return index;
        }
 }