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 com.att.bpm.scripts
23 import static org.apache.commons.lang3.StringUtils.*;
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.runtime.Execution;
27 import org.apache.commons.codec.binary.Base64
28 import org.apache.commons.lang3.*
30 import org.openecomp.mso.bpmn.core.WorkflowException
31 import org.openecomp.mso.rest.APIResponse
32 import org.openecomp.mso.rest.RESTClient
33 import org.openecomp.mso.rest.RESTConfig
35 import org.springframework.web.util.UriUtils
39 * TODO: Support getting vnf type = vpe
41 * This class supports the GenericGetVnf Sub Flow.
42 * This Generic sub flow can be used by any flow for accomplishing
43 * the goal of getting a Vnf Object (from AAI). The flow currently
44 * supports the querying of 2 types of Vnfs, generic-vnf and vce. The
45 * type must be provided by the calling flow and the type should
46 * be mapped to the variable GENDV_type. The type should either be
47 * "generic-vnf" or "vce". If the Vnf Id is not provided by the calling
48 * flow then this sub flow will execute the query to get the
49 * Vnf using the Vnf Name. Therefore, the calling flow must provide
50 * either the Vnf Id or Vnf Name.
52 * Upon successful completion of this sub flow the
53 * GENDV_SuccessIndicator will be true and the query response payload
54 * will be set to GENDV_vnf. An MSOWorkflowException will
55 * be thrown upon unsuccessful completion or if an error occurs
56 * at any time during this sub flow. Please map variables
57 * to the corresponding variable names below.
59 * Note - if this sub flow receives a Not Found (404) response
60 * from AAI at any time this will be considered an acceptable
61 * successful response however the GENDV_FoundIndicator
62 * set to false. This will allow the calling flow to distinguish
63 * between the two success scenarios, "Success where Vnf is found"
64 * and "Success where Vnf is NOT found".
68 * @param - GENDV_vnfId
70 * @param (Optional) - GENDV_resourceVersion
75 * @param - GENDV_SuccessIndicator
76 * @param - GENDV_FoundIndicator
77 * @param - WorkflowException
79 class GenericDeleteVnf extends AbstractServiceTaskProcessor{
81 String Prefix = "GENDV_"
82 ExceptionUtil exceptionUtil = new ExceptionUtil()
85 * This method validates the incoming variables and
86 * determines if the resource version was provided
90 public void preProcessRequest(Execution execution) {
91 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
92 execution.setVariable("prefix",Prefix)
93 utils.log("DEBUG", " *** STARTED GenericDeleteVnf PreProcessRequest Process*** ", isDebugEnabled)
95 execution.setVariable("GENDV_resourceVersionProvided", true)
96 execution.setVariable("GENDV_SuccessIndicator", false)
97 execution.setVariable("GENDV_FoundIndicator", false)
101 String vnfId = execution.getVariable("GENDV_vnfId")
102 String type = execution.getVariable("GENDV_type")
104 if(isBlank(type) || isBlank(vnfId)){
105 utils.log("ERROR", "Incoming Required Variable is null!", isDebugEnabled)
106 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Required Variable is Missing or Null!")
108 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
110 String resourceVersion = execution.getVariable("GENDV_resourceVersion")
111 if(isBlank(resourceVersion)){
112 utils.log("DEBUG", "Vnf Resource Version is NOT Provided", isDebugEnabled)
113 execution.setVariable("GENDV_resourceVersionProvided", false)
115 utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)
119 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
122 utils.log("ERROR", " Error encountered within GenericDeleteVnf PreProcessRequest method!" + e, isDebugEnabled)
123 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericDeleteVnf PreProcessRequest")
126 utils.log("DEBUG", "*** COMPLETED GenericDeleteVnf PreProcessRequest Process ***", isDebugEnabled)
130 * This method executes a GET call to AAI for the Vnf
131 * so that the Vnf's resource-version can be
136 public void getVnfResourceVersion(Execution execution){
137 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
138 execution.setVariable("prefix",Prefix)
139 utils.log("DEBUG", " *** STARTED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)
141 String vnfId = execution.getVariable("GENDV_vnfId")
142 String type = execution.getVariable("GENDV_type")
143 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
145 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
146 AaiUtil aaiUriUtil = new AaiUtil(this)
148 //Determine Type of Vnf Querying For
150 if(type.equals("generic-vnf")){
151 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
152 }else if(type.equals("vce")){
153 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
155 utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)
156 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")
159 String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
161 execution.setVariable("GENDV_getVnfPath", getVnfPath)
162 utils.logAudit("Get Vnf Resource Version Url is: " + getVnfPath)
164 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
165 int responseCode = response.getStatusCode()
166 execution.setVariable("GENDV_getVnfResponseCode", responseCode)
167 utils.log("DEBUG", " GET Vnf response code is: " + responseCode, isDebugEnabled)
169 String aaiResponse = response.getResponseBodyAsString()
170 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
171 execution.setVariable("GENDV_getVnfResponse", aaiResponse)
174 if(responseCode == 200 || responseCode == 202){
175 utils.log("DEBUG", "GET Vnf Received a Good Response: \n" + aaiResponse, isDebugEnabled)
176 execution.setVariable("GENDV_FoundIndicator", true)
177 if(utils.nodeExists(aaiResponse, "resource-version")){
178 String resourceVersion = utils.getNodeText1(aaiResponse, "resource-version")
179 execution.setVariable("GENDV_resourceVersion", resourceVersion)
180 utils.log("DEBUG", "SI Resource Version is: " + resourceVersion, isDebugEnabled)
182 utils.log("DEBUG", "GET Vnf for Resource Version Response Does NOT Contain a resource-version", isDebugEnabled)
183 exceptionUtil.buildAndThrowWorkflowException(execution, 400, "Unable to obtain Vnf resource-version. GET Vnf Response Does NOT Contain a resource-version")
186 }else if(responseCode == 404){
187 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
188 execution.setVariable("GENDV_SuccessIndicator", true)
189 execution.setVariable("WorkflowResponse", " ") // for junits
192 utils.log("DEBUG", " GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
193 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
194 throw new BpmnError("MSOWorkflowException")
197 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
200 utils.log("DEBUG", " Error encountered within GenericDeleteVnf GetVnfResourceVersion method!" + e, isDebugEnabled)
201 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfResourceVersion")
203 utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf GetVnfResourceVersion Process*** ", isDebugEnabled)
207 * This method executes a DELETE call to AAI for the provided
212 public void deleteVnf(Execution execution){
213 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
214 execution.setVariable("prefix",Prefix)
215 utils.log("DEBUG", " *** STARTED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)
217 String vnfId = execution.getVariable("GENDV_vnfId")
218 String type = execution.getVariable("GENDV_type")
219 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
220 String resourceVersion = execution.getVariable("GENDV_resourceVersion")
221 utils.log("DEBUG", "Incoming Vnf Resource Version is: " + resourceVersion, isDebugEnabled)
223 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
224 AaiUtil aaiUriUtil = new AaiUtil(this)
226 //Determine Type of Vnf Querying For
228 if(type.equals("generic-vnf")){
229 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
230 }else if(type.equals("vce")){
231 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
233 utils.log("DEBUG", "Invalid Incoming GENDV_type", isDebugEnabled)
234 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENDV_type")
237 String deleteVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") +'?resource-version=' + UriUtils.encode(resourceVersion,"UTF-8")
239 execution.setVariable("GENDV_deleteVnfPath", deleteVnfPath)
240 utils.logAudit("Delete Vnf Url is: " + deleteVnfPath)
242 APIResponse response = aaiUriUtil.executeAAIDeleteCall(execution, deleteVnfPath)
243 int responseCode = response.getStatusCode()
244 execution.setVariable("GENDV_deleteVnfResponseCode", responseCode)
245 utils.log("DEBUG", " DELETE Vnf response code is: " + responseCode, isDebugEnabled)
247 String aaiResponse = response.getResponseBodyAsString()
248 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
249 execution.setVariable("GENDV_deleteVnfResponse", aaiResponse)
252 if(responseCode == 204){
253 utils.log("DEBUG", " DELETE Vnf Received a Good Response", isDebugEnabled)
254 execution.setVariable("GENDV_FoundIndicator", true)
255 }else if(responseCode == 404){
256 utils.log("DEBUG", " DELETE Vnf Received a Not Found (404) Response", isDebugEnabled)
257 }else if(responseCode == 412){
258 utils.log("DEBUG", "DELETE Vnf Received a Resource Version Mismatch Error: \n" + aaiResponse, isDebugEnabled)
259 exceptionUtil.buildAndThrowWorkflowException(execution, 412, "Delete Vnf Received a resource-version Mismatch Error Response from AAI")
261 utils.log("DEBUG", "DELETE Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
262 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
263 throw new BpmnError("MSOWorkflowException")
266 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
269 utils.log("DEBUG", " Error encountered within GenericDeleteVnf DeleteVnf method!" + e, isDebugEnabled)
270 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During Delete Vnf")
272 utils.log("DEBUG", " *** COMPLETED GenericDeleteVnf DeleteVnf Process*** ", isDebugEnabled)