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