Merge "Added Junits for POLICY-SDK-APP controllers"
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / ActionPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.pap.xacml.rest.components;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.onap.policy.common.logging.eelf.MessageCodes;
35 import org.onap.policy.common.logging.eelf.PolicyLogger;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.rest.adapter.PolicyRestAdapter;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.FunctionDefinition;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
42
43 import com.att.research.xacml.api.pap.PAPException;
44
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; 
60
61 public class ActionPolicy extends Policy {
62     
63     /**
64      * ActionPolicy Fields
65      */
66     private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicy.class);
67     
68     public static final String JSON_CONFIG = "JSON";
69     
70     public static final String PDP_ACTION = "PDP";
71     public static final String PEP_ACTION = "PEP";
72     public static final String TYPE_ACTION = "REST";
73
74     public static final String GET_METHOD = "GET";
75     public static final String PUT_METHOD = "PUT";
76     public static final String POST_METHOD = "POST";
77
78     public static final String PERFORMER_ATTRIBUTEID = "performer";
79     public static final String TYPE_ATTRIBUTEID = "type";
80     public static final String METHOD_ATTRIBUTEID = "method";
81     public static final String HEADERS_ATTRIBUTEID = "headers";
82     public static final String URL_ATTRIBUTEID = "url";
83     public static final String BODY_ATTRIBUTEID = "body";
84     
85     List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
86     List<String> dynamicFieldFunctionRuleAlgorithms = new LinkedList<>();
87     List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
88     List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
89     
90
91     
92     private CommonClassDao commonClassDao;
93     
94     private static boolean isAttribute = false;
95     private synchronized static boolean getAttribute () {
96         return isAttribute;
97
98     }
99     
100     public ActionPolicy() {
101         super();
102     }
103     
104     public ActionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao){
105         this.policyAdapter = policyAdapter;
106         this.commonClassDao = commonClassDao;
107     }
108
109         @Override
110     public Map<String, String> savePolicies() throws PAPException {
111         
112         Map<String, String> successMap = new HashMap<>();
113         if(isPolicyExists()){
114             successMap.put("EXISTS", "This Policy already exist on the PAP");
115             return successMap;
116         }
117         
118         if(!ActionPolicy.getAttribute()) {
119             successMap.put("invalidAttribute", "Action Attrbute was not in the database.");
120             return successMap;
121         }
122         
123         if(!isPreparedToSave()){
124             //Prep and configure the policy for saving
125             prepareToSave();
126         }
127
128         // Until here we prepared the data and here calling the method to create xml.
129         Path newPolicyPath = null;
130         newPolicyPath = Paths.get(policyAdapter.getNewFileName());
131         successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject() );     
132         return successMap;      
133     }
134     
135     //This is the method for preparing the policy for saving.  We have broken it out
136     //separately because the fully configured policy is used for multiple things
137     @Override
138     public boolean prepareToSave() throws PAPException{
139
140         if(isPreparedToSave()){
141             //we have already done this
142             return true;
143         }
144         
145         int version = 0;
146         String policyID = policyAdapter.getPolicyID();
147         version = policyAdapter.getHighestVersion();
148         
149         // Create the Instance for pojo, PolicyType object is used in marshalling.
150         if (policyAdapter.getPolicyType().equals("Action")) {
151             PolicyType policyConfig = new PolicyType();
152
153             policyConfig.setVersion(Integer.toString(version));
154             policyConfig.setPolicyId(policyID);
155             policyConfig.setTarget(new TargetType());
156             policyAdapter.setData(policyConfig);
157         }
158         
159         policyName = policyAdapter.getNewFileName();
160         
161         if (policyAdapter.getData() != null) {
162             // Action body is optional so checking value provided or not
163             String comboDictValue = policyAdapter.getActionAttribute();
164             String actionBody = policyAdapter.getActionBody();
165             setAttribute(false);
166
167             //if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the dictionary
168             if(!(actionBody==null || "".equals(actionBody))){   
169                 saveActionBody(policyName, actionBody);
170                 setAttribute(true);
171             } else {
172                 if(!getAttribute()){
173                     LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue + " in the ActionPolicyDict table.");
174                     return false;
175                 }
176             }
177             
178             PolicyType actionPolicy = (PolicyType) policyAdapter.getData();
179             actionPolicy.setDescription(policyAdapter.getPolicyDescription());
180             actionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
181
182             AllOfType allOf = new AllOfType();
183             
184             Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
185             
186             // If there is any dynamic field attributes create the matches here
187             for (String keyField : dynamicFieldComponentAttributes.keySet()) {
188                 String key = keyField;
189                 String value = dynamicFieldComponentAttributes.get(key);
190                 MatchType dynamicMatch = createDynamicMatch(key, value);
191                 allOf.getMatch().add(dynamicMatch);
192             }
193
194             AnyOfType anyOf = new AnyOfType();
195             anyOf.getAllOf().add(allOf);
196
197             TargetType target = new TargetType();
198             target.getAnyOf().add(anyOf);
199             
200             // Adding the target to the policy element
201             actionPolicy.setTarget(target);
202             
203             RuleType rule = new RuleType();
204             rule.setRuleId(policyAdapter.getRuleID());
205
206             rule.setEffect(EffectType.PERMIT);
207             rule.setTarget(new TargetType());
208             
209             dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
210             dynamicFieldFunctionRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
211             dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
212             dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
213                         
214             // Rule attributes are optional and dynamic so check and add them to condition.
215             if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) {
216                 boolean isCompound = false;
217                 ConditionType condition = new ConditionType();
218                 int index = dynamicFieldOneRuleAlgorithms.size() - 1;
219
220                 for (String labelAttr : dynamicLabelRuleAlgorithms) {
221                     // if the rule algorithm as a label means it is a compound
222                     if (dynamicFieldOneRuleAlgorithms.get(index).equals(labelAttr)) {
223                         ApplyType actionApply = new ApplyType();
224
225                         String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index).toString();
226                         String value1 = dynamicFieldOneRuleAlgorithms.get(index);
227                         String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
228                         actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
229                         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
230                         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
231                         condition.setExpression(new ObjectFactory().createApply(actionApply));
232                         isCompound = true;
233                     }
234                 }
235                 // if rule algorithm not a compound
236                 if (!isCompound) {
237                     condition.setExpression(new ObjectFactory().createApply(getInnerActionApply(dynamicLabelRuleAlgorithms.get(index).toString())));
238                 }
239                 rule.setCondition(condition);
240             }
241             // set the obligations to rule
242             rule.setObligationExpressions(getObligationExpressions());
243             actionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
244             policyAdapter.setPolicyData(actionPolicy);
245         }  else {
246             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
247         }   
248
249         setPreparedToSave(true);
250         return true;
251     }
252     
253     private static synchronized void setAttribute(boolean b) {
254         isAttribute = b;
255     }
256
257     // Saving the json Configurations file if exists at server location for action policy.
258     private void saveActionBody(String policyName, String actionBodyData) {
259         try {
260             if(policyName.endsWith(".xml")){
261                 policyName = policyName.replace(".xml", "");
262             }
263             File file = new File(ACTION_HOME+ File.separator + policyName + ".json");
264             FileWriter fw = new FileWriter(file.getAbsoluteFile());
265             BufferedWriter bw = new BufferedWriter(fw);
266             bw.write(actionBodyData);
267             bw.close();
268             if (LOGGER.isInfoEnabled()) {
269                 LOGGER.info("Action Body is succesfully saved at " + file.getAbsolutePath());
270             }
271         } catch (IOException e) {
272             LOGGER.error("Exception Occured"+e);
273         }
274     }
275     
276     // Data required for obligation part is setting here.
277     private ObligationExpressionsType getObligationExpressions() {
278         ObligationExpressionsType obligations = new ObligationExpressionsType();
279
280         ObligationExpressionType obligation = new ObligationExpressionType();
281         String comboDictValue = policyAdapter.getActionAttribute();
282         obligation.setObligationId(comboDictValue);
283         obligation.setFulfillOn(EffectType.PERMIT);
284
285         // Add Action Assignment:
286         AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
287         assignment1.setAttributeId(PERFORMER_ATTRIBUTEID);
288         assignment1.setCategory(CATEGORY_RECIPIENT_SUBJECT);
289
290         AttributeValueType actionNameAttributeValue = new AttributeValueType();
291         actionNameAttributeValue.setDataType(STRING_DATATYPE);
292         actionNameAttributeValue.getContent().add(performer.get(policyAdapter.getActionPerformer()));
293
294         assignment1.setExpression(new ObjectFactory().createAttributeValue(actionNameAttributeValue));
295         obligation.getAttributeAssignmentExpression().add(assignment1);
296
297         // Add Type Assignment:
298         AttributeAssignmentExpressionType assignmentType = new AttributeAssignmentExpressionType();
299         assignmentType.setAttributeId(TYPE_ATTRIBUTEID);
300         assignmentType.setCategory(CATEGORY_RESOURCE);
301
302         AttributeValueType typeAttributeValue = new AttributeValueType();
303         typeAttributeValue.setDataType(STRING_DATATYPE);
304         String actionDictType = policyAdapter.getActionDictType();
305         typeAttributeValue.getContent().add(actionDictType);
306
307         assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
308         obligation.getAttributeAssignmentExpression().add(assignmentType);
309
310         // Add Rest_URL Assignment:
311         AttributeAssignmentExpressionType assignmentURL = new AttributeAssignmentExpressionType();
312         assignmentURL.setAttributeId(URL_ATTRIBUTEID);
313         assignmentURL.setCategory(CATEGORY_RESOURCE);
314
315         AttributeValueType actionURLAttributeValue = new AttributeValueType();
316         actionURLAttributeValue.setDataType(URI_DATATYPE);
317         String actionDictUrl = policyAdapter.getActionDictUrl();
318         actionURLAttributeValue.getContent().add(actionDictUrl);
319
320         assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
321         obligation.getAttributeAssignmentExpression().add(assignmentURL);
322
323         // Add Method Assignment:
324         AttributeAssignmentExpressionType assignmentMethod = new AttributeAssignmentExpressionType();
325         assignmentMethod.setAttributeId(METHOD_ATTRIBUTEID);
326         assignmentMethod.setCategory(CATEGORY_RESOURCE);
327
328         AttributeValueType methodAttributeValue = new AttributeValueType();
329         methodAttributeValue.setDataType(STRING_DATATYPE);
330         String actionDictMethod = policyAdapter.getActionDictMethod();
331         methodAttributeValue.getContent().add(actionDictMethod);
332
333         assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
334         obligation.getAttributeAssignmentExpression().add(assignmentMethod);
335
336         // Add JSON_URL Assignment:
337         String actionBody = policyAdapter.getActionBody();      
338         if (actionBody != null) {
339             AttributeAssignmentExpressionType assignmentJsonURL = new AttributeAssignmentExpressionType();
340             assignmentJsonURL.setAttributeId(BODY_ATTRIBUTEID);
341             assignmentJsonURL.setCategory(CATEGORY_RESOURCE);
342
343             AttributeValueType jsonURLAttributeValue = new AttributeValueType();
344             jsonURLAttributeValue.setDataType(URI_DATATYPE);
345             jsonURLAttributeValue.getContent().add(CONFIG_URL + "/Action/"  + policyName + ".json");
346
347             assignmentJsonURL.setExpression(new ObjectFactory().createAttributeValue(jsonURLAttributeValue));
348             obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
349         }
350
351         String headerVal = policyAdapter.getActionDictHeader();
352         if(headerVal != null && !headerVal.trim().isEmpty()){
353             // parse it on : to get number of headers
354             String[] result = headerVal.split(":");
355             for (String eachString : result){
356                 // parse each value on =
357                 String[] textFieldVals = eachString.split("=");
358                 obligation.getAttributeAssignmentExpression().add(addDynamicHeaders(textFieldVals[0], textFieldVals[1]));
359             }
360         }
361             
362         obligations.getObligationExpression().add(obligation);
363         return obligations;
364     }
365
366     
367     // if compound setting the inner apply here
368     protected ApplyType getInnerActionApply(String value1Label) {
369         ApplyType actionApply = new ApplyType();
370         int index = 0;
371         // check the index for the label.
372         for (String labelAttr : dynamicLabelRuleAlgorithms) {
373             if (labelAttr.equals(value1Label)) {
374                 String value1 = dynamicFieldOneRuleAlgorithms.get(index).toString();
375                 // check if the row contains label again
376                 for (String labelValue : dynamicLabelRuleAlgorithms) {
377                     if (labelValue.equals(value1)) {
378                         return getCompoundApply(index);
379                     }
380                 }
381
382                 // Getting the values from the form.
383                 String functionKey = dynamicFieldFunctionRuleAlgorithms.get(index);
384                 String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
385                 actionApply.setFunctionId(getFunctionDefinitionId(functionKey));
386                 // if two text field are rule attributes.
387                 if ((value1.contains(RULE_VARIABLE)) && (value2.contains(RULE_VARIABLE))) {
388                     ApplyType innerActionApply1 = new ApplyType();
389                     ApplyType innerActionApply2 = new ApplyType();
390                     AttributeDesignatorType attributeDesignator1 = new AttributeDesignatorType();
391                     AttributeDesignatorType attributeDesignator2 = new AttributeDesignatorType();
392                     // If selected function is Integer function set integer functionID
393                     if (functionKey.toLowerCase().contains("integer")) {
394                         innerActionApply1.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
395                         innerActionApply2.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
396                         attributeDesignator1.setDataType(INTEGER_DATATYPE);
397                         attributeDesignator2.setDataType(INTEGER_DATATYPE);
398                     } else {
399                         // If selected function is not a Integer function
400                         // set String functionID
401                         innerActionApply1.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
402                         innerActionApply2.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
403                         attributeDesignator1.setDataType(STRING_DATATYPE);
404                         attributeDesignator2.setDataType(STRING_DATATYPE);
405                     }
406                     attributeDesignator1.setCategory(CATEGORY_RESOURCE);
407                     attributeDesignator2.setCategory(CATEGORY_RESOURCE);
408
409                     // Here set actual field values
410                     attributeDesignator1.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
411                     attributeDesignator2.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
412
413                     innerActionApply1.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
414                     innerActionApply2.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
415
416                     actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply1));
417                     actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply2));
418
419                 } else {// if either of one text field is rule attribute.
420                     ApplyType innerActionApply = new ApplyType();
421                     AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
422                     AttributeValueType actionConditionAttributeValue = new AttributeValueType();
423
424                     if (functionKey.toLowerCase().contains("integer")) {
425                         innerActionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
426                         actionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
427                         attributeDesignator.setDataType(INTEGER_DATATYPE);
428                     } else {
429                         innerActionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
430                         actionConditionAttributeValue.setDataType(STRING_DATATYPE);
431                         attributeDesignator.setDataType(STRING_DATATYPE);
432                     }
433
434                     String attributeId = null;
435                     String attributeValue = null;
436
437                     // Find which textField has rule attribute and set it as
438                     attributeId = value1;
439                     attributeValue = value2;
440
441                     if (attributeId != null) {
442                         attributeDesignator.setCategory(CATEGORY_RESOURCE);
443                         attributeDesignator.setAttributeId(attributeId);
444                     }
445                     actionConditionAttributeValue.getContent().add(attributeValue);
446                     innerActionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
447                     // Decide the order of element based the values.
448                     if (attributeId.equals(value1)) {
449                         actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
450                         actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
451                     } else {
452                         actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
453                         actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
454                     }
455                 }
456             }
457             index++;
458         }
459         return actionApply;
460     }
461
462     // if the rule algorithm is multiple compound one setting the apply
463     protected ApplyType getCompoundApply(int index) {
464         ApplyType actionApply = new ApplyType();
465         String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index);
466         String value1 = dynamicFieldOneRuleAlgorithms.get(index);
467         String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
468         actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
469         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
470         actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
471         return actionApply;
472     }
473         
474     // Adding the dynamic headers if any
475     private AttributeAssignmentExpressionType addDynamicHeaders(String header, String value) {
476         AttributeAssignmentExpressionType assignmentHeaders = new AttributeAssignmentExpressionType();
477         assignmentHeaders.setAttributeId("headers:" + header);
478         assignmentHeaders.setCategory(CATEGORY_RESOURCE);
479
480         AttributeValueType headersAttributeValue = new AttributeValueType();
481         headersAttributeValue.setDataType(STRING_DATATYPE);
482         headersAttributeValue.getContent().add(value);
483
484         assignmentHeaders.setExpression(new ObjectFactory().createAttributeValue(headersAttributeValue));
485         return assignmentHeaders;
486     }
487
488     @Override
489     public Object getCorrectPolicyDataObject() {
490         return policyAdapter.getPolicyData();
491     }
492     
493     public String getFunctionDefinitionId(String key){
494         FunctionDefinition object = (FunctionDefinition) commonClassDao.getDataById(FunctionDefinition.class, "short_name", key);
495         if(object != null){
496                 return object.getXacmlid();
497         }
498         return null;
499     }
500
501 }