[MSO-8] Second step of the rebase for MSO
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteServiceInstance.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * OPENECOMP - MSO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
21 \r
22 import static org.apache.commons.lang3.StringUtils.*;\r
23 import groovy.xml.XmlUtil\r
24 import groovy.json.*\r
25 \r
26 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
27 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
28 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
29 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
30 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
31 import org.openecomp.mso.bpmn.core.WorkflowException\r
32 import org.openecomp.mso.rest.APIResponse;\r
33 import org.openecomp.mso.rest.RESTClient\r
34 import org.openecomp.mso.rest.RESTConfig\r
35 \r
36 import java.util.UUID;\r
37 import javax.xml.parsers.DocumentBuilder\r
38 import javax.xml.parsers.DocumentBuilderFactory\r
39 \r
40 import org.camunda.bpm.engine.delegate.BpmnError\r
41 import org.camunda.bpm.engine.runtime.Execution\r
42 import org.json.JSONObject;\r
43 import org.apache.commons.lang3.*\r
44 import org.apache.commons.codec.binary.Base64;\r
45 import org.springframework.web.util.UriUtils;\r
46 import org.w3c.dom.Document\r
47 import org.w3c.dom.Element\r
48 import org.w3c.dom.Node\r
49 import org.w3c.dom.NodeList\r
50 import org.xml.sax.InputSource\r
51 \r
52 /**\r
53  * This groovy class supports the <class>DoDeleteServiceInstance.bpmn</class> process.\r
54  * \r
55  * Inputs:\r
56  * @param - msoRequestId\r
57  * @param - globalSubscriberId - O\r
58  * @param - subscriptionServiceType - O\r
59  * @param - serviceInstanceId\r
60  * @param - serviceInstanceName - O\r
61  * @param - serviceModelInfo - O\r
62  * @param - productFamilyId\r
63  * @param - sdncVersion \r
64  * @param - failNotFound - TODO\r
65  * @param - serviceInputParams - TODO\r
66  *\r
67  * Outputs:\r
68  * @param - WorkflowException\r
69  * \r
70  * Rollback - Deferred\r
71  */\r
72 public class DoDeleteServiceInstance extends AbstractServiceTaskProcessor {\r
73 \r
74         String Prefix="DDELSI_"\r
75         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
76         JsonUtils jsonUtil = new JsonUtils()\r
77         VidUtils vidUtils = new VidUtils()\r
78 \r
79         public void preProcessRequest (Execution execution) {\r
80                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
81                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)\r
82                 String msg = ""\r
83 \r
84                 try {\r
85                         String requestId = execution.getVariable("msoRequestId")\r
86                         execution.setVariable("prefix",Prefix)\r
87 \r
88                         //Inputs\r
89                         //requestDetails.subscriberInfo. for AAI GET & PUT & SDNC assignToplology\r
90                         String globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId\r
91                         if (globalSubscriberId == null)\r
92                         {\r
93                                 execution.setVariable("globalSubscriberId", "")\r
94                         }\r
95 \r
96                         //requestDetails.requestParameters. for AAI PUT & SDNC assignTopology\r
97                         String subscriptionServiceType = execution.getVariable("subscriptionServiceType")\r
98                         if (subscriptionServiceType == null)\r
99                         {\r
100                                 execution.setVariable("subscriptionServiceType", "")\r
101                         }\r
102 \r
103                         //Generated in parent for AAI PUT\r
104                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
105                         if (isBlank(serviceInstanceId)){\r
106                                 msg = "Input serviceInstanceId is null"\r
107                                 utils.log("DEBUG", msg, isDebugEnabled)\r
108                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
109                         }\r
110 \r
111                         String sdncCallbackUrl = execution.getVariable('URN_mso_workflow_sdncadapter_callback')\r
112                         if (isBlank(sdncCallbackUrl)) {\r
113                                 msg = "URN_mso_workflow_sdncadapter_callback is null"\r
114                                 utils.log("DEBUG", msg, isDebugEnabled)\r
115                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
116                         }\r
117                         execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)\r
118                         utils.log("DEBUG","SDNC Callback URL: " + sdncCallbackUrl, isDebugEnabled)\r
119 \r
120                 } catch (BpmnError e) {\r
121                         throw e;\r
122                 } catch (Exception ex){\r
123                         msg = "Exception in preProcessRequest " + ex.getMessage()\r
124                         utils.log("DEBUG", msg, isDebugEnabled)\r
125                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
126                 }\r
127                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
128         }\r
129 \r
130         public void preProcessSDNCDelete (Execution execution) {\r
131                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
132                 utils.log("DEBUG"," ***** preProcessSDNCDelete *****", isDebugEnabled)\r
133                 String msg = ""\r
134 \r
135                 try {\r
136                         /*\r
137                          String uuid = execution.getVariable('testReqId') // for junits\r
138                          if(uuid==null){\r
139                          uuid = execution.getVariable("msoRequestId") + "-" +   System.currentTimeMillis()\r
140                          }\r
141                          */\r
142                         def serviceInstanceId = execution.getVariable("serviceInstanceId")\r
143                         def serviceInstanceName = execution.getVariable("serviceInstanceName")\r
144                         def callbackURL = execution.getVariable("sdncCallbackUrl")\r
145                         def requestId = execution.getVariable("msoRequestId")\r
146                         def serviceId = execution.getVariable("productFamilyId")\r
147                         def subscriptionServiceType = execution.getVariable("subscriptionServiceType")\r
148                         def globalSubscriberId = execution.getVariable("globalSubscriberId") //globalCustomerId\r
149 \r
150                         String serviceModelInfo = execution.getVariable("serviceModelInfo")\r
151                         def modelInvariantId = ""\r
152                         def modelVersion = ""\r
153                         def modelUUId = ""\r
154                         def modelName = ""\r
155                         if (!isBlank(serviceModelInfo))\r
156                         {\r
157                                 modelInvariantId = jsonUtil.getJsonValue(serviceModelInfo, "modelInvariantId")\r
158                                 modelVersion = jsonUtil.getJsonValue(serviceModelInfo, "modelVersion")\r
159                                 modelUUId = jsonUtil.getJsonValue(serviceModelInfo, "modelVersionId")\r
160                                 modelName = jsonUtil.getJsonValue(serviceModelInfo, "modelName")\r
161                                 \r
162                                 if (modelInvariantId == null) {\r
163                                         modelInvariantId = ""\r
164                                 }\r
165                                 if (modelVersion == null) {\r
166                                         modelVersion = ""\r
167                                 }\r
168                                 if (modelUUId == null) {\r
169                                         modelUUId = ""\r
170                                 }\r
171                                 if (modelName == null) {\r
172                                         modelName = ""\r
173                                 }\r
174                         }\r
175                         if (serviceInstanceName == null) {\r
176                                 serviceInstanceName = ""\r
177                         }\r
178                         if (serviceId == null) {\r
179                                 serviceId = ""\r
180                         }\r
181                         \r
182                         def sdncRequestId = UUID.randomUUID().toString()\r
183 \r
184                         String sdncDelete =\r
185                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"\r
186                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"\r
187                                                                                                         xmlns:sdncadapter="http://org.openecomp/workflow/sdnc/adapter/schema/v1">\r
188                                    <sdncadapter:RequestHeader>\r
189                                                         <sdncadapter:RequestId>${sdncRequestId}</sdncadapter:RequestId>\r
190                                                         <sdncadapter:SvcInstanceId>${serviceInstanceId}</sdncadapter:SvcInstanceId>\r
191                                                         <sdncadapter:SvcAction>delete</sdncadapter:SvcAction>\r
192                                                         <sdncadapter:SvcOperation>service-topology-operation</sdncadapter:SvcOperation>\r
193                                                         <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>\r
194                                         </sdncadapter:RequestHeader>\r
195                                 <sdncadapterworkflow:SDNCRequestData>\r
196                                         <request-information>\r
197                                                 <request-id>${requestId}</request-id>\r
198                                                 <source>MSO</source>\r
199                                                 <notification-url/>\r
200                                                 <order-number/>\r
201                                                 <order-version/>\r
202                                                 <request-action>DeleteServiceInstance</request-action>\r
203                                         </request-information>\r
204                                         <service-information>\r
205                                                 <service-id>${serviceId}</service-id>\r
206                                                 <subscription-service-type>${subscriptionServiceType}</subscription-service-type>\r
207                                                 <ecomp-model-information>\r
208                                                  <model-invariant-uuid>${modelInvariantId}</model-invariant-uuid>\r
209                                                  <model-uuid>${modelUUId}</model-uuid>\r
210                                                  <model-version>${modelVersion}</model-version>\r
211                                                  <model-name>${modelName}</model-name>\r
212                                             </ecomp-model-information>\r
213                                                 <service-instance-id>${serviceInstanceId}</service-instance-id>\r
214                                                 <subscriber-name/>\r
215                                                 <global-customer-id>${globalSubscriberId}</global-customer-id>\r
216                                         </service-information>\r
217                                         <service-request-input>\r
218                                                 <service-instance-name>${serviceInstanceName}</service-instance-name>\r
219                                         </service-request-input>\r
220                                 </sdncadapterworkflow:SDNCRequestData>\r
221                                 </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""\r
222 \r
223                         utils.log("DEBUG","sdncDelete:\n" + sdncDelete, isDebugEnabled)\r
224                         sdncDelete = utils.formatXml(sdncDelete)\r
225                         execution.setVariable("sdncDelete", sdncDelete)\r
226                         utils.logAudit("sdncDelete:  " + sdncDelete)\r
227 \r
228 \r
229                 } catch (BpmnError e) {\r
230                         throw e;\r
231                 } catch(Exception ex) {\r
232                         msg = "Exception in preProcessSDNCDelete. " + ex.getMessage()\r
233                         utils.log("DEBUG", msg, isDebugEnabled)\r
234                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception Occured in preProcessSDNCDelete.\n" + ex.getMessage())\r
235                 }\r
236                 utils.log("DEBUG"," *****Exit preProcessSDNCDelete *****", isDebugEnabled)\r
237         }\r
238 \r
239         public void postProcessSDNCDelete(Execution execution) {\r
240 \r
241                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
242                 utils.log("DEBUG"," ***** postProcessSDNCDelete ***** ", isDebugEnabled)\r
243                 String msg = ""\r
244 \r
245                 try {\r
246                         WorkflowException workflowException = execution.getVariable("WorkflowException")\r
247                         utils.logAudit("workflowException: " + workflowException)\r
248 \r
249                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")\r
250                         String response = execution.getVariable("sdncAdapterResponse")\r
251                         utils.logAudit("SDNCResponse: " + response)\r
252 \r
253                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)\r
254                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)\r
255 \r
256                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){\r
257                                 utils.log("DEBUG","Good response from SDNC Adapter for service-instance  topology assign: \n" + response, isDebugEnabled)\r
258 \r
259                         }else{\r
260                                 msg = "Bad Response from SDNC Adapter for service-instance delete"\r
261                                 utils.log("DEBUG", msg, isDebugEnabled)\r
262                                 exceptionUtil.buildAndThrowWorkflowException(execution, 3500, msg)\r
263                         }\r
264                 } catch (BpmnError e) {\r
265                         throw e;\r
266                 } catch(Exception ex) {\r
267                         msg = "Exception in postProcessSDNCDelete. " + ex.getMessage()\r
268                         utils.log("DEBUG", msg, isDebugEnabled)\r
269                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, "Exception Occured in preProcessSDNCDelete.\n" + ex.getMessage())\r
270                 }\r
271                 utils.log("DEBUG"," *** Exit postProcessSDNCDelete *** ", isDebugEnabled)\r
272         }\r
273 \r
274         public void postProcessAAIGET(Execution execution) {\r
275                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
276                 utils.log("DEBUG"," ***** postProcessAAIGET ***** ", isDebugEnabled)\r
277                 String msg = ""\r
278 \r
279                 try {\r
280 \r
281                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
282                         boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")\r
283 \r
284                         if(foundInAAI == true){\r
285                                 utils.log("DEBUG","Found Service-instance in AAI", isDebugEnabled)\r
286 \r
287                                 //Extract GlobalSubscriberId\r
288                                 String siRelatedLink = execution.getVariable("GENGS_siResourceLink")\r
289                                 if (isBlank(siRelatedLink))\r
290                                 {\r
291                                         msg = "Could not retrive ServiceInstance data from AAI to delete id:" + serviceInstanceId\r
292                                         utils.log("DEBUG", msg, isDebugEnabled)\r
293                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
294                                 }\r
295                                 else\r
296                                 {\r
297                                         utils.log("DEBUG","Found Service-instance in AAI. link: " + siRelatedLink, isDebugEnabled)\r
298                                         String  globalSubscriberId = execution.getVariable("globalSubscriberId")\r
299                                         if(isBlank(globalSubscriberId)){\r
300                                                 int custStart = siRelatedLink.indexOf("customer/")\r
301                                                 int custEnd = siRelatedLink.indexOf("/service-subscriptions")\r
302                                                 globalSubscriberId = siRelatedLink.substring(custStart + 9, custEnd)\r
303                                                 execution.setVariable("globalSubscriberId", globalSubscriberId)\r
304                                         }\r
305 \r
306                                         //Extract Service Type if not provided on request\r
307                                         String serviceType = execution.getVariable("subscriptionServiceType")\r
308                                         if(isBlank(serviceType)){\r
309                                                 int serviceStart = siRelatedLink.indexOf("service-subscription/")\r
310                                                 int serviceEnd = siRelatedLink.indexOf("/service-instances/")\r
311                                                 String serviceTypeEncoded = siRelatedLink.substring(serviceStart + 21, serviceEnd)\r
312                                                 serviceType = UriUtils.decode(serviceTypeEncoded, "UTF-8")\r
313                                                 execution.setVariable("subscriptionServiceType", serviceType)\r
314                                         }\r
315 \r
316                                         if (isBlank(globalSubscriberId) || isBlank(serviceType))\r
317                                         {\r
318                                                 msg = "Could not retrive global-customer-id & service-type from AAI to delete id:" + serviceInstanceId\r
319                                                 utils.log("DEBUG", msg, isDebugEnabled)\r
320                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
321                                         }\r
322                                 }\r
323 \r
324                                 String siData = execution.getVariable("GENGS_service")\r
325                                 utils.log("DEBUG", "SI Data", isDebugEnabled)\r
326                                 if (isBlank(siData))\r
327                                 {\r
328                                         msg = "Could not retrive ServiceInstance data from AAI to delete id:" + serviceInstanceId\r
329                                         utils.log("DEBUG", msg, isDebugEnabled)\r
330                                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)\r
331                                 }\r
332                                 else\r
333                                 {\r
334                                         utils.log("DEBUG", "SI Data" + siData, isDebugEnabled)\r
335                                         //Confirm there are no related service instances (vnf/network or volume)\r
336                                         if (utils.nodeExists(siData, "relationship-list")) {\r
337                                                 utils.log("DEBUG", "SI Data relationship-list exists:", isDebugEnabled)\r
338                                                 InputSource source = new InputSource(new StringReader(siData));\r
339                                                 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();\r
340                                                 DocumentBuilder docBuilder = docFactory.newDocumentBuilder()\r
341                                                 Document serviceXml = docBuilder.parse(source)\r
342 \r
343                                                 NodeList nodeList = serviceXml.getElementsByTagName("relationship")\r
344                                                 for (int x = 0; x < nodeList.getLength(); x++) {\r
345                                                         Node node = nodeList.item(x)\r
346                                                         if (node.getNodeType() == Node.ELEMENT_NODE) {\r
347                                                                 Element eElement = (Element) node\r
348                                                                 def e = eElement.getElementsByTagName("related-to").item(0).getTextContent()\r
349                                                                 if(e.equals("generic-vnf") || e.equals("l3-network")){\r
350                                                                         utils.log("DEBUG", "ServiceInstance still has relationship(s) to generic-vnfs or l3-networks", isDebugEnabled)\r
351                                                                         execution.setVariable("siInUse", true)\r
352                                                                         //there are relationship dependencies to this Service Instance\r
353                                                                         msg = " Stopped deleting Service Instance, it has dependencies. Service instance id: " + serviceInstanceId\r
354                                                                         utils.log("DEBUG", msg, isDebugEnabled)\r
355                                                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)\r
356                                                                 }else{\r
357                                                                         utils.log("DEBUG", "Relationship NOT related to OpenStack", isDebugEnabled)\r
358                                                                 }\r
359                                                         }\r
360                                                 }\r
361                                         }\r
362                                 }\r
363                         }else{\r
364                                 boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")\r
365                                 if(succInAAI != true){\r
366                                         utils.log("DEBUG","Error getting Service-instance from AAI", + serviceInstanceId, isDebugEnabled)\r
367                                         WorkflowException workflowException = execution.getVariable("WorkflowException")\r
368                                         utils.logAudit("workflowException: " + workflowException)\r
369                                         if(workflowException != null){\r
370                                                 exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())\r
371                                         }\r
372                                         else\r
373                                         {\r
374                                                 msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI\r
375                                                 utils.log("DEBUG", msg, isDebugEnabled)\r
376                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)\r
377                                         }\r
378                                 }\r
379                                 utils.log("DEBUG","Service-instance NOT found in AAI. Silent Success", isDebugEnabled)\r
380                         }\r
381                 } catch (BpmnError e) {\r
382                         throw e;\r
383                 } catch (Exception ex) {\r
384                         msg = "Exception in DoDeleteServiceInstance.postProcessAAIGET. " + ex.getMessage()\r
385                         utils.log("DEBUG", msg, isDebugEnabled)\r
386                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
387                 }\r
388                 utils.log("DEBUG"," *** Exit postProcessAAIGET *** ", isDebugEnabled)\r
389         }\r
390         \r
391         public void postProcessAAIDEL(Execution execution) {\r
392                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
393                 utils.log("DEBUG"," ***** postProcessAAIDEL ***** ", isDebugEnabled)\r
394                 String msg = ""\r
395                 try {\r
396                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
397                         boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")\r
398                         if(succInAAI != true){\r
399                                 msg = "Error deleting Service-instance in AAI" + serviceInstanceId\r
400                                 utils.log("DEBUG", msg, isDebugEnabled)\r
401                                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
402                                 utils.logAudit("workflowException: " + workflowException)\r
403                                 if(workflowException != null){\r
404                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())\r
405                                 }\r
406                                 else\r
407                                 {\r
408                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)\r
409                                 }\r
410                         }\r
411                 } catch (BpmnError e) {\r
412                         throw e;\r
413                 } catch (Exception ex) {\r
414                         msg = "Exception in DoDeleteServiceInstance.postProcessAAIDEL. " + ex.getMessage()\r
415                         utils.log("DEBUG", msg, isDebugEnabled)\r
416                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
417                 }\r
418                 utils.log("DEBUG"," *** Exit postProcessAAIDEL *** ", isDebugEnabled)\r
419         }\r
420 }\r