1710 Rebase - Second Attempt
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateVfModuleVolumeRollback.groovy
1 /*\r
2  * © 2016 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.\r
3  */\r
4 package org.openecomp.mso.bpmn.infrastructure.scripts;\r
5 \r
6 import groovy.xml.XmlUtil\r
7 import groovy.json.*\r
8 \r
9 \r
10 import java.util.UUID;\r
11 \r
12 import org.camunda.bpm.engine.delegate.BpmnError\r
13 import org.camunda.bpm.engine.runtime.Execution\r
14 import org.apache.commons.lang3.*\r
15 import org.apache.commons.codec.binary.Base64;\r
16 import org.openecomp.mso.bpmn.common.scripts.AaiUtil\r
17 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
18 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
19 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
20 import org.openecomp.mso.bpmn.core.RollbackData\r
21 import org.openecomp.mso.bpmn.core.WorkflowException\r
22 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
23 import org.openecomp.mso.rest.APIResponse\r
24 import org.springframework.web.util.UriUtils\r
25 \r
26 \r
27 public class DoCreateVfModuleVolumeRollback extends AbstractServiceTaskProcessor {\r
28         String Prefix="DCVFMODVOLRBK_"\r
29         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
30         JsonUtils jsonUtil = new JsonUtils()\r
31         VidUtils vidUtils = new VidUtils(this)\r
32 \r
33         def className = getClass().getSimpleName()\r
34         \r
35         /**\r
36          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.\r
37          * @param execution\r
38          */\r
39         public InitializeProcessVariables(Execution execution){\r
40                 /* Initialize all the process variables in this block */\r
41 \r
42                 execution.setVariable(Prefix + "volumeGroupName", null)\r
43                 execution.setVariable(Prefix + "lcpCloudRegionId", null)\r
44                 execution.setVariable(Prefix + "rollbackVnfARequest", null)\r
45 \r
46         }\r
47 \r
48         // **************************************************\r
49         //     Pre or Prepare Request Section\r
50         // **************************************************\r
51         /**\r
52          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.\r
53          * @param execution\r
54          */\r
55         public void preProcessRequest (Execution execution) {\r
56                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
57 \r
58                 InitializeProcessVariables(execution)\r
59 //              rollbackData.put("DCVFMODULEVOL", "aiccloudregion", cloudSiteId)\r
60                 RollbackData rollbackData = execution.getVariable("rollbackData")\r
61                 \r
62 //              String vnfId = rollbackData.get("DCVFMODULEVOL", "vnfid")\r
63 //              execution.setVariable("DCVFMODVOLRBK_vnfId", vnfId)\r
64 //              String vfModuleId = rollbackData.get("DCVFMODULEVOL", "vfmoduleid")\r
65 //              execution.setVariable("DCVFMODVOLRBK_vfModuleId", vfModuleId)\r
66 //              String source = rollbackData.get("DCVFMODULEVOL", "source")\r
67 //              execution.setVariable("DCVFMODVOLRBK_source", source)\r
68 //              String serviceInstanceId = rollbackData.get("DCVFMODULEVOL", "serviceInstanceId")\r
69 //              execution.setVariable("DCVFMODVOLRBK_serviceInstanceId", serviceInstanceId)\r
70 //              String serviceId = rollbackData.get("DCVFMODULEVOL", "service-id")\r
71 //              execution.setVariable("DCVFMODVOLRBK_serviceId", serviceId)\r
72 //              String vnfType = rollbackData.get("DCVFMODULEVOL", "vnftype")\r
73 //              execution.setVariable("DCVFMODVOLRBK_vnfType", vnfType)\r
74 //              String vnfName = rollbackData.get("DCVFMODULEVOL", "vnfname")\r
75 //              execution.setVariable("DCVFMODVOLRBK_vnfName", vnfName)\r
76 //              String tenantId = rollbackData.get("DCVFMODULEVOL", "tenantid")\r
77 //              execution.setVariable("DCVFMODVOLRBK_tenantId", tenantId)\r
78 //              String vfModuleName = rollbackData.get("DCVFMODULEVOL", "vfmodulename")\r
79 //              execution.setVariable("DCVFMODVOLRBK_vfModuleName", vfModuleName)\r
80 //              String vfModuleModelName = rollbackData.get("DCVFMODULEVOL", "vfmodulemodelname")\r
81 //              execution.setVariable("DCVFMODVOLRBK_vfModuleModelName", vfModuleModelName)\r
82 //              String cloudSiteId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")\r
83 //              execution.setVariable("DCVFMODVOLRBK_cloudSiteId", cloudSiteId)\r
84 //              String heatStackId = rollbackData.get("DCVFMODULEVOL", "heatstackid")\r
85 //              execution.setVariable("DCVFMODVOLRBK_heatStackId", heatStackId)\r
86 //              String requestId = rollbackData.get("DCVFMODULEVOL", "msorequestid")\r
87 //              execution.setVariable("DCVFMODVOLRBK_requestId", requestId)\r
88                 \r
89                 String volumeGroupName = rollbackData.get("DCVFMODULEVOL", "volumeGroupName")\r
90                 execution.setVariable("DCVFMODVOLRBK_volumeGroupName", volumeGroupName)\r
91 \r
92                 String lcpCloudRegionId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")\r
93                 execution.setVariable("DCVFMODVOLRBK_lcpCloudRegionId", lcpCloudRegionId)\r
94                 \r
95                 execution.setVariable("DCVFMODVOLRBK_rollbackVnfARequest", rollbackData.get("DCVFMODULEVOL", "rollbackVnfARequest"))\r
96                 execution.setVariable("DCVFMODVOLRBK_backoutOnFailure", rollbackData.get("DCVFMODULEVOL", "backoutOnFailure"))\r
97                 execution.setVariable("DCVFMODVOLRBK_isCreateVnfRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isCreateVnfRollbackNeeded"))\r
98                 execution.setVariable("DCVFMODVOLRBK_isAAIRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isAAIRollbackNeeded"))\r
99 \r
100         }\r
101         \r
102         /**\r
103          * Query AAI volume group by name\r
104          * @param execution\r
105          * @param isDebugEnabled\r
106          */\r
107         public void callRESTQueryAAIVolGrpName(Execution execution, isDebugEnabled) {\r
108                 \r
109                 def volumeGroupName = execution.getVariable('DCVFMODVOLRBK_volumeGroupName')\r
110                 def cloudRegion = execution.getVariable('DCVFMODVOLRBK_lcpCloudRegionId')\r
111                 \r
112                 // This is for stub testing\r
113                 def testVolumeGroupName = execution.getVariable('test-volume-group-name')\r
114                 if (testVolumeGroupName != null && testVolumeGroupName.length() > 0) {\r
115                         volumeGroupName = testVolumeGroupName\r
116                 }\r
117                 \r
118                 AaiUtil aaiUtil = new AaiUtil(this)\r
119                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)\r
120                 String queryAAIVolumeNameRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups" + "?volume-group-name=" + UriUtils.encode(volumeGroupName, 'UTF-8')\r
121 \r
122                 utils.logAudit('Query AAI volume group by name: ' + queryAAIVolumeNameRequest)\r
123                 \r
124                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIVolumeNameRequest)\r
125                 \r
126                 String returnCode = response.getStatusCode()\r
127                 String aaiResponseAsString = response.getResponseBodyAsString()\r
128                 //aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)\r
129                 \r
130                 utils.logAudit("AAI query volume group by name return code: " + returnCode)\r
131                 utils.logAudit("AAI query volume group by name response: " + aaiResponseAsString)\r
132 \r
133                 ExceptionUtil exceptionUtil = new ExceptionUtil()\r
134 \r
135                 execution.setVariable(prefix+"queryAAIVolGrpNameResponse", aaiResponseAsString)\r
136                 execution.setVariable(prefix+'AaiReturnCode', returnCode)\r
137 \r
138                 if (returnCode=='200') {\r
139                         // @TODO: verify error code\r
140                         // @TODO: create class of literals representing error codes\r
141                         execution.setVariable(prefix+'queryAAIVolGrpNameResponse', aaiResponseAsString)\r
142                         utils.log("DEBUG", "Volume Group Name $volumeGroupName exists in AAI.", isDebugEnabled)\r
143                 } else {\r
144                         if (returnCode=='404') {\r
145                                 utils.log("DEBUG", "Volume Group Name $volumeGroupName does not exist in AAI.", isDebugEnabled)\r
146                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupName not found in AAI. Response code: 404")\r
147                         } else {\r
148                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)\r
149                                 throw new BpmnError("MSOWorkflowException")\r
150                         }\r
151                 }\r
152         }\r
153         \r
154         \r
155         \r
156         public void callRESTDeleteAAIVolumeGroup(Execution execution, isDebugEnabled) {\r
157 \r
158                 callRESTQueryAAIVolGrpName(execution, isDebugEnabled)\r
159                 \r
160                 def queryAaiVolumeGroupResponse = execution.getVariable(prefix+'queryAAIVolGrpNameResponse')\r
161                 \r
162                 def volumeGroupId = utils.getNodeText(queryAaiVolumeGroupResponse, "volume-group-id")\r
163                 def resourceVersion = utils.getNodeText(queryAaiVolumeGroupResponse, "resource-version")\r
164 \r
165                 def cloudRegion = execution.getVariable("DCVFMODVOLRBK_lcpCloudRegionId")\r
166                 \r
167                 AaiUtil aaiUtil = new AaiUtil(this)\r
168                 String aaiEndpoint = aaiUtil.getCloudInfrastructureCloudRegionEndpoint(execution)\r
169                 String deleteAAIVolumeGrpIdRequest = aaiEndpoint + '/' + cloudRegion + "/volume-groups/volume-group" + '/' +  volumeGroupId + "?resource-version=" + UriUtils.encode(resourceVersion, "UTF-8")\r
170 \r
171                 utils.logAudit('Delete AAI volume group : ' + deleteAAIVolumeGrpIdRequest)\r
172                 \r
173                 APIResponse response = aaiUtil.executeAAIDeleteCall(execution, deleteAAIVolumeGrpIdRequest)\r
174                 \r
175                 String returnCode = response.getStatusCode()\r
176                 String aaiResponseAsString = response.getResponseBodyAsString()\r
177                 //aaiResponseAsString = StringEscapeUtils.unescapeXml(aaiResponseAsString)\r
178                 \r
179                 utils.logAudit("AAI delete volume group return code: " + returnCode)\r
180                 utils.logAudit("AAI delete volume group response: " + aaiResponseAsString)\r
181 \r
182                 ExceptionUtil exceptionUtil = new ExceptionUtil()\r
183 \r
184                 def volumeGroupNameFound = prefix+'volumeGroupNameFound'\r
185                 if (returnCode=='200' || returnCode=='204' ) {\r
186                         utils.log("DEBUG", "Volume group $volumeGroupId deleted.", isDebugEnabled)\r
187                 } else {\r
188                         if (returnCode=='404') {\r
189                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupId not found for delete in AAI Response code: 404")\r
190                         } else {\r
191                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)\r
192                                 throw new BpmnError("MSOWorkflowException")\r
193                         }\r
194                 }\r
195         }\r
196 \r
197         // *******************************\r
198         //     Build Error Section\r
199         // *******************************\r
200 \r
201 \r
202         \r
203         public void processJavaException(Execution execution){\r
204                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
205                 execution.setVariable("prefix",Prefix)\r
206                 \r
207                 try{\r
208                         utils.log("DEBUG", "Caught a Java Exception in " + Prefix, isDebugEnabled)\r
209                         utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)\r
210                         utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)\r
211                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated\r
212                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")\r
213                         \r
214                 }catch(Exception e){\r
215                         utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)\r
216                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated\r
217                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)\r
218                 }\r
219                 utils.log("DEBUG", "Completed processJavaException Method in " + Prefix, isDebugEnabled)\r
220         }\r
221 \r
222 }\r