* ============LICENSE_START=======================================================
* ONAP-PAP-REST
* ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 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.
package org.onap.policy.pap.xacml.rest.components;
+import com.att.research.xacml.api.XACML3;
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.std.IdentifierImpl;
+
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
+
+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;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
+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.lang3.StringEscapeUtils;
import org.onap.policy.common.logging.eelf.MessageCodes;
import org.onap.policy.common.logging.eelf.PolicyLogger;
import org.onap.policy.controlloop.policy.guard.GuardPolicy;
import org.onap.policy.controlloop.policy.guard.MatchParameters;
import org.onap.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
-import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
import org.onap.policy.rest.adapter.PolicyRestAdapter;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.DecisionSettings;
import org.onap.policy.xacml.api.XACMLErrorConstants;
import org.onap.policy.xacml.std.pip.engines.aaf.AAFEngine;
import org.onap.policy.xacml.util.XACMLPolicyScanner;
-import com.att.research.xacml.api.XACML3;
-import com.att.research.xacml.api.pap.PAPException;
-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;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
-import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
-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.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+@Component
public class DecisionPolicy extends Policy {
private static final Logger LOGGER = FlexLogger.getLogger(DecisionPolicy.class);
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";
+ public static final String MS_MODEL = "MicroService_Model";
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 POLICY_NAME = "PolicyName";
private static final String DESCRIPTION = "description";
-
List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
List<String> dynamicFieldComboRuleAlgorithms = new LinkedList<>();
List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
List<String> dataTypeList = new LinkedList<>();
- private CommonClassDao commonClassDao;
+ private static CommonClassDao commonClassDao;
public DecisionPolicy() {
super();
}
- public DecisionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao) {
+ @Autowired
+ public DecisionPolicy(CommonClassDao commonClassDao) {
+ DecisionPolicy.commonClassDao = commonClassDao;
+ }
+
+ public DecisionPolicy(PolicyRestAdapter policyAdapter) {
this.policyAdapter = policyAdapter;
this.commonClassDao = commonClassDao;
}
successMap = createPolicy(newPolicyPath, getCorrectPolicyDataObject());
return successMap;
}
-
+
/**
* Scan the Raw Policy data and set to PolicyAdapter.
*/
private void readRawPolicyData() {
Object policy;
if ("API".equalsIgnoreCase(policyAdapter.getApiflag())) {
- policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(StringEscapeUtils
- .unescapeXml(policyAdapter.getRawXacmlPolicy()).getBytes(StandardCharsets.UTF_8)));
+ policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(
+ StringEscapeUtils.unescapeXml(policyAdapter.getRawXacmlPolicy()).getBytes(StandardCharsets.UTF_8)));
} else {
policy = XACMLPolicyScanner.readPolicy(
new ByteArrayInputStream(policyAdapter.getRawXacmlPolicy().getBytes(StandardCharsets.UTF_8)));
}
String policyRawDesc;
if (policy instanceof PolicySetType) {
- policyRawDesc =
- ((PolicySetType) policy).getDescription() + "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
+ policyRawDesc = ((PolicySetType) policy).getDescription() + "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
((PolicySetType) policy).setDescription(policyRawDesc);
} else {
policyRawDesc = ((PolicyType) policy).getDescription() + "@#RuleProvider@#Decision_Raw@#RuleProvider@#";
policyAdapter.setPolicyData(policy);
policyAdapter.setData(policy);
setPreparedToSave(true);
-
+
}
// This is the method for preparing the policy for saving. We have broken it out
}
policyName = policyAdapter.getNewFileName();
- if(policyAdapter.getRuleProvider().equals(GUARD_YAML) ||
- policyAdapter.getRuleProvider().equals(GUARD_BL_YAML) ||
- policyAdapter.getRuleProvider().equals(GUARD_MIN_MAX)){
-
+ if (policyAdapter.getRuleProvider().equals(GUARD_YAML) || policyAdapter.getRuleProvider().equals(GUARD_BL_YAML)
+ || policyAdapter.getRuleProvider().equals(GUARD_MIN_MAX)) {
+
Map<String, String> yamlParams = new HashMap<>();
- String blackListEntryType = policyAdapter.getBlackListEntryType() != null
- ? policyAdapter.getBlackListEntryType() : "Use Manual Entry";
+ String blackListEntryType =
+ policyAdapter.getBlackListEntryType() != null ? policyAdapter.getBlackListEntryType()
+ : "Use Manual Entry";
String description = policyAdapter.getPolicyDescription() != null ? policyAdapter.getPolicyDescription()
: "YAML Guard Policy";
yamlParams.put(DESCRIPTION, description + "@blEntry@" + blackListEntryType + "@blEntry@");
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")),
+ cons = new Constraint(Integer.parseInt(yamlParams.get("min")),
Integer.parseInt(yamlParams.get("max")), activeTimeRange);
break;
default:
cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), timeWindow, activeTimeRange);
break;
}
-
+
builder = builder.addLimitConstraint(policy1.getId(), cons);
// Build the specification
Results results = builder.buildSpecification();
yamlSpecs.put(POLICY_NAME, yamlParams.get(POLICY_NAME));
yamlSpecs.put(DESCRIPTION, yamlParams.get(DESCRIPTION));
yamlSpecs.put(ONAPNAME, yamlParams.get(ONAPNAME));
- 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());
+ GuardPolicy guard = yamlGuardObject.getGuards().get(0);
+
+ yamlSpecs.put("actor", guard.getMatch_parameters().getActor());
+ yamlSpecs.put("recipe", guard.getMatch_parameters().getRecipe());
+ yamlSpecs.put("clname", guard.getMatch_parameters().getControlLoopName());
+
+ Constraint constraints = guard.getLimit_constraints().get(0);
+ if (constraints.getFreq_limit_per_target() != null) {
+ yamlSpecs.put("limit", constraints.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"));
+ if (constraints.getTime_window() != null) {
+ yamlSpecs.put("twValue", constraints.getTime_window().get("value"));
+ yamlSpecs.put("twUnits", constraints.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 (constraints.getMaxVnfCount() != null) {
+ yamlSpecs.put("max", constraints.getMaxVnfCount().toString());
}
- if (yamlGuardObject.getGuards().getFirst().getLimit_constraints().
- getFirst().getMinVnfCount() != null) {
- yamlSpecs.put("min", yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst()
- .getMinVnfCount().toString());
+ if (constraints.getMinVnfCount() != null) {
+ yamlSpecs.put("min", constraints.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()
- .getActive_time_range().get("end"));
+ yamlSpecs.put("guardActiveStart", constraints.getActive_time_range().get("start"));
+ yamlSpecs.put("guardActiveEnd", constraints.getActive_time_range().get("end"));
String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent, yamlSpecs,
- yamlGuardObject.getGuards().getFirst().getLimit_constraints().getFirst().getBlacklist(),
- yamlGuardObject.getGuards().getFirst().getMatch_parameters().getTargets());
-
+ constraints.getBlacklist(), guard.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(),
}
private DecisionSettings findDecisionSettingsBySettingId(String settingId) {
- DecisionSettings decisionSetting = null;
-
- EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
- Query getDecisionSettings = em.createNamedQuery("DecisionSettings.findAll");
- List<?> decisionSettingsList = getDecisionSettings.getResultList();
-
- for (Object id : decisionSettingsList) {
- decisionSetting = (DecisionSettings) id;
- if (decisionSetting.getXacmlId().equals(settingId)) {
- break;
- }
- }
- return decisionSetting;
+ return (DecisionSettings) commonClassDao.getEntityItem(DecisionSettings.class, "xacml_id", settingId);
}
private void createRule(PolicyType decisionPolicy, boolean permitRule) {
// check the index for the label.
for (String labelAttr : dynamicLabelRuleAlgorithms) {
if (labelAttr.equals(value1Label)) {
- String value1 = dynamicFieldOneRuleAlgorithms.get(index);
- populateDataTypeList(value1);
+ String attributeId = dynamicFieldOneRuleAlgorithms.get(index);
+ populateDataTypeList(attributeId);
// check if the row contains label again
for (String labelValue : dynamicLabelRuleAlgorithms) {
- if (labelValue.equals(value1)) {
+ if (labelValue.equals(attributeId)) {
return getCompoundDecisionApply(index);
}
}
// Getting the values from the form.
String functionKey = dynamicFieldComboRuleAlgorithms.get(index);
- String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
+ String attributeValue = dynamicFieldTwoRuleAlgorithms.get(index);
decisionApply.setFunctionId(getFunctionDefinitionId(functionKey));
// if two text field are rule attributes.
- if ((value1.contains(RULE_VARIABLE)) && (value2.contains(RULE_VARIABLE))) {
- ApplyType innerDecisionApply1 = new ApplyType();
- ApplyType innerDecisionApply2 = new ApplyType();
- AttributeDesignatorType attributeDesignator1 = new AttributeDesignatorType();
- AttributeDesignatorType attributeDesignator2 = new AttributeDesignatorType();
- // If selected function is Integer function set integer functionID
- if (functionKey.toLowerCase().contains("integer")) {
- innerDecisionApply1.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
- innerDecisionApply2.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
- attributeDesignator1.setDataType(INTEGER_DATATYPE);
- attributeDesignator2.setDataType(INTEGER_DATATYPE);
- } else {
- // If selected function is not a Integer function set String functionID
- innerDecisionApply1.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
- innerDecisionApply2.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
- attributeDesignator1.setDataType(STRING_DATATYPE);
- attributeDesignator2.setDataType(STRING_DATATYPE);
- }
- attributeDesignator1.setCategory(CATEGORY_RESOURCE);
- attributeDesignator2.setCategory(CATEGORY_RESOURCE);
- // Here set actual field values
- attributeDesignator1
- .setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
- attributeDesignator2
- .setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
- innerDecisionApply1.getExpression()
- .add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
- innerDecisionApply2.getExpression()
- .add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
- decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply1));
- decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply2));
+ if ((attributeId.contains(RULE_VARIABLE)) && (attributeValue.contains(RULE_VARIABLE))) {
+ applyTwoTextFieldRuleAttribute(decisionApply, attributeId, functionKey);
} else {
// if either of one text field is rule attribute.
- if (!value1.startsWith("S_")) {
- ApplyType innerDecisionApply = new ApplyType();
- AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
- AttributeValueType decisionConditionAttributeValue = new AttributeValueType();
-
- if (functionKey.toLowerCase().contains("integer")) {
- innerDecisionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
- decisionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
- attributeDesignator.setDataType(INTEGER_DATATYPE);
- } else {
- innerDecisionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
- decisionConditionAttributeValue.setDataType(STRING_DATATYPE);
- attributeDesignator.setDataType(STRING_DATATYPE);
- }
-
- String attributeId = null;
- String attributeValue = null;
-
- // Find which textField has rule attribute and set it as
- // attributeId and the other as attributeValue.
- attributeId = value1;
- attributeValue = value2;
+ if (!attributeId.startsWith("S_")) {
+ ApplyType innerDecisionApply = generateApplyTypeDataType(functionKey);
+ AttributeDesignatorType attributeDesignator = generateAttributeDesignatorDataType(functionKey);
+ AttributeValueType decisionConditionAttributeValue =
+ generateAttributeValueTypeDataType(functionKey);
if (attributeId != null) {
attributeDesignator.setCategory(CATEGORY_RESOURCE);
.add(new ObjectFactory().createAttributeValue(decisionConditionAttributeValue));
decisionApply.getExpression().add(new ObjectFactory().createApply(innerDecisionApply));
} else {
- value1 = value1.substring(2, value1.length());
+ attributeId = attributeId.substring(2, attributeId.length());
VariableReferenceType variableReferenceType = new VariableReferenceType();
- variableReferenceType.setVariableId(value1);
+ variableReferenceType.setVariableId(attributeId);
String dataType = dataTypeList.get(index);
AttributeValueType decisionConditionAttributeValue = new AttributeValueType();
decisionConditionAttributeValue.setDataType(dataType);
- decisionConditionAttributeValue.getContent().add(value2);
+ decisionConditionAttributeValue.getContent().add(attributeValue);
decisionApply.getExpression()
.add(new ObjectFactory().createVariableReference(variableReferenceType));
decisionApply.getExpression()
dataTypeList.add(dataType);
}
-
private String getDataType(String key) {
DecisionSettings decisionSettings = findDecisionSettingsBySettingId(key);
public String getFunctionDefinitionId(String key) {
FunctionDefinition object =
- (FunctionDefinition) commonClassDao.getDataById(FunctionDefinition.class, "shortname", key);
+ (FunctionDefinition) commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", key);
if (object != null) {
return object.getXacmlid();
}
return null;
}
+ private AttributeDesignatorType generateAttributeDesignatorDataType(String functionKey) {
+ AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
+ switch (functionKey.toLowerCase()) {
+ case "integer":
+ attributeDesignator.setDataType(INTEGER_DATATYPE);
+ break;
+ default:
+ attributeDesignator.setDataType(STRING_DATATYPE);
+ }
+ return attributeDesignator;
+ }
+
+ private ApplyType generateApplyTypeDataType(String functionKey) {
+ ApplyType applyType = new ApplyType();
+ switch (functionKey.toLowerCase()) {
+ case "integer":
+ applyType.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
+ break;
+ default:
+ applyType.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
+ }
+ return applyType;
+ }
+
+ private AttributeValueType generateAttributeValueTypeDataType(String functionKey) {
+ AttributeValueType applyType = new AttributeValueType();
+ switch (functionKey.toLowerCase()) {
+ case "integer":
+ applyType.setDataType(INTEGER_DATATYPE);
+ break;
+ default:
+ applyType.setDataType(STRING_DATATYPE);
+ }
+ return applyType;
+ }
+
+ private void applyTwoTextFieldRuleAttribute(ApplyType decisionApply, String value1, String functionKey) {
+ decisionApply.getExpression()
+ .add(new ObjectFactory().createApply(generateApplyTypeDataType(value1, functionKey)));
+ decisionApply.getExpression()
+ .add(new ObjectFactory().createApply(generateApplyTypeDataType(value1, functionKey)));
+ }
+
+ private ApplyType generateApplyTypeDataType(String value1, String functionKey) {
+ ApplyType innerApply = generateApplyTypeDataType(functionKey);
+ AttributeDesignatorType attributeDesignator = generateAttributeDesignatorDataType(functionKey);
+
+ attributeDesignator.setCategory(CATEGORY_RESOURCE);
+ // Here set actual field values
+ attributeDesignator.setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
+ innerApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
+ return innerApply;
+ }
+
}