1710 Rebase - Second Attempt
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteVfModuleFromVnf.groovy
1 package org.openecomp.mso.bpmn.infrastructure.scripts\r
2 \r
3 import javax.xml.parsers.DocumentBuilder\r
4 import javax.xml.parsers.DocumentBuilderFactory\r
5 \r
6 import org.camunda.bpm.engine.delegate.BpmnError\r
7 import org.camunda.bpm.engine.runtime.Execution\r
8 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
9 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
10 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
11 import org.openecomp.mso.bpmn.common.scripts.VfModule\r
12 import org.openecomp.mso.bpmn.common.scripts.VfModuleBase\r
13 import org.openecomp.mso.bpmn.core.WorkflowException\r
14 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
15 import org.openecomp.mso.rest.APIResponse\r
16 import org.openecomp.mso.rest.RESTClient\r
17 import org.openecomp.mso.rest.RESTConfig\r
18 import org.springframework.web.util.UriUtils\r
19 import org.w3c.dom.Document\r
20 import org.w3c.dom.Element\r
21 import org.xml.sax.InputSource\r
22 \r
23 public class DoDeleteVfModuleFromVnf extends VfModuleBase {\r
24 \r
25         def Prefix="DDVFMV_"\r
26         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
27         JsonUtils jsonUtil = new JsonUtils()\r
28 \r
29         public void initProcessVariables(Execution execution) {\r
30                 execution.setVariable("prefix",Prefix)\r
31                 execution.setVariable("DDVFMV_contrailNetworkPolicyFqdnList", null)\r
32         }\r
33 \r
34         // parse the incoming request\r
35         public void preProcessRequest(Execution execution) {\r
36                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
37                 initProcessVariables(execution)\r
38 \r
39                 try {\r
40                         \r
41                                 // Building Block-type request\r
42 \r
43                                 // Set mso-request-id to request-id for VNF Adapter interface\r
44                                 String requestId = execution.getVariable("msoRequestId")\r
45                                 execution.setVariable("mso-request-id", requestId)\r
46                                 execution.setVariable("requestId", requestId)\r
47                                 utils.log("DEBUG", "msoRequestId: " + requestId, isDebugEnabled)\r
48                                 String tenantId = execution.getVariable("tenantId")\r
49                                 utils.log("DEBUG", "tenantId: " + tenantId, isDebugEnabled)                             \r
50                                 String cloudSiteId = execution.getVariable("lcpCloudRegionId")\r
51                                 execution.setVariable("cloudSiteId", cloudSiteId)\r
52                                 utils.log("DEBUG", "cloudSiteId: " + cloudSiteId, isDebugEnabled)\r
53                                 // Source is HARDCODED\r
54                                 String source = "VID"\r
55                                 execution.setVariable("source", source)\r
56                                 // isVidRequest is hardcoded to "true"\r
57                                 execution.setVariable("isVidRequest", "true")\r
58                                 // SrvInstId is hardcoded to empty\r
59                                 execution.setVariable("srvInstId", "")\r
60                                 // ServiceId is hardcoded to empty\r
61                                 execution.setVariable("serviceId", "")\r
62                                 String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
63                                 utils.log("DEBUG", "serviceInstanceId: " + serviceInstanceId, isDebugEnabled)\r
64                                 String vnfId = execution.getVariable("vnfId")\r
65                                 utils.log("DEBUG", "vnfId: " + vnfId, isDebugEnabled)\r
66                                 String vfModuleId = execution.getVariable("vfModuleId")\r
67                                 utils.log("DEBUG", "vfModuleId: " + vfModuleId, isDebugEnabled)\r
68                                 if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {\r
69                                         execution.setVariable(Prefix + "serviceInstanceIdToSdnc", vfModuleId)\r
70                                 }\r
71                                 else {\r
72                                         execution.setVariable(Prefix + "serviceInstanceIdToSdnc", serviceInstanceId)\r
73                                 }                               \r
74                                 \r
75                                 String sdncVersion = execution.getVariable("sdncVersion")\r
76                                 if (sdncVersion == null) {\r
77                                         sdncVersion = "1707"\r
78                                 }\r
79                                 execution.setVariable(Prefix + "sdncVersion", sdncVersion)\r
80                                 utils.log("DEBUG", "Incoming Sdnc Version is: " + sdncVersion, isDebugEnabled)                          \r
81                                 \r
82                                 String sdncCallbackUrl = (String) execution.getVariable('URN_mso_workflow_sdncadapter_callback')\r
83                                 if (sdncCallbackUrl == null || sdncCallbackUrl.trim().isEmpty()) {\r
84                                         def msg = 'Required variable \'URN_mso_workflow_sdncadapter_callback\' is missing'\r
85                                         logError(msg)\r
86                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, msg)\r
87                                 }\r
88                                 execution.setVariable("sdncCallbackUrl", sdncCallbackUrl)\r
89                                 utils.logAudit("SDNC Callback URL: " + sdncCallbackUrl)\r
90                                 utils.log("DEBUG:", "SDNC Callback URL is: " + sdncCallbackUrl, isDebugEnabled)\r
91 \r
92                         \r
93                         \r
94                 }catch(BpmnError b){\r
95                         throw b\r
96                 }catch(Exception e){\r
97                         utils.log("DEBUG", "Exception is: " + e.getMessage(), isDebugEnabled)\r
98                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, "Internal Error encountered in PreProcess method!")\r
99                 }\r
100         }\r
101         \r
102         public void queryAAIForVfModule(Execution execution) {\r
103                 def method = getClass().getSimpleName() + '.queryAAIForVfModule(' +\r
104                         'execution=' + execution.getId() +\r
105                         ')'\r
106                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
107                 logDebug('Entered ' + method, isDebugLogEnabled)\r
108 \r
109                 try {\r
110                         def vnfId = execution.getVariable('vnfId')\r
111 \r
112                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
113                         def aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)\r
114                         logDebug('AAI URI is: ' + aai_uri, isDebugLogEnabled)\r
115 \r
116                         String endPoint = execution.getVariable("URN_aai_endpoint") + "${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"\r
117 \r
118                         utils.logAudit("DoDeleteVfModuleFromVnf: AAI endPoint  : " + endPoint)\r
119 \r
120                         try {\r
121                                 RESTConfig config = new RESTConfig(endPoint);\r
122                                 def responseData = ''\r
123                                 def aaiRequestId = UUID.randomUUID().toString()\r
124                                 RESTClient client = new RESTClient(config).\r
125                                         addHeader('X-TransactionId', aaiRequestId).\r
126                                         addHeader('X-FromAppId', 'MSO').\r
127                                         addHeader('Content-Type', 'application/xml').\r
128                                         addHeader('Accept','application/xml');\r
129                                 logDebug('sending GET to AAI endpoint \'' + endPoint + '\'', isDebugLogEnabled)\r
130                                 APIResponse response = client.httpGet()\r
131                                 utils.logAudit("DoDeleteVfModuleFromVnf: - invoking httpGet to AAI")\r
132 \r
133                                 responseData = response.getResponseBodyAsString()\r
134                                 execution.setVariable('DDVMFV_getVnfResponseCode', response.getStatusCode())\r
135                                 execution.setVariable('DDVMFV_getVnfResponse', responseData)\r
136 \r
137                                 utils.logAudit("DoDeleteVfModuleFromVnf: AAI Response : " + responseData)\r
138                                 utils.logAudit("DoDeleteVfModuleFromVnf: AAI ResponseCode : " + response.getStatusCode())\r
139 \r
140                                 logDebug('Response code:' + response.getStatusCode(), isDebugLogEnabled)\r
141                                 logDebug('Response:' + System.lineSeparator() + responseData, isDebugLogEnabled)                        \r
142                                 \r
143                         } catch (Exception ex) {\r
144                                 ex.printStackTrace()\r
145                                 logDebug('Exception occurred while executing AAI GET:' + ex.getMessage(), isDebugLogEnabled)\r
146                                 execution.setVariable('DDVMFV_getVnfResponseCode', 500)\r
147                                 execution.setVariable('DDVFMV_getVnfResponse', 'AAI GET Failed:' + ex.getMessage())\r
148                         }\r
149                         logDebug('Exited ' + method, isDebugLogEnabled)\r
150                 } catch (BpmnError e) {\r
151                         throw e;\r
152                 } catch (Exception e) {\r
153                         logError('Caught exception in ' + method, e)\r
154                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in queryAAIForVfModule(): ' + e.getMessage())\r
155                 }\r
156         }\r
157         \r
158         /**\r
159          * Validate the VF Module.  That is, confirm that a VF Module with the input VF Module ID\r
160          * exists in the retrieved Generic VNF.  Then, check to make sure that if that VF Module\r
161          * is the base VF Module and it's not the only VF Module for this Generic VNF, that we're not\r
162          * attempting to delete it.\r
163          *\r
164          * @param execution The flow's execution instance.\r
165          */\r
166         public void validateVfModule(Execution execution) {\r
167                 def method = getClass().getSimpleName() + '.validateVfModule(' +\r
168                         'execution=' + execution.getId() +\r
169                         ')'\r
170                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
171                 logDebug('Entered ' + method, isDebugLogEnabled)\r
172 \r
173                 try {\r
174                         def genericVnf = execution.getVariable('DDVMFV_getVnfResponse')\r
175                         def vnfId = execution.getVariable('_vnfId')\r
176                         def vfModuleId = execution.getVariable('vfModuleId')                    \r
177                         def VfModule vfModule = findVfModule(genericVnf, vfModuleId)\r
178                         if (vfModule == null) {\r
179                                 def String msg = 'VF Module \'' + vfModuleId + '\' does not exist in Generic VNF \'' + vnfId + '\''\r
180                                 logDebug(msg, isDebugLogEnabled)\r
181                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, msg)\r
182                         } else {\r
183                                 \r
184                                 if (isDebugLogEnabled) {\r
185                                         logDebug('VF Module \'' + vfModuleId + '\': isBaseVfModule=' + vfModule.isBaseVfModule() +\r
186                                                 ', isOnlyVfModule=' + vfModule.isOnlyVfModule(),\r
187                                                 isDebugLogEnabled)\r
188                                 }\r
189                                 if (vfModule.isBaseVfModule() && !vfModule.isOnlyVfModule()) {\r
190                                         def String msg = 'Cannot delete VF Module \'' + vfModuleId +\r
191                                                 '\'since it is the base VF Module and it\'s not the only VF Module in Generic VNF \'' + vnfId + '\''\r
192                                                 logDebug("Received a BAD Response from VNF Adapter for CREATE_VF_MODULE Call.", isDebugLogEnabled)\r
193                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "VNF Adapter Error")\r
194                                 }\r
195                                 \r
196                                 def heatStackId = vfModule.getElementText('heat-stack-id')\r
197                                 execution.setVariable('DDVMFV_heatStackId', heatStackId)\r
198                                 logDebug('VF Module heatStackId retrieved from AAI: ' + heatStackId, isDebugLogEnabled)\r
199                         }\r
200 \r
201                         logDebug('Exited ' + method, isDebugLogEnabled)\r
202                 } catch (BpmnError e) {\r
203                         throw e;\r
204                 } catch (Exception e) {\r
205                         logError('Caught exception in ' + method, e)\r
206                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in validateVfModule(): ' + e.getMessage())\r
207                 }\r
208         }\r
209 \r
210 \r
211         public void preProcessSDNCDeactivateRequest(Execution execution){\r
212                 def isDebugLogEnabled = execution.getVariable("isDebugLogEnabled")\r
213                 execution.setVariable("prefix", Prefix)\r
214                 logDebug(" ======== STARTED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)\r
215                 \r
216                 def serviceInstanceId = execution.getVariable("serviceInstanceId")\r
217         \r
218                 try{\r
219                         //Build SDNC Request\r
220                         \r
221                         String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")\r
222         \r
223                         deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)\r
224                         execution.setVariable("DDVMFV_deactivateSDNCRequest", deactivateSDNCRequest)\r
225                         logDebug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest, isDebugLogEnabled)\r
226                         utils.logAudit("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)\r
227         \r
228                 }catch(Exception e){\r
229                         utils.log("ERROR", "Exception Occured Processing preProcessSDNCDeactivateRequest. Exception is:\n" + e, isDebugLogEnabled)\r
230                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())\r
231                 }\r
232                 logDebug("======== COMPLETED preProcessSDNCDeactivateRequest ======== ", isDebugLogEnabled)\r
233         }\r
234 \r
235         public void preProcessSDNCUnassignRequest(Execution execution) {\r
236                 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +\r
237                         'execution=' + execution.getId() +\r
238                         ')'\r
239                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
240                 logDebug('Entered ' + method, isDebugLogEnabled)\r
241                 execution.setVariable("prefix", Prefix)\r
242                 logDebug(" ======== STARTED preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)\r
243                 try{                    \r
244                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
245         \r
246                         String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")\r
247         \r
248                         execution.setVariable("DDVMFV_unassignSDNCRequest", unassignSDNCRequest)\r
249                         logDebug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest, isDebugLogEnabled)\r
250                         utils.logAudit("Outgoing UnassignSDNCRequest is: \n"  + unassignSDNCRequest)\r
251         \r
252                 }catch(Exception e){\r
253                         log.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e, isDebugLogEnabled)\r
254                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCUnassignRequest Method:\n" + e.getMessage())\r
255                 }\r
256                 logDebug("======== COMPLETED  preProcessSDNCUnassignRequest Process ======== ", isDebugLogEnabled)\r
257         }\r
258 \r
259         public String buildSDNCRequest(Execution execution, String svcInstId, String action){\r
260         \r
261                         String uuid = execution.getVariable('testReqId') // for junits\r
262                         if(uuid==null){\r
263                                 uuid = execution.getVariable("msoRequestId") + "-" +    System.currentTimeMillis()\r
264                         }\r
265                         def callbackURL = execution.getVariable("sdncCallbackUrl")      \r
266                         def requestId = execution.getVariable("msoRequestId")\r
267                         def serviceId = execution.getVariable("serviceId")\r
268                         def serviceInstanceId = execution.getVariable("serviceInstanceId")\r
269                         def vfModuleId = execution.getVariable("vfModuleId")\r
270                         def source = execution.getVariable("source")\r
271                         def vnfId = execution.getVariable("vnfId")\r
272                                 \r
273                         def sdncVersion = execution.getVariable(Prefix + "sdncVersion")\r
274                         \r
275                         String sdncRequest =\r
276                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.openecomp/mso/request/types/v1"\r
277                                                                                                         xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"\r
278                                                                                                         xmlns:sdncadapter="http://org.openecomp.mso/workflow/sdnc/adapter/schema/v1">\r
279            <sdncadapter:RequestHeader>\r
280                                 <sdncadapter:RequestId>${uuid}</sdncadapter:RequestId>\r
281                                 <sdncadapter:SvcInstanceId>${svcInstId}</sdncadapter:SvcInstanceId>\r
282                                 <sdncadapter:SvcAction>${action}</sdncadapter:SvcAction>\r
283                                 <sdncadapter:SvcOperation>vf-module-topology-operation</sdncadapter:SvcOperation>\r
284                                 <sdncadapter:CallbackUrl>${callbackURL}</sdncadapter:CallbackUrl>\r
285                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>\r
286                 </sdncadapter:RequestHeader>\r
287         <sdncadapterworkflow:SDNCRequestData>\r
288                 <request-information>\r
289                         <request-id>${requestId}</request-id>\r
290                         <request-action>DeleteVfModuleInstance</request-action>\r
291                         <source>${source}</source>\r
292                         <notification-url/>\r
293                         <order-number/>\r
294                         <order-version/>\r
295                 </request-information>\r
296                 <service-information>\r
297                         <service-id/>\r
298                         <subscription-service-type/>                    \r
299                         <service-instance-id>${serviceInstanceId}</service-instance-id>\r
300                         <global-customer-id/>\r
301                 </service-information>\r
302                 <vnf-information>\r
303                         <vnf-id>${vnfId}</vnf-id>\r
304                         <vnf-type/>                     \r
305                 </vnf-information>\r
306                 <vf-module-information>\r
307                         <vf-module-id>${vfModuleId}</vf-module-id>\r
308                 </vf-module-information>\r
309                 <vf-module-request-input/>              \r
310         </sdncadapterworkflow:SDNCRequestData>\r
311         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""\r
312         \r
313                 utils.logAudit("sdncRequest:  " + sdncRequest)\r
314                 return sdncRequest\r
315         }\r
316         \r
317         public void validateSDNCResponse(Execution execution, String response, String method){\r
318                 def isDebugLogEnabled=execution.getVariable("isDebugLogEnabled")\r
319                 execution.setVariable("prefix",Prefix)\r
320                 logDebug(" *** STARTED ValidateSDNCResponse Process*** ", isDebugLogEnabled)\r
321         \r
322                 WorkflowException workflowException = execution.getVariable("WorkflowException")\r
323                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")\r
324         \r
325                 utils.logAudit("workflowException: " + workflowException)\r
326         \r
327                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)\r
328                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)\r
329         \r
330                 utils.logAudit("SDNCResponse: " + response)\r
331         \r
332                 String sdncResponse = response\r
333                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){\r
334                         logDebug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse, isDebugLogEnabled)\r
335                 }else{\r
336                         logDebug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.", isDebugLogEnabled)\r
337                         throw new BpmnError("MSOWorkflowException")\r
338                 }\r
339                 logDebug(" *** COMPLETED ValidateSDNCResponse Process*** ", isDebugLogEnabled)\r
340         }\r
341 \r
342 \r
343         // parse the incoming DELETE_VF_MODULE request\r
344         // and formulate the outgoing VnfAdapterDeleteV1 request\r
345         public void prepVNFAdapterRequest(Execution execution) {\r
346                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
347                 def requestId = UUID.randomUUID().toString()\r
348                 def origRequestId = execution.getVariable('requestId')\r
349                 def srvInstId = execution.getVariable("serviceInstanceId")\r
350                 def aicCloudRegion = execution.getVariable("cloudSiteId")\r
351                 def vnfId = execution.getVariable("vnfId")\r
352                 def vfModuleId = execution.getVariable("vfModuleId")\r
353                 def vfModuleStackId = execution.getVariable('DDVMFV_heatStackId')\r
354                 def tenantId = execution.getVariable("tenantId")\r
355                 def messageId = execution.getVariable('requestId') + '-' +\r
356                         System.currentTimeMillis()\r
357                 def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId)\r
358                 def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")\r
359                 if ('true'.equals(useQualifiedHostName)) {\r
360                         notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)\r
361                 }\r
362 \r
363                 String request = """\r
364                         <deleteVfModuleRequest>\r
365                             <cloudSiteId>${aicCloudRegion}</cloudSiteId>\r
366                             <tenantId>${tenantId}</tenantId>\r
367                             <vnfId>${vnfId}</vnfId>\r
368                             <vfModuleId>${vfModuleId}</vfModuleId>\r
369                             <vfModuleStackId>${vfModuleStackId}</vfModuleStackId>\r
370                             <skipAAI>true</skipAAI>\r
371                             <msoRequest>\r
372                                 <requestId>${origRequestId}</requestId>\r
373                                 <serviceInstanceId>${srvInstId}</serviceInstanceId>\r
374                             </msoRequest>\r
375                             <messageId>${messageId}</messageId>\r
376                             <notificationUrl>${notificationUrl}</notificationUrl>\r
377                         </deleteVfModuleRequest>\r
378                         """ as String\r
379 \r
380                 utils.log("DEBUG", "vnfAdapterRestV1Request: " + request, isDebugEnabled)\r
381                 utils.logAudit("deleteVfModuleRequest: " + request)\r
382                 execution.setVariable("vnfAdapterRestV1Request", request)\r
383         }\r
384 \r
385         \r
386         // generates a WorkflowException if\r
387         //              -\r
388         public void handleDoDeleteVfModuleFailure(Execution execution) {\r
389                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
390                 utils.log("ERROR", "AAI error occurred deleting the Generic Vnf: "\r
391                         + execution.getVariable("DDVFMV_deleteGenericVnfResponse"), isDebugEnabled)\r
392                 String processKey = getProcessKey(execution);\r
393                 WorkflowException exception = new WorkflowException(processKey, 5000,\r
394                         execution.getVariable("DDVFMV_deleteGenericVnfResponse"))\r
395                 execution.setVariable("WorkflowException", exception)\r
396         }\r
397         \r
398         public void postProcessVNFAdapterRequest(Execution execution) {\r
399                 def method = getClass().getSimpleName() + '.postProcessVNFAdapterRequest(' +\r
400                         'execution=' + execution.getId() +\r
401                         ')'\r
402                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
403                 logDebug('Entered ' + method, isDebugLogEnabled)\r
404                 execution.setVariable("prefix",Prefix)\r
405                 try{\r
406                 logDebug(" *** STARTED postProcessVNFAdapterRequest Process*** ", isDebugLogEnabled)\r
407                 \r
408                 String vnfResponse = execution.getVariable("DDVMFV_doDeleteVfModuleResponse")\r
409                 logDebug("VNF Adapter Response is: " + vnfResponse, isDebugLogEnabled)\r
410                 utils.logAudit("deleteVnfAResponse is: \n"  + vnfResponse)\r
411 \r
412                 if(vnfResponse != null){\r
413 \r
414                         if(vnfResponse.contains("deleteVfModuleResponse")){\r
415                                 logDebug("Received a Good Response from VNF Adapter for DELETE_VF_MODULE Call.", isDebugLogEnabled)\r
416                                 execution.setVariable("DDVFMV_vnfVfModuleDeleteCompleted", true)\r
417 \r
418                                 // Parse vnfOutputs for contrail network polcy FQDNs\r
419                                 if (vnfResponse.contains("vfModuleOutputs")) {\r
420                                         def vfModuleOutputsXml = utils.getNodeXml(vnfResponse, "vfModuleOutputs")\r
421                                         InputSource source = new InputSource(new StringReader(vfModuleOutputsXml));\r
422                         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();\r
423                         docFactory.setNamespaceAware(true)\r
424                         DocumentBuilder docBuilder = docFactory.newDocumentBuilder()\r
425                         Document outputsXml = docBuilder.parse(source)\r
426 \r
427                                         NodeList entries = outputsXml.getElementsByTagNameNS("*", "entry")\r
428                                         List contrailNetworkPolicyFqdnList = []\r
429                                         for (int i = 0; i< entries.getLength(); i++) {\r
430                                                 Node node = entries.item(i)\r
431                                                 if (node.getNodeType() == Node.ELEMENT_NODE) {\r
432                                                         Element element = (Element) node\r
433                                                         String key = element.getElementsByTagNameNS("*", "key").item(0).getTextContent()\r
434                                                         if (key.endsWith("contrail_network_policy_fqdn")) {\r
435                                                                 String contrailNetworkPolicyFqdn = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()\r
436                                                                 logDebug("Obtained contrailNetworkPolicyFqdn: " + contrailNetworkPolicyFqdn, isDebugLogEnabled)\r
437                                                                 contrailNetworkPolicyFqdnList.add(contrailNetworkPolicyFqdn)\r
438                                                         }\r
439                                                         else if (key.equals("oam_management_v4_address")) {\r
440                                                                 String oamManagementV4Address = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()\r
441                                                                 logDebug("Obtained oamManagementV4Address: " + oamManagementV4Address, isDebugLogEnabled)\r
442                                                                 execution.setVariable(Prefix + "oamManagementV4Address", oamManagementV4Address)\r
443                                                         }\r
444                                                         else if (key.equals("oam_management_v6_address")) {\r
445                                                                 String oamManagementV6Address = element.getElementsByTagNameNS("*", "value").item(0).getTextContent()\r
446                                                                 logDebug("Obtained oamManagementV6Address: " + oamManagementV6Address, isDebugLogEnabled)\r
447                                                                 execution.setVariable(Prefix + "oamManagementV6Address", oamManagementV6Address)\r
448                                                         }\r
449 \r
450                                                 }\r
451                                         }\r
452                                         if (!contrailNetworkPolicyFqdnList.isEmpty()) {\r
453                                                 logDebug("Setting the fqdn list", isDebugLogEnabled)\r
454                                                 execution.setVariable("DDVFMV_contrailNetworkPolicyFqdnList", contrailNetworkPolicyFqdnList)\r
455                                         }\r
456                                 }\r
457                         }else{\r
458                                 logDebug("Received a BAD Response from VNF Adapter for DELETE_VF_MODULE Call.", isDebugLogEnabled)\r
459                                 exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "VNF Adapter Error")\r
460                         }\r
461                 }else{\r
462                         logDebug("Response from VNF Adapter is Null for DELETE_VF_MODULE Call.", isDebugLogEnabled)\r
463                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Empty response from VNF Adapter")\r
464                 }\r
465 \r
466                 }catch(BpmnError b){\r
467                         throw b\r
468                 }catch(Exception e){\r
469                         logDebug("Internal Error Occured in PostProcess Method", isDebugLogEnabled)\r
470                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Internal Error Occured in PostProcess Method")\r
471                 }\r
472                 logDebug(" *** COMPLETED postProcessVnfAdapterResponse Process*** ", isDebugLogEnabled)\r
473         }\r
474 \r
475         public void deleteNetworkPoliciesFromAAI(Execution execution) {\r
476                 def method = getClass().getSimpleName() + '.deleteNetworkPoliciesFromAAI(' +\r
477                 'execution=' + execution.getId() +\r
478                 ')'\r
479                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
480                 logDebug('Entered ' + method, isDebugLogEnabled)\r
481                 execution.setVariable("prefix", Prefix)\r
482                 logDebug(" ======== STARTED deleteNetworkPoliciesFromAAI ======== ", isDebugLogEnabled)\r
483 \r
484                 try {\r
485                         // get variables\r
486                         List fqdnList = execution.getVariable("DDVFMV_contrailNetworkPolicyFqdnList")\r
487                         if (fqdnList == null) {\r
488                                 logDebug("No network policies to delete", isDebugLogEnabled)\r
489                                 return\r
490                         }\r
491                         int fqdnCount = fqdnList.size()\r
492 \r
493                         execution.setVariable("DDVFMV_networkPolicyFqdnCount", fqdnCount)\r
494                         logDebug("DDVFMV_networkPolicyFqdnCount - " + fqdnCount, isDebugLogEnabled)\r
495 \r
496                         String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
497                         AaiUtil aaiUriUtil = new AaiUtil(this)\r
498                         String aai_uri = aaiUriUtil.getNetworkPolicyUri(execution)\r
499 \r
500                         if (fqdnCount > 0) {\r
501                                 // AII loop call over contrail network policy fqdn list\r
502                                 for (i in 0..fqdnCount-1) {\r
503 \r
504                                         int counting = i+1\r
505                                         String fqdn = fqdnList[i]\r
506 \r
507                                         // Query AAI for this network policy FQDN\r
508 \r
509                                         String queryNetworkPolicyByFqdnAAIRequest = "${aai_endpoint}${aai_uri}?network-policy-fqdn=" + UriUtils.encode(fqdn, "UTF-8")\r
510                                         utils.logAudit("AAI request endpoint: " + queryNetworkPolicyByFqdnAAIRequest)\r
511                                         logDebug("AAI request endpoint: "  + queryNetworkPolicyByFqdnAAIRequest, isDebugLogEnabled)\r
512 \r
513                                         APIResponse response = aaiUriUtil.executeAAIGetCall(execution, queryNetworkPolicyByFqdnAAIRequest)\r
514                                         int returnCode = response.getStatusCode()\r
515                                         execution.setVariable("DCVFM_aaiQueryNetworkPolicyByFqdnReturnCode", returnCode)\r
516                                         logDebug(" ***** AAI query network policy Response Code, NetworkPolicy #" + counting + " : " + returnCode, isDebugLogEnabled)\r
517 \r
518                                         String aaiResponseAsString = response.getResponseBodyAsString()\r
519 \r
520                                         if (isOneOf(returnCode, 200, 201)) {\r
521                                                 logDebug("The return code is: "  + returnCode, isDebugLogEnabled)\r
522                                                 // This network policy FQDN exists in AAI - need to delete it now\r
523                                                 utils.logAudit(aaiResponseAsString)\r
524                                                 execution.setVariable("DDVFMV_queryNetworkPolicyByFqdnAAIResponse", aaiResponseAsString)\r
525                                                 logDebug("QueryAAINetworkPolicyByFQDN Success REST Response, , NetworkPolicy #" + counting + " : " + "\n" + aaiResponseAsString, isDebugLogEnabled)\r
526                                                 // Retrieve the network policy id for this FQDN\r
527                                                 def networkPolicyId = utils.getNodeText1(aaiResponseAsString, "network-policy-id")\r
528                                                 logDebug("Deleting network-policy with network-policy-id " + networkPolicyId, isDebugLogEnabled)\r
529 \r
530                                                 // Retrieve the resource version for this network policy\r
531                                                 def resourceVersion = utils.getNodeText1(aaiResponseAsString, "resource-version")\r
532                                                 logDebug("Deleting network-policy with resource-version " + resourceVersion, isDebugLogEnabled)\r
533 \r
534                                                 String delNetworkPolicyAAIRequest = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(networkPolicyId, "UTF-8") +\r
535                                                         "?resource-version=" + UriUtils.encode(resourceVersion, "UTF-8")\r
536                                                 utils.logAudit("AAI request endpoint: " + delNetworkPolicyAAIRequest)\r
537                                                 logDebug("AAI request endpoint: " + delNetworkPolicyAAIRequest, isDebugLogEnabled)\r
538 \r
539                                                 logDebug("invoking DELETE call to AAI", isDebugLogEnabled)\r
540                                                 utils.logAudit("Sending DELETE call to AAI with Endpoint /n" + delNetworkPolicyAAIRequest)\r
541                                                 APIResponse responseDel = aaiUriUtil.executeAAIDeleteCall(execution, delNetworkPolicyAAIRequest)\r
542                                                 int returnCodeDel = responseDel.getStatusCode()\r
543                                                 execution.setVariable("DDVFMV_aaiDeleteNetworkPolicyReturnCode", returnCodeDel)\r
544                                                 logDebug(" ***** AAI delete network policy Response Code, NetworkPolicy #" + counting + " : " + returnCodeDel, isDebugLogEnabled)\r
545 \r
546                                                 if (isOneOf(returnCodeDel, 200, 201, 204)) {\r
547                                                         logDebug("The return code from deleting network policy is: "  + returnCodeDel, isDebugLogEnabled)\r
548                                                         // This network policy was deleted from AAI successfully\r
549                                                         logDebug(" DelAAINetworkPolicy Success REST Response, , NetworkPolicy #" + counting + " : ", isDebugLogEnabled)\r
550 \r
551                                                 } else {\r
552                                                                 // aai all errors\r
553                                                                 String delErrorMessage = "Unable to delete network-policy to AAI deleteNetworkPoliciesFromAAI - " + returnCodeDel\r
554                                                          logDebug(delErrorMessage, isDebugLogEnabled)\r
555                                                          exceptionUtil.buildAndThrowWorkflowException(execution, 2500, delErrorMessage)\r
556                                                 }\r
557                                         } else if (returnCode == 404) {\r
558                                                 // This network policy FQDN is not in AAI. No need to delete.\r
559                                                 logDebug("The return code is: "  + returnCode, isDebugLogEnabled)\r
560                                                 logDebug("This network policy FQDN is not in AAI: " + fqdn, isDebugLogEnabled)\r
561                                                 utils.logAudit("Network policy FQDN is not in AAI")\r
562                                         } else {\r
563                                            if (aaiResponseAsString.contains("RESTFault")) {\r
564                                                    WorkflowException exceptionObject = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)\r
565                                                    execution.setVariable("WorkflowException", exceptionObject)\r
566                                                    throw new BpmnError("MSOWorkflowException")\r
567 \r
568                                                    } else {\r
569                                                                 // aai all errors\r
570                                                                 String dataErrorMessage = "Unexpected Response from deleteNetworkPoliciesFromAAI - " + returnCode\r
571                                                                 logDebug(dataErrorMessage, isDebugLogEnabled)\r
572                                                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)\r
573 \r
574                                                   }\r
575                                         }\r
576 \r
577 \r
578 \r
579                                 } // end loop\r
580 \r
581 \r
582                         } else {\r
583                                    logDebug("No contrail network policies to query/create", isDebugLogEnabled)\r
584 \r
585                         }\r
586 \r
587                 } catch (BpmnError e) {\r
588                         throw e;\r
589 \r
590                 } catch (Exception ex) {\r
591                         String exceptionMessage = "Bpmn error encountered in DoDeletVfModule flow. deleteNetworkPoliciesFromAAI() - " + ex.getMessage()\r
592                         logDebug(exceptionMessage, isDebugLogEnabled)\r
593                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)\r
594                 }\r
595 \r
596         }       \r
597         \r
598         // and formulate the outgoing DeleteAAIVfModuleRequest request\r
599         public void prepDeleteAAIVfModule(Execution execution) {\r
600                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
601 \r
602                 def vnfId = execution.getVariable("vnfId")\r
603                 def vfModuleId = execution.getVariable("vfModuleId")\r
604                 // formulate the request for UpdateAAIVfModule\r
605                 String request = """<DeleteAAIVfModuleRequest>\r
606                                                                 <vnf-id>${vnfId}</vnf-id>\r
607                                                                 <vf-module-id>${vfModuleId}</vf-module-id>\r
608                                                         </DeleteAAIVfModuleRequest>""" as String\r
609                 utils.log("DEBUG", "DeleteAAIVfModuleRequest :" + request, isDebugEnabled)\r
610                 utils.logAudit("DeleteAAIVfModuleRequest: " + request)\r
611                 execution.setVariable("DeleteAAIVfModuleRequest", request)\r
612         }\r
613 }