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