From eb654437ee2815028602daa943d0a939a5bee3cd Mon Sep 17 00:00:00 2001 From: Michael Mokry Date: Fri, 7 Jul 2017 16:00:59 -0500 Subject: [POLICY-67] Add new Rainy Day Decision Policy Change-Id: I8fb730e4363c885ccbe3a05ae971c5de5608d70c Signed-off-by: Michael Mokry --- .../pap/xacml/rest/components/DecisionPolicy.java | 100 ++++++++++- .../DecisionPolicyDictionaryController.java | 183 +++++++++++++++++++++ .../pap/xacml/rest/handler/DeleteHandler.java | 12 +- .../rest/policycontroller/PolicyCreation.java | 36 +++- 4 files changed, 320 insertions(+), 11 deletions(-) (limited to 'ECOMP-PAP-REST/src/main/java/org') 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 02ea630a6..180b989e7 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 @@ -88,7 +88,8 @@ public class DecisionPolicy extends Policy { public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not"; private static final String AAFProvider = "AAF"; public static final String GUARD_YAML = "GUARD_YAML"; - public static final String GUARD_BL_YAML = "GUARD_BL_YAML"; + 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"; @@ -235,9 +236,21 @@ public class DecisionPolicy extends Policy { 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); @@ -483,6 +496,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 ACCESS 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_ACTION); + errorcodeAttributeDesignator.setDataType(STRING_DATATYPE); + errorcodeAttributeDesignator.setAttributeId("ErrorCode"); + errorcodeMatch.setAttributeDesignator(errorcodeAttributeDesignator); + errorcodeMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE); + + 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(); diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java index db3ebda44..fc525bbe3 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/DecisionPolicyDictionaryController.java @@ -21,7 +21,9 @@ package org.openecomp.policy.pap.xacml.rest.controller; import java.io.PrintWriter; +import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -35,6 +37,7 @@ import org.openecomp.policy.pap.xacml.rest.util.JsonMessage; import org.openecomp.policy.rest.dao.CommonClassDao; import org.openecomp.policy.rest.jpa.Datatype; import org.openecomp.policy.rest.jpa.DecisionSettings; +import org.openecomp.policy.rest.jpa.RainyDayTreatments; import org.openecomp.policy.rest.jpa.UserInfo; import org.openecomp.policy.xacml.api.XACMLErrorConstants; import org.springframework.beans.factory.annotation.Autowired; @@ -238,4 +241,184 @@ public class DecisionPolicyDictionaryController { return null; } + + + @RequestMapping(value={"/get_RainyDayDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE) + public void getRainyDayDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){ + try{ + Map model = new HashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(RainyDayTreatments.class, "bbID"))); + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); + JSONObject j = new JSONObject(msg); + response.getWriter().write(j.toString()); + } + catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + } + } + + + @RequestMapping(value={"/get_RainyDayDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE) + public void getRainyDayDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){ + try{ + Map model = new HashMap<>(); + ObjectMapper mapper = new ObjectMapper(); + model.put("rainyDayDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class))); + JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); + JSONObject j = new JSONObject(msg); + response.addHeader("successMapKey", "success"); + response.addHeader("operation", "getDictionary"); + response.getWriter().write(j.toString()); + } + catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.addHeader("error", "dictionaryDBQuery"); + } + } + + @RequestMapping(value={"/decision_dictionary/save_RainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) + public ModelAndView saveRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{ + try { + boolean duplicateflag = false; + boolean isFakeUpdate = false; + boolean fromAPI = false; + if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) { + fromAPI = true; + } + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JsonNode root = mapper.readTree(request.getReader()); + RainyDayTreatments decisionRainyDay; + TreatmentValues treatmentsData = null; + if (fromAPI) { + decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("dictionaryFields").toString(), RainyDayTreatments.class); + treatmentsData = (TreatmentValues)mapper.readValue(root.get("dictionaryFields").toString(), TreatmentValues.class); + //check if update operation or create, get id for data to be updated and update attributeData + if (request.getParameter("operation").equals("update")) { + List duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class); + int id = 0; + RainyDayTreatments data = (RainyDayTreatments) duplicateData.get(0); + id = data.getId(); + if(id==0){ + isFakeUpdate=true; + } else { + decisionRainyDay.setId(id); + } + } + } else { + decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("rainyDayDictionaryData").toString(), RainyDayTreatments.class); + treatmentsData = (TreatmentValues)mapper.readValue(root.get("rainyDayDictionaryData").toString(), TreatmentValues.class); + } + + String userValue = ""; + int counter = 0; + if(treatmentsData.getUserDataTypeValues().size() > 0){ + for(Object treatment : treatmentsData.getUserDataTypeValues()){ + if(treatment instanceof LinkedHashMap){ + String key = ((LinkedHashMap) treatment).get("treatment").toString(); + if(counter>0){ + userValue = userValue + ","; + } + userValue = userValue + key ; + counter ++; + } + } + } + decisionRainyDay.setTreatments(userValue); + + if(decisionRainyDay.getId() == 0){ + List duplicateData = commonClassDao.checkDuplicateEntry(decisionRainyDay.getBbid()+":"+decisionRainyDay.getWorkstep(), "bbid:workstep", RainyDayTreatments.class); + if(!duplicateData.isEmpty()){ + duplicateflag = true; + }else{ + commonClassDao.save(decisionRainyDay); + } + }else{ + if(!isFakeUpdate) { + commonClassDao.update(decisionRainyDay); + } + } + String responseString = ""; + if(duplicateflag){ + responseString = "Duplicate"; + }else{ + responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)); + } + + if (fromAPI) { + if (responseString!=null && !responseString.equals("Duplicate")) { + if(isFakeUpdate){ + responseString = "Exists"; + } else { + responseString = "Success"; + } + } + ModelAndView result = new ModelAndView(); + result.setViewName(responseString); + return result; + } else { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application / json"); + request.setCharacterEncoding("UTF-8"); + + PrintWriter out = response.getWriter(); + JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}"); + out.write(j.toString()); + return null; + } + + }catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding("UTF-8"); + PrintWriter out = response.getWriter(); + out.write(e.getMessage()); + } + return null; + } + + @RequestMapping(value={"/decision_dictionary/remove_rainyDay"}, method={org.springframework.web.bind.annotation.RequestMethod.POST}) + public ModelAndView removeRainyDayDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception { + try{ + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + JsonNode root = mapper.readTree(request.getReader()); + RainyDayTreatments decisionRainyDay = (RainyDayTreatments)mapper.readValue(root.get("data").toString(), RainyDayTreatments.class); + commonClassDao.delete(decisionRainyDay); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application / json"); + request.setCharacterEncoding("UTF-8"); + + PrintWriter out = response.getWriter(); + + String responseString = mapper.writeValueAsString(commonClassDao.getData(RainyDayTreatments.class)); + JSONObject j = new JSONObject("{rainyDayDictionaryDatas: " + responseString + "}"); + out.write(j.toString()); + + return null; + } + catch (Exception e){ + LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e); + response.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding("UTF-8"); + PrintWriter out = response.getWriter(); + out.write(e.getMessage()); + } + return null; + } + +} + +class TreatmentValues { + private ArrayList userDataTypeValues; + + public ArrayList getUserDataTypeValues() { + return userDataTypeValues; + } + + public void setUserDataTypeValues(ArrayList userDataTypeValues) { + this.userDataTypeValues = userDataTypeValues; + } } diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java index dd8cc656a..693196517 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/DeleteHandler.java @@ -158,7 +158,7 @@ public class DeleteHandler { getPolicyVersion.setParameter("pname", removeVersionExtension.replace(".", File.separator)); List pvResult = getPolicyVersion.getResultList(); PolicyVersion pVersion = (PolicyVersion) pvResult.get(0); - int highestVersion = 0; + int newVersion = 0; em.getTransaction().begin(); Class.forName(papDbDriver); con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword); @@ -180,7 +180,7 @@ public class DeleteHandler { return; }else if(status.equals("PolicyInPDP")){ PolicyLogger.error(MessageCodes.GENERAL_WARNING + "Policy can't be deleted, it is active in PDP Groups."); - response.addHeader("error", "unknown"); + response.addHeader("error", "PolicyInPDP"); response.setStatus(HttpServletResponse.SC_CONFLICT); return; }else{ @@ -217,13 +217,13 @@ public class DeleteHandler { policyEntity = (PolicyEntity) object; String policyEntityName = policyEntity.getPolicyName().replace(".xml", ""); int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf(".")+1)); - if(policyEntityVersion > highestVersion){ - highestVersion = policyEntityVersion; + if(policyEntityVersion > newVersion){ + newVersion = policyEntityVersion-1; } } } - pVersion.setActiveVersion(highestVersion); - pVersion.setHigherVersion(highestVersion); + pVersion.setActiveVersion(newVersion); + pVersion.setHigherVersion(newVersion); try{ policyVersionDeleted = true; em.persist(pVersion); diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java index 3437dab03..0d84519d3 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java @@ -82,6 +82,14 @@ public class PolicyCreation extends AbstractPolicyCreation{ private static CommonClassDao commonClassDao; + public static CommonClassDao getCommonClassDao() { + return commonClassDao; + } + + public static void setCommonClassDao(CommonClassDao commonClassDao) { + PolicyCreation.commonClassDao = commonClassDao; + } + @Autowired public PolicyCreation(CommonClassDao commonClassDao){ PolicyCreation.commonClassDao = commonClassDao; @@ -334,12 +342,15 @@ public class PolicyCreation extends AbstractPolicyCreation{ } else if (policyType.equalsIgnoreCase("Decision")) { if(policyData.getApiflag() == null){ Map settingsMap = new HashMap<>(); + Map treatmentMap = new HashMap<>(); List dynamicRuleAlgorithmLabels = new LinkedList<>(); List dynamicRuleAlgorithmCombo = new LinkedList<>(); List dynamicRuleAlgorithmField1 = new LinkedList<>(); List dynamicRuleAlgorithmField2 = new LinkedList<>(); List dynamicVariableList = new LinkedList<>(); List dataTypeList = new LinkedList<>(); + List errorCodeList = new LinkedList<>(); + List treatmentList = new LinkedList<>(); if(policyData.getSettings().size() > 0){ for(Object settingsData : policyData.getSettings()){ @@ -365,7 +376,8 @@ public class PolicyCreation extends AbstractPolicyCreation{ } } if(policyData.getRuleProvider()!=null && (policyData.getRuleProvider().equals(DecisionPolicy.GUARD_YAML)|| policyData.getRuleProvider().equals(DecisionPolicy.GUARD_BL_YAML)) - && policyData.getYamlparams()!=null){ attributeMap.put("actor", policyData.getYamlparams().getActor()); + && policyData.getYamlparams()!=null){ + attributeMap.put("actor", policyData.getYamlparams().getActor()); attributeMap.put("recipe", policyData.getYamlparams().getRecipe()); attributeMap.put("limit", policyData.getYamlparams().getLimit()); attributeMap.put("timeWindow", policyData.getYamlparams().getTimeWindow()); @@ -376,6 +388,23 @@ public class PolicyCreation extends AbstractPolicyCreation{ attributeMap.put("blackList", blackList); } } + if(policyData.getRuleProvider()!=null && policyData.getRuleProvider().equals(DecisionPolicy.RAINY_DAY)){ + attributeMap.put("ServiceType", policyData.getRainyday().getServiceType()); + attributeMap.put("VNFType", policyData.getRainyday().getVnfType()); + attributeMap.put("BB_ID", policyData.getRainyday().getBbid()); + attributeMap.put("WorkStep", policyData.getRainyday().getWorkstep()); + + if(policyData.getRainyday().getTreatmentTableChoices()!=null && policyData.getRainyday().getTreatmentTableChoices().size() > 0){ + for (Object table : policyData.getRainyday().getTreatmentTableChoices()){ + if(table instanceof LinkedHashMap){ + String errorcode = ((LinkedHashMap) table).get("errorcode").toString(); + String treatment = ((LinkedHashMap) table).get("treatment").toString(); + treatmentMap.put(errorcode, treatment); + } + } + } + } + policyData.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels); policyData.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo); policyData.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1); @@ -384,6 +413,9 @@ public class PolicyCreation extends AbstractPolicyCreation{ policyData.setDynamicSettingsMap(settingsMap); policyData.setDynamicFieldConfigAttributes(attributeMap); policyData.setDataTypeList(dataTypeList); + policyData.setRainydayMap(treatmentMap); + policyData.setErrorCodeList(errorCodeList); + policyData.setTreatmentList(treatmentList); } newPolicy = new DecisionPolicy(policyData); } @@ -480,7 +512,7 @@ public class PolicyCreation extends AbstractPolicyCreation{ } } catch (Exception e){ - LOGGER.error("Exception Occured"+e); + LOGGER.error("Exception Occured : "+e); } return new ResponseEntity(body, status); } -- cgit 1.2.3-korg