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.portalsdk.core.controller.RestrictedBaseController;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
37 import org.openecomp.policy.common.logging.flexlogger.Logger;
38 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
39 import org.openecomp.policy.rest.jpa.PolicyEntity;
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<Object>();
72 decisionList = new ArrayList<Object>();
73 ruleAlgorithmList = new ArrayList<Object>();
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.
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 componet at index 0.
124 Map<String, String> attribute = new HashMap<String, String>();
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.PERMIT)) {
154 ConditionType condition = ((RuleType) object).getCondition();
155 if (condition != null) {
156 ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
157 ruleAlgoirthmTracker = new LinkedList<Integer>();
158 // Populating Rule Algorithms starting from compound.
159 prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
160 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
162 }else if(((RuleType) object).getEffect().equals(EffectType.DENY)) {
163 if(((RuleType) object).getAdviceExpressions()!=null){
164 if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("AAF")){
165 policyAdapter.setRuleProvider("AAF");
169 policyAdapter.setRuleProvider("Custom");
175 policyAdapter.setSettings(decisionList);
180 private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
181 Map<String, String> ruleMap = new HashMap<String, String>();
182 ruleMap.put("id", "A" + (index +1));
183 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
184 for (String key : dropDownMap.keySet()) {
185 String keyValue = dropDownMap.get(key);
186 if (keyValue.equals(decisionApply.getFunctionId())) {
187 ruleMap.put("dynamicRuleAlgorithmCombo", key);
190 // Populate the key and value fields
191 if (((jaxbDecisionTypes.get(0).getValue()) instanceof AttributeValueType)) {
192 ApplyType innerDecisionApply = (ApplyType) jaxbDecisionTypes.get(1).getValue();
193 List<JAXBElement<?>> jaxbInnerDecisionTypes = innerDecisionApply.getExpression();
194 if (jaxbInnerDecisionTypes.get(0).getValue() instanceof AttributeDesignatorType) {
195 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerDecisionTypes.get(0).getValue();
196 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
198 // Get from Attribute Value
199 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(0).getValue();
200 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
201 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
203 } else if ((jaxbDecisionTypes.get(0).getValue()) instanceof VariableReferenceType) {
204 VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();
205 ruleMap.put("dynamicRuleAlgorithmField1", "S_"+ variableReference.getVariableId());
208 // Get from Attribute Value
209 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
210 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
211 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
213 ruleAlgorithmList.add(ruleMap);
216 private int prePopulateDecisionCompoundRuleAlgorithm(int index, ApplyType decisionApply) {
217 boolean isCompoundRule = true;
218 List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
219 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
220 // If There is Attribute Value under Decision Type that means we came to the final child
221 if (logger.isDebugEnabled()) {
222 logger.debug("Prepopulating rule algoirthm: " + index);
224 // Check to see if Attribute Value exists, if yes then it is not a compound rule
225 if(jaxbElement.getValue() instanceof AttributeValueType) {
226 prePopulateDecisionRuleAlgorithms(index, decisionApply, jaxbDecisionTypes);
227 ruleAlgoirthmTracker.addLast(index);
228 isCompoundRule = false;
232 if (isCompoundRule) {
233 // As it's compound rule, Get the Apply types
234 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
235 ApplyType innerDecisionApply = (ApplyType) jaxbElement.getValue();
236 index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
238 // Populate combo box
239 if (logger.isDebugEnabled()) {
240 logger.debug("Prepopulating Compound rule algorithm: " + index);
242 Map<String, String> rule = new HashMap<String, String>();
243 for (String key : PolicyController.getDropDownMap().keySet()) {
244 String keyValue = PolicyController.getDropDownMap().get(key);
245 if (keyValue.equals(decisionApply.getFunctionId())) {
246 rule.put("dynamicRuleAlgorithmCombo", key);
250 rule.put("id", "A" + (index +1));
251 // Populate Key and values for Compound Rule
252 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
253 ruleAlgoirthmTracker.removeLast();
254 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
255 ruleAlgoirthmTracker.removeLast();
256 ruleAlgoirthmTracker.addLast(index);
257 ruleAlgorithmList.add(rule);