Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVfModuleRollback.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 import org.camunda.bpm.engine.delegate.BpmnError
23 import org.camunda.bpm.engine.delegate.DelegateExecution
24 import org.onap.aai.domain.yang.NetworkPolicies
25 import org.onap.aai.domain.yang.NetworkPolicy
26 import org.onap.so.bpmn.common.scripts.AaiUtil
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
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.core.UrnPropertiesReader
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.client.aai.AAIObjectPlurals
34 import org.onap.so.client.aai.AAIObjectType
35 import org.onap.so.client.aai.entities.uri.AAIResourceUri
36 import org.onap.so.client.aai.entities.uri.AAIUriFactory
37 import org.onap.so.logger.MessageEnum
38 import org.onap.so.logger.MsoLogger
39 import org.onap.so.rest.APIResponse
40 import org.springframework.web.util.UriUtils
41
42 import javax.ws.rs.NotFoundException
43
44
45 public class DoCreateVfModuleRollback extends AbstractServiceTaskProcessor{
46         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateVfModuleRollback.class);
47         
48         def Prefix="DCVFMR_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50
51         public void initProcessVariables(DelegateExecution execution) {
52                 execution.setVariable("prefix",Prefix)
53         }
54
55         // parse the incoming DELETE_VF_MODULE request for the Generic Vnf and Vf Module Ids
56         // and formulate the outgoing request for PrepareUpdateAAIVfModuleRequest
57         public void preProcessRequest(DelegateExecution execution) {
58                 
59
60                 initProcessVariables(execution)
61
62                 try {
63
64                         execution.setVariable("rolledBack", null)
65                         execution.setVariable("rollbackError", null)
66                         
67                         def rollbackData = execution.getVariable("rollbackData")
68                         msoLogger.debug("RollbackData:" + rollbackData)
69                         
70                         if (rollbackData != null) {
71                         String vnfId = rollbackData.get("VFMODULE", "vnfid")
72                         execution.setVariable("DCVFMR_vnfId", vnfId)
73                         String vfModuleId = rollbackData.get("VFMODULE", "vfmoduleid")
74                         execution.setVariable("DCVFMR_vfModuleId", vfModuleId)
75                         String source = rollbackData.get("VFMODULE", "source")
76                         execution.setVariable("DCVFMR_source", source)
77                         String serviceInstanceId = rollbackData.get("VFMODULE", "serviceInstanceId")
78                         execution.setVariable("DCVFMR_serviceInstanceId", serviceInstanceId)
79                         String serviceId = rollbackData.get("VFMODULE", "service-id")
80                         execution.setVariable("DCVFMR_serviceId", serviceId)
81                         String vnfType = rollbackData.get("VFMODULE", "vnftype")
82                         execution.setVariable("DCVFMR_vnfType", vnfType)
83                         String vnfName = rollbackData.get("VFMODULE", "vnfname")
84                         execution.setVariable("DCVFMR_vnfName", vnfName)
85                         String tenantId = rollbackData.get("VFMODULE", "tenantid")
86                         execution.setVariable("DCVFMR_tenantId", tenantId)
87                         String vfModuleName = rollbackData.get("VFMODULE", "vfmodulename")
88                         execution.setVariable("DCVFMR_vfModuleName", vfModuleName)
89                         String vfModuleModelName = rollbackData.get("VFMODULE", "vfmodulemodelname")
90                         execution.setVariable("DCVFMR_vfModuleModelName", vfModuleModelName)
91                         String cloudSiteId = rollbackData.get("VFMODULE", "aiccloudregion")
92                         execution.setVariable("DCVFMR_cloudSiteId", cloudSiteId)
93                         String heatStackId = rollbackData.get("VFMODULE", "heatstackid")
94                         execution.setVariable("DCVFMR_heatStackId", heatStackId)
95                         String requestId = rollbackData.get("VFMODULE", "msorequestid")
96                         execution.setVariable("DCVFMR_requestId", requestId)
97                         // Set mso-request-id to request-id for VNF Adapter interface
98                         execution.setVariable("mso-request-id", requestId)
99                         List createdNetworkPolicyFqdnList = []
100                         int i = 0
101                         while (i < 100) {
102                                 String fqdn = rollbackData.get("VFMODULE", "contrailNetworkPolicyFqdn" + i)
103                                 if (fqdn == null) {
104                                         break
105                                 }
106                                 createdNetworkPolicyFqdnList.add(fqdn)
107                                 msoLogger.debug("got fqdn # " + i + ": " + fqdn)
108                                 i = i + 1
109         
110                         }
111         
112                         execution.setVariable("DCVFMR_createdNetworkPolicyFqdnList", createdNetworkPolicyFqdnList)
113                         String oamManagementV4Address = rollbackData.get("VFMODULE", "oamManagementV4Address")
114                         execution.setVariable("DCVFMR_oamManagementV4Address", oamManagementV4Address)
115                         String oamManagementV6Address = rollbackData.get("VFMODULE", "oamManagementV6Address")
116                         execution.setVariable("DCVFMR_oamManagementV6Address", oamManagementV6Address)
117                         //String serviceInstanceId = rollbackData.get("VFMODULE", "msoserviceinstanceid")
118                         //execution.setVariable("DCVFMR_serviceInstanceId", serviceInstanceId)
119                         execution.setVariable("DCVFMR_rollbackPrepareUpdateVfModule", rollbackData.get("VFMODULE", "rollbackPrepareUpdateVfModule"))
120                         execution.setVariable("DCVFMR_rollbackUpdateAAIVfModule", rollbackData.get("VFMODULE", "rollbackUpdateAAIVfModule"))
121                         execution.setVariable("DCVFMR_rollbackVnfAdapterCreate", rollbackData.get("VFMODULE", "rollbackVnfAdapterCreate"))
122                         execution.setVariable("DCVFMR_rollbackSDNCRequestAssign", rollbackData.get("VFMODULE", "rollbackSDNCRequestAssign"))
123                         execution.setVariable("DCVFMR_rollbackSDNCRequestActivate", rollbackData.get("VFMODULE", "rollbackSDNCRequestActivate"))
124                         execution.setVariable("DCVFMR_rollbackCreateAAIVfModule", rollbackData.get("VFMODULE", "rollbackCreateAAIVfModule"))
125                         execution.setVariable("DCVFMR_rollbackCreateNetworkPoliciesAAI", rollbackData.get("VFMODULE", "rollbackCreateNetworkPoliciesAAI"))
126                         execution.setVariable("DCVFMR_rollbackUpdateVnfAAI", rollbackData.get("VFMODULE", "rollbackUpdateVnfAAI"))
127         
128                         // formulate the request for PrepareUpdateAAIVfModule
129                         String request = """<PrepareUpdateAAIVfModuleRequest>
130                                                                         <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
131                                                                         <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
132                                                                         <orchestration-status>pending-delete</orchestration-status>
133                                                                 </PrepareUpdateAAIVfModuleRequest>""" as String
134                         msoLogger.debug("PrepareUpdateAAIVfModuleRequest :" + request)
135                         execution.setVariable("PrepareUpdateAAIVfModuleRequest", request)
136                 } else {
137                         execution.setVariable("skipRollback", true)
138                 }
139                 
140                 if (execution.getVariable("disableRollback").equals("true" )) {
141                         execution.setVariable("skipRollback", true)
142                 }
143                 
144                 } catch (BpmnError e) {
145                         throw e;
146                 } catch (Exception ex){
147                         def msg = "Exception in DoCreateVfModuleRollback preProcessRequest " + ex.getMessage()
148                         msoLogger.debug(msg)
149                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
150                 }
151         }
152
153         // build a SDNC vnf-topology-operation request for the specified action
154         // (note: the action passed is expected to be 'changedelete' or 'delete')
155         public void prepSDNCAdapterRequest(DelegateExecution execution) {
156                 
157                 String srvInstId = execution.getVariable("DCVFMR_serviceInstanceId")
158
159                 String uuid = execution.getVariable('testReqId') // for junits
160                 if(uuid==null){
161                         uuid = execution.getVariable("DCVFMR_requestId") + "-" +        System.currentTimeMillis()
162                 }
163                 
164                 def callbackUrl = UrnPropertiesReader.getVariable("mso.workflow.sdncadapter.callback",execution)
165
166                 String source = execution.getVariable("DCVFMR_source")
167                 String serviceId = execution.getVariable("DCVFMR_serviceId")
168                 String vnfId = execution.getVariable("DCVFMR_vnfId")
169                 String vnfType = execution.getVariable("DCVFMR_vnfType")
170                 String vnfName = execution.getVariable("DCVFMR_vnfName")
171                 String tenantId = execution.getVariable("DCVFMR_tenantId")
172                 String vfModuleId = execution.getVariable("DCVFMR_vfModuleId")
173                 String vfModuleName = execution.getVariable("DCVFMR_vfModuleName")
174                 String vfModuleModelName = execution.getVariable("DCVFMR_vfModuleModelName")
175                 String cloudSiteId = execution.getVariable("DCVFMR_cloudSiteId")
176                 String requestId = execution.getVariable("DCVFMR_requestId")
177
178                 String serviceInstanceIdToSdnc = ""
179                 if (srvInstId != null && !srvInstId.isEmpty()) {
180                         serviceInstanceIdToSdnc = srvInstId
181                 } else {
182                     serviceInstanceIdToSdnc = vfModuleId
183                 }
184
185                 def doSDNCActivateRollback = execution.getVariable("DCVFMR_rollbackSDNCRequestActivate")
186                 def doSDNCAssignRollback = execution.getVariable("DCVFMR_rollbackSDNCRequestAssign")
187
188                 def action = ""
189                 def requestAction = ""
190
191                 if (doSDNCActivateRollback.equals("true")) {
192                         action = "delete"
193                         requestAction = "DisconnectVNFRequest"
194                 }
195                 else if (doSDNCAssignRollback.equals("true")) {
196                         action = "rollback"
197                         requestAction = "VNFActivateRequest"
198                 }
199                 else
200                         return
201
202
203                 String request = """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
204                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
205                                                                                                         xmlns:sdncadapter="http://org.onap/workflow/sdnc/adapter/schema/v1">
206                                                       <sdncadapter:RequestHeader>
207                                                          <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
208                                                          <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(vfModuleId)}</sdncadapter:SvcInstanceId>
209                                                          <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
210                                                          <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
211                                                          <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackUrl)}</sdncadapter:CallbackUrl>
212                                                       </sdncadapter:RequestHeader>
213                                                       <sdncadapterworkflow:SDNCRequestData>
214                                                          <request-information>
215                                                             <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
216                                                             <request-action>${MsoUtils.xmlEscape(requestAction)}</request-action>
217                                                             <source>${MsoUtils.xmlEscape(source)}</source>
218                                                             <notification-url/>
219                                                             <order-number/>
220                                                             <order-version/>
221                                                          </request-information>
222                                                          <service-information>
223                                                             <service-id>${MsoUtils.xmlEscape(serviceId)}</service-id>
224                                                                         <service-type>${MsoUtils.xmlEscape(serviceId)}</service-type>
225                                                             <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceIdToSdnc)}</service-instance-id>
226                                                             <subscriber-name>notsurewecare</subscriber-name>
227                                                          </service-information>
228                                                          <vnf-request-information>
229                                                                 <vnf-id>${MsoUtils.xmlEscape(vfModuleId)}</vnf-id>
230                                                                         <vnf-type>${MsoUtils.xmlEscape(vfModuleModelName)}</vnf-type>
231                                     <vnf-name>${MsoUtils.xmlEscape(vfModuleName)}</vnf-name>
232                                                                         <generic-vnf-id>${MsoUtils.xmlEscape(vnfId)}</generic-vnf-id>
233                                     <generic-vnf-name>${MsoUtils.xmlEscape(vnfName)}</generic-vnf-name>
234                                                                         <generic-vnf-type>${MsoUtils.xmlEscape(vnfType)}</generic-vnf-type>
235                                                                         <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>
236                                                                         <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
237                                                          </vnf-request-information>
238                                                       </sdncadapterworkflow:SDNCRequestData>
239                                                    </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
240
241                 msoLogger.debug("sdncAdapterWorkflowRequest: " + request)
242                 execution.setVariable("sdncAdapterWorkflowRequest", request)
243         }
244
245         public void preProcessSDNCDeactivateRequest(DelegateExecution execution){
246                 
247                 execution.setVariable("prefix", Prefix)
248                 msoLogger.trace("STARTED preProcessSDNCDeactivateRequest")
249                 
250                 def serviceInstanceId = execution.getVariable("DCVFMR_serviceInstanceId")
251         
252                 try{
253                         //Build SDNC Request
254                         
255                         String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
256         
257                         deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)
258                         execution.setVariable("DCVFMR_deactivateSDNCRequest", deactivateSDNCRequest)
259                         msoLogger.debug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)
260         
261                 }catch(Exception e){
262                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing preProcessSDNCDeactivateRequest.", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
263                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())
264                 }
265                 msoLogger.trace("COMPLETED preProcessSDNCDeactivateRequest")
266         }
267
268         public void preProcessSDNCUnassignRequest(DelegateExecution execution) {
269                 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
270                         'execution=' + execution.getId() +
271                         ')'
272                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
273                 msoLogger.trace('Entered ' + method)
274                 execution.setVariable("prefix", Prefix)
275                 msoLogger.trace("STARTED preProcessSDNCUnassignRequest Process")
276                 try{
277                         String serviceInstanceId = execution.getVariable("DCVFMR_serviceInstanceId")
278         
279                         String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
280         
281                         execution.setVariable("DCVFMR_unassignSDNCRequest", unassignSDNCRequest)
282                         msoLogger.debug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest)
283         
284                 }catch(Exception e){
285                         msoLogger.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e)
286                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCUnassignRequest Method:\n" + e.getMessage())
287                 }
288                 msoLogger.trace("COMPLETED  preProcessSDNCUnassignRequest Process")
289         }
290
291         public String buildSDNCRequest(DelegateExecution execution, String svcInstId, String action){
292         
293                         String uuid = execution.getVariable('testReqId') // for junits
294                         if(uuid==null){
295                                 uuid = execution.getVariable("DCVFMR_requestId") + "-" +        System.currentTimeMillis()
296                         }
297                         def callbackURL = UrnPropertiesReader.getVariable("mso.workflow.sdncadapter.callback",execution)
298                         def requestId = execution.getVariable("DCVFMR_requestId")
299                         def serviceId = execution.getVariable("DCVFMR_serviceId")
300                         def serviceInstanceId = execution.getVariable("DCVFMR_serviceInstanceId")
301                         def vfModuleId = execution.getVariable("DCVFMR_vfModuleId")
302                         def source = execution.getVariable("DCVFMR_source")
303                         def vnfId = execution.getVariable("DCVFMR_vnfId")
304                                 
305                         def sdncVersion = execution.getVariable("sdncVersion")
306                         
307                         String sdncRequest =
308                         """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
309                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
310                                                                                                         xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1">
311            <sdncadapter:RequestHeader>
312                                 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
313                                 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
314                                 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
315                                 <sdncadapter:SvcOperation>vf-module-topology-operation</sdncadapter:SvcOperation>
316                                 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
317                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
318                 </sdncadapter:RequestHeader>
319         <sdncadapterworkflow:SDNCRequestData>
320                 <request-information>
321                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
322                         <request-action>DeleteVfModuleInstance</request-action>
323                         <source>${MsoUtils.xmlEscape(source)}</source>
324                         <notification-url/>
325                         <order-number/>
326                         <order-version/>
327                 </request-information>
328                 <service-information>
329                         <service-id/>
330                         <subscription-service-type/>                    
331                         <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
332                         <global-customer-id/>
333                 </service-information>
334                 <vnf-information>
335                         <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
336                         <vnf-type/>                     
337                 </vnf-information>
338                 <vf-module-information>
339                         <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
340                 </vf-module-information>
341                 <vf-module-request-input/>              
342         </sdncadapterworkflow:SDNCRequestData>
343         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
344         
345                 msoLogger.debug("sdncRequest:  " + sdncRequest)
346                 return sdncRequest
347         }
348         
349         // parse the incoming DELETE_VF_MODULE request
350         // and formulate the outgoing VnfAdapterDeleteV1 request
351         public void prepVNFAdapterRequest(DelegateExecution execution) {
352                 
353                 String requestId = UUID.randomUUID().toString()
354                 String origRequestId = execution.getVariable("DCVFMR_requestId")
355                 String srvInstId = execution.getVariable("DCVFMR_serviceInstanceId")
356                 String aicCloudRegion = execution.getVariable("DCVFMR_cloudSiteId")
357                 String vnfId = execution.getVariable("DCVFMR_vnfId")
358                 String vfModuleId = execution.getVariable("DCVFMR_vfModuleId")
359                 String vfModuleStackId = execution.getVariable("DCVFMR_heatStackId")
360                 String tenantId = execution.getVariable("DCVFMR_tenantId")
361                 def messageId = execution.getVariable('mso-request-id') + '-' +
362                         System.currentTimeMillis()
363                 def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId)
364                 def useQualifiedHostName = UrnPropertiesReader.getVariable("mso.use.qualified.host",execution)
365                 if ('true'.equals(useQualifiedHostName)) {
366                         notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
367                 }
368
369                 String request = """
370                         <deleteVfModuleRequest>
371                             <cloudSiteId>${MsoUtils.xmlEscape(aicCloudRegion)}</cloudSiteId>
372                             <tenantId>${MsoUtils.xmlEscape(tenantId)}</tenantId>
373                             <vnfId>${MsoUtils.xmlEscape(vnfId)}</vnfId>
374                             <vfModuleId>${MsoUtils.xmlEscape(vfModuleId)}</vfModuleId>
375                             <vfModuleStackId>${MsoUtils.xmlEscape(vfModuleStackId)}</vfModuleStackId>
376                             <skipAAI>true</skipAAI>
377                             <msoRequest>
378                                 <requestId>${MsoUtils.xmlEscape(origRequestId)}</requestId>
379                                 <serviceInstanceId>${MsoUtils.xmlEscape(srvInstId)}</serviceInstanceId>
380                             </msoRequest>
381                             <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
382                             <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
383                         </deleteVfModuleRequest>
384                         """ as String
385
386                 msoLogger.debug("vnfAdapterRestV1Request: " + request)
387                 execution.setVariable("vnfAdapterRestV1Request", request)
388         }
389
390         // parse the incoming DELETE_VF_MODULE request
391         // and formulate the outgoing UpdateAAIVfModuleRequest request
392         public void prepUpdateAAIVfModule(DelegateExecution execution) {
393                 
394                 String vnfId = execution.getVariable("DCVFMR_vnfId")
395                 String vfModuleId = execution.getVariable("DCVFMR_vfModuleId")
396                 // formulate the request for UpdateAAIVfModule
397                 String request = """<UpdateAAIVfModuleRequest>
398                                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
399                                                                 <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
400                                                                 <heat-stack-id>DELETE</heat-stack-id>
401                                                                 <orchestration-status>deleted</orchestration-status>
402                                                         </UpdateAAIVfModuleRequest>""" as String
403                 msoLogger.debug("UpdateAAIVfModuleRequest :" + request)
404                 execution.setVariable("UpdateAAIVfModuleRequest", request)
405         }
406         
407         // parse the incoming DELETE_VF_MODULE request
408         // and formulate the outgoing UpdateAAIVfModuleRequest request
409         public void prepUpdateAAIVfModuleToAssigned(DelegateExecution execution) {
410                 
411                 String vnfId = execution.getVariable("DCVFMR_vnfId")
412                 String vfModuleId = execution.getVariable("DCVFMR_vfModuleId")
413                 // formulate the request for UpdateAAIVfModule
414                 String request = """<UpdateAAIVfModuleRequest>
415                                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
416                                                                 <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
417                                                                 <heat-stack-id></heat-stack-id>
418                                                                 <orchestration-status>Assigned</orchestration-status>
419                                                         </UpdateAAIVfModuleRequest>""" as String
420                 msoLogger.debug("UpdateAAIVfModuleRequest :" + request)
421                 execution.setVariable("UpdateAAIVfModuleRequest", request)
422         }
423
424         // parse the incoming DELETE_VF_MODULE request
425         // and formulate the outgoing DeleteAAIVfModuleRequest request
426         public void prepDeleteAAIVfModule(DelegateExecution execution) {
427                 
428                 String vnfId = execution.getVariable("DCVFMR_vnfId")
429                 String vfModuleId = execution.getVariable("DCVFMR_vfModuleId")
430                 // formulate the request for UpdateAAIVfModule
431                 String request = """<DeleteAAIVfModuleRequest>
432                                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
433                                                                 <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
434                                                         </DeleteAAIVfModuleRequest>""" as String
435                 msoLogger.debug("DeleteAAIVfModuleRequest :" + request)
436                 execution.setVariable("DeleteAAIVfModuleRequest", request)
437         }
438
439         // generates a WorkflowException if
440         //              -
441         public void handleDoDeleteVfModuleFailure(DelegateExecution execution) {
442                 
443                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "AAI error occurred deleting the Generic Vnf"+ execution.getVariable("DoDVfMod_deleteGenericVnfResponse"), "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError);
444                 String processKey = getProcessKey(execution);
445                 exceptionUtil.buildWorkflowException(execution, 5000, "Failure in DoDeleteVfModule")
446
447         }
448
449         public void sdncValidateResponse(DelegateExecution execution, String response){
450                 
451                 execution.setVariable("prefix",Prefix)
452
453                 WorkflowException workflowException = execution.getVariable("WorkflowException")
454                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
455
456                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
457                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
458
459                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
460                         msoLogger.debug("Successfully Validated SDNC Response")
461                 }else{
462                         throw new BpmnError("MSOWorkflowException")
463                 }
464         }
465
466         public void deleteNetworkPoliciesFromAAI(DelegateExecution execution) {
467                 def method = getClass().getSimpleName() + '.deleteNetworkPoliciesFromAAI(' +
468                 'execution=' + execution.getId() +
469                 ')'
470                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
471                 msoLogger.trace('Entered ' + method)
472                 execution.setVariable("prefix", Prefix)
473                 msoLogger.trace("STARTED deleteNetworkPoliciesFromAAI")
474
475                 try {
476                         // get variables
477                         List fqdnList = execution.getVariable(Prefix + "createdNetworkPolicyFqdnList")
478                         if (fqdnList == null) {
479                                 msoLogger.debug("No network policies to delete")
480                                 return
481                         }
482                         int fqdnCount = fqdnList.size()
483
484                         execution.setVariable(Prefix + "networkPolicyFqdnCount", fqdnCount)
485                         msoLogger.debug("networkPolicyFqdnCount - " + fqdnCount)
486
487                         AaiUtil aaiUriUtil = new AaiUtil(this)
488
489                         if (fqdnCount > 0) {
490                                 // AII loop call over contrail network policy fqdn list
491                                 for (i in 0..fqdnCount-1) {
492
493                                         int counting = i+1
494                                         String fqdn = fqdnList[i]
495
496                     try {
497                         // Query AAI for this network policy FQDN
498                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.NETWORK_POLICY)
499                         uri.queryParam("network-policy-fqdn", fqdn)
500                         Optional<NetworkPolicies> networkPolicies = getAAIClient().get(NetworkPolicies.class, uri)
501
502                         if (networkPolicies.isPresent() && !networkPolicies.get().getNetworkPolicy().isEmpty()) {
503                             execution.setVariable(Prefix + "aaiQueryNetworkPolicyByFqdnReturnCode", 200)
504                             NetworkPolicy networkPolicy = networkPolicies.get().getNetworkPolicy().get(0)
505
506                             try{
507                             AAIResourceUri delUri = AAIUriFactory.createResourceUri(AAIObjectType.NETWORK_POLICY, networkPolicy.getNetworkPolicyId())
508                             getAAIClient().delete(delUri)
509                             execution.setVariable(Prefix + "aaiDeleteNetworkPolicyReturnCode", 200)
510                             msoLogger.debug("AAI delete network policy Response Code, NetworkPolicy #" + counting + " : " + 200)
511                                 msoLogger.debug("The return code from deleting network policy is: " + 200)
512                                 // This network policy was deleted from AAI successfully
513                                 msoLogger.debug(" DelAAINetworkPolicy Success REST Response, , NetworkPolicy #" + counting + " : ")
514                             }catch(NotFoundException ne){
515                                 // This network policy FQDN is not in AAI. No need to delete.
516                                 execution.setVariable(Prefix + "aaiDeleteNetworkPolicyReturnCode", 404)
517                                 msoLogger.debug("The return code is: " + 404)
518                                 msoLogger.debug("This network policy FQDN is not in AAI: " + fqdn)
519                             }catch(Exception e){
520                                 // aai all errors
521                                 String delErrorMessage = "Unable to delete network-policy to AAI deleteNetworkPoliciesFromAAI - " + e.getMessage()
522                                 msoLogger.debug(delErrorMessage)
523                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, delErrorMessage)
524                             }
525                         } else {
526                             // This network policy FQDN is not in AAI. No need to delete.
527                             msoLogger.debug("This network policy FQDN is not in AAI: " + fqdn)
528                         }
529                     }catch (BpmnError e){
530                         throw e
531                     }
532                     catch (Exception e) {
533                         // aai all errors
534                         String dataErrorMessage = "Unexpected Response from deleteNetworkPoliciesFromAAI - " + e.getMessage()
535                         msoLogger.debug(dataErrorMessage)
536                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
537                     }
538                                 } // end loop
539
540
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 DoCreateVfModuleRollback flow. deleteNetworkPoliciesFromAAI() - " + ex.getMessage()
551                         msoLogger.debug(exceptionMessage)
552                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
553                 }
554
555         }
556
557
558         /**
559          * Prepare a Request for invoking the UpdateAAIGenericVnf subflow.
560          *
561          * @param execution The flow's execution instance.
562          */
563         public void preProcessUpdateAAIGenericVnf(DelegateExecution execution) {
564                 def method = getClass().getSimpleName() + '.preProcessUpdateAAIGenericVnf((' +
565                         'execution=' + execution.getId() +
566                         ')'
567                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
568                 msoLogger.trace('Entered ' + method)
569
570                 try {
571                         def vnfId = execution.getVariable('DCVFMR_vnfId')
572                         def oamManagementV4Address = execution.getVariable(Prefix + 'oamManagementV4Address')
573                         def oamManagementV6Address = execution.getVariable(Prefix + 'oamManagementV6Address')
574                         def ipv4OamAddressElement = ''
575                         def managementV6AddressElement = ''
576
577                         if (oamManagementV4Address != null) {
578                                 ipv4OamAddressElement = '<ipv4-oam-address>' + 'DELETE' + '</ipv4-oam-address>'
579                         }
580
581                         if (oamManagementV6Address != null) {
582                                 managementV6AddressElement = '<management-v6-address>' + 'DELETE' + '</management-v6-address>'
583                         }
584
585
586                         String updateAAIGenericVnfRequest = """
587                                         <UpdateAAIGenericVnfRequest>
588                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
589                                                 ${ipv4OamAddressElement}
590                                                 ${managementV6AddressElement}
591                                         </UpdateAAIGenericVnfRequest>
592                                 """
593                                 updateAAIGenericVnfRequest = utils.formatXml(updateAAIGenericVnfRequest)
594                                 execution.setVariable(Prefix + 'updateAAIGenericVnfRequest', updateAAIGenericVnfRequest)
595                                 msoLogger.debug('Request for UpdateAAIGenericVnf:\n' + updateAAIGenericVnfRequest)
596
597
598                         msoLogger.trace('Exited ' + method)
599                 } catch (BpmnError e) {
600                         throw e;
601                 } catch (Exception e) {
602                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, 'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
603                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessUpdateAAIGenericVnf((): ' + e.getMessage())
604                 }
605         }
606         
607         public void setSuccessfulRollbackStatus (DelegateExecution execution){
608                 
609                 execution.setVariable("prefix", Prefix)
610                 msoLogger.trace("STARTED setSuccessfulRollbackStatus")
611         
612                 try{
613                         // Set rolledBack to true, rollbackError to null
614                         execution.setVariable("rolledBack", true)
615                         execution.setVariable("rollbackError", null)
616         
617                 }catch(Exception e){
618                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing setSuccessfulRollbackStatus.", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
619                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setSuccessfulRollbackStatus Method:\n" + e.getMessage())
620                 }
621                 msoLogger.trace("COMPLETED setSuccessfulRollbackStatus")
622         }
623         
624         public void setFailedRollbackStatus (DelegateExecution execution){
625                 
626                 execution.setVariable("prefix", Prefix)
627                 msoLogger.trace("STARTED setFailedRollbackStatus")
628         
629                 try{
630                         // Set rolledBack to false, rollbackError to actual value, rollbackData to null
631                         execution.setVariable("rolledBack", false)
632                         execution.setVariable("rollbackError", 'Caught exception in DoCreateVfModuleRollback')
633                         execution.setVariable("rollbackData", null)
634         
635                 }catch(Exception e){
636                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occured Processing setFailedRollbackStatus.", "BPMN", MsoLogger.getServiceName(),MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + e);
637                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setFailedRollbackStatus Method:\n" + e.getMessage())
638                 }
639                 msoLogger.trace("COMPLETED setFailedRollbackStatus")
640         }
641 }