301c74d2eb31c7b7bf6d24d003cea645ec1ca16f
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / onap / so / bpmn / common / scripts / DeleteAAIVfModule.groovy
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.common.scripts
24 import org.camunda.bpm.engine.delegate.DelegateExecution
25 import org.onap.aai.domain.yang.GenericVnf
26 import org.onap.so.client.aai.AAIObjectType
27 import org.onap.so.client.aai.entities.uri.AAIResourceUri
28 import org.onap.so.client.aai.entities.uri.AAIUriFactory
29 import org.onap.so.client.graphinventory.entities.uri.Depth
30 import org.onap.so.logger.MessageEnum
31 import org.onap.so.logger.MsoLogger
32 import org.slf4j.Logger
33 import org.slf4j.LoggerFactory
34
35 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{
36     private static final Logger logger = LoggerFactory.getLogger( DeleteAAIVfModule.class);
37
38         def Prefix="DAAIVfMod_"
39         ExceptionUtil exceptionUtil = new ExceptionUtil()
40     private MsoUtils utils = new MsoUtils()
41         public void initProcessVariables(DelegateExecution execution) {
42                 execution.setVariable("prefix",Prefix)
43                 execution.setVariable("DAAIVfMod_vnfId",null)
44                 execution.setVariable("DAAIVfMod_vnfName",null)
45                 execution.setVariable("DAAIVfMod_genVnfRsrcVer",null)
46                 execution.setVariable("DAAIVfMod_vfModuleId",null)
47                 execution.setVariable("DAAIVfMod_vfModRsrcVer",null)
48                 execution.setVariable("DAAIVfMod_moduleExists",false)
49                 execution.setVariable("DAAIVfMod_isBaseModule", false)
50                 execution.setVariable("DAAIVfMod_isLastModule", false)
51
52                 // DeleteAAIVfModule workflow response variable placeholders
53                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode",null)
54                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse","")
55                 execution.setVariable("DAAIVfMod_parseModuleResponse","")
56                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode",null)
57                 execution.setVariable("DAAIVfMod_deleteGenericVnfResponse","")
58                 execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode",null)
59                 execution.setVariable("DAAIVfMod_deleteVfModuleResponse","")
60
61         }
62
63         // parse the incoming DELETE_VF_MODULE request and store the Generic Vnf
64         // and Vf Module Ids in the flow DelegateExecution
65         public void preProcessRequest(DelegateExecution execution) {
66                 def xml = execution.getVariable("DeleteAAIVfModuleRequest")
67                 logger.debug("DeleteAAIVfModule Request: " + xml)
68                 logger.debug("input request xml:" + xml)
69                 initProcessVariables(execution)
70                 def vnfId = utils.getNodeText(xml,"vnf-id")
71                 def vfModuleId = utils.getNodeText(xml,"vf-module-id")
72                 execution.setVariable("DAAIVfMod_vnfId", vnfId)
73                 execution.setVariable("DAAIVfMod_vfModuleId", vfModuleId)
74         }
75
76         // send a GET request to AA&I to retrieve the Generic Vnf/Vf Module information based on a Vnf Id
77         // expect a 200 response with the information in the response body or a 404 if the Generic Vnf does not exist
78         public void queryAAIForGenericVnf(DelegateExecution execution) {
79                 
80                 def vnfId = execution.getVariable("DAAIVfMod_vnfId")
81
82                 try {
83                         AaiUtil aaiUriUtil = new AaiUtil(this)
84                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
85                         uri.depth(Depth.ONE)
86             Optional<GenericVnf> genericVnf = getAAIClient().get(GenericVnf.class, uri)
87
88             if(genericVnf.isPresent()) {
89                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", 200)
90                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", genericVnf.get())
91
92             }else{
93                 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", 404)
94                 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "Vnf Not Found!")
95             }
96
97                 } catch (Exception ex) {
98                         logger.debug("Exception occurred while executing AAI GET:" + ex.getMessage())
99                         execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "AAI GET Failed:" + ex.getMessage())
100                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during queryAAIForGenericVnf")
101                 }
102         }
103
104         // construct and send a DELETE request to A&AI to delete a Generic Vnf
105         // note: to get here, all the modules associated with the Generic Vnf must already be deleted
106         public void deleteGenericVnf(DelegateExecution execution) {
107
108                 try {
109                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
110                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
111             getAAIClient().delete(uri)
112                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponseCode", 200)
113                         execution.setVariable("DAAIVfMod_deleteGenericVnfResponse", "Vnf Deleted")
114                 } catch (Exception ex) {
115                         ex.printStackTrace()
116                         logger.debug("Exception occurred while executing AAI DELETE:" + ex.getMessage())
117                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")
118                 }
119         }
120
121         // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module
122         public void deleteVfModule(DelegateExecution execution) {
123                 def responseData = ""
124                 try {
125                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
126                         String vfModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
127
128                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
129
130             getAAIClient().delete(uri)
131                         execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", 200)
132                         execution.setVariable("DAAIVfMod_deleteVfModuleResponse", "Vf Module Deleted")
133                 } catch (Exception ex) {
134                         ex.printStackTrace()
135                         logger.debug("Exception occurred while executing AAI PUT:" + ex.getMessage())
136                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")
137                 }
138         }
139
140         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
141         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
142         // there are no Add-on Modules present
143         public void parseForVfModule(DelegateExecution execution) {
144         GenericVnf genericVnf = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
145
146                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
147                 logger.debug("Vf Module to be deleted: " + delModuleId)
148
149         execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
150
151         execution.setVariable("DAAIVfMod_moduleExists", false)
152         execution.setVariable("DAAIVfMod_isBaseModule", false)
153         execution.setVariable("DAAIVfMod_isLastModule", false)
154         if(genericVnf.getVfModules()!= null && !genericVnf.getVfModules().getVfModule().isEmpty()){
155             Optional<org.onap.aai.domain.yang.VfModule> vfModule = genericVnf.getVfModules().getVfModule().stream().
156                     filter{ v -> v.getVfModuleId().equals(delModuleId)}.findFirst()
157             if(vfModule.isPresent()){
158                 execution.setVariable("DAAIVfMod_moduleExists", true)
159                 execution.setVariable("DAAIVfMod_vfModRsrcVer", vfModule.get().getResourceVersion())
160
161                 if (vfModule.get().isBaseVfModule  && genericVnf.getVfModules().getVfModule().size() != 1) {
162                     execution.setVariable("DAAIVfMod_parseModuleResponse",
163                             "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
164                                     execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")
165                     execution.setVariable("DAAIVfMod_isBaseModule", true)
166                 } else {
167                     if (vfModule.get().isBaseVfModule && genericVnf.getVfModules().getVfModule().size() == 1) {
168                         execution.setVariable("DAAIVfMod_parseModuleResponse",
169                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
170                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")
171                         execution.setVariable("DAAIVfMod_isBaseModule", true)
172                         execution.setVariable("DAAIVfMod_isLastModule", true)
173                     } else {
174                         if (genericVnf.getVfModules().getVfModule().size() == 1) {
175                             execution.setVariable("DAAIVfMod_parseModuleResponse",
176                                     "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
177                                             execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")
178                             execution.setVariable("DAAIVfMod_isLastModule", true)
179                         } else {
180                             execution.setVariable("DAAIVfMod_parseModuleResponse",
181                                     "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
182                                             execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")
183                         }
184                     }
185                 }
186                 logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
187             }
188         }
189         if (execution.getVariable("DAAIVfMod_moduleExists") == false) { // (execution.getVariable("DAAIVfMod_moduleExists") == false)
190             logger.debug("Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " + execution.getVariable("DAAIVfMod_vnfId"))
191             execution.setVariable("DAAIVfMod_parseModuleResponse",
192                     "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
193                             execution.getVariable("DAAIVfMod_vnfName"))
194         }
195         }
196
197         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
198         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
199         // there are no Add-on Modules present
200         public void parseForResourceVersion(DelegateExecution execution) {
201         GenericVnf genericVnf =  execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
202                 execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
203                 logger.debug("Latest Generic VNF Resource Version: " + genericVnf.getResourceVersion())
204         }
205
206
207         // generates a WorkflowException if the A&AI query returns a response code other than 200
208         public void handleAAIQueryFailure(DelegateExecution execution) {
209                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
210                                 "Error occurred attempting to query AAI, Response Code " + execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " + execution.getVariable("DAAIVfMod_queryGenericVnfResponse"),
211                                 "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError.getValue());
212                 def errorCode = 5000
213                 // set the errorCode to distinguish between a A&AI failure
214                 // and the Generic Vnf Id not found
215                 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {
216                         errorCode = 1002
217                 }
218                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
219         }
220
221         // generates a WorkflowException if
222         //              - the A&AI Vf Module DELETE returns a response code other than 200
223         //              - the Vf Module is a Base Module that is not the last Vf Module
224         //              - the Vf Module does not exist for the Generic Vnf
225         public void handleDeleteVfModuleFailure(DelegateExecution execution) {
226                 def errorCode = 2000
227                 def errorResponse = ""
228                 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&
229                         execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {
230                         logger.debug("AAI failure deleting a Vf Module: " + execution.getVariable("DAAIVfMod_deleteVfModuleResponse"))
231                         errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")
232                         logger.debug("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)
233                         errorCode = 5000
234                 } else {
235                         if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&
236                                         execution.getVariable("DAAIVfMod_isLastModule") == false) {
237                                 // attempt to delete a Base Module that is not the last Vf Module
238                                 logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
239                                 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
240                                 logger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
241                                 errorCode = 1002
242                         } else {
243                                 // attempt to delete a non-existant Vf Module
244                                 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {
245                                         logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
246                                         errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
247                                         logger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
248                                         errorCode = 1002
249                                 } else {
250                                         // if the responses get populated corerctly, we should never get here
251                                         errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
252                                 }
253                         }
254                 }
255
256                 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
257                                 "Error occurred during DeleteAAIVfModule flow", "BPMN", MsoLogger.getServiceName(),
258                                 MsoLogger.ErrorCode.UnknownError.getValue(), errorResponse);
259                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
260
261         }
262
263         // generates a WorkflowException if
264         //              - the A&AI Generic Vnf DELETE returns a response code other than 200
265         public void handleDeleteGenericVnfFailure(DelegateExecution execution) {
266                 logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
267                                 "AAI error occurred deleting the Generic Vnf", "BPMN", MsoLogger.getServiceName(),
268                                 MsoLogger.ErrorCode.UnknownError.getValue(),
269                                 execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"));
270                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))
271         }
272 }