2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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
35 public class DeleteAAIVfModule extends AbstractServiceTaskProcessor{
36 private static final Logger logger = LoggerFactory.getLogger( DeleteAAIVfModule.class);
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)
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","")
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)
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) {
80 def vnfId = execution.getVariable("DAAIVfMod_vnfId")
83 AaiUtil aaiUriUtil = new AaiUtil(this)
84 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId)
86 Optional<GenericVnf> genericVnf = getAAIClient().get(GenericVnf.class, uri)
88 if(genericVnf.isPresent()) {
89 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", 200)
90 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", genericVnf.get())
93 execution.setVariable("DAAIVfMod_queryGenericVnfResponseCode", 404)
94 execution.setVariable("DAAIVfMod_queryGenericVnfResponse", "Vnf Not Found!")
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")
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) {
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")
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 = ""
124 String vnfId = execution.getVariable("DAAIVfMod_vnfId")
125 String vfModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
127 AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId)
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")
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")
144 def delModuleId = execution.getVariable("DAAIVfMod_vfModuleId")
145 logger.debug("Vf Module to be deleted: " + delModuleId)
147 execution.setVariable("DAAIVfMod_genVnfRsrcVer", genericVnf.getResourceVersion())
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())
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)
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)
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)
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")
184 logger.debug(execution.getVariable("DAAIVfMod_parseModuleResponse"))
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"))
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())
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());
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) {
216 exceptionUtil.buildAndThrowWorkflowException(execution, errorCode, execution.getVariable("DAAIVfMod_queryGenericVnfResponse"))
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) {
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)
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)
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)
248 // if the responses get populated corerctly, we should never get here
249 errorResponse = "Unknown error occurred during DeleteAAIVfModule flow"
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)
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"))