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