2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Bell Canada
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controller;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.List;
29 import java.util.Map.Entry;
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.portalsdk.core.controller.RestrictedBaseController;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
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.MatchType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
55 @RequestMapping({ "/" })
56 public class ActionPolicyController extends RestrictedBaseController {
57 private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyController.class);
58 private static final String PERFORMER_ATTRIBUTE_ID = "performer";
59 private static final String DYNAMIC_RULE_ALGORITHM_FIELD_1 = "dynamicRuleAlgorithmField1";
60 private static final String DYNAMIC_RULE_ALGORITHM_FIELD_2 = "dynamicRuleAlgorithmField2";
61 private LinkedList<Integer> ruleAlgorithmTracker;
62 private Map<String, String> performer = new HashMap<>();
63 private List<Object> ruleAlgorithmList;
65 public ActionPolicyController() {
66 // Default Constructor
69 public void prePopulateActionPolicyData(PolicyRestAdapter policyAdapter) {
70 ruleAlgorithmList = new ArrayList<>();
71 performer.put("PDP", "PDPAction");
72 performer.put("PEP", "PEPAction");
74 if (policyAdapter.getPolicyData() instanceof PolicyType) {
75 PolicyType policy = (PolicyType) policyAdapter.getPolicyData();
77 // 1. Set policy-name, policy-filename and description to Policy Adapter
78 setPolicyAdapterPolicyNameAndDesc(policyAdapter, policy);
80 // 2a. Get the target data under policy for Action.
81 TargetType target = policy.getTarget();
86 // 2b. Set attributes to Policy Adapter
87 setPolicyAdapterAttributes(policyAdapter, target.getAnyOf());
89 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
90 // Under rule we have Condition and obligation.
91 for (Object o : ruleList) {
92 if (!(o instanceof RuleType)) {
95 // 3. Set rule-algorithm choices to Policy Adapter
96 setPolicyAdapterRuleAlgorithmschoices(policyAdapter, (RuleType) o);
98 // 4a. Get the Obligation data under the rule for Form elements.
99 ObligationExpressionsType obligations = ((RuleType) o).getObligationExpressions();
101 // 4b. Set action attribute-value and action-performer to Policy Adapter
102 setPolicyAdapterActionData(policyAdapter, obligations);
107 private void setPolicyAdapterActionData(PolicyRestAdapter policyAdapter, ObligationExpressionsType obligations) {
108 if (obligations == null) {
111 // Under the obligationExpressions we have obligationExpression.
112 List<ObligationExpressionType> obligationList = obligations.getObligationExpression();
113 if (obligationList == null) {
116 for (ObligationExpressionType obligation : obligationList) {
117 policyAdapter.setActionAttributeValue(obligation.getObligationId());
118 // Under the obligationExpression we have attributeAssignmentExpression.
119 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionList = obligation
120 .getAttributeAssignmentExpression();
121 if (attributeAssignmentExpressionList == null) {
124 for (AttributeAssignmentExpressionType attributeAssignmentExpression : attributeAssignmentExpressionList) {
125 String attributeID = attributeAssignmentExpression.getAttributeId();
126 AttributeValueType attributeValue = (AttributeValueType) attributeAssignmentExpression
127 .getExpression().getValue();
128 if (!attributeID.equals(PERFORMER_ATTRIBUTE_ID)) {
131 performer.forEach((key, keyValue) -> {
132 if (keyValue.equals(attributeValue.getContent().get(0))) {
133 policyAdapter.setActionPerformer(key);
140 private void setPolicyAdapterPolicyNameAndDesc(PolicyRestAdapter policyAdapter, PolicyType policy) {
141 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
142 String policyNameValue = policyAdapter.getPolicyName()
143 .substring(policyAdapter.getPolicyName().indexOf('_') + 1);
144 policyAdapter.setPolicyName(policyNameValue);
147 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
148 } catch (Exception e) {
149 LOGGER.error("Error while collecting the description tag in ActionPolicy " + policyNameValue, e);
150 description = policy.getDescription();
152 policyAdapter.setPolicyDescription(description);
155 private void setPolicyAdapterRuleAlgorithmschoices(PolicyRestAdapter policyAdapter, RuleType o) {
156 ConditionType condition = o.getCondition();
157 if (condition != null) {
159 ApplyType actionApply = (ApplyType) condition.getExpression().getValue();
160 ruleAlgorithmTracker = new LinkedList<>();
161 // Populating Rule Algorithms starting from compound.
162 prePopulateCompoundRuleAlgorithm(index, actionApply);
164 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
167 private void setPolicyAdapterAttributes(PolicyRestAdapter policyAdapter, List<AnyOfType> anyOfList) {
168 List<Object> attributeList = new ArrayList<>();
169 if (anyOfList == null) {
172 // under target we have AnyOFType
173 for (AnyOfType anyOf : anyOfList) {
174 // Under AntOfType we have AllOfType
175 List<AllOfType> allOfList = anyOf.getAllOf();
176 if (allOfList == null) {
179 // Under AllOfType we have Match.
180 for (AllOfType allOfType : allOfList) {
181 List<MatchType> matchList = allOfType.getMatch();
182 if (matchList != null) {
184 // Under the match we have attributeValue and
185 // attributeDesignator. So,finally down to the actual attribute.
187 // Component attributes are saved under Target here we are fetching them back.
188 // One row is default so we are not adding dynamic component at index 0.
189 matchList.forEach(match -> {
190 AttributeValueType attributeValue = match.getAttributeValue();
191 String value = (String) attributeValue.getContent().get(0);
192 AttributeDesignatorType designator = match.getAttributeDesignator();
193 String attributeId = designator.getAttributeId();
194 Map<String, String> attribute = new HashMap<>();
195 attribute.put("key", attributeId);
196 attribute.put("value", value);
197 attributeList.add(attribute);
200 policyAdapter.setAttributes(attributeList);
205 private int prePopulateCompoundRuleAlgorithm(int index, ApplyType actionApply) {
206 boolean isCompoundRule = true;
207 List<JAXBElement<?>> jaxbActionTypes = actionApply.getExpression();
208 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
209 // If There is Attribute Value under Action Type that means we came to the final child
210 if (LOGGER.isDebugEnabled()) {
211 LOGGER.debug("Prepopulating rule algoirthm: " + index);
213 // Check to see if Attribute Value exists, if yes then it is not a compound rule
214 if (jaxbElement.getValue() instanceof AttributeValueType) {
215 prePopulateRuleAlgorithms(index, actionApply, jaxbActionTypes);
216 ruleAlgorithmTracker.addLast(index);
217 isCompoundRule = false;
221 if (isCompoundRule) {
222 // As it's compound rule, Get the Apply types
223 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
224 ApplyType innerActionApply = (ApplyType) jaxbElement.getValue();
225 index = prePopulateCompoundRuleAlgorithm(index, innerActionApply);
227 // Populate combo box
228 if (LOGGER.isDebugEnabled()) {
229 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
231 Map<String, String> rule = new HashMap<>();
232 for (String key : PolicyController.getDropDownMap().keySet()) {
233 String keyValue = PolicyController.getDropDownMap().get(key);
234 if (keyValue.equals(actionApply.getFunctionId())) {
235 rule.put("dynamicRuleAlgorithmCombo", key);
238 rule.put("id", "A" + (index + 1));
239 // Populate Key and values for Compound Rule
240 rule.put(DYNAMIC_RULE_ALGORITHM_FIELD_1, "A" + (ruleAlgorithmTracker.getLast() + 1));
241 ruleAlgorithmTracker.removeLast();
242 rule.put(DYNAMIC_RULE_ALGORITHM_FIELD_2, "A" + (ruleAlgorithmTracker.getLast() + 1));
243 ruleAlgorithmTracker.removeLast();
244 ruleAlgorithmTracker.addLast(index);
245 ruleAlgorithmList.add(rule);
251 private void prePopulateRuleAlgorithms(int index, ApplyType actionApply, List<JAXBElement<?>> jaxbActionTypes) {
252 Map<String, String> ruleMap = new HashMap<>();
253 ruleMap.put("id", "A" + (index + 1));
254 // Populate combo box
255 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
256 for ( Entry<String, String> entry : dropDownMap.entrySet()) {
257 if (entry.getValue().equals(actionApply.getFunctionId())) {
258 ruleMap.put("dynamicRuleAlgorithmCombo", entry.getKey());
261 // Populate the key and value fields
262 // Rule Attribute added as key
263 if ((jaxbActionTypes.get(0).getValue()) instanceof ApplyType) {
264 // Get from Attribute Designator
265 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(0).getValue();
266 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
267 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0)
269 ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_1, attributeDesignator.getAttributeId());
271 // Get from Attribute Value
272 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(1).getValue();
273 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
274 ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_2, attributeValue);
276 // Rule Attribute added as value
277 else if ((jaxbActionTypes.get(0).getValue()) instanceof AttributeValueType) {
278 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(0).getValue();
279 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
280 ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_2, attributeValue);
282 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue();
283 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
284 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0)
286 ruleMap.put(DYNAMIC_RULE_ALGORITHM_FIELD_1, attributeDesignator.getAttributeId());
288 ruleAlgorithmList.add(ruleMap);