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