From e957a1d9a9fa6bd0f9f8348d1e864c09cfe2cb92 Mon Sep 17 00:00:00 2001 From: Michael Mokry Date: Mon, 10 Sep 2018 15:23:15 -0500 Subject: New min/max Guard Policy Made changes to support Guard policy create/update and other policy manager functions including deletePolicy and pushPolicy. Includes changes for new Guard policy support using GUI and API (Rest and Java Client) Made changes to address Jim's comments Made changes to address Liam's comments Change-Id: I133fe1fd9287ea77ea41a2788de90c7642c36b6a Issue-ID: POLICY-1038 Signed-off-by: Mike Mokry --- .../pap/xacml/rest/components/DecisionPolicy.java | 29 ++++- .../rest/policycontroller/PolicyCreation.java | 38 +++--- .../Decision_GuardMinMaxPolicyTemplate.xml | 138 +++++++++++++++++++++ .../org/onap/policy/pap/test/XACMLPAPTest.java | 37 ++++++ 4 files changed, 226 insertions(+), 16 deletions(-) create mode 100644 ONAP-PAP-REST/src/main/resources/Decision_GuardMinMaxPolicyTemplate.xml (limited to 'ONAP-PAP-REST') diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java index 47ab45894..03fbe0763 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/components/DecisionPolicy.java @@ -90,9 +90,11 @@ public class DecisionPolicy extends Policy { 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_MIN_MAX = "GUARD_MIN_MAX"; 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 XACML_GUARD_MIN_MAX_TEMPLATE = "Decision_GuardMinMaxPolicyTemplate.xml"; private static final String ONAPNAME = "ONAPName"; private static final String POLICY_NAME = "PolicyName"; @@ -194,8 +196,10 @@ public class DecisionPolicy extends Policy { } policyName = policyAdapter.getNewFileName(); - if (policyAdapter.getRuleProvider().equals(GUARD_YAML) - || policyAdapter.getRuleProvider().equals(GUARD_BL_YAML)) { + if(policyAdapter.getRuleProvider().equals(GUARD_YAML) || + policyAdapter.getRuleProvider().equals(GUARD_BL_YAML) || + policyAdapter.getRuleProvider().equals(GUARD_MIN_MAX)){ + Map yamlParams = new HashMap<>(); String blackListEntryType = policyAdapter.getBlackListEntryType() != null ? policyAdapter.getBlackListEntryType() : "Use Manual Entry"; @@ -353,6 +357,12 @@ public class DecisionPolicy extends Policy { } cons.setBlacklist(blackList); break; + case GUARD_MIN_MAX: + templateFile = new File(classLoader.getResource(XACML_GUARD_MIN_MAX_TEMPLATE).getFile()); + xacmlTemplatePath = templateFile.toPath(); + cons = new Constraint(Integer.parseInt(yamlParams.get("min")), + Integer.parseInt(yamlParams.get("max")), activeTimeRange); + break; default: templateFile = new File(classLoader.getResource(XACML_GUARD_TEMPLATE).getFile()); xacmlTemplatePath = templateFile.toPath(); @@ -372,6 +382,7 @@ public class DecisionPolicy extends Policy { cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), timeWindow, activeTimeRange); break; } + builder = builder.addLimitConstraint(policy1.getId(), cons); // Build the specification Results results = builder.buildSpecification(); @@ -399,6 +410,18 @@ public class DecisionPolicy extends Policy { yamlSpecs.put("twUnits", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst() .getTime_window().get("units")); } + + if (yamlGuardObject.getGuards().getFirst().getLimit_constraints(). + getFirst().getMaxVnfCount() != null) { + yamlSpecs.put("max", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst() + .getMaxVnfCount().toString()); + } + if (yamlGuardObject.getGuards().getFirst().getLimit_constraints(). + getFirst().getMinVnfCount() != null) { + yamlSpecs.put("min", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst() + .getMinVnfCount().toString()); + } + yamlSpecs.put("guardActiveStart", yamlGuardObject.getGuards().getFirst().getLimit_constraints() .getFirst().getActive_time_range().get("start")); yamlSpecs.put("guardActiveEnd", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst() @@ -406,9 +429,11 @@ public class DecisionPolicy extends Policy { 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) { LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage(), diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java index 951f25c25..de5d1cf49 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/policycontroller/PolicyCreation.java @@ -402,46 +402,56 @@ 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){ + if (policyData.getRuleProvider() != null + && (policyData.getRuleProvider().equals(DecisionPolicy.GUARD_YAML) + || policyData.getRuleProvider().equals(DecisionPolicy.GUARD_BL_YAML) + || policyData.getRuleProvider().equals(DecisionPolicy.GUARD_MIN_MAX)) + && policyData.getYamlparams() != null) { attributeMap.put("actor", policyData.getYamlparams().getActor()); attributeMap.put("recipe", policyData.getYamlparams().getRecipe()); attributeMap.put("clname", policyData.getYamlparams().getClname()); attributeMap.put("limit", policyData.getYamlparams().getLimit()); + attributeMap.put("min", policyData.getYamlparams().getMin()); + attributeMap.put("max", policyData.getYamlparams().getMax()); attributeMap.put("timeWindow", policyData.getYamlparams().getTimeWindow()); attributeMap.put("timeUnits", policyData.getYamlparams().getTimeUnits()); attributeMap.put("guardActiveStart", policyData.getYamlparams().getGuardActiveStart()); attributeMap.put("guardActiveEnd", policyData.getYamlparams().getGuardActiveEnd()); - if(policyData.getYamlparams().getBlackList()!=null){ + if (policyData.getYamlparams().getBlackList() != null) { String blackList = StringUtils.join(policyData.getYamlparams().getBlackList(), ","); attributeMap.put("blackList", blackList); } - if(DecisionPolicy.GUARD_BL_YAML.equals(policyData.getRuleProvider()) && "Use File Upload".equals(policyData.getBlackListEntryType())){ - if(policyData.getBlackListEntries() != null && !policyData.getBlackListEntries().isEmpty()){ + if (DecisionPolicy.GUARD_BL_YAML.equals(policyData.getRuleProvider()) + && "Use File Upload".equals(policyData.getBlackListEntryType())) { + if (policyData.getBlackListEntries() != null + && !policyData.getBlackListEntries().isEmpty()) { String blackList = StringUtils.join(policyData.getBlackListEntries(), ","); attributeMap.put("blackList", blackList); } - if(policyData.getAppendBlackListEntries() != null && !policyData.getAppendBlackListEntries().isEmpty()){ + if (policyData.getAppendBlackListEntries() != null + && !policyData.getAppendBlackListEntries().isEmpty()) { String blackList = StringUtils.join(policyData.getAppendBlackListEntries(), ","); attributeMap.put("appendBlackList", blackList); } } - if(policyData.getYamlparams().getTargets()!=null){ - String targets = StringUtils.join(policyData.getYamlparams().getTargets(),","); + if (policyData.getYamlparams().getTargets() != null) { + String targets = StringUtils.join(policyData.getYamlparams().getTargets(), ","); attributeMap.put("targets", targets); } } - if(policyData.getRuleProvider()!=null && policyData.getRuleProvider().equals(DecisionPolicy.RAINY_DAY)){ + 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(); + if (policyData.getRainyday().getTreatmentTableChoices() != null + && policyData.getRainyday().getTreatmentTableChoices().isEmpty()) { + 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); } } diff --git a/ONAP-PAP-REST/src/main/resources/Decision_GuardMinMaxPolicyTemplate.xml b/ONAP-PAP-REST/src/main/resources/Decision_GuardMinMaxPolicyTemplate.xml new file mode 100644 index 000000000..afb118aa5 --- /dev/null +++ b/ONAP-PAP-REST/src/main/resources/Decision_GuardMinMaxPolicyTemplate.xml @@ -0,0 +1,138 @@ + + + + ${description} + + + + + ${PolicyName} + + + + + + ${ONAPName} + + + + (?i)${actor} + + + + (?i)${recipe} + + + + ${targets} + + + + ${clname} + + + + + + + + + + + DECIDE + + + + + + + + + + + + + ${guardActiveStart} + ${guardActiveEnd} + + + + + + ${min} + + + + + + ${max} + + + + + + + + + + + DECIDE + + + + + + + + + + + + + + ${guardActiveStart} + ${guardActiveEnd} + + + + + + ${min} + + + + + + ${max} + + + + + + + + + Denied By Guard + + + + + \ No newline at end of file diff --git a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java index 4ba72afcb..00553e0ee 100644 --- a/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java +++ b/ONAP-PAP-REST/src/test/java/org/onap/policy/pap/test/XACMLPAPTest.java @@ -408,6 +408,43 @@ public class XACMLPAPTest { Mockito.verify(httpServletResponse).addHeader("successMapKey", "success"); Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml"); } + + @Test + public void testDecisonGuardMinMaxPolicy() throws IOException, ServletException, SQLException { + httpServletRequest = Mockito.mock(HttpServletRequest.class); + Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL"); + Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT"); + Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api"); + Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create"); + Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision"); + Map matchingAttributes = new HashMap<>(); + matchingAttributes.put("actor", "test"); + matchingAttributes.put("recipe", "scaleOut"); + matchingAttributes.put("targets", "test,test1"); + matchingAttributes.put("clname", "test"); + matchingAttributes.put("min", "1"); + matchingAttributes.put("max", "5"); + matchingAttributes.put("guardActiveStart", "05:00"); + matchingAttributes.put("guardActiveEnd", "10:00"); + StdPAPPolicy newPAPPolicy = + + new StdPAPPolicy( + StdPAPPolicyParams.builder().policyName("testGuard").description("test rule").onapName("PDPD") + .providerComboBox("GUARD_MIN_MAX").dynamicFieldConfigAttributes(matchingAttributes) + .editPolicy(false).domain("test").highestVersion(0).build()); + MockServletInputStream mockInput = new MockServletInputStream( + PolicyUtils.objectToJsonString(newPAPPolicy).getBytes()); + Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput); + + // set DBDao + setDBDao(); + pap.service(httpServletRequest, httpServletResponse); + + Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK); + Mockito.verify(httpServletResponse).addHeader("successMapKey", "success"); + Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml"); + } + @Test public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException { -- cgit 1.2.3-korg