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.RainyDayParams;
36 import org.openecomp.policy.rest.adapter.YAMLParams;
37 import org.openecomp.policy.rest.jpa.PolicyEntity;
38 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.RequestMapping;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
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 logger = 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 description = policy.getDescription();
93 policyAdapter.setPolicyDescription(description);
94 // Get the target data under policy for Action.
95 TargetType target = policy.getTarget();
97 // under target we have AnyOFType
98 List<AnyOfType> anyOfList = target.getAnyOf();
99 if (anyOfList != null) {
100 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
101 while (iterAnyOf.hasNext()) {
102 AnyOfType anyOf = iterAnyOf.next();
103 // Under AntOfType we have AllOfType
104 List<AllOfType> allOfList = anyOf.getAllOf();
105 if (allOfList != null) {
106 Iterator<AllOfType> iterAllOf = allOfList.iterator();
107 while (iterAllOf.hasNext()) {
108 AllOfType allOf = iterAllOf.next();
109 // Under AllOfType we have Mathch.
110 List<MatchType> matchList = allOf.getMatch();
112 if (matchList != null) {
113 Iterator<MatchType> iterMatch = matchList.iterator();
114 while (iterMatch.hasNext()) {
115 MatchType match = iterMatch.next();
117 // Under the match we have attributevalue and
118 // attributeDesignator. So,finally down to the actual attribute.
120 AttributeValueType attributeValue = match.getAttributeValue();
121 String value = (String) attributeValue.getContent().get(0);
122 AttributeDesignatorType designator = match.getAttributeDesignator();
123 String attributeId = designator.getAttributeId();
124 // First match in the target is EcompName, so set that value.
125 if (attributeId.equals("ECOMPName")) {
126 policyAdapter.setEcompName(value);
128 // Component attributes are saved under Target here we are fetching them back.
129 // One row is default so we are not adding dynamic component at index 0.
131 Map<String, String> attribute = new HashMap<>();
132 attribute.put("key", attributeId);
133 attribute.put("value", value);
134 attributeList.add(attribute);
139 policyAdapter.setAttributes(attributeList);
143 // Setting rainy day attributes to the parameters object if they exist
144 if(!attributeList.isEmpty()) {
145 for(int i=0; i<attributeList.size() ; i++){
146 Map<String, String> map = (Map<String,String>)attributeList.get(i);
147 if(map.get("key").equals("WorkStep")){
148 rainydayParams.setWorkstep(map.get("value"));
149 }else if(map.get("key").equals("BB_ID")){
150 rainydayParams.setBbid(map.get("value"));
151 }else if(map.get("key").equals("ServiceType")){
152 rainydayParams.setServiceType(map.get("value"));
153 }else if(map.get("key").equals("VNFType")){
154 rainydayParams.setVnfType(map.get("value"));
159 policyAdapter.setRuleProvider("Rainy_Day");
163 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
165 for (Object object : ruleList) {
166 if (object instanceof VariableDefinitionType) {
167 VariableDefinitionType variableDefinitionType = (VariableDefinitionType) object;
168 Map<String, String> settings = new HashMap<String, String>();
169 settings.put("key", variableDefinitionType.getVariableId());
170 JAXBElement<AttributeValueType> attributeValueTypeElement = (JAXBElement<AttributeValueType>) variableDefinitionType.getExpression();
171 if (attributeValueTypeElement != null) {
172 AttributeValueType attributeValueType = attributeValueTypeElement.getValue();
173 settings.put("value", attributeValueType.getContent().get(0).toString());
175 decisionList.add(settings);
176 } else if (object instanceof RuleType) {
177 // get the condition data under the rule for rule Algorithms.
178 if(((RuleType) object).getEffect().equals(EffectType.DENY)) {
179 if(((RuleType) object).getAdviceExpressions()!=null){
180 if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("AAF")){
181 policyAdapter.setRuleProvider("AAF");
183 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_YAML")){
184 policyAdapter.setRuleProvider("GUARD_YAML");
185 }else if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("GUARD_BL_YAML")){
186 policyAdapter.setRuleProvider("GUARD_BL_YAML");
189 policyAdapter.setRuleProvider("Custom");
191 ConditionType condition = ((RuleType) object).getCondition();
192 if (condition != null) {
193 ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
194 decisionApply = (ApplyType) decisionApply.getExpression().get(0).getValue();
195 ruleAlgoirthmTracker = new LinkedList<>();
196 if(policyAdapter.getRuleProvider()!=null && (policyAdapter.getRuleProvider().equals("GUARD_YAML")||(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")))){
197 YAMLParams yamlParams = new YAMLParams();
198 for(int i=0; i<attributeList.size() ; i++){
199 Map<String, String> map = (Map<String,String>)attributeList.get(i);
200 if(map.get("key").equals("actor")){
201 yamlParams.setActor(map.get("value"));
202 }else if(map.get("key").equals("recipe")){
203 yamlParams.setRecipe(map.get("value"));
206 ApplyType apply = ((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(0).getValue());
207 yamlParams.setGuardActiveStart(((AttributeValueType)apply.getExpression().get(1).getValue()).getContent().get(0).toString());
208 yamlParams.setGuardActiveEnd(((AttributeValueType)apply.getExpression().get(2).getValue()).getContent().get(0).toString());
209 if(policyAdapter.getRuleProvider().equals("GUARD_BL_YAML")){
210 apply = (ApplyType)((ApplyType)((ApplyType)decisionApply.getExpression().get(0).getValue()).getExpression().get(1).getValue()).getExpression().get(2).getValue();
211 Iterator<JAXBElement<?>> attributes = apply.getExpression().iterator();
212 List<String> blackList = new ArrayList<>();
213 while(attributes.hasNext()){
214 blackList.add(((AttributeValueType)attributes.next().getValue()).getContent().get(0).toString());
216 yamlParams.setBlackList(blackList);
218 yamlParams.setLimit(((AttributeValueType)((ApplyType)decisionApply.getExpression().get(1).getValue()).getExpression().get(1).getValue()).getContent().get(0).toString());
219 String timeWindow = ((AttributeDesignatorType)((ApplyType)((ApplyType)decisionApply.getExpression().get(1).getValue()).getExpression().get(0).getValue()).getExpression().get(0).getValue()).getIssuer();
220 yamlParams.setTimeWindow(timeWindow.substring(timeWindow.lastIndexOf(":")+1));
222 policyAdapter.setYamlparams(yamlParams);
223 policyAdapter.setAttributes(new ArrayList<Object>());
224 policyAdapter.setRuleAlgorithmschoices(new ArrayList<Object>());
227 // Populating Rule Algorithms starting from compound.
228 prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
229 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
231 } else if (((RuleType) object).getEffect().equals(EffectType.PERMIT)) {
233 TargetType ruleTarget = ((RuleType) object).getTarget();
234 AdviceExpressionsType adviceExpression = ((RuleType) object).getAdviceExpressions();
236 String errorcode = ruleTarget.getAnyOf().get(0).getAllOf().get(0).getMatch().
237 get(1).getAttributeValue().getContent().get(0).toString();
238 JAXBElement<AttributeValueType> tempTreatmentObj = (JAXBElement<AttributeValueType>) adviceExpression.getAdviceExpression().
239 get(0).getAttributeAssignmentExpression().get(0).getExpression();
240 String treatment = tempTreatmentObj.getValue().getContent().get(0).toString();
242 prePopulateRainyDayTreatments(errorcode, treatment);
249 rainydayParams.setTreatmentTableChoices(treatmentList);
250 policyAdapter.setRainyday(rainydayParams);
251 policyAdapter.setSettings(decisionList);
256 private void prePopulateRainyDayTreatments(String errorcode, String treatment) {
257 Map<String, String> ruleMap = new HashMap<>();
259 ruleMap.put("errorcode", errorcode);
260 ruleMap.put("treatment", treatment);
261 treatmentList.add(ruleMap);
265 private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
266 Map<String, String> ruleMap = new HashMap<>();
267 ruleMap.put("id", "A" + (index +1));
268 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
269 for (String key : dropDownMap.keySet()) {
270 String keyValue = dropDownMap.get(key);
271 if (keyValue.equals(decisionApply.getFunctionId())) {
272 ruleMap.put("dynamicRuleAlgorithmCombo", key);
275 // Populate the key and value fields
276 if (((jaxbDecisionTypes.get(0).getValue()) instanceof AttributeValueType)) {
277 ApplyType innerDecisionApply = (ApplyType) jaxbDecisionTypes.get(1).getValue();
278 List<JAXBElement<?>> jaxbInnerDecisionTypes = innerDecisionApply.getExpression();
279 if (jaxbInnerDecisionTypes.get(0).getValue() instanceof AttributeDesignatorType) {
280 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerDecisionTypes.get(0).getValue();
281 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
283 // Get from Attribute Value
284 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(0).getValue();
285 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
286 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
288 } else if ((jaxbDecisionTypes.get(0).getValue()) instanceof VariableReferenceType) {
289 VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();
290 ruleMap.put("dynamicRuleAlgorithmField1", "S_"+ variableReference.getVariableId());
293 // Get from Attribute Value
294 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
295 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
296 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
298 ruleAlgorithmList.add(ruleMap);
301 private int prePopulateDecisionCompoundRuleAlgorithm(int index, ApplyType decisionApply) {
302 boolean isCompoundRule = true;
303 List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
304 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
305 // If There is Attribute Value under Decision Type that means we came to the final child
306 if (logger.isDebugEnabled()) {
307 logger.debug("Prepopulating rule algoirthm: " + index);
309 // Check to see if Attribute Value exists, if yes then it is not a compound rule
310 if(jaxbElement.getValue() instanceof AttributeValueType) {
311 prePopulateDecisionRuleAlgorithms(index, decisionApply, jaxbDecisionTypes);
312 ruleAlgoirthmTracker.addLast(index);
313 isCompoundRule = false;
317 if (isCompoundRule) {
318 // As it's compound rule, Get the Apply types
319 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
320 ApplyType innerDecisionApply = (ApplyType) jaxbElement.getValue();
321 index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
323 // Populate combo box
324 if (logger.isDebugEnabled()) {
325 logger.debug("Prepopulating Compound rule algorithm: " + index);
327 Map<String, String> rule = new HashMap<>();
328 for (String key : PolicyController.getDropDownMap().keySet()) {
329 String keyValue = PolicyController.getDropDownMap().get(key);
330 if (keyValue.equals(decisionApply.getFunctionId())) {
331 rule.put("dynamicRuleAlgorithmCombo", key);
336 rule.put("id", "A" + (index +1));
337 // Populate Key and values for Compound Rule
338 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
339 ruleAlgoirthmTracker.removeLast();
340 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
341 ruleAlgoirthmTracker.removeLast();
342 ruleAlgoirthmTracker.addLast(index);
343 ruleAlgorithmList.add(rule);