c9ecee86bbf7988b363c1c28556988f814fb9934
[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-2018 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             if(deleteResult.contains("groupId")) {
79                 status = HttpStatus.NOT_FOUND;
80             } else {
81                 status = HttpStatus.BAD_REQUEST;
82             }
83         } else if (deleteResult.contains("locked down")){
84             status = HttpStatus.ACCEPTED;
85         } else if (deleteResult.contains("not Authorized")) {
86             status = HttpStatus.FORBIDDEN;
87         } else if (deleteResult.contains("JPAUtils")||deleteResult.contains("database")||deleteResult.contains("policy file")||
88                 deleteResult.contains("unknown")||deleteResult.contains("configuration")) {
89             status = HttpStatus.INTERNAL_SERVER_ERROR;
90         } 
91     }
92
93     private void run() throws PolicyException{
94         // Check Validation. 
95         if(!getValidation()){
96             LOGGER.error(message);
97             throw new PolicyException(message);
98         }
99         // Get Result. 
100         try{
101             status = HttpStatus.OK;
102             deleteResult = processResult();
103         }catch (Exception e){
104             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e);
105             status = HttpStatus.BAD_REQUEST;
106             throw new PolicyException(e);
107         }
108     }
109
110     private String processResult() throws PolicyException{
111         String response = null;
112         String fullPolicyName = null;
113         String pdpGroup = deletePolicyParameters.getPdpGroup();
114         // PDP Group Check. 
115         if (pdpGroup==null){
116             pdpGroup="NA";
117         }
118         PAPServices papServices = new PAPServices();
119         if (!deletePolicyParameters.getPolicyName().contains("xml")) {
120             
121             String activeVersion = papServices.getActiveVersion(policyScope, filePrefix, policyName, clientScope, deletePolicyParameters.getRequestID());
122             LOGGER.debug("The active version of " + policyScope + File.separator + filePrefix + policyName + " is " + activeVersion);
123             String id = null;
124             if ("pe100".equalsIgnoreCase(activeVersion)) {
125                 response = XACMLErrorConstants.ERROR_PERMISSIONS + "response code of the URL is 403. PEP is not Authorized for making this Request!! "
126                         + "Contact Administrator for this Scope. "; 
127                 LOGGER.error(response);
128                 return response;
129             } else if ("pe300".equalsIgnoreCase(activeVersion)) {
130                 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "response code of the URL is 404.  "
131                         + "This indicates a problem with getting the version from the PAP or the policy does not exist.";
132                 LOGGER.error(response);
133                 return response;
134             }
135             if (!activeVersion.equalsIgnoreCase("0")) {
136                 id = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
137                 LOGGER.debug("The policyId is " + id);
138             } else {
139                 response = XACMLErrorConstants.ERROR_DATA_ISSUE + "could not retrieve the activeVersion for this policy. could not retrieve the activeVersion for this policy.  "
140                         + "This indicates the policy does not exist, please verify the policy exists."; 
141                 LOGGER.error(response);
142                 return response;
143             }
144             
145             fullPolicyName = policyScope + "." + filePrefix + policyName + "." + activeVersion + ".xml";
146             
147         } else {
148             fullPolicyName = policyName;
149         }
150
151         if ("PAP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
152             if (deletePolicyParameters.getDeleteCondition()==null||deletePolicyParameters.getDeleteCondition().toString().trim().isEmpty()){
153                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Delete Condition given.";
154                 LOGGER.error(message);
155                 return message;
156             }
157             
158             StdPAPPolicy deletePapPolicy = new StdPAPPolicy(fullPolicyName, deletePolicyParameters.getDeleteCondition().toString());
159             //send JSON object to PAP
160             response = (String) papServices.callPAP(deletePapPolicy, new String[] {"groupId="+pdpGroup, "apiflag=deletePapApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope);
161         } else if ("PDP".equalsIgnoreCase(deletePolicyParameters.getPolicyComponent())) {
162             if (deletePolicyParameters.getPdpGroup()==null||deletePolicyParameters.getPdpGroup().trim().isEmpty()){
163                 message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PDP Group given."; 
164                 LOGGER.error(message);
165                 return message;
166             }
167             //send JSON object to PAP
168             response = (String) papServices.callPAP(null, new String[] {"policyName="+fullPolicyName, "groupId="+pdpGroup, "apiflag=deletePdpApi", "operation=delete" }, deletePolicyParameters.getRequestID(), clientScope);
169         } else {
170             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.";
171             LOGGER.error(message);
172             response = message;
173         }
174         return response;
175     }
176
177  private boolean getValidation() {
178      // While Validating, extract the required values.
179          if (deletePolicyParameters.getPolicyName()==null||deletePolicyParameters.getPolicyName().trim().isEmpty()){
180          message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Name given.";
181          return false;
182      }
183      if (!deletePolicyParameters.getPolicyName().contains("xml")) {
184          if (deletePolicyParameters.getPolicyName() != null
185                  && deletePolicyParameters.getPolicyName().contains(".")) {
186              policyName = deletePolicyParameters.getPolicyName().substring(deletePolicyParameters.getPolicyName().lastIndexOf('.') + 1,
187                      deletePolicyParameters.getPolicyName().length());
188              policyScope = deletePolicyParameters.getPolicyName().substring(0,deletePolicyParameters.getPolicyName().lastIndexOf('.'));
189              LOGGER.info("Name is " + policyName + "   scope is " + policyScope);
190          } else {
191              message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Scope given.";
192              return false;
193          }
194      } else {
195          policyName = deletePolicyParameters.getPolicyName();
196      }
197      policyType = deletePolicyParameters.getPolicyType();
198      if(policyType== null || policyType.trim().isEmpty()){
199          message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No PolicyType given.";
200          return false;
201      }
202      setClientScope();
203      if (clientScope==null||clientScope.trim().isEmpty()){
204          message = XACMLErrorConstants.ERROR_DATA_ISSUE + deletePolicyParameters.getPolicyType() + " is not a valid Policy Type."; 
205          LOGGER.error(message);
206          return false;
207      }
208      LOGGER.debug("clientScope is " + clientScope);
209      LOGGER.debug("filePrefix is " + filePrefix);
210      if(deletePolicyParameters.getPolicyComponent()==null){
211          message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Policy Component given.";
212          return false;
213      }
214      return true;
215     }
216
217     private void setClientScope() {
218         if ("Firewall".equalsIgnoreCase(policyType)) {
219             clientScope = "ConfigFirewall";
220             filePrefix = "Config_FW_";
221         } else if ("Action".equalsIgnoreCase(policyType)) {
222             clientScope = "Action";
223             filePrefix = "Action_";
224         } else if ("Decision".equalsIgnoreCase(policyType)) {
225             clientScope = "Decision";
226             filePrefix = "Decision_";
227         } else if ("Base".equalsIgnoreCase(policyType)) {
228             clientScope = "Config";
229             filePrefix = "Config_";
230         } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyType)) {
231             clientScope = "ConfigClosedLoop";
232             filePrefix = "Config_Fault_";
233         } else if ("ClosedLoop_PM".equalsIgnoreCase(policyType)) {
234             clientScope = "ConfigClosedLoop";
235             filePrefix = "Config_PM_";
236         } else if ("MicroService".equalsIgnoreCase(policyType)) {
237             clientScope = "ConfigMS";
238             filePrefix = "Config_MS_";
239         } else if ("Optimization".equalsIgnoreCase(policyType)) {
240                 clientScope = "ConfigOptimization";
241                 filePrefix = "Config_OOF_";
242         }else if ("BRMS_RAW".equalsIgnoreCase(policyType)) {
243             clientScope = "ConfigBrmsRaw";
244             filePrefix = "Config_BRMS_Raw_";
245         } else if ("BRMS_PARAM".equalsIgnoreCase(policyType)) {
246             clientScope = "ConfigBrmsParam";
247             filePrefix = "Config_BRMS_Param_";
248         } else {
249             clientScope = null;
250             message = XACMLErrorConstants.ERROR_DATA_ISSUE + policyType
251                     + " is not a valid Policy Type.";
252         }
253     }
254
255     public String getResult() {
256         return deleteResult;
257     }
258
259     public HttpStatus getResponseCode() {
260         return status;
261     }
262
263 }