2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.controller;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
30 import javax.xml.bind.JAXBElement;
32 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
33 import org.openecomp.policy.common.logging.flexlogger.Logger;
34 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
35 import org.openecomp.policy.rest.adapter.YAMLParams;
36 import org.openecomp.policy.rest.jpa.PolicyEntity;
37 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
57 public class DecisionPolicyController extends RestrictedBaseController {
58 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyController.class);
60 public DecisionPolicyController(){}
62 protected PolicyRestAdapter policyAdapter = null;
63 private ArrayList<Object> attributeList;
64 private ArrayList<Object> decisionList;
65 private ArrayList<Object> ruleAlgorithmList;
66 protected LinkedList<Integer> ruleAlgoirthmTracker;
67 public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
69 @SuppressWarnings("unchecked")
70 public void prePopulateDecisionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
71 attributeList = new ArrayList<>();
72 decisionList = new ArrayList<>();
73 ruleAlgorithmList = new ArrayList<>();
74 if (policyAdapter.getPolicyData() instanceof PolicyType) {
75 Object policyData = policyAdapter.getPolicyData();
76 PolicyType policy = (PolicyType) policyData;
77 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
78 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("_") + 1);
79 policyAdapter.setPolicyName(policyNameValue);
80 String description = "";
82 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
84 description = policy.getDescription();
86 policyAdapter.setPolicyDescription(description);
87 // Get the target data under policy for Action.
88 TargetType target = policy.getTarget();
90 // under target we have AnyOFType
91 List<AnyOfType> anyOfList = target.getAnyOf();
92 if (anyOfList != null) {
93 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
94 while (iterAnyOf.hasNext()) {
95 AnyOfType anyOf = iterAnyOf.next();
96 // Under AntOfType we have AllOfType
97 List<AllOfType> allOfList = anyOf.getAllOf();
98 if (allOfList != null) {
99 Iterator<AllOfType> iterAllOf = allOfList.iterator();
100 while (iterAllOf.hasNext()) {
101 AllOfType allOf = iterAllOf.next();
102 // Under AllOfType we have Mathch.
103 List<MatchType> matchList = allOf.getMatch();
105 if (matchList != null) {
106 Iterator<MatchType> iterMatch = matchList.iterator();
107 while (iterMatch.hasNext()) {
108 MatchType match = iterMatch.next();
110 // Under the match we have attributevalue and
111 // attributeDesignator. So,finally down to the actual attribute.
113 AttributeValueType attributeValue = match.getAttributeValue();
114 String value = (String) attributeValue.getContent().get(0);
115 AttributeDesignatorType designator = match.getAttributeDesignator();
116 String attributeId = designator.getAttributeId();
117 // First match in the target is EcompName, so set that value.
118 if (attributeId.equals("ECOMPName")) {
119 policyAdapter.setEcompName(value);
121 // Component attributes are saved under Target here we are fetching them back.
122 // One row is default so we are not adding dynamic component at index 0.
124 Map<String, String> attribute = new HashMap<>();
125 attribute.put("key", attributeId);
126 attribute.put("value", value);
127 attributeList.add(attribute);
132 policyAdapter.setAttributes(attributeList);
138 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
140 for (Object object : ruleList) {
141 if (object instanceof VariableDefinitionType) {
142 VariableDefinitionType variableDefinitionType = (VariableDefinitionType) object;
143 Map<String, String> settings = new HashMap<String, String>();
144 settings.put("key", variableDefinitionType.getVariableId());
145 JAXBElement<AttributeValueType> attributeValueTypeElement = (JAXBElement<AttributeValueType>) variableDefinitionType.getExpression();
146 if (attributeValueTypeElement != null) {
147 AttributeValueType attributeValueType = attributeValueTypeElement.getValue();
148 settings.put("value", attributeValueType.getContent().get(0).toString());
150 decisionList.add(settings);
151 } else if (object instanceof RuleType) {
152 // get the condition data under the rule for rule Algorithms.
153 if(((RuleType) object).getEffect().equals(EffectType.DENY)) {
154 if(((RuleType) object).getAdviceExpressions()!=null){
155 if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("AAF")){
156 policyAdapter.setRuleProvider("AAF");
158 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_YAML")){
159 policyAdapter.setRuleProvider("GUARD_YAML");
160 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_BL_YAML")){
161 policyAdapter.setRuleProvider("GUARD_BL_YAML");
164 policyAdapter.setRuleProvider("Custom");
166 ConditionType condition = ((RuleType) object).getCondition();
167 if (condition != null) {
168 ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
169 decisionApply = (ApplyType) decisionApply.getExpression().get(0).getValue();
170 ruleAlgoirthmTracker = new LinkedList<>();
171 if(policyAdapter.getRuleProvider()!=null && (policyAdapter.getRuleProvider().equals("GUARD_YAML")||(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")))){
172 YAMLParams yamlParams = new YAMLParams();
173 for(int i=0; i<attributeList.size() ; i++){
174 Map<String, String> map = (Map<String,String>)attributeList.get(i);
175 if(map.get("key").equals("actor")){
176 yamlParams.setActor(map.get("value"));
177 }else if(map.get("key").equals("recipe")){
178 yamlParams.setRecipe(map.get("value"));
181 ApplyType apply = ((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(0).getValue());
182 yamlParams.setGuardActiveStart(((AttributeValueType)apply.getExpression().get(1).getValue()).getContent().get(0).toString());
183 yamlParams.setGuardActiveEnd(((AttributeValueType)apply.getExpression().get(2).getValue()).getContent().get(0).toString());
184 if(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")){
185 apply = (ApplyType)((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue()).getExpression().get(2).getValue();
186 Iterator<JAXBElement<?>> attributes = apply.getExpression().iterator();
187 List<String> blackList = new ArrayList<>();
188 while(attributes.hasNext()){
189 blackList.add(((AttributeValueType)attributes.next().getValue()).getContent().get(0).toString());
191 yamlParams.setBlackList(blackList);
193 yamlParams.setLimit(((AttributeValueType)((ApplyType)decisionApply.getExpression().get(1).getValue()).getExpression().get(1).getValue()).getContent().get(0).toString());
194 String timeWindow = ((AttributeDesignatorType)((ApplyType)((ApplyType)decisionApply.getExpression().get(1).getValue()).getExpression().get(0).getValue()).getExpression().get(0).getValue()).getIssuer();
195 yamlParams.setTimeWindow(timeWindow.substring(timeWindow.lastIndexOf(":")+1));
197 policyAdapter.setYamlparams(yamlParams);
198 policyAdapter.setAttributes(new ArrayList<Object>());
199 policyAdapter.setRuleAlgorithmschoices(new ArrayList<Object>());
202 // Populating Rule Algorithms starting from compound.
203 prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
204 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
210 policyAdapter.setSettings(decisionList);
215 private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
216 Map<String, String> ruleMap = new HashMap<>();
217 ruleMap.put("id", "A" + (index +1));
218 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
219 for (String key : dropDownMap.keySet()) {
220 String keyValue = dropDownMap.get(key);
221 if (keyValue.equals(decisionApply.getFunctionId())) {
222 ruleMap.put("dynamicRuleAlgorithmCombo", key);
225 // Populate the key and value fields
226 if (((jaxbDecisionTypes.get(0).getValue()) instanceof AttributeValueType)) {
227 ApplyType innerDecisionApply = (ApplyType) jaxbDecisionTypes.get(1).getValue();
228 List<JAXBElement<?>> jaxbInnerDecisionTypes = innerDecisionApply.getExpression();
229 if (jaxbInnerDecisionTypes.get(0).getValue() instanceof AttributeDesignatorType) {
230 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerDecisionTypes.get(0).getValue();
231 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
233 // Get from Attribute Value
234 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(0).getValue();
235 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
236 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
238 } else if ((jaxbDecisionTypes.get(0).getValue()) instanceof VariableReferenceType) {
239 VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();
240 ruleMap.put("dynamicRuleAlgorithmField1", "S_"+ variableReference.getVariableId());
243 // Get from Attribute Value
244 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
245 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
246 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
248 ruleAlgorithmList.add(ruleMap);
251 private int prePopulateDecisionCompoundRuleAlgorithm(int index, ApplyType decisionApply) {
252 boolean isCompoundRule = true;
253 List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
254 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
255 // If There is Attribute Value under Decision Type that means we came to the final child
256 if (logger.isDebugEnabled()) {
257 logger.debug("Prepopulating rule algoirthm: " + index);
259 // Check to see if Attribute Value exists, if yes then it is not a compound rule
260 if(jaxbElement.getValue() instanceof AttributeValueType) {
261 prePopulateDecisionRuleAlgorithms(index, decisionApply, jaxbDecisionTypes);
262 ruleAlgoirthmTracker.addLast(index);
263 isCompoundRule = false;
267 if (isCompoundRule) {
268 // As it's compound rule, Get the Apply types
269 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
270 ApplyType innerDecisionApply = (ApplyType) jaxbElement.getValue();
271 index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
273 // Populate combo box
274 if (logger.isDebugEnabled()) {
275 logger.debug("Prepopulating Compound rule algorithm: " + index);
277 Map<String, String> rule = new HashMap<>();
278 for (String key : PolicyController.getDropDownMap().keySet()) {
279 String keyValue = PolicyController.getDropDownMap().get(key);
280 if (keyValue.equals(decisionApply.getFunctionId())) {
281 rule.put("dynamicRuleAlgorithmCombo", key);
286 rule.put("id", "A" + (index +1));
287 // Populate Key and values for Compound Rule
288 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
289 ruleAlgoirthmTracker.removeLast();
290 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
291 ruleAlgoirthmTracker.removeLast();
292 ruleAlgoirthmTracker.addLast(index);
293 ruleAlgorithmList.add(rule);