a3d18b3e6a82dfddaf0f1835f2ba22c39cc14053
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / PushPolicyService.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.UUID;
23
24 import org.onap.policy.api.PolicyException;
25 import org.onap.policy.api.PushPolicyParameters;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.xacml.api.XACMLErrorConstants;
29 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
30 import org.springframework.http.HttpStatus;
31
32 import com.att.research.xacml.api.pap.PAPException;
33
34 public class PushPolicyService {
35     private static final Logger LOGGER = FlexLogger.getLogger(PushPolicyService.class.getName());
36     
37     private String pushResult = null;
38     private HttpStatus status = HttpStatus.BAD_REQUEST;
39     private PushPolicyParameters pushPolicyParameters = null;
40     private String message = null;
41     private String policyName = null;
42     private String policyScope = null;
43     private String pdpGroup = null;
44     private String policyType = null;
45     private String filePrefix = null;
46     private String clientScope = null; 
47
48     public PushPolicyService(PushPolicyParameters pushPolicyParameters,
49             String requestID) {
50         this.pushPolicyParameters = pushPolicyParameters;
51         if(pushPolicyParameters.getRequestID()==null){
52             UUID requestUUID = null;
53             if (requestID != null && !requestID.isEmpty()) {
54                 try {
55                     requestUUID = UUID.fromString(requestID);
56                 } catch (IllegalArgumentException e) {
57                     requestUUID = UUID.randomUUID();
58                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
59                 }
60             }else{
61                 requestUUID = UUID.randomUUID();
62                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
63             }
64             this.pushPolicyParameters.setRequestID(requestUUID);
65         }
66         try{
67             run();
68             specialCheck();
69         }catch(PolicyException e){
70             pushResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
71             status = HttpStatus.BAD_REQUEST;
72         }
73     }
74
75     private void specialCheck() {
76         if(pushResult.contains("BAD REQUEST") || pushResult.contains("PE300")){
77             status = HttpStatus.BAD_REQUEST;
78         }
79     }
80
81     private void run() throws PolicyException{
82         // Check Validation. 
83         if(!getValidation()){
84             LOGGER.error(message);
85             throw new PolicyException(message);
86         }
87         // Process Results. 
88         try{
89             status = HttpStatus.OK;
90             pushResult = processResult();
91         }catch(Exception e){
92             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
93             status = HttpStatus.BAD_REQUEST;
94             throw new PolicyException(e);
95         }
96     }
97
98     private String processResult() throws PolicyException{
99         PAPServices papServices = new PAPServices();
100         String response = null;
101         StdPDPPolicy selectedPolicy = papServices.pushPolicy(policyScope, filePrefix, policyName, clientScope, pdpGroup, pushPolicyParameters.getRequestID());
102         if(selectedPolicy==null){
103                  response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404.  "
104                      + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
105              LOGGER.error(response);
106              return response;
107         }
108         try {
109             LOGGER.debug("StdPDPPolicy object contains: " + selectedPolicy.getId() + ", " + selectedPolicy.getName() + ", " + selectedPolicy.getLocation().toString());
110             response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=api", "operation=POST"}, pushPolicyParameters.getRequestID(), clientScope);
111         } catch (PAPException e) {
112             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
113             throw new PolicyException(e);
114         }
115         LOGGER.debug("Final API response: " + response);
116         return response;
117     }
118
119     private boolean getValidation() {
120         // While Validating, extract the required values.
121         if (pushPolicyParameters.getPolicyName() != null
122                 && pushPolicyParameters.getPolicyName().contains(".")) {
123             policyName = pushPolicyParameters.getPolicyName().substring(pushPolicyParameters.getPolicyName().lastIndexOf('.') + 1,
124                     pushPolicyParameters.getPolicyName().length());
125             policyScope = pushPolicyParameters.getPolicyName().substring(0,pushPolicyParameters.getPolicyName().lastIndexOf('.'));
126             LOGGER.info("Name is " + policyName + "   scope is " + policyScope);
127         } else {
128             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
129             return false;
130         }
131         if (policyName==null||policyName.trim().isEmpty()){
132             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
133             return false;
134         }
135         policyType = pushPolicyParameters.getPolicyType();
136         if(policyType== null || policyType.trim().isEmpty()){
137             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
138             return false;
139         }
140
141         setClientScope();
142         if(clientScope==null){
143             return false;
144         }
145         
146         pdpGroup = pushPolicyParameters.getPdpGroup();
147         if(pdpGroup==null || pdpGroup.trim().isEmpty()){
148                 pdpGroup = "default";
149         }
150         
151         LOGGER.debug("clientScope is " + clientScope);
152         LOGGER.debug("filePrefix is " + filePrefix);
153
154         return true;
155     }
156
157     private void setClientScope() {
158         if ("Firewall".equalsIgnoreCase(policyType)) {
159             clientScope = "ConfigFirewall";
160             filePrefix = "Config_FW_";
161         } else if ("Action".equalsIgnoreCase(policyType)) {
162             clientScope = "Action";
163             filePrefix = "Action_";
164         } else if ("Decision".equalsIgnoreCase(policyType)) {
165             clientScope = "Decision";
166             filePrefix = "Decision_";
167         } else if ("Base".equalsIgnoreCase(policyType)) {
168             clientScope = "Config";
169             filePrefix = "Config_";
170         } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
171             clientScope = "ConfigClosedLoop";
172             filePrefix = "Config_Fault_";
173         } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
174             clientScope = "ConfigClosedLoop";
175             filePrefix = "Config_PM_";
176         } else if ("MicroService".equalsIgnoreCase(policyType)) {
177             clientScope = "ConfigMS";
178             filePrefix = "Config_MS_";
179         } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
180             clientScope = "ConfigBrmsRaw";
181             filePrefix = "Config_BRMS_Raw_";
182         } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
183             clientScope = "ConfigBrmsParam";
184             filePrefix = "Config_BRMS_Param_";
185         } else {
186             clientScope = null;
187             message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
188                     + " is not a valid Policy Type.";
189         }
190     }
191
192     public String getResult() {
193         return pushResult;
194     }
195
196     public HttpStatus getResponseCode() {
197         return status;
198     }
199
200 }