Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / DecisionPolicyController.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 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.openecomp.portalsdk.core.controller.RestrictedBaseController;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.RequestMapping;
35
36 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
37 import org.openecomp.policy.common.logging.flexlogger.Logger;
38 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
39 import org.openecomp.policy.rest.jpa.PolicyEntity;
40
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
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.EffectType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
54
55 @Controller
56 @RequestMapping("/")
57 public class DecisionPolicyController extends RestrictedBaseController {
58         private static final Logger logger = FlexLogger.getLogger(DecisionPolicyController.class);
59         
60         public DecisionPolicyController(){}
61
62         protected PolicyRestAdapter policyAdapter = null;
63         private ArrayList<Object> attributeList;
64         private ArrayList<Object> decisionList;
65         private ArrayList<Object>  ruleAlgorithmList;
66         protected LinkedList<Integer> ruleAlgoirthmTracker;
67         public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not";
68
69         @SuppressWarnings("unchecked")
70         public void prePopulateDecisionPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
71                 attributeList = new ArrayList<Object>();
72                 decisionList = new ArrayList<Object>();
73                 ruleAlgorithmList = new ArrayList<Object>();
74                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
75                         Object policyData = policyAdapter.getPolicyData();
76                         PolicyType policy = (PolicyType) policyData;
77                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
78                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("_") + 1);
79                         policyAdapter.setPolicyName(policyNameValue);
80                         String description = "";
81                         try{
82                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
83                         }catch(Exception e){
84                                 description = policy.getDescription();
85                         }
86                         policyAdapter.setPolicyDescription(description);
87                         // Get the target data under policy for Action.
88                         TargetType target = policy.getTarget();
89                         if (target != null) {
90                                 // under target we have AnyOFType
91                                 List<AnyOfType> anyOfList = target.getAnyOf();
92                                 if (anyOfList != null) {
93                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
94                                         while (iterAnyOf.hasNext()) {
95                                                 AnyOfType anyOf = iterAnyOf.next();
96                                                 // Under AntOfType we have AllOfType
97                                                 List<AllOfType> allOfList = anyOf.getAllOf();
98                                                 if (allOfList != null) {
99                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
100                                                         while (iterAllOf.hasNext()) {
101                                                                 AllOfType allOf = iterAllOf.next();
102                                                                 // Under AllOfType we have Mathch.
103                                                                 List<MatchType> matchList = allOf.getMatch();
104                                                                 int index = 0;
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                                                                                 // First match in the target is EcompName, so set that value.
118                                                                                 if (index == 0) {
119                                                                                         policyAdapter.setEcompName(value);
120                                                                                 }
121                                                                                 // Component attributes are saved under Target here we are fetching  them back.
122                                                                                 // One row is default so we are not adding dynamic componet at index 0.
123                                                                                 if (index >= 1) {       
124                                                                                         Map<String, String> attribute = new HashMap<String, String>();
125                                                                                         attribute.put("key", attributeId);
126                                                                                         attribute.put("value", value);
127                                                                                         attributeList.add(attribute);   
128                                                                                 }
129                                                                                 index++;
130                                                                         }
131                                                                 }
132                                                                 policyAdapter.setAttributes(attributeList);
133                                                         }
134                                                 }
135                                         }
136                                 }
137
138                                 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
139                                 int index = 0;
140                                 for (Object object : ruleList) {
141                                         if (object instanceof VariableDefinitionType) {
142                                                 VariableDefinitionType variableDefinitionType = (VariableDefinitionType) object;
143                                                 Map<String, String> settings = new HashMap<String, String>();
144                                                 settings.put("key", variableDefinitionType.getVariableId());
145                                                 JAXBElement<AttributeValueType> attributeValueTypeElement = (JAXBElement<AttributeValueType>) variableDefinitionType.getExpression();
146                                                 if (attributeValueTypeElement != null) {
147                                                         AttributeValueType attributeValueType = attributeValueTypeElement.getValue();
148                                                         settings.put("value", attributeValueType.getContent().get(0).toString());
149                                                 }
150                                                 decisionList.add(settings);
151                                         } else if (object instanceof RuleType) {
152                                                 // get the condition data under the rule for rule Algorithms.
153                                                 if (((RuleType) object).getEffect().equals(EffectType.PERMIT)) {
154                                                         ConditionType condition = ((RuleType) object).getCondition();
155                                                         if (condition != null) {
156                                                                 ApplyType decisionApply = (ApplyType) condition.getExpression().getValue();
157                                                                 ruleAlgoirthmTracker = new LinkedList<Integer>();
158                                                                 // Populating Rule Algorithms starting from compound.
159                                                                 prePopulateDecisionCompoundRuleAlgorithm(index, decisionApply);
160                                                                 policyAdapter.setRuleAlgorithmschoices(ruleAlgorithmList);
161                                                         }
162                                                 }else if(((RuleType) object).getEffect().equals(EffectType.DENY)) {
163                                                         if(((RuleType) object).getAdviceExpressions()!=null){
164                                                                 if(((RuleType) object).getAdviceExpressions().getAdviceExpression().get(0).getAdviceId().toString().equalsIgnoreCase("AAF")){
165                                                                         policyAdapter.setRuleProvider("AAF");
166                                                                         break;
167                                                                 }
168                                                         }else{
169                                                                 policyAdapter.setRuleProvider("Custom");
170                                                         }
171                                                 }
172                                         }
173                                 }
174                         }
175                         policyAdapter.setSettings(decisionList);        
176                 }       
177
178         }
179
180         private void prePopulateDecisionRuleAlgorithms(int index, ApplyType decisionApply, List<JAXBElement<?>> jaxbDecisionTypes) {
181                 Map<String, String> ruleMap = new HashMap<String, String>();
182                 ruleMap.put("id", "A" + (index +1));
183                 Map<String, String> dropDownMap = PolicyController.getDropDownMap();
184                 for (String key : dropDownMap.keySet()) {
185                         String keyValue = dropDownMap.get(key);
186                         if (keyValue.equals(decisionApply.getFunctionId())) {
187                                 ruleMap.put("dynamicRuleAlgorithmCombo", key);
188                         }
189                 }
190                 // Populate the key and value fields
191                 if (((jaxbDecisionTypes.get(0).getValue()) instanceof AttributeValueType)) {
192                         ApplyType innerDecisionApply = (ApplyType) jaxbDecisionTypes.get(1).getValue();
193                         List<JAXBElement<?>> jaxbInnerDecisionTypes = innerDecisionApply.getExpression();
194                         if (jaxbInnerDecisionTypes.get(0).getValue() instanceof AttributeDesignatorType) {
195                                 AttributeDesignatorType attributeDesignator = (AttributeDesignatorType) jaxbInnerDecisionTypes.get(0).getValue();
196                                 ruleMap.put("dynamicRuleAlgorithmField1", attributeDesignator.getAttributeId());
197
198                                 // Get from Attribute Value
199                                 AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(0).getValue();
200                                 String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
201                                 ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
202                         }
203                 } else if ((jaxbDecisionTypes.get(0).getValue()) instanceof VariableReferenceType) {
204                         VariableReferenceType variableReference = (VariableReferenceType) jaxbDecisionTypes.get(0).getValue();  
205                         ruleMap.put("dynamicRuleAlgorithmField1", "S_"+ variableReference.getVariableId());
206
207
208                         // Get from Attribute Value
209                         AttributeValueType actionConditionAttributeValue = (AttributeValueType) jaxbDecisionTypes.get(1).getValue();
210                         String attributeValue = (String) actionConditionAttributeValue.getContent().get(0);
211                         ruleMap.put("dynamicRuleAlgorithmField2", attributeValue);
212                 }
213                 ruleAlgorithmList.add(ruleMap);
214         }
215
216         private int prePopulateDecisionCompoundRuleAlgorithm(int index, ApplyType decisionApply) {
217                 boolean isCompoundRule = true;
218                 List<JAXBElement<?>> jaxbDecisionTypes = decisionApply.getExpression();
219                 for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
220                         // If There is Attribute Value under Decision Type that means we came to the final child
221                         if (logger.isDebugEnabled()) {
222                                 logger.debug("Prepopulating rule algoirthm: " + index);
223                         }
224                         // Check to see if Attribute Value exists, if yes then it is not a compound rule
225                         if(jaxbElement.getValue() instanceof AttributeValueType) {
226                                 prePopulateDecisionRuleAlgorithms(index, decisionApply, jaxbDecisionTypes);
227                                 ruleAlgoirthmTracker.addLast(index);
228                                 isCompoundRule = false;
229                                 index++;
230                         } 
231                 }
232                 if (isCompoundRule) {
233                         // As it's compound rule, Get the Apply types
234                         for (JAXBElement<?> jaxbElement : jaxbDecisionTypes) {
235                                 ApplyType innerDecisionApply = (ApplyType) jaxbElement.getValue();
236                                 index = prePopulateDecisionCompoundRuleAlgorithm(index, innerDecisionApply);
237                         }
238                         // Populate combo box
239                         if (logger.isDebugEnabled()) {
240                                 logger.debug("Prepopulating Compound rule algorithm: " + index);
241                         }
242                         Map<String, String> rule = new HashMap<String, String>();
243                         for (String key : PolicyController.getDropDownMap().keySet()) {
244                                 String keyValue = PolicyController.getDropDownMap().get(key);
245                                 if (keyValue.equals(decisionApply.getFunctionId())) {
246                                         rule.put("dynamicRuleAlgorithmCombo", key);
247                                 }
248                         }
249
250                         rule.put("id", "A" + (index +1));
251                         // Populate Key and values for Compound Rule
252                         rule.put("dynamicRuleAlgorithmField1", "A" + (ruleAlgoirthmTracker.getLast() + 1 ));
253                         ruleAlgoirthmTracker.removeLast();
254                         rule.put("dynamicRuleAlgorithmField2", "A" + (ruleAlgoirthmTracker.getLast() + 1));
255                         ruleAlgoirthmTracker.removeLast();
256                         ruleAlgoirthmTracker.addLast(index);
257                         ruleAlgorithmList.add(rule);
258                         index++;
259                 }
260                 return index;
261         }
262 }