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