2 * ============LICENSE_START=======================================================
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
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 java.io.BufferedWriter;
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;
33 import java.util.Objects;
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;
44 import com.att.research.xacml.api.pap.PAPException;
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;
62 public class ActionPolicy extends Policy {
67 private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicy.class);
69 public static final String JSON_CONFIG = "JSON";
71 public static final String PDP_ACTION = "PDP";
72 public static final String PEP_ACTION = "PEP";
73 public static final String TYPE_ACTION = "REST";
75 public static final String GET_METHOD = "GET";
76 public static final String PUT_METHOD = "PUT";
77 public static final String POST_METHOD = "POST";
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";
86 List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
87 List<String> dynamicFieldFunctionRuleAlgorithms = new LinkedList<>();
88 List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
89 List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
92 private CommonClassDao commonClassDao;
94 private static boolean isAttribute = false;
96 private synchronized static boolean getAttribute() {
101 public ActionPolicy() {
105 public ActionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao) {
106 this.policyAdapter = policyAdapter;
107 this.commonClassDao = commonClassDao;
111 public Map<String, String> savePolicies() throws PAPException {
113 Map<String, String> successMap = new HashMap<>();
114 if (isPolicyExists()) {
115 successMap.put("EXISTS", "This Policy already exist on the PAP");
119 if (!ActionPolicy.getAttribute()) {
120 successMap.put("invalidAttribute", "Action Attrbute was not in the database.");
124 if (!isPreparedToSave()) {
125 //Prep and configure the policy for saving
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());
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
139 public boolean prepareToSave() throws PAPException {
141 if (isPreparedToSave()) {
142 //we have already done this
147 String policyID = policyAdapter.getPolicyID();
148 version = policyAdapter.getHighestVersion();
150 // Create the Instance for pojo, PolicyType object is used in marshalling.
151 if (policyAdapter.getPolicyType().equals("Action")) {
152 PolicyType policyConfig = new PolicyType();
154 policyConfig.setVersion(Integer.toString(version));
155 policyConfig.setPolicyId(policyID);
156 policyConfig.setTarget(new TargetType());
157 policyAdapter.setData(policyConfig);
160 policyName = policyAdapter.getNewFileName();
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();
168 //if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the
170 if (!(actionBody == null || "".equals(actionBody))) {
171 saveActionBody(policyName, actionBody);
174 if (!getAttribute()) {
175 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue +
176 " in the ActionPolicyDict table.");
181 PolicyType actionPolicy = (PolicyType) policyAdapter.getData();
182 actionPolicy.setDescription(policyAdapter.getPolicyDescription());
183 actionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
185 AllOfType allOf = new AllOfType();
187 Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
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);
197 AnyOfType anyOf = new AnyOfType();
198 anyOf.getAllOf().add(allOf);
200 TargetType target = new TargetType();
201 target.getAnyOf().add(anyOf);
203 // Adding the target to the policy element
204 actionPolicy.setTarget(target);
206 RuleType rule = new RuleType();
207 rule.setRuleId(policyAdapter.getRuleID());
209 rule.setEffect(EffectType.PERMIT);
210 rule.setTarget(new TargetType());
212 dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
213 dynamicFieldFunctionRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
214 dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
215 dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
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;
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();
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));
238 // if rule algorithm not a compound
240 condition.setExpression(new ObjectFactory().createApply(getInnerActionApply(
241 dynamicLabelRuleAlgorithms.get(index))));
243 rule.setCondition(condition);
245 // set the obligations to rule
246 rule.setObligationExpressions(getObligationExpressions());
247 actionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
248 policyAdapter.setPolicyData(actionPolicy);
250 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object." + Objects
251 .requireNonNull(policyAdapter.getData()).getClass().getCanonicalName());
254 setPreparedToSave(true);
258 private static synchronized void setAttribute(boolean b) {
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", "");
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());
273 } catch (IOException e) {
274 LOGGER.error("Exception Occured" + e);
278 // Data required for obligation part is setting here.
279 private ObligationExpressionsType getObligationExpressions() {
280 ObligationExpressionsType obligations = new ObligationExpressionsType();
282 ObligationExpressionType obligation = new ObligationExpressionType();
283 String comboDictValue = policyAdapter.getActionAttribute();
284 obligation.setObligationId(comboDictValue);
285 obligation.setFulfillOn(EffectType.PERMIT);
287 // Add Action Assignment:
288 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
289 assignment1.setAttributeId(PERFORMER_ATTRIBUTEID);
290 assignment1.setCategory(CATEGORY_RECIPIENT_SUBJECT);
292 AttributeValueType actionNameAttributeValue = new AttributeValueType();
293 actionNameAttributeValue.setDataType(STRING_DATATYPE);
294 actionNameAttributeValue.getContent().add(performer.get(policyAdapter.getActionPerformer()));
296 assignment1.setExpression(new ObjectFactory().createAttributeValue(actionNameAttributeValue));
297 obligation.getAttributeAssignmentExpression().add(assignment1);
299 // Add Type Assignment:
300 AttributeAssignmentExpressionType assignmentType = new AttributeAssignmentExpressionType();
301 assignmentType.setAttributeId(TYPE_ATTRIBUTEID);
302 assignmentType.setCategory(CATEGORY_RESOURCE);
304 AttributeValueType typeAttributeValue = new AttributeValueType();
305 typeAttributeValue.setDataType(STRING_DATATYPE);
306 String actionDictType = policyAdapter.getActionDictType();
307 typeAttributeValue.getContent().add(actionDictType);
309 assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
310 obligation.getAttributeAssignmentExpression().add(assignmentType);
312 // Add Rest_URL Assignment:
313 AttributeAssignmentExpressionType assignmentURL = new AttributeAssignmentExpressionType();
314 assignmentURL.setAttributeId(URL_ATTRIBUTEID);
315 assignmentURL.setCategory(CATEGORY_RESOURCE);
317 AttributeValueType actionURLAttributeValue = new AttributeValueType();
318 actionURLAttributeValue.setDataType(URI_DATATYPE);
319 String actionDictUrl = policyAdapter.getActionDictUrl();
320 actionURLAttributeValue.getContent().add(actionDictUrl);
322 assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
323 obligation.getAttributeAssignmentExpression().add(assignmentURL);
325 // Add Method Assignment:
326 AttributeAssignmentExpressionType assignmentMethod = new AttributeAssignmentExpressionType();
327 assignmentMethod.setAttributeId(METHOD_ATTRIBUTEID);
328 assignmentMethod.setCategory(CATEGORY_RESOURCE);
330 AttributeValueType methodAttributeValue = new AttributeValueType();
331 methodAttributeValue.setDataType(STRING_DATATYPE);
332 String actionDictMethod = policyAdapter.getActionDictMethod();
333 methodAttributeValue.getContent().add(actionDictMethod);
335 assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
336 obligation.getAttributeAssignmentExpression().add(assignmentMethod);
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);
345 AttributeValueType jsonURLAttributeValue = new AttributeValueType();
346 jsonURLAttributeValue.setDataType(URI_DATATYPE);
347 jsonURLAttributeValue.getContent().add(CONFIG_URL + "/Action/" + policyName + ".json");
349 assignmentJsonURL.setExpression(new ObjectFactory().createAttributeValue(jsonURLAttributeValue));
350 obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
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]));
365 obligations.getObligationExpression().add(obligation);
370 // if compound setting the inner apply here
371 protected ApplyType getInnerActionApply(String value1Label) {
372 ApplyType actionApply = new ApplyType();
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);
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);
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);
409 attributeDesignator1.setCategory(CATEGORY_RESOURCE);
410 attributeDesignator2.setCategory(CATEGORY_RESOURCE);
412 // Here set actual field values
414 .setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
416 .setAttributeId(value1.contains("resource:") ? value1.substring(9) : value1.substring(8));
418 innerActionApply1.getExpression()
419 .add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
420 innerActionApply2.getExpression()
421 .add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
423 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply1));
424 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply2));
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();
431 if (functionKey.toLowerCase().contains("integer")) {
432 innerActionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
433 actionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
434 attributeDesignator.setDataType(INTEGER_DATATYPE);
436 innerActionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
437 actionConditionAttributeValue.setDataType(STRING_DATATYPE);
438 attributeDesignator.setDataType(STRING_DATATYPE);
441 String attributeId = null;
442 String attributeValue = null;
444 // Find which textField has rule attribute and set it as
445 attributeId = value1;
446 attributeValue = value2;
448 if (attributeId != null) {
449 attributeDesignator.setCategory(CATEGORY_RESOURCE);
450 attributeDesignator.setAttributeId(attributeId);
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));
461 actionApply.getExpression()
462 .add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
463 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
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)));
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);
490 AttributeValueType headersAttributeValue = new AttributeValueType();
491 headersAttributeValue.setDataType(STRING_DATATYPE);
492 headersAttributeValue.getContent().add(value);
494 assignmentHeaders.setExpression(new ObjectFactory().createAttributeValue(headersAttributeValue));
495 return assignmentHeaders;
499 public Object getCorrectPolicyDataObject() {
500 return policyAdapter.getPolicyData();
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();