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 GENGV_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 * GENGV_SuccessIndicator will be true and the query response payload
54 * will be set to GENGV_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 GENGV_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".
67 * Incoming Required Variables:
68 * @param - GENGV_vnfId or @param - GENGV_vnfName
74 * @param - GENGV_SuccessIndicator
75 * @param - GENGV_FoundIndicator
76 * @param - WorkflowException
78 class GenericGetVnf extends AbstractServiceTaskProcessor{
80 String Prefix = "GENGV_"
81 ExceptionUtil exceptionUtil = new ExceptionUtil()
84 * This method validates the incoming variables and
85 * determines the subsequent event based on which
86 * variables the calling flow provided.
90 public void preProcessRequest(Execution execution) {
91 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
92 execution.setVariable("prefix",Prefix)
93 utils.log("DEBUG", " *** STARTED GenericGetVnf PreProcessRequest Process*** ", isDebugEnabled)
95 execution.setVariable("GENGV_getVnfByName", false)
96 execution.setVariable("GENGV_SuccessIndicator", false)
97 execution.setVariable("GENGV_FoundIndicator", false)
101 String vnfId = execution.getVariable("GENGV_vnfId")
102 utils.log("DEBUG", "Incoming Vnf Id is: " + vnfId, isDebugEnabled)
103 String vnfName = execution.getVariable("GENGV_vnfName")
104 utils.log("DEBUG", "Incoming Vnf Name is: " + vnfName, isDebugEnabled)
106 if(isBlank(vnfId) && isBlank(vnfName)){
107 utils.log("DEBUG", "Incoming Vnf Name and Vnf Id are null. At least one is required!", isDebugEnabled)
108 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Vnf Name and Vnf Id are null. At least one is required.")
111 execution.setVariable("GENGV_getVnfByName", true)
116 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
119 utils.log("DEBUG", " Error encountered within GenericGetVnf PreProcessRequest method!" + e, isDebugEnabled)
120 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetVnf PreProcessRequest")
123 utils.log("DEBUG", "*** COMPLETED GenericGetVnf PreProcessRequest Process ***", isDebugEnabled)
127 * This method executes a GET call to AAI to obtain the
128 * Vnf using the Vnf Name
132 public void getVnfByName(Execution execution){
133 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
134 execution.setVariable("prefix",Prefix)
135 utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)
137 String vnfName = execution.getVariable("GENGV_vnfName")
138 utils.log("DEBUG", "Getting Vnf by Vnf Name: " + vnfName, isDebugEnabled)
139 String type = execution.getVariable("GENGV_type")
140 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
142 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
143 AaiUtil aaiUriUtil = new AaiUtil(this)
145 //Determine Type of Vnf Querying For.
147 if(type.equals("generic-vnf")){
148 aai_uri = aaiUriUtil.getNetworkGenericVnfUri(execution)
149 }else if(type.equals("vce")){
150 aai_uri = aaiUriUtil.getNetworkVceUri(execution)
152 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
153 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
156 String getVnfPath = "${aai_endpoint}${aai_uri}?vnf-name=" + UriUtils.encode(vnfName, "UTF-8")
158 execution.setVariable("GENGV_getVnfPath", getVnfPath)
159 utils.logAudit("Get Vnf Url is: " + getVnfPath)
161 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
162 int responseCode = response.getStatusCode()
163 execution.setVariable("GENGV_getVnfResponseCode", responseCode)
164 utils.log("DEBUG", " GET Vnf response code is: " + responseCode, isDebugEnabled)
166 String aaiResponse = response.getResponseBodyAsString()
167 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
168 execution.setVariable("GENGV_getVnfResponse", aaiResponse)
171 if(responseCode == 200){
172 utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)
173 if(utils.nodeExists(aaiResponse, type)){
174 utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)
175 execution.setVariable("GENGV_FoundIndicator", true)
176 execution.setVariable("GENGV_vnf", aaiResponse)
177 execution.setVariable("WorkflowResponse", aaiResponse)
179 utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)
182 }else if(responseCode == 404){
183 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
185 utils.log("DEBUG", "GET Vnf Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
186 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
187 throw new BpmnError("MSOWorkflowException")
190 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
193 utils.log("ERROR", " Error encountered within GenericGetVnf GetVnfByName method!" + e, isDebugEnabled)
194 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfByName")
196 utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfByName Process*** ", isDebugEnabled)
200 * This method executes a GET call to AAI to obtain the
201 * Vnf using the Vnf Id
205 public void getVnfById(Execution execution){
206 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
207 execution.setVariable("prefix",Prefix)
208 utils.log("DEBUG", " *** STARTED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)
210 String vnfId = execution.getVariable("GENGV_vnfId")
211 utils.log("DEBUG", "Getting Vnf by Vnf Id: " + vnfId, isDebugEnabled)
212 String type = execution.getVariable("GENGV_type")
213 utils.log("DEBUG", "Type of Vnf Getting is: " + type, isDebugEnabled)
215 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
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 utils.log("DEBUG", "Invalid Incoming GENGV_type", isDebugEnabled)
228 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Invalid Incoming GENGV_type")
230 utils.log("DEBUG", "Using AAI Uri: " + aai_uri, isDebugEnabled)
232 String getVnfPath = "${aai_endpoint}${aai_uri}/" + UriUtils.encode(vnfId, "UTF-8")
233 utils.log("DEBUG", "GET Vnf Endpoint is: " + getVnfPath, isDebugEnabled)
235 execution.setVariable("GENGV_getVnfPath", getVnfPath)
236 utils.logAudit("Get Vnf Url is: " + getVnfPath)
238 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, getVnfPath)
239 int responseCode = response.getStatusCode()
240 execution.setVariable("GENGV_getVnfResponseCode", responseCode)
241 utils.log("DEBUG", " GET Vnf response code is: " + responseCode, isDebugEnabled)
243 String aaiResponse = response.getResponseBodyAsString()
244 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
245 execution.setVariable("GENGV_getVnfResponse", aaiResponse)
248 if(responseCode == 200){
249 utils.log("DEBUG", "GET Vnf Received a Good Response", isDebugEnabled)
250 if(utils.nodeExists(aaiResponse, type)){
251 utils.log("DEBUG", "GET Vnf Response Contains a Vnf", isDebugEnabled)
252 execution.setVariable("GENGV_FoundIndicator", true)
253 execution.setVariable("GENGV_vnf", aaiResponse)
254 execution.setVariable("WorkflowResponse", aaiResponse)
256 utils.log("DEBUG", "GET Vnf Response Does NOT Contain a Vnf", isDebugEnabled)
259 }else if(responseCode == 404){
260 utils.log("DEBUG", "GET Vnf Received a Not Found (404) Response", isDebugEnabled)
262 utils.log("DEBUG", "GET Vnf Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
263 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
264 throw new BpmnError("MSOWorkflowException")
267 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
270 utils.log("ERROR", " Error encountered within GenericGetVnf GetVnfById method!" + e, isDebugEnabled)
271 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GetVnfById")
273 utils.log("DEBUG", " *** COMPLETED GenericGetVnf GetVnfById Process*** ", isDebugEnabled)