Optimize PAP policy constructor with builder
[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  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.policy.pdp.rest.api.services;
22
23 import java.util.Map;
24
25 import org.onap.policy.api.AttributeType;
26 import org.onap.policy.api.PolicyException;
27 import org.onap.policy.api.PolicyParameters;
28 import org.onap.policy.common.logging.flexlogger.FlexLogger;
29 import org.onap.policy.common.logging.flexlogger.Logger;
30 import org.onap.policy.xacml.api.XACMLErrorConstants;
31 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
32 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
33
34 /**
35  * Action Policy Implementation.
36  *
37  * @version 0.1
38  */
39 public class ActionPolicyService {
40     private static final Logger LOGGER = FlexLogger.getLogger(ActionPolicyService.class.getName());
41     private PAPServices papServices = null;
42
43     private PolicyParameters policyParameters = null;
44     private String message = null;
45     private String policyName = null;
46     private String policyScope = null;
47     private Map<String, String> componentAttributes = null;
48     private String actionAttribute = null;
49     private String actionPerformer = null;
50
51     public ActionPolicyService(String policyScope, String policyName,
52                                PolicyParameters policyParameters) {
53         this.policyParameters = policyParameters;
54         this.policyName = policyName;
55         this.policyScope = policyScope;
56         papServices = new PAPServices();
57     }
58
59     public Boolean getValidation() {
60         if (policyParameters.getAttributes() == null || policyParameters.getAttributes().isEmpty()) {
61             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given.";
62             return false;
63         }
64         componentAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
65         if (componentAttributes == null || componentAttributes.isEmpty()) {
66             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Component Attributes given.";
67             return false;
68         }
69         actionAttribute = policyParameters.getActionAttribute();
70         if (actionAttribute == null || actionAttribute.trim().isEmpty()) {
71             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Attribute given.";
72             return false;
73         }
74         actionPerformer = policyParameters.getActionPerformer();
75         if (actionPerformer == null || actionPerformer.trim().isEmpty()) {
76             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Action Performer given.";
77             return false;
78         }
79         if (!"PEP".equalsIgnoreCase(actionPerformer) && !"PDP".equalsIgnoreCase(actionPerformer)) {
80             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Action Performer given.";
81             return false;
82         }
83         return true;
84     }
85
86     public String getMessage() {
87         return message;
88     }
89
90     public String getResult(boolean updateFlag) throws PolicyException {
91         String response = null;
92         String operation = null;
93         if (updateFlag) {
94             operation = "update";
95         } else {
96             operation = "create";
97         }
98         // Create Policy
99         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().policyName(policyName)
100                 .description(policyParameters.getPolicyDescription())
101                 .dynamicFieldConfigAttributes(componentAttributes)
102                 .dynamicRuleAlgorithmLabels(policyParameters.getDynamicRuleAlgorithmLabels())
103                 .dynamicRuleAlgorithmCombo(policyParameters.getDynamicRuleAlgorithmFunctions())
104                 .dynamicRuleAlgorithmField1(policyParameters.getDynamicRuleAlgorithmField1())
105                 .dynamicRuleAlgorithmField2(policyParameters.getDynamicRuleAlgorithmField2())
106                 .actionPerformer(actionPerformer)
107                 .actionAttribute(actionAttribute)
108                 .editPolicy(updateFlag)
109                 .domain(policyScope)
110                 .highestVersion(0)
111                 .build());
112         // send Json to PAP
113         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
114                 "policyType=Action"}, policyParameters.getRequestID(), "Action");
115         LOGGER.info(response);
116         return response;
117     }
118 }