3784ae224918dbc0b0286c348a74d74351e0e837
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericDeleteService.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.scripts\r
22 \r
23 import static org.apache.commons.lang3.StringUtils.*\r
24 \r
25 import org.apache.commons.lang3.*\r
26 import org.camunda.bpm.engine.delegate.BpmnError\r
27 import org.camunda.bpm.engine.runtime.Execution\r
28 import org.openecomp.mso.rest.APIResponse\r
29 import org.springframework.web.util.UriUtils\r
30 \r
31 \r
32 /**\r
33  * This class supports the GenericDeleteService Sub Flow.\r
34  * This Generic sub flow can be used by any flow for the\r
35  * goal of deleting a Service-Instance or Service-Subscription.\r
36  * The calling flow must set the GENDS_type variable as "service-instance"\r
37  * or "service-subscription".\r
38  *\r
39  * If the resource-version is not provided by the calling flow\r
40  * then this sub flow will query the service-instance or\r
41  * service-subscription, prior to deleting it, in order to\r
42  * obtain this resource version.  Upon successful completion of\r
43  * this sub flow the GENDS_SuccessIndicator will be true.  A\r
44  * MSOWorkflowException will be thrown if an error occurs within this flow.\r
45  *\r
46  * Please map variables to the corresponding variable names\r
47  * below.\r
48  *\r
49  * Note - if this sub flow receives a Not Found (404) response\r
50  * from AAI at any time this will be considered an acceptable\r
51  * response.\r
52  *\r
53  *\r
54  * Variable Mapping Below\r
55  *\r
56  * In Mapping Variables:\r
57  *   For Service-Instance:\r
58  *     @param - GENDS_serviceInstanceId\r
59  *     @param - GENDS_serviceType\r
60  *     @param - GENDS_globalCustomerId\r
61  *     @param - GENDS_type\r
62  *     @param (Optional) - GENDS_resourceVersion\r
63  *\r
64  *   For Service-Subscription:\r
65  *     @param - GENDS_serviceType\r
66  *     @param - GENDS_globalCustomerId\r
67  *     @param - GENDS_type\r
68  *     @param (Optional) - GENDS_resourceVersion\r
69  *\r
70  *\r
71  * Out Mapping Variables:\r
72  *    @param - GENDS_FoundIndicator\r
73  *    @param - WorkflowException\r
74  *\r
75  *\r
76  */\r
77 class GenericDeleteService extends AbstractServiceTaskProcessor{\r
78 \r
79         String Prefix = "GENDS_"\r
80         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
81 \r
82         /**\r
83          * This method validates the incoming variables and\r
84          * determines if the resource version was provided\r
85          *\r
86          * @param - execution\r
87          */\r
88         public void preProcessRequest(Execution execution) {\r
89                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
90                 execution.setVariable("prefix",Prefix)\r
91                 utils.log("DEBUG", " *** STARTED GenericDeleteService PreProcessRequest Process*** ", isDebugEnabled)\r
92 \r
93                 execution.setVariable("GENDS_resourceVersionProvidedFlag", true)\r
94                 execution.setVariable("GENDS_SuccessIndicator", false)\r
95                 execution.setVariable("GENDS_FoundIndicator", false)\r
96 \r
97                 try{\r
98                         // Get Variables\r
99                         String globalCustomerId = execution.getVariable("GENDS_globalCustomerId")\r
100                         String serviceInstanceId = execution.getVariable("GENDS_serviceInstanceId")\r
101                         String serviceType = execution.getVariable("GENDS_serviceType")\r
102                         String type = execution.getVariable("GENDS_type")\r
103 \r
104                         if(type != null){\r
105                                 utils.log("DEBUG", "Incoming GENDS_type is: " + type, isDebugEnabled)\r
106                                 if(isBlank(globalCustomerId) || isBlank(serviceType)){\r
107                                         utils.log("DEBUG", "Incoming Required Variable is null!", isDebugEnabled)\r
108                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")\r
109                                 }else{\r
110                                         utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)\r
111                                         utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)\r
112                                         if(type.equalsIgnoreCase("service-instance")){\r
113                                                 if(isBlank(serviceInstanceId)){\r
114                                                         utils.log("DEBUG", "Incoming Required Variable is null!", isDebugEnabled)\r
115                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")\r
116                                                 }else{\r
117                                                         utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)\r
118                                                         utils.log("DEBUG", "Preparing Delete Service-Instance Process", isDebugEnabled)\r
119                                                 }\r
120                                         }else if(type.equalsIgnoreCase("service-subscription")){\r
121                                                 utils.log("DEBUG", "Preparing Delete Service-Subscription Process", isDebugEnabled)\r
122                                         }else{\r
123                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")\r
124                                         }\r
125                                 }\r
126 \r
127                                 String resourceVersion = execution.getVariable('GENDS_resourceVersion')\r
128                                 if(isBlank(resourceVersion)){\r
129                                         utils.log("DEBUG", "Service Instance Resource Version is NOT Provided", isDebugEnabled)\r
130                                         execution.setVariable("GENDS_resourceVersionProvidedFlag", false)\r
131                                 }else{\r
132                                         utils.log("DEBUG", "Incoming SI Resource Version is: " + resourceVersion, isDebugEnabled)\r
133                                 }\r
134 \r
135                         }else{\r
136                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENDS_type is null. Variable is Required.")\r
137                         }\r
138                 }catch(BpmnError b){\r
139                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
140                         throw b\r
141                 }catch(Exception e){\r
142                         utils.log("ERROR", " Error encountered within GenericDeleteService PreProcessRequest method!" + e, isDebugEnabled)\r
143                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteService PreProcessRequest")\r
144 \r
145                 }\r
146                 utils.log("DEBUG", "*** COMPLETED GenericDeleteService PreProcessRequest Process ***", isDebugEnabled)\r
147         }\r
148 \r
149         /**\r
150          * This method executes a GET call to AAI for the service instance\r
151          * or service-subscription so that the objects's resource-version\r
152          * can be obtained.\r
153          *\r
154          * @param - execution\r
155          */\r
156         public void getServiceResourceVersion(Execution execution){\r
157                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
158                 execution.setVariable("prefix",Prefix)\r
159                 utils.log("DEBUG", " *** STARTED GenericDeleteService GetServiceResourceVersion Process*** ", isDebugEnabled)\r
160                 try {\r
161                         String serviceType = execution.getVariable("GENDS_serviceType")\r
162                         utils.log("DEBUG", " Incoming GENDS_serviceType is: " + serviceType, isDebugEnabled)\r
163                         String globalCustomerId = execution.getVariable("GENDS_globalCustomerId")\r
164                         utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)\r
165                         String type = execution.getVariable("GENDS_type")\r
166                         String serviceEndpoint = ""\r
167 \r
168                         if(type.equalsIgnoreCase("service-instance")){\r
169                                 String serviceInstanceId = execution.getVariable("GENDS_serviceInstanceId")\r
170                                 utils.log("DEBUG", " Incoming GENDS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)\r
171                                 serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8")\r
172 \r
173                         }else if(type.equalsIgnoreCase("service-subscription")){\r
174                                 serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")\r
175                         }\r
176 \r
177                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
178                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
179                         String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)\r
180                         logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)\r
181 \r
182                         String serviceAaiPath = "${aai_endpoint}${aai_uri}/"  + serviceEndpoint\r
183 \r
184                         execution.setVariable("GENDS_serviceAaiPath", serviceAaiPath)\r
185                         utils.log("DEBUG", "GET Service Instance AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)\r
186                         utils.logAudit("GenericDeleteService GET AAI Path: " + serviceAaiPath)\r
187                         \r
188                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceAaiPath)\r
189                         int responseCode = response.getStatusCode()\r
190                         execution.setVariable("GENDS_getServiceResponseCode", responseCode)\r
191                         utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)\r
192                         utils.logAudit("GET Service Instance response code: " + responseCode)\r
193                         \r
194                         String aaiResponse = response.getResponseBodyAsString()\r
195                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
196                         execution.setVariable("GENDS_getServiceResponse", aaiResponse)\r
197 \r
198                         utils.logAudit("GET Service Instance response : " + aaiResponse)\r
199                         //Process Response\r
200                         if(responseCode == 200 || responseCode == 202){\r
201                                 utils.log("DEBUG", "GET Service Received a Good Response: \n" + aaiResponse, isDebugEnabled)\r
202                                 execution.setVariable("GENDS_SuccessIndicator", true)\r
203                                 execution.setVariable("GENDS_FoundIndicator", true)\r
204                                 String resourceVersion = utils.getNodeText1(aaiResponse, "resource-version")\r
205                                 execution.setVariable("GENDS_resourceVersion", resourceVersion)\r
206                                 utils.log("DEBUG", type + " Resource Version is: " + resourceVersion, isDebugEnabled)\r
207 \r
208                         }else if(responseCode == 404){\r
209                                 utils.log("DEBUG", "GET Service Received a Not Found (404) Response", isDebugEnabled)\r
210                                 execution.setVariable("GENDS_SuccessIndicator", true)\r
211                                 execution.setVariable("WorkflowResponse", "  ") // for junits\r
212                         }\r
213                         else{\r
214                                 utils.log("DEBUG", "  GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)\r
215                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
216                                 throw new BpmnError("MSOWorkflowException")\r
217                         }\r
218                 }catch(BpmnError b){\r
219                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
220                         throw b\r
221                 }catch(Exception e){\r
222                         utils.log("DEBUG", " Error encountered within GenericDeleteService GetServiceResourceVersion method!" + e, isDebugEnabled)\r
223                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetServiceResourceVersion")\r
224                 }\r
225                 utils.log("DEBUG", " *** COMPLETED GenericDeleteService GetServiceResourceVersion Process*** ", isDebugEnabled)\r
226         }\r
227 \r
228         /**\r
229          * This method executes a DELETE call to AAI for the provided\r
230          * service-instance or service-subscription.\r
231          *\r
232          * @param - execution\r
233          */\r
234         public void deleteServiceObject(Execution execution){\r
235                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
236                 execution.setVariable("prefix",Prefix)\r
237                 utils.log("DEBUG", " *** STARTED GenericDeleteService DeleteServiceObject Process*** ", isDebugEnabled)\r
238                 try {\r
239                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
240                         String type = execution.getVariable("GENDS_type")\r
241                         String serviceAaiPath = execution.getVariable("GENDS_serviceAaiPath")\r
242                         String serviceEndpoint = ""\r
243 \r
244                         if(isEmpty(serviceAaiPath)){\r
245                                 String serviceType = execution.getVariable("GENDS_serviceType")\r
246                                 utils.log("DEBUG", " Incoming GENDS_serviceType is: " + serviceType, isDebugEnabled)\r
247                                 String globalCustomerId = execution.getVariable("GENDS_globalCustomerId")\r
248                                 utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)\r
249                                 if(type.equalsIgnoreCase("service-instance")){\r
250                                         String serviceInstanceId = execution.getVariable("GENDS_serviceInstanceId")\r
251                                         utils.log("DEBUG", " Incoming GENDS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)\r
252                                         serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8")\r
253 \r
254                                 }else if(type.equalsIgnoreCase("service-subscription")){\r
255                                         serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")\r
256                                 }\r
257 \r
258                                 String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
259                                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)\r
260                                 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)\r
261 \r
262                                 serviceAaiPath = "${aai_endpoint}${aai_uri}/"  + serviceEndpoint\r
263                         }\r
264 \r
265                         String resourceVersion = execution.getVariable("GENDS_resourceVersion")\r
266                         utils.log("DEBUG", "Incoming Resource Version is: " + resourceVersion, isDebugEnabled)\r
267                         if(resourceVersion !=null){\r
268                                 serviceAaiPath = serviceAaiPath +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")\r
269                         }\r
270 \r
271                         execution.setVariable("GENDS_deleteServiceAaiPath", serviceAaiPath)\r
272                         utils.log("DEBUG", "DELETE Service AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)\r
273                         utils.logAudit("DELETE Service AAI Path: " + serviceAaiPath)\r
274                         \r
275                         APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, serviceAaiPath)\r
276                         int responseCode = response.getStatusCode()\r
277                         execution.setVariable("GENDS_deleteServiceResponseCode", responseCode)\r
278                         utils.log("DEBUG", "  DELETE Service response code is: " + responseCode, isDebugEnabled)\r
279                         utils.logAudit("DELETE Service Response Code: " + responseCode)\r
280 \r
281                         String aaiResponse = response.getResponseBodyAsString()\r
282                         aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
283                         execution.setVariable("GENDS_deleteServiceResponse", aaiResponse)\r
284                         utils.logAudit("DELETE Service Response: " + aaiResponse)\r
285                         \r
286                         //Process Response\r
287                         if(responseCode == 200 || responseCode == 204){\r
288                                 utils.log("DEBUG", "  DELETE Service Received a Good Response", isDebugEnabled)\r
289                                 execution.setVariable("GENDS_FoundIndicator", true)\r
290                         }else if(responseCode == 404){\r
291                                 utils.log("DEBUG", "  DELETE Service Received a Not Found (404) Response", isDebugEnabled)\r
292                                 execution.setVariable("GENDS_FoundIndicator", false)\r
293                         }else{\r
294                                 utils.log("DEBUG", "DELETE Service Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)\r
295                                 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
296                                 throw new BpmnError("MSOWorkflowException")\r
297                         }\r
298                 }catch(BpmnError b){\r
299                         utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
300                         throw b\r
301                 }catch(Exception e){\r
302                         utils.log("DEBUG", " Error encountered within GenericDeleteService DeleteServiceObject method!" + e, isDebugEnabled)\r
303                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete Service Object")\r
304                 }\r
305                 utils.log("DEBUG", " *** COMPLETED GenericDeleteService DeleteServiceObject Process*** ", isDebugEnabled)\r
306         }\r
307 \r
308 }\r