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;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.List;
31 import javax.xml.bind.JAXBElement;
33 import org.onap.policy.rest.adapter.PolicyRestAdapter;
34 import org.onap.policy.rest.jpa.PolicyEntity;
35 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
36 import org.springframework.stereotype.Controller;
37 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;
53 import org.onap.policy.common.logging.flexlogger.FlexLogger;
54 import org.onap.policy.common.logging.flexlogger.Logger;
57 @RequestMapping({"/"})
58 public class ActionPolicyController extends RestrictedBaseController{
59 private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyController.class);
61 public ActionPolicyController(){
65 private ArrayList<Object> attributeList;
66 protected LinkedList<Integer> ruleAlgoirthmTracker;
67 public static final String PERFORMER_ATTRIBUTEID = "performer";
68 protected Map<String, String> performer = new HashMap<>();
69 private ArrayList<Object> ruleAlgorithmList;
71 public void prePopulateActionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
72 attributeList = new ArrayList<>();
73 ruleAlgorithmList = new ArrayList<>();
74 performer.put("PDP", "PDPAction");
75 performer.put("PEP", "PEPAction");
77 if (policyAdapter.getPolicyData() instanceof PolicyType) {
78 Object policyData = policyAdapter.getPolicyData();
79 PolicyType policy = (PolicyType) policyData;
80 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
81 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("_") + 1);
82 policyAdapter.setPolicyName(policyNameValue);
83 String description = "";
85 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
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.getAttributeAssignmentExpression();
158 if (attributeAssignmentExpressionList != null) {
159 Iterator<AttributeAssignmentExpressionType> iterAttributeAssignmentExpression = attributeAssignmentExpressionList.iterator();
160 while (iterAttributeAssignmentExpression.hasNext()) {
161 AttributeAssignmentExpressionType attributeAssignmentExpression = iterAttributeAssignmentExpression.next();
162 String attributeID = attributeAssignmentExpression.getAttributeId();
163 AttributeValueType attributeValue = (AttributeValueType) attributeAssignmentExpression.getExpression().getValue();
164 if (attributeID.equals(PERFORMER_ATTRIBUTEID)) {
165 for (String key : performer.keySet()) {
166 String keyValue = performer.get(key);
167 if (keyValue.equals(attributeValue.getContent().get(0))) {
168 policyAdapter.setActionPerformer(key);
183 private int prePopulateCompoundRuleAlgorithm(int index, ApplyType actionApply) {
184 boolean isCompoundRule = true;
185 List<JAXBElement<?>> jaxbActionTypes = actionApply.getExpression();
186 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
187 // If There is Attribute Value under Action Type that means we came to the final child
188 if (LOGGER.isDebugEnabled()) {
189 LOGGER.debug("Prepopulating rule algoirthm: " + index);
191 // Check to see if Attribute Value exists, if yes then it is not a compound rule
192 if (jaxbElement.getValue() instanceof AttributeValueType) {
193 prePopulateRuleAlgorithms(index, actionApply, jaxbActionTypes);
194 ruleAlgoirthmTracker.addLast(index);
195 isCompoundRule = false;
199 if (isCompoundRule) {
200 // As it's compound rule, Get the Apply types
201 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
202 ApplyType innerActionApply = (ApplyType) jaxbElement.getValue();
203 index = prePopulateCompoundRuleAlgorithm(index, innerActionApply);
205 // Populate combo box
206 if (LOGGER.isDebugEnabled()) {
207 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
209 Map<String, String> rule = new HashMap<String, String>();
210 for (String key : PolicyController.getDropDownMap().keySet()) {
211 String keyValue = PolicyController.getDropDownMap().get(key);
212 if (keyValue.equals(actionApply.getFunctionId())) {
213 rule.put("dynamicRuleAlgorithmCombo", key);
216 rule.put("id", "A" + (index +1));
217 // Populate Key and values for Compound Rule
218 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
219 ruleAlgoirthmTracker.removeLast();
220 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
221 ruleAlgoirthmTracker.removeLast();
222 ruleAlgoirthmTracker.addLast(index);
223 ruleAlgorithmList.add(rule);
229 private void prePopulateRuleAlgorithms(int index, ApplyType actionApply, List<JAXBElement<?>> jaxbActionTypes) {
230 Map<String, String> ruleMap = new HashMap<String, String>();
231 ruleMap.put("id", "A" + (index +1));
232 // Populate combo box
233 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
234 for (String key : dropDownMap.keySet()) {
235 String keyValue = dropDownMap.get(key);
236 if (keyValue.equals(actionApply.getFunctionId())) {
237 ruleMap.put("dynamicRuleAlgorithmCombo", key);
240 // Populate the key and value fields
241 // Rule Attribute added as key
242 if ((jaxbActionTypes.get(0).getValue()) instanceof ApplyType) {
243 // Get from Attribute Designator
244 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(0).getValue();
245 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
246 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0).getValue();
247 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
249 // Get from Attribute Value
250 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(1).getValue();
251 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
252 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
254 // Rule Attribute added as value
255 else if (((jaxbActionTypes.get(0).getValue()) instanceof AttributeValueType)) {
256 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(0).getValue();
257 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
258 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
260 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue();
261 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
262 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0).getValue();
263 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
265 ruleAlgorithmList.add(ruleMap);