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