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 com.att.bpm.scripts
23 import static org.apache.commons.lang3.StringUtils.*;
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.*
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
35 import org.springframework.web.util.UriUtils
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".
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.
52 * Please map variables to the corresponding variable names
55 * Note - if this sub flow receives a Not Found (404) response
56 * from AAI at any time this will be considered an acceptable
59 * Incoming Variables (Service-Instance):
60 * @param - GENDS_serviceInstanceId
61 * @param - GENDS_serviceType
62 * @param - GENDS_globalCustomerId
64 * @param (Optional) - GENDS_resourceVersion
66 * Incoming Variables (Service-Subscription):
67 * @param - GENDS_serviceType
68 * @param - GENDS_globalCustomerId
70 * @param (Optional) - GENDS_resourceVersion
74 * @param - GENDS_SuccessIndicator
75 * @param - GENDS_FoundIndicator
76 * @param - WorkflowException
78 class GenericDeleteService extends AbstractServiceTaskProcessor{
80 String Prefix = "GENDS_"
81 ExceptionUtil exceptionUtil = new ExceptionUtil()
84 * This method validates the incoming variables and
85 * determines if the resource version was provided
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)
94 execution.setVariable("GENDS_resourceVersionProvidedFlag", true)
95 execution.setVariable("GENDS_SuccessIndicator", false)
96 execution.setVariable("GENDS_FoundIndicator", false)
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")
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!")
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!")
118 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
119 utils.log("DEBUG", "Preparing Delete Service-Instance Process", isDebugEnabled)
121 }else if(type.equalsIgnoreCase("service-subscription")){
122 utils.log("DEBUG", "Preparing Delete Service-Subscription Process", isDebugEnabled)
124 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
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)
133 utils.log("DEBUG", "Incoming SI Resource Version is: " + resourceVersion, isDebugEnabled)
137 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENDS_type is null. Variable is Required.")
140 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
143 utils.log("ERROR", " Error encountered within GenericDeleteService PreProcessRequest method!" + e, isDebugEnabled)
144 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteService PreProcessRequest")
147 utils.log("DEBUG", "*** COMPLETED GenericDeleteService PreProcessRequest Process ***", isDebugEnabled)
151 * This method executes a GET call to AAI for the service instance
152 * or service-subscription so that the objects's resource-version
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)
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 = ""
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")
174 }else if(type.equalsIgnoreCase("service-subscription")){
175 serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
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)
183 String serviceAaiPath = "${aai_endpoint}${aai_uri}/" + serviceEndpoint
185 execution.setVariable("GENDS_serviceAaiPath", serviceAaiPath)
186 utils.log("DEBUG", "GET Service Instance AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)
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)
193 String aaiResponse = response.getResponseBodyAsString()
194 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
195 execution.setVariable("GENDS_getServiceResponse", aaiResponse)
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)
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
212 utils.log("DEBUG", " GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
213 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
214 throw new BpmnError("MSOWorkflowException")
217 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
220 utils.log("DEBUG", " Error encountered within GenericDeleteService GetServiceResourceVersion method!" + e, isDebugEnabled)
221 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetServiceResourceVersion")
223 utils.log("DEBUG", " *** COMPLETED GenericDeleteService GetServiceResourceVersion Process*** ", isDebugEnabled)
227 * This method executes a DELETE call to AAI for the provided
228 * service-instance or service-subscription.
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)
237 AaiUtil aaiUriUtil = new AaiUtil(this)
238 String type = execution.getVariable("GENDS_type")
239 String serviceAaiPath = execution.getVariable("GENDS_serviceAaiPath")
240 String serviceEndpoint = ""
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")
252 }else if(type.equalsIgnoreCase("service-subscription")){
253 serviceEndpoint = UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
256 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
257 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
258 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
260 serviceAaiPath = "${aai_endpoint}${aai_uri}/" + serviceEndpoint
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")
269 execution.setVariable("GENDS_deleteServiceAaiPath", serviceAaiPath)
270 utils.log("DEBUG", "DELETE Service AAI Path is: " + "\n" + serviceAaiPath, isDebugEnabled)
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)
277 String aaiResponse = response.getResponseBodyAsString()
278 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
279 execution.setVariable("GENDS_deleteServiceResponse", 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)