Modify std pap policy to use builder in constr
[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 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
32
33 /**
34  * Action Policy Implementation.
35  *
36  * @version 0.1
37  */
38 public class ActionPolicyService {
39     private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyService.class.getName());
40     private PAPServices papServices = null;
41
42     private PolicyParameters policyParameters = null;
43     private String message = null;
44     private String policyName = null;
45     private String policyScope = null;
46     private Map<String, String> componentAttributes = null;
47     private String actionAttribute = null;
48     private String actionPerformer = null;
49
50     public ActionPolicyService(String policyScope, String policyName,
51                                PolicyParameters policyParameters) {
52         this.policyParameters = policyParameters;
53         this.policyName = policyName;
54         this.policyScope = policyScope;
55         papServices = new PAPServices();
56     }
57
58     public Boolean getValidation() {
59         if (policyParameters.getAttributes() == null || policyParameters.getAttributes().isEmpty()) {
60             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given.";
61             return false;
62         }
63         componentAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
64         if (componentAttributes == null || componentAttributes.isEmpty()) {
65             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given.";
66             return false;
67         }
68         actionAttribute = policyParameters.getActionAttribute();
69         if (actionAttribute == null || actionAttribute.trim().isEmpty()) {
70             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Attribute given.";
71             return false;
72         }
73         actionPerformer = policyParameters.getActionPerformer();
74         if (actionPerformer == null || actionPerformer.trim().isEmpty()) {
75             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Performer given.";
76             return false;
77         }
78         if (!"PEP".equalsIgnoreCase(actionPerformer) && !"PDP".equalsIgnoreCase(actionPerformer)) {
79             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Action Performer given.";
80             return false;
81         }
82         return true;
83     }
84
85     public String getMessage() {
86         return message;
87     }
88
89     public String getResult(boolean updateFlag) throws PolicyException {
90         String response = null;
91         String operation = null;
92         if (updateFlag) {
93             operation = "update";
94         } else {
95             operation = "create";
96         }
97         // Create Policy
98         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().policyName(policyName)
99                 .description(policyParameters.getPolicyDescription())
100                 .dyanamicFieldConfigAttributes(componentAttributes)
101                 .dynamicRuleAlgorithmLabels(policyParameters.getDynamicRuleAlgorithmLabels())
102                 .dynamicRuleAlgorithmCombo(policyParameters.getDynamicRuleAlgorithmFunctions())
103                 .dynamicRuleAlgorithmField1(policyParameters.getDynamicRuleAlgorithmField1())
104                 .dynamicRuleAlgorithmField2(policyParameters.getDynamicRuleAlgorithmField2())
105                 .actionPerformer(actionPerformer)
106                 .actionAttribute(actionAttribute)
107                 .editPolicy(updateFlag)
108                 .domain(policyScope)
109                 .highestVersion(0)
110                 .build());
111         // send Json to PAP
112         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
113                 "policyType=Action"}, policyParameters.getRequestID(), "Action");
114         LOGGER.info(response);
115         return response;
116     }
117 }