X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ECOMP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcomponents%2FDecisionPolicy.java;h=5a566c9bdc1d1df76f5fe01e8c430dc85cbc760b;hb=4ca818fdfb9b807562166800a086b413593d6894;hp=e5a114ba85954c1fd1ca782c0e5434c849db7fac;hpb=7e547eaa55920dfbc9691eab33bb728395b50cf2;p=policy%2Fengine.git diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java index e5a114ba8..5a566c9bd 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java @@ -21,6 +21,7 @@ package org.openecomp.policy.pap.xacml.rest.components; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @@ -28,6 +29,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -47,6 +50,7 @@ import org.openecomp.policy.controlloop.policy.guard.Constraint; import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard; import org.openecomp.policy.controlloop.policy.guard.Guard; import org.openecomp.policy.controlloop.policy.guard.GuardPolicy; +import org.openecomp.policy.controlloop.policy.guard.MatchParameters; import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder; import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet; import org.openecomp.policy.pap.xacml.rest.util.JPAUtils; @@ -54,6 +58,7 @@ import org.openecomp.policy.rest.adapter.PolicyRestAdapter; import org.openecomp.policy.rest.jpa.Datatype; import org.openecomp.policy.rest.jpa.DecisionSettings; import org.openecomp.policy.rest.jpa.FunctionDefinition; +import org.openecomp.policy.utils.PolicyUtils; import org.openecomp.policy.xacml.api.XACMLErrorConstants; import org.openecomp.policy.xacml.std.pip.engines.aaf.AAFEngine; import org.openecomp.policy.xacml.util.XACMLPolicyScanner; @@ -83,18 +88,25 @@ public class DecisionPolicy extends Policy { private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicy.class); public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not"; - private static final String AAFProvider = "AAF"; + private static final String AAFPROVIDER = "AAF"; public static final String GUARD_YAML = "GUARD_YAML"; - private static final String XACMLTEMPLATE = "Decision_GuardPolicyTemplate.xml"; + public static final String GUARD_BL_YAML = "GUARD_BL_YAML"; + public static final String RAINY_DAY = "Rainy_Day"; + private static final String XACML_GUARD_TEMPLATE = "Decision_GuardPolicyTemplate.xml"; + private static final String XACML_BLGUARD_TEMPLATE = "Decision_GuardBLPolicyTemplate.xml"; + + private static final String ECOMPNAME = "ECOMPName"; + private static final String POLICY_NAME = "PolicyName"; + private static final String DESCRIPTION = "description"; - List dynamicLabelRuleAlgorithms = new LinkedList(); - List dynamicFieldComboRuleAlgorithms = new LinkedList(); - List dynamicFieldOneRuleAlgorithms = new LinkedList(); - List dynamicFieldTwoRuleAlgorithms = new LinkedList(); - List dataTypeList = new LinkedList(); + List dynamicLabelRuleAlgorithms = new LinkedList<>(); + List dynamicFieldComboRuleAlgorithms = new LinkedList<>(); + List dynamicFieldOneRuleAlgorithms = new LinkedList<>(); + List dynamicFieldTwoRuleAlgorithms = new LinkedList<>(); + List dataTypeList = new LinkedList<>(); - protected Map dropDownMap = new HashMap(); + protected Map dropDownMap = new HashMap<>(); public DecisionPolicy() { @@ -108,7 +120,7 @@ public class DecisionPolicy extends Policy { @Override public Map savePolicies() throws Exception { - Map successMap = new HashMap(); + Map successMap = new HashMap<>(); if(isPolicyExists()){ successMap.put("EXISTS", "This Policy already exist on the PAP"); return successMap; @@ -142,7 +154,7 @@ public class DecisionPolicy extends Policy { version = policyAdapter.getHighestVersion(); // Create the Instance for pojo, PolicyType object is used in marshalling. - if (policyAdapter.getPolicyType().equals("Decision")) { + if ("Decision".equals(policyAdapter.getPolicyType())) { PolicyType policyConfig = new PolicyType(); policyConfig.setVersion(Integer.toString(version)); @@ -152,20 +164,20 @@ public class DecisionPolicy extends Policy { } policyName = policyAdapter.getNewFileName(); - if(policyAdapter.getRuleProvider().equals(GUARD_YAML)){ - Map yamlParams = new HashMap(); - yamlParams.put("description", (policyAdapter.getPolicyDescription()!=null)? policyAdapter.getPolicyDescription(): "YAML Guard Policy"); + if(policyAdapter.getRuleProvider().equals(GUARD_YAML) || policyAdapter.getRuleProvider().equals(GUARD_BL_YAML)){ + Map yamlParams = new HashMap<>(); + yamlParams.put(DESCRIPTION, (policyAdapter.getPolicyDescription()!=null)? policyAdapter.getPolicyDescription(): "YAML Guard Policy"); String fileName = policyAdapter.getNewFileName(); - String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length()); - if ((name == null) || (name.equals(""))) { - name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()); + String name = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length()); + if ((name == null) || ("".equals(name))) { + name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length()); } - yamlParams.put("PolicyName", name); - yamlParams.put("ECOMPName", policyAdapter.getEcompName()); + yamlParams.put(POLICY_NAME, name); + yamlParams.put(ECOMPNAME, policyAdapter.getEcompName()); Map params = policyAdapter.getDynamicFieldConfigAttributes(); yamlParams.putAll(params); // Call YAML to XACML - PolicyType decisionPolicy = getGuardPolicy(yamlParams); + PolicyType decisionPolicy = getGuardPolicy(yamlParams, policyAdapter.getRuleProvider()); decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); decisionPolicy.setVersion(Integer.toString(version)); policyAdapter.setPolicyData(decisionPolicy); @@ -178,20 +190,20 @@ public class DecisionPolicy extends Policy { decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); AllOfType allOfOne = new AllOfType(); String fileName = policyAdapter.getNewFileName(); - String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length()); - if ((name == null) || (name.equals(""))) { - name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()); + String name = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length()); + if ((name == null) || ("".equals(name))) { + name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length()); } - allOfOne.getMatch().add(createMatch("PolicyName", name)); + allOfOne.getMatch().add(createMatch(POLICY_NAME, name)); AllOfType allOf = new AllOfType(); // Match for Ecomp - allOf.getMatch().add(createMatch("ECOMPName", (policyAdapter.getEcompName()))); + allOf.getMatch().add(createMatch(ECOMPNAME, (policyAdapter.getEcompName()))); Map dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes(); - if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){ - dynamicFieldComponentAttributes = new HashMap(); + if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFPROVIDER)){ + dynamicFieldComponentAttributes = new HashMap<>(); } // If there is any dynamic field attributes create the matches here @@ -211,67 +223,133 @@ public class DecisionPolicy extends Policy { decisionPolicy.setTarget(target); Map dynamicFieldDecisionSettings = policyAdapter.getDynamicSettingsMap(); - - //dynamicVariableList = policyAdapter.getDynamicVariableList(); - if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){ - dynamicFieldDecisionSettings = new HashMap(); + if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFPROVIDER)){ + dynamicFieldDecisionSettings = new HashMap<>(); } // settings are dynamic so check how many rows are added and add all for (String keyField : dynamicFieldDecisionSettings.keySet()) { String key = keyField; String value = dynamicFieldDecisionSettings.get(key); - //String dataType = (String) dynamicVariableList.get(counter); String dataType = getDataType(key); VariableDefinitionType dynamicVariable = createDynamicVariable(key, value, dataType); decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(dynamicVariable); } + Map dynamicFieldTreatmentAttributes = policyAdapter.getRainydayMap(); + + if(policyAdapter.getRuleProvider().equals(RAINY_DAY)){ + for(String keyField : dynamicFieldTreatmentAttributes.keySet()) { + String errorcode = keyField; + String treatment = dynamicFieldTreatmentAttributes.get(errorcode); + createRainydayRule(decisionPolicy, errorcode, treatment, true); + } + } else { + createRule(decisionPolicy, true); + createRule(decisionPolicy, false); + } - createRule(decisionPolicy, true); - createRule(decisionPolicy, false); } - setPreparedToSave(true); return true; } - public PolicyType getGuardPolicy(Map yamlParams) { + public PolicyType getGuardPolicy(Map yamlParams, String ruleProvider) throws BuilderException{ try { ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard()); - GuardPolicy policy1 = new GuardPolicy((policyAdapter.getUuid()!=null? policyAdapter.getUuid(): UUID.randomUUID().toString()) ,yamlParams.get("PolicyName"), yamlParams.get("description"), yamlParams.get("actor"), yamlParams.get("recipe")); + MatchParameters matchParameters = new MatchParameters(yamlParams.get("actor"), yamlParams.get("recipe")); + matchParameters.setControlLoopName(yamlParams.get("clname")); + if(yamlParams.containsKey("targets")){ + String targetString = yamlParams.get("targets"); + List targets = null; + if(targetString!=null && !targetString.isEmpty()){ + if (targetString.contains(",")){ + targets = Arrays.asList(targetString.split(",")); + } + else{ + targets = new ArrayList<>(); + targets.add(targetString); + } + } + matchParameters.setTargets(targets); + } + GuardPolicy policy1 = new GuardPolicy((policyAdapter.getUuid()!=null? policyAdapter.getUuid(): UUID.randomUUID().toString()) ,yamlParams.get(POLICY_NAME), yamlParams.get(DESCRIPTION), matchParameters); builder = builder.addGuardPolicy(policy1); - Map time_in_range = new HashMap(); - time_in_range.put("arg2", yamlParams.get("guardActiveStart")); - time_in_range.put("arg3", yamlParams.get("guardActiveEnd")); - Constraint cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), yamlParams.get("timeWindow"), time_in_range); - builder = builder.addLimitConstraint(policy1.id, cons); + Map activeTimeRange = new HashMap<>(); + activeTimeRange.put("start", yamlParams.get("guardActiveStart")); + activeTimeRange.put("end", yamlParams.get("guardActiveEnd")); + String blackListString = yamlParams.get("blackList"); + List blackList = null; + if(blackListString!=null){ + if (blackListString.contains(",")){ + blackList = Arrays.asList(blackListString.split(",")); + } + else{ + blackList = new ArrayList<>(); + blackList.add(blackListString); + } + } + File templateFile; + Path xacmlTemplatePath; + ClassLoader classLoader = getClass().getClassLoader(); + Constraint cons = new Constraint(); + switch (ruleProvider){ + case GUARD_BL_YAML: + templateFile = new File(classLoader.getResource(XACML_BLGUARD_TEMPLATE).getFile()); + xacmlTemplatePath = templateFile.toPath(); + cons.setActive_time_range(activeTimeRange); + cons.setBlacklist(blackList); + break; + default: + templateFile = new File(classLoader.getResource(XACML_GUARD_TEMPLATE).getFile()); + xacmlTemplatePath = templateFile.toPath(); + Map timeWindow = new HashMap<>(); + if(!PolicyUtils.isInteger(yamlParams.get("timeWindow"))){ + throw new BuilderException("time window is not in Integer format."); + } + String timeUnits = yamlParams.get("timeUnits"); + if(timeUnits==null || !(timeUnits.equalsIgnoreCase("minute") || timeUnits.equalsIgnoreCase("hour") || timeUnits.equalsIgnoreCase("day") + || timeUnits.equalsIgnoreCase("week") || timeUnits.equalsIgnoreCase("month")||timeUnits.equalsIgnoreCase("year"))){ + throw new BuilderException("time Units is not in proper format."); + } + timeWindow.put("value", yamlParams.get("timeWindow")); + timeWindow.put("units", yamlParams.get("timeUnits")); + cons = new Constraint(Integer.parseInt(yamlParams.get("limit")),timeWindow,activeTimeRange); + break; + } + builder = builder.addLimitConstraint(policy1.getId(), cons); // Build the specification Results results = builder.buildSpecification(); // YAML TO XACML ControlLoopGuard yamlGuardObject = SafePolicyBuilder.loadYamlGuard(results.getSpecification()); - Path xacmlTemplatePath = Paths.get(XACMLTEMPLATE); String xacmlTemplateContent; try { xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath)); - HashMap yamlSpecs = new HashMap(); - yamlSpecs.put("PolicyName", yamlParams.get("PolicyName")); - yamlSpecs.put("description", yamlParams.get("description")); - yamlSpecs.put("ECOMPName", yamlParams.get("ECOMPName")); - yamlSpecs.put("actor", yamlGuardObject.guards.getFirst().actor); - yamlSpecs.put("recipe", yamlGuardObject.guards.getFirst().recipe); - yamlSpecs.put("limit", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num.toString()); - yamlSpecs.put("timeWindow", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration); - yamlSpecs.put("guardActiveStart", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg2")); - yamlSpecs.put("guardActiveEnd", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg3")); - String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent,yamlSpecs); - // Convert the Policy into Stream input to Policy Adapter. + HashMap yamlSpecs = new HashMap<>(); + yamlSpecs.put(POLICY_NAME, yamlParams.get(POLICY_NAME)); + yamlSpecs.put(DESCRIPTION, yamlParams.get(DESCRIPTION)); + yamlSpecs.put(ECOMPNAME, yamlParams.get(ECOMPNAME)); + yamlSpecs.put("actor", yamlGuardObject.getGuards().getFirst().getMatch_parameters().getActor()); + yamlSpecs.put("recipe", yamlGuardObject.getGuards().getFirst().getMatch_parameters().getRecipe()); + yamlSpecs.put("clname", yamlGuardObject.getGuards().getFirst().getMatch_parameters().getControlLoopName()); + if(yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getFreq_limit_per_target()!=null){ + yamlSpecs.put("limit", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getFreq_limit_per_target().toString()); + } + if(yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_window()!=null){ + yamlSpecs.put("twValue", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_window().get("value")); + yamlSpecs.put("twUnits", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_window().get("units")); + } + yamlSpecs.put("guardActiveStart", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getActive_time_range().get("start")); + yamlSpecs.put("guardActiveEnd", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getActive_time_range().get("end")); + String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent,yamlSpecs, yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getBlacklist(), yamlGuardObject.getGuards().getFirst().getMatch_parameters().getTargets()); + // Convert the Policy into Stream input to Policy Adapter. Object policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(xacmlPolicyContent.getBytes(StandardCharsets.UTF_8))); return (PolicyType) policy; } catch (IOException e) { - PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage()); + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage() , e); } } catch (BuilderException e) { - PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage()); + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage() ,e); + throw e; } return null; } @@ -332,7 +410,7 @@ public class DecisionPolicy extends Policy { dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2(); dropDownMap = createDropDownMap(); - if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){ + if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFPROVIDER)){ // Values for AAF Provider are here for XML Creation. ConditionType condition = new ConditionType(); ApplyType decisionApply = new ApplyType(); @@ -375,7 +453,7 @@ public class DecisionPolicy extends Policy { if(!permitRule){ AdviceExpressionsType adviceExpressions = new AdviceExpressionsType(); AdviceExpressionType adviceExpression = new AdviceExpressionType(); - adviceExpression.setAdviceId(AAFProvider); + adviceExpression.setAdviceId(AAFPROVIDER); adviceExpression.setAppliesTo(EffectType.DENY); AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType(); assignment.setAttributeId("aaf.response"); @@ -393,19 +471,19 @@ public class DecisionPolicy extends Policy { decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); policyAdapter.setPolicyData(decisionPolicy); - }else if (dynamicLabelRuleAlgorithms != null && dynamicLabelRuleAlgorithms.size() > 0) { + }else if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) { boolean isCompound = false; ConditionType condition = new ConditionType(); int index = dynamicFieldOneRuleAlgorithms.size() - 1; for (String labelAttr : dynamicLabelRuleAlgorithms) { // if the rule algorithm as a label means it is a compound - if (dynamicFieldOneRuleAlgorithms.get(index).toString().equals(labelAttr)) { + if (dynamicFieldOneRuleAlgorithms.get(index).equals(labelAttr)) { ApplyType decisionApply = new ApplyType(); - String selectedFunction = (String) dynamicFieldComboRuleAlgorithms.get(index).toString(); - String value1 = (String) dynamicFieldOneRuleAlgorithms.get(index).toString(); - String value2 = dynamicFieldTwoRuleAlgorithms.get(index).toString(); + String selectedFunction = dynamicFieldComboRuleAlgorithms.get(index); + String value1 = dynamicFieldOneRuleAlgorithms.get(index); + String value2 = dynamicFieldTwoRuleAlgorithms.get(index); decisionApply.setFunctionId(dropDownMap.get(selectedFunction)); decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value1))); decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value2))); @@ -415,7 +493,7 @@ public class DecisionPolicy extends Policy { // if rule algorithm not a compound if (!isCompound) { - condition.setExpression(new ObjectFactory().createApply(getInnerDecisionApply(dynamicLabelRuleAlgorithms.get(index).toString()))); + condition.setExpression(new ObjectFactory().createApply(getInnerDecisionApply(dynamicLabelRuleAlgorithms.get(index)))); } } if (!permitRule) { @@ -444,6 +522,87 @@ public class DecisionPolicy extends Policy { } + private void createRainydayRule(PolicyType decisionPolicy, String errorcode, String treatment, boolean permitRule) { + RuleType rule = new RuleType(); + + rule.setRuleId(UUID.randomUUID().toString()); + + if (permitRule) { + rule.setEffect(EffectType.PERMIT); + } else { + rule.setEffect(EffectType.DENY); + } + rule.setTarget(new TargetType()); + + // Create Target in Rule + AllOfType allOfInRule = new AllOfType(); + + // Creating match for DECIDE in rule target + MatchType accessMatch = new MatchType(); + AttributeValueType accessAttributeValue = new AttributeValueType(); + accessAttributeValue.setDataType(STRING_DATATYPE); + accessAttributeValue.getContent().add("DECIDE"); + accessMatch.setAttributeValue(accessAttributeValue); + AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType(); + URI accessURI = null; + try { + accessURI = new URI(ACTION_ID); + } catch (URISyntaxException e) { + PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DecisionPolicy", "Exception creating ACCESS URI"); + } + accessAttributeDesignator.setCategory(CATEGORY_ACTION); + accessAttributeDesignator.setDataType(STRING_DATATYPE); + accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue()); + accessMatch.setAttributeDesignator(accessAttributeDesignator); + accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE); + + allOfInRule.getMatch().add(accessMatch); + + // Creating match for ErrorCode in rule target + MatchType errorcodeMatch = new MatchType(); + AttributeValueType errorcodeAttributeValue = new AttributeValueType(); + errorcodeAttributeValue.setDataType(STRING_DATATYPE); + errorcodeAttributeValue.getContent().add(errorcode); + errorcodeMatch.setAttributeValue(errorcodeAttributeValue); + AttributeDesignatorType errorcodeAttributeDesignator = new AttributeDesignatorType(); + errorcodeAttributeDesignator.setCategory(CATEGORY_RESOURCE); + errorcodeAttributeDesignator.setDataType(STRING_DATATYPE); + errorcodeAttributeDesignator.setAttributeId("ErrorCode"); + errorcodeMatch.setAttributeDesignator(errorcodeAttributeDesignator); + errorcodeMatch.setMatchId(FUNCTION_STRING_REGEXP_MATCH); + + allOfInRule.getMatch().add(errorcodeMatch); + + AnyOfType anyOfInRule = new AnyOfType(); + anyOfInRule.getAllOf().add(allOfInRule); + + TargetType targetInRule = new TargetType(); + targetInRule.getAnyOf().add(anyOfInRule); + + rule.setTarget(targetInRule); + + AdviceExpressionsType adviceExpressions = new AdviceExpressionsType(); + AdviceExpressionType adviceExpression = new AdviceExpressionType(); + adviceExpression.setAdviceId(RAINY_DAY); + adviceExpression.setAppliesTo(EffectType.PERMIT); + + AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType(); + assignment.setAttributeId("treatment"); + assignment.setCategory(CATEGORY_RESOURCE); + + AttributeValueType treatmentAttributeValue = new AttributeValueType(); + treatmentAttributeValue.setDataType(STRING_DATATYPE); + treatmentAttributeValue.getContent().add(treatment); + assignment.setExpression(new ObjectFactory().createAttributeValue(treatmentAttributeValue)); + + adviceExpression.getAttributeAssignmentExpression().add(assignment); + adviceExpressions.getAdviceExpression().add(adviceExpression); + rule.setAdviceExpressions(adviceExpressions); + decisionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule); + policyAdapter.setPolicyData(decisionPolicy); + + } + // if compound setting the inner apply here protected ApplyType getInnerDecisionApply(String value1Label) { ApplyType decisionApply = new ApplyType(); @@ -451,7 +610,7 @@ public class DecisionPolicy extends Policy { // check the index for the label. for (String labelAttr : dynamicLabelRuleAlgorithms) { if (labelAttr.equals(value1Label)) { - String value1 = (String) dynamicFieldOneRuleAlgorithms.get(index).toString(); + String value1 = dynamicFieldOneRuleAlgorithms.get(index); populateDataTypeList(value1); // check if the row contains label again @@ -462,8 +621,8 @@ public class DecisionPolicy extends Policy { } // Getting the values from the form. - String functionKey = (String) dynamicFieldComboRuleAlgorithms.get(index).toString(); - String value2 = dynamicFieldTwoRuleAlgorithms.get(index).toString(); + String functionKey = dynamicFieldComboRuleAlgorithms.get(index); + String value2 = dynamicFieldTwoRuleAlgorithms.get(index); decisionApply.setFunctionId(dropDownMap.get(functionKey)); // if two text field are rule attributes. if ((value1.contains(RULE_VARIABLE)) && (value2.contains(RULE_VARIABLE))) { @@ -549,9 +708,9 @@ public class DecisionPolicy extends Policy { // if the rule algorithm is multiple compound one setting the apply protected ApplyType getCompoundDecisionApply(int index) { ApplyType decisionApply = new ApplyType(); - String selectedFunction = dynamicFieldComboRuleAlgorithms.get(index).toString(); - String value1 = dynamicFieldOneRuleAlgorithms.get(index).toString(); - String value2 = dynamicFieldTwoRuleAlgorithms.get(index).toString(); + String selectedFunction = dynamicFieldComboRuleAlgorithms.get(index); + String value1 = dynamicFieldOneRuleAlgorithms.get(index); + String value2 = dynamicFieldTwoRuleAlgorithms.get(index); decisionApply.setFunctionId(dropDownMap.get(selectedFunction)); decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value1))); decisionApply.getExpression().add(new ObjectFactory().createApply(getInnerDecisionApply(value2))); @@ -573,16 +732,13 @@ public class DecisionPolicy extends Policy { } private void populateDataTypeList(String value1) { - - ///String value1 = dynamicFieldDecisionOneRuleAlgorithms.get(index).getValue().toString(); String dataType = null; - if(value1.contains("S_")) { value1 = value1.substring(2, value1.length()); - DecisionSettings decisionSettings = findDecisionSettingsBySettingId(value1); - if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("string")) { + DecisionSettings decisionSettings = findDecisionSettingsBySettingId(value1.substring(2, value1.length())); + if (decisionSettings != null && "string".equals(decisionSettings.getDatatypeBean().getShortName())) { dataType = STRING_DATATYPE; - } else if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("boolean")) { + } else if (decisionSettings != null && "boolean".equals(decisionSettings.getDatatypeBean().getShortName())) { dataType = BOOLEAN_DATATYPE; } else { dataType = INTEGER_DATATYPE; @@ -601,17 +757,16 @@ public class DecisionPolicy extends Policy { } catch (Exception e) { LOGGER.error("Exception Occured"+e); } - Map> functionMap = jpaUtils.getFunctionDatatypeMap(); - Map dropDownMap = new HashMap(); - for (Datatype id : functionMap.keySet()) { - List functionDefinitions = (List) functionMap - .get(id); - for (FunctionDefinition functionDef : functionDefinitions) { - dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid()); + Map dropDownOptions = new HashMap<>(); + if(jpaUtils!=null){ + Map> functionMap = jpaUtils.getFunctionDatatypeMap(); + for (Map.Entry> map: functionMap.entrySet()) { + for (FunctionDefinition functionDef : map.getValue()) { + dropDownOptions.put(functionDef.getShortname(),functionDef.getXacmlid()); + } } } - - return dropDownMap; + return dropDownOptions; } private String getDataType(String key) { @@ -619,9 +774,9 @@ public class DecisionPolicy extends Policy { DecisionSettings decisionSettings = findDecisionSettingsBySettingId(key); String dataType = null; - if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("string")) { + if (decisionSettings != null && "string".equals(decisionSettings.getDatatypeBean().getShortName())) { dataType = STRING_DATATYPE; - } else if (decisionSettings != null && decisionSettings.getDatatypeBean().getShortName().equals("boolean")) { + } else if (decisionSettings != null && "boolean".equals(decisionSettings.getDatatypeBean().getShortName())) { dataType = BOOLEAN_DATATYPE; } else { dataType = INTEGER_DATATYPE;