Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / 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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
23 import org.apache.commons.lang3.*
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution
26 import org.onap.so.bpmn.common.scripts.AaiUtil
27 import org.onap.so.bpmn.common.scripts.ExceptionUtil
28 import org.onap.so.bpmn.common.scripts.MsoUtils
29 import org.onap.so.bpmn.common.scripts.NetworkUtils
30 import org.onap.so.bpmn.common.scripts.VfModuleBase;
31 import org.onap.so.bpmn.core.RollbackData
32 import org.onap.so.bpmn.core.UrnPropertiesReader
33 import org.onap.so.bpmn.core.WorkflowException
34 import org.onap.so.bpmn.core.json.JsonUtils;
35 import org.onap.so.logger.MsoLogger
36 import org.onap.so.rest.APIResponse
37 import org.springframework.web.util.UriUtils
38
39 class DoCreateVfModuleVolumeV2 extends VfModuleBase {
40
41         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateVfModuleVolumeV2.class);
42         String prefix='DCVFMODVOLV2_'
43         JsonUtils jsonUtil = new JsonUtils()
44
45
46     /**
47      * Perform initial processing, such as request validation, initialization of variables, etc.
48      * * @param execution
49      */
50     public void preProcessRequest(DelegateExecution execution) {
51         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
52         preProcessRequest(execution, isDebugEnabled)
53     }
54
55     public void preProcessRequest(DelegateExecution execution, isDebugLogEnabled) {
56                 
57                 execution.setVariable("prefix",prefix)
58                 execution.setVariable(prefix+'SuccessIndicator', false)
59                 execution.setVariable(prefix+'isPONR', false)
60
61                 displayInput(execution, isDebugLogEnabled)
62                 setRollbackData(execution, isDebugLogEnabled)
63                 setRollbackEnabled(execution, isDebugLogEnabled) 
64                 
65                 
66                 def tenantId = execution.getVariable("tenantId")
67                 if (tenantId == null) {
68                         String cloudConfiguration = execution.getVariable("cloudConfiguration")
69                         tenantId = jsonUtil.getJsonValue(cloudConfiguration, "cloudConfiguration.tenantId")
70                         execution.setVariable("tenantId", tenantId)
71                 }
72
73                 def cloudSiteId = execution.getVariable("lcpCloudRegionId")
74                 if (cloudSiteId == null) {
75                         String cloudConfiguration = execution.getVariable("cloudConfiguration")
76                         cloudSiteId = jsonUtil.getJsonValue(cloudConfiguration, "cloudConfiguration.lcpCloudRegionId")
77                         execution.setVariable("lcpCloudRegionId", cloudSiteId)
78                 }
79
80                 // Extract attributes from modelInfo
81                 String vfModuleModelInfo = execution.getVariable("vfModuleModelInfo")
82
83                 //modelCustomizationUuid
84                 def modelCustomizationUuid = jsonUtil.getJsonValue(vfModuleModelInfo, "modelCustomizationUuid")
85                 execution.setVariable("modelCustomizationId", modelCustomizationUuid)
86                 msoLogger.debug("modelCustomizationId: " + modelCustomizationUuid)
87                 
88                 //modelName
89                 def modelName = jsonUtil.getJsonValue(vfModuleModelInfo, "modelName")
90                 execution.setVariable("modelName", modelName)
91                 msoLogger.debug("modelName: " + modelName)
92                 
93                 // The following is used on the get Generic Service Instance call
94                 execution.setVariable('GENGS_type', 'service-instance')
95         }
96
97         
98         /**
99          * Display input variables
100          * @param execution
101          * @param isDebugLogEnabled
102          */
103         public void displayInput(DelegateExecution execution, isDebugLogEnabled) {
104                 def input = ['mso-request-id', 'msoRequestId', 'isDebugLogEnabled', 'disableRollback', 'failIfExists', 'serviceInstanceId',
105                         'vnfId', 'vnfName', 'tenantId', 'volumeGroupId', 'volumeGroupName', 'lcpCloudRegionId', 'vnfType', 'vfModuleModelInfo',  'asdcServiceModelVersion',
106                         'test-volume-group-name', 'test-volume-group-id', 'vfModuleInputParams']
107
108                 msoLogger.debug('Begin input: ')
109                 input.each {
110                         msoLogger.debug(it + ': ' + execution.getVariable(it))
111                 }
112                 msoLogger.debug('End input.')
113         }
114         
115         
116         /**
117          * Define and set rollbackdata object 
118          * @param execution
119          * @param isDebugEnabled
120          */
121         public void setRollbackData(DelegateExecution execution, isDebugEnabled) {
122                 def rollbackData = execution.getVariable("rollbackData")
123                 if (rollbackData == null) {
124                         rollbackData = new RollbackData()
125                 }
126                 def volumeGroupName = execution.getVariable('volumeGroupName')
127                 rollbackData.put("DCVFMODULEVOL", "volumeGroupName", volumeGroupName)
128                 execution.setVariable("rollbackData", rollbackData)
129         }
130         
131                 
132         /**
133          * validate getServiceInstance response
134          * @param execution
135          * @param isDebugEnabled
136          */
137         public void validateGetServiceInstanceCall(DelegateExecution execution, isDebugEnabled) {
138                 def found = execution.getVariable('GENGS_FoundIndicator')
139                 def success = execution.getVariable('GENGS_SuccessIndicator')
140                 def serviceInstanceId = execution.getVariable('serviceInstanceId')
141                 msoLogger.debug("getServiceInstance success: " + success)
142                 msoLogger.debug("getServiceInstance found: " + found)
143                 if(!found || !success) {
144                         String errorMessage = "Service instance id not found in AAI: ${serviceInstanceId}."
145                         msoLogger.debug(errorMessage)
146                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, errorMessage)
147                 }
148                 
149         }
150
151         /**
152          * Get cloud region
153          * @param execution
154          * @param isDebugEnabled
155          */
156         public void callRESTQueryAAICloudRegion (DelegateExecution execution, isDebugEnabled) {
157
158                 def cloudRegion = execution.getVariable("lcpCloudRegionId")
159                 msoLogger.debug('Request cloud region is: ' + cloudRegion)
160
161                 AaiUtil aaiUtil = new AaiUtil(this)
162                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
163                 String queryCloudRegionRequest = aaiEndpoint + '/' + cloudRegion
164
165                 msoLogger.debug(queryCloudRegionRequest)
166
167                 cloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
168
169                 def aaiCloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "AAI", cloudRegion)
170                 if ((aaiCloudRegion != "ERROR")) {
171                         execution.setVariable("lcpCloudRegionId", aaiCloudRegion)
172                         msoLogger.debug("AIC Cloud Region for AAI: " + aaiCloudRegion)
173                 } else {
174                         String errorMessage = "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode")
175                         msoLogger.debug(errorMessage)
176                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, errorMessage)
177                 }
178                 
179                 def poCloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
180                 if ((poCloudRegion != "ERROR")) {
181                         execution.setVariable("poLcpCloudRegionId", poCloudRegion)
182                         msoLogger.debug("AIC Cloud Region for PO: " + poCloudRegion)
183                 } else {
184                         String errorMessage = "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode")
185                         msoLogger.debug(errorMessage)
186                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, errorMessage)
187                 }
188                 
189                 def rollbackData = execution.getVariable("rollbackData")
190                 rollbackData.put("DCVFMODULEVOL", "aiccloudregion", cloudRegion)
191         }
192
193
194         /**
195          * Query AAI volume group by name
196          * @param execution
197          * @param isDebugEnabled
198          */
199         public void callRESTQueryAAIVolGrpName(DelegateExecution execution, isDebugEnabled) {
200
201                 def volumeGroupName = execution.getVariable('volumeGroupName')
202                 def cloudRegion = execution.getVariable('lcpCloudRegionId')
203
204                 // This is for stub testing
205                 def testVolumeGroupName = execution.getVariable('test-volume-group-name')
206                 if (testVolumeGroupName != null && testVolumeGroupName.length() > 0) {
207                         volumeGroupName = testVolumeGroupName
208                 }
209
210                 AaiUtil aaiUtil = new AaiUtil(this)
211                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
212                 String queryAAIVolumeNameRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups" + "?volume-group-name=" + UriUtils.encode(volumeGroupName, 'UTF-8')
213
214                 msoLogger.debug('Query AAI volume group by name: ' + queryAAIVolumeNameRequest)
215
216                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIVolumeNameRequest)
217
218                 String returnCode = response.getStatusCode()
219                 String aaiResponseAsString = response.getResponseBodyAsString()
220
221                 msoLogger.debug("AAI query volume group by name return code: " + returnCode)
222                 msoLogger.debug("AAI query volume group by name response: " + aaiResponseAsString)
223
224                 ExceptionUtil exceptionUtil = new ExceptionUtil()
225
226                 execution.setVariable(prefix+"queryAAIVolGrpNameResponse", aaiResponseAsString)
227                 execution.setVariable(prefix+'AaiReturnCode', returnCode)
228
229                 if (returnCode=='200') {
230                         execution.setVariable(prefix+'queryAAIVolGrpNameResponse', aaiResponseAsString)
231                         msoLogger.debug("Volume Group Name $volumeGroupName exists in AAI.")
232                 } else {
233                         if (returnCode=='404') {
234                                 msoLogger.debug("Volume Group Name $volumeGroupName does not exist in AAI.")
235                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupName not found in AAI. Response code: 404")
236                         } else {
237                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
238                                 throw new BpmnError("MSOWorkflowException")
239                         }
240                 }
241         }
242
243
244         /**
245          * Create a WorkflowException 
246          * @param execution
247          * @param isDebugEnabled
248          */
249         public void buildWorkflowException(DelegateExecution execution, int errorCode, errorMessage, isDebugEnabled) {
250                 msoLogger.debug(errorMessage)
251                 (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)
252         }
253
254
255         /**
256          * Create a WorkflowException
257          * @param execution
258          * @param isDebugEnabled
259          */
260         public void handleError(DelegateExecution execution, isDebugEnabled) {
261                 WorkflowException we = execution.getVariable('WorkflowException')
262                 if (we == null) {
263                         (new ExceptionUtil()).buildWorkflowException(execution, 2500, "Enexpected error encountered!")
264                 }
265                 throw new BpmnError("MSOWorkflowException")
266         }
267
268         
269         /**
270          * Create volume group in AAI
271          * @param execution
272          * @param isDebugEnabled
273          */
274         public void callRESTCreateAAIVolGrpName(DelegateExecution execution, isDebugEnabled) {
275
276                 def vnfId = execution.getVariable('vnfId')
277                 def volumeGroupId = execution.getVariable('volumeGroupId')
278                 def volumeName = execution.getVariable("volumeGroupName")
279                 def modelCustomizationId = execution.getVariable("modelCustomizationId")
280                 def vnfType = execution.getVariable("vnfType")
281                 def tenantId = execution.getVariable("tenantId")
282                 def cloudRegion = execution.getVariable('lcpCloudRegionId')
283                 
284                 msoLogger.debug("volumeGroupId: " + volumeGroupId)
285
286                 def testGroupId = execution.getVariable('test-volume-group-id')
287                 if (testGroupId != null && testGroupId.trim() != '') {
288                         msoLogger.debug("test volumeGroupId is present: " + testGroupId)
289                         volumeGroupId = testGroupId
290                         execution.setVariable("test-volume-group-name", "MSOTESTVOL101a-vSAMP12_base_vol_module-0")
291                 }
292
293                 msoLogger.debug("volumeGroupId to be used: " + volumeGroupId)
294
295                 AaiUtil aaiUtil = new AaiUtil(this)
296                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
297                 String createAAIVolumeGrpNameUrlRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups/volume-group/" + UriUtils.encode(volumeGroupId, "UTF-8")
298
299                 String namespace =  aaiUtil.getNamespaceFromUri(aaiUtil.getCloudInfrastructureCloudRegionUri(execution))
300                 msoLogger.debug("AAI namespace is: " + namespace)
301
302                 msoLogger.debug("Request URL for PUT: " + createAAIVolumeGrpNameUrlRequest)
303
304                 NetworkUtils networkUtils = new NetworkUtils()
305                 String payload = networkUtils.createCloudRegionVolumeRequest(volumeGroupId, volumeName, vnfType, vnfId, tenantId, cloudRegion, namespace, modelCustomizationId)
306                 String payloadXml = utils.formatXml(payload)
307                 msoLogger.debug("Request payload for PUT: " + payloadXml)
308
309                 APIResponse response = aaiUtil.executeAAIPutCall(execution, createAAIVolumeGrpNameUrlRequest, payloadXml)
310
311                 String returnCode = response.getStatusCode()
312                 String aaiResponseAsString = response.getResponseBodyAsString()
313
314                 msoLogger.debug("AAI create volume group return code: " + returnCode)
315                 msoLogger.debug("AAI create volume group response: " + aaiResponseAsString)
316
317                 execution.setVariable(prefix+"createAAIVolumeGrpNameReturnCode", returnCode)
318                 execution.setVariable(prefix+"createAAIVolumeGrpNameResponse", aaiResponseAsString)
319                 
320                 ExceptionUtil exceptionUtil = new ExceptionUtil()
321
322                 if (returnCode =='201') {
323                         RollbackData rollbackData = execution.getVariable("rollbackData")
324                         rollbackData.put("DCVFMODULEVOL", "isAAIRollbackNeeded", "true")
325                 } else {
326                         execution.setVariable(prefix+"isErrorMessageException", true)
327                         if (returnCode=='404') {
328                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Unable to create volume group in AAI. Response code: 404")
329                         } else {
330                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
331                                 msoLogger.debug(" AAI Adapter Query Failed.  WorkflowException - " + "\n" + aWorkflowException)
332                                 throw new BpmnError("MSOWorkflowException")
333                         }
334                 }
335         }
336         
337
338         /**
339          * Prepare VNF adapter create request XML
340          * @param execution
341          */
342         public void prepareVnfAdapterCreateRequest(DelegateExecution execution, isDebugEnabled) {
343
344                 def aaiGenericVnfResponse = execution.getVariable(prefix+'AAIQueryGenericVfnResponse')
345                 def vnfId = utils.getNodeText(aaiGenericVnfResponse, 'vnf-id')
346                 def vnfName = utils.getNodeText(aaiGenericVnfResponse, 'vnf-name')
347                 def vnfType = utils.getNodeText(aaiGenericVnfResponse, "vnf-type")
348                 
349                 def requestId = execution.getVariable('msoRequestId')
350                 def serviceId = execution.getVariable('serviceInstanceId')
351                 def cloudSiteId = execution.getVariable('poLcpCloudRegionId')
352                 def tenantId = execution.getVariable('tenantId')
353                 def volumeGroupId = execution.getVariable('volumeGroupId')
354                 def volumeGroupnName = execution.getVariable('volumeGroupName')
355
356                 def vnfVersion = execution.getVariable("asdcServiceModelVersion") 
357                 def vnfModuleType = execution.getVariable("modelName")
358
359                 def modelCustomizationId = execution.getVariable("modelCustomizationId")
360                 
361                 // for testing
362                 msoLogger.debug("volumeGroupId: " + volumeGroupId)
363                 def testGroupId = execution.getVariable('test-volume-group-id')
364                 if (testGroupId != null && testGroupId.trim() != '') {
365                         msoLogger.debug("test volumeGroupId is present: " + testGroupId)
366                         volumeGroupId = testGroupId
367                         execution.setVariable("test-volume-group-name", "MSOTESTVOL101a-vSAMP12_base_vol_module-0")
368                 }
369                 msoLogger.debug("volumeGroupId to be used: " + volumeGroupId)
370                                 
371                 // volume group parameters
372                 
373                 String volumeGroupParams = ''
374                 StringBuilder sbParams = new StringBuilder()
375                 Map<String, String> paramsMap = execution.getVariable("vfModuleInputParams")
376                 for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
377                         String paramsXml
378                         String paramName = entry.getKey();
379                         String paramValue = entry.getValue()
380                         paramsXml =
381                                 """     <entry>
382                            <key>${MsoUtils.xmlEscape(paramName)}</key>
383                            <value>${MsoUtils.xmlEscape(paramValue)}</value>
384                         </entry>
385                         """
386                         sbParams.append(paramsXml)
387                 }
388
389                 volumeGroupParams = sbParams.toString()
390                 msoLogger.debug("volumeGroupParams: "+ volumeGroupParams)
391
392                 def backoutOnFailure = execution.getVariable(prefix+"backoutOnFailure")
393                 msoLogger.debug("backoutOnFailure: "+ backoutOnFailure)
394
395                 def failIfExists = execution.getVariable("failIfExists")
396                 if(failIfExists == null) {
397                         failIfExists = 'true'
398                 }
399                 
400                 String messageId = UUID.randomUUID()
401                 msoLogger.debug("messageId to be used is generated: " + messageId)
402                 
403                 def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId)
404                 def useQualifiedHostName = UrnPropertiesReader.getVariable("mso.use.qualified.host",execution)
405                 if ('true'.equals(useQualifiedHostName)) {
406                         notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
407                 }
408                 msoLogger.debug("CreateVfModuleVolume - notificationUrl: "+ notificationUrl)
409                 
410                 // build request
411                 String vnfSubCreateWorkflowRequest =
412                                 """
413                         <createVolumeGroupRequest>
414                                 <cloudSiteId>${MsoUtils.xmlEscape(cloudSiteId)}</cloudSiteId>
415                                 <tenantId>${MsoUtils.xmlEscape(tenantId)}</tenantId>
416                                 <vnfId>${MsoUtils.xmlEscape(vnfId)}</vnfId>
417                                 <vnfName>${MsoUtils.xmlEscape(vnfName)}</vnfName>
418                                 <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
419                                 <volumeGroupName>${MsoUtils.xmlEscape(volumeGroupnName)}</volumeGroupName>
420                                 <vnfType>${MsoUtils.xmlEscape(vnfType)}</vnfType>
421                                 <vnfVersion>${MsoUtils.xmlEscape(vnfVersion)}</vnfVersion>
422                                 <vfModuleType>${MsoUtils.xmlEscape(vnfModuleType)}</vfModuleType>
423                                 <modelCustomizationUuid>${MsoUtils.xmlEscape(modelCustomizationId)}</modelCustomizationUuid>
424                                 <volumeGroupParams>
425                                         <entry>
426                                                 <key>vnf_id</key>
427                                                 <value>${MsoUtils.xmlEscape(vnfId)}</value>
428                                         </entry>
429                                         <entry>
430                                                 <key>vnf_name</key>
431                                                 <value>${MsoUtils.xmlEscape(vnfName)}</value>
432                                         </entry>
433                                         <entry>
434                                                 <key>vf_module_id</key>
435                                                 <value>${MsoUtils.xmlEscape(volumeGroupId)}</value>
436                                         </entry>
437                                         <entry>
438                                                 <key>vf_module_name</key>
439                                                 <value>${MsoUtils.xmlEscape(volumeGroupnName)}</value>
440                                         </entry>
441                                         ${volumeGroupParams}
442                             </volumeGroupParams>
443                                 <skipAAI>true</skipAAI>
444                                 <backout>${MsoUtils.xmlEscape(backoutOnFailure)}</backout>
445                                 <failIfExists>${MsoUtils.xmlEscape(failIfExists)}</failIfExists>
446                             <msoRequest>
447                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
448                                 <serviceInstanceId>${MsoUtils.xmlEscape(serviceId)}</serviceInstanceId>
449                             </msoRequest>
450                             <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
451                             <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
452                         </createVolumeGroupRequest>
453                 """
454
455                 String vnfSubCreateWorkflowRequestAsString = utils.formatXml(vnfSubCreateWorkflowRequest)
456                 msoLogger.debug(vnfSubCreateWorkflowRequestAsString)
457                 msoLogger.debug(vnfSubCreateWorkflowRequestAsString)
458                 execution.setVariable(prefix+"createVnfARequest", vnfSubCreateWorkflowRequestAsString)
459
460                 // build rollback request for use later if needed
461                 String vnfSubRollbackWorkflowRequest = buildRollbackVolumeGroupRequestXml(volumeGroupId, cloudSiteId, tenantId, requestId, serviceId, messageId, notificationUrl)
462
463                 msoLogger.debug("Sub Vnf flow rollback request: vnfSubRollbackWorkflowRequest " + "\n" + vnfSubRollbackWorkflowRequest)
464
465                 String vnfSubRollbackWorkflowRequestAsString = utils.formatXml(vnfSubRollbackWorkflowRequest)
466                 execution.setVariable(prefix+"rollbackVnfARequest", vnfSubRollbackWorkflowRequestAsString)
467         }
468         
469         public String buildRollbackVolumeGroupRequestXml(volumeGroupId, cloudSiteId, tenantId, requestId, serviceId, messageId, notificationUrl) {
470                 
471                 def request = """
472                 <rollbackVolumeGroupRequest>
473                         <volumeGroupRollback>
474                            <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
475                            <volumeGroupStackId>{{VOLUMEGROUPSTACKID}}</volumeGroupStackId>
476                            <tenantId>${MsoUtils.xmlEscape(tenantId)}</tenantId>
477                            <cloudSiteId>${MsoUtils.xmlEscape(cloudSiteId)}</cloudSiteId>
478                            <volumeGroupCreated>true</volumeGroupCreated>
479                            <msoRequest>
480                               <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
481                               <serviceInstanceId>${MsoUtils.xmlEscape(serviceId)}</serviceInstanceId>
482                            </msoRequest>
483                            <messageId>${MsoUtils.xmlEscape(messageId)}</messageId>
484                         </volumeGroupRollback>
485                         <skipAAI>true</skipAAI>
486                         <notificationUrl>${MsoUtils.xmlEscape(notificationUrl)}</notificationUrl>
487                 </rollbackVolumeGroupRequest>
488                 """ 
489                 
490                 return request  
491         }
492
493         public String updateRollbackVolumeGroupRequestXml(String rollabackRequest, String heatStackId) {
494                 String newRequest = rollabackRequest.replace("{{VOLUMEGROUPSTACKID}}", heatStackId)
495                 return newRequest
496         }
497         
498         /**
499          * Validate VNF adapter response
500          * @param execution
501          */
502         public void validateVnfResponse(DelegateExecution execution, isDebugEnabled) {
503                 def vnfSuccess = execution.getVariable('VNFREST_SuccessIndicator')
504                 msoLogger.debug("vnfAdapterSuccessIndicator: "+ vnfSuccess)
505                 if(vnfSuccess==true) {
506                         String createVnfAResponse = execution.getVariable(prefix+"createVnfAResponse")
507                         String heatStackID = utils.getNodeText(createVnfAResponse, "volumeGroupStackId")
508                         String vnfRollbackRequest = execution.getVariable(prefix+"rollbackVnfARequest")
509                         String updatedVnfRollbackRequest = updateRollbackVolumeGroupRequestXml(vnfRollbackRequest, heatStackID)
510                         msoLogger.debug("vnfAdapter rollback request: "+ updatedVnfRollbackRequest)
511                         RollbackData rollbackData = execution.getVariable("rollbackData")
512                         rollbackData.put("DCVFMODULEVOL", "rollbackVnfARequest", updatedVnfRollbackRequest)
513                         rollbackData.put("DCVFMODULEVOL", "isCreateVnfRollbackNeeded", "true")
514                 }
515         }
516         
517
518         /**
519          * Update voulume group in AAI 
520          * @TODO: Can we re-use the create method??
521          * @param execution
522          * @param isDebugEnabled
523          */
524         public void callRESTUpdateCreatedVolGrpName(DelegateExecution execution, isDebugEnabled) {
525
526                 String requeryAAIVolGrpNameResponse = execution.getVariable(prefix+"queryAAIVolGrpNameResponse")
527                 String volumeGroupId = utils.getNodeText(requeryAAIVolGrpNameResponse, "volume-group-id")
528                 String modelCustomizationId = execution.getVariable("modelCustomizationId")  
529                 String cloudRegion = execution.getVariable("lcpCloudRegionId")
530
531                 AaiUtil aaiUtil = new AaiUtil(this)
532                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
533                 String updateAAIVolumeGroupUrlRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups/volume-group/" + UriUtils.encode(volumeGroupId, 'UTF-8')
534
535                 String namespace =  aaiUtil.getNamespaceFromUri(aaiUtil.getCloudInfrastructureCloudRegionUri(execution))
536
537                 msoLogger.debug("updateAAIVolumeGroupUrlRequest - " +  updateAAIVolumeGroupUrlRequest)
538
539                 String createVnfAResponse = execution.getVariable(prefix+"createVnfAResponse")
540                 def heatStackID = utils.getNodeText(createVnfAResponse, "volumeGroupStackId")
541                 
542                 execution.setVariable(prefix+"heatStackId", heatStackID)
543
544                 NetworkUtils networkUtils = new NetworkUtils()
545                 String payload = networkUtils.updateCloudRegionVolumeRequest(requeryAAIVolGrpNameResponse, heatStackID, namespace, modelCustomizationId)
546                 String payloadXml = utils.formatXml(payload)
547
548                 msoLogger.debug("Payload to Update Created VolumeGroupName - " + "\n" + payloadXml)
549
550                 APIResponse response = aaiUtil.executeAAIPutCall(execution, updateAAIVolumeGroupUrlRequest, payloadXml)
551
552                 String returnCode = response.getStatusCode()
553                 String aaiResponseAsString = response.getResponseBodyAsString()
554
555                 msoLogger.debug("AAI create volume group return code: " + returnCode)
556                 msoLogger.debug("AAI create volume group response: " + aaiResponseAsString)
557
558                 ExceptionUtil exceptionUtil = new ExceptionUtil()
559
560                 if (returnCode =='200') {
561                         execution.setVariable(prefix+"updateCreatedAAIVolumeGrpNameResponse", aaiResponseAsString)
562                         execution.setVariable(prefix+"isPONR", true)
563                 } else {
564                         execution.setVariable(prefix+"isErrorMessageException", true)
565                         if (returnCode=='404') {
566                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Unable to update volume group in AAI. Response code: 404")
567                         } else {
568                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
569                                 msoLogger.debug(" AAI Adapter Query Failed.  WorkflowException - " + "\n" + aWorkflowException)
570                                 throw new BpmnError("MSOWorkflowException")
571                         }
572                 }
573         }
574
575
576         /**
577          * Query AAI Generic VNF
578          * @param execution
579          * @param isDebugEnabled
580          */
581         public void callRESTQueryAAIGenericVnf(DelegateExecution execution, isDebugEnabled) {
582
583                 def vnfId = execution.getVariable('vnfId')
584
585                 AaiUtil aaiUtil = new AaiUtil(this)
586                 String aaiEndpoint = aaiUtil.getNetworkGenericVnfEndpoint(execution)
587                 def String queryAAIRequest = aaiEndpoint + "/" + UriUtils.encode(vnfId, "UTF-8")
588
589                 msoLogger.debug("AAI query generic vnf request: " + queryAAIRequest)
590
591                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIRequest)
592
593                 String returnCode = response.getStatusCode()
594                 String aaiResponseAsString = response.getResponseBodyAsString()
595
596                 msoLogger.debug("AAI query generic vnf return code: " + returnCode)
597                 msoLogger.debug("AAI query generic vnf response: " + aaiResponseAsString)
598
599                 ExceptionUtil exceptionUtil = new ExceptionUtil()
600
601                 if (returnCode=='200') {
602                         msoLogger.debug('Generic vnf ' + vnfId + ' found in AAI.')
603                         execution.setVariable(prefix+'AAIQueryGenericVfnResponse', aaiResponseAsString)
604                 } else {
605                         if (returnCode=='404') {
606                                 def message = 'Generic vnf ' + vnfId + ' was not found in AAI. Return code: 404.'
607                                 msoLogger.debug(message)
608                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, message)
609                         } else {
610                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
611                                 throw new BpmnError("MSOWorkflowException")
612                         }
613                 }
614         }
615
616 }