12a21c6eba08de1e5307cae890e4f63ec59d109e
[so.git] / bpmn / MSOGammaBPMN / src / main / groovy / com / att / bpm / scripts / DoCreateVfModuleVolumeV1.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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 com.att.bpm.scripts
22
23 import static org.camunda.spin.Spin.XML;
24 import groovy.json.JsonSlurper
25 import groovy.lang.GroovyInterceptable;
26 import groovy.xml.XmlUtil
27 import org.springframework.web.util.UriUtils
28
29 import org.openecomp.mso.rest.APIResponse
30 import org.openecomp.mso.rest.RESTClient
31 import org.openecomp.mso.rest.RESTConfig
32 import org.openecomp.mso.bpmn.core.WorkflowException
33
34 import java.util.UUID;
35
36 import org.camunda.bpm.engine.delegate.BpmnError
37 import org.camunda.bpm.engine.runtime.Execution
38 import org.apache.commons.lang3.*
39 import org.apache.commons.codec.binary.Base64;
40
41 class DoCreateVfModuleVolumeV1 extends VfModuleBase {
42         
43         String prefix='DCVFMODVOLV1_'
44         
45         
46         /**
47          * Perform initial processing, such as request validation, initialization of variables, etc.
48          * * @param execution
49          */
50         public void preProcessRequest (Execution execution) {
51                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
52                 preProcessRequest(execution, isDebugEnabled)
53         }
54         
55         /**
56          * Perform initial processing, such as request validation, initialization of variables, etc.
57          * @param execution
58          * @param isDebugEnabled
59          */
60         public void preProcessRequest (Execution execution, isDebugEnabled) {
61
62                 execution.setVariable("prefix",prefix)
63                 execution.setVariable(prefix+'SuccessIndicator', false)
64                 
65         
66                 // INPUT: DoCreateVfModuleVolumeV1Request, att-mso-request-id, volume-group-id, vnf-id, is-vid-request
67                 // OUTPUT: DCVFMODVOLV1_SuccessIndicator. WorkflowException
68                 
69                 def volumeRequest  = getVariable(execution, 'DoCreateVfModuleVolumeV1Request')
70                 if (volumeRequest != null) {
71                         execution.setVariable(prefix+'Request', volumeRequest)
72                 } else {        
73                         volumeRequest  = getVariable(execution, prefix+'Request')
74                         if (volumeRequest == null) {
75                                 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'DoCreateVfModuleVolumeV1 received null request.')
76                         }
77                 }
78                 
79                 def vnfId = execution.getVariable('vnf-id')
80                 if (vnfId == null) {
81                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'DoCreateVfModuleVolumeV1 received null vnf-id.')
82                 }
83                 
84                 def volumeGroupId = execution.getVariable('volume-group-id')
85                 if (volumeGroupId == null) {
86                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'DoCreateVfModuleVolumeV1 received null volume-group-id.')
87                 }
88                 
89                 def requestId = execution.getVariable("att-mso-request-id")
90                 if (requestId == null || requestId == "") {
91                         requestId = utils.getNodeText1(volumeRequest, "request-id")
92                 }
93
94                 //def serviceId = execution.getVariable("att-mso-service-instance-id")
95                 //if (serviceId == null || serviceId == "") {
96                 def     serviceId = utils.getNodeText1(volumeRequest, "service-id")
97                 //}
98                 
99                 def source = utils.getNodeText1(volumeRequest, "source")
100                 
101                 execution.setVariable(prefix+'requestId', requestId)
102                 execution.setVariable(prefix+'serviceId', serviceId)
103                 execution.setVariable(prefix+'source', source)
104                 
105                 // @TODO: for better tracking of logs, should we strip all new lines in the log message?
106                 utils.logAudit('Incoming request: ' + volumeRequest)
107
108                 // Rollback settings
109                 NetworkUtils networkUtils = new NetworkUtils()
110                 def rollbackEnabled = networkUtils.isRollbackEnabled(execution,volumeRequest)
111                 execution.setVariable(prefix+"rollbackEnabled", rollbackEnabled)
112                 utils.log("DEBUG", 'rollbackEnabled: ' + rollbackEnabled, isDebugEnabled)
113                 
114         }
115         
116
117         /**
118          * Get cloud region
119          * @param execution
120          * @param isDebugEnabled
121          */
122         public void callRESTQueryAAICloudRegion (Execution execution, isDebugEnabled) {
123                 
124                 def request = execution.getVariable(prefix+'Request')
125                 def cloudRegion = utils.getNodeText1(request, "aic-cloud-region")
126                 utils.log("DEBUG", 'Request cloud region is: ' + cloudRegion, isDebugEnabled)
127
128                 AaiUtil aaiUtil = new AaiUtil(this)
129                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
130                 String queryCloudRegionRequest = aaiEndpoint + '/' + cloudRegion
131                 
132                 utils.logAudit(queryCloudRegionRequest)
133
134                 cloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
135
136                 if ((cloudRegion != "ERROR")) {
137                         if(execution.getVariable(prefix+"queryCloudRegionReturnCode") == "404"){
138                                 cloudRegion = "AAIAIC25"
139                         }
140                         execution.setVariable(prefix+"aicCloudRegion", cloudRegion)
141                         utils.log("DEBUG", "AIC Cloud Region: " + cloudRegion, isDebugEnabled)
142                 } else {
143                         String errorMessage = "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable(prefix+"queryCloudRegionReturnCode")
144                         utils.log("DEBUG", errorMessage, isDebugEnabled)
145                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, errorMessage)
146                 }
147         }
148         
149
150         /**
151          * Query AAI volume group by name
152          * @param execution
153          * @param isDebugEnabled
154          */
155         public void callRESTQueryAAIVolGrpName(Execution execution, isDebugEnabled) {
156
157                 def volumeRequest = execution.getVariable(prefix+'Request')
158                 def volumeGroupName = utils.getNodeText(volumeRequest, "volume-group-name")
159                 def cloudRegion = execution.getVariable(prefix+"aicCloudRegion")
160                 
161                 // Save volume group name
162                 execution.setVariable(prefix+'volumeGroupName', volumeGroupName)
163                 
164                 // This is for stub testing
165                 def testVolumeGroupName = execution.getVariable('test-volume-group-name')
166                 if (testVolumeGroupName != null && testVolumeGroupName.length() > 0) {
167                         volumeGroupName = testVolumeGroupName
168                         //reset to null
169                         execution.setVariable('test-volume-group-name', null)
170                 }
171                 
172                 AaiUtil aaiUtil = new AaiUtil(this)
173                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
174                 String queryAAIVolumeNameRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups" + "?volume-group-name=" + UriUtils.encode(volumeGroupName, 'UTF-8')
175
176                 utils.logAudit('Query AAI volume group by name: ' + queryAAIVolumeNameRequest)
177                 
178                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIVolumeNameRequest)
179                 
180                 String returnCode = response.getStatusCode()
181                 String aaiResponseAsString = response.getResponseBodyAsString()
182                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
183                 
184                 utils.logAudit("AAI query volume group by name return code: " + returnCode)
185                 utils.logAudit("AAI query volume group by name response: " + aaiResponseAsString)
186
187                 ExceptionUtil exceptionUtil = new ExceptionUtil()
188
189                 execution.setVariable(prefix+"queryAAIVolGrpNameResponse", aaiResponseAsString)
190                 execution.setVariable(prefix+'AaiReturnCode', returnCode)
191
192                 if (returnCode=='200') {
193                         // @TODO: verify error code
194                         // @TODO: create class of literals representing error codes
195                         execution.setVariable(prefix+'queryAAIVolGrpNameResponse', aaiResponseAsString)
196                         utils.log("DEBUG", "Volume Group Name $volumeGroupName exists in AAI.", isDebugEnabled)
197                 } else {
198                         if (returnCode=='404') {
199                                 utils.log("DEBUG", "Volume Group Name $volumeGroupName does not exist in AAI.", isDebugEnabled)
200                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupName not found in AAI. Response code: 404")
201                         } else {
202                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
203                                 throw new BpmnError("MSOWorkflowException")
204                         }
205                 }
206         }
207         
208
209         /**
210          * Create a WorkflowException 
211          * @param execution
212          * @param isDebugEnabled
213          */
214         public void buildWorkflowException(Execution execution, int errorCode, errorMessage, isDebugEnabled) {
215                 utils.log("DEBUG", errorMessage, isDebugEnabled)
216                 (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)
217         }
218         
219
220         /**
221          * Create a WorkflowException
222          * @param execution
223          * @param isDebugEnabled
224          */
225         public void handleError(Execution execution, isDebugEnabled) {
226                 WorkflowException we = execution.getVariable('WorkflowException')
227                 if (we == null) {
228                         (new ExceptionUtil()).buildWorkflowException(execution, 2500, "Enexpected error encountered!")
229                 }
230                 throw new BpmnError("MSOWorkflowException")
231         }
232         
233         /**
234          * Create volume group in AAI
235          * @param execution
236          * @param isDebugEnabled
237          */
238         public void callRESTCreateAAIVolGrpName(Execution execution, isDebugEnabled) {
239
240                 def volumeRequest  = execution.getVariable(prefix+'Request')
241                 def vnfId = execution.getVariable('vnf-id')
242                 def volumeName = utils.getNodeText(volumeRequest, "volume-group-name")
243                 
244                 def cloudRegion = execution.getVariable(prefix+"aicCloudRegion")
245                 def groupId = execution.getVariable('volume-group-id')
246                 utils.log("DEBUG", "volume group id: " + groupId, isDebugEnabled)
247                 
248                 AaiUtil aaiUtil = new AaiUtil(this)
249                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
250                 String createAAIVolumeGrpNameUrlRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups/volume-group/" + UriUtils.encode(groupId, "UTF-8")
251                 
252                 String namespace =  aaiUtil.getNamespaceFromUri(aaiUtil.getCloudInfrastructureCloudRegionUri(execution))
253                 utils.log("DEBUG", "AAI namespace is: " + namespace, isDebugEnabled)
254                 
255                 utils.logAudit(createAAIVolumeGrpNameUrlRequest)
256                 
257                 //Prepare payload (PUT)
258                 def vnfType = utils.getNodeText(volumeRequest, "vnf-type")
259                 def tenantId = utils.getNodeText(volumeRequest, "tenant-id")
260                 
261                 NetworkUtils networkUtils = new NetworkUtils()
262                 String payload = networkUtils.createCloudRegionVolumeRequest(groupId, volumeName, vnfType, vnfId, tenantId, cloudRegion, namespace)
263                 String payloadXml = utils.formatXml(payload)
264                 utils.logAudit(payloadXml)
265                 
266                 APIResponse response = aaiUtil.executeAAIPutCall(execution, createAAIVolumeGrpNameUrlRequest, payloadXml)
267                                 
268                 String returnCode = response.getStatusCode()
269                 String aaiResponseAsString = response.getResponseBodyAsString()
270                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
271                 
272                 utils.logAudit("AAI create volume group return code: " + returnCode)
273                 utils.logAudit("AAI create volume group response: " + aaiResponseAsString)
274
275                 execution.setVariable(prefix+"createAAIVolumeGrpNameReturnCode", returnCode)
276                 execution.setVariable(prefix+"createAAIVolumeGrpNameResponse", aaiResponseAsString)
277
278                 ExceptionUtil exceptionUtil = new ExceptionUtil()
279                 
280                 if (returnCode =='201') {
281                         execution.setVariable(prefix+"isAAIRollbackNeeded", true)
282                 } else {
283                         execution.setVariable(prefix+"isErrorMessageException", true)
284                         if (returnCode=='404') {
285                                 // @TODO: verify return code and make static LITERAL
286                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Unable to create volume group in AAI. Response code: 404")
287                         } else {
288                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
289                                 utils.log("DEBUG", " AAI Adapter Query Failed.  WorkflowException - " + "\n" + aWorkflowException, isDebugEnabled)
290                                 throw new BpmnError("MSOWorkflowException")
291                         }
292                 }
293         }
294
295         /**
296          * Prepare VNF adapter create request XML
297          * @param execution
298          */
299         public void prepareVnfAdapterCreateRequest(Execution execution, isDebugEnabled) {
300                 
301                 
302                 def volumeRequest  = execution.getVariable(prefix+'Request')
303                 def requestId = execution.getVariable(prefix+'requestId')
304                 def serviceId = execution.getVariable(prefix+'serviceId')
305                 
306                 def aaiGenericVnfResponse = execution.getVariable(prefix+'AAIQueryGenericVfnResponse')
307                 def vnfId = utils.getNodeText1(aaiGenericVnfResponse, 'vnf-id')
308                 def vnfName = utils.getNodeText1(aaiGenericVnfResponse, 'vnf-name')
309                                 
310                 String messageId = UUID.randomUUID()
311                 utils.log("DEBUG", "messageId to be used is generated: " + messageId, isDebugEnabled)
312
313                 // prepare vnf request for vnfAdapterCreateV1
314                 def cloudSiteId = utils.getNodeText1(volumeRequest, 'aic-cloud-region')
315                 def tenantId = utils.getNodeText1(volumeRequest, "tenant-id")
316                 def vnfType = utils.getNodeText1(volumeRequest, "vnf-type")
317                 def vnfVersion = utils.getNodeText1(volumeRequest, "asdc-service-model-version")
318                 def vnfModuleType = utils.getNodeText1(volumeRequest, "vf-module-model-name")
319                 def volumeGroupnName = utils.getNodeText1(volumeRequest, "volume-group-name")
320
321                 def volumeParamsXml = utils.getNodeXml(volumeRequest, 'volume-params')
322                 def volumeGroupParams = transformVolumeParamsToEntries(volumeParamsXml)
323
324                 utils.log("DEBUG", "volumeGroupParams: "+ volumeGroupParams, isDebugEnabled)
325
326                 String volumeGroupId = execution.getVariable('volume-group-id')
327                 def notificationUrl = execution.getVariable("URN_mso_workflow_vnfadapter_rest_callback")
328                 def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")
329                 if ('true'.equals(useQualifiedHostName)) {
330                                 notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
331                 }
332                 utils.log("DEBUG", "CreateVfModuleVolume - notificationUrl: "+ notificationUrl, isDebugEnabled)
333                 
334                 def backoutOnFailure = execution.getVariable("DCVFMODVOLV1_rollbackEnabled")
335                 utils.log("DEBUG", "backoutOnFailure: "+ backoutOnFailure, isDebugEnabled)
336                 
337                 // build request
338                 String vnfSubCreateWorkflowRequest =
339                 """
340                         <createVolumeGroupRequest>
341                                 <cloudSiteId>${cloudSiteId}</cloudSiteId>
342                                 <tenantId>${tenantId}</tenantId>
343                                 <vnfId>${vnfId}</vnfId>
344                                 <vnfName>${vnfName}</vnfName>
345                                 <volumeGroupId>${volumeGroupId}</volumeGroupId>
346                                 <volumeGroupName>${volumeGroupnName}</volumeGroupName>
347                                 <vnfType>${vnfType}</vnfType>
348                                 <vnfVersion>${vnfVersion}</vnfVersion>
349                                 <vfModuleType>${vnfModuleType}</vfModuleType>
350                                 <volumeGroupParams>
351                                         <entry>
352                                                 <key>vnf_id</key>
353                                                 <value>${vnfId}</value>
354                                         </entry>
355                                         <entry>
356                                                 <key>vnf_name</key>
357                                                 <value>${vnfName}</value>
358                                         </entry>
359                                         <entry>
360                                                 <key>vf_module_id</key>
361                                                 <value>${volumeGroupId}</value>
362                                         </entry>
363                                         <entry>
364                                                 <key>vf_module_name</key>
365                                                 <value>${volumeGroupnName}</value>
366                                         </entry>
367                                         ${volumeGroupParams}
368                             </volumeGroupParams>
369                                 <skipAAI>true</skipAAI>
370                                 <backout>${backoutOnFailure}</backout>
371                                 <failIfExists>true</failIfExists>
372                             <msoRequest>
373                                 <requestId>${requestId}</requestId>
374                                 <serviceInstanceId>${serviceId}</serviceInstanceId>
375                             </msoRequest>
376                             <messageId>${messageId}</messageId>
377                             <notificationUrl>${notificationUrl}</notificationUrl>
378                         </createVolumeGroupRequest>
379                 """
380
381                 String vnfSubCreateWorkflowRequestAsString = utils.formatXml(vnfSubCreateWorkflowRequest)
382                 utils.logAudit(vnfSubCreateWorkflowRequestAsString)
383                 execution.setVariable(prefix+"createVnfARequest", vnfSubCreateWorkflowRequestAsString)
384                 
385                 // build rollback request for use later if needed
386                 
387                 String vnfSubRollbackWorkflowRequest =
388                 """<rollbackVolumeGroupRequest>
389                                 <cloudSiteId>${cloudSiteId}</cloudSiteId>
390                                 <tenantId>${tenantId}</tenantId>
391                                 <volumeGroupId>${volumeGroupId}</volumeGroupId>
392                                 <skipAAI>true</skipAAI>
393                                 <volumeGroupCreated>true</volumeGroupCreated>
394                             <msoRequest>
395                                 <requestId>${requestId}</requestId>
396                                 <serviceInstanceId>${serviceId}</serviceInstanceId>
397                             </msoRequest>
398                             <messageId>${messageId}</messageId>
399                             <notificationUrl>${notificationUrl}</notificationUrl>
400                         </rollbackVolumeGroupRequest>"""
401
402                 utils.log("DEBUG", "Sub Vnf flow rollback request: vnfSubRollbackWorkflowRequest " + "\n" + vnfSubRollbackWorkflowRequest, isDebugEnabled)
403                 
404                 String vnfSubRollbackWorkflowRequestAsString = utils.formatXml(vnfSubRollbackWorkflowRequest)
405                 execution.setVariable(prefix+"rollbackVnfARequest", vnfSubRollbackWorkflowRequestAsString)
406         }
407
408         
409
410         /**
411          * Update voulume group in AAI 
412          * @TODO: Can we re-use the create method??
413          * @param execution
414          * @param isDebugEnabled
415          */
416         public void callRESTUpdateCreatedVolGrpName(Execution execution, isDebugEnabled) {
417                 
418                 // get variables
419                 String requeryAAIVolGrpNameResponse = execution.getVariable(prefix+"queryAAIVolGrpNameResponse")
420                 String volumeGroupId = utils.getNodeText(requeryAAIVolGrpNameResponse, "volume-group-id")
421                 String cloudRegion = execution.getVariable(prefix+"aicCloudRegion")
422                 
423                 AaiUtil aaiUtil = new AaiUtil(this)
424                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
425                 String updateAAIVolumeGroupUrlRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups/volume-group/" + UriUtils.encode(volumeGroupId, 'UTF-8')
426                 
427                 String namespace =  aaiUtil.getNamespaceFromUri(aaiUtil.getCloudInfrastructureCloudRegionUri(execution))
428
429                 utils.logAudit(updateAAIVolumeGroupUrlRequest)
430
431                 //Prepare payload (PUT)
432                 String createVnfAResponse = execution.getVariable(prefix+"createVnfAResponse")
433                 
434                 // @TODO: revisit
435                 // if VID request createVnfresponse will be null, use vnf from JSON request
436                 def vnfId = ""
437                 if(createVnfAResponse == null || createVnfAResponse == "") {
438                         vnfId = execution.getVariable(prefix+'vnfId')
439                 }
440                 else {
441                         vnfId = utils.getNodeText(createVnfAResponse, "volumeGroupStackId")
442                 }
443                 
444                 NetworkUtils networkUtils = new NetworkUtils()
445                 String payload = networkUtils.updateCloudRegionVolumeRequest(requeryAAIVolGrpNameResponse, vnfId, namespace)
446                 String payloadXml = utils.formatXml(payload)
447                 
448                 //execution.setVariable(prefix+"updateCreatedAAIVolumeGrpNamePayloadRequest", payloadXml)
449                 utils.logAudit(payload)
450                 //utils.log("DEBUG", " 'payload' to Update Created VolumeGroupName - " + "\n" + payloadXml, isDebugEnabled)
451
452                 APIResponse response = aaiUtil.executeAAIPutCall(execution, updateAAIVolumeGroupUrlRequest, payloadXml)
453                 
454                 String returnCode = response.getStatusCode()
455                 String aaiResponseAsString = response.getResponseBodyAsString()
456                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
457                 
458                 utils.logAudit("AAI create volume group return code: " + returnCode)
459                 utils.logAudit("AAI create volume group response: " + aaiResponseAsString)
460
461                 ExceptionUtil exceptionUtil = new ExceptionUtil()
462
463                 if (returnCode =='200') {
464                         execution.setVariable(prefix+"updateCreatedAAIVolumeGrpNameResponse", aaiResponseAsString)
465                         execution.setVariable(prefix+"isPONR", true)
466                 } else {
467                         execution.setVariable(prefix+"isErrorMessageException", true)
468                         if (returnCode=='404') {
469                                 // @TODO: verify return code and make static LITERAL
470                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Unable to update volume group in AAI. Response code: 404")
471                         } else {
472                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
473                                 utils.log("DEBUG", " AAI Adapter Query Failed.  WorkflowException - " + "\n" + aWorkflowException, isDebugEnabled)
474                                 throw new BpmnError("MSOWorkflowException")
475                         }
476                 }
477         }
478
479         
480         /**
481          * Query AAI service instance
482          * @param execution
483          * @param isDebugEnabled
484          */
485         public void callRESTQueryAAIGenericVnf(Execution execution, isDebugEnabled) {
486                 
487                 def request = execution.getVariable(prefix+"Request")
488                 def vnfId = execution.getVariable('vnf-id')
489                 
490                 AaiUtil aaiUtil = new AaiUtil(this)
491                 String aaiEndpoint = aaiUtil.getNetworkGenericVnfEndpoint(execution)
492                 def String queryAAIRequest = aaiEndpoint + "/" + UriUtils.encode(vnfId, "UTF-8")
493                 
494                 utils.logAudit("AAI query generic vnf request: " + queryAAIRequest)
495                 
496                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIRequest)
497                 
498                 String returnCode = response.getStatusCode()
499                 String aaiResponseAsString = response.getResponseBodyAsString()
500                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
501                 
502                 utils.logAudit("AAI query generic vnf return code: " + returnCode)
503                 utils.logAudit("AAI query generic vnf response: " + aaiResponseAsString)
504
505                 ExceptionUtil exceptionUtil = new ExceptionUtil()
506                 
507                 if (returnCode=='200') {
508                         utils.log("DEBUG", 'Generic vnf ' + vnfId + ' found in AAI.', isDebugEnabled)
509                         execution.setVariable(prefix+'AAIQueryGenericVfnResponse', aaiResponseAsString)
510                 } else {
511                         if (returnCode=='404') {
512                                 def message = 'Generic vnf ' + vnfId + ' was not found in AAI. Return code: 404.'
513                                 utils.log("DEBUG", message, isDebugEnabled)
514                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, message)
515                         } else {
516                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
517                                 throw new BpmnError("MSOWorkflowException")
518                         }
519                 }
520         }
521
522         public void callRESTDeleteAAIVolumeGroup(Execution execution, isDebugEnabled) {
523
524                 callRESTQueryAAIVolGrpName(execution, isDebugEnabled)
525                 
526                 def queryAaiVolumeGroupResponse = execution.getVariable(prefix+'queryAAIVolGrpNameResponse')
527                 
528                 def volumeGroupId = utils.getNodeText(queryAaiVolumeGroupResponse, "volume-group-id")
529                 def resourceVersion = utils.getNodeText(queryAaiVolumeGroupResponse, "resource-version")
530
531                 def cloudRegion = execution.getVariable(prefix+"aicCloudRegion")
532                 
533                 AaiUtil aaiUtil = new AaiUtil(this)
534                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
535                 String deleteAAIVolumeGrpIdRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups/volume-group" + '/' +  volumeGroupId + "?resource-version=" + UriUtils.encode(resourceVersion, "UTF-8")
536
537                 utils.logAudit('Delete AAI volume group : ' + deleteAAIVolumeGrpIdRequest)
538                 
539                 APIResponse response = aaiUtil.executeAAIDeleteCall(execution, deleteAAIVolumeGrpIdRequest)
540                 
541                 String returnCode = response.getStatusCode()
542                 String aaiResponseAsString = response.getResponseBodyAsString()
543                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
544                 
545                 utils.logAudit("AAI delete volume group return code: " + returnCode)
546                 utils.logAudit("AAI delete volume group response: " + aaiResponseAsString)
547
548                 ExceptionUtil exceptionUtil = new ExceptionUtil()
549
550                 def volumeGroupNameFound = prefix+'volumeGroupNameFound'
551                 if (returnCode=='200' || returnCode=='204' ) {
552                         utils.log("DEBUG", "Volume group $volumeGroupId deleted.", isDebugEnabled)
553                 } else {
554                         if (returnCode=='404') {
555                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupId not found for delete in AAI Response code: 404")
556                         } else {
557                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
558                                 throw new BpmnError("MSOWorkflowException")
559                         }
560                 }
561         }
562
563         
564         
565         
566         
567         public void prepareSuccessDBRequest(Execution execution, isDebugEnabled) {
568                 String requestId = execution.getVariable(prefix+'requestId')
569                 String dbVnfOutputs = execution.getVariable(prefix+'volumeOutputs')
570                 prepareDBRequest(execution, requestId, "VolumeGroup successfully created.", "COMPLETED", "100", dbVnfOutputs, isDebugEnabled)
571         }
572         
573         public void prepareFailDbRequest(Execution execution, isDebugEnabled) {
574                 
575                 WorkflowException we = execution.getVariable("WorkflowException")
576                 
577                 String requestId = execution.getVariable(prefix+'requestId')
578                 String dbVnfOutputs = execution.getVariable(prefix+'volumeOutputs')
579                 prepareDBRequest(execution, requestId, we.getErrorMessage(), "FAILURE", "", dbVnfOutputs, isDebugEnabled)
580
581         }
582         
583         
584         /**
585          * Prepare Infra DB XML Request 
586          * @param execution
587          */
588         public void prepareDBRequest (Execution execution, String requestId, String statusMessage, String requestStatus, String progress, String dbVnfOutputs, isDebugEnabled) {
589                 
590                 String dbRequest =
591                 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
592                         <soapenv:Header/>
593                         <soapenv:Body>
594                                 <ns:updateInfraRequest xmlns:ns="http://com.att.mso/requestsdb">
595                                         <requestId>${requestId}</requestId>
596                                         <lastModifiedBy>BPMN</lastModifiedBy>
597                                         <statusMessage>${statusMessage}</statusMessage>
598                                         <responseBody></responseBody>
599                                         <requestStatus>${requestStatus}</requestStatus>
600                                         <progress>${progress}</progress>
601                                         <vnfOutputs>${dbVnfOutputs}</vnfOutputs>
602                                 </ns:updateInfraRequest>
603                         </soapenv:Body>
604                    </soapenv:Envelope>"""
605
606            utils.log("DEBUG", " DB Adapter Request - " + "\n" + dbRequest, isDebugEnabled)
607            String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)
608            
609            execution.setVariable(prefix+"createDBRequest", buildDeleteDBRequestAsString)
610            
611            utils.logAudit(buildDeleteDBRequestAsString)
612          }
613
614
615
616         
617         public void postProcessResponse (Execution execution, isDebugEnabled) {
618
619                 String dbReturnCode = execution.getVariable(prefix+"dbReturnCode")
620                 utils.log("DEBUG", "DB Update Response Code  : " + dbReturnCode, isDebugEnabled)
621                 
622                 String createDBResponse =  execution.getVariable(prefix+"createDBResponse")
623                 utils.logAudit(createDBResponse)
624
625                 String source = execution.getVariable(prefix+"source")
626                 String requestId = execution.getVariable(prefix+'requestId')
627
628                 String msoCompletionRequest =
629                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
630                                                         xmlns:ns="http://ecomp.att.com/mso/request/types/v1">
631                                         <request-info xmlns="http://ecomp.att.com/mso/infra/vnf-request/v1">
632                                                 <request-id>${requestId}</request-id>
633                                                 <action>CREATE</action>
634                                                 <source>${source}</source>
635                                         </request-info>
636                                         <aetgt:mso-bpel-name>BPEL Volume Group action: CREATE</aetgt:mso-bpel-name>
637                                 </aetgt:MsoCompletionRequest>"""
638
639                 // Format Response
640                 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
641                 utils.logAudit(xmlMsoCompletionRequest)
642                 
643                 // normal path
644                 if (dbReturnCode == "200") {
645                                 execution.setVariable(prefix+"Success", true)
646                                 execution.setVariable(prefix+"CompleteMsoProcessRequest", xmlMsoCompletionRequest)
647                                 utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
648                 } else {
649                                 execution.setVariable(prefix+"isErrorMessageException", true)
650                                 utils.log("DEBUG", " DB Update failed, code: " + dbReturnCode + ", going to Unexpected Error.", isDebugEnabled)
651                 }
652         }
653
654 }