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;
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.openecomp.policy.rest.adapter.PolicyRestAdapter;
34 import org.openecomp.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.openecomp.policy.common.logging.flexlogger.FlexLogger;
54 import org.openecomp.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(){}
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<String, String>();
67 private ArrayList<Object> ruleAlgorithmList;
69 public void prePopulateActionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
70 attributeList = new ArrayList<Object>();
71 ruleAlgorithmList = new ArrayList<Object>();
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().substring(policyAdapter.getPolicyName().indexOf("_") + 1);
80 policyAdapter.setPolicyName(policyNameValue);
81 String description = "";
83 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
85 description = policy.getDescription();
87 policyAdapter.setPolicyDescription(description);
88 // Get the target data under policy for Action.
89 TargetType target = policy.getTarget();
91 // under target we have AnyOFType
92 List<AnyOfType> anyOfList = target.getAnyOf();
93 if (anyOfList != null) {
94 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
95 while (iterAnyOf.hasNext()) {
96 AnyOfType anyOf = iterAnyOf.next();
97 // Under AntOfType we have AllOfType
98 List<AllOfType> allOfList = anyOf.getAllOf();
99 if (allOfList != null) {
100 Iterator<AllOfType> iterAllOf = allOfList.iterator();
101 while (iterAllOf.hasNext()) {
102 AllOfType allOf = iterAllOf.next();
103 // Under AllOfType we have Mathch.
104 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 // Component attributes are saved under Target here we are fetching them back.
118 // One row is default so we are not adding dynamic component at index 0.
119 Map<String, String> attribute = new HashMap<String, String>();
120 attribute.put("key", attributeId);
121 attribute.put("value", value);
122 attributeList.add(attribute);
125 policyAdapter.setAttributes(attributeList);
131 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
132 // Under rule we have Condition and obligation.
133 for (Object o : ruleList) {
134 if (o instanceof RuleType) {
135 ConditionType condition = ((RuleType) o).getCondition();
136 ObligationExpressionsType obligations = ((RuleType) o).getObligationExpressions();
137 if (condition != null) {
139 ApplyType actionApply = (ApplyType) condition.getExpression().getValue();
140 ruleAlgoirthmTracker = new LinkedList<Integer>();
141 // Populating Rule Algorithms starting from compound.
142 prePopulateCompoundRuleAlgorithm(index, actionApply);
144 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
145 // get the Obligation data under the rule for Form elements.
146 if (obligations != null) {
147 // Under the obligationExpressions we have obligationExpression.
148 List<ObligationExpressionType> obligationList = obligations.getObligationExpression();
149 if (obligationList != null) {
150 Iterator<ObligationExpressionType> iterObligation = obligationList.iterator();
151 while (iterObligation.hasNext()) {
152 ObligationExpressionType obligation = iterObligation.next();
153 policyAdapter.setActionAttributeValue(obligation.getObligationId());
154 // Under the obligationExpression we have attributeAssignmentExpression.
155 List<AttributeAssignmentExpressionType> attributeAssignmentExpressionList = obligation.getAttributeAssignmentExpression();
156 if (attributeAssignmentExpressionList != null) {
157 Iterator<AttributeAssignmentExpressionType> iterAttributeAssignmentExpression = attributeAssignmentExpressionList.iterator();
158 while (iterAttributeAssignmentExpression.hasNext()) {
159 AttributeAssignmentExpressionType attributeAssignmentExpression = iterAttributeAssignmentExpression.next();
160 String attributeID = attributeAssignmentExpression.getAttributeId();
161 AttributeValueType attributeValue = (AttributeValueType) attributeAssignmentExpression.getExpression().getValue();
162 if (attributeID.equals(PERFORMER_ATTRIBUTEID)) {
163 for (String key : performer.keySet()) {
164 String keyValue = performer.get(key);
165 if (keyValue.equals(attributeValue.getContent().get(0))) {
166 policyAdapter.setActionPerformer(key);
181 private int prePopulateCompoundRuleAlgorithm(int index, ApplyType actionApply) {
182 boolean isCompoundRule = true;
183 List<JAXBElement<?>> jaxbActionTypes = actionApply.getExpression();
184 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
185 // If There is Attribute Value under Action Type that means we came to the final child
186 if (LOGGER.isDebugEnabled()) {
187 LOGGER.debug("Prepopulating rule algoirthm: " + index);
189 // Check to see if Attribute Value exists, if yes then it is not a compound rule
190 if (jaxbElement.getValue() instanceof AttributeValueType) {
191 prePopulateRuleAlgorithms(index, actionApply, jaxbActionTypes);
192 ruleAlgoirthmTracker.addLast(index);
193 isCompoundRule = false;
197 if (isCompoundRule) {
198 // As it's compound rule, Get the Apply types
199 for (JAXBElement<?> jaxbElement : jaxbActionTypes) {
200 ApplyType innerActionApply = (ApplyType) jaxbElement.getValue();
201 index = prePopulateCompoundRuleAlgorithm(index, innerActionApply);
203 // Populate combo box
204 if (LOGGER.isDebugEnabled()) {
205 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
207 Map<String, String> rule = new HashMap<String, String>();
208 for (String key : PolicyController.getDropDownMap().keySet()) {
209 String keyValue = PolicyController.getDropDownMap().get(key);
210 if (keyValue.equals(actionApply.getFunctionId())) {
211 rule.put("dynamicRuleAlgorithmCombo", key);
214 rule.put("id", "A" + (index +1));
215 // Populate Key and values for Compound Rule
216 rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
217 ruleAlgoirthmTracker.removeLast();
218 rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
219 ruleAlgoirthmTracker.removeLast();
220 ruleAlgoirthmTracker.addLast(index);
221 ruleAlgorithmList.add(rule);
227 private void prePopulateRuleAlgorithms(int index, ApplyType actionApply, List<JAXBElement<?>> jaxbActionTypes) {
228 Map<String, String> ruleMap = new HashMap<String, String>();
229 ruleMap.put("id", "A" + (index +1));
230 // Populate combo box
231 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
232 for (String key : dropDownMap.keySet()) {
233 String keyValue = dropDownMap.get(key);
234 if (keyValue.equals(actionApply.getFunctionId())) {
235 ruleMap.put("dynamicRuleAlgorithmCombo", key);
238 // Populate the key and value fields
239 // Rule Attribute added as key
240 if ((jaxbActionTypes.get(0).getValue()) instanceof ApplyType) {
241 // Get from Attribute Designator
242 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(0).getValue();
243 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
244 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0).getValue();
245 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
247 // Get from Attribute Value
248 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(1).getValue();
249 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
250 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
252 // Rule Attribute added as value
253 else if (((jaxbActionTypes.get(0).getValue()) instanceof AttributeValueType)) {
254 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbActionTypes.get(0).getValue();
255 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
256 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
258 ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue();
259 List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
260 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0).getValue();
261 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
263 ruleAlgorithmList.add(ruleMap);