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