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