2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.bpmn.common.scripts
23 import static org.apache.commons.lang3.StringUtils.*
25 import org.apache.commons.lang3.*
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.runtime.Execution
28 import org.openecomp.mso.rest.APIResponse
29 import org.springframework.web.util.UriUtils
33 * TODO: Support getting vnf type = vpe
35 * This class supports the GenericGetVnf Sub Flow.
36 * This Generic sub flow can be used by any flow for accomplishing
37 * the goal of getting a Vnf Object (from AAI). The flow currently
38 * supports the querying of 2 types of Vnfs, generic-vnf and vce. The
39 * type must be provided by the calling flow and the type should
40 * be mapped to the variable GENDV_type. The type should either be
41 * "generic-vnf" or "vce". If the Vnf Id is not provided by the calling
42 * flow then this sub flow will execute the query to get the
43 * Vnf using the Vnf Name. Therefore, the calling flow must provide
44 * either the Vnf Id or Vnf Name.
46 * Upon successful completion of this sub flow the
47 * GENDV_SuccessIndicator will be true and the query response payload
48 * will be set to GENDV_vnf. An MSOWorkflowException will
49 * be thrown upon unsuccessful completion or if an error occurs
50 * at any time during this sub flow. Please map variables
51 * to the corresponding variable names below.
53 * Note - if this sub flow receives a Not Found (404) response
54 * from AAI at any time this will be considered an acceptable
55 * successful response however the GENDV_FoundIndicator
56 * set to false. This will allow the calling flow to distinguish
57 * between the two success scenarios, "Success where Vnf is found"
58 * and "Success where Vnf is NOT found".
62 * @param - GENDV_vnfId
64 * @param (Optional) - GENDV_resourceVersion
69 * @param - GENDV_SuccessIndicator
70 * @param - GENDV_FoundIndicator
71 * @param - WorkflowException
73 class GenericDeleteVnf extends AbstractServiceTaskProcessor{
75 String Prefix = "GENDV_"
76 ExceptionUtil exceptionUtil = new ExceptionUtil()
79 * This method validates the incoming variables and
80 * determines if the resource version was provided
84 public void preProcessRequest(Execution execution) {
85 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
86 execution.setVariable("prefix",Prefix)
87 utils.log("DEBUG", " *** STARTED GenericDeleteVnf PreProcessRequest Process*** ", isDebugEnabled)
89 execution.setVariable("GENDV_resourceVersionProvided", true)
90 execution.setVariable("GENDV_SuccessIndicator", false)
91 execution.setVariable("GENDV_FoundIndicator", false)
95 String vnfId = execution.getVariable("GENDV_vnfId")
96 String type = execution.getVariable("GENDV_type")
98 if(isBlank(type) || isBlank(vnfId)){
99 utils.log("ERROR", "Incoming Required Variable is null!", isDebugEnabled)
100 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
102 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
104 String resourceVersion = execution.getVariable("GENDV_resourceVersion")
105 if(isBlank(resourceVersion)){
106 utils.log("DEBUG", "Vnf Resource Version is NOT Provided", isDebugEnabled)
107 execution.setVariable("GENDV_resourceVersionProvided", false)
109 utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)
113 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
116 utils.log("ERROR", " Error encountered within GenericDeleteVnf PreProcessRequest method!" + e, isDebugEnabled)
117 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteVnf PreProcessRequest")
120 utils.log("DEBUG", "*** COMPLETED GenericDeleteVnf PreProcessRequest Process ***", isDebugEnabled)
124 * This method executes a GET call to AAI for the Vnf
125 * so that the Vnf's resource-version can be
130 public void getVnfResourceVersion(Execution execution){
131 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
132 execution.setVariable("prefix",Prefix)
133 utils.log("DEBUG", " *** STARTED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)
135 String vnfId = execution.getVariable("GENDV_vnfId")
136 String type = execution.getVariable("GENDV_type")
137 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
139 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
140 AaiUtil aaiUriUtil = new AaiUtil(this)
142 //Determine Type of Vnf Querying For
144 if(type.equals("generic-vnf")){
145 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
146 }else if(type.equals("vce")){
147 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
149 utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)
150 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")
153 String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
155 execution.setVariable("GENDV_getVnfPath", getVnfPath)
156 utils.logAudit("Get Vnf Resource Version Url is: " + getVnfPath)
158 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
159 int responseCode = response.getStatusCode()
160 execution.setVariable("GENDV_getVnfResponseCode", responseCode)
161 utils.log("DEBUG", " GET Vnf response code is: " + responseCode, isDebugEnabled)
163 utils.logAudit("GenericDeleteVnf Get VNF Response Code: " + responseCode)
164 String aaiResponse = response.getResponseBodyAsString()
165 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
166 execution.setVariable("GENDV_getVnfResponse", aaiResponse)
168 utils.logAudit("GenericDeleteVnf Get VNF Response: " + aaiResponse)
170 if(responseCode == 200 || responseCode == 202){
171 utils.log("DEBUG", "GET Vnf Received a Good Response: \n" + aaiResponse, isDebugEnabled)
172 execution.setVariable("GENDV_FoundIndicator", true)
173 if(utils.nodeExists(aaiResponse, "resource-version")){
174 String resourceVersion = utils.getNodeText1(aaiResponse, "resource-version")
175 execution.setVariable("GENDV_resourceVersion", resourceVersion)
176 utils.log("DEBUG", "SI Resource Version is: " + resourceVersion, isDebugEnabled)
178 utils.log("DEBUG", "GET Vnf for Resource Version Response Does NOT Contain a resource-version", isDebugEnabled)
179 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Unable to obtain Vnf resource-version. GET Vnf Response Does NOT Contain a resource-version")
182 }else if(responseCode == 404){
183 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
184 execution.setVariable("GENDV_SuccessIndicator", true)
185 execution.setVariable("WorkflowResponse", " ") // for junits
188 utils.log("DEBUG", " GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
189 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
190 throw new BpmnError("MSOWorkflowException")
193 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
196 utils.log("DEBUG", " Error encountered within GenericDeleteVnf GetVnfResourceVersion method!" + e, isDebugEnabled)
197 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfResourceVersion")
199 utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)
203 * This method executes a DELETE call to AAI for the provided
208 public void deleteVnf(Execution execution){
209 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
210 execution.setVariable("prefix",Prefix)
211 utils.log("DEBUG", " *** STARTED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)
213 String vnfId = execution.getVariable("GENDV_vnfId")
214 String type = execution.getVariable("GENDV_type")
215 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
216 String resourceVersion = execution.getVariable("GENDV_resourceVersion")
217 utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)
219 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
220 AaiUtil aaiUriUtil = new AaiUtil(this)
222 //Determine Type of Vnf Querying For
224 if(type.equals("generic-vnf")){
225 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
226 }else if(type.equals("vce")){
227 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
229 utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)
230 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")
233 String deleteVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")
235 execution.setVariable("GENDV_deleteVnfPath", deleteVnfPath)
236 utils.logAudit("Delete Vnf Url is: " + deleteVnfPath)
238 APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, deleteVnfPath)
239 int responseCode = response.getStatusCode()
240 execution.setVariable("GENDV_deleteVnfResponseCode", responseCode)
241 utils.log("DEBUG", " DELETE Vnf response code is: " + responseCode, isDebugEnabled)
243 utils.logAudit("GenericDeleteVnf Delete VNF Response Code: " + responseCode)
244 String aaiResponse = response.getResponseBodyAsString()
245 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
246 execution.setVariable("GENDV_deleteVnfResponse", aaiResponse)
248 utils.logAudit("GenericDeleteVnf Delete VNF Response: " + aaiResponse)
250 if(responseCode == 204){
251 utils.log("DEBUG", " DELETE Vnf Received a Good Response", isDebugEnabled)
252 execution.setVariable("GENDV_FoundIndicator", true)
253 }else if(responseCode == 404){
254 utils.log("DEBUG", " DELETE Vnf Received a Not Found (404) Response", isDebugEnabled)
255 }else if(responseCode == 412){
256 utils.log("DEBUG", "DELETE Vnf Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)
257 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "Delete Vnf Received a resource-version Mismatch Error Response from AAI")
259 utils.log("DEBUG", "DELETE Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
260 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
261 throw new BpmnError("MSOWorkflowException")
264 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
267 utils.log("DEBUG", " Error encountered within GenericDeleteVnf DeleteVnf method!" + e, isDebugEnabled)
268 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete Vnf")
270 utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)