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.onap.so.bpmn.common.scripts
23 import org.onap.so.bpmn.core.UrnPropertiesReader
24 import static org.onap.so.bpmn.common.scripts.GenericUtils.*;
26 import org.apache.commons.lang3.*
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution
29 import org.onap.so.rest.APIResponse
30 import org.springframework.web.util.UriUtils
31 import org.onap.so.logger.MessageEnum
32 import org.onap.so.logger.MsoLogger
38 * TODO: Support getting vnf type = vpe
40 * This class supports the GenericGetVnf Sub Flow.
41 * This Generic sub flow can be used by any flow for accomplishing
42 * the goal of getting a Vnf Object (from AAI). The flow currently
43 * supports the querying of 2 types of Vnfs, generic-vnf and vce. The
44 * type must be provided by the calling flow and the type should
45 * be mapped to the variable GENGV_type. The type should either be
46 * "generic-vnf" or "vce". If the Vnf Id is not provided by the calling
47 * flow then this sub flow will execute the query to get the
48 * Vnf using the Vnf Name. Therefore, the calling flow must provide
49 * either the Vnf Id or Vnf Name.
51 * Upon successful completion of this sub flow the
52 * GENGV_SuccessIndicator will be true and the query response payload
53 * will be set to GENGV_vnf. An MSOWorkflowException will
54 * be thrown upon unsuccessful completion or if an error occurs
55 * at any time during this sub flow. Please map variables
56 * to the corresponding variable names below.
58 * Note - if this sub flow receives a Not Found (404) response
59 * from AAI at any time this will be considered an acceptable
60 * successful response however the GENGV_FoundIndicator
61 * set to false. This will allow the calling flow to distinguish
62 * between the two success scenarios, "Success where Vnf is found"
63 * and "Success where Vnf is NOT found".
66 * Variable Mapping Below
68 * In Mapping Variables:
69 * @param - GENGV_vnfId or @param - GENGV_vnfName
72 * Out Mapping Variables:
74 * @param - GENGV_SuccessIndicator
75 * @param - GENGV_FoundIndicator
76 * @param - WorkflowException
80 class GenericGetVnf extends AbstractServiceTaskProcessor{
81 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenericGetVnf.class);
84 String Prefix = "GENGV_"
85 ExceptionUtil exceptionUtil = new ExceptionUtil()
88 * This method validates the incoming variables and
89 * determines the subsequent event based on which
90 * variables the calling flow provided.
94 public void preProcessRequest(DelegateExecution execution) {
95 execution.setVariable("prefix",Prefix)
96 msoLogger.trace("STARTED GenericGetVnf PreProcessRequest Process")
98 execution.setVariable("GENGV_getVnfByName", false)
99 execution.setVariable("GENGV_SuccessIndicator", false)
100 execution.setVariable("GENGV_FoundIndicator", false)
104 String vnfId = execution.getVariable("GENGV_vnfId")
105 msoLogger.debug("Incoming Vnf Id is: " + vnfId)
106 String vnfName = execution.getVariable("GENGV_vnfName")
107 msoLogger.debug("Incoming Vnf Name is: " + vnfName)
109 if(isBlank(vnfId) && isBlank(vnfName)){
110 msoLogger.debug("Incoming Vnf Name and Vnf Id are null. At least one is required!")
111 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Name and Vnf Id are null. At least one is required.")
114 execution.setVariable("GENGV_getVnfByName", true)
119 msoLogger.debug("Rethrowing MSOWorkflowException")
122 msoLogger.debug(" Error encountered within GenericGetVnf PreProcessRequest method!" + e)
123 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetVnf PreProcessRequest")
126 msoLogger.trace("COMPLETED GenericGetVnf PreProcessRequest Process ")
130 * This method executes a GET call to AAI to obtain the
131 * Vnf using the Vnf Name
135 public void getVnfByName(DelegateExecution execution){
136 execution.setVariable("prefix",Prefix)
137 msoLogger.trace("STARTED GenericGetVnf GetVnfByName Process")
139 String vnfName = execution.getVariable("GENGV_vnfName")
140 msoLogger.debug("Getting Vnf by Vnf Name: " + vnfName)
141 String type = execution.getVariable("GENGV_type")
142 msoLogger.debug("Type of Vnf Getting is: " + type)
144 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
145 AaiUtil aaiUriUtil = new AaiUtil(this)
147 //Determine Type of Vnf Querying For.
149 if(type.equals("generic-vnf")){
150 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
151 }else if(type.equals("vce")){
152 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
154 msoLogger.debug("Invalid Incoming GENGV_type")
155 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
158 String getVnfPath = "${aai_endpoint}${aai_uri}?vnf-name=" + UriUtils.encode(vnfName, "UTF-8") + "&depth=1"
160 execution.setVariable("GENGV_getVnfPath", getVnfPath)
161 msoLogger.debug("Get Vnf Url is: " + getVnfPath)
163 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
164 int responseCode = response.getStatusCode()
165 execution.setVariable("GENGV_getVnfResponseCode", responseCode)
166 msoLogger.debug(" GET Vnf response code is: " + responseCode)
168 String aaiResponse = response.getResponseBodyAsString()
169 execution.setVariable("GENGV_getVnfResponse", aaiResponse)
172 if(responseCode == 200){
173 msoLogger.debug("GET Vnf Received a Good Response")
174 if(utils.nodeExists(aaiResponse, type)){
175 msoLogger.debug("GET Vnf Response Contains a Vnf")
176 execution.setVariable("GENGV_FoundIndicator", true)
177 execution.setVariable("GENGV_vnf", aaiResponse)
178 execution.setVariable("WorkflowResponse", aaiResponse)
180 msoLogger.debug("GET Vnf Response Does NOT Contain a Vnf")
183 }else if(responseCode == 404){
184 msoLogger.debug("GET Vnf Received a Not Found (404) Response")
186 msoLogger.debug("GET Vnf Received a Bad Response: \n" + aaiResponse)
187 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
188 throw new BpmnError("MSOWorkflowException")
191 msoLogger.debug("Rethrowing MSOWorkflowException")
194 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericGetVnf GetVnfByName method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
195 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfByName")
197 msoLogger.trace("COMPLETED GenericGetVnf GetVnfByName Process")
201 * This method executes a GET call to AAI to obtain the
202 * Vnf using the Vnf Id
206 public void getVnfById(DelegateExecution execution){
207 execution.setVariable("prefix",Prefix)
208 msoLogger.trace("STARTED GenericGetVnf GetVnfById Process")
210 String vnfId = execution.getVariable("GENGV_vnfId")
211 msoLogger.debug("Getting Vnf by Vnf Id: " + vnfId)
212 String type = execution.getVariable("GENGV_type")
213 msoLogger.debug("Type of Vnf Getting is: " + type)
215 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
216 AaiUtil aaiUriUtil = new AaiUtil(this)
218 //Determine Type of Vnf Querying For.
220 if(type.equals("generic-vnf")){
221 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
222 }else if(type.equals("vce")){
223 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
224 }else if(type.equals("vpe")){
225 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "GenericGetVnf does not yet support getting type of vnf = vpe")
227 msoLogger.debug("Invalid Incoming GENGV_type")
228 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
230 msoLogger.debug("Using AAI Uri: " + aai_uri)
232 String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8") + "?depth=1"
233 msoLogger.debug("GET Vnf Endpoint is: " + getVnfPath)
235 execution.setVariable("GENGV_getVnfPath", getVnfPath)
236 msoLogger.debug("Get Vnf Url is: " + getVnfPath)
238 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
239 int responseCode = response.getStatusCode()
240 execution.setVariable("GENGV_getVnfResponseCode", responseCode)
241 msoLogger.debug(" GET Vnf response code is: " + responseCode)
243 String aaiResponse = response.getResponseBodyAsString()
244 execution.setVariable("GENGV_getVnfResponse", aaiResponse)
247 if(responseCode == 200){
248 msoLogger.debug("GET Vnf Received a Good Response")
249 if(utils.nodeExists(aaiResponse, type)){
250 msoLogger.debug("GET Vnf Response Contains a Vnf")
251 execution.setVariable("GENGV_FoundIndicator", true)
252 execution.setVariable("GENGV_vnf", aaiResponse)
253 execution.setVariable("WorkflowResponse", aaiResponse)
255 msoLogger.debug("GET Vnf Response Does NOT Contain a Vnf")
258 }else if(responseCode == 404){
259 msoLogger.debug("GET Vnf Received a Not Found (404) Response")
261 msoLogger.debug("GET Vnf Received a BAD REST Response: \n" + aaiResponse)
262 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
263 throw new BpmnError("MSOWorkflowException")
266 msoLogger.debug("Rethrowing MSOWorkflowException")
269 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericGetVnf GetVnfById method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
270 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfById")
272 msoLogger.trace("COMPLETED GenericGetVnf GetVnfById Process")