Springboot 2.0 upgrade
[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
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.VolumeGroups
26 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.onap.so.bpmn.common.scripts.ExceptionUtil
28 import org.onap.so.bpmn.common.scripts.VidUtils
29 import org.onap.so.bpmn.core.RollbackData
30 import org.onap.so.bpmn.core.json.JsonUtils
31 import org.onap.so.client.aai.AAIObjectPlurals
32 import org.onap.so.client.aai.AAIObjectType
33 import org.onap.so.client.aai.entities.uri.AAIResourceUri
34 import org.onap.so.client.aai.entities.uri.AAIUriFactory
35 import org.onap.so.constants.Defaults
36 import org.onap.so.logger.MsoLogger
37
38 import javax.ws.rs.NotFoundException
39
40 public class DoCreateVfModuleVolumeRollback extends AbstractServiceTaskProcessor {
41         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateVfModuleVolumeRollback.class);
42
43         String Prefix="DCVFMODVOLRBK_"
44         ExceptionUtil exceptionUtil = new ExceptionUtil()
45         JsonUtils jsonUtil = new JsonUtils()
46         VidUtils vidUtils = new VidUtils(this)
47
48         def className = getClass().getSimpleName()
49
50         /**
51          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.
52          * @param execution
53          */
54         public InitializeProcessVariables(DelegateExecution execution){
55                 /* Initialize all the process variables in this block */
56
57                 execution.setVariable(Prefix + "volumeGroupName", null)
58                 execution.setVariable(Prefix + "lcpCloudRegionId", null)
59                 execution.setVariable(Prefix + "rollbackVnfARequest", null)
60
61         }
62
63         // **************************************************
64         //     Pre or Prepare Request Section
65         // **************************************************
66         /**
67          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.
68          * @param execution
69          */
70         public void preProcessRequest (DelegateExecution execution) {
71                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
72
73                 InitializeProcessVariables(execution)
74 //              rollbackData.put("DCVFMODULEVOL", "aiccloudregion", cloudSiteId)
75                 RollbackData rollbackData = execution.getVariable("rollbackData")
76
77 //              String vnfId = rollbackData.get("DCVFMODULEVOL", "vnfid")
78 //              execution.setVariable("DCVFMODVOLRBK_vnfId", vnfId)
79 //              String vfModuleId = rollbackData.get("DCVFMODULEVOL", "vfmoduleid")
80 //              execution.setVariable("DCVFMODVOLRBK_vfModuleId", vfModuleId)
81 //              String source = rollbackData.get("DCVFMODULEVOL", "source")
82 //              execution.setVariable("DCVFMODVOLRBK_source", source)
83 //              String serviceInstanceId = rollbackData.get("DCVFMODULEVOL", "serviceInstanceId")
84 //              execution.setVariable("DCVFMODVOLRBK_serviceInstanceId", serviceInstanceId)
85 //              String serviceId = rollbackData.get("DCVFMODULEVOL", "service-id")
86 //              execution.setVariable("DCVFMODVOLRBK_serviceId", serviceId)
87 //              String vnfType = rollbackData.get("DCVFMODULEVOL", "vnftype")
88 //              execution.setVariable("DCVFMODVOLRBK_vnfType", vnfType)
89 //              String vnfName = rollbackData.get("DCVFMODULEVOL", "vnfname")
90 //              execution.setVariable("DCVFMODVOLRBK_vnfName", vnfName)
91 //              String tenantId = rollbackData.get("DCVFMODULEVOL", "tenantid")
92 //              execution.setVariable("DCVFMODVOLRBK_tenantId", tenantId)
93 //              String vfModuleName = rollbackData.get("DCVFMODULEVOL", "vfmodulename")
94 //              execution.setVariable("DCVFMODVOLRBK_vfModuleName", vfModuleName)
95 //              String vfModuleModelName = rollbackData.get("DCVFMODULEVOL", "vfmodulemodelname")
96 //              execution.setVariable("DCVFMODVOLRBK_vfModuleModelName", vfModuleModelName)
97 //              String cloudSiteId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")
98 //              execution.setVariable("DCVFMODVOLRBK_cloudSiteId", cloudSiteId)
99 //              String heatStackId = rollbackData.get("DCVFMODULEVOL", "heatstackid")
100 //              execution.setVariable("DCVFMODVOLRBK_heatStackId", heatStackId)
101 //              String requestId = rollbackData.get("DCVFMODULEVOL", "msorequestid")
102 //              execution.setVariable("DCVFMODVOLRBK_requestId", requestId)
103
104                 String volumeGroupName = rollbackData.get("DCVFMODULEVOL", "volumeGroupName")
105                 execution.setVariable("DCVFMODVOLRBK_volumeGroupName", volumeGroupName)
106
107                 String lcpCloudRegionId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")
108                 execution.setVariable("DCVFMODVOLRBK_lcpCloudRegionId", lcpCloudRegionId)
109
110                 execution.setVariable("DCVFMODVOLRBK_rollbackVnfARequest", rollbackData.get("DCVFMODULEVOL", "rollbackVnfARequest"))
111                 execution.setVariable("DCVFMODVOLRBK_backoutOnFailure", rollbackData.get("DCVFMODULEVOL", "backoutOnFailure"))
112                 execution.setVariable("DCVFMODVOLRBK_isCreateVnfRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isCreateVnfRollbackNeeded"))
113                 execution.setVariable("DCVFMODVOLRBK_isAAIRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isAAIRollbackNeeded"))
114
115         }
116
117         /**
118          * Query AAI volume group by name
119          * @param execution
120          * @param cloudRegion
121          * @return
122          */
123         private String callRESTQueryAAIVolGrpName(DelegateExecution execution, String cloudRegion) {
124
125                 def volumeGroupName = execution.getVariable('DCVFMODVOLRBK_volumeGroupName')
126
127                 def testVolumeGroupName = execution.getVariable('test-volume-group-name')
128                 if (testVolumeGroupName != null && testVolumeGroupName.length() > 0) {
129                         volumeGroupName = testVolumeGroupName
130                 }
131
132                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), cloudRegion).queryParam("volume-group-name", volumeGroupName)
133                 try {
134                         Optional<VolumeGroups> volumeGroups = getAAIClient().get(VolumeGroups.class, uri)
135                         if (volumeGroups.isPresent()) {
136                                 return volumeGroups.get().getVolumeGroup().get(0).getVolumeGroupId()
137                         } else {
138                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupName not found in AAI. Response code: 404")
139                         }
140                 } catch (Exception e) {
141                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, e.getMessage())
142                 }
143                 return null
144         }
145
146
147
148         public void callRESTDeleteAAIVolumeGroup(DelegateExecution execution, isDebugEnabled) {
149
150                 String cloudRegion = execution.getVariable("DCVFMODVOLRBK_lcpCloudRegionId")
151                 String volumeGroupId = callRESTQueryAAIVolGrpName(execution, cloudRegion)
152
153                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), cloudRegion, volumeGroupId)
154                 try {
155                         getAAIClient().delete(uri)
156                 }catch(NotFoundException ignored){
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupId not found for delete in AAI Response code: 404")
158                 }catch(Exception e){
159                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500,e.getMessage())
160                 }
161         }
162
163         // *******************************
164         //     Build Error Section
165         // *******************************
166
167
168
169         public void processJavaException(DelegateExecution execution){
170                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
171                 execution.setVariable("prefix",Prefix)
172
173                 try{
174                         msoLogger.debug("Caught a Java Exception in " + Prefix)
175                         msoLogger.debug("Started processJavaException Method")
176                         msoLogger.debug("Variables List: " + execution.getVariables())
177                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
178                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
179
180                 }catch(Exception e){
181                         msoLogger.debug("Caught Exception during processJavaException Method: " + e)
182                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
183                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
184                 }
185                 msoLogger.debug("Completed processJavaException Method in " + Prefix)
186         }
187
188 }