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 GENGV_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 * GENGV_SuccessIndicator will be true and the query response payload
48 * will be set to GENGV_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 GENGV_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".
61 * Incoming Required Variables:
62 * @param - GENGV_vnfId or @param - GENGV_vnfName
68 * @param - GENGV_SuccessIndicator
69 * @param - GENGV_FoundIndicator
70 * @param - WorkflowException
72 class GenericGetVnf extends AbstractServiceTaskProcessor{
74 String Prefix = "GENGV_"
75 ExceptionUtil exceptionUtil = new ExceptionUtil()
78 * This method validates the incoming variables and
79 * determines the subsequent event based on which
80 * variables the calling flow provided.
84 public void preProcessRequest(Execution execution) {
85 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
86 execution.setVariable("prefix",Prefix)
87 utils.log("DEBUG", " *** STARTED GenericGetVnf PreProcessRequest Process*** ", isDebugEnabled)
89 execution.setVariable("GENGV_getVnfByName", false)
90 execution.setVariable("GENGV_SuccessIndicator", false)
91 execution.setVariable("GENGV_FoundIndicator", false)
95 String vnfId = execution.getVariable("GENGV_vnfId")
96 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
97 String vnfName = execution.getVariable("GENGV_vnfName")
98 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
100 if(isBlank(vnfId) && isBlank(vnfName)){
101 utils.log("DEBUG", "Incoming Vnf Name and Vnf Id are null. At least one is required!", isDebugEnabled)
102 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Name and Vnf Id are null. At least one is required.")
105 execution.setVariable("GENGV_getVnfByName", true)
110 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
113 utils.log("DEBUG", " Error encountered within GenericGetVnf PreProcessRequest method!" + e, isDebugEnabled)
114 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetVnf PreProcessRequest")
117 utils.log("DEBUG", "*** COMPLETED GenericGetVnf PreProcessRequest Process ***", isDebugEnabled)
121 * This method executes a GET call to AAI to obtain the
122 * Vnf using the Vnf Name
126 public void getVnfByName(Execution execution){
127 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
128 execution.setVariable("prefix",Prefix)
129 utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)
131 String vnfName = execution.getVariable("GENGV_vnfName")
132 utils.log("DEBUG", "Getting Vnf by Vnf Name: " + vnfName, isDebugEnabled)
133 String type = execution.getVariable("GENGV_type")
134 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
136 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
137 AaiUtil aaiUriUtil = new AaiUtil(this)
139 //Determine Type of Vnf Querying For.
141 if(type.equals("generic-vnf")){
142 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
143 }else if(type.equals("vce")){
144 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
146 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
147 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
150 String getVnfPath = "${aai_endpoint}${aai_uri}?vnf-name=" + UriUtils.encode(vnfName, "UTF-8") + "&depth=1"
152 execution.setVariable("GENGV_getVnfPath", getVnfPath)
153 utils.logAudit("Get Vnf Url is: " + getVnfPath)
155 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
156 int responseCode = response.getStatusCode()
157 execution.setVariable("GENGV_getVnfResponseCode", responseCode)
158 utils.log("DEBUG", " GET Vnf response code is: " + responseCode, isDebugEnabled)
160 String aaiResponse = response.getResponseBodyAsString()
161 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
162 execution.setVariable("GENGV_getVnfResponse", aaiResponse)
165 if(responseCode == 200){
166 utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)
167 if(utils.nodeExists(aaiResponse, type)){
168 utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)
169 execution.setVariable("GENGV_FoundIndicator", true)
170 execution.setVariable("GENGV_vnf", aaiResponse)
171 execution.setVariable("WorkflowResponse", aaiResponse)
173 utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)
176 }else if(responseCode == 404){
177 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
179 utils.log("DEBUG", "GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
180 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
181 throw new BpmnError("MSOWorkflowException")
184 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
187 utils.log("ERROR", " Error encountered within GenericGetVnf GetVnfByName method!" + e, isDebugEnabled)
188 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfByName")
190 utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)
194 * This method executes a GET call to AAI to obtain the
195 * Vnf using the Vnf Id
199 public void getVnfById(Execution execution){
200 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
201 execution.setVariable("prefix",Prefix)
202 utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)
204 String vnfId = execution.getVariable("GENGV_vnfId")
205 utils.log("DEBUG", "Getting Vnf by Vnf Id: " + vnfId, isDebugEnabled)
206 String type = execution.getVariable("GENGV_type")
207 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
209 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
210 AaiUtil aaiUriUtil = new AaiUtil(this)
212 //Determine Type of Vnf Querying For.
214 if(type.equals("generic-vnf")){
215 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
216 }else if(type.equals("vce")){
217 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
218 }else if(type.equals("vpe")){
219 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericGetVnf does not yet support getting type of vnf = vpe")
221 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
222 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
224 utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)
226 String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
227 utils.log("DEBUG", "GET Vnf Endpoint is: " + getVnfPath, isDebugEnabled)
229 execution.setVariable("GENGV_getVnfPath", getVnfPath)
230 utils.logAudit("Get Vnf Url is: " + getVnfPath)
232 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
233 int responseCode = response.getStatusCode()
234 execution.setVariable("GENGV_getVnfResponseCode", responseCode)
235 utils.log("DEBUG", " GET Vnf response code is: " + responseCode, isDebugEnabled)
237 String aaiResponse = response.getResponseBodyAsString()
238 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
239 execution.setVariable("GENGV_getVnfResponse", aaiResponse)
242 if(responseCode == 200){
243 utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)
244 if(utils.nodeExists(aaiResponse, type)){
245 utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)
246 execution.setVariable("GENGV_FoundIndicator", true)
247 execution.setVariable("GENGV_vnf", aaiResponse)
248 execution.setVariable("WorkflowResponse", aaiResponse)
250 utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)
253 }else if(responseCode == 404){
254 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
256 utils.log("DEBUG", "GET Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
257 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
258 throw new BpmnError("MSOWorkflowException")
261 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
264 utils.log("ERROR", " Error encountered within GenericGetVnf GetVnfById method!" + e, isDebugEnabled)
265 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfById")
267 utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)