01670a5fde195e63ccbe8762ac5740445073a69b
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / services / DeletePolicyService.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.io.File;
23 import java.util.UUID;
24
25 import org.onap.policy.api.DeletePolicyParameters;
26 import org.onap.policy.api.PolicyException;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.xacml.api.XACMLErrorConstants;
30 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
31 import org.springframework.http.HttpStatus;
32
33 public class DeletePolicyService {
34     private static final Logger LOGGER = FlexLogger.getLogger(DeletePolicyService.class.getName());
35     
36     private String deleteResult = null;
37     private HttpStatus status = HttpStatus.BAD_REQUEST;
38     private DeletePolicyParameters deletePolicyParameters = null;
39     private String message = null;
40     private String filePrefix = null;
41     private String clientScope = null; 
42     private String policyType = null;
43     private String policyName = null;
44     private String policyScope = null;
45
46     public DeletePolicyService(DeletePolicyParameters deletePolicyParameters,
47             String requestID) {
48         this.deletePolicyParameters = deletePolicyParameters;
49         if(deletePolicyParameters.getRequestID()==null){
50             UUID requestUUID = null;
51             if (requestID != null && !requestID.isEmpty()) {
52                 try {
53                     requestUUID = UUID.fromString(requestID);
54                 } catch (IllegalArgumentException e) {
55                     requestUUID = UUID.randomUUID();
56                     LOGGER.info("Generated Random UUID: " + requestUUID.toString(), e);
57                 }
58             }else{
59                 requestUUID = UUID.randomUUID();
60                 LOGGER.info("Generated Random UUID: " + requestUUID.toString());
61             }
62             this.deletePolicyParameters.setRequestID(requestUUID);
63         }
64         try{
65             run();
66             specialCheck();
67         }catch(PolicyException e){
68             deleteResult = XACMLErrorConstants.ERROR_DATA_ISSUE + e;
69             status = HttpStatus.BAD_REQUEST;
70         }
71     }
72
73     private void specialCheck() {
74         if(deleteResult==null){
75                 return;
76         }
77         if (deleteResult.contains("BAD REQUEST")||deleteResult.contains("PE300")||deleteResult.contains("PE200")||deleteResult.contains("not exist")||deleteResult.contains("Invalid policyName")) {
78             status = HttpStatus.BAD_REQUEST;
79         } else if (deleteResult.contains("locked down")){
80             status = HttpStatus.ACCEPTED;
81         } else if (deleteResult.contains("not Authorized")) {
82             status = HttpStatus.FORBIDDEN;
83         } else if (deleteResult.contains("groupId")) {
84             status = HttpStatus.NOT_FOUND;
85         } else if (deleteResult.contains("JPAUtils")||deleteResult.contains("database")||deleteResult.contains("policy file")||
86                 deleteResult.contains("unknown")||deleteResult.contains("configuration")) {
87             status = HttpStatus.INTERNAL_SERVER_ERROR;
88         } 
89     }
90
91     private void run() throws PolicyException{
92         // Check Validation. 
93         if(!getValidation()){
94             LOGGER.error(message);
95             throw new PolicyException(message);
96         }
97         // Get Result. 
98         try{
99             status = HttpStatus.OK;
100             deleteResult = processResult();
101         }catch (Exception e){
102             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
103             status = HttpStatus.BAD_REQUEST;
104             throw new PolicyException(e);
105         }
106     }
107
108     private String processResult() throws PolicyException{
109         String response = null;
110         String fullPolicyName = null;
111         String pdpGroup = deletePolicyParameters.getPdpGroup();
112         // PDP Group Check. 
113         if (pdpGroup==null){
114             pdpGroup="NA";
115         }
116         PAPServices papServices = new PAPServices();
117         if (!deletePolicyParameters.getPolicyName().contains("xml")) {
118             
119             String activeVersion = papServices.getActiveVersion(policyScope, filePrefix, policyName, clientScope, deletePolicyParameters.getRequestID());
120             LOGGER.debug("The active version of " + policyScope + File.separator + filePrefix + policyName + " is " + activeVersion);
121             String id = null;
122             if ("pe100".equalsIgnoreCase(activeVersion)) {
123                 response = XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is 403. PEP is not Authorized for making this Request!! "
124                         + "Contact Administrator for this Scope. "; 
125                 LOGGER.error(response);
126                 return response;
127             } else if ("pe300".equalsIgnoreCase(activeVersion)) {
128                 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404.  "
129                         + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
130                 LOGGER.error(response);
131                 return response;
132             }
133             if (!activeVersion.equalsIgnoreCase("0")) {
134                 id = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
135                 LOGGER.debug("The policyId is " + id);
136             } else {
137                 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the activeVersion for this policy. could not retrieve the activeVersion for this policy.  "
138                         + "This indicates the policy does not exist, please verify the policy exists."; 
139                 LOGGER.error(response);
140                 return response;
141             }
142             
143             fullPolicyName = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
144             
145         } else {
146             fullPolicyName = policyName;
147         }
148
149         if ("PAP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
150             if (deletePolicyParameters.getDeleteCondition()==null||deletePolicyParameters.getDeleteCondition().toString().trim().isEmpty()){
151                 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Delete Condition given.";
152                 LOGGER.error(message);
153                 return message;
154             }
155             
156             StdPAPPolicy deletePapPolicy = new StdPAPPolicy(fullPolicyName, deletePolicyParameters.getDeleteCondition().toString());
157             //send JSON object to PAP
158             response = (String) papServices.callPAP(deletePapPolicy, new String[] {"groupId="+pdpGroup, "apiflag=deletePapApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope);
159         } else if ("PDP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
160             if (deletePolicyParameters.getPdpGroup()==null||deletePolicyParameters.getPdpGroup().trim().isEmpty()){
161                 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PDP Group given."; 
162                 LOGGER.error(message);
163                 return message;
164             }
165             //send JSON object to PAP
166             response = (String) papServices.callPAP(null, new String[] {"policyName="+fullPolicyName, "groupId="+pdpGroup, "apiflag=deletePdpApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope);
167         } else {
168             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Policy Component does not exist. Please enter either PAP or PDP to delete the policy from a specified Policy Component.";
169             LOGGER.error(message);
170             response = message;
171         }
172         return response;
173     }
174
175  private boolean getValidation() {
176      // While Validating, extract the required values.
177          if (deletePolicyParameters.getPolicyName()==null||deletePolicyParameters.getPolicyName().trim().isEmpty()){
178          message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
179          return false;
180      }
181      if (!deletePolicyParameters.getPolicyName().contains("xml")) {
182          if (deletePolicyParameters.getPolicyName() != null
183                  && deletePolicyParameters.getPolicyName().contains(".")) {
184              policyName = deletePolicyParameters.getPolicyName().substring(deletePolicyParameters.getPolicyName().lastIndexOf(".") + 1,
185                      deletePolicyParameters.getPolicyName().length());
186              policyScope = deletePolicyParameters.getPolicyName().substring(0,deletePolicyParameters.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      } else {
193          policyName = deletePolicyParameters.getPolicyName();
194      }
195      policyType = deletePolicyParameters.getPolicyType();
196      if(policyType== null || policyType.trim().isEmpty()){
197          message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
198          return false;
199      }
200      setClientScope();
201      if (clientScope==null||clientScope.trim().isEmpty()){
202          message = XACMLErrorConstants.ERROR_DATA_ISSUE + deletePolicyParameters.getPolicyType() + " is not a valid Policy Type."; 
203          LOGGER.error(message);
204          return false;
205      }
206      LOGGER.debug("clientScope is " + clientScope);
207      LOGGER.debug("filePrefix is " + filePrefix);
208      if(deletePolicyParameters.getPolicyComponent()==null){
209          message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Component given.";
210          return false;
211      }
212      return true;
213     }
214
215     private void setClientScope() {
216         if ("Firewall".equalsIgnoreCase(policyType)) {
217             clientScope = "ConfigFirewall";
218             filePrefix = "Config_FW_";
219         } else if ("Action".equalsIgnoreCase(policyType)) {
220             clientScope = "Action";
221             filePrefix = "Action_";
222         } else if ("Decision".equalsIgnoreCase(policyType)) {
223             clientScope = "Decision";
224             filePrefix = "Decision_";
225         } else if ("Base".equalsIgnoreCase(policyType)) {
226             clientScope = "Config";
227             filePrefix = "Config_";
228         } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
229             clientScope = "ConfigClosedLoop";
230             filePrefix = "Config_Fault_";
231         } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
232             clientScope = "ConfigClosedLoop";
233             filePrefix = "Config_PM_";
234         } else if ("MicroService".equalsIgnoreCase(policyType)) {
235             clientScope = "ConfigMS";
236             filePrefix = "Config_MS_";
237         }else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
238             clientScope = "ConfigBrmsRaw";
239             filePrefix = "Config_BRMS_Raw_";
240         } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
241             clientScope = "ConfigBrmsParam";
242             filePrefix = "Config_BRMS_Param_";
243         } else {
244             clientScope = null;
245             message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
246                     + " is not a valid Policy Type.";
247         }
248     }
249
250     public String getResult() {
251         return deleteResult;
252     }
253
254     public HttpStatus getResponseCode() {
255         return status;
256     }
257
258 }