Policy TestSuite Enabled
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / ActionPolicyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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.openecomp.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.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;
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.openecomp.policy.common.logging.flexlogger.FlexLogger; 
54 import org.openecomp.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
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;
68
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");
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().substring(policyAdapter.getPolicyName().indexOf("_") + 1);
80                         policyAdapter.setPolicyName(policyNameValue);
81                         String description = "";
82                         try{
83                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
84                         }catch(Exception e){
85                                 description = policy.getDescription();
86                         }
87                         policyAdapter.setPolicyDescription(description);
88                         // Get the target data under policy for Action.
89                         TargetType target = policy.getTarget();
90                         if (target != null) {
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();
109                                                                                 //
110                                                                                 // Under the match we have attributevalue and
111                                                                                 // attributeDesignator. So,finally down to the actual attribute.
112                                                                                 //
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);   
123                                                                         }
124                                                                 }
125                                                                 policyAdapter.setAttributes(attributeList);
126                                                         }
127                                                 }
128                                         }       
129                                 }
130
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) {
138                                                         int index = 0;
139                                                         ApplyType actionApply = (ApplyType) condition.getExpression().getValue();
140                                                         ruleAlgoirthmTracker = new LinkedList<Integer>();
141                                                         // Populating Rule Algorithms starting from compound.
142                                                         prePopulateCompoundRuleAlgorithm(index, actionApply);
143                                                 }
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);
167                                                                                                         }
168                                                                                                 }
169                                                                                         }
170                                                                                 }
171                                                                         }
172                                                                 }
173                                                         }
174                                                 } 
175                                         }
176                                 }
177                         }
178                 }               
179         }
180
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);
188                         }
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;
194                                 index++;
195                         }
196                 }
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);
202                         }
203                         // Populate combo box
204                         if (LOGGER.isDebugEnabled()) {
205                                 LOGGER.debug("Prepopulating Compound rule algorithm: " + index);
206                         }
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);
212                                 }
213                         }
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);
222                         index++;
223                 }
224                 return index;
225         }
226
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);
236                         }
237                 }
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());
246
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);
251                 }
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);
257
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());
262                 }
263                 ruleAlgorithmList.add(ruleMap);
264         }
265
266 }