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