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=========================================================
20 package org.openecomp.mso.bpmn.infrastructure.scripts;
22 import static org.apache.commons.lang3.StringUtils.*;
23 import groovy.xml.XmlUtil
26 import org.openecomp.mso.bpmn.core.json.JsonUtils
27 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
29 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
30 import org.openecomp.mso.bpmn.common.scripts.VidUtils
31 import org.openecomp.mso.bpmn.core.WorkflowException
32 import org.openecomp.mso.rest.APIResponse;
33 import org.openecomp.mso.rest.RESTClient
34 import org.openecomp.mso.rest.RESTConfig
36 import java.util.UUID;
37 import javax.xml.parsers.DocumentBuilder
38 import javax.xml.parsers.DocumentBuilderFactory
40 import org.camunda.bpm.engine.delegate.BpmnError
41 import org.camunda.bpm.engine.runtime.Execution
42 import org.json.JSONObject;
43 import org.apache.commons.lang3.*
44 import org.apache.commons.codec.binary.Base64;
45 import org.springframework.web.util.UriUtils;
46 import org.w3c.dom.Document
47 import org.w3c.dom.Element
48 import org.w3c.dom.Node
49 import org.w3c.dom.NodeList
50 import org.xml.sax.InputSource
53 * This groovy class supports the <class>DoDeleteServiceInstance.bpmn</class> process.
56 * @param - msoRequestId
57 * @param - globalSubscriberId - O
58 * @param - subscriptionServiceType - O
59 * @param - serviceInstanceId
60 * @param - serviceInstanceName - O
61 * @param - serviceModelInfo - O
62 * @param - productFamilyId
63 * @param - sdncVersion
64 * @param - failNotFound - TODO
65 * @param - serviceInputParams - TODO
68 * @param - WorkflowException
72 public class DoDeleteServiceInstance extends AbstractServiceTaskProcessor {
74 String Prefix="DDELSI_"
75 ExceptionUtil exceptionUtil = new ExceptionUtil()
76 JsonUtils jsonUtil = new JsonUtils()
77 VidUtils vidUtils = new VidUtils()
79 public void preProcessRequest (Execution execution) {
80 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
81 utils.log("DEBUG"," ***** preProcessRequest *****", isDebugEnabled)
85 String requestId = execution.getVariable("msoRequestId")
86 execution.setVariable("prefix",Prefix)
89 //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
90 String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
91 if (globalSubscriberId == null)
93 execution.setVariable("globalSubscriberId", "")
96 //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
97 String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
98 if (subscriptionServiceType == null)
100 execution.setVariable("subscriptionServiceType", "")
103 //Generated in parent for AAI PUT
104 String serviceInstanceId = execution.getVariable("serviceInstanceId")
105 if (isBlank(serviceInstanceId)){
106 msg = "Input serviceInstanceId is null"
107 utils.log("DEBUG", msg, isDebugEnabled)
108 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
111 String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')
112 if (isBlank(sdncCallbackUrl)) {
113 msg = "URN_mso_workflow_sdncadapter_callback is null"
114 utils.log("DEBUG", msg, isDebugEnabled)
115 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
117 execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
118 utils.log("DEBUG","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
120 } catch (BpmnError e) {
122 } catch (Exception ex){
123 msg = "Exception in preProcessRequest " + ex.getMessage()
124 utils.log("DEBUG", msg, isDebugEnabled)
125 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
127 utils.log("DEBUG"," ***** Exit preProcessRequest *****", isDebugEnabled)
130 public void preProcessSDNCDelete (Execution execution) {
131 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
132 utils.log("DEBUG"," ***** preProcessSDNCDelete *****", isDebugEnabled)
137 String uuid = execution.getVariable('testReqId') // for junits
139 uuid = execution.getVariable("msoRequestId") + "-" + System.currentTimeMillis()
142 def serviceInstanceId = execution.getVariable("serviceInstanceId")
143 def serviceInstanceName = execution.getVariable("serviceInstanceName")
144 def callbackURL = execution.getVariable("sdncCallbackUrl")
145 def requestId = execution.getVariable("msoRequestId")
146 def serviceId = execution.getVariable("productFamilyId")
147 def subscriptionServiceType = execution.getVariable("subscriptionServiceType")
148 def globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
150 String serviceModelInfo = execution.getVariable("serviceModelInfo")
151 def modelInvariantId = ""
152 def modelVersion = ""
155 if (!isBlank(serviceModelInfo))
157 modelInvariantId = jsonUtil.getJsonValue(serviceModelInfo, "modelInvariantId")
158 modelVersion = jsonUtil.getJsonValue(serviceModelInfo, "modelVersion")
159 modelUUId = jsonUtil.getJsonValue(serviceModelInfo, "modelVersionId")
160 modelName = jsonUtil.getJsonValue(serviceModelInfo, "modelName")
162 if (modelInvariantId == null) {
163 modelInvariantId = ""
165 if (modelVersion == null) {
168 if (modelUUId == null) {
171 if (modelName == null) {
175 if (serviceInstanceName == null) {
176 serviceInstanceName = ""
178 if (serviceId == null) {
182 def sdncRequestId = UUID.randomUUID().toString()
185 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"
186 xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"
187 xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">
188 <sdncadapter:RequestHeader>
189 <sdncadapter:RequestId>${sdncRequestId}</sdncadapter:RequestId>
190 <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>
191 <sdncadapter:SvcAction>delete</sdncadapter:SvcAction>
192 <sdncadapter:SvcOperation>service-topology-operation</sdncadapter:SvcOperation>
193 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>
194 </sdncadapter:RequestHeader>
195 <sdncadapterworkflow:SDNCRequestData>
196 <request-information>
197 <request-id>${requestId}</request-id>
202 <request-action>DeleteServiceInstance</request-action>
203 </request-information>
204 <service-information>
205 <service-id>${serviceId}</service-id>
206 <subscription-service-type>${subscriptionServiceType}</subscription-service-type>
207 <ecomp-model-information>
208 <model-invariant-uuid>${modelInvariantId}</model-invariant-uuid>
209 <model-uuid>${modelUUId}</model-uuid>
210 <model-version>${modelVersion}</model-version>
211 <model-name>${modelName}</model-name>
212 </ecomp-model-information>
213 <service-instance-id>${serviceInstanceId}</service-instance-id>
215 <global-customer-id>${globalSubscriberId}</global-customer-id>
216 </service-information>
217 <service-request-input>
218 <service-instance-name>${serviceInstanceName}</service-instance-name>
219 </service-request-input>
220 </sdncadapterworkflow:SDNCRequestData>
221 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
223 sdncDelete = utils.formatXml(sdncDelete)
224 def sdncRequestId2 = UUID.randomUUID().toString()
225 String sdncDeactivate = sdncDelete.replace(">delete<", ">deactivate<").replace(">${sdncRequestId}<", ">${sdncRequestId2}<")
226 execution.setVariable("sdncDelete", sdncDelete)
227 execution.setVariable("sdncDeactivate", sdncDeactivate)
228 utils.log("DEBUG","sdncDeactivate:\n" + sdncDeactivate, isDebugEnabled)
229 utils.log("DEBUG","sdncDelete:\n" + sdncDelete, isDebugEnabled)
231 } catch (BpmnError e) {
233 } catch(Exception ex) {
234 msg = "Exception in preProcessSDNCDelete. " + ex.getMessage()
235 utils.log("DEBUG", msg, isDebugEnabled)
236 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception Occured in preProcessSDNCDelete.\n" + ex.getMessage())
238 utils.log("DEBUG"," *****Exit preProcessSDNCDelete *****", isDebugEnabled)
241 public void postProcessSDNCDelete(Execution execution, String response, String method) {
243 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
244 utils.log("DEBUG"," ***** postProcessSDNC " + method + " *****", isDebugEnabled)
248 WorkflowException workflowException = execution.getVariable("WorkflowException")
249 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
250 utils.log("DEBUG", "SDNCResponse: " + response, isDebugEnabled)
251 utils.log("DEBUG", "workflowException: " + workflowException, isDebugEnabled)
253 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
254 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
256 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
257 utils.log("DEBUG","Good response from SDNC Adapter for service-instance " + method + "response:\n" + response, isDebugEnabled)
260 msg = "Bad Response from SDNC Adapter for service-instance " + method
261 utils.log("DEBUG", msg, isDebugEnabled)
262 exceptionUtil.buildAndThrowWorkflowException(execution, 3500, msg)
264 } catch (BpmnError e) {
266 } catch(Exception ex) {
267 msg = "Exception in postProcessSDNC " + method + " Exception:" + ex.getMessage()
268 utils.log("DEBUG", msg, isDebugEnabled)
269 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
271 utils.log("DEBUG"," *** Exit postProcessSDNC " + method + " ***", isDebugEnabled)
274 public void postProcessAAIGET(Execution execution) {
275 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
276 utils.log("DEBUG"," ***** postProcessAAIGET ***** ", isDebugEnabled)
281 String serviceInstanceId = execution.getVariable("serviceInstanceId")
282 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
284 if(foundInAAI == true){
285 utils.log("DEBUG","Found Service-instance in AAI", isDebugEnabled)
287 //Extract GlobalSubscriberId
288 String siRelatedLink = execution.getVariable("GENGS_siResourceLink")
289 if (isBlank(siRelatedLink))
291 msg = "Could not retrive ServiceInstance data from AAI to delete id:" + serviceInstanceId
292 utils.log("DEBUG", msg, isDebugEnabled)
293 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
297 utils.log("DEBUG","Found Service-instance in AAI. link: " + siRelatedLink, isDebugEnabled)
298 String globalSubscriberId = execution.getVariable("globalSubscriberId")
299 if(isBlank(globalSubscriberId)){
300 int custStart = siRelatedLink.indexOf("customer/")
301 int custEnd = siRelatedLink.indexOf("/service-subscriptions")
302 globalSubscriberId = siRelatedLink.substring(custStart + 9, custEnd)
303 execution.setVariable("globalSubscriberId", globalSubscriberId)
306 //Extract Service Type if not provided on request
307 String serviceType = execution.getVariable("subscriptionServiceType")
308 if(isBlank(serviceType)){
309 int serviceStart = siRelatedLink.indexOf("service-subscription/")
310 int serviceEnd = siRelatedLink.indexOf("/service-instances/")
311 String serviceTypeEncoded = siRelatedLink.substring(serviceStart + 21, serviceEnd)
312 serviceType = UriUtils.decode(serviceTypeEncoded, "UTF-8")
313 execution.setVariable("subscriptionServiceType", serviceType)
316 if (isBlank(globalSubscriberId) || isBlank(serviceType))
318 msg = "Could not retrive global-customer-id & service-type from AAI to delete id:" + serviceInstanceId
319 utils.log("DEBUG", msg, isDebugEnabled)
320 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
324 String siData = execution.getVariable("GENGS_service")
325 utils.log("DEBUG", "SI Data", isDebugEnabled)
328 msg = "Could not retrive ServiceInstance data from AAI to delete id:" + serviceInstanceId
329 utils.log("DEBUG", msg, isDebugEnabled)
330 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
334 utils.log("DEBUG", "SI Data" + siData, isDebugEnabled)
335 //Confirm there are no related service instances (vnf/network or volume)
336 if (utils.nodeExists(siData, "relationship-list")) {
337 utils.log("DEBUG", "SI Data relationship-list exists:", isDebugEnabled)
338 InputSource source = new InputSource(new StringReader(siData));
339 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
340 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
341 Document serviceXml = docBuilder.parse(source)
343 NodeList nodeList = serviceXml.getElementsByTagName("relationship")
344 for (int x = 0; x < nodeList.getLength(); x++) {
345 Node node = nodeList.item(x)
346 if (node.getNodeType() == Node.ELEMENT_NODE) {
347 Element eElement = (Element) node
348 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent()
349 if(e.equals("generic-vnf") || e.equals("l3-network")){
350 utils.log("DEBUG", "ServiceInstance still has relationship(s) to generic-vnfs or l3-networks", isDebugEnabled)
351 execution.setVariable("siInUse", true)
352 //there are relationship dependencies to this Service Instance
353 msg = " Stopped deleting Service Instance, it has dependencies. Service instance id: " + serviceInstanceId
354 utils.log("DEBUG", msg, isDebugEnabled)
355 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
357 utils.log("DEBUG", "Relationship NOT related to OpenStack", isDebugEnabled)
364 boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
365 if(succInAAI != true){
366 utils.log("DEBUG","Error getting Service-instance from AAI", + serviceInstanceId, isDebugEnabled)
367 WorkflowException workflowException = execution.getVariable("WorkflowException")
368 utils.logAudit("workflowException: " + workflowException)
369 if(workflowException != null){
370 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
374 msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
375 utils.log("DEBUG", msg, isDebugEnabled)
376 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
379 utils.log("DEBUG","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)
381 } catch (BpmnError e) {
383 } catch (Exception ex) {
384 msg = "Exception in DoDeleteServiceInstance.postProcessAAIGET. " + ex.getMessage()
385 utils.log("DEBUG", msg, isDebugEnabled)
386 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
388 utils.log("DEBUG"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
391 public void postProcessAAIDEL(Execution execution) {
392 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
393 utils.log("DEBUG"," ***** postProcessAAIDEL ***** ", isDebugEnabled)
396 String serviceInstanceId = execution.getVariable("serviceInstanceId")
397 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
398 if(succInAAI != true){
399 msg = "Error deleting Service-instance in AAI" + serviceInstanceId
400 utils.log("DEBUG", msg, isDebugEnabled)
401 WorkflowException workflowException = execution.getVariable("WorkflowException")
402 utils.logAudit("workflowException: " + workflowException)
403 if(workflowException != null){
404 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
408 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
411 } catch (BpmnError e) {
413 } catch (Exception ex) {
414 msg = "Exception in DoDeleteServiceInstance.postProcessAAIDEL. " + ex.getMessage()
415 utils.log("DEBUG", msg, isDebugEnabled)
416 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
418 utils.log("DEBUG"," *** Exit postProcessAAIDEL *** ", isDebugEnabled)