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