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.onap.policy.controller;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.List;
31 import javax.xml.bind.JAXBElement;
33 import org.onap.policy.common.logging.flexlogger.FlexLogger;
34 import org.onap.policy.common.logging.flexlogger.Logger;
35 import org.onap.policy.rest.adapter.PolicyRestAdapter;
36 import org.onap.policy.rest.adapter.RainyDayParams;
37 import org.onap.policy.rest.adapter.YAMLParams;
38 import org.onap.policy.rest.jpa.PolicyEntity;
39 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
60 public class DecisionPolicyController extends RestrictedBaseController {
61 private static final Logger policyLogger = FlexLogger.getLogger(DecisionPolicyController.class);
63 public DecisionPolicyController(){}
65 protected PolicyRestAdapter policyAdapter = null;
66 private ArrayList<Object> attributeList;
67 private ArrayList<Object> decisionList;
68 private ArrayList<Object> ruleAlgorithmList;
69 private ArrayList<Object> treatmentList = null;
70 protected LinkedList<Integer> ruleAlgoirthmTracker;
71 public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
73 @SuppressWarnings("unchecked")
74 public void prePopulateDecisionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
75 attributeList = new ArrayList<>();
76 decisionList = new ArrayList<>();
77 ruleAlgorithmList = new ArrayList<>();
78 treatmentList = new ArrayList<>();
80 if (policyAdapter.getPolicyData() instanceof PolicyType) {
81 RainyDayParams rainydayParams = new RainyDayParams();
82 Object policyData = policyAdapter.getPolicyData();
83 PolicyType policy = (PolicyType) policyData;
84 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
85 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("_") + 1);
86 policyAdapter.setPolicyName(policyNameValue);
87 String description = "";
89 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
91 policyLogger.info("General error", e);
92 description = policy.getDescription();
94 policyAdapter.setPolicyDescription(description);
95 // Get the target data under policy for Action.
96 TargetType target = policy.getTarget();
98 // under target we have AnyOFType
99 List<AnyOfType> anyOfList = target.getAnyOf();
100 if (anyOfList != null) {
101 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
102 while (iterAnyOf.hasNext()) {
103 AnyOfType anyOf = iterAnyOf.next();
104 // Under AntOfType we have AllOfType
105 List<AllOfType> allOfList = anyOf.getAllOf();
106 if (allOfList != null) {
107 Iterator<AllOfType> iterAllOf = allOfList.iterator();
108 while (iterAllOf.hasNext()) {
109 AllOfType allOf = iterAllOf.next();
110 // Under AllOfType we have Mathch.
111 List<MatchType> matchList = allOf.getMatch();
113 if (matchList != null) {
114 Iterator<MatchType> iterMatch = matchList.iterator();
115 while (iterMatch.hasNext()) {
116 MatchType match = iterMatch.next();
118 // Under the match we have attributevalue and
119 // attributeDesignator. So,finally down to the actual attribute.
121 AttributeValueType attributeValue = match.getAttributeValue();
122 String value = (String) attributeValue.getContent().get(0);
124 value = value.replaceAll("\\(\\?i\\)", "");
126 AttributeDesignatorType designator = match.getAttributeDesignator();
127 String attributeId = designator.getAttributeId();
128 // First match in the target is OnapName, so set that value.
129 if (attributeId.equals("ONAPName")) {
130 policyAdapter.setOnapName(value);
132 // Component attributes are saved under Target here we are fetching them back.
133 // One row is default so we are not adding dynamic component at index 0.
135 Map<String, String> attribute = new HashMap<>();
136 attribute.put("key", attributeId);
137 attribute.put("value", value);
138 attributeList.add(attribute);
143 policyAdapter.setAttributes(attributeList);
147 // Setting rainy day attributes to the parameters object if they exist
148 boolean rainy = false;
149 if(!attributeList.isEmpty()) {
150 for(int i=0; i<attributeList.size() ; i++){
151 Map<String, String> map = (Map<String,String>)attributeList.get(i);
152 if(map.get("key").equals("WorkStep")){
153 rainydayParams.setWorkstep(map.get("value"));
155 }else if(map.get("key").equals("BB_ID")){
156 rainydayParams.setBbid(map.get("value"));
158 }else if(map.get("key").equals("ServiceType")){
159 rainydayParams.setServiceType(map.get("value"));
161 }else if(map.get("key").equals("VNFType")){
162 rainydayParams.setVnfType(map.get("value"));
168 policyAdapter.setRuleProvider("Rainy_Day");
172 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
174 for (Object object : ruleList) {
175 if (object instanceof VariableDefinitionType) {
176 VariableDefinitionType variableDefinitionType = (VariableDefinitionType) object;
177 Map<String, String> settings = new HashMap<>();
178 settings.put("key", variableDefinitionType.getVariableId());
179 JAXBElement<AttributeValueType> attributeValueTypeElement = (JAXBElement<AttributeValueType>) variableDefinitionType.getExpression();
180 if (attributeValueTypeElement != null) {
181 AttributeValueType attributeValueType = attributeValueTypeElement.getValue();
182 settings.put("value", attributeValueType.getContent().get(0).toString());
184 decisionList.add(settings);
185 } else if (object instanceof RuleType) {
186 // get the condition data under the rule for rule Algorithms.
187 if(((RuleType) object).getEffect().equals(EffectType.DENY)) {
188 if(((RuleType) object).getAdviceExpressions()!=null){
189 if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("AAF")){
190 policyAdapter.setRuleProvider("AAF");
192 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_YAML")){
193 policyAdapter.setRuleProvider("GUARD_YAML");
194 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_BL_YAML")){
195 policyAdapter.setRuleProvider("GUARD_BL_YAML");
198 policyAdapter.setRuleProvider("Custom");
200 ConditionType condition = ((RuleType) object).getCondition();
201 if (condition != null) {
202 ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
203 decisionApply = (ApplyType) decisionApply.getExpression().get(0).getValue();
204 ruleAlgoirthmTracker = new LinkedList<>();
205 if(policyAdapter.getRuleProvider()!=null && (policyAdapter.getRuleProvider().equals("GUARD_YAML")||(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")))){
206 YAMLParams yamlParams = new YAMLParams();
207 for(int i=0; i<attributeList.size() ; i++){
208 Map<String, String> map = (Map<String,String>)attributeList.get(i);
209 if("actor".equals(map.get("key"))){
210 yamlParams.setActor(map.get("value"));
211 }else if("recipe".equals(map.get("key"))){
212 yamlParams.setRecipe(map.get("value"));
213 }else if("target".equals(map.get("key"))){
214 yamlParams.setTargets(Arrays.asList(map.get("value").split("\\|")));
215 }else if("clname".equals(map.get("key"))){
216 yamlParams.setClname(map.get("value"));
219 ApplyType apply = ((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(0).getValue());
220 yamlParams.setGuardActiveStart(((AttributeValueType)apply.getExpression().get(1).getValue()).getContent().get(0).toString());
221 yamlParams.setGuardActiveEnd(((AttributeValueType)apply.getExpression().get(2).getValue()).getContent().get(0).toString());
222 if(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")){
223 apply = (ApplyType)((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue()).getExpression().get(2).getValue();
224 Iterator<JAXBElement<?>> attributes = apply.getExpression().iterator();
225 List<String> blackList = new ArrayList<>();
226 while(attributes.hasNext()){
227 blackList.add(((AttributeValueType)attributes.next().getValue()).getContent().get(0).toString());
229 yamlParams.setBlackList(blackList);
231 ApplyType timeWindowSection = (ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue();
232 yamlParams.setLimit(((AttributeValueType)timeWindowSection.getExpression().get(1).getValue()).getContent().get(0).toString());
233 String timeWindow = ((AttributeDesignatorType)((ApplyType)timeWindowSection.getExpression().get(0).getValue()).getExpression().get(0).getValue()).getIssuer();
234 yamlParams.setTimeUnits(timeWindow.substring(timeWindow.lastIndexOf(':')+1));
235 yamlParams.setTimeWindow(timeWindow.substring(timeWindow.indexOf(":tw:")+4,timeWindow.lastIndexOf(':')));
237 policyAdapter.setYamlparams(yamlParams);
238 policyAdapter.setAttributes(new ArrayList<Object>());
239 policyAdapter.setRuleAlgorithmschoices(new ArrayList<Object>());
242 // Populating Rule Algorithms starting from compound.
243 prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
244 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
246 } else if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals("Rainy_Day")&& ((RuleType) object).getEffect().equals(EffectType.PERMIT)) {
248 TargetType ruleTarget = ((RuleType) object).getTarget();
249 AdviceExpressionsType adviceExpression = ((RuleType) object).getAdviceExpressions();
251 String errorcode = ruleTarget.getAnyOf().get(0).getAllOf().get(0).getMatch().
252 get(1).getAttributeValue().getContent().get(0).toString();
253 JAXBElement<AttributeValueType> tempTreatmentObj = (JAXBElement<AttributeValueType>) adviceExpression.getAdviceExpression().
254 get(0).getAttributeAssignmentExpression().get(0).getExpression();
255 String treatment = tempTreatmentObj.getValue().getContent().get(0).toString();
257 prePopulateRainyDayTreatments(errorcode, treatment);
264 rainydayParams.setTreatmentTableChoices(treatmentList);
265 policyAdapter.setRainyday(rainydayParams);
266 policyAdapter.setSettings(decisionList);
271 private void prePopulateRainyDayTreatments(String errorcode, String treatment) {
272 Map<String, String> ruleMap = new HashMap<>();
274 ruleMap.put("errorcode", errorcode);
275 ruleMap.put("treatment", treatment);
276 treatmentList.add(ruleMap);
280 private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
281 Map<String, String> ruleMap = new HashMap<>();
282 ruleMap.put("id", "A" + (index +1));
283 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
284 for (String key : dropDownMap.keySet()) {
285 String keyValue = dropDownMap.get(key);
286 if (keyValue.equals(decisionApply.getFunctionId())) {
287 ruleMap.put("dynamicRuleAlgorithmCombo", key);
290 // Populate the key and value fields
291 if (((jaxbDecisionTypes.get(0).getValue()) instanceof AttributeValueType)) {
292 ApplyType innerDecisionApply = (ApplyType) jaxbDecisionTypes.get(1).getValue();
293 List<JAXBElement<?>> jaxbInnerDecisionTypes = innerDecisionApply.getExpression();
294 if (jaxbInnerDecisionTypes.get(0).getValue() instanceof AttributeDesignatorType) {
295 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerDecisionTypes.get(0).getValue();
296 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
298 // Get from Attribute Value
299 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(0).getValue();
300 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
301 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
303 } else if ((jaxbDecisionTypes.get(0).getValue()) instanceof VariableReferenceType) {
304 VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();
305 ruleMap.put("dynamicRuleAlgorithmField1", "S_"+ variableReference.getVariableId());
308 // Get from Attribute Value
309 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
310 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
311 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
313 ruleAlgorithmList.add(ruleMap);
316 private int prePopulateDecisionCompoundRuleAlgorithm(int index, ApplyType decisionApply) {
317 boolean isCompoundRule = true;
318 List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
319 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
320 // If There is Attribute Value under Decision Type that means we came to the final child
321 if (policyLogger.isDebugEnabled()) {
322 policyLogger.debug("Prepopulating rule algoirthm: " + index);
324 // Check to see if Attribute Value exists, if yes then it is not a compound rule
325 if(jaxbElement.getValue() instanceof AttributeValueType) {
326 prePopulateDecisionRuleAlgorithms(index, decisionApply, jaxbDecisionTypes);
327 ruleAlgoirthmTracker.addLast(index);
328 isCompoundRule = false;
332 if (isCompoundRule) {
333 // As it's compound rule, Get the Apply types
334 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
335 ApplyType innerDecisionApply = (ApplyType) jaxbElement.getValue();
336 index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
338 // Populate combo box
339 if (policyLogger.isDebugEnabled()) {
340 policyLogger.debug("Prepopulating Compound rule algorithm: " + index);
342 Map<String, String> rule = new HashMap<>();
343 for (String key : PolicyController.getDropDownMap().keySet()) {
344 String keyValue = PolicyController.getDropDownMap().get(key);
345 if (keyValue.equals(decisionApply.getFunctionId())) {
346 rule.put("dynamicRuleAlgorithmCombo", key);
351 rule.put("id", "A" + (index +1));
352 // Populate Key and values for Compound Rule
353 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
354 ruleAlgoirthmTracker.removeLast();
355 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
356 ruleAlgoirthmTracker.removeLast();
357 ruleAlgoirthmTracker.addLast(index);
358 ruleAlgorithmList.add(rule);