diff options
author | ITSERVICES\rb7147 <rb7147@att.com> | 2017-05-08 22:20:44 -0400 |
---|---|---|
committer | ITSERVICES\rb7147 <rb7147@att.com> | 2017-05-09 13:58:03 -0400 |
commit | dda032f8bb161d54eb1f59de2b4a3efb774fc4d1 (patch) | |
tree | 9a11825d59434d97bb0c7dcbf00a0b84e7e5f526 /ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components | |
parent | a330af579866dacbe595e2e4ad1dd29cd3c96945 (diff) |
Policy 1707 Second commit
Change-Id: I18f5b142238733d17280cf17c3d1dd28204d34e9
Signed-off-by: ITSERVICES\rb7147 <rb7147@att.com>
Diffstat (limited to 'ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components')
2 files changed, 147 insertions, 14 deletions
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 b7b47cf28..4c67d798a 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,44 @@ package org.openecomp.policy.pap.xacml.rest.components; +import java.io.ByteArrayInputStream; +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.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.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,22 +76,13 @@ 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.openecomp.policy.common.logging.eelf.MessageCodes; -import org.openecomp.policy.common.logging.eelf.PolicyLogger; -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.std.pip.engines.aaf.AAFEngine; - -import com.att.research.xacml.std.IdentifierImpl; - 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"; + private static final String XACMLTEMPLATE = "Decision_GuardPolicyTemplate.xml"; + List<String> dynamicLabelRuleAlgorithms = new LinkedList<String>(); List<String> dynamicFieldComboRuleAlgorithms = new LinkedList<String>(); @@ -131,7 +148,25 @@ public class DecisionPolicy extends Policy { } policyName = policyAdapter.getNewFileName(); - if (policyAdapter.getData() != null) { + if(policyAdapter.getRuleProvider().equals(GUARD_YAML)){ + Map<String, String> yamlParams = new HashMap<String, String>(); + 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()); + } + yamlParams.put("PolicyName", name); + yamlParams.put("ECOMPName", policyAdapter.getEcompName()); + Map<String, String> params = policyAdapter.getDynamicFieldConfigAttributes(); + yamlParams.putAll(params); + // Call YAML to XACML + PolicyType decisionPolicy = getGuardPolicy(yamlParams); + 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()); @@ -174,7 +209,7 @@ public class DecisionPolicy extends Policy { Map<String, String> dynamicFieldDecisionSettings = policyAdapter.getDynamicSettingsMap(); //dynamicVariableList = policyAdapter.getDynamicVariableList(); - if(policyAdapter.getProviderComboBox()!=null && policyAdapter.getProviderComboBox().equals(AAFProvider)){ + if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){ dynamicFieldDecisionSettings = new HashMap<String,String>(); } @@ -196,6 +231,47 @@ public class DecisionPolicy extends Policy { return true; } + public PolicyType getGuardPolicy(Map<String, String> yamlParams) { + 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")); + builder = builder.addGuardPolicy(policy1); + Map<String, String> time_in_range = new HashMap<String, String>(); + 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); + // 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<String, String> yamlSpecs = new HashMap<String, String>(); + 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. + 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()); + } + } catch (BuilderException e) { + PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage()); + } + return null; + } + private DecisionSettings findDecisionSettingsBySettingId(String settingId) { DecisionSettings decisionSetting = null; diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/SafePolicyBuilder.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/SafePolicyBuilder.java new file mode 100644 index 000000000..7637719b1 --- /dev/null +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/SafePolicyBuilder.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-PAP-REST + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.openecomp.policy.pap.xacml.rest.components; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +public class SafePolicyBuilder { + + public static ControlLoopGuard loadYamlGuard(String specification) { + // + // Read the yaml into our Java Object + // + PolicyLogger.info("Requested YAML to convert : " + specification); + Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); + Object obj = yaml.load(specification); + return (ControlLoopGuard) obj; + } + + public static String generateXacmlGuard(String xacmlFileContent,Map<String, String> generateMap) { + for(String key: generateMap.keySet()){ + Pattern p = Pattern.compile("\\$\\{" +key +"\\}"); + Matcher m = p.matcher(xacmlFileContent); + String finalInput = generateMap.get(key); + if(finalInput.contains("$")){ + finalInput = finalInput.replace("$", "\\$"); + } + xacmlFileContent=m.replaceAll(finalInput); + } + PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent); + + return xacmlFileContent; + } +} |