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);
123 AttributeDesignatorType designator = match.getAttributeDesignator();
124 String attributeId = designator.getAttributeId();
125 // First match in the target is OnapName, so set that value.
126 if (attributeId.equals("ONAPName")) {
127 policyAdapter.setOnapName(value);
129 // Component attributes are saved under Target here we are fetching them back.
130 // One row is default so we are not adding dynamic component at index 0.
132 Map<String, String> attribute = new HashMap<>();
133 attribute.put("key", attributeId);
134 attribute.put("value", value);
135 attributeList.add(attribute);
140 policyAdapter.setAttributes(attributeList);
144 // Setting rainy day attributes to the parameters object if they exist
145 boolean rainy = false;
146 if(!attributeList.isEmpty()) {
147 for(int i=0; i<attributeList.size() ; i++){
148 Map<String, String> map = (Map<String,String>)attributeList.get(i);
149 if(map.get("key").equals("WorkStep")){
150 rainydayParams.setWorkstep(map.get("value"));
152 }else if(map.get("key").equals("BB_ID")){
153 rainydayParams.setBbid(map.get("value"));
155 }else if(map.get("key").equals("ServiceType")){
156 rainydayParams.setServiceType(map.get("value"));
158 }else if(map.get("key").equals("VNFType")){
159 rainydayParams.setVnfType(map.get("value"));
165 policyAdapter.setRuleProvider("Rainy_Day");
169 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
171 for (Object object : ruleList) {
172 if (object instanceof VariableDefinitionType) {
173 VariableDefinitionType variableDefinitionType = (VariableDefinitionType) object;
174 Map<String, String> settings = new HashMap<>();
175 settings.put("key", variableDefinitionType.getVariableId());
176 JAXBElement<AttributeValueType> attributeValueTypeElement = (JAXBElement<AttributeValueType>) variableDefinitionType.getExpression();
177 if (attributeValueTypeElement != null) {
178 AttributeValueType attributeValueType = attributeValueTypeElement.getValue();
179 settings.put("value", attributeValueType.getContent().get(0).toString());
181 decisionList.add(settings);
182 } else if (object instanceof RuleType) {
183 // get the condition data under the rule for rule Algorithms.
184 if(((RuleType) object).getEffect().equals(EffectType.DENY)) {
185 if(((RuleType) object).getAdviceExpressions()!=null){
186 if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("AAF")){
187 policyAdapter.setRuleProvider("AAF");
189 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_YAML")){
190 policyAdapter.setRuleProvider("GUARD_YAML");
191 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_BL_YAML")){
192 policyAdapter.setRuleProvider("GUARD_BL_YAML");
195 policyAdapter.setRuleProvider("Custom");
197 ConditionType condition = ((RuleType) object).getCondition();
198 if (condition != null) {
199 ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
200 decisionApply = (ApplyType) decisionApply.getExpression().get(0).getValue();
201 ruleAlgoirthmTracker = new LinkedList<>();
202 if(policyAdapter.getRuleProvider()!=null && (policyAdapter.getRuleProvider().equals("GUARD_YAML")||(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")))){
203 YAMLParams yamlParams = new YAMLParams();
204 for(int i=0; i<attributeList.size() ; i++){
205 Map<String, String> map = (Map<String,String>)attributeList.get(i);
206 if(map.get("key").equals("actor")){
207 yamlParams.setActor(map.get("value"));
208 }else if(map.get("key").equals("recipe")){
209 yamlParams.setRecipe(map.get("value"));
210 }else if(map.get("key").equals("targets")){
211 yamlParams.setTargets(Arrays.asList(map.get("value").split("\\|")));
212 }else if(map.get("key").equals("clname")){
213 yamlParams.setClname(map.get("value"));
216 ApplyType apply = ((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(0).getValue());
217 yamlParams.setGuardActiveStart(((AttributeValueType)apply.getExpression().get(1).getValue()).getContent().get(0).toString());
218 yamlParams.setGuardActiveEnd(((AttributeValueType)apply.getExpression().get(2).getValue()).getContent().get(0).toString());
219 if(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")){
220 apply = (ApplyType)((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue()).getExpression().get(2).getValue();
221 Iterator<JAXBElement<?>> attributes = apply.getExpression().iterator();
222 List<String> blackList = new ArrayList<>();
223 while(attributes.hasNext()){
224 blackList.add(((AttributeValueType)attributes.next().getValue()).getContent().get(0).toString());
226 yamlParams.setBlackList(blackList);
228 ApplyType timeWindowSection = (ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue();
229 yamlParams.setLimit(((AttributeValueType)timeWindowSection.getExpression().get(1).getValue()).getContent().get(0).toString());
230 String timeWindow = ((AttributeDesignatorType)((ApplyType)timeWindowSection.getExpression().get(0).getValue()).getExpression().get(0).getValue()).getIssuer();
231 yamlParams.setTimeUnits(timeWindow.substring(timeWindow.lastIndexOf(':')+1));
232 yamlParams.setTimeWindow(timeWindow.substring(timeWindow.indexOf(":tw:")+4,timeWindow.lastIndexOf(':')));
234 policyAdapter.setYamlparams(yamlParams);
235 policyAdapter.setAttributes(new ArrayList<Object>());
236 policyAdapter.setRuleAlgorithmschoices(new ArrayList<Object>());
239 // Populating Rule Algorithms starting from compound.
240 prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
241 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
243 } else if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals("Rainy_Day")&& ((RuleType) object).getEffect().equals(EffectType.PERMIT)) {
245 TargetType ruleTarget = ((RuleType) object).getTarget();
246 AdviceExpressionsType adviceExpression = ((RuleType) object).getAdviceExpressions();
248 String errorcode = ruleTarget.getAnyOf().get(0).getAllOf().get(0).getMatch().
249 get(1).getAttributeValue().getContent().get(0).toString();
250 JAXBElement<AttributeValueType> tempTreatmentObj = (JAXBElement<AttributeValueType>) adviceExpression.getAdviceExpression().
251 get(0).getAttributeAssignmentExpression().get(0).getExpression();
252 String treatment = tempTreatmentObj.getValue().getContent().get(0).toString();
254 prePopulateRainyDayTreatments(errorcode, treatment);
261 rainydayParams.setTreatmentTableChoices(treatmentList);
262 policyAdapter.setRainyday(rainydayParams);
263 policyAdapter.setSettings(decisionList);
268 private void prePopulateRainyDayTreatments(String errorcode, String treatment) {
269 Map<String, String> ruleMap = new HashMap<>();
271 ruleMap.put("errorcode", errorcode);
272 ruleMap.put("treatment", treatment);
273 treatmentList.add(ruleMap);
277 private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
278 Map<String, String> ruleMap = new HashMap<>();
279 ruleMap.put("id", "A" + (index +1));
280 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
281 for (String key : dropDownMap.keySet()) {
282 String keyValue = dropDownMap.get(key);
283 if (keyValue.equals(decisionApply.getFunctionId())) {
284 ruleMap.put("dynamicRuleAlgorithmCombo", key);
287 // Populate the key and value fields
288 if (((jaxbDecisionTypes.get(0).getValue()) instanceof AttributeValueType)) {
289 ApplyType innerDecisionApply = (ApplyType) jaxbDecisionTypes.get(1).getValue();
290 List<JAXBElement<?>> jaxbInnerDecisionTypes = innerDecisionApply.getExpression();
291 if (jaxbInnerDecisionTypes.get(0).getValue() instanceof AttributeDesignatorType) {
292 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerDecisionTypes.get(0).getValue();
293 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
295 // Get from Attribute Value
296 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(0).getValue();
297 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
298 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
300 } else if ((jaxbDecisionTypes.get(0).getValue()) instanceof VariableReferenceType) {
301 VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();
302 ruleMap.put("dynamicRuleAlgorithmField1", "S_"+ variableReference.getVariableId());
305 // Get from Attribute Value
306 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
307 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
308 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
310 ruleAlgorithmList.add(ruleMap);
313 private int prePopulateDecisionCompoundRuleAlgorithm(int index, ApplyType decisionApply) {
314 boolean isCompoundRule = true;
315 List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
316 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
317 // If There is Attribute Value under Decision Type that means we came to the final child
318 if (policyLogger.isDebugEnabled()) {
319 policyLogger.debug("Prepopulating rule algoirthm: " + index);
321 // Check to see if Attribute Value exists, if yes then it is not a compound rule
322 if(jaxbElement.getValue() instanceof AttributeValueType) {
323 prePopulateDecisionRuleAlgorithms(index, decisionApply, jaxbDecisionTypes);
324 ruleAlgoirthmTracker.addLast(index);
325 isCompoundRule = false;
329 if (isCompoundRule) {
330 // As it's compound rule, Get the Apply types
331 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
332 ApplyType innerDecisionApply = (ApplyType) jaxbElement.getValue();
333 index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
335 // Populate combo box
336 if (policyLogger.isDebugEnabled()) {
337 policyLogger.debug("Prepopulating Compound rule algorithm: " + index);
339 Map<String, String> rule = new HashMap<>();
340 for (String key : PolicyController.getDropDownMap().keySet()) {
341 String keyValue = PolicyController.getDropDownMap().get(key);
342 if (keyValue.equals(decisionApply.getFunctionId())) {
343 rule.put("dynamicRuleAlgorithmCombo", key);
348 rule.put("id", "A" + (index +1));
349 // Populate Key and values for Compound Rule
350 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
351 ruleAlgoirthmTracker.removeLast();
352 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
353 ruleAlgoirthmTracker.removeLast();
354 ruleAlgoirthmTracker.addLast(index);
355 ruleAlgorithmList.add(rule);