[POLICY-73] replace openecomp for policy-engine
[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
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;
29 import java.util.Map;
30
31 import javax.xml.bind.JAXBElement;
32
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;
38
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 import org.onap.policy.common.logging.flexlogger.FlexLogger; 
54 import org.onap.policy.common.logging.flexlogger.Logger;
55
56 @Controller
57 @RequestMapping({"/"})
58 public class ActionPolicyController extends RestrictedBaseController{
59         private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyController.class);
60         
61         public ActionPolicyController(){
62                 //Default Constructor
63         }
64
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;
70
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");
76
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 = "";
84                         try{
85                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
86                         }catch(Exception 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.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);
169                                                                                                         }
170                                                                                                 }
171                                                                                         }
172                                                                                 }
173                                                                         }
174                                                                 }
175                                                         }
176                                                 } 
177                                         }
178                                 }
179                         }
180                 }               
181         }
182
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);
190                         }
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;
196                                 index++;
197                         }
198                 }
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);
204                         }
205                         // Populate combo box
206                         if (LOGGER.isDebugEnabled()) {
207                                 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
208                         }
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);
214                                 }
215                         }
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);
224                         index++;
225                 }
226                 return index;
227         }
228
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);
238                         }
239                 }
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());
248
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);
253                 }
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);
259
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());
264                 }
265                 ruleAlgorithmList.add(ruleMap);
266         }
267
268 }