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