2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.xacml.rest.components;
23 import com.att.research.xacml.api.pap.PAPException;
24 import java.io.BufferedWriter;
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;
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;
59 public class ActionPolicy extends Policy {
61 private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicy.class);
63 public static final String JSON_CONFIG = "JSON";
65 public static final String PDP_ACTION = "PDP";
66 public static final String PEP_ACTION = "PEP";
67 public static final String TYPE_ACTION = "REST";
69 public static final String GET_METHOD = "GET";
70 public static final String PUT_METHOD = "PUT";
71 public static final String POST_METHOD = "POST";
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";
80 List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
81 List<String> dynamicFieldFunctionRuleAlgorithms = new LinkedList<>();
82 List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
83 List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
86 private CommonClassDao commonClassDao;
88 private static boolean isAttribute = false;
90 private static synchronized boolean getAttribute() {
95 public ActionPolicy() {
99 public ActionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao) {
100 this.policyAdapter = policyAdapter;
101 this.commonClassDao = commonClassDao;
105 public Map<String, String> savePolicies() throws PAPException {
107 Map<String, String> successMap = new HashMap<>();
108 if (isPolicyExists()) {
109 successMap.put("EXISTS", "This Policy already exist on the PAP");
113 if (!ActionPolicy.getAttribute()) {
114 successMap.put("invalidAttribute", "Action Attrbute was not in the database.");
118 if (!isPreparedToSave()) {
119 // Prep and configure the policy for saving
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());
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
133 public boolean prepareToSave() throws PAPException {
135 if (isPreparedToSave()) {
136 // we have already done this
141 String policyID = policyAdapter.getPolicyID();
142 version = policyAdapter.getHighestVersion();
144 // Create the Instance for pojo, PolicyType object is used in marshalling.
145 if (policyAdapter.getPolicyType().equals("Action")) {
146 PolicyType policyConfig = new PolicyType();
148 policyConfig.setVersion(Integer.toString(version));
149 policyConfig.setPolicyId(policyID);
150 policyConfig.setTarget(new TargetType());
151 policyAdapter.setData(policyConfig);
154 policyName = policyAdapter.getNewFileName();
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();
162 // if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the
164 if (!(actionBody == null || "".equals(actionBody))) {
165 saveActionBody(policyName, actionBody);
168 if (!getAttribute()) {
169 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue
170 + " in the ActionPolicyDict table.");
175 PolicyType actionPolicy = (PolicyType) policyAdapter.getData();
176 actionPolicy.setDescription(policyAdapter.getPolicyDescription());
177 actionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
179 AllOfType allOf = new AllOfType();
181 Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
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);
191 AnyOfType anyOf = new AnyOfType();
192 anyOf.getAllOf().add(allOf);
194 TargetType target = new TargetType();
195 target.getAnyOf().add(anyOf);
197 // Adding the target to the policy element
198 actionPolicy.setTarget(target);
200 RuleType rule = new RuleType();
201 rule.setRuleId(policyAdapter.getRuleID());
203 rule.setEffect(EffectType.PERMIT);
204 rule.setTarget(new TargetType());
206 dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
207 dynamicFieldFunctionRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
208 dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
209 dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
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;
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();
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));
232 // if rule algorithm not a compound
234 condition.setExpression(new ObjectFactory()
235 .createApply(getInnerActionApply(dynamicLabelRuleAlgorithms.get(index))));
237 rule.setCondition(condition);
239 // set the obligations to rule
240 rule.setObligationExpressions(getObligationExpressions());
241 actionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
242 policyAdapter.setPolicyData(actionPolicy);
244 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object."
245 + Objects.requireNonNull(policyAdapter.getData()).getClass().getCanonicalName());
248 setPreparedToSave(true);
252 private static synchronized void setAttribute(boolean b) {
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", "");
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());
267 } catch (IOException e) {
268 LOGGER.error("Exception Occured" + e);
272 // Data required for obligation part is setting here.
273 private ObligationExpressionsType getObligationExpressions() {
274 ObligationExpressionsType obligations = new ObligationExpressionsType();
276 ObligationExpressionType obligation = new ObligationExpressionType();
277 String comboDictValue = policyAdapter.getActionAttribute();
278 obligation.setObligationId(comboDictValue);
279 obligation.setFulfillOn(EffectType.PERMIT);
281 // Add Action Assignment:
282 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
283 assignment1.setAttributeId(PERFORMER_ATTRIBUTEID);
284 assignment1.setCategory(CATEGORY_RECIPIENT_SUBJECT);
286 AttributeValueType actionNameAttributeValue = new AttributeValueType();
287 actionNameAttributeValue.setDataType(STRING_DATATYPE);
288 actionNameAttributeValue.getContent().add(performer.get(policyAdapter.getActionPerformer()));
290 assignment1.setExpression(new ObjectFactory().createAttributeValue(actionNameAttributeValue));
291 obligation.getAttributeAssignmentExpression().add(assignment1);
293 // Add Type Assignment:
294 AttributeAssignmentExpressionType assignmentType = new AttributeAssignmentExpressionType();
295 assignmentType.setAttributeId(TYPE_ATTRIBUTEID);
296 assignmentType.setCategory(CATEGORY_RESOURCE);
298 AttributeValueType typeAttributeValue = new AttributeValueType();
299 typeAttributeValue.setDataType(STRING_DATATYPE);
300 String actionDictType = policyAdapter.getActionDictType();
301 typeAttributeValue.getContent().add(actionDictType);
303 assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
304 obligation.getAttributeAssignmentExpression().add(assignmentType);
306 // Add Rest_URL Assignment:
307 AttributeAssignmentExpressionType assignmentURL = new AttributeAssignmentExpressionType();
308 assignmentURL.setAttributeId(URL_ATTRIBUTEID);
309 assignmentURL.setCategory(CATEGORY_RESOURCE);
311 AttributeValueType actionURLAttributeValue = new AttributeValueType();
312 actionURLAttributeValue.setDataType(URI_DATATYPE);
313 String actionDictUrl = policyAdapter.getActionDictUrl();
314 actionURLAttributeValue.getContent().add(actionDictUrl);
316 assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
317 obligation.getAttributeAssignmentExpression().add(assignmentURL);
319 // Add Method Assignment:
320 AttributeAssignmentExpressionType assignmentMethod = new AttributeAssignmentExpressionType();
321 assignmentMethod.setAttributeId(METHOD_ATTRIBUTEID);
322 assignmentMethod.setCategory(CATEGORY_RESOURCE);
324 AttributeValueType methodAttributeValue = new AttributeValueType();
325 methodAttributeValue.setDataType(STRING_DATATYPE);
326 String actionDictMethod = policyAdapter.getActionDictMethod();
327 methodAttributeValue.getContent().add(actionDictMethod);
329 assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
330 obligation.getAttributeAssignmentExpression().add(assignmentMethod);
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);
339 AttributeValueType jsonURLAttributeValue = new AttributeValueType();
340 jsonURLAttributeValue.setDataType(URI_DATATYPE);
341 jsonURLAttributeValue.getContent().add(CONFIG_URL + "/Action/" + policyName + ".json");
343 assignmentJsonURL.setExpression(new ObjectFactory().createAttributeValue(jsonURLAttributeValue));
344 obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
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]));
359 obligations.getObligationExpression().add(obligation);
364 // if compound setting the inner apply here
365 protected ApplyType getInnerActionApply(String value1Label) {
366 ApplyType actionApply = new ApplyType();
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);
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);
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);
403 attributeDesignator1.setCategory(CATEGORY_RESOURCE);
404 attributeDesignator2.setCategory(CATEGORY_RESOURCE);
406 // Here set actual field values
408 .setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
410 .setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
412 innerActionApply1.getExpression()
413 .add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
414 innerActionApply2.getExpression()
415 .add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
417 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply1));
418 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply2));
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();
425 if (functionKey.toLowerCase().contains("integer")) {
426 innerActionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
427 actionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
428 attributeDesignator.setDataType(INTEGER_DATATYPE);
430 innerActionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
431 actionConditionAttributeValue.setDataType(STRING_DATATYPE);
432 attributeDesignator.setDataType(STRING_DATATYPE);
435 String attributeId = null;
436 String attributeValue = null;
438 // Find which textField has rule attribute and set it as
439 attributeId = value1;
440 attributeValue = value2;
442 if (attributeId != null) {
443 attributeDesignator.setCategory(CATEGORY_RESOURCE);
444 attributeDesignator.setAttributeId(attributeId);
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));
455 actionApply.getExpression()
456 .add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
457 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
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)));
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);
484 AttributeValueType headersAttributeValue = new AttributeValueType();
485 headersAttributeValue.setDataType(STRING_DATATYPE);
486 headersAttributeValue.getContent().add(value);
488 assignmentHeaders.setExpression(new ObjectFactory().createAttributeValue(headersAttributeValue));
489 return assignmentHeaders;
493 public Object getCorrectPolicyDataObject() {
494 return policyAdapter.getPolicyData();
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();