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 / DecisionPolicyService.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.api.RuleProvider;
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  * Decision Policy Implementation
36  *
37  * @version 0.1
38  */
39 public class DecisionPolicyService {
40     private static Logger LOGGER = FlexLogger.getLogger(DecisionPolicyService.class.getName());
41     private static PAPServices papServices = null;
42
43     private String message = null;
44     private String policyScope = null;
45     private String policyName = null;
46     private PolicyParameters policyParameters = null;
47     private String onapName = null;
48
49     public DecisionPolicyService(String policyScope, String policyName,
50                                  PolicyParameters policyParameters) {
51         this.policyScope = policyScope;
52         this.policyName = policyName;
53         this.policyParameters = policyParameters;
54         papServices = new PAPServices();
55     }
56
57     public Boolean getValidation() {
58         onapName = policyParameters.getOnapName();
59         if (onapName == null || onapName.trim().isEmpty()) {
60             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No ONAP Name given.";
61             return false;
62         }
63         return true;
64     }
65
66     public String getMessage() {
67         return message;
68     }
69
70     public String getResult(boolean updateFlag) throws PolicyException {
71         String response = null;
72         String operation = null;
73         if (updateFlag) {
74             operation = "update";
75         } else {
76             operation = "create";
77         }
78         RuleProvider ruleProvider = policyParameters.getRuleProvider();
79         if (ruleProvider == null) {
80             ruleProvider = RuleProvider.CUSTOM;
81         }
82         Map<String, String> matchingAttributes = null;
83         Map<String, String> settingsAttributes = null;
84
85         //Get the MATCHING and/or SETTINGS attributes
86         if (policyParameters.getAttributes() != null &&
87                 policyParameters.getAttributes().containsKey(AttributeType.MATCHING) &&
88                 policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) {
89             matchingAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
90             settingsAttributes = policyParameters.getAttributes().get(AttributeType.SETTINGS);
91         } else if (policyParameters.getAttributes() != null &&
92                 !policyParameters.getAttributes().containsKey(AttributeType.MATCHING) &&
93                 policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) {
94             settingsAttributes = policyParameters.getAttributes().get(AttributeType.SETTINGS);
95         } else if (policyParameters.getAttributes() != null &&
96                 policyParameters.getAttributes().containsKey(AttributeType.MATCHING) &&
97                 !policyParameters.getAttributes().containsKey(AttributeType.SETTINGS)) {
98             matchingAttributes = policyParameters.getAttributes().get(AttributeType.MATCHING);
99         }
100         // Create StdPAPPolicy object used to send policy data to PAP-REST.
101         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
102                 .policyName(policyName)
103                 .description(policyParameters.getPolicyDescription())
104                 .onapName(onapName)
105                 .providerComboBox(ruleProvider.toString())
106                 .dyanamicFieldConfigAttributes(matchingAttributes)
107                 .dynamicSettingsMap(settingsAttributes)
108                 .treatments(policyParameters.getTreatments())
109                 .dynamicRuleAlgorithmLabels(policyParameters.getDynamicRuleAlgorithmLabels())
110                 .dynamicRuleAlgorithmCombo(policyParameters.getDynamicRuleAlgorithmFunctions())
111                 .dynamicRuleAlgorithmField1(policyParameters.getDynamicRuleAlgorithmField1())
112                 .dynamicRuleAlgorithmField2(policyParameters.getDynamicRuleAlgorithmField2())
113                 .editPolicy(updateFlag)
114                 .domain(policyScope)
115                 .highestVersion(0)
116                 .build());
117         // Send JSON to PAP.
118         response = (String) papServices.callPAP(newPAPPolicy, new String[]{"operation=" + operation, "apiflag=api",
119                 "policyType=Decision"}, policyParameters.getRequestID(), "Decision");
120         LOGGER.info(message);
121         return response;
122     }
123
124 }