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