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