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