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;
34 import org.onap.policy.common.logging.eelf.MessageCodes;
35 import org.onap.policy.common.logging.eelf.PolicyLogger;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.rest.adapter.PolicyRestAdapter;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.FunctionDefinition;
41 import org.onap.policy.xacml.api.XACMLErrorConstants;
43 import com.att.research.xacml.api.pap.PAPException;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
47 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
48 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
49 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionsType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
61 public class ActionPolicy extends Policy {
66 private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicy.class);
68 public static final String JSON_CONFIG = "JSON";
70 public static final String PDP_ACTION = "PDP";
71 public static final String PEP_ACTION = "PEP";
72 public static final String TYPE_ACTION = "REST";
74 public static final String GET_METHOD = "GET";
75 public static final String PUT_METHOD = "PUT";
76 public static final String POST_METHOD = "POST";
78 public static final String PERFORMER_ATTRIBUTEID = "performer";
79 public static final String TYPE_ATTRIBUTEID = "type";
80 public static final String METHOD_ATTRIBUTEID = "method";
81 public static final String HEADERS_ATTRIBUTEID = "headers";
82 public static final String URL_ATTRIBUTEID = "url";
83 public static final String BODY_ATTRIBUTEID = "body";
85 List<String> dynamicLabelRuleAlgorithms = new LinkedList<>();
86 List<String> dynamicFieldFunctionRuleAlgorithms = new LinkedList<>();
87 List<String> dynamicFieldOneRuleAlgorithms = new LinkedList<>();
88 List<String> dynamicFieldTwoRuleAlgorithms = new LinkedList<>();
92 private CommonClassDao commonClassDao;
94 private static boolean isAttribute = false;
95 private synchronized static boolean getAttribute () {
100 public ActionPolicy() {
104 public ActionPolicy(PolicyRestAdapter policyAdapter, CommonClassDao commonClassDao){
105 this.policyAdapter = policyAdapter;
106 this.commonClassDao = commonClassDao;
110 public Map<String, String> savePolicies() throws PAPException {
112 Map<String, String> successMap = new HashMap<>();
113 if(isPolicyExists()){
114 successMap.put("EXISTS", "This Policy already exist on the PAP");
118 if(!ActionPolicy.getAttribute()) {
119 successMap.put("invalidAttribute", "Action Attrbute was not in the database.");
123 if(!isPreparedToSave()){
124 //Prep and configure the policy for saving
128 // Until here we prepared the data and here calling the method to create xml.
129 Path newPolicyPath = null;
130 newPolicyPath = Paths.get(policyAdapter.getNewFileName());
131 successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject() );
135 //This is the method for preparing the policy for saving. We have broken it out
136 //separately because the fully configured policy is used for multiple things
138 public boolean prepareToSave() throws PAPException{
140 if(isPreparedToSave()){
141 //we have already done this
146 String policyID = policyAdapter.getPolicyID();
147 version = policyAdapter.getHighestVersion();
149 // Create the Instance for pojo, PolicyType object is used in marshalling.
150 if (policyAdapter.getPolicyType().equals("Action")) {
151 PolicyType policyConfig = new PolicyType();
153 policyConfig.setVersion(Integer.toString(version));
154 policyConfig.setPolicyId(policyID);
155 policyConfig.setTarget(new TargetType());
156 policyAdapter.setData(policyConfig);
159 policyName = policyAdapter.getNewFileName();
161 if (policyAdapter.getData() != null) {
162 // Action body is optional so checking value provided or not
163 String comboDictValue = policyAdapter.getActionAttribute();
164 String actionBody = policyAdapter.getActionBody();
167 //if actionBody is null or empty then we know the ActionAttribute in the request does not exist in the dictionary
168 if(!(actionBody==null || "".equals(actionBody))){
169 saveActionBody(policyName, actionBody);
173 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Could not find " + comboDictValue + " in the ActionPolicyDict table.");
178 PolicyType actionPolicy = (PolicyType) policyAdapter.getData();
179 actionPolicy.setDescription(policyAdapter.getPolicyDescription());
180 actionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
182 AllOfType allOf = new AllOfType();
184 Map<String, String> dynamicFieldComponentAttributes = policyAdapter.getDynamicFieldConfigAttributes();
186 // If there is any dynamic field attributes create the matches here
187 for (String keyField : dynamicFieldComponentAttributes.keySet()) {
188 String key = keyField;
189 String value = dynamicFieldComponentAttributes.get(key);
190 MatchType dynamicMatch = createDynamicMatch(key, value);
191 allOf.getMatch().add(dynamicMatch);
194 AnyOfType anyOf = new AnyOfType();
195 anyOf.getAllOf().add(allOf);
197 TargetType target = new TargetType();
198 target.getAnyOf().add(anyOf);
200 // Adding the target to the policy element
201 actionPolicy.setTarget(target);
203 RuleType rule = new RuleType();
204 rule.setRuleId(policyAdapter.getRuleID());
206 rule.setEffect(EffectType.PERMIT);
207 rule.setTarget(new TargetType());
209 dynamicLabelRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmLabels();
210 dynamicFieldFunctionRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmCombo();
211 dynamicFieldOneRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField1();
212 dynamicFieldTwoRuleAlgorithms = policyAdapter.getDynamicRuleAlgorithmField2();
214 // Rule attributes are optional and dynamic so check and add them to condition.
215 if (dynamicLabelRuleAlgorithms != null && !dynamicLabelRuleAlgorithms.isEmpty()) {
216 boolean isCompound = false;
217 ConditionType condition = new ConditionType();
218 int index = dynamicFieldOneRuleAlgorithms.size() - 1;
220 for (String labelAttr : dynamicLabelRuleAlgorithms) {
221 // if the rule algorithm as a label means it is a compound
222 if (dynamicFieldOneRuleAlgorithms.get(index).equals(labelAttr)) {
223 ApplyType actionApply = new ApplyType();
225 String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index).toString();
226 String value1 = dynamicFieldOneRuleAlgorithms.get(index);
227 String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
228 actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
229 actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
230 actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
231 condition.setExpression(new ObjectFactory().createApply(actionApply));
235 // if rule algorithm not a compound
237 condition.setExpression(new ObjectFactory().createApply(getInnerActionApply(dynamicLabelRuleAlgorithms.get(index).toString())));
239 rule.setCondition(condition);
241 // set the obligations to rule
242 rule.setObligationExpressions(getObligationExpressions());
243 actionPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
244 policyAdapter.setPolicyData(actionPolicy);
246 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
249 setPreparedToSave(true);
253 private static synchronized void setAttribute(boolean b) {
257 // Saving the json Configurations file if exists at server location for action policy.
258 private void saveActionBody(String policyName, String actionBodyData) {
259 if(policyName.endsWith(".xml")){
260 policyName = policyName.replace(".xml", "");
262 File file = new File(ACTION_HOME+ File.separator + policyName + ".json");
263 try(BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()))) {
264 bw.write(actionBodyData);
265 if (LOGGER.isInfoEnabled()) {
266 LOGGER.info("Action Body is succesfully saved at " + file.getAbsolutePath());
268 } catch (IOException e) {
269 LOGGER.error("Exception Occured"+e);
273 // Data required for obligation part is setting here.
274 private ObligationExpressionsType getObligationExpressions() {
275 ObligationExpressionsType obligations = new ObligationExpressionsType();
277 ObligationExpressionType obligation = new ObligationExpressionType();
278 String comboDictValue = policyAdapter.getActionAttribute();
279 obligation.setObligationId(comboDictValue);
280 obligation.setFulfillOn(EffectType.PERMIT);
282 // Add Action Assignment:
283 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
284 assignment1.setAttributeId(PERFORMER_ATTRIBUTEID);
285 assignment1.setCategory(CATEGORY_RECIPIENT_SUBJECT);
287 AttributeValueType actionNameAttributeValue = new AttributeValueType();
288 actionNameAttributeValue.setDataType(STRING_DATATYPE);
289 actionNameAttributeValue.getContent().add(performer.get(policyAdapter.getActionPerformer()));
291 assignment1.setExpression(new ObjectFactory().createAttributeValue(actionNameAttributeValue));
292 obligation.getAttributeAssignmentExpression().add(assignment1);
294 // Add Type Assignment:
295 AttributeAssignmentExpressionType assignmentType = new AttributeAssignmentExpressionType();
296 assignmentType.setAttributeId(TYPE_ATTRIBUTEID);
297 assignmentType.setCategory(CATEGORY_RESOURCE);
299 AttributeValueType typeAttributeValue = new AttributeValueType();
300 typeAttributeValue.setDataType(STRING_DATATYPE);
301 String actionDictType = policyAdapter.getActionDictType();
302 typeAttributeValue.getContent().add(actionDictType);
304 assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
305 obligation.getAttributeAssignmentExpression().add(assignmentType);
307 // Add Rest_URL Assignment:
308 AttributeAssignmentExpressionType assignmentURL = new AttributeAssignmentExpressionType();
309 assignmentURL.setAttributeId(URL_ATTRIBUTEID);
310 assignmentURL.setCategory(CATEGORY_RESOURCE);
312 AttributeValueType actionURLAttributeValue = new AttributeValueType();
313 actionURLAttributeValue.setDataType(URI_DATATYPE);
314 String actionDictUrl = policyAdapter.getActionDictUrl();
315 actionURLAttributeValue.getContent().add(actionDictUrl);
317 assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
318 obligation.getAttributeAssignmentExpression().add(assignmentURL);
320 // Add Method Assignment:
321 AttributeAssignmentExpressionType assignmentMethod = new AttributeAssignmentExpressionType();
322 assignmentMethod.setAttributeId(METHOD_ATTRIBUTEID);
323 assignmentMethod.setCategory(CATEGORY_RESOURCE);
325 AttributeValueType methodAttributeValue = new AttributeValueType();
326 methodAttributeValue.setDataType(STRING_DATATYPE);
327 String actionDictMethod = policyAdapter.getActionDictMethod();
328 methodAttributeValue.getContent().add(actionDictMethod);
330 assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
331 obligation.getAttributeAssignmentExpression().add(assignmentMethod);
333 // Add JSON_URL Assignment:
334 String actionBody = policyAdapter.getActionBody();
335 if (actionBody != null) {
336 AttributeAssignmentExpressionType assignmentJsonURL = new AttributeAssignmentExpressionType();
337 assignmentJsonURL.setAttributeId(BODY_ATTRIBUTEID);
338 assignmentJsonURL.setCategory(CATEGORY_RESOURCE);
340 AttributeValueType jsonURLAttributeValue = new AttributeValueType();
341 jsonURLAttributeValue.setDataType(URI_DATATYPE);
342 jsonURLAttributeValue.getContent().add(CONFIG_URL + "/Action/" + policyName + ".json");
344 assignmentJsonURL.setExpression(new ObjectFactory().createAttributeValue(jsonURLAttributeValue));
345 obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
348 String headerVal = policyAdapter.getActionDictHeader();
349 if(headerVal != null && !headerVal.trim().isEmpty()){
350 // parse it on : to get number of headers
351 String[] result = headerVal.split(":");
352 for (String eachString : result){
353 // parse each value on =
354 String[] textFieldVals = eachString.split("=");
355 obligation.getAttributeAssignmentExpression().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).toString();
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
407 attributeDesignator1.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
408 attributeDesignator2.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
410 innerActionApply1.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
411 innerActionApply2.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
413 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply1));
414 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply2));
416 } else {// if either of one text field is rule attribute.
417 ApplyType innerActionApply = new ApplyType();
418 AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
419 AttributeValueType actionConditionAttributeValue = new AttributeValueType();
421 if (functionKey.toLowerCase().contains("integer")) {
422 innerActionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
423 actionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
424 attributeDesignator.setDataType(INTEGER_DATATYPE);
426 innerActionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
427 actionConditionAttributeValue.setDataType(STRING_DATATYPE);
428 attributeDesignator.setDataType(STRING_DATATYPE);
431 String attributeId = null;
432 String attributeValue = null;
434 // Find which textField has rule attribute and set it as
435 attributeId = value1;
436 attributeValue = value2;
438 if (attributeId != null) {
439 attributeDesignator.setCategory(CATEGORY_RESOURCE);
440 attributeDesignator.setAttributeId(attributeId);
442 actionConditionAttributeValue.getContent().add(attributeValue);
443 innerActionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
444 // Decide the order of element based the values.
445 if (attributeId.equals(value1)) {
446 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
447 actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
449 actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
450 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
459 // if the rule algorithm is multiple compound one setting the apply
460 protected ApplyType getCompoundApply(int index) {
461 ApplyType actionApply = new ApplyType();
462 String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index);
463 String value1 = dynamicFieldOneRuleAlgorithms.get(index);
464 String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
465 actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
466 actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
467 actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
471 // Adding the dynamic headers if any
472 private AttributeAssignmentExpressionType addDynamicHeaders(String header, String value) {
473 AttributeAssignmentExpressionType assignmentHeaders = new AttributeAssignmentExpressionType();
474 assignmentHeaders.setAttributeId("headers:" + header);
475 assignmentHeaders.setCategory(CATEGORY_RESOURCE);
477 AttributeValueType headersAttributeValue = new AttributeValueType();
478 headersAttributeValue.setDataType(STRING_DATATYPE);
479 headersAttributeValue.getContent().add(value);
481 assignmentHeaders.setExpression(new ObjectFactory().createAttributeValue(headersAttributeValue));
482 return assignmentHeaders;
486 public Object getCorrectPolicyDataObject() {
487 return policyAdapter.getPolicyData();
490 public String getFunctionDefinitionId(String key){
491 FunctionDefinition object = (FunctionDefinition) commonClassDao.getDataById(FunctionDefinition.class, "short_name", key);
493 return object.getXacmlid();