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