Merge "Modify the SO to support BBS usecase"
[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.ErrorCode
31 import org.onap.so.logger.MessageEnum
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                         logger.debug("Exception occurred while executing AAI DELETE: {}", ex.getMessage(), ex)
116                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteGenericVnf")
117                 }
118         }
119
120         // construct and send a DELETE request to A&AI to delete the Base or Add-on Vf Module
121         public void deleteVfModule(DelegateExecution execution) {
122                 def responseData = ""
123                 try {
124                         String vnfId = execution.getVariable("DAAIVfMod_vnfId")
125                         String vfModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
126
127                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
128
129             getAAIClient().delete(uri)
130                         execution.setVariable("DAAIVfMod_deleteVfModuleResponseCode", 200)
131                         execution.setVariable("DAAIVfMod_deleteVfModuleResponse", "Vf Module Deleted")
132                 } catch (Exception ex) {
133                         logger.debug("Exception occurred while executing AAI PUT: {}", ex.getMessage(), ex)
134                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Internal Error - Occured during deleteVfModule")
135                 }
136         }
137
138         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
139         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
140         // there are no Add-on Modules present
141         public void parseForVfModule(DelegateExecution execution) {
142         GenericVnf genericVnf = execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
143
144                 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
145                 logger.debug("Vf Module to be deleted: " + delModuleId)
146
147         execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
148
149         execution.setVariable("DAAIVfMod_moduleExists", false)
150         execution.setVariable("DAAIVfMod_isBaseModule", false)
151         execution.setVariable("DAAIVfMod_isLastModule", false)
152         if(genericVnf.getVfModules()!= null && !genericVnf.getVfModules().getVfModule().isEmpty()){
153             Optional<org.onap.aai.domain.yang.VfModule> vfModule = genericVnf.getVfModules().getVfModule().stream().
154                     filter{ v -> v.getVfModuleId().equals(delModuleId)}.findFirst()
155             if(vfModule.isPresent()){
156                 execution.setVariable("DAAIVfMod_moduleExists", true)
157                 execution.setVariable("DAAIVfMod_vfModRsrcVer", vfModule.get().getResourceVersion())
158
159                 if (vfModule.get().isBaseVfModule  && genericVnf.getVfModules().getVfModule().size() != 1) {
160                     execution.setVariable("DAAIVfMod_parseModuleResponse",
161                             "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
162                                     execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module, not Last Module")
163                     execution.setVariable("DAAIVfMod_isBaseModule", true)
164                 } else {
165                     if (vfModule.get().isBaseVfModule && genericVnf.getVfModules().getVfModule().size() == 1) {
166                         execution.setVariable("DAAIVfMod_parseModuleResponse",
167                                 "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
168                                         execution.getVariable("DAAIVfMod_vnfId") + ": is Base Module and Last Module")
169                         execution.setVariable("DAAIVfMod_isBaseModule", true)
170                         execution.setVariable("DAAIVfMod_isLastModule", true)
171                     } else {
172                         if (genericVnf.getVfModules().getVfModule().size() == 1) {
173                             execution.setVariable("DAAIVfMod_parseModuleResponse",
174                                     "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
175                                             execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module, is Last Module")
176                             execution.setVariable("DAAIVfMod_isLastModule", true)
177                         } else {
178                             execution.setVariable("DAAIVfMod_parseModuleResponse",
179                                     "Found Vf Module Id " + delModuleId + " for Generic Vnf Id " +
180                                             execution.getVariable("DAAIVfMod_vnfId") + ": is Not Base Module and Not Last Module")
181                         }
182                     }
183                 }
184                 logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
185             }
186         }
187         if (execution.getVariable("DAAIVfMod_moduleExists") == false) { // (execution.getVariable("DAAIVfMod_moduleExists") == false)
188             logger.debug("Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " + execution.getVariable("DAAIVfMod_vnfId"))
189             execution.setVariable("DAAIVfMod_parseModuleResponse",
190                     "Vf Module Id " + delModuleId + " does not exist for Generic Vnf Id " +
191                             execution.getVariable("DAAIVfMod_vnfName"))
192         }
193         }
194
195         // parses the output from the result from queryAAIForGenericVnf() to determine if the Vf Module
196         // to be deleted exists for the specified Generic Vnf and if it is the Base Module,
197         // there are no Add-on Modules present
198         public void parseForResourceVersion(DelegateExecution execution) {
199         GenericVnf genericVnf =  execution.getVariable("DAAIVfMod_queryGenericVnfResponse")
200                 execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
201                 logger.debug("Latest Generic VNF Resource Version: " + genericVnf.getResourceVersion())
202         }
203
204
205         // generates a WorkflowException if the A&AI query returns a response code other than 200
206         public void handleAAIQueryFailure(DelegateExecution execution) {
207                 logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
208                                 "Error occurred attempting to query AAI, Response Code " + execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") + ", Error Response " + execution.getVariable("DAAIVfMod_queryGenericVnfResponse"),
209                                 "BPMN", ErrorCode.UnknownError.getValue());
210                 def errorCode = 5000
211                 // set the errorCode to distinguish between a A&AI failure
212                 // and the Generic Vnf Id not found
213                 if (execution.getVariable("DAAIVfMod_queryGenericVnfResponseCode") == 404) {
214                         errorCode = 1002
215                 }
216                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
217         }
218
219         // generates a WorkflowException if
220         //              - the A&AI Vf Module DELETE returns a response code other than 200
221         //              - the Vf Module is a Base Module that is not the last Vf Module
222         //              - the Vf Module does not exist for the Generic Vnf
223         public void handleDeleteVfModuleFailure(DelegateExecution execution) {
224                 def errorCode = 2000
225                 def errorResponse = ""
226                 if (execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != null &&
227                         execution.getVariable("DAAIVfMod_deleteVfModuleResponseCode") != 200) {
228                         logger.debug("AAI failure deleting a Vf Module: " + execution.getVariable("DAAIVfMod_deleteVfModuleResponse"))
229                         errorResponse = execution.getVariable("DAAIVfMod_deleteVfModuleResponse")
230                         logger.debug("DeleteAAIVfModule - deleteVfModuleResponse" + errorResponse)
231                         errorCode = 5000
232                 } else {
233                         if (execution.getVariable("DAAIVfMod_isBaseModule", true) == true &&
234                                         execution.getVariable("DAAIVfMod_isLastModule") == false) {
235                                 // attempt to delete a Base Module that is not the last Vf Module
236                                 logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
237                                 errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
238                                 logger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
239                                 errorCode = 1002
240                         } else {
241                                 // attempt to delete a non-existant Vf Module
242                                 if (execution.getVariable("DAAIVfMod_moduleExists") == false) {
243                                         logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
244                                         errorResponse = execution.getVariable("DAAIVfMod_parseModuleResponse")
245                                         logger.debug("DeleteAAIVfModule - parseModuleResponse" + errorResponse)
246                                         errorCode = 1002
247                                 } else {
248                                         // if the responses get populated corerctly, we should never get here
249                                         errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
250                                 }
251                         }
252                 }
253
254                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
255                                 "Error occurred during DeleteAAIVfModule flow", "BPMN",
256                                 ErrorCode.UnknownError.getValue(), errorResponse);
257                 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, errorResponse)
258
259         }
260
261         // generates a WorkflowException if
262         //              - the A&AI Generic Vnf DELETE returns a response code other than 200
263         public void handleDeleteGenericVnfFailure(DelegateExecution execution) {
264                 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
265                                 "AAI error occurred deleting the Generic Vnf", "BPMN",
266                                 ErrorCode.UnknownError.getValue(),
267                                 execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"));
268                 exceptionUtil.buildAndThrowWorkflowException(execution, 5000, execution.getVariable("DAAIVfMod_deleteGenericVnfResponse"))
269         }
270 }