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.HashMap;
25 import java.util.Iterator;
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.policy.rest.jpa.PolicyEntity;
37 import org.onap.portalsdk.core.controller.RestrictedBaseController;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
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.AttributeAssignmentExpressionType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
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.ObligationExpressionType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
56 @RequestMapping({ "/" })
57 public class ActionPolicyController extends RestrictedBaseController {
58 private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyController.class);
60 public ActionPolicyController() {
61 // Default Constructor
64 private ArrayList<Object> attributeList;
65 protected LinkedList<Integer> ruleAlgoirthmTracker;
66 public static final String PERFORMER_ATTRIBUTEID = "performer";
67 protected Map<String, String> performer = new HashMap<>();
68 private ArrayList<Object> ruleAlgorithmList;
70 public void prePopulateActionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
71 attributeList = new ArrayList<>();
72 ruleAlgorithmList = new ArrayList<>();
73 performer.put("PDP", "PDPAction");
74 performer.put("PEP", "PEPAction");
76 if (policyAdapter.getPolicyData() instanceof PolicyType) {
77 Object policyData = policyAdapter.getPolicyData();
78 PolicyType policy = (PolicyType) policyData;
79 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
80 String policyNameValue = policyAdapter.getPolicyName()
81 .substring(policyAdapter.getPolicyName().indexOf('_') + 1);
82 policyAdapter.setPolicyName(policyNameValue);
83 String description = "";
85 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
86 } catch (Exception e) {
87 LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue, e);
88 description = policy.getDescription();
90 policyAdapter.setPolicyDescription(description);
91 // Get the target data under policy for Action.
92 TargetType target = policy.getTarget();
94 // under target we have AnyOFType
95 List<AnyOfType> anyOfList = target.getAnyOf();
96 if (anyOfList != null) {
97 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
98 while (iterAnyOf.hasNext()) {
99 AnyOfType anyOf = iterAnyOf.next();
100 // Under AntOfType we have AllOfType
101 List<AllOfType> allOfList = anyOf.getAllOf();
102 if (allOfList != null) {
103 Iterator<AllOfType> iterAllOf = allOfList.iterator();
104 while (iterAllOf.hasNext()) {
105 AllOfType allOf = iterAllOf.next();
106 // Under AllOfType we have Mathch.
107 List<MatchType> matchList = allOf.getMatch();
108 if (matchList != null) {
109 Iterator<MatchType> iterMatch = matchList.iterator();
110 while (iterMatch.hasNext()) {
111 MatchType match = iterMatch.next();
113 // Under the match we have attributevalue and
114 // attributeDesignator. So,finally down to the actual attribute.
116 AttributeValueType attributeValue = match.getAttributeValue();
117 String value = (String) attributeValue.getContent().get(0);
118 AttributeDesignatorType designator = match.getAttributeDesignator();
119 String attributeId = designator.getAttributeId();
120 // Component attributes are saved under Target here we are fetching them back.
121 // One row is default so we are not adding dynamic component at index 0.
122 Map<String, String> attribute = new HashMap<>();
123 attribute.put("key", attributeId);
124 attribute.put("value", value);
125 attributeList.add(attribute);
128 policyAdapter.setAttributes(attributeList);
134 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
135 // Under rule we have Condition and obligation.
136 for (Object o : ruleList) {
137 if (o instanceof RuleType) {
138 ConditionType condition = ((RuleType) o).getCondition();
139 ObligationExpressionsType obligations = ((RuleType) o).getObligationExpressions();
140 if (condition != null) {
142 ApplyType actionApply = (ApplyType) condition.getExpression().getValue();
143 ruleAlgoirthmTracker = new LinkedList<>();
144 // Populating Rule Algorithms starting from compound.
145 prePopulateCompoundRuleAlgorithm(index, actionApply);
147 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
148 // get the Obligation data under the rule for Form elements.
149 if (obligations != null) {
150 // Under the obligationExpressions we have obligationExpression.
151 List<ObligationExpressionType> obligationList = obligations.getObligationExpression();
152 if (obligationList != null) {
153 Iterator<ObligationExpressionType> iterObligation = obligationList.iterator();
154 while (iterObligation.hasNext()) {
155 ObligationExpressionType obligation = iterObligation.next();
156 policyAdapter.setActionAttributeValue(obligation.getObligationId());
157 // Under the obligationExpression we have attributeAssignmentExpression.
158 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionList = obligation
159 .getAttributeAssignmentExpression();
160 if (attributeAssignmentExpressionList != null) {
161 Iterator<AttributeAssignmentExpressionType> iterAttributeAssignmentExpression = attributeAssignmentExpressionList
163 while (iterAttributeAssignmentExpression.hasNext()) {
164 AttributeAssignmentExpressionType attributeAssignmentExpression = iterAttributeAssignmentExpression
166 String attributeID = attributeAssignmentExpression.getAttributeId();
167 AttributeValueType attributeValue = (AttributeValueType) attributeAssignmentExpression
168 .getExpression().getValue();
169 if (attributeID.equals(PERFORMER_ATTRIBUTEID)) {
170 for ( Entry<String, String> entry: performer.entrySet()) {
171 String key = entry.getKey();
172 String keyValue = entry.getValue();
173 if (keyValue.equals(attributeValue.getContent().get(0))) {
174 policyAdapter.setActionPerformer(key);
189 private int prePopulateCompoundRuleAlgorithm(int index, ApplyType actionApply) {
190 boolean isCompoundRule = true;
191 List<JAXBElement<?>> jaxbActionTypes = actionApply.getExpression();
192 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
193 // If There is Attribute Value under Action Type that means we came to the final child
194 if (LOGGER.isDebugEnabled()) {
195 LOGGER.debug("Prepopulating rule algoirthm: " + index);
197 // Check to see if Attribute Value exists, if yes then it is not a compound rule
198 if (jaxbElement.getValue() instanceof AttributeValueType) {
199 prePopulateRuleAlgorithms(index, actionApply, jaxbActionTypes);
200 ruleAlgoirthmTracker.addLast(index);
201 isCompoundRule = false;
205 if (isCompoundRule) {
206 // As it's compound rule, Get the Apply types
207 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
208 ApplyType innerActionApply = (ApplyType) jaxbElement.getValue();
209 index = prePopulateCompoundRuleAlgorithm(index, innerActionApply);
211 // Populate combo box
212 if (LOGGER.isDebugEnabled()) {
213 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
215 Map<String, String> rule = new HashMap<>();
216 for (String key : PolicyController.getDropDownMap().keySet()) {
217 String keyValue = PolicyController.getDropDownMap().get(key);
218 if (keyValue.equals(actionApply.getFunctionId())) {
219 rule.put("dynamicRuleAlgorithmCombo", key);
222 rule.put("id", "A" + (index + 1));
223 // Populate Key and values for Compound Rule
224 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1));
225 ruleAlgoirthmTracker.removeLast();
226 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
227 ruleAlgoirthmTracker.removeLast();
228 ruleAlgoirthmTracker.addLast(index);
229 ruleAlgorithmList.add(rule);
235 private void prePopulateRuleAlgorithms(int index, ApplyType actionApply, List<JAXBElement<?>> jaxbActionTypes) {
236 Map<String, String> ruleMap = new HashMap<>();
237 ruleMap.put("id", "A" + (index + 1));
238 // Populate combo box
239 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
240 for ( Entry<String, String> entry : dropDownMap.entrySet()) {
241 if (entry.getValue().equals(actionApply.getFunctionId())) {
242 ruleMap.put("dynamicRuleAlgorithmCombo", entry.getKey());
245 // Populate the key and value fields
246 // Rule Attribute added as key
247 if ((jaxbActionTypes.get(0).getValue()) instanceof ApplyType) {
248 // Get from Attribute Designator
249 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(0).getValue();
250 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
251 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0)
253 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
255 // Get from Attribute Value
256 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(1).getValue();
257 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
258 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
260 // Rule Attribute added as value
261 else if ((jaxbActionTypes.get(0).getValue()) instanceof AttributeValueType) {
262 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(0).getValue();
263 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
264 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
266 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue();
267 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
268 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0)
270 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
272 ruleAlgorithmList.add(ruleMap);