Remove unnecessary use of Calendar.getInstance()
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DeleteVfModuleVolumeInfraV1.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 org.openecomp.mso.bpmn.infrastructure.scripts;
22
23 import groovy.json.JsonSlurper
24
25 import java.util.concurrent.ExecutionException;
26
27 import org.springframework.web.util.UriUtils
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.runtime.Execution
30 import org.apache.commons.lang3.*
31 import org.openecomp.mso.bpmn.common.scripts.AaiUtil;
32 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor;
33 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil;
34 import org.openecomp.mso.bpmn.common.scripts.VidUtils;
35 import org.openecomp.mso.bpmn.core.WorkflowException
36 import org.openecomp.mso.rest.APIResponse
37
38 /**
39  * This groovy class supports the <class>DeleteVfModuleVolume.bpmn</class> process.
40  */
41 public class DeleteVfModuleVolumeInfraV1 extends AbstractServiceTaskProcessor {
42
43         private XmlParser xmlParser = new XmlParser()
44         /**
45          * This method is executed during the preProcessRequest task of the <class>DeleteVfModuleVolume.bpmn</class> process.
46          * @param execution
47          */
48         public InitializeProcessVariables(Execution execution){
49                 execution.setVariable('prefix', 'DELVfModVol_')
50                 execution.setVariable("DELVfModVol_volumeRequest", null)
51                 execution.setVariable('DELVfModVol_requestInfo', null)
52                 execution.setVariable('DELVfModVol_requestId', null)
53                 execution.setVariable('DELVfModVol_source', null)
54                 execution.setVariable('DELVfModVol_volumeInputs', null)
55                 execution.setVariable('DELVfModVol_volumeOutputs', null)
56                 execution.setVariable('DELVfModVol_volumeGroupId', null)
57                 execution.setVariable('DELVfModVol_vnfType', null)
58                 execution.setVariable('DELVfModVol_serviceId', null)
59                 execution.setVariable('DELVfModVol_cloudRegion', null)
60                 execution.setVariable('DELVfModVol_tenantId', null)
61                 execution.setVariable('DELVfModVol_volumeParams', null)
62                 execution.setVariable('DELVfModVol_volumeGroupHeatStackId', null)
63                 execution.setVariable('DELVfModVol_volumeGroupTenantId', null)
64                 execution.setVariable("DELVfModVol_queryAAIVolGrpResponse", null)
65                 execution.setVariable('DELVfModVol_messageId', null)
66                 execution.setVariable('DELVfModVol_deleteVnfARequest', null)
67                 execution.setVariable('DELVfModVol_updateInfraRequest', null)
68                 execution.setVariable('DELVfModVol_CompleteMsoProcessRequest', null)
69                 execution.setVariable('DELVfModVol_WorkflowException', null)
70                 execution.setVariable('DELVfModVol_TransactionSuccessIndicator', false)
71                 execution.setVariable("DELVfModVol_isErrorMessageException", false)
72                 execution.setVariable('DELVfModVol_syncResponseSent', false)
73         }
74
75         /**
76          * Perform initial processing, such as request validation, initialization of variables, etc.
77          * * @param execution
78          */
79         public void preProcessRequest (Execution execution) {
80                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
81                 preProcessRequest(execution, isDebugEnabled)
82         }
83
84         /**
85          * This method is executed during the preProcessRequest task of the <class>DeleteVfModuleVolume.bpmn</class> process.
86          * @param execution
87          */
88         public void preProcessRequest (Execution execution, isDebugLogEnabled) {
89
90                 InitializeProcessVariables(execution)
91
92                 String createVolumeIncoming = validateRequest(execution)
93                 utils.logAudit(createVolumeIncoming)
94
95                 // check if request is xml or json
96                 try {
97                         def jsonSlurper = new JsonSlurper()
98                         Map reqMap = jsonSlurper.parseText(createVolumeIncoming)
99                         utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
100
101                         def serviceInstanceId = execution.getVariable('serviceInstanceId')
102                         def volumeGroupId = execution.getVariable('volumeGroupId')
103                         def vidUtils = new VidUtils(this)
104                         createVolumeIncoming = vidUtils.createXmlVolumeRequest(reqMap, 'DELETE_VF_MODULE_VOL', serviceInstanceId, volumeGroupId)
105                         execution.setVariable("DELVfModVol_isVidRequest", true)
106                 }
107                 catch(groovy.json.JsonException je) {
108                         utils.log("DEBUG", " Request is in XML format.", isDebugLogEnabled)
109                         // assume request is in XML format - proceed as usual to process XML request
110                 }
111
112                 String request = utils.getNodeXml(createVolumeIncoming, "volume-request").drop(38).trim().replace("tag0:","").replace(":tag0","")
113                 execution.setVariable("DELVfModVol_volumeRequest", request)
114
115                 def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
116                 execution.setVariable('DELVfModVol_requestInfo', requestInfo)
117                 String requestId = execution.getVariable("mso-request-id")
118                 if (requestId == null || requestId == "") {
119                         requestId = getRequiredNodeText(execution, requestInfo, 'request-id')
120                 }
121                 execution.setVariable('DELVfModVol_requestId', requestId)
122                 execution.setVariable('DELVfModVol_source', getNodeTextForce(requestInfo, 'source'))
123
124                 def volumeInputs = getRequiredNodeXml(execution, request, 'volume-inputs')
125                 execution.setVariable('DELVfModVol_volumeInputs', volumeInputs)
126                 execution.setVariable('DELVfModVol_volumeGroupId', getRequiredNodeText(execution, volumeInputs, 'volume-group-id'))
127                 execution.setVariable('DELVfModVol_vnfType', getRequiredNodeText(execution, volumeInputs, 'vnf-type'))
128                 execution.setVariable('DELVfModVol_serviceId', getRequiredNodeText(execution, volumeInputs, 'service-id'))
129                 execution.setVariable('DELVfModVol_tenantId', getRequiredNodeText(execution, volumeInputs, 'tenant-id'))
130                 execution.setVariable('DELVfModVol_messageId', UUID.randomUUID().toString())
131                 execution.setVariable('DELVfModVol_volumeOutputs', utils.getNodeXml(request, 'volume-outputs', false))
132                 execution.setVariable('DELVfModVol_volumeParams', utils.getNodeXml(request, 'volume-params'))
133                 execution.setVariable('DELVfModVol_cloudRegion', utils.getNodeText1(request, 'aic-cloud-region'))
134
135                 try {
136                         // Catalog DB headers Authorization
137                         String basicAuthValueDB = execution.getVariable("URN_mso_adapters_db_auth")
138                         utils.log("DEBUG", " Obtained BasicAuth userid password for Catalog DB adapter: " + basicAuthValueDB, isDebugLogEnabled)
139                         
140                         def encodedString = utils.getBasicAuth(basicAuthValueDB, execution.getVariable("URN_mso_msoKey"))
141                         execution.setVariable("BasicAuthHeaderValueDB",encodedString)
142                 } catch (IOException ex) {
143                         String dataErrorMessage = " Unable to encode Catalog DB user/password string - " + ex.getMessage()
144                         utils.log("DEBUG", dataErrorMessage, isDebugLogEnabled)
145                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
146                 }
147                 
148                 logDebug('Request: ' + createVolumeIncoming, isDebugLogEnabled)
149         }
150
151         public void sendSyncResponse (Execution execution, isDebugEnabled) {
152
153                 String volumeRequest = execution.getVariable("DELVfModVol_volumeRequest")
154                 utils.log("DEBUG", " DELVfModVol_volumeRequest - " + "\n" + volumeRequest, isDebugEnabled)
155                 // RESTResponse (for API Handler (APIH) Reply Task)
156                 String deleteVolumeRequest =
157                                 """<rest:RESTResponse xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd" statusCode="200">
158                 <rest:payload xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"
159                           contentType="text/xml">
160                                 ${volumeRequest}
161                 </rest:payload>
162                        </rest:RESTResponse>""".trim()
163
164                 def isVidRequest = execution.getVariable('DELVfModVol_isVidRequest')
165                 def syncResponse = ''
166
167                 if(isVidRequest) {
168                         def serviceInstanceId = execution.getVariable('serviceInstanceId')
169                         def volumeGroupId = execution.getVariable('volumeGroupId')
170                         def requestId = execution.getVariable('DELVfModVol_requestId')
171                         syncResponse = """{"requestReferences":{"instanceId":"${volumeGroupId}","requestId":"${requestId}"}}""".trim()
172                 }
173                 else {
174                         syncResponse = utils.formatXml(deleteVolumeRequest)
175                 }
176
177                 execution.setVariable('DELVfModVol_syncResponseSent', true)
178
179                 sendWorkflowResponse(execution, 200, syncResponse)
180         }
181
182
183         public void sendSyncError (Execution execution, isDebugEnabled) {
184                 WorkflowException we = execution.getVariable('WorkflowException')
185                 def errorCode = we?.getErrorCode()
186                 def errorMessage = we?.getErrorMessage()
187                 //default to 400 since only invalid request will trigger this method
188                 sendWorkflowResponse(execution, 400, errorMessage)
189         }
190
191
192         public void callRESTQueryAAICloudRegion (Execution execution, isDebugEnabled) {
193
194                 String cloudRegion = execution.getVariable('DELVfModVol_cloudRegion')
195
196                 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
197                 AaiUtil aaiUtil = new AaiUtil(this)
198                 String aai_uri = aaiUtil.getCloudInfrastructureCloudRegionUri(execution)
199                 String queryCloudRegionRequest = "${aai_endpoint}${aai_uri}/" + cloudRegion
200                 utils.logAudit(queryCloudRegionRequest)
201                 execution.setVariable("DELVfModVol_queryCloudRegionRequest", queryCloudRegionRequest)
202                 utils.log("DEBUG", " DELVfModVol_queryCloudRegionRequest - " + "\n" + queryCloudRegionRequest, isDebugEnabled)
203
204                 cloudRegion = aaiUtil.getAAICloudReqion(execution,  queryCloudRegionRequest, "PO", cloudRegion)
205
206                 ExceptionUtil exceptionUtil = new ExceptionUtil()
207
208                 if ((cloudRegion != "ERROR")) {
209                         if(execution.getVariable("DELVfModVol_queryCloudRegionReturnCode") == "404"){
210                                 execution.setVariable("DELVfModVol_aicCloudRegion", "AAIAIC25")
211                         }else{
212                                 execution.setVariable("DELVfModVol_aicCloudRegion", cloudRegion)
213                         }
214                         execution.setVariable("DELVfModVol_cloudRegion", cloudRegion)
215                         execution.setVariable("DELVfModVol_isCloudRegionGood", true)
216
217                 } else {
218                         utils.log("DEBUG", "AAI Query Cloud Region Unsuccessful.", isDebugEnabled)
219                         execution.setVariable("DELVfModVol_isCloudRegionGood", false)
220                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "AAI Query Cloud Region Unsuccessful. Return Code: " + execution.getVariable("DELVfModVol_queryCloudRegionReturnCode"))
221                 }
222
223                 utils.log("DEBUG", " is Cloud Region Good: " + execution.getVariable("DELVfModVol_isCloudRegionGood"), isDebugEnabled)
224         }
225
226         /**
227          * Query volume group by id
228          * @param execution
229          */
230         public void queryAAIForVolumeGroup(Execution execution, isDebugLogEnabled) {
231
232                 ExceptionUtil exceptionUtil = new ExceptionUtil()
233
234                 def volumeGroupId = execution.getVariable('DELVfModVol_volumeGroupId')
235                 if(volumeGroupId == null) {
236                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, 'volume-group-id is not provided in the request')
237                         throw new Exception('volume-group-id is not provided in the request')
238                 }
239                 String cloudRegion = execution.getVariable('DELVfModVol_aicCloudRegion')
240
241                 AaiUtil aaiUtil = new AaiUtil(this)
242                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
243                 String queryAAIVolumeGroupRequest = aaiEndpoint + '/' + URLEncoder.encode(cloudRegion, "UTF-8") + "/volume-groups/volume-group/" + UriUtils.encode(volumeGroupId, "UTF-8")
244
245                 utils.logAudit('Query AAI volume group by ID: ' + queryAAIVolumeGroupRequest)
246                 logDebug('Query AAI volume group by ID: ' + queryAAIVolumeGroupRequest, isDebugLogEnabled)
247
248                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIVolumeGroupRequest)
249
250                 String returnCode = response.getStatusCode()
251                 String aaiResponseAsString = response.getResponseBodyAsString()
252                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
253
254                 utils.logAudit("AAI query volume group by id return code: " + returnCode)
255                 utils.logAudit("AAI query volume group by id response: " + aaiResponseAsString)
256
257                 execution.setVariable("DELVfModVol_queryAAIVolGrpResponse", aaiResponseAsString)
258
259                 if (returnCode=='200' || returnCode == '204') {
260
261                         def heatStackId = getNodeTextForce(aaiResponseAsString, 'heat-stack-id')
262                         execution.setVariable('DELVfModVol_volumeGroupHeatStackId', heatStackId)
263
264                         if(hasVfModuleRelationship(aaiResponseAsString)){
265                                 utils.log("DEBUG", 'Volume Group ' + volumeGroupId + ' currently in use', isDebugLogEnabled)
266                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume Group ${volumeGroupId} currently in use - found vf-module relationship.")
267                         }
268
269                         def volumeGroupTenantId = getTenantIdFromVolumeGroup(aaiResponseAsString)
270                         if (volumeGroupTenantId == null) {
271                                 utils.log("DEBUG", "Could not find Tenant Id element in Volume Group with Volume Group Id ${volumeGroupId}", isDebugLogEnabled)
272                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Could not find Tenant Id element in Volume Group with Volume Group Id ${volumeGroupId}")
273                         }
274
275                         execution.setVariable('DELVfModVol_volumeGroupTenantId', volumeGroupTenantId)
276                         logDebug('Received Tenant Id ' + volumeGroupTenantId + ' from AAI for Volume Group with Volume Group Id ' + volumeGroupId , isDebugLogEnabled)
277                 }
278                 else {
279                         if (returnCode=='404') {
280                                 utils.log("DEBUG", "Volume Group ${volumeGroupId} not found in AAI", isDebugLogEnabled)
281                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume Group ${volumeGroupId} not found in AAI. Response code: 404")
282                         }
283                         else {
284                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
285                                 throw new BpmnError("MSOWorkflowException")
286                         }
287                 }
288         }
289
290         /**
291          * Extract the Tenant Id from the Volume Group information returned by AAI.
292          *
293          * @param volumeGroupXml Volume Group XML returned by AAI.
294          * @return the Tenant Id extracted from the Volume Group information. 'null' is returned if
295          * the Tenant Id is missing or could not otherwise be extracted.
296          */
297         private String getTenantIdFromVolumeGroup(String volumeGroupXml) {
298                 def Node volumeGroupNode = xmlParser.parseText(volumeGroupXml)
299                 def Node relationshipList = utils.getChildNode(volumeGroupNode, 'relationship-list')
300                 if (relationshipList != null) {
301                         def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
302                         for (Node relationship in relationships) {
303                                 def Node relatedTo = utils.getChildNode(relationship, 'related-to')
304                                 if ((relatedTo != null) && (relatedTo.text().equals('tenant'))) {
305                                         def NodeList relationshipDataList = utils.getIdenticalChildren(relationship, 'relationship-data')
306                                         for (Node relationshipData in relationshipDataList) {
307                                                 def Node relationshipKey = utils.getChildNode(relationshipData, 'relationship-key')
308                                                 if ((relationshipKey != null) && (relationshipKey.text().equals('tenant.tenant-id'))) {
309                                                         def Node relationshipValue = utils.getChildNode(relationshipData, 'relationship-value')
310                                                         if (relationshipValue != null) {
311                                                                 return relationshipValue.text()
312                                                         }
313                                                 }
314                                         }
315                                 }
316                         }
317                 }
318                 return null
319         }
320
321         private boolean hasVnfRelationship(String volumeGroupXml) {
322                 def Node volumeGroupNode = xmlParser.parseText(volumeGroupXml)
323                 def Node relationshipList = utils.getChildNode(volumeGroupNode, 'relationship-list')
324                 if (relationshipList != null) {
325                         def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
326                         for (Node relationship in relationships) {
327                                 def Node relatedTo = utils.getChildNode(relationship, 'related-to')
328                                 if ((relatedTo != null) && (relatedTo.text().equals('generic-vnf'))) {
329                                         def Node relatedLink = utils.getChildNode(relationship, 'related-link')
330                                         if (relatedLink !=null && relatedLink.text() != null){
331                                                 return true
332                                         }
333                                 }
334                         }
335                 }
336                 return false
337         }
338
339         private boolean hasVfModuleRelationship(String volumeGroupXml) {
340                 def Node volumeGroupNode = xmlParser.parseText(volumeGroupXml)
341                 def Node relationshipList = utils.getChildNode(volumeGroupNode, 'relationship-list')
342                 if (relationshipList != null) {
343                         def NodeList relationships = utils.getIdenticalChildren(relationshipList, 'relationship')
344                         for (Node relationship in relationships) {
345                                 def Node relatedTo = utils.getChildNode(relationship, 'related-to')
346                                 if ((relatedTo != null) && (relatedTo.text().equals('vf-module'))) {
347                                         def Node relatedLink = utils.getChildNode(relationship, 'related-link')
348                                         if (relatedLink !=null && relatedLink.text() != null){
349                                                 return true
350                                         }
351                                 }
352                         }
353                 }
354                 return false
355         }
356
357         public void prepareVnfAdapterDeleteRequest(Execution execution, isDebugLogEnabled) {
358                 def cloudRegion = execution.getVariable('DELVfModVol_cloudRegion')
359                 def tenantId = execution.getVariable('DELVfModVol_tenantId')
360                 def volumeGroupId = execution.getVariable('DELVfModVol_volumeGroupId')
361                 def volumeGroupHeatStackId = execution.getVariable('DELVfModVol_volumeGroupHeatStackId')
362                 def requestId = execution.getVariable('DELVfModVol_requestId')
363                 def serviceId = execution.getVariable('DELVfModVol_serviceId')
364
365                 def messageId = execution.getVariable('DELVfModVol_messageId')
366                 def notificationUrl = createCallbackURL(execution, "VNFAResponse", messageId) 
367                 def useQualifiedHostName = execution.getVariable("URN_mso_use_qualified_host")
368                 if ('true'.equals(useQualifiedHostName)) {
369                                 notificationUrl = utils.getQualifiedHostNameForCallback(notificationUrl)
370                 }
371
372                 String vnfAdapterRestRequest = """
373                         <deleteVolumeGroupRequest>
374                                 <cloudSiteId>${cloudRegion}</cloudSiteId>
375                                 <tenantId>${tenantId}</tenantId>
376                                 <volumeGroupId>${volumeGroupId}</volumeGroupId>
377                                 <volumeGroupStackId>${volumeGroupHeatStackId}</volumeGroupStackId>
378                                 <skipAAI>true</skipAAI>
379                             <msoRequest>
380                                 <requestId>${requestId}</requestId>
381                                 <serviceInstanceId>${serviceId}</serviceInstanceId>
382                             </msoRequest>
383                             <messageId>${messageId}</messageId>
384                             <notificationUrl>${notificationUrl}</notificationUrl>
385                         </deleteVolumeGroupRequest>
386                 """
387                 vnfAdapterRestRequest = utils.formatXml(vnfAdapterRestRequest)
388                 execution.setVariable('DELVfModVol_deleteVnfARequest', vnfAdapterRestRequest)
389                 logDebug('Request for VNFAdapter Rest:\n' + vnfAdapterRestRequest, isDebugLogEnabled)
390         }
391
392
393         public void deleteVolGrpId(Execution execution, isDebugEnabled) {
394
395                 // get variables
396                 String queryAAIVolGrpIdResponse = execution.getVariable("DELVfModVol_queryAAIVolGrpResponse")
397                 String groupId = utils.getNodeText(queryAAIVolGrpIdResponse, "volume-group-id")
398                 String resourceVersion = utils.getNodeText(queryAAIVolGrpIdResponse, "resource-version")
399                 String messageId = execution.getVariable('DELVfModVol_messageId')
400                 String cloudRegion = execution.getVariable('DELVfModVol_aicCloudRegion')
401
402                 AaiUtil aaiUtil = new AaiUtil(this)
403                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)
404                 String deleteAAIVolumeGrpIdRequest = aaiEndpoint + '/' + URLEncoder.encode(cloudRegion, "UTF-8")  + "/volume-groups/volume-group/" +  UriUtils.encode(groupId, "UTF-8")
405
406                 if(resourceVersion !=null){
407                         deleteAAIVolumeGrpIdRequest = deleteAAIVolumeGrpIdRequest +'?resource-version=' + UriUtils.encode(resourceVersion, 'UTF-8')
408                 }
409
410                 utils.logAudit('Delete AAI volume group : ' + deleteAAIVolumeGrpIdRequest)
411                 utils.log("DEBUG", "Delete AAI volume group : " + deleteAAIVolumeGrpIdRequest, isDebugEnabled)
412
413                 APIResponse response = aaiUtil.executeAAIDeleteCall(execution, deleteAAIVolumeGrpIdRequest)
414
415                 String returnCode = response.getStatusCode()
416                 String aaiResponseAsString = response.getResponseBodyAsString()
417                 aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)
418
419                 utils.logAudit("AAI delete volume group return code: " + returnCode)
420                 utils.logAudit("AAI delete volume group response: " + aaiResponseAsString)
421
422                 ExceptionUtil exceptionUtil = new ExceptionUtil()
423                 if (returnCode=='200' || (returnCode == '204')) {
424                         utils.log("DEBUG", "Volume group $groupId deleted.", isDebugEnabled)
425                 } else {
426                         if (returnCode=='404') {
427                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $groupId not found for delete in AAI Response code: 404")
428                         } else {
429                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
430                                 throw new BpmnError("MSOWorkflowException")
431                         }
432                 }
433         }
434
435
436         public void prepareDBRequest (Execution execution, isDebugLogEnabled) {
437
438                 WorkflowException workflowExceptionObj = execution.getVariable("WorkflowException")
439
440                 def requestId = execution.getVariable('DELVfModVol_requestId')
441                 def volOutputs = execution.getVariable('DELVfModVol_volumeOutputs')
442                 def statusMessage = "VolumeGroup successfully deleted"
443                 def progress = "100"
444                 def requestStatus = "COMPLETE"
445
446                 if (workflowExceptionObj != null) {
447                         statusMessage = (workflowExceptionObj.getErrorMessage()).replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
448                         execution.setVariable("DELVfModVol_WorkflowExceptionMessage", statusMessage)
449                         execution.setVariable("DELVfModVol_WorkflowExceptionCode", workflowExceptionObj.getErrorCode())
450                         requestStatus = "FAILURE"
451                         progress = ""
452                 }
453
454                 String updateInfraRequest = """
455                         <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
456                                         xmlns:req="http://org.openecomp.mso/requestsdb">
457                                 <soapenv:Header/>
458                                 <soapenv:Body>
459                                         <req:updateInfraRequest>
460                                                 <requestId>${requestId}</requestId>
461                                                 <lastModifiedBy>BPMN</lastModifiedBy>
462                                                 <statusMessage>${statusMessage}</statusMessage>
463                                                 <requestStatus>${requestStatus}</requestStatus>
464                                                 <progress>${progress}</progress>
465                                                 <vnfOutputs>${volOutputs}</vnfOutputs>
466                                         </req:updateInfraRequest>
467                                 </soapenv:Body>
468                         </soapenv:Envelope>
469                 """
470
471                 updateInfraRequest = utils.formatXml(updateInfraRequest)
472                 execution.setVariable('DELVfModVol_updateInfraRequest', updateInfraRequest)
473                 logDebug('Request for Update Infra Request:\n' + updateInfraRequest, isDebugLogEnabled)
474
475         }
476
477
478         public void prepareCompletionHandlerRequest (Execution execution, isDebugLogEnabled) {
479                 def requestId = execution.getVariable("mso-request-id")
480                 def source = execution.getVariable("DELVfModVol_source")
481
482                 String msoCompletionRequest =
483                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
484                                                         xmlns:ns="http://org.openecomp/mso/request/types/v1">
485                                         <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
486                                                 <request-id>${requestId}</request-id>
487                                                 <action>DELETE</action>
488                                                 <source>${source}</source>
489                                         </request-info>
490                                         <aetgt:status-message>Volume Group has been deleted successfully.</aetgt:status-message>
491                                         <aetgt:mso-bpel-name>BPMN VF Module Volume action: DELETE</aetgt:mso-bpel-name>
492                                 </aetgt:MsoCompletionRequest>"""
493
494                 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
495                 execution.setVariable('DELVfModVol_CompleteMsoProcessRequest', xmlMsoCompletionRequest)
496                 utils.log("DEBUG", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugLogEnabled)
497
498         }
499
500
501
502         public void prepareFalloutHandler (Execution execution, isDebugEnabled) {
503
504                 execution.setVariable("DELVfModVol_Success", false)
505                 String requestId = execution.getVariable("DELVfModVol_requestId")
506                 String source = execution.getVariable("DELVfModVol_source")
507
508                 WorkflowException workflowExceptionObj = execution.getVariable("WorkflowException")
509                 def errorMessage = workflowExceptionObj.getErrorMessage()
510                 def errorCode =  workflowExceptionObj.getErrorCode()
511
512                 String falloutHandlerRequest =
513                     """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.openecomp/mso/workflow/schema/v1"
514                                                              xmlns:ns="http://org.openecomp/mso/request/types/v1"
515                                                              xmlns:wfsch="http://org.openecomp/mso/workflow/schema/v1">
516                                    <request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">
517                                       <request-id>${requestId}</request-id>
518                                       <action>DELETE</action>
519                                       <source>${source}</source>
520                                    </request-info>
521                                         <aetgt:WorkflowException>
522                                       <aetgt:ErrorMessage>${errorMessage}</aetgt:ErrorMessage>
523                                       <aetgt:ErrorCode>${errorCode}</aetgt:ErrorCode>
524                                         </aetgt:WorkflowException>
525                                 </aetgt:FalloutHandlerRequest>"""
526
527                 // Format Response
528                 String xmlHandlerRequest = utils.formatXml(falloutHandlerRequest)
529                 utils.logAudit(xmlHandlerRequest)
530
531                 execution.setVariable("DELVfModVol_FalloutHandlerRequest", xmlHandlerRequest)
532                 utils.log("ERROR", "  Overall Error Response going to FalloutHandler: " + "\n" + xmlHandlerRequest, isDebugEnabled)
533
534         }
535
536
537         /**
538          * Create a WorkflowException for the error case where the Tenant Id from
539          * AAI did not match the Tenant Id in the incoming request.
540          *
541          * @param execution The flow's execution instance.
542          */
543         public void handleTenantIdMismatch(Execution execution, isDebugLogEnabled) {
544
545                 def volumeGroupId = execution.getVariable('DELVfModVol_volumeGroupId')
546                 def aicCloudRegion = execution.getVariable('DELVfModVol_aicCloudRegion')
547                 def tenantId = execution.getVariable('DELVfModVol_tenantId')
548                 def volumeGroupTenantId = execution.getVariable('DELVfModVol_volumeGroupTenantId')
549
550                 def String errorMessage = 'TenantId ' + tenantId + ' in incoming request does not match Tenant Id ' + volumeGroupTenantId +
551                         ' retrieved from AAI for Volume Group Id ' + volumeGroupId
552
553                 logError('Error in DeleteVfModuleVolume: ' + errorMessage)
554
555                 ExceptionUtil exceptionUtil = new ExceptionUtil()
556                 exceptionUtil.buildWorkflowException(execution, 5000, errorMessage)
557
558         }
559
560 }