2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.bpmn.common.scripts
23 import static org.apache.commons.lang3.StringUtils.*
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
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".
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.
46 * Please map variables to the corresponding variable names
49 * Note - if this sub flow receives a Not Found (404) response
50 * from AAI at any time this will be considered an acceptable
53 * Incoming Variables (Service-Instance):
54 * @param - GENDS_serviceInstanceId
55 * @param - GENDS_serviceType
56 * @param - GENDS_globalCustomerId
58 * @param (Optional) - GENDS_resourceVersion
60 * Incoming Variables (Service-Subscription):
61 * @param - GENDS_serviceType
62 * @param - GENDS_globalCustomerId
64 * @param (Optional) - GENDS_resourceVersion
68 * @param - GENDS_SuccessIndicator
69 * @param - GENDS_FoundIndicator
70 * @param - WorkflowException
72 class GenericDeleteService extends AbstractServiceTaskProcessor{
74 String Prefix = "GENDS_"
75 ExceptionUtil exceptionUtil = new ExceptionUtil()
78 * This method validates the incoming variables and
79 * determines if the resource version was provided
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)
88 execution.setVariable("GENDS_resourceVersionProvidedFlag", true)
89 execution.setVariable("GENDS_SuccessIndicator", false)
90 execution.setVariable("GENDS_FoundIndicator", false)
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")
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!")
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!")
112 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
113 utils.log("DEBUG", "Preparing Delete Service-Instance Process", isDebugEnabled)
115 }else if(type.equalsIgnoreCase("service-subscription")){
116 utils.log("DEBUG", "Preparing Delete Service-Subscription Process", isDebugEnabled)
118 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
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)
127 utils.log("DEBUG", "Incoming SI Resource Version is: " + resourceVersion, isDebugEnabled)
131 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENDS_type is null. Variable is Required.")
134 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
137 utils.log("ERROR", " Error encountered within GenericDeleteService PreProcessRequest method!" + e, isDebugEnabled)
138 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteService PreProcessRequest")
141 utils.log("DEBUG", "*** COMPLETED GenericDeleteService PreProcessRequest Process ***", isDebugEnabled)
145 * This method executes a GET call to AAI for the service instance
146 * or service-subscription so that the objects's resource-version
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)
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 = ""
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")
168 }else if(type.equalsIgnoreCase("service-subscription")){
169 serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
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)
177 String serviceAaiPath = "${aai_endpoint}${aai_uri}/" + serviceEndpoint
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)
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)
189 String aaiResponse = response.getResponseBodyAsString()
190 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
191 execution.setVariable("GENDS_getServiceResponse", aaiResponse)
193 utils.logAudit("GET Service Instance response : " + aaiResponse)
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)
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
209 utils.log("DEBUG", " GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
210 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
211 throw new BpmnError("MSOWorkflowException")
214 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
217 utils.log("DEBUG", " Error encountered within GenericDeleteService GetServiceResourceVersion method!" + e, isDebugEnabled)
218 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetServiceResourceVersion")
220 utils.log("DEBUG", " *** COMPLETED GenericDeleteService GetServiceResourceVersion Process*** ", isDebugEnabled)
224 * This method executes a DELETE call to AAI for the provided
225 * service-instance or service-subscription.
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)
234 AaiUtil aaiUriUtil = new AaiUtil(this)
235 String type = execution.getVariable("GENDS_type")
236 String serviceAaiPath = execution.getVariable("GENDS_serviceAaiPath")
237 String serviceEndpoint = ""
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")
249 }else if(type.equalsIgnoreCase("service-subscription")){
250 serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
253 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
254 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
255 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
257 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + serviceEndpoint
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")
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)
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)
276 String aaiResponse = response.getResponseBodyAsString()
277 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
278 execution.setVariable("GENDS_deleteServiceResponse", aaiResponse)
279 utils.logAudit("DELETE Service Response: " + aaiResponse)
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)
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")
294 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
297 utils.log("DEBUG", " Error encountered within GenericDeleteService DeleteServiceObject method!" + e, isDebugEnabled)
298 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete Service Object")
300 utils.log("DEBUG", " *** COMPLETED GenericDeleteService DeleteServiceObject Process*** ", isDebugEnabled)