2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.so.bpmn.common.scripts
24 import static org.apache.commons.lang3.StringUtils.*
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.bpmn.core.UrnPropertiesReader
32 import org.onap.so.logger.MessageEnum
33 import org.onap.so.logger.MsoLogger
37 * This class supports the GenericGetService Sub Flow.
38 * This Generic sub flow can be used by any flow for accomplishing
39 * the goal of getting a Service-Instance or Service-Subscription (from AAI).
40 * The calling flow must set the GENGS_type variable as "service-instance"
41 * or "service-subscription".
43 * When using to Get a Service-Instance:
44 * If the global-customer-id and service-type are not provided
45 * this flow executes a query to get the service- Url using the
46 * Service Id or Name (whichever is provided).
48 * When using to Get a Service-Subscription:
49 * The global-customer-id and service-type must be
52 * Upon successful completion of this sub flow the
53 * GENGS_SuccessIndicator will be true and the query response payload
54 * will be set to GENGS_service. 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 GENGS_FoundIndicator
62 * will be set to false. This variable will allow the calling flow
63 * to distinguish between the two Success scenarios,
64 * "Success where service- is found" and
65 * "Success where service- is NOT found".
68 * Variable Mapping Below:
70 * In Mapping Variables:
71 * For Allotted-Resource:
72 * @param - GENGS_allottedResourceId
74 * @param (Optional) - GENGS_serviceInstanceId
75 * @param (Optional) - GENGS_serviceType
76 * @param (Optional) - GENGS_globalCustomerId
78 * For Service-Instance:
79 * @param - GENGS_serviceInstanceId or @param - GENGS_serviceInstanceName
81 * @param (Optional) - GENGS_serviceType
82 * @param (Optional) - GENGS_globalCustomerId
84 * For Service-Subscription:
86 * @param - GENGS_serviceType
87 * @param - GENGS_globalCustomerId
90 * Out Mapping Variables:
91 * @param - GENGS_service
92 * @param - GENGS_FoundIndicator
93 * @param - WorkflowException
95 class CustomE2EGetService extends AbstractServiceTaskProcessor{
96 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CustomE2EGetService.class);
98 String Prefix = "GENGS_"
99 ExceptionUtil exceptionUtil = new ExceptionUtil()
102 * This method validates the incoming variables and
103 * determines the subsequent event based on which
104 * variables the calling flow provided.
109 public void preProcessRequest(DelegateExecution execution) {
110 execution.setVariable("prefix",Prefix)
111 msoLogger.trace("STARTED GenericGetService PreProcessRequest Process")
113 execution.setVariable("GENGS_obtainObjectsUrl", false)
114 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", false)
115 execution.setVariable("GENGS_SuccessIndicator", false)
116 execution.setVariable("GENGS_FoundIndicator", false)
117 execution.setVariable("GENGS_resourceLink", null)
118 execution.setVariable("GENGS_siResourceLink", null)
122 String allottedResourceId = execution.getVariable("GENGS_allottedResourceId")
123 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
124 String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
125 String serviceType = execution.getVariable("GENGS_serviceType")
126 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
127 String type = execution.getVariable("GENGS_type")
130 msoLogger.debug("Incoming GENGS_type is: " + type)
131 if(type.equalsIgnoreCase("allotted-resource")){
132 if(isBlank(allottedResourceId)){
133 msoLogger.debug("Incoming allottedResourceId is null. Allotted Resource Id is required to Get an allotted-resource.")
134 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming allottedResourceId is null. Allotted Resource Id is required to Get an allotted-resource.")
136 msoLogger.debug("Incoming Allotted Resource Id is: " + allottedResourceId)
137 if(isBlank(globalCustomerId) || isBlank(serviceType) || isBlank(serviceInstanceId)){
138 execution.setVariable("GENGS_obtainObjectsUrl", true)
140 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
141 msoLogger.debug("Incoming Service Type is: " + serviceType)
142 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
145 }else if(type.equalsIgnoreCase("service-instance")){
146 if(isBlank(serviceInstanceId) && isBlank(serviceInstanceName)){
147 msoLogger.debug("Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
148 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
150 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
151 msoLogger.debug("Incoming Service Instance Name is: " + serviceInstanceName)
152 if(isBlank(globalCustomerId) || isBlank(serviceType)){
153 execution.setVariable("GENGS_obtainObjectsUrl", true)
154 if(isBlank(serviceInstanceId)){
155 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", true)
158 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
159 msoLogger.debug("Incoming Service Type is: " + serviceType)
162 }else if(type.equalsIgnoreCase("service-subscription")){
163 if(isBlank(serviceType) || isBlank(globalCustomerId)){
164 msoLogger.debug("Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
165 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
167 msoLogger.debug("Incoming Service Type is: " + serviceType)
168 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
171 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
174 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENGS_type is null. Variable is Required.")
178 msoLogger.debug("Rethrowing MSOWorkflowException")
181 msoLogger.debug("Internal Error encountered within GenericGetService PreProcessRequest method!" + e)
182 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetService PreProcessRequest")
185 msoLogger.trace("COMPLETED GenericGetService PreProcessRequest Process ")
189 * This method obtains the Url to the provided service instance
190 * using the Service Instance Id.
194 public void obtainServiceInstanceUrlById(DelegateExecution execution){
195 execution.setVariable("prefix",Prefix)
196 msoLogger.trace("STARTED GenericGetService ObtainServiceInstanceUrlById Process")
198 AaiUtil aaiUriUtil = new AaiUtil(this)
199 String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
200 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
202 String type = execution.getVariable("GENGS_type")
204 if(type.equalsIgnoreCase("service-instance")){
205 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
206 msoLogger.debug(" Querying Node for Service-Instance URL by using Service-Instance Id: " + serviceInstanceId)
207 path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-id:EQUALS:${serviceInstanceId}"
208 msoLogger.debug("Service Instance Node Query Url is: " + path)
209 msoLogger.debug("Service Instance Node Query Url is: " + path)
210 }else if(type.equalsIgnoreCase("allotted-resource")){
211 String allottedResourceId = execution.getVariable("GENGS_allottedResourceId")
212 msoLogger.debug(" Querying Node for Service-Instance URL by using Allotted Resource Id: " + allottedResourceId)
213 path = "${aai_uri}?search-node-type=allotted-resource&filter=id:EQUALS:${allottedResourceId}"
214 msoLogger.debug("Allotted Resource Node Query Url is: " + path)
215 msoLogger.debug("Allotted Resource Node Query Url is: " + path)
218 //String url = "${aai_endpoint}${path}" host name needs to be removed from property
219 String url = "${path}"
220 execution.setVariable("GENGS_genericQueryPath", url)
222 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
223 int responseCode = response.getStatusCode()
224 execution.setVariable("GENGS_genericQueryResponseCode", responseCode)
225 msoLogger.debug(" GET Service Instance response code is: " + responseCode)
226 msoLogger.debug("GenericGetService AAI GET Response Code: " + responseCode)
228 String aaiResponse = response.getResponseBodyAsString()
229 execution.setVariable("GENGS_obtainSIUrlResponseBeforeUnescaping", aaiResponse)
230 msoLogger.debug("GenericGetService AAI Response before unescaping: " + aaiResponse)
231 execution.setVariable("GENGS_genericQueryResponse", aaiResponse)
232 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
233 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
236 if(responseCode == 200){
237 msoLogger.debug("Generic Query Received a Good Response Code")
238 execution.setVariable("GENGS_SuccessIndicator", true)
239 if(utils.nodeExists(aaiResponse, "result-data")){
240 msoLogger.debug("Generic Query Response Does Contain Data" )
241 execution.setVariable("GENGS_FoundIndicator", true)
242 String resourceLink = utils.getNodeText(aaiResponse, "resource-link")
243 execution.setVariable("GENGS_resourceLink", resourceLink)
244 execution.setVariable("GENGS_siResourceLink", resourceLink)
246 msoLogger.debug("Generic Query Response Does NOT Contains Data" )
247 execution.setVariable("WorkflowResponse", " ") //for junits
249 }else if(responseCode == 404){
250 msoLogger.debug("Generic Query Received a Not Found (404) Response")
251 execution.setVariable("GENGS_SuccessIndicator", true)
252 execution.setVariable("WorkflowResponse", " ") //for junits
254 msoLogger.debug("Generic Query Received a BAD REST Response: \n" + aaiResponse)
255 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
256 throw new BpmnError("MSOWorkflowException")
259 msoLogger.debug("Rethrowing MSOWorkflowException")
262 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericGetService ObtainServiceInstanceUrlById method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
263 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlById")
265 msoLogger.trace("COMPLETED GenericGetService ObtainServiceInstanceUrlById Process")
269 * This method obtains the Url to the provided service instance
270 * using the Service Instance Name.
274 public void obtainServiceInstanceUrlByName(DelegateExecution execution){
275 execution.setVariable("prefix",Prefix)
276 msoLogger.trace("STARTED GenericGetService ObtainServiceInstanceUrlByName Process")
278 String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
279 msoLogger.debug(" Querying Node for Service-Instance URL by using Service-Instance Name " + serviceInstanceName)
281 AaiUtil aaiUriUtil = new AaiUtil(this)
282 String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
283 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
284 String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-name:EQUALS:${serviceInstanceName}"
286 //String url = "${aai_endpoint}${path}" host name needs to be removed from property
287 String url = "${path}"
288 execution.setVariable("GENGS_obtainSIUrlPath", url)
290 msoLogger.debug("GenericGetService AAI Endpoint: " + aai_endpoint)
291 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
292 int responseCode = response.getStatusCode()
293 execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
294 msoLogger.debug(" GET Service Instance response code is: " + responseCode)
295 msoLogger.debug("GenericGetService AAI Response Code: " + responseCode)
297 String aaiResponse = response.getResponseBodyAsString()
298 execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
299 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
301 if(responseCode == 200){
302 msoLogger.debug(" Query for Service Instance Url Received a Good Response Code")
303 execution.setVariable("GENGS_SuccessIndicator", true)
304 if(utils.nodeExists(aaiResponse, "result-data")){
305 msoLogger.debug("Query for Service Instance Url Response Does Contain Data" )
306 execution.setVariable("GENGS_FoundIndicator", true)
307 String resourceLink = utils.getNodeText(aaiResponse, "resource-link")
308 execution.setVariable("GENGS_resourceLink", resourceLink)
309 execution.setVariable("GENGS_siResourceLink", resourceLink)
311 msoLogger.debug("Query for Service Instance Url Response Does NOT Contains Data" )
312 execution.setVariable("WorkflowResponse", " ") //for junits
314 }else if(responseCode == 404){
315 msoLogger.debug(" Query for Service Instance Received a Not Found (404) Response")
316 execution.setVariable("GENGS_SuccessIndicator", true)
317 execution.setVariable("WorkflowResponse", " ") //for junits
319 msoLogger.debug("Query for Service Instance Received a BAD REST Response: \n" + aaiResponse)
320 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
321 throw new BpmnError("MSOWorkflowException")
324 msoLogger.debug("Rethrowing MSOWorkflowException")
327 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericGetService ObtainServiceInstanceUrlByName method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
328 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlByName")
330 msoLogger.trace("COMPLETED GenericGetService ObtainServiceInstanceUrlByName Process")
335 * This method executes a GET call to AAI to obtain the
336 * service-instance or service-subscription
340 public void getServiceObject(DelegateExecution execution){
341 execution.setVariable("prefix",Prefix)
342 msoLogger.trace("STARTED GenericGetService GetServiceObject Process")
344 String type = execution.getVariable("GENGS_type")
345 AaiUtil aaiUriUtil = new AaiUtil(this)
346 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
347 String serviceEndpoint = ""
349 msoLogger.debug("GenericGetService getServiceObject AAI Endpoint: " + aai_endpoint)
350 if(type.equalsIgnoreCase("service-instance")){
351 String siResourceLink = execution.getVariable("GENGS_resourceLink")
352 if(isBlank(siResourceLink)){
353 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
354 msoLogger.debug(" Incoming GENGS_serviceInstanceId is: " + serviceInstanceId)
355 String serviceType = execution.getVariable("GENGS_serviceType")
356 msoLogger.debug(" Incoming GENGS_serviceType is: " + serviceType)
357 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
358 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
360 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
361 msoLogger.debug('AAI URI is: ' + aai_uri)
362 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")
364 msoLogger.debug("Incoming Service Instance Url is: " + siResourceLink)
365 String[] split = siResourceLink.split("/aai/")
366 serviceEndpoint = "/aai/" + split[1]
368 }else if(type.equalsIgnoreCase("allotted-resource")){
369 String siResourceLink = execution.getVariable("GENGS_resourceLink")
370 if(isBlank(siResourceLink)){
371 String allottedResourceId = execution.getVariable("GENGS_allottedResourceId")
372 msoLogger.debug(" Incoming GENGS_allottedResourceId is: " + allottedResourceId)
373 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
374 msoLogger.debug(" Incoming GENGS_serviceInstanceId is: " + serviceInstanceId)
375 String serviceType = execution.getVariable("GENGS_serviceType")
376 msoLogger.debug(" Incoming GENGS_serviceType is: " + serviceType)
377 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
378 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
380 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
381 msoLogger.debug('AAI URI is: ' + aai_uri)
382 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") + "/allotted-resources/allotted-resource/" + UriUtils.encode(allottedResourceId,"UTF-8")
384 msoLogger.debug("Incoming Allotted-Resource Url is: " + siResourceLink)
385 String[] split = siResourceLink.split("/aai/")
386 serviceEndpoint = "/aai/" + split[1]
388 }else if(type.equalsIgnoreCase("service-subscription")){
389 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
390 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
391 String serviceType = execution.getVariable("GENGS_serviceType")
392 serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
395 String serviceUrl = "${aai_endpoint}" + serviceEndpoint
397 execution.setVariable("GENGS_getServiceUrl", serviceUrl)
398 msoLogger.debug("GET Service AAI Path is: \n" + serviceUrl)
400 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceUrl)
401 int responseCode = response.getStatusCode()
402 execution.setVariable("GENGS_getServiceResponseCode", responseCode)
403 msoLogger.debug(" GET Service response code is: " + responseCode)
404 msoLogger.debug("GenericGetService AAI Response Code: " + responseCode)
406 String aaiResponse = response.getResponseBodyAsString()
407 execution.setVariable("GENGS_getServiceResponse", aaiResponse)
408 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
410 if(responseCode == 200 || responseCode == 202){
411 msoLogger.debug("GET Service Received a Good Response Code")
412 if(utils.nodeExists(aaiResponse, "service-instance") || utils.nodeExists(aaiResponse, "service-subscription")){
413 msoLogger.debug("GET Service Response Contains a service-instance" )
414 execution.setVariable("GENGS_FoundIndicator", true)
415 execution.setVariable("GENGS_service", aaiResponse)
416 execution.setVariable("WorkflowResponse", aaiResponse)
419 msoLogger.debug("GET Service Response Does NOT Contain Data" )
421 }else if(responseCode == 404){
422 msoLogger.debug("GET Service Received a Not Found (404) Response")
423 execution.setVariable("WorkflowResponse", " ") //for junits
426 msoLogger.debug(" GET Service Received a Bad Response: \n" + aaiResponse)
427 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
428 throw new BpmnError("MSOWorkflowException")
431 msoLogger.debug("Rethrowing MSOWorkflowException")
434 msoLogger.debug(" Error encountered within GenericGetService GetServiceObject method!" + e)
435 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GenericGetService")
437 msoLogger.trace("COMPLETED GenericGetService GetServiceObject Process")