32197c1bc2c13c49f988e31b1e00d3ed07d53ec4
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteServiceInstance.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 package org.openecomp.mso.bpmn.infrastructure.scripts;
21
22 import static org.apache.commons.lang3.StringUtils.*;
23 import groovy.xml.XmlUtil
24 import groovy.json.*
25
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
35
36 import java.util.UUID;
37 import javax.xml.parsers.DocumentBuilder
38 import javax.xml.parsers.DocumentBuilderFactory
39
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
51
52 /**
53  * This groovy class supports the <class>DoDeleteServiceInstance.bpmn</class> process.
54  * 
55  * Inputs:
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
66  *
67  * Outputs:
68  * @param - WorkflowException
69  * 
70  * Rollback - Deferred
71  */
72 public class DoDeleteServiceInstance extends AbstractServiceTaskProcessor {
73
74         String Prefix="DDELSI_"
75         ExceptionUtil exceptionUtil = new ExceptionUtil()
76         JsonUtils jsonUtil = new JsonUtils()
77         VidUtils vidUtils = new VidUtils()
78
79         public void preProcessRequest (Execution execution) {
80                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
81                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)
82                 String msg = ""
83
84                 try {
85                         String requestId = execution.getVariable("msoRequestId")
86                         execution.setVariable("prefix",Prefix)
87
88                         //Inputs
89                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology
90                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId
91                         if (globalSubscriberId == null)
92                         {
93                                 execution.setVariable("globalSubscriberId", "")
94                         }
95
96                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology
97                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")
98                         if (subscriptionServiceType == null)
99                         {
100                                 execution.setVariable("subscriptionServiceType", "")
101                         }
102
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)
109                         }
110
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)
116                         }
117                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)
118                         utils.log("DEBUG","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)
119
120                 } catch (BpmnError e) {
121                         throw e;
122                 } catch (Exception ex){
123                         msg = "Exception in preProcessRequest " + ex.getMessage()
124                         utils.log("DEBUG", msg, isDebugEnabled)
125                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
126                 }
127                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
128         }
129
130         public void preProcessSDNCDelete (Execution execution) {
131                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
132                 utils.log("DEBUG"," ***** preProcessSDNCDelete *****", isDebugEnabled)
133                 String msg = ""
134
135                 try {
136                         /*
137                          String uuid = execution.getVariable('testReqId') // for junits
138                          if(uuid==null){
139                          uuid = execution.getVariable("msoRequestId") + "-" +   System.currentTimeMillis()
140                          }
141                          */
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
149
150                         String serviceModelInfo = execution.getVariable("serviceModelInfo")
151                         def modelInvariantId = ""
152                         def modelVersion = ""
153                         def modelUUId = ""
154                         def modelName = ""
155                         if (!isBlank(serviceModelInfo))
156                         {
157                                 modelInvariantId = jsonUtil.getJsonValue(serviceModelInfo, "modelInvariantId")
158                                 modelVersion = jsonUtil.getJsonValue(serviceModelInfo, "modelVersion")
159                                 modelUUId = jsonUtil.getJsonValue(serviceModelInfo, "modelVersionId")
160                                 modelName = jsonUtil.getJsonValue(serviceModelInfo, "modelName")
161                                 
162                                 if (modelInvariantId == null) {
163                                         modelInvariantId = ""
164                                 }
165                                 if (modelVersion == null) {
166                                         modelVersion = ""
167                                 }
168                                 if (modelUUId == null) {
169                                         modelUUId = ""
170                                 }
171                                 if (modelName == null) {
172                                         modelName = ""
173                                 }
174                         }
175                         if (serviceInstanceName == null) {
176                                 serviceInstanceName = ""
177                         }
178                         if (serviceId == null) {
179                                 serviceId = ""
180                         }
181                         
182                         def sdncRequestId = UUID.randomUUID().toString()
183
184                         String sdncDelete =
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>
198                                                 <source>MSO</source>
199                                                 <notification-url/>
200                                                 <order-number/>
201                                                 <order-version/>
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>
214                                                 <subscriber-name/>
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>"""
222
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)
230
231                 } catch (BpmnError e) {
232                         throw 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())
237                 }
238                 utils.log("DEBUG"," *****Exit preProcessSDNCDelete *****", isDebugEnabled)
239         }
240
241         public void postProcessSDNCDelete(Execution execution, String response, String method) {
242
243                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
244                 utils.log("DEBUG"," ***** postProcessSDNC " + method + " *****", isDebugEnabled)
245                 String msg = ""
246
247                 try {
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)
252                         
253                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
254                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
255
256                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
257                                 utils.log("DEBUG","Good response from SDNC Adapter for service-instance " + method + "response:\n" + response, isDebugEnabled)
258
259                         }else{
260                                 msg = "Bad Response from SDNC Adapter for service-instance " + method
261                                 utils.log("DEBUG", msg, isDebugEnabled)
262                                 exceptionUtil.buildAndThrowWorkflowException(execution, 3500, msg)
263                         }
264                 } catch (BpmnError e) {
265                         throw 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)
270                 }
271                 utils.log("DEBUG"," *** Exit postProcessSDNC " + method + " ***", isDebugEnabled)
272         }
273
274         public void postProcessAAIGET(Execution execution) {
275                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
276                 utils.log("DEBUG"," ***** postProcessAAIGET ***** ", isDebugEnabled)
277                 String msg = ""
278
279                 try {
280
281                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
282                         boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
283
284                         if(foundInAAI == true){
285                                 utils.log("DEBUG","Found Service-instance in AAI", isDebugEnabled)
286
287                                 //Extract GlobalSubscriberId
288                                 String siRelatedLink = execution.getVariable("GENGS_siResourceLink")
289                                 if (isBlank(siRelatedLink))
290                                 {
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)
294                                 }
295                                 else
296                                 {
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)
304                                         }
305
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)
314                                         }
315
316                                         if (isBlank(globalSubscriberId) || isBlank(serviceType))
317                                         {
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)
321                                         }
322                                 }
323
324                                 String siData = execution.getVariable("GENGS_service")
325                                 utils.log("DEBUG", "SI Data", isDebugEnabled)
326                                 if (isBlank(siData))
327                                 {
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)
331                                 }
332                                 else
333                                 {
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)
342
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)
356                                                                 }else{
357                                                                         utils.log("DEBUG", "Relationship NOT related to OpenStack", isDebugEnabled)
358                                                                 }
359                                                         }
360                                                 }
361                                         }
362                                 }
363                         }else{
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())
371                                         }
372                                         else
373                                         {
374                                                 msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
375                                                 utils.log("DEBUG", msg, isDebugEnabled)
376                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
377                                         }
378                                 }
379                                 utils.log("DEBUG","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)
380                         }
381                 } catch (BpmnError e) {
382                         throw 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)
387                 }
388                 utils.log("DEBUG"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
389         }
390         
391         public void postProcessAAIDEL(Execution execution) {
392                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
393                 utils.log("DEBUG"," ***** postProcessAAIDEL ***** ", isDebugEnabled)
394                 String msg = ""
395                 try {
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())
405                                 }
406                                 else
407                                 {
408                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
409                                 }
410                         }
411                 } catch (BpmnError e) {
412                         throw 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)
417                 }
418                 utils.log("DEBUG"," *** Exit postProcessAAIDEL *** ", isDebugEnabled)
419         }
420 }