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