Upgraded the latest ONAP SDK
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / ActionPolicyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controller;
22
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;
28 import java.util.Map;
29
30 import javax.xml.bind.JAXBElement;
31
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.rest.adapter.PolicyRestAdapter;
35 import org.onap.policy.rest.jpa.PolicyEntity;
36 import org.onap.portalsdk.core.controller.RestrictedBaseController;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39
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
54 @Controller
55 @RequestMapping({ "/" })
56 public class ActionPolicyController extends RestrictedBaseController {
57     private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyController.class);
58
59     public ActionPolicyController() {
60         // Default Constructor
61     }
62
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;
68
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");
74
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 = "";
83             try {
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();
88             }
89             policyAdapter.setPolicyDescription(description);
90             // Get the target data under policy for Action.
91             TargetType target = policy.getTarget();
92             if (target != null) {
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();
111                                         //
112                                         // Under the match we have attributevalue and
113                                         // attributeDesignator. So,finally down to the actual attribute.
114                                         //
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);
125                                     }
126                                 }
127                                 policyAdapter.setAttributes(attributeList);
128                             }
129                         }
130                     }
131                 }
132
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) {
140                             int index = 0;
141                             ApplyType actionApply = (ApplyType) condition.getExpression().getValue();
142                             ruleAlgoirthmTracker = new LinkedList<>();
143                             // Populating Rule Algorithms starting from compound.
144                             prePopulateCompoundRuleAlgorithm(index, actionApply);
145                         }
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
161                                                 .iterator();
162                                         while (iterAttributeAssignmentExpression.hasNext()) {
163                                             AttributeAssignmentExpressionType attributeAssignmentExpression = iterAttributeAssignmentExpression
164                                                     .next();
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);
173                                                     }
174                                                 }
175                                             }
176                                         }
177                                     }
178                                 }
179                             }
180                         }
181                     }
182                 }
183             }
184         }
185     }
186
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);
194             }
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;
200                 index++;
201             }
202         }
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);
208             }
209             // Populate combo box
210             if (LOGGER.isDebugEnabled()) {
211                 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
212             }
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);
218                 }
219             }
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);
228             index++;
229         }
230         return index;
231     }
232
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);
242             }
243         }
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)
251                     .getValue();
252             ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
253
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);
258         }
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);
264
265             ApplyType innerActionApply = (ApplyType) jaxbActionTypes.get(1).getValue();
266             List<JAXBElement<?>> jaxbInnerActionTypes = innerActionApply.getExpression();
267             AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerActionTypes.get(0)
268                     .getValue();
269             ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
270         }
271         ruleAlgorithmList.add(ruleMap);
272     }
273
274 }