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