8968c751c5e18e82f9db32152c49a12f2f951fce
[so.git] / bpmn / so-bpmn-infrastructure-common / 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.client.aai.entities.uri.AAIResourceUri
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory
43 import org.onap.so.client.aai.AAIObjectType
44 import org.onap.so.client.aai.AAIObjectPlurals
45 import org.onap.so.constants.Defaults
46 import org.onap.so.rest.APIResponse
47 import org.springframework.web.util.UriUtils
48 import org.onap.so.logger.MsoLogger
49 import org.onap.so.logger.MessageEnum
50
51
52 public class DoCreateVfModuleVolumeRollback extends AbstractServiceTaskProcessor {
53         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateVfModuleVolumeRollback.class);
54
55         String Prefix="DCVFMODVOLRBK_"
56         ExceptionUtil exceptionUtil = new ExceptionUtil()
57         JsonUtils jsonUtil = new JsonUtils()
58         VidUtils vidUtils = new VidUtils(this)
59
60         def className = getClass().getSimpleName()
61
62         /**
63          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.
64          * @param execution
65          */
66         public InitializeProcessVariables(DelegateExecution execution){
67                 /* Initialize all the process variables in this block */
68
69                 execution.setVariable(Prefix + "volumeGroupName", null)
70                 execution.setVariable(Prefix + "lcpCloudRegionId", null)
71                 execution.setVariable(Prefix + "rollbackVnfARequest", null)
72
73         }
74
75         // **************************************************
76         //     Pre or Prepare Request Section
77         // **************************************************
78         /**
79          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.
80          * @param execution
81          */
82         public void preProcessRequest (DelegateExecution execution) {
83                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
84
85                 InitializeProcessVariables(execution)
86 //              rollbackData.put("DCVFMODULEVOL", "aiccloudregion", cloudSiteId)
87                 RollbackData rollbackData = execution.getVariable("rollbackData")
88
89 //              String vnfId = rollbackData.get("DCVFMODULEVOL", "vnfid")
90 //              execution.setVariable("DCVFMODVOLRBK_vnfId", vnfId)
91 //              String vfModuleId = rollbackData.get("DCVFMODULEVOL", "vfmoduleid")
92 //              execution.setVariable("DCVFMODVOLRBK_vfModuleId", vfModuleId)
93 //              String source = rollbackData.get("DCVFMODULEVOL", "source")
94 //              execution.setVariable("DCVFMODVOLRBK_source", source)
95 //              String serviceInstanceId = rollbackData.get("DCVFMODULEVOL", "serviceInstanceId")
96 //              execution.setVariable("DCVFMODVOLRBK_serviceInstanceId", serviceInstanceId)
97 //              String serviceId = rollbackData.get("DCVFMODULEVOL", "service-id")
98 //              execution.setVariable("DCVFMODVOLRBK_serviceId", serviceId)
99 //              String vnfType = rollbackData.get("DCVFMODULEVOL", "vnftype")
100 //              execution.setVariable("DCVFMODVOLRBK_vnfType", vnfType)
101 //              String vnfName = rollbackData.get("DCVFMODULEVOL", "vnfname")
102 //              execution.setVariable("DCVFMODVOLRBK_vnfName", vnfName)
103 //              String tenantId = rollbackData.get("DCVFMODULEVOL", "tenantid")
104 //              execution.setVariable("DCVFMODVOLRBK_tenantId", tenantId)
105 //              String vfModuleName = rollbackData.get("DCVFMODULEVOL", "vfmodulename")
106 //              execution.setVariable("DCVFMODVOLRBK_vfModuleName", vfModuleName)
107 //              String vfModuleModelName = rollbackData.get("DCVFMODULEVOL", "vfmodulemodelname")
108 //              execution.setVariable("DCVFMODVOLRBK_vfModuleModelName", vfModuleModelName)
109 //              String cloudSiteId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")
110 //              execution.setVariable("DCVFMODVOLRBK_cloudSiteId", cloudSiteId)
111 //              String heatStackId = rollbackData.get("DCVFMODULEVOL", "heatstackid")
112 //              execution.setVariable("DCVFMODVOLRBK_heatStackId", heatStackId)
113 //              String requestId = rollbackData.get("DCVFMODULEVOL", "msorequestid")
114 //              execution.setVariable("DCVFMODVOLRBK_requestId", requestId)
115
116                 String volumeGroupName = rollbackData.get("DCVFMODULEVOL", "volumeGroupName")
117                 execution.setVariable("DCVFMODVOLRBK_volumeGroupName", volumeGroupName)
118
119                 String lcpCloudRegionId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")
120                 execution.setVariable("DCVFMODVOLRBK_lcpCloudRegionId", lcpCloudRegionId)
121
122                 execution.setVariable("DCVFMODVOLRBK_rollbackVnfARequest", rollbackData.get("DCVFMODULEVOL", "rollbackVnfARequest"))
123                 execution.setVariable("DCVFMODVOLRBK_backoutOnFailure", rollbackData.get("DCVFMODULEVOL", "backoutOnFailure"))
124                 execution.setVariable("DCVFMODVOLRBK_isCreateVnfRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isCreateVnfRollbackNeeded"))
125                 execution.setVariable("DCVFMODVOLRBK_isAAIRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isAAIRollbackNeeded"))
126
127         }
128
129         /**
130          * Query AAI volume group by name
131          * @param execution
132          * @param isDebugEnabled
133          */
134         public void callRESTQueryAAIVolGrpName(DelegateExecution execution, isDebugEnabled) {
135
136                 def volumeGroupName = execution.getVariable('DCVFMODVOLRBK_volumeGroupName')
137                 def cloudRegion = execution.getVariable('DCVFMODVOLRBK_lcpCloudRegionId')
138
139                 // This is for stub testing
140                 def testVolumeGroupName = execution.getVariable('test-volume-group-name')
141                 if (testVolumeGroupName != null && testVolumeGroupName.length() > 0) {
142                         volumeGroupName = testVolumeGroupName
143                 }
144
145                 AaiUtil aaiUtil = new AaiUtil(this)
146
147                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), cloudRegion).queryParam("volume-group-name", volumeGroupName)
148                 def queryAAIVolumeNameRequest = aaiUtil.createAaiUri(uri)
149
150                 msoLogger.debug('Query AAI volume group by name: ' + queryAAIVolumeNameRequest)
151
152                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIVolumeNameRequest)
153
154                 String returnCode = response.getStatusCode()
155                 String aaiResponseAsString = response.getResponseBodyAsString()
156
157                 msoLogger.debug("AAI query volume group by name return code: " + returnCode)
158                 msoLogger.debug("AAI query volume group by name response: " + aaiResponseAsString)
159
160                 ExceptionUtil exceptionUtil = new ExceptionUtil()
161
162                 execution.setVariable(prefix+"queryAAIVolGrpNameResponse", aaiResponseAsString)
163                 execution.setVariable(prefix+'AaiReturnCode', returnCode)
164
165                 if (returnCode=='200') {
166                         // @TODO: verify error code
167                         // @TODO: create class of literals representing error codes
168                         execution.setVariable(prefix+'queryAAIVolGrpNameResponse', aaiResponseAsString)
169                         msoLogger.debug("Volume Group Name $volumeGroupName exists in AAI.")
170                 } else {
171                         if (returnCode=='404') {
172                                 msoLogger.debug("Volume Group Name $volumeGroupName does not exist in AAI.")
173                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupName not found in AAI. Response code: 404")
174                         } else {
175                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
176                                 throw new BpmnError("MSOWorkflowException")
177                         }
178                 }
179         }
180
181
182
183         public void callRESTDeleteAAIVolumeGroup(DelegateExecution execution, isDebugEnabled) {
184
185                 callRESTQueryAAIVolGrpName(execution, isDebugEnabled)
186
187                 def queryAaiVolumeGroupResponse = execution.getVariable(prefix+'queryAAIVolGrpNameResponse')
188
189                 def volumeGroupId = utils.getNodeText(queryAaiVolumeGroupResponse, "volume-group-id")
190                 def resourceVersion = utils.getNodeText(queryAaiVolumeGroupResponse, "resource-version")
191
192                 def cloudRegion = execution.getVariable("DCVFMODVOLRBK_lcpCloudRegionId")
193
194                 AaiUtil aaiUtil = new AaiUtil(this)
195
196                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), cloudRegion, volumeGroupId).resourceVersion(resourceVersion)
197                 def deleteAAIVolumeGrpIdRequest = aaiUtil.createAaiUri(uri)
198
199                 msoLogger.debug('Delete AAI volume group : ' + deleteAAIVolumeGrpIdRequest)
200
201                 APIResponse response = aaiUtil.executeAAIDeleteCall(execution, deleteAAIVolumeGrpIdRequest)
202
203                 String returnCode = response.getStatusCode()
204                 String aaiResponseAsString = response.getResponseBodyAsString()
205
206                 msoLogger.debug("AAI delete volume group return code: " + returnCode)
207                 msoLogger.debug("AAI delete volume group response: " + aaiResponseAsString)
208
209                 ExceptionUtil exceptionUtil = new ExceptionUtil()
210
211                 def volumeGroupNameFound = prefix+'volumeGroupNameFound'
212                 if (returnCode=='200' || returnCode=='204' ) {
213                         msoLogger.debug("Volume group $volumeGroupId deleted.")
214                 } else {
215                         if (returnCode=='404') {
216                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupId not found for delete in AAI Response code: 404")
217                         } else {
218                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
219                                 throw new BpmnError("MSOWorkflowException")
220                         }
221                 }
222         }
223
224         // *******************************
225         //     Build Error Section
226         // *******************************
227
228
229
230         public void processJavaException(DelegateExecution execution){
231                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
232                 execution.setVariable("prefix",Prefix)
233
234                 try{
235                         msoLogger.debug("Caught a Java Exception in " + Prefix)
236                         msoLogger.debug("Started processJavaException Method")
237                         msoLogger.debug("Variables List: " + execution.getVariables())
238                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
239                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
240
241                 }catch(Exception e){
242                         msoLogger.debug("Caught Exception during processJavaException Method: " + e)
243                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
244                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
245                 }
246                 msoLogger.debug("Completed processJavaException Method in " + Prefix)
247         }
248
249 }