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