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