Add PAP Policy parameter builder xacml policy
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / ActionPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.policy.pdp.rest.api.services;
21
22 import java.util.Map;
23
24 import org.onap.policy.api.AttributeType;
25 import org.onap.policy.api.PolicyException;
26 import org.onap.policy.api.PolicyParameters;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.xacml.api.XACMLErrorConstants;
30 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
31
32 /**
33  * Action Policy Implementation.
34  *
35  * @version 0.1
36  */
37 public class ActionPolicyService {
38     private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyService.class.getName());
39     private PAPServices papServices = null;
40
41     private PolicyParameters policyParameters = null;
42     private String message = null;
43     private String policyName = null;
44     private String policyScope = null;
45     private Map<String, String> componentAttributes = null;
46     private String actionAttribute = null;
47     private String actionPerformer = null;
48
49     public ActionPolicyService(String policyScope, String policyName,
50                                PolicyParameters policyParameters) {
51         this.policyParameters = policyParameters;
52         this.policyName = policyName;
53         this.policyScope = policyScope;
54         papServices = new PAPServices();
55     }
56
57     public Boolean getValidation() {
58         if (policyParameters.getAttributes() == null || policyParameters.getAttributes().isEmpty()) {
59             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given.";
60             return false;
61         }
62         componentAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
63         if (componentAttributes == null || componentAttributes.isEmpty()) {
64             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given.";
65             return false;
66         }
67         actionAttribute = policyParameters.getActionAttribute();
68         if (actionAttribute == null || actionAttribute.trim().isEmpty()) {
69             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Attribute given.";
70             return false;
71         }
72         actionPerformer = policyParameters.getActionPerformer();
73         if (actionPerformer == null || actionPerformer.trim().isEmpty()) {
74             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Performer given.";
75             return false;
76         }
77         if (!"PEP".equalsIgnoreCase(actionPerformer) && !"PDP".equalsIgnoreCase(actionPerformer)) {
78             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Action Performer given.";
79             return false;
80         }
81         return true;
82     }
83
84     public String getMessage() {
85         return message;
86     }
87
88     public String getResult(boolean updateFlag) throws PolicyException {
89         String response = null;
90         String operation = null;
91         if (updateFlag) {
92             operation = "update";
93         } else {
94             operation = "create";
95         }
96         // Create Policy
97         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(policyName, policyParameters.getPolicyDescription(),
98                 componentAttributes, policyParameters.getDynamicRuleAlgorithmLabels(),
99                 policyParameters.getDynamicRuleAlgorithmFunctions(),
100                 policyParameters.getDynamicRuleAlgorithmField1(), policyParameters.getDynamicRuleAlgorithmField2(),
101                 actionPerformer, actionAttribute, updateFlag, policyScope, 0);
102         // send Json to PAP
103         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
104                 "policyType=Action"}, policyParameters.getRequestID(), "Action");
105         LOGGER.info(response);
106         return response;
107     }
108 }