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) {
260 if(policyName.endsWith(".xml")){
261 policyName = policyName.replace(".xml", "");
263 File file = new File(ACTION_HOME+ File.separator + policyName + ".json");
264 FileWriter fw = new FileWriter(file.getAbsoluteFile());
265 BufferedWriter bw = new BufferedWriter(fw);
266 bw.write(actionBodyData);
268 if (LOGGER.isInfoEnabled()) {
269 LOGGER.info("Action Body is succesfully saved at " + file.getAbsolutePath());
271 } catch (IOException e) {
272 LOGGER.error("Exception Occured"+e);
276 // Data required for obligation part is setting here.
277 private ObligationExpressionsType getObligationExpressions() {
278 ObligationExpressionsType obligations = new ObligationExpressionsType();
280 ObligationExpressionType obligation = new ObligationExpressionType();
281 String comboDictValue = policyAdapter.getActionAttribute();
282 obligation.setObligationId(comboDictValue);
283 obligation.setFulfillOn(EffectType.PERMIT);
285 // Add Action Assignment:
286 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
287 assignment1.setAttributeId(PERFORMER_ATTRIBUTEID);
288 assignment1.setCategory(CATEGORY_RECIPIENT_SUBJECT);
290 AttributeValueType actionNameAttributeValue = new AttributeValueType();
291 actionNameAttributeValue.setDataType(STRING_DATATYPE);
292 actionNameAttributeValue.getContent().add(performer.get(policyAdapter.getActionPerformer()));
294 assignment1.setExpression(new ObjectFactory().createAttributeValue(actionNameAttributeValue));
295 obligation.getAttributeAssignmentExpression().add(assignment1);
297 // Add Type Assignment:
298 AttributeAssignmentExpressionType assignmentType = new AttributeAssignmentExpressionType();
299 assignmentType.setAttributeId(TYPE_ATTRIBUTEID);
300 assignmentType.setCategory(CATEGORY_RESOURCE);
302 AttributeValueType typeAttributeValue = new AttributeValueType();
303 typeAttributeValue.setDataType(STRING_DATATYPE);
304 String actionDictType = policyAdapter.getActionDictType();
305 typeAttributeValue.getContent().add(actionDictType);
307 assignmentType.setExpression(new ObjectFactory().createAttributeValue(typeAttributeValue));
308 obligation.getAttributeAssignmentExpression().add(assignmentType);
310 // Add Rest_URL Assignment:
311 AttributeAssignmentExpressionType assignmentURL = new AttributeAssignmentExpressionType();
312 assignmentURL.setAttributeId(URL_ATTRIBUTEID);
313 assignmentURL.setCategory(CATEGORY_RESOURCE);
315 AttributeValueType actionURLAttributeValue = new AttributeValueType();
316 actionURLAttributeValue.setDataType(URI_DATATYPE);
317 String actionDictUrl = policyAdapter.getActionDictUrl();
318 actionURLAttributeValue.getContent().add(actionDictUrl);
320 assignmentURL.setExpression(new ObjectFactory().createAttributeValue(actionURLAttributeValue));
321 obligation.getAttributeAssignmentExpression().add(assignmentURL);
323 // Add Method Assignment:
324 AttributeAssignmentExpressionType assignmentMethod = new AttributeAssignmentExpressionType();
325 assignmentMethod.setAttributeId(METHOD_ATTRIBUTEID);
326 assignmentMethod.setCategory(CATEGORY_RESOURCE);
328 AttributeValueType methodAttributeValue = new AttributeValueType();
329 methodAttributeValue.setDataType(STRING_DATATYPE);
330 String actionDictMethod = policyAdapter.getActionDictMethod();
331 methodAttributeValue.getContent().add(actionDictMethod);
333 assignmentMethod.setExpression(new ObjectFactory().createAttributeValue(methodAttributeValue));
334 obligation.getAttributeAssignmentExpression().add(assignmentMethod);
336 // Add JSON_URL Assignment:
337 String actionBody = policyAdapter.getActionBody();
338 if (actionBody != null) {
339 AttributeAssignmentExpressionType assignmentJsonURL = new AttributeAssignmentExpressionType();
340 assignmentJsonURL.setAttributeId(BODY_ATTRIBUTEID);
341 assignmentJsonURL.setCategory(CATEGORY_RESOURCE);
343 AttributeValueType jsonURLAttributeValue = new AttributeValueType();
344 jsonURLAttributeValue.setDataType(URI_DATATYPE);
345 jsonURLAttributeValue.getContent().add(CONFIG_URL + "/Action/" + policyName + ".json");
347 assignmentJsonURL.setExpression(new ObjectFactory().createAttributeValue(jsonURLAttributeValue));
348 obligation.getAttributeAssignmentExpression().add(assignmentJsonURL);
351 String headerVal = policyAdapter.getActionDictHeader();
352 if(headerVal != null && !headerVal.trim().isEmpty()){
353 // parse it on : to get number of headers
354 String[] result = headerVal.split(":");
355 for (String eachString : result){
356 // parse each value on =
357 String[] textFieldVals = eachString.split("=");
358 obligation.getAttributeAssignmentExpression().add(addDynamicHeaders(textFieldVals[0], textFieldVals[1]));
362 obligations.getObligationExpression().add(obligation);
367 // if compound setting the inner apply here
368 protected ApplyType getInnerActionApply(String value1Label) {
369 ApplyType actionApply = new ApplyType();
371 // check the index for the label.
372 for (String labelAttr : dynamicLabelRuleAlgorithms) {
373 if (labelAttr.equals(value1Label)) {
374 String value1 = dynamicFieldOneRuleAlgorithms.get(index).toString();
375 // check if the row contains label again
376 for (String labelValue : dynamicLabelRuleAlgorithms) {
377 if (labelValue.equals(value1)) {
378 return getCompoundApply(index);
382 // Getting the values from the form.
383 String functionKey = dynamicFieldFunctionRuleAlgorithms.get(index);
384 String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
385 actionApply.setFunctionId(getFunctionDefinitionId(functionKey));
386 // if two text field are rule attributes.
387 if ((value1.contains(RULE_VARIABLE)) && (value2.contains(RULE_VARIABLE))) {
388 ApplyType innerActionApply1 = new ApplyType();
389 ApplyType innerActionApply2 = new ApplyType();
390 AttributeDesignatorType attributeDesignator1 = new AttributeDesignatorType();
391 AttributeDesignatorType attributeDesignator2 = new AttributeDesignatorType();
392 // If selected function is Integer function set integer functionID
393 if (functionKey.toLowerCase().contains("integer")) {
394 innerActionApply1.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
395 innerActionApply2.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
396 attributeDesignator1.setDataType(INTEGER_DATATYPE);
397 attributeDesignator2.setDataType(INTEGER_DATATYPE);
399 // If selected function is not a Integer function
400 // set String functionID
401 innerActionApply1.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
402 innerActionApply2.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
403 attributeDesignator1.setDataType(STRING_DATATYPE);
404 attributeDesignator2.setDataType(STRING_DATATYPE);
406 attributeDesignator1.setCategory(CATEGORY_RESOURCE);
407 attributeDesignator2.setCategory(CATEGORY_RESOURCE);
409 // Here set actual field values
410 attributeDesignator1.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
411 attributeDesignator2.setAttributeId(value1.contains("resource:") ? value1.substring(9): value1.substring(8));
413 innerActionApply1.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator1));
414 innerActionApply2.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator2));
416 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply1));
417 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply2));
419 } else {// if either of one text field is rule attribute.
420 ApplyType innerActionApply = new ApplyType();
421 AttributeDesignatorType attributeDesignator = new AttributeDesignatorType();
422 AttributeValueType actionConditionAttributeValue = new AttributeValueType();
424 if (functionKey.toLowerCase().contains("integer")) {
425 innerActionApply.setFunctionId(FUNTION_INTEGER_ONE_AND_ONLY);
426 actionConditionAttributeValue.setDataType(INTEGER_DATATYPE);
427 attributeDesignator.setDataType(INTEGER_DATATYPE);
429 innerActionApply.setFunctionId(FUNCTION_STRING_ONE_AND_ONLY);
430 actionConditionAttributeValue.setDataType(STRING_DATATYPE);
431 attributeDesignator.setDataType(STRING_DATATYPE);
434 String attributeId = null;
435 String attributeValue = null;
437 // Find which textField has rule attribute and set it as
438 attributeId = value1;
439 attributeValue = value2;
441 if (attributeId != null) {
442 attributeDesignator.setCategory(CATEGORY_RESOURCE);
443 attributeDesignator.setAttributeId(attributeId);
445 actionConditionAttributeValue.getContent().add(attributeValue);
446 innerActionApply.getExpression().add(new ObjectFactory().createAttributeDesignator(attributeDesignator));
447 // Decide the order of element based the values.
448 if (attributeId.equals(value1)) {
449 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
450 actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
452 actionApply.getExpression().add(new ObjectFactory().createAttributeValue(actionConditionAttributeValue));
453 actionApply.getExpression().add(new ObjectFactory().createApply(innerActionApply));
462 // if the rule algorithm is multiple compound one setting the apply
463 protected ApplyType getCompoundApply(int index) {
464 ApplyType actionApply = new ApplyType();
465 String selectedFunction = dynamicFieldFunctionRuleAlgorithms.get(index);
466 String value1 = dynamicFieldOneRuleAlgorithms.get(index);
467 String value2 = dynamicFieldTwoRuleAlgorithms.get(index);
468 actionApply.setFunctionId(getFunctionDefinitionId(selectedFunction));
469 actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value1)));
470 actionApply.getExpression().add(new ObjectFactory().createApply(getInnerActionApply(value2)));
474 // Adding the dynamic headers if any
475 private AttributeAssignmentExpressionType addDynamicHeaders(String header, String value) {
476 AttributeAssignmentExpressionType assignmentHeaders = new AttributeAssignmentExpressionType();
477 assignmentHeaders.setAttributeId("headers:" + header);
478 assignmentHeaders.setCategory(CATEGORY_RESOURCE);
480 AttributeValueType headersAttributeValue = new AttributeValueType();
481 headersAttributeValue.setDataType(STRING_DATATYPE);
482 headersAttributeValue.getContent().add(value);
484 assignmentHeaders.setExpression(new ObjectFactory().createAttributeValue(headersAttributeValue));
485 return assignmentHeaders;
489 public Object getCorrectPolicyDataObject() {
490 return policyAdapter.getPolicyData();
493 public String getFunctionDefinitionId(String key){
494 FunctionDefinition object = (FunctionDefinition) commonClassDao.getDataById(FunctionDefinition.class, "short_name", key);
496 return object.getXacmlid();