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=02ea630a6828cbe67055747864e1ecc933482838;hb=fc5c07705edc4dcb7083b39116a43844bb6a1490;hp=822768f6daaa8deeb78b7ea6ea4b187ba562da82;hpb=91d04c64771832a0b8815ffbe1f0f9920320d94d;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 822768f6d..02ea630a6 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 @@ -20,18 +20,49 @@ 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; +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; import java.util.Map; +import java.util.UUID; import javax.persistence.EntityManager; import javax.persistence.Query; +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import org.openecomp.policy.common.logging.flexlogger.FlexLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.openecomp.policy.controlloop.policy.builder.BuilderException; +import org.openecomp.policy.controlloop.policy.builder.Results; +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.builder.ControlLoopGuardBuilder; +import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet; +import org.openecomp.policy.pap.xacml.rest.util.JPAUtils; +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.xacml.api.XACMLErrorConstants; +import org.openecomp.policy.xacml.std.pip.engines.aaf.AAFEngine; +import org.openecomp.policy.xacml.util.XACMLPolicyScanner; + +import com.att.research.xacml.std.IdentifierImpl; + import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; @@ -50,61 +81,29 @@ 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.FilenameUtils; -import org.openecomp.policy.pap.xacml.rest.adapters.PolicyRestAdapter; -import org.openecomp.policy.pap.xacml.rest.util.JPAUtils; -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.xacml.std.pip.engines.aaf.AAFEngine; - -import com.att.research.xacml.std.IdentifierImpl; - -import org.openecomp.policy.common.logging.eelf.MessageCodes; -import org.openecomp.policy.common.logging.eelf.PolicyLogger; -import org.openecomp.policy.common.logging.flexlogger.FlexLogger; -import org.openecomp.policy.common.logging.flexlogger.Logger; - public class DecisionPolicy extends Policy { - - /** - * Config Fields - */ - private static final Logger logger = FlexLogger.getLogger(ConfigPolicy.class); - - public static final String JSON_CONFIG = "JSON"; - public static final String XML_CONFIG = "XML"; - public static final String PROPERTIES_CONFIG = "PROPERTIES"; - public static final String OTHER_CONFIG = "OTHER"; - public static final String PDP_ACTION = "PDP"; - public static final String PEP_ACTION = "PEP"; - public static final String TYPE_ACTION = "REST"; - - public static final String GET_METHOD = "GET"; - public static final String PUT_METHOD = "PUT"; - public static final String POST_METHOD = "POST"; - - public static final String PERFORMER_ATTRIBUTEID = "performer"; - public static final String TYPE_ATTRIBUTEID = "type"; - public static final String METHOD_ATTRIBUTEID = "method"; - public static final String HEADERS_ATTRIBUTEID = "headers"; - public static final String URL_ATTRIBUTEID = "url"; - public static final String BODY_ATTRIBUTEID = "body"; + 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 CustomProvider = "Custom"; + public static final String GUARD_YAML = "GUARD_YAML"; + public static final String GUARD_BL_YAML = "GUARD_BL_YAML"; + 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 dynamicVariableList = 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() { @@ -117,13 +116,13 @@ 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; } - + if(!isPreparedToSave()){ //Prep and configure the policy for saving prepareToSave(); @@ -131,12 +130,9 @@ public class DecisionPolicy extends Policy { // Until here we prepared the data and here calling the method to create xml. Path newPolicyPath = null; - newPolicyPath = Paths.get(policyAdapter.getParentPath().toString(), policyName); - successMap = createPolicy(newPolicyPath, getCorrectPolicyDataObject()); - if (successMap.containsKey("success")) { - Path finalPolicyPath = getFinalPolicyPath(); - policyAdapter.setFinalPolicyPath(finalPolicyPath.toString()); - } + newPolicyPath = Paths.get(policyAdapter.getNewFileName()); + + successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject()); return successMap; } @@ -152,15 +148,10 @@ public class DecisionPolicy extends Policy { int version = 0; String policyID = policyAdapter.getPolicyID(); - - if (policyAdapter.isEditPolicy()) { - version = policyAdapter.getHighestVersion() + 1; - } else { - version = 1; - } + 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)); @@ -168,59 +159,48 @@ public class DecisionPolicy extends Policy { policyConfig.setTarget(new TargetType()); policyAdapter.setData(policyConfig); } + policyName = policyAdapter.getNewFileName(); - if (policyAdapter.getData() != null) { - - // Save off everything - // making ready all the required elements to generate the action policy xml. - // Get the uniqueness for policy name. - Path newFile = getNextFilename(Paths.get(policyAdapter.getParentPath().toString()), policyAdapter.getPolicyType(), policyAdapter.getPolicyName(), version); - if (newFile == null) { - //TODO:EELF Cleanup - Remove logger - //logger.error("File already exists, cannot create the policy."); - PolicyLogger.error("File already exists, cannot create the policy."); - setPolicyExists(true); - return false; - } - policyName = newFile.getFileName().toString(); - - // Make sure the filename ends with an extension - if (policyName.endsWith(".xml") == false) { - policyName = policyName + ".xml"; + 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) || ("".equals(name))) { + name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length()); } - + 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, policyAdapter.getRuleProvider()); + decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); + decisionPolicy.setVersion(Integer.toString(version)); + policyAdapter.setPolicyData(decisionPolicy); + policyAdapter.setData(decisionPolicy); + }else if (policyAdapter.getData() != null) { PolicyType decisionPolicy = (PolicyType) policyAdapter.getData(); decisionPolicy.setDescription(policyAdapter.getPolicyDescription()); decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); AllOfType allOfOne = new AllOfType(); - final Path gitPath = Paths.get(policyAdapter.getUserGitPath().toString()); - String policyDir = policyAdapter.getParentPath().toString(); - int startIndex = policyDir.indexOf(gitPath.toString()) + gitPath.toString().length() + 1; - policyDir = policyDir.substring(startIndex, policyDir.length()); - logger.info("print the main domain value "+policyDir); - String path = policyDir.replace('\\', '.'); - if(path.contains("/")){ - path = policyDir.replace('/', '.'); - logger.info("print the path:" +path); + String fileName = policyAdapter.getNewFileName(); + String name = fileName.substring(fileName.lastIndexOf('\\') + 1, fileName.length()); + if ((name == null) || ("".equals(name))) { + name = fileName.substring(fileName.lastIndexOf('/') + 1, fileName.length()); } - String fileName = FilenameUtils.removeExtension(policyName); - fileName = path + "." + fileName + ".xml"; - String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length()); - if ((name == null) || (name.equals(""))) { - 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.getProviderComboBox()!=null && policyAdapter.getProviderComboBox().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 @@ -242,8 +222,8 @@ public class DecisionPolicy extends Policy { Map dynamicFieldDecisionSettings = policyAdapter.getDynamicSettingsMap(); //dynamicVariableList = policyAdapter.getDynamicVariableList(); - if(policyAdapter.getProviderComboBox()!=null && policyAdapter.getProviderComboBox().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 @@ -264,10 +244,81 @@ public class DecisionPolicy extends Policy { return true; } + public PolicyType getGuardPolicy(Map yamlParams, String ruleProvider) { + try { + ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard()); + GuardPolicy policy1 = new GuardPolicy((policyAdapter.getUuid()!=null? policyAdapter.getUuid(): UUID.randomUUID().toString()) ,yamlParams.get(POLICY_NAME), yamlParams.get(DESCRIPTION), yamlParams.get("actor"), yamlParams.get("recipe")); + 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")); + 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; + Constraint cons; + ClassLoader classLoader = getClass().getClassLoader(); + switch (ruleProvider){ + case GUARD_BL_YAML: + templateFile = new File(classLoader.getResource(XACML_BLGUARD_TEMPLATE).getFile()); + xacmlTemplatePath = templateFile.toPath(); + cons = new Constraint(time_in_range,blackList); + break; + default: + templateFile = new File(classLoader.getResource(XACML_GUARD_TEMPLATE).getFile()); + xacmlTemplatePath = templateFile.toPath(); + cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), yamlParams.get("timeWindow"), time_in_range); + break; + } + builder = builder.addLimitConstraint(policy1.getId(), cons); + // Build the specification + Results results = builder.buildSpecification(); + // YAML TO XACML + ControlLoopGuard yamlGuardObject = SafePolicyBuilder.loadYamlGuard(results.getSpecification()); + + String xacmlTemplateContent; + try { + xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath)); + 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().getActor()); + yamlSpecs.put("recipe", yamlGuardObject.getGuards().getFirst().getRecipe()); + if(yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getNum()!=null){ + yamlSpecs.put("limit", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getNum().toString()); + } + if(yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getDuration()!=null){ + yamlSpecs.put("timeWindow", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getDuration()); + } + yamlSpecs.put("guardActiveStart", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_in_range().get("arg2")); + yamlSpecs.put("guardActiveEnd", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getTime_in_range().get("arg3")); + String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent,yamlSpecs, yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getBlacklist()); + // 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) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage() + e); + } + } catch (BuilderException e) { + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage() +e); + } + return null; + } + private DecisionSettings findDecisionSettingsBySettingId(String settingId) { DecisionSettings decisionSetting = null; - EntityManager em = policyAdapter.getEntityManagerFactory().createEntityManager(); + EntityManager em = XACMLPapServlet.getEmf().createEntityManager(); Query getDecisionSettings = em.createNamedQuery("DecisionSettings.findAll"); List decisionSettingsList = getDecisionSettings.getResultList(); @@ -306,8 +357,6 @@ public class DecisionPolicy extends Policy { try { accessURI = new URI(ACTION_ID); } catch (URISyntaxException e) { - //TODO:EELF Cleanup - Remove logger - //logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getStackTrace()); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DecisionPolicy", "Exception creating ACCESS URI"); } accessAttributeDesignator.setCategory(CATEGORY_ACTION); @@ -322,7 +371,7 @@ public class DecisionPolicy extends Policy { dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2(); dropDownMap = createDropDownMap(); - if(policyAdapter.getProviderComboBox()!=null && policyAdapter.getProviderComboBox().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(); @@ -383,19 +432,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))); @@ -405,7 +454,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) { @@ -429,8 +478,6 @@ public class DecisionPolicy extends Policy { policyAdapter.setPolicyData(decisionPolicy); } else { - //TODO:EELF Cleanup - Remove logger - //logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Unsupported data object."+ policyAdapter.getData().getClass().getCanonicalName()); PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object."+ policyAdapter.getData().getClass().getCanonicalName()); } @@ -443,7 +490,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 @@ -454,8 +501,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))) { @@ -541,9 +588,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))); @@ -565,16 +612,14 @@ 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; @@ -589,18 +634,17 @@ public class DecisionPolicy extends Policy { private Map createDropDownMap(){ JPAUtils jpaUtils = null; try { - jpaUtils = JPAUtils.getJPAUtilsInstance(policyAdapter.getEntityManagerFactory()); + jpaUtils = JPAUtils.getJPAUtilsInstance(XACMLPapServlet.getEmf()); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + 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 dropDownMap = new HashMap<>(); + if(jpaUtils!=null){ + Map> functionMap = jpaUtils.getFunctionDatatypeMap(); + for (Map.Entry> map: functionMap.entrySet()) { + for (FunctionDefinition functionDef : map.getValue()) { + dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid()); + } } } @@ -612,9 +656,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; @@ -627,7 +671,5 @@ public class DecisionPolicy extends Policy { public Object getCorrectPolicyDataObject() { return policyAdapter.getData(); } - - - + }