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 * This class supports the GenericGetService Sub Flow.
34 * This Generic sub flow can be used by any flow for accomplishing
35 * the goal of getting a Service-Instance or Service-Subscription (from AAI).
36 * The calling flow must set the GENGS_type variable as "service-instance"
37 * or "service-subscription".
39 * When using to Get a Service-Instance:
40 * If the global-customer-id and service-type are not provided
41 * this flow executes a query to get the service- Url using the
42 * Service Id or Name (whichever is provided).
44 * When using to Get a Service-Subscription:
45 * The global-customer-id and service-type must be
48 * Upon successful completion of this sub flow the
49 * GENGS_SuccessIndicator will be true and the query response payload
50 * will be set to GENGS_service. An MSOWorkflowException will
51 * be thrown upon unsuccessful completion or if an error occurs
52 * at any time during this sub flow. Please map variables
53 * to the corresponding variable names below.
55 * Note - If this sub flow receives a Not Found (404) response
56 * from AAI at any time this will be considered an acceptable
57 * successful response however the GENGS_FoundIndicator
58 * will be set to false. This variable will allow the calling flow
59 * to distinguish between the two Success scenarios,
60 * "Success where service- is found" and
61 * "Success where service- is NOT found".
64 * Incoming Variables (Service-Instance):
65 * @param - GENGS_serviceInstanceId or @param - GENGS_serviceInstanceName
67 * @param (Optional) - GENGS_serviceType
68 * @param (Optional) - GENGS_globalCustomerId
70 * Incoming Variables (Service-Subscription):
72 * @param - GENGS_serviceType
73 * @param - GENGS_globalCustomerId
77 * @param - GENGS_service
78 * @param - GENGS_SuccessIndicator
79 * @param - GENGS_FoundIndicator
80 * @param - WorkflowException
82 class GenericGetService extends AbstractServiceTaskProcessor{
84 String Prefix = "GENGS_"
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(Execution execution) {
95 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
96 execution.setVariable("prefix",Prefix)
97 utils.log("DEBUG", " *** STARTED GenericGetService PreProcessRequest Process*** ", isDebugEnabled)
99 execution.setVariable("GENGS_obtainServiceInstanceUrl", false)
100 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", false)
101 execution.setVariable("GENGS_SuccessIndicator", false)
102 execution.setVariable("GENGS_FoundIndicator", false)
103 execution.setVariable("GENGS_siResourceLink", null)
107 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
108 String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
109 String serviceType = execution.getVariable("GENGS_serviceType")
110 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
111 String type = execution.getVariable("GENGS_type")
114 utils.log("DEBUG", "Incoming GENGS_type is: " + type, isDebugEnabled)
115 if(type.equalsIgnoreCase("service-instance")){
116 if(isBlank(serviceInstanceId) && isBlank(serviceInstanceName)){
117 utils.log("DEBUG", "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.", isDebugEnabled)
118 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
120 utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
121 utils.log("DEBUG", "Incoming Service Instance Name is: " + serviceInstanceName, isDebugEnabled)
122 if(isBlank(globalCustomerId) || isBlank(serviceType)){
123 execution.setVariable("GENGS_obtainServiceInstanceUrl", true)
124 if(isBlank(serviceInstanceId)){
125 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", true)
128 utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
129 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
132 }else if(type.equalsIgnoreCase("service-subscription")){
133 if(isBlank(serviceType) || isBlank(globalCustomerId)){
134 utils.log("DEBUG", "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.", isDebugEnabled)
135 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
137 utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
138 utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
141 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
144 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENGS_type is null. Variable is Required.")
148 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
151 utils.log("DEBUG", "Internal Error encountered within GenericGetService PreProcessRequest method!" + e, isDebugEnabled)
152 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetService PreProcessRequest")
155 utils.log("DEBUG", "*** COMPLETED GenericGetService PreProcessRequest Process ***", isDebugEnabled)
159 * This method obtains the Url to the provided service instance
160 * using the Service Instance Id.
164 public void obtainServiceInstanceUrlById(Execution execution){
165 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
166 execution.setVariable("prefix",Prefix)
167 utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)
169 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
170 utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Id: " + serviceInstanceId, isDebugEnabled)
172 AaiUtil aaiUriUtil = new AaiUtil(this)
173 String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
174 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
176 utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)
177 String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-id:EQUALS:${serviceInstanceId}"
179 //String url = "${aai_endpoint}${path}" host name needs to be removed from property
180 String url = "${path}"
181 execution.setVariable("GENGS_obtainSIUrlPath", url)
183 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
184 int responseCode = response.getStatusCode()
185 execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
186 utils.log("DEBUG", " GET Service Instance response code is: " + responseCode, isDebugEnabled)
187 utils.logAudit("GenericGetService AAI GET Response Code: " + responseCode)
189 String aaiResponse = response.getResponseBodyAsString()
190 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
191 execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
192 utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
194 if(responseCode == 200){
195 utils.log("DEBUG", " Query for Service Instance Url Received a Good Response Code", isDebugEnabled)
196 execution.setVariable("GENGS_SuccessIndicator", true)
197 if(utils.nodeExists(aaiResponse, "result-data")){
198 utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)
199 execution.setVariable("GENGS_FoundIndicator", true)
200 String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")
201 execution.setVariable("GENGS_siResourceLink", resourceLink)
203 utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)
204 execution.setVariable("WorkflowResponse", " ") //for junits
206 }else if(responseCode == 404){
207 utils.log("DEBUG", " Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)
208 execution.setVariable("GENGS_SuccessIndicator", true)
209 execution.setVariable("WorkflowResponse", " ") //for junits
211 utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
212 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
213 throw new BpmnError("MSOWorkflowException")
216 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
219 utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlById method!" + e, isDebugEnabled)
220 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlById")
222 utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)
226 * This method obtains the Url to the provided service instance
227 * using the Service Instance Name.
231 public void obtainServiceInstanceUrlByName(Execution execution){
232 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
233 execution.setVariable("prefix",Prefix)
234 utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)
236 String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
237 utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Name " + serviceInstanceName, isDebugEnabled)
239 AaiUtil aaiUriUtil = new AaiUtil(this)
240 String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
241 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
242 String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-name:EQUALS:${serviceInstanceName}"
244 //String url = "${aai_endpoint}${path}" host name needs to be removed from property
245 String url = "${path}"
246 execution.setVariable("GENGS_obtainSIUrlPath", url)
248 utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)
249 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
250 int responseCode = response.getStatusCode()
251 execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
252 utils.log("DEBUG", " GET Service Instance response code is: " + responseCode, isDebugEnabled)
253 utils.logAudit("GenericGetService AAI Response Code: " + responseCode)
255 String aaiResponse = response.getResponseBodyAsString()
256 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
257 execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
258 utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
260 if(responseCode == 200){
261 utils.log("DEBUG", " Query for Service Instance Url Received a Good Response Code", isDebugEnabled)
262 execution.setVariable("GENGS_SuccessIndicator", true)
263 if(utils.nodeExists(aaiResponse, "result-data")){
264 utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)
265 execution.setVariable("GENGS_FoundIndicator", true)
266 String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")
267 execution.setVariable("GENGS_siResourceLink", resourceLink)
269 utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)
270 execution.setVariable("WorkflowResponse", " ") //for junits
272 }else if(responseCode == 404){
273 utils.log("DEBUG", " Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)
274 execution.setVariable("GENGS_SuccessIndicator", true)
275 execution.setVariable("WorkflowResponse", " ") //for junits
277 utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
278 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
279 throw new BpmnError("MSOWorkflowException")
282 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
285 utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlByName method!" + e, isDebugEnabled)
286 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlByName")
288 utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)
293 * This method executes a GET call to AAI to obtain the
294 * service-instance or service-subscription
298 public void getServiceObject(Execution execution){
299 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
300 execution.setVariable("prefix",Prefix)
301 utils.log("DEBUG", " *** STARTED GenericGetService GetServiceObject Process*** ", isDebugEnabled)
303 String type = execution.getVariable("GENGS_type")
304 AaiUtil aaiUriUtil = new AaiUtil(this)
305 String aai_endpoint = execution.getVariable("URN_aai_endpoint")
306 String serviceEndpoint = ""
308 utils.logAudit("GenericGetService getServiceObject AAI Endpoint: " + aai_endpoint)
309 if(type.equalsIgnoreCase("service-instance")){
310 String siResourceLink = execution.getVariable("GENGS_siResourceLink")
311 if(isBlank(siResourceLink)){
312 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
313 utils.log("DEBUG", " Incoming GENGS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
314 String serviceType = execution.getVariable("GENGS_serviceType")
315 utils.log("DEBUG", " Incoming GENGS_serviceType is: " + serviceType, isDebugEnabled)
316 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
317 utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
319 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
320 logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
321 serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8") + "/service-instances/service-instance/" + UriUtils.encode(serviceInstanceId,"UTF-8")
323 utils.log("DEBUG", "Incoming Service Instance Resource Link is: " + siResourceLink, isDebugEnabled)
324 String[] split = siResourceLink.split("/aai/")
325 serviceEndpoint = "/aai/" + split[1]
327 }else if(type.equalsIgnoreCase("service-subscription")){
328 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
329 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
330 String serviceType = execution.getVariable("GENGS_serviceType")
331 serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
334 String serviceUrl = "${aai_endpoint}" + serviceEndpoint
336 execution.setVariable("GENGS_getServiceUrl", serviceUrl)
337 utils.log("DEBUG", "GET Service AAI Path is: \n" + serviceUrl, isDebugEnabled)
339 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceUrl)
340 int responseCode = response.getStatusCode()
341 execution.setVariable("GENGS_getServiceResponseCode", responseCode)
342 utils.log("DEBUG", " GET Service response code is: " + responseCode, isDebugEnabled)
343 utils.logAudit("GenericGetService AAI Response Code: " + responseCode)
345 String aaiResponse = response.getResponseBodyAsString()
346 aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
347 execution.setVariable("GENGS_getServiceResponse", aaiResponse)
348 utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
351 if(responseCode == 200 || responseCode == 202){
352 utils.log("DEBUG", "GET Service Received a Good Response Code", isDebugEnabled)
353 if(utils.nodeExists(aaiResponse, "service-instance") || utils.nodeExists(aaiResponse, "service-subscription")){
354 utils.log("DEBUG", "GET Service Response Contains a service-instance" , isDebugEnabled)
355 execution.setVariable("GENGS_FoundIndicator", true)
356 execution.setVariable("GENGS_service", aaiResponse)
357 execution.setVariable("WorkflowResponse", aaiResponse)
360 utils.log("DEBUG", "GET Service Response Does NOT Contain Data" , isDebugEnabled)
362 }else if(responseCode == 404){
363 utils.log("DEBUG", "GET Service Received a Not Found (404) Response", isDebugEnabled)
364 execution.setVariable("WorkflowResponse", " ") //for junits
367 utils.log("DEBUG", " GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
368 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
369 throw new BpmnError("MSOWorkflowException")
372 utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
375 utils.log("DEBUG", " Error encountered within GenericGetService GetServiceObject method!" + e, isDebugEnabled)
376 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GenericGetService")
378 utils.log("DEBUG", " *** COMPLETED GenericGetService GetServiceObject Process*** ", isDebugEnabled)