Merge "Fix the issue where response is built on rollback"
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVfModuleVolumeV2.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 javax.ws.rs.NotFoundException
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.aai.domain.yang.GenericVnf
29 import org.onap.aai.domain.yang.VolumeGroup
30 import org.onap.aai.domain.yang.VolumeGroups
31 import org.onap.aaiclient.client.aai.AAIObjectType
32 import org.onap.aaiclient.client.aai.AAIResourcesClient
33 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
34 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder
36 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types
37 import org.onap.so.bpmn.common.scripts.AaiUtil
38 import org.onap.so.bpmn.common.scripts.ExceptionUtil
39 import org.onap.so.bpmn.common.scripts.MsoUtils
40 import org.onap.so.bpmn.common.scripts.VfModuleBase
41 import org.onap.so.bpmn.core.RollbackData
42 import org.onap.so.bpmn.core.UrnPropertiesReader
43 import org.onap.so.bpmn.core.WorkflowException
44 import org.onap.so.bpmn.core.json.JsonUtils
45 import org.onap.so.constants.Defaults
46 import org.onap.so.db.catalog.beans.OrchestrationStatus
47 import org.slf4j.Logger
48 import org.slf4j.LoggerFactory
49
50 class DoCreateVfModuleVolumeV2 extends VfModuleBase {
51
52     private static final Logger logger = LoggerFactory.getLogger( DoCreateVfModuleVolumeV2.class);
53     String prefix='DCVFMODVOLV2_'
54     JsonUtils jsonUtil = new JsonUtils()
55     private ExceptionUtil exceptionUtil = new ExceptionUtil()
56
57
58     /**
59      * Perform initial processing, such as request validation, initialization of variables, etc.
60      * * @param execution
61      */
62     public void preProcessRequest(DelegateExecution execution) {
63         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
64         preProcessRequest(execution, isDebugEnabled)
65     }
66
67     public void preProcessRequest(DelegateExecution execution, isDebugLogEnabled) {
68
69         execution.setVariable("prefix",prefix)
70         execution.setVariable(prefix+'SuccessIndicator', false)
71         execution.setVariable(prefix+'isPONR', false)
72
73         displayInput(execution, isDebugLogEnabled)
74         setRollbackData(execution, isDebugLogEnabled)
75         setRollbackEnabled(execution, isDebugLogEnabled)
76
77
78         def tenantId = execution.getVariable("tenantId")
79         if (tenantId == null) {
80             String cloudConfiguration = execution.getVariable("cloudConfiguration")
81             tenantId = jsonUtil.getJsonValue(cloudConfiguration, "cloudConfiguration.tenantId")
82             execution.setVariable("tenantId", tenantId)
83         }
84
85         def cloudSiteId = execution.getVariable("lcpCloudRegionId")
86         if (cloudSiteId == null) {
87             String cloudConfiguration = execution.getVariable("cloudConfiguration")
88             cloudSiteId = jsonUtil.getJsonValue(cloudConfiguration, "cloudConfiguration.lcpCloudRegionId")
89             def cloudOwner = jsonUtil.getJsonValue(cloudConfiguration, "cloudConfiguration.cloudOwner")
90             execution.setVariable("lcpCloudRegionId", cloudSiteId)
91             execution.setVariable("cloudOwner", cloudOwner)
92         }
93
94         // Extract attributes from modelInfo
95         String vfModuleModelInfo = execution.getVariable("vfModuleModelInfo")
96
97         //modelCustomizationUuid
98         def modelCustomizationUuid = jsonUtil.getJsonValue(vfModuleModelInfo, "modelCustomizationUuid")
99         execution.setVariable("modelCustomizationId", modelCustomizationUuid)
100         logger.debug("modelCustomizationId: " + modelCustomizationUuid)
101
102         //modelName
103         def modelName = jsonUtil.getJsonValue(vfModuleModelInfo, "modelName")
104         execution.setVariable("modelName", modelName)
105         logger.debug("modelName: " + modelName)
106
107         // The following is used on the get Generic Service Instance call
108         execution.setVariable('GENGS_type', 'service-instance')
109     }
110
111
112     /**
113      * Display input variables
114      * @param execution
115      * @param isDebugLogEnabled
116      */
117     public void displayInput(DelegateExecution execution, isDebugLogEnabled) {
118         def input = ['mso-request-id', 'msoRequestId', 'isDebugLogEnabled', 'disableRollback', 'failIfExists', 'serviceInstanceId', 'vnfId', 'vnfName', 'tenantId', 'volumeGroupId', 'volumeGroupName', 'lcpCloudRegionId', 'vnfType', 'vfModuleModelInfo', 'asdcServiceModelVersion', 'test-volume-group-name', 'test-volume-group-id', 'vfModuleInputParams']
119
120         logger.debug('Begin input: ')
121         input.each {
122             logger.debug(it + ': ' + execution.getVariable(it))
123         }
124         logger.debug('End input.')
125     }
126
127
128     /**
129      * Define and set rollbackdata object
130      * @param execution
131      * @param isDebugEnabled
132      */
133     public void setRollbackData(DelegateExecution execution, isDebugEnabled) {
134         def rollbackData = execution.getVariable("rollbackData")
135         if (rollbackData == null) {
136             rollbackData = new RollbackData()
137         }
138         def volumeGroupName = execution.getVariable('volumeGroupName')
139         rollbackData.put("DCVFMODULEVOL", "volumeGroupName", volumeGroupName)
140         execution.setVariable("rollbackData", rollbackData)
141     }
142
143
144     /**
145      * Gets the service instance uri from aai
146      */
147     public void getServiceInstance(DelegateExecution execution) {
148         try {
149             String serviceInstanceId = execution.getVariable('serviceInstanceId')
150
151             AAIResourcesClient resourceClient = new AAIResourcesClient()
152             AAIResourceUri uri = AAIUriFactory.createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstanceId))
153
154             if(!resourceClient.exists(uri)){
155                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, "Service instance was not found in aai")
156             }
157         }catch(BpmnError e) {
158             throw e
159         }catch (Exception ex){
160             String msg = "Exception in getServiceInstance. " + ex.getMessage()
161             logger.debug(msg)
162             (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, msg)
163         }
164     }
165
166     /**
167      * Get cloud region
168      * @param execution
169      * @param isDebugEnabled
170      */
171     public void callRESTQueryAAICloudRegion (DelegateExecution execution, isDebugEnabled) {
172
173         def cloudRegion = execution.getVariable("lcpCloudRegionId")
174         logger.debug('Request cloud region is: ' + cloudRegion)
175
176         AaiUtil aaiUtil = new AaiUtil(this)
177
178         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), cloudRegion))
179         def queryCloudRegionRequest = aaiUtil.createAaiUri(uri)
180
181         cloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
182
183         def aaiCloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "AAI", cloudRegion)
184         if ((aaiCloudRegion != "ERROR")) {
185             execution.setVariable("lcpCloudRegionId", aaiCloudRegion)
186             logger.debug("AIC Cloud Region for AAI: " + aaiCloudRegion)
187         } else {
188             String errorMessage = "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode")
189             logger.debug(errorMessage)
190             (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, errorMessage)
191         }
192
193         def poCloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
194         if ((poCloudRegion != "ERROR")) {
195             execution.setVariable("poLcpCloudRegionId", poCloudRegion)
196             logger.debug("AIC Cloud Region for PO: " + poCloudRegion)
197         } else {
198             String errorMessage = "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode")
199             logger.debug(errorMessage)
200             (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, errorMessage)
201         }
202
203         def rollbackData = execution.getVariable("rollbackData")
204         rollbackData.put("DCVFMODULEVOL", "aiccloudregion", cloudRegion)
205     }
206
207
208     /**
209      * Query AAI volume group by name
210      * @param execution
211      * @param isDebugEnabled
212      */
213     public void callRESTQueryAAIVolGrpName(DelegateExecution execution, isDebugEnabled) {
214
215         def volumeGroupName = execution.getVariable('volumeGroupName')
216         def cloudRegion = execution.getVariable('lcpCloudRegionId')
217
218         try {
219             AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(Defaults.CLOUD_OWNER.toString(), cloudRegion).volumeGroups()).queryParam("volume-group-name", volumeGroupName)
220             Optional<VolumeGroups> volumeGroups = getAAIClient().get(VolumeGroups.class,uri)
221             if(volumeGroups.isPresent()){
222                 VolumeGroup volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
223                 execution.setVariable(prefix+'AaiReturnCode', 200)
224                 execution.setVariable("queriedVolumeGroupId",volumeGroup.getVolumeGroupId())
225                 logger.debug("Volume Group Name $volumeGroupName exists in AAI.")
226             }else{
227                 execution.setVariable(prefix+'AaiReturnCode', 404)
228                 exceptionUtil.buildAndThrowWorkflowException(execution,25000, "Volume Group Name $volumeGroupName does not exist in AAI.")
229             }
230         }catch(BpmnError error){
231             throw error
232         }catch(Exception e){
233             execution.setVariable(prefix+'AaiReturnCode', 500)
234             exceptionUtil.buildAndThrowWorkflowException(execution,25000, "Exception in get volume group by name: " + e.getMessage())
235         }
236     }
237
238
239     /**
240      * Create a WorkflowException
241      * @param execution
242      * @param isDebugEnabled
243      */
244     public void buildWorkflowException(DelegateExecution execution, int errorCode, errorMessage) {
245         logger.debug(errorMessage)
246         (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)
247     }
248
249
250     /**
251      * Create a WorkflowException
252      * @param execution
253      * @param isDebugEnabled
254      */
255     public void handleError(DelegateExecution execution, isDebugEnabled) {
256         WorkflowException we = execution.getVariable('WorkflowException')
257         if (we == null) {
258             (new ExceptionUtil()).buildWorkflowException(execution, 2500, "Enexpected error encountered!")
259         }
260         throw new BpmnError("MSOWorkflowException")
261     }
262
263
264     /**
265      * Create volume group in AAI
266      * @param execution
267      * @param isDebugEnabled
268      */
269     public void callRESTCreateAAIVolGrpName(DelegateExecution execution, isDebugEnabled) {
270
271         def vnfId = execution.getVariable('vnfId')
272         def volumeGroupId = execution.getVariable('volumeGroupId')
273         def volumeName = execution.getVariable("volumeGroupName")
274         def modelCustomizationId = execution.getVariable("modelCustomizationId")
275         def vnfType = execution.getVariable("vnfType")
276         def tenantId = execution.getVariable("tenantId")
277         def cloudRegion = execution.getVariable('lcpCloudRegionId')
278         def cloudOwner = execution.getVariable('cloudOwner')
279
280         def testGroupId = execution.getVariable('test-volume-group-id')
281         if (testGroupId != null && testGroupId.trim() != '') {
282             logger.debug("test volumeGroupId is present: " + testGroupId)
283             volumeGroupId = testGroupId
284             execution.setVariable("test-volume-group-name", "MSOTESTVOL101a-vSAMP12_base_vol_module-0")
285         }
286
287         VolumeGroup volumeGroup = new VolumeGroup()
288         volumeGroup.setVolumeGroupId(volumeGroupId)
289         volumeGroup.setVolumeGroupName(volumeName)
290         volumeGroup.setVnfType(vnfType)
291         volumeGroup.setOrchestrationStatus(OrchestrationStatus.PENDING.toString())
292         volumeGroup.setModelCustomizationId(modelCustomizationId)
293
294         logger.debug("volumeGroupId to be used: " + volumeGroupId)
295
296         AAIResourceUri volumeGroupUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(cloudOwner, cloudRegion).volumeGroup(volumeGroupId))
297         AAIResourceUri tenantUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(cloudOwner, cloudRegion).tenant(tenantId))
298         AAIResourceUri vnfUri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))
299         try {
300             getAAIClient().create(volumeGroupUri, volumeGroup)
301             getAAIClient().connect(volumeGroupUri, vnfUri)
302             getAAIClient().connect(volumeGroupUri, tenantUri)
303             execution.setVariable("queriedVolumeGroupId", volumeGroupId)
304             RollbackData rollbackData = execution.getVariable("rollbackData")
305             rollbackData.put("DCVFMODULEVOL", "isAAIRollbackNeeded", "true")
306         } catch (NotFoundException ignored) {
307             execution.setVariable(prefix + "isErrorMessageException", true)
308             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Unable to create volume group in AAI. Response code: 404")
309         } catch (Exception ex) {
310             execution.setVariable(prefix + "isErrorMessageException", true)
311             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, ex.getMessage())
312         }
313     }
314
315     /**
316      * Prepare VNF adapter create request XML
317      * @param execution
318      */
319     public void prepareVnfAdapterCreateRequest(DelegateExecution execution, isDebugEnabled) {
320
321         GenericVnf aaiGenericVnfResponse = execution.getVariable(prefix+'AAIQueryGenericVfnResponse')
322         def vnfId = aaiGenericVnfResponse.getVnfId()
323         def vnfName = aaiGenericVnfResponse.getVnfName()
324         def vnfType = aaiGenericVnfResponse.getVnfType()
325
326         def requestId = execution.getVariable('msoRequestId')
327         def serviceId = execution.getVariable('serviceInstanceId')
328         def cloudSiteId = execution.getVariable('poLcpCloudRegionId')
329         def tenantId = execution.getVariable('tenantId')
330         def volumeGroupId = execution.getVariable('volumeGroupId')
331         def volumeGroupnName = execution.getVariable('volumeGroupName')
332
333         def vnfVersion = execution.getVariable("asdcServiceModelVersion")
334         def vnfModuleType = execution.getVariable("modelName")
335
336         def modelCustomizationId = execution.getVariable("modelCustomizationId")
337
338         // for testing
339         logger.debug("volumeGroupId: " + volumeGroupId)
340         def testGroupId = execution.getVariable('test-volume-group-id')
341         if (testGroupId != null && testGroupId.trim() != '') {
342             logger.debug("test volumeGroupId is present: " + testGroupId)
343             volumeGroupId = testGroupId
344             execution.setVariable("test-volume-group-name", "MSOTESTVOL101a-vSAMP12_base_vol_module-0")
345         }
346         logger.debug("volumeGroupId to be used: " + volumeGroupId)
347
348         // volume group parameters
349
350         String volumeGroupParams = ''
351         StringBuilder sbParams = new StringBuilder()
352         Map<String, String> paramsMap = execution.getVariable("vfModuleInputParams")
353         for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
354             String paramsXml
355             String paramName = entry.getKey()
356             String paramValue = entry.getValue()
357             paramsXml =
358                     """ <entry>
359                            <key>${MsoUtils.xmlEscape(paramName)}</key>
360                            <value>${MsoUtils.xmlEscape(paramValue)}</value>
361                         </entry>
362                         """
363             sbParams.append(paramsXml)
364         }
365
366         volumeGroupParams = sbParams.toString()
367         logger.debug("volumeGroupParams: "+ volumeGroupParams)
368
369         def backoutOnFailure = execution.getVariable(prefix+"backoutOnFailure")
370         logger.debug("backoutOnFailure: "+ backoutOnFailure)
371
372         def failIfExists = execution.getVariable("failIfExists")
373         if(failIfExists == null) {
374             failIfExists = 'true'
375         }
376
377         String messageId = UUID.randomUUID()
378         logger.debug("messageId to be used is generated: " + messageId)
379
380         def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId)
381         def useQualifiedHostName = UrnPropertiesReader.getVariable("mso.use.qualified.host",execution)
382         if ('true'.equals(useQualifiedHostName)) {
383             notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
384         }
385         logger.debug("CreateVfModuleVolume - notificationUrl: "+ notificationUrl)
386
387         // build request
388         String vnfSubCreateWorkflowRequest =
389                 """
390                         <createVolumeGroupRequest>
391                                 <cloudSiteId>${MsoUtils.xmlEscape(cloudSiteId)}</cloudSiteId>
392                                 <tenantId>${MsoUtils.xmlEscape(tenantId)}</tenantId>
393                                 <vnfId>${MsoUtils.xmlEscape(vnfId)}</vnfId>
394                                 <vnfName>${MsoUtils.xmlEscape(vnfName)}</vnfName>
395                                 <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
396                                 <volumeGroupName>${MsoUtils.xmlEscape(volumeGroupnName)}</volumeGroupName>
397                                 <vnfType>${MsoUtils.xmlEscape(vnfType)}</vnfType>
398                                 <vnfVersion>${MsoUtils.xmlEscape(vnfVersion)}</vnfVersion>
399                                 <vfModuleType>${MsoUtils.xmlEscape(vnfModuleType)}</vfModuleType>
400                                 <modelCustomizationUuid>${MsoUtils.xmlEscape(modelCustomizationId)}</modelCustomizationUuid>
401                                 <volumeGroupParams>
402                                         <entry>
403                                                 <key>vnf_id</key>
404                                                 <value>${MsoUtils.xmlEscape(vnfId)}</value>
405                                         </entry>
406                                         <entry>
407                                                 <key>vnf_name</key>
408                                                 <value>${MsoUtils.xmlEscape(vnfName)}</value>
409                                         </entry>
410                                         <entry>
411                                                 <key>vf_module_id</key>
412                                                 <value>${MsoUtils.xmlEscape(volumeGroupId)}</value>
413                                         </entry>
414                                         <entry>
415                                                 <key>vf_module_name</key>
416                                                 <value>${MsoUtils.xmlEscape(volumeGroupnName)}</value>
417                                         </entry>
418                                         ${volumeGroupParams}
419                             </volumeGroupParams>
420                                 <skipAAI>true</skipAAI>
421                                 <backout>${MsoUtils.xmlEscape(backoutOnFailure)}</backout>
422                                 <failIfExists>${MsoUtils.xmlEscape(failIfExists)}</failIfExists>
423                             <msoRequest>
424                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
425                                 <serviceInstanceId>${MsoUtils.xmlEscape(serviceId)}</serviceInstanceId>
426                             </msoRequest>
427                             <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
428                             <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
429                         </createVolumeGroupRequest>
430                 """
431
432         String vnfSubCreateWorkflowRequestAsString = utils.formatXml(vnfSubCreateWorkflowRequest)
433         logger.debug(vnfSubCreateWorkflowRequestAsString)
434         logger.debug(vnfSubCreateWorkflowRequestAsString)
435         execution.setVariable(prefix+"createVnfARequest", vnfSubCreateWorkflowRequestAsString)
436
437         // build rollback request for use later if needed
438         String vnfSubRollbackWorkflowRequest = buildRollbackVolumeGroupRequestXml(volumeGroupId, cloudSiteId, tenantId, requestId, serviceId, messageId, notificationUrl)
439
440         logger.debug("Sub Vnf flow rollback request: vnfSubRollbackWorkflowRequest " + "\n" + vnfSubRollbackWorkflowRequest)
441
442         String vnfSubRollbackWorkflowRequestAsString = utils.formatXml(vnfSubRollbackWorkflowRequest)
443         execution.setVariable(prefix+"rollbackVnfARequest", vnfSubRollbackWorkflowRequestAsString)
444     }
445
446     public String buildRollbackVolumeGroupRequestXml(volumeGroupId, cloudSiteId, tenantId, requestId, serviceId, messageId, notificationUrl) {
447
448         String request = """
449                 <rollbackVolumeGroupRequest>
450                         <volumeGroupRollback>
451                            <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
452                            <volumeGroupStackId>{{VOLUMEGROUPSTACKID}}</volumeGroupStackId>
453                            <tenantId>${MsoUtils.xmlEscape(tenantId)}</tenantId>
454                            <cloudSiteId>${MsoUtils.xmlEscape(cloudSiteId)}</cloudSiteId>
455                            <volumeGroupCreated>true</volumeGroupCreated>
456                            <msoRequest>
457                               <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
458                               <serviceInstanceId>${MsoUtils.xmlEscape(serviceId)}</serviceInstanceId>
459                            </msoRequest>
460                            <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
461                         </volumeGroupRollback>
462                         <skipAAI>true</skipAAI>
463                         <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
464                 </rollbackVolumeGroupRequest>
465                 """
466
467         return request
468     }
469
470     public String updateRollbackVolumeGroupRequestXml(String rollabackRequest, String heatStackId) {
471         String newRequest = rollabackRequest.replace("{{VOLUMEGROUPSTACKID}}", heatStackId)
472         return newRequest
473     }
474
475     /**
476      * Validate VNF adapter response
477      * @param execution
478      */
479     public void validateVnfResponse(DelegateExecution execution, isDebugEnabled) {
480         def vnfSuccess = execution.getVariable('VNFREST_SuccessIndicator')
481         logger.debug("vnfAdapterSuccessIndicator: "+ vnfSuccess)
482         if(vnfSuccess==true) {
483             String createVnfAResponse = execution.getVariable(prefix+"createVnfAResponse")
484             String heatStackID = utils.getNodeText(createVnfAResponse, "volumeGroupStackId")
485             String vnfRollbackRequest = execution.getVariable(prefix+"rollbackVnfARequest")
486             String updatedVnfRollbackRequest = updateRollbackVolumeGroupRequestXml(vnfRollbackRequest, heatStackID)
487             logger.debug("vnfAdapter rollback request: "+ updatedVnfRollbackRequest)
488             RollbackData rollbackData = execution.getVariable("rollbackData")
489             rollbackData.put("DCVFMODULEVOL", "rollbackVnfARequest", updatedVnfRollbackRequest)
490             rollbackData.put("DCVFMODULEVOL", "isCreateVnfRollbackNeeded", "true")
491         }
492     }
493
494
495     /**
496      * Update voulume group in AAI
497      * @TODO: Can we re-use the create method??
498      * @param execution
499      * @param isDebugEnabled
500      */
501     public void callRESTUpdateCreatedVolGrpName(DelegateExecution execution, isDebugEnabled) {
502         String volumeGroupId = execution.getVariable("queriedVolumeGroupId")
503         String modelCustomizationId = execution.getVariable("modelCustomizationId")
504         String cloudRegion = execution.getVariable("lcpCloudRegionId")
505         String cloudOwner = execution.getVariable('cloudOwner')
506         String createVnfAResponse = execution.getVariable(prefix+"createVnfAResponse")
507         def heatStackID = utils.getNodeText(createVnfAResponse, "volumeGroupStackId")
508         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure().cloudRegion(cloudOwner, cloudRegion).volumeGroup(volumeGroupId))
509
510         execution.setVariable(prefix+"heatStackId", heatStackID)
511
512         VolumeGroup volumeGroup = new VolumeGroup()
513         volumeGroup.setHeatStackId(heatStackID)
514         volumeGroup.setModelCustomizationId(modelCustomizationId)
515         try {
516             getAAIClient().update(uri, volumeGroup)
517             execution.setVariable(prefix+"isPONR", true)
518         }catch(NotFoundException ignored){
519             execution.setVariable(prefix+"isErrorMessageException", true)
520             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Unable to update volume group in AAI. Response code: 404")
521         }catch(BpmnError error){
522             throw error
523         }catch(Exception e){
524             execution.setVariable(prefix+"isErrorMessageException", true)
525             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "AAI Adapter Query Failed. "+ e.getMessage())
526         }
527     }
528
529
530     /**
531      * Query AAI Generic VNF
532      * @param execution
533      * @param isDebugEnabled
534      */
535     public void callRESTQueryAAIGenericVnf(DelegateExecution execution, isDebugEnabled) {
536
537         def vnfId = execution.getVariable('vnfId')
538         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().genericVnf(vnfId))
539         try {
540             Optional<GenericVnf> genericVnf = getAAIClient().get(GenericVnf.class, uri)
541             if (genericVnf.isPresent()) {
542                 execution.setVariable(prefix + 'AAIQueryGenericVfnResponse', genericVnf.get())
543             } else {
544                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, 'Generic vnf ' + vnfId + ' was not found in AAI. Return code: 404.')
545             }
546         }catch(Exception e){
547             exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Exception in get generic VNF: " + e.getMessage())
548         }
549     }
550
551 }