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