Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PDP-REST / src / main / java / org / openecomp / policy / pdp / rest / api / services / PushPolicyService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-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.openecomp.policy.pdp.rest.api.services;
21
22 import java.util.UUID;
23
24 import org.openecomp.policy.api.PolicyException;
25 import org.openecomp.policy.api.PushPolicyParameters;
26 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
27 import org.openecomp.policy.common.logging.flexlogger.Logger;
28 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
29 import org.openecomp.policy.xacml.std.pap.StdPAPPolicy;
30 import org.openecomp.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 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());
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         /* //String activeVersion = papServices.getActiveVersion(policyScope, filePrefix, policyName, clientScope, pushPolicyParameters.getRequestID());
111         LOGGER.debug("The active version of " + policyScope + File.separator + filePrefix + policyName + " is " + activeVersion);
112         String id = null;
113         if ("pe100".equalsIgnoreCase(activeVersion)) {
114             response = XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is 403. PEP is not Authorized for making this Request!! "
115                     + "Contact Administrator for this Scope. "; 
116             LOGGER.error(response);
117             return response;
118         } else if ("pe300".equalsIgnoreCase(activeVersion)) {
119             response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404.  "
120                     + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
121             LOGGER.error(response);
122             return response;
123         }
124         if (!activeVersion.equalsIgnoreCase("0")) {
125             id = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
126             LOGGER.debug("The policyId is " + id);
127         } else {
128             response = XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the activeVersion for this policy.  "
129                     + "This indicates the policy does not exist, please verify the policy exists."; 
130             LOGGER.error(response);
131             return response;
132         }
133         StdPDPPolicy selectedPolicy = papServices.getGitPath(policyScope, filePrefix, policyName, activeVersion, clientScope, pushPolicyParameters.getRequestID(), id);
134         */
135         try {
136             LOGGER.debug("StdPDPPolicy object contains: " + selectedPolicy.getId() + ", " + selectedPolicy.getName() + ", " + selectedPolicy.getLocation().toString());
137             response = copyPolicy(selectedPolicy, pdpGroup, clientScope, pushPolicyParameters.getRequestID());
138         } catch (PAPException e) {
139             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage());
140             throw new PolicyException(e);
141         }
142         LOGGER.debug("copyPolicy response:  " + response);
143         if(response.contains("successfully")){
144             response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=addPolicyToGroup", "operation=PUT"}, pushPolicyParameters.getRequestID(), clientScope);
145         }
146         LOGGER.debug("Final API response: " + response);
147         return response;
148     }
149
150     private String copyPolicy(PDPPolicy policy, String group, String policyType, UUID requestID) throws PAPException {
151         String response = null;
152         if (policy == null || group == null) {
153             throw new PAPException("Null input policy="+policy+"  group="+group);
154         }
155         try {
156             StdPAPPolicy location = new StdPAPPolicy(policy.getLocation());
157             response = copyFile(policy.getId(), group, location, policyType, requestID);
158         } catch (Exception e) {
159             String message = "Unable to PUT policy '" + policy.getId() + "', e:" + e;
160             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
161             throw new PAPException(message);
162         }
163         return response;
164     }
165     
166     private String copyFile(String policyId, String group, StdPAPPolicy location, String clientScope, UUID requestID) throws PAPException {
167         String response = null;
168         // send the policy file to the PAP Servlet
169         PAPServices papService = new PAPServices();
170         try {
171             response = (String) papService.callPAP(location, new String[] {"groupId=" + group, "policyId="+policyId, "apiflag=api", "operation=post"}, requestID, clientScope);
172         } catch (Exception e) {
173             String message = "Unable to PUT policy '" + policyId + "', e:" + e;
174             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e);
175             throw new PAPException(message);
176         }
177         return response;
178     }
179
180     private boolean getValidation() {
181         // While Validating, extract the required values.
182         if (pushPolicyParameters.getPolicyName() != null
183                 && pushPolicyParameters.getPolicyName().contains(".")) {
184             policyName = pushPolicyParameters.getPolicyName().substring(pushPolicyParameters.getPolicyName().lastIndexOf(".") + 1,
185                     pushPolicyParameters.getPolicyName().length());
186             policyScope = pushPolicyParameters.getPolicyName().substring(0,pushPolicyParameters.getPolicyName().lastIndexOf("."));
187             LOGGER.info("Name is " + policyName + "   scope is " + policyScope);
188         } else {
189             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
190             return false;
191         }
192         if (policyName==null||policyName.trim().isEmpty()){
193             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
194             return false;
195         }
196         policyType = pushPolicyParameters.getPolicyType();
197         if(policyType== null || policyType.trim().isEmpty()){
198             message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
199             return false;
200         }
201
202         setClientScope();
203         if(clientScope==null){
204             return false;
205         }
206         
207         pdpGroup = pushPolicyParameters.getPdpGroup();
208         if(pdpGroup==null || pdpGroup.trim().isEmpty()){
209                 pdpGroup = "default";
210         }
211         
212         LOGGER.debug("clientScope is " + clientScope);
213         LOGGER.debug("filePrefix is " + filePrefix);
214
215         return true;
216     }
217
218     private void setClientScope() {
219         if ("Firewall".equalsIgnoreCase(policyType)) {
220             clientScope = "ConfigFirewall";
221             filePrefix = "Config_FW_";
222         } else if ("Action".equalsIgnoreCase(policyType)) {
223             clientScope = "Action";
224             filePrefix = "Action_";
225         } else if ("Decision".equalsIgnoreCase(policyType)) {
226             clientScope = "Decision";
227             filePrefix = "Decision_";
228         } else if ("Base".equalsIgnoreCase(policyType)) {
229             clientScope = "Config";
230             filePrefix = "Config_";
231         } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
232             clientScope = "ConfigClosedLoop";
233             filePrefix = "Config_Fault_";
234         } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
235             clientScope = "ConfigClosedLoop";
236             filePrefix = "Config_PM_";
237         } else if ("MicroService".equalsIgnoreCase(policyType)) {
238             clientScope = "ConfigMS";
239             filePrefix = "Config_MS_";
240         } else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
241             clientScope = "ConfigBrmsRaw";
242             filePrefix = "Config_BRMS_Raw_";
243         } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
244             clientScope = "ConfigBrmsParam";
245             filePrefix = "Config_BRMS_Param_";
246         } else {
247             clientScope = null;
248             message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
249                     + " is not a valid Policy Type.";
250         }
251     }
252
253     public String getResult() {
254         return pushResult;
255     }
256
257     public HttpStatus getResponseCode() {
258         return status;
259     }
260
261 }