80f6e0edb231a79d146a35cfe1c84bf7400773c5
[so.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25
26 import org.camunda.bpm.engine.delegate.DelegateExecution
27 import org.onap.aai.domain.yang.VolumeGroups
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil
30 import org.onap.so.bpmn.common.scripts.VidUtils
31 import org.onap.so.bpmn.core.RollbackData
32 import org.onap.so.bpmn.core.json.JsonUtils
33 import org.onap.aaiclient.client.aai.AAIObjectPlurals
34 import org.onap.aaiclient.client.aai.AAIObjectType
35 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri
36 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri
37 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory
38 import org.onap.so.constants.Defaults
39 import org.slf4j.Logger
40 import org.slf4j.LoggerFactory
41
42 import javax.ws.rs.NotFoundException
43
44 public class DoCreateVfModuleVolumeRollback extends AbstractServiceTaskProcessor {
45     private static final Logger logger = LoggerFactory.getLogger( DoCreateVfModuleVolumeRollback.class);
46
47         String Prefix="DCVFMODVOLRBK_"
48         ExceptionUtil exceptionUtil = new ExceptionUtil()
49         JsonUtils jsonUtil = new JsonUtils()
50         VidUtils vidUtils = new VidUtils(this)
51
52         def className = getClass().getSimpleName()
53
54         /**
55          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.
56          * @param execution
57          */
58         public InitializeProcessVariables(DelegateExecution execution){
59                 /* Initialize all the process variables in this block */
60
61                 execution.setVariable(Prefix + "volumeGroupName", null)
62                 execution.setVariable(Prefix + "lcpCloudRegionId", null)
63                 execution.setVariable(Prefix + "rollbackVnfARequest", null)
64
65         }
66
67         // **************************************************
68         //     Pre or Prepare Request Section
69         // **************************************************
70         /**
71          * This method is executed during the preProcessRequest task of the <class>DoCreateVfModuleVolumeRollback.bpmn</class> process.
72          * @param execution
73          */
74         public void preProcessRequest (DelegateExecution execution) {
75                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
76
77                 InitializeProcessVariables(execution)
78 //              rollbackData.put("DCVFMODULEVOL", "aiccloudregion", cloudSiteId)
79                 RollbackData rollbackData = execution.getVariable("rollbackData")
80
81 //              String vnfId = rollbackData.get("DCVFMODULEVOL", "vnfid")
82 //              execution.setVariable("DCVFMODVOLRBK_vnfId", vnfId)
83 //              String vfModuleId = rollbackData.get("DCVFMODULEVOL", "vfmoduleid")
84 //              execution.setVariable("DCVFMODVOLRBK_vfModuleId", vfModuleId)
85 //              String source = rollbackData.get("DCVFMODULEVOL", "source")
86 //              execution.setVariable("DCVFMODVOLRBK_source", source)
87 //              String serviceInstanceId = rollbackData.get("DCVFMODULEVOL", "serviceInstanceId")
88 //              execution.setVariable("DCVFMODVOLRBK_serviceInstanceId", serviceInstanceId)
89 //              String serviceId = rollbackData.get("DCVFMODULEVOL", "service-id")
90 //              execution.setVariable("DCVFMODVOLRBK_serviceId", serviceId)
91 //              String vnfType = rollbackData.get("DCVFMODULEVOL", "vnftype")
92 //              execution.setVariable("DCVFMODVOLRBK_vnfType", vnfType)
93 //              String vnfName = rollbackData.get("DCVFMODULEVOL", "vnfname")
94 //              execution.setVariable("DCVFMODVOLRBK_vnfName", vnfName)
95 //              String tenantId = rollbackData.get("DCVFMODULEVOL", "tenantid")
96 //              execution.setVariable("DCVFMODVOLRBK_tenantId", tenantId)
97 //              String vfModuleName = rollbackData.get("DCVFMODULEVOL", "vfmodulename")
98 //              execution.setVariable("DCVFMODVOLRBK_vfModuleName", vfModuleName)
99 //              String vfModuleModelName = rollbackData.get("DCVFMODULEVOL", "vfmodulemodelname")
100 //              execution.setVariable("DCVFMODVOLRBK_vfModuleModelName", vfModuleModelName)
101 //              String cloudSiteId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")
102 //              execution.setVariable("DCVFMODVOLRBK_cloudSiteId", cloudSiteId)
103 //              String heatStackId = rollbackData.get("DCVFMODULEVOL", "heatstackid")
104 //              execution.setVariable("DCVFMODVOLRBK_heatStackId", heatStackId)
105 //              String requestId = rollbackData.get("DCVFMODULEVOL", "msorequestid")
106 //              execution.setVariable("DCVFMODVOLRBK_requestId", requestId)
107
108                 String volumeGroupName = rollbackData.get("DCVFMODULEVOL", "volumeGroupName")
109                 execution.setVariable("DCVFMODVOLRBK_volumeGroupName", volumeGroupName)
110
111                 String lcpCloudRegionId = rollbackData.get("DCVFMODULEVOL", "aiccloudregion")
112                 execution.setVariable("DCVFMODVOLRBK_lcpCloudRegionId", lcpCloudRegionId)
113
114                 execution.setVariable("DCVFMODVOLRBK_rollbackVnfARequest", rollbackData.get("DCVFMODULEVOL", "rollbackVnfARequest"))
115                 execution.setVariable("DCVFMODVOLRBK_backoutOnFailure", rollbackData.get("DCVFMODULEVOL", "backoutOnFailure"))
116                 execution.setVariable("DCVFMODVOLRBK_isCreateVnfRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isCreateVnfRollbackNeeded"))
117                 execution.setVariable("DCVFMODVOLRBK_isAAIRollbackNeeded", rollbackData.get("DCVFMODULEVOL", "isAAIRollbackNeeded"))
118
119         }
120
121         /**
122          * Query AAI volume group by name
123          * @param execution
124          * @param cloudRegion
125          * @return
126          */
127         private String callRESTQueryAAIVolGrpName(DelegateExecution execution, String cloudRegion) {
128
129                 def volumeGroupName = execution.getVariable('DCVFMODVOLRBK_volumeGroupName')
130
131                 def testVolumeGroupName = execution.getVariable('test-volume-group-name')
132                 if (testVolumeGroupName != null && testVolumeGroupName.length() > 0) {
133                         volumeGroupName = testVolumeGroupName
134                 }
135
136                 AAIPluralResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), cloudRegion).queryParam("volume-group-name", volumeGroupName)
137                 try {
138                         Optional<VolumeGroups> volumeGroups = getAAIClient().get(VolumeGroups.class, uri)
139                         if (volumeGroups.isPresent()) {
140                                 return volumeGroups.get().getVolumeGroup().get(0).getVolumeGroupId()
141                         } else {
142                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupName not found in AAI. Response code: 404")
143                         }
144                 } catch (Exception e) {
145                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, e.getMessage())
146                 }
147                 return null
148         }
149
150
151
152         public void callRESTDeleteAAIVolumeGroup(DelegateExecution execution, isDebugEnabled) {
153
154                 String cloudRegion = execution.getVariable("DCVFMODVOLRBK_lcpCloudRegionId")
155                 String volumeGroupId = callRESTQueryAAIVolGrpName(execution, cloudRegion)
156
157                 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, Defaults.CLOUD_OWNER.toString(), cloudRegion, volumeGroupId)
158                 try {
159                         getAAIClient().delete(uri)
160                 }catch(NotFoundException ignored){
161                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Volume group $volumeGroupId not found for delete in AAI Response code: 404")
162                 }catch(Exception e){
163                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500,e.getMessage())
164                 }
165         }
166
167         // *******************************
168         //     Build Error Section
169         // *******************************
170
171
172
173         public void processJavaException(DelegateExecution execution){
174                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
175                 execution.setVariable("prefix",Prefix)
176
177                 try{
178                         logger.debug("Caught a Java Exception in " + Prefix)
179                         logger.debug("Started processJavaException Method")
180                         logger.debug("Variables List: " + execution.getVariables())
181                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
182                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
183
184                 }catch(Exception e){
185                         logger.debug("Caught Exception during processJavaException Method: " + e)
186                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
187                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
188                 }
189                 logger.debug("Completed processJavaException Method in " + Prefix)
190         }
191
192 }