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
25 import org.apache.commons.lang3.StringEscapeUtils
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.so.rest.APIResponse
29 import org.springframework.web.util.UriUtils
30 import org.onap.so.logger.MessageEnum
31 import org.onap.so.logger.MsoLogger
33 import static org.apache.commons.lang3.StringUtils.isBlank
38 * This class supports the GenericGetService Sub Flow.
39 * This Generic sub flow can be used by any flow for accomplishing
40 * the goal of getting a Service-Instance or Service-Subscription (from AAI).
41 * The calling flow must set the GENGS_type variable as "service-instance"
42 * or "service-subscription".
44 * When using to Get a Service-Instance:
45 * If the global-customer-id and service-type are not provided
46 * this flow executes a query to get the service- Url using the
47 * Service Id or Name (whichever is provided).
49 * When using to Get a Service-Subscription:
50 * The global-customer-id and service-type must be
53 * Upon successful completion of this sub flow the
54 * GENGS_SuccessIndicator will be true and the query response payload
55 * will be set to GENGS_service. An MSOWorkflowException will
56 * be thrown upon unsuccessful completion or if an error occurs
57 * at any time during this sub flow. Please map variables
58 * to the corresponding variable names below.
60 * Note - If this sub flow receives a Not Found (404) response
61 * from AAI at any time this will be considered an acceptable
62 * successful response however the GENGS_FoundIndicator
63 * will be set to false. This variable will allow the calling flow
64 * to distinguish between the two Success scenarios,
65 * "Success where service- is found" and
66 * "Success where service- is NOT found".
69 * Variable Mapping Below:
71 * In Mapping Variables:
72 * For Allotted-Resource:
73 * @param - GENGS_allottedResourceId
75 * @param (Optional) - GENGS_serviceInstanceId
76 * @param (Optional) - GENGS_serviceType
77 * @param (Optional) - GENGS_globalCustomerId
79 * For Service-Instance:
80 * @param - GENGS_serviceInstanceId or @param - GENGS_serviceInstanceName
82 * @param (Optional) - GENGS_serviceType
83 * @param (Optional) - GENGS_globalCustomerId
85 * For Service-Subscription:
87 * @param - GENGS_serviceType
88 * @param - GENGS_globalCustomerId
91 * Out Mapping Variables:
92 * @param - GENGS_service
93 * @param - GENGS_FoundIndicator
94 * @param - WorkflowException
96 class GenericGetService extends AbstractServiceTaskProcessor{
97 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, GenericGetService.class);
100 String Prefix = "GENGS_"
101 ExceptionUtil exceptionUtil = new ExceptionUtil()
104 * This method validates the incoming variables and
105 * determines the subsequent event based on which
106 * variables the calling flow provided.
111 public void preProcessRequest(DelegateExecution execution) {
112 execution.setVariable("prefix",Prefix)
113 msoLogger.trace("STARTED GenericGetService PreProcessRequest Process")
115 execution.setVariable("GENGS_obtainObjectsUrl", false)
116 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", false)
117 execution.setVariable("GENGS_SuccessIndicator", false)
118 execution.setVariable("GENGS_FoundIndicator", false)
119 execution.setVariable("GENGS_resourceLink", null)
120 execution.setVariable("GENGS_siResourceLink", null)
124 String allottedResourceId = execution.getVariable("GENGS_allottedResourceId")
125 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
126 String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
127 String serviceType = execution.getVariable("GENGS_serviceType")
128 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
129 String type = execution.getVariable("GENGS_type")
132 msoLogger.debug("Incoming GENGS_type is: " + type)
133 if(type.equalsIgnoreCase("allotted-resource")){
134 if(isBlank(allottedResourceId)){
135 msoLogger.debug("Incoming allottedResourceId is null. Allotted Resource Id is required to Get an allotted-resource.")
136 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming allottedResourceId is null. Allotted Resource Id is required to Get an allotted-resource.")
138 msoLogger.debug("Incoming Allotted Resource Id is: " + allottedResourceId)
139 if(isBlank(globalCustomerId) || isBlank(serviceType) || isBlank(serviceInstanceId)){
140 execution.setVariable("GENGS_obtainObjectsUrl", true)
142 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
143 msoLogger.debug("Incoming Service Type is: " + serviceType)
144 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
147 }else if(type.equalsIgnoreCase("service-instance")){
148 if(isBlank(serviceInstanceId) && isBlank(serviceInstanceName)){
149 msoLogger.debug("Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
150 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
152 msoLogger.debug("Incoming Service Instance Id is: " + serviceInstanceId)
153 msoLogger.debug("Incoming Service Instance Name is: " + serviceInstanceName)
154 if(isBlank(globalCustomerId) || isBlank(serviceType)){
155 execution.setVariable("GENGS_obtainObjectsUrl", true)
156 if(isBlank(serviceInstanceId)){
157 execution.setVariable("GENGS_obtainServiceInstanceUrlByName", true)
160 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
161 msoLogger.debug("Incoming Service Type is: " + serviceType)
164 }else if(type.equalsIgnoreCase("service-subscription")){
165 if(isBlank(serviceType) || isBlank(globalCustomerId)){
166 msoLogger.debug("Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
167 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
169 msoLogger.debug("Incoming Service Type is: " + serviceType)
170 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
173 exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
176 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENGS_type is null. Variable is Required.")
180 msoLogger.debug("Rethrowing MSOWorkflowException")
183 msoLogger.debug("Internal Error encountered within GenericGetService PreProcessRequest method!" + e)
184 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetService PreProcessRequest")
187 msoLogger.trace("COMPLETED GenericGetService PreProcessRequest Process ")
191 * This method obtains the Url to the provided service instance
192 * using the Service Instance Id.
196 public void obtainServiceInstanceUrlById(DelegateExecution execution){
197 execution.setVariable("prefix",Prefix)
198 msoLogger.trace("STARTED GenericGetService ObtainServiceInstanceUrlById Process")
200 AaiUtil aaiUriUtil = new AaiUtil(this)
201 String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
202 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
204 String type = execution.getVariable("GENGS_type")
206 if(type.equalsIgnoreCase("service-instance")){
207 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
208 msoLogger.debug(" Querying Node for Service-Instance URL by using Service-Instance Id: " + serviceInstanceId)
209 path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-id:EQUALS:${serviceInstanceId}"
210 msoLogger.debug("Service Instance Node Query Url is: " + path)
211 msoLogger.debug("Service Instance Node Query Url is: " + path)
212 }else if(type.equalsIgnoreCase("allotted-resource")){
213 String allottedResourceId = execution.getVariable("GENGS_allottedResourceId")
214 msoLogger.debug(" Querying Node for Service-Instance URL by using Allotted Resource Id: " + allottedResourceId)
215 path = "${aai_uri}?search-node-type=allotted-resource&filter=id:EQUALS:${allottedResourceId}"
216 msoLogger.debug("Allotted Resource Node Query Url is: " + path)
217 msoLogger.debug("Allotted Resource Node Query Url is: " + path)
220 //String url = "${aai_endpoint}${path}" host name needs to be removed from property
221 String url = "${path}"
222 execution.setVariable("GENGS_genericQueryPath", url)
224 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
225 int responseCode = response.getStatusCode()
226 execution.setVariable("GENGS_genericQueryResponseCode", responseCode)
227 msoLogger.debug(" GET Service Instance response code is: " + responseCode)
228 msoLogger.debug("GenericGetService AAI GET Response Code: " + responseCode)
230 String aaiResponse = response.getResponseBodyAsString()
231 execution.setVariable("GENGS_obtainSIUrlResponseBeforeUnescaping", aaiResponse)
232 msoLogger.debug("GenericGetService AAI Response before unescaping: " + aaiResponse)
233 execution.setVariable("GENGS_genericQueryResponse", aaiResponse)
234 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
235 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
238 if(responseCode == 200){
239 msoLogger.debug("Generic Query Received a Good Response Code")
240 execution.setVariable("GENGS_SuccessIndicator", true)
241 if(utils.nodeExists(aaiResponse, "result-data")){
242 msoLogger.debug("Generic Query Response Does Contain Data" )
243 execution.setVariable("GENGS_FoundIndicator", true)
244 String resourceLink = utils.getNodeText(aaiResponse, "resource-link")
245 execution.setVariable("GENGS_resourceLink", resourceLink)
246 execution.setVariable("GENGS_siResourceLink", resourceLink)
248 msoLogger.debug("Generic Query Response Does NOT Contains Data" )
249 execution.setVariable("WorkflowResponse", " ") //for junits
251 }else if(responseCode == 404){
252 msoLogger.debug("Generic Query Received a Not Found (404) Response")
253 execution.setVariable("GENGS_SuccessIndicator", true)
254 execution.setVariable("WorkflowResponse", " ") //for junits
256 msoLogger.debug("Generic Query Received a BAD REST Response: \n" + aaiResponse)
257 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
258 throw new BpmnError("MSOWorkflowException")
261 msoLogger.debug("Rethrowing MSOWorkflowException")
264 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericGetService ObtainServiceInstanceUrlById method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
265 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlById")
267 msoLogger.trace("COMPLETED GenericGetService ObtainServiceInstanceUrlById Process")
271 * This method obtains the Url to the provided service instance
272 * using the Service Instance Name.
276 public void obtainServiceInstanceUrlByName(DelegateExecution execution){
277 execution.setVariable("prefix",Prefix)
278 msoLogger.trace("STARTED GenericGetService ObtainServiceInstanceUrlByName Process")
280 String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
281 msoLogger.debug(" Querying Node for Service-Instance URL by using Service-Instance Name " + serviceInstanceName)
283 AaiUtil aaiUriUtil = new AaiUtil(this)
284 String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
285 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
286 String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-name:EQUALS:${serviceInstanceName}"
288 //String url = "${aai_endpoint}${path}" host name needs to be removed from property
289 String url = "${path}"
290 execution.setVariable("GENGS_obtainSIUrlPath", url)
292 msoLogger.debug("GenericGetService AAI Endpoint: " + aai_endpoint)
293 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
294 int responseCode = response.getStatusCode()
295 execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
296 msoLogger.debug(" GET Service Instance response code is: " + responseCode)
297 msoLogger.debug("GenericGetService AAI Response Code: " + responseCode)
299 String aaiResponse = response.getResponseBodyAsString()
300 execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
301 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
303 if(responseCode == 200){
304 msoLogger.debug(" Query for Service Instance Url Received a Good Response Code")
305 execution.setVariable("GENGS_SuccessIndicator", true)
306 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
307 boolean nodeExists = isBlank(globalCustomerId) ? utils.nodeExists(aaiResponse, "result-data") : hasCustomerServiceInstance(aaiResponse, globalCustomerId)
309 msoLogger.debug("Query for Service Instance Url Response Does Contain Data" )
310 execution.setVariable("GENGS_FoundIndicator", true)
311 String resourceLink = utils.getNodeText(aaiResponse, "resource-link")
312 execution.setVariable("GENGS_resourceLink", resourceLink)
313 execution.setVariable("GENGS_siResourceLink", resourceLink)
315 msoLogger.debug("Query for Service Instance Url Response Does NOT Contains Data" )
316 execution.setVariable("WorkflowResponse", " ") //for junits
318 }else if(responseCode == 404){
319 msoLogger.debug(" Query for Service Instance Received a Not Found (404) Response")
320 execution.setVariable("GENGS_SuccessIndicator", true)
321 execution.setVariable("WorkflowResponse", " ") //for junits
323 msoLogger.debug("Query for Service Instance Received a BAD REST Response: \n" + aaiResponse)
324 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
325 throw new BpmnError("MSOWorkflowException")
328 msoLogger.debug("Rethrowing MSOWorkflowException")
331 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, " Error encountered within GenericGetService ObtainServiceInstanceUrlByName method!" + e, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "");
332 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlByName")
334 msoLogger.trace("COMPLETED GenericGetService ObtainServiceInstanceUrlByName Process")
339 * This method executes a GET call to AAI to obtain the
340 * service-instance or service-subscription
344 public void getServiceObject(DelegateExecution execution){
345 execution.setVariable("prefix",Prefix)
346 msoLogger.trace("STARTED GenericGetService GetServiceObject Process")
348 String type = execution.getVariable("GENGS_type")
349 AaiUtil aaiUriUtil = new AaiUtil(this)
350 String aai_endpoint = UrnPropertiesReader.getVariable("aai.endpoint", execution)
351 String serviceEndpoint = ""
353 msoLogger.debug("GenericGetService getServiceObject AAI Endpoint: " + aai_endpoint)
354 if(type.equalsIgnoreCase("service-instance")){
355 String siResourceLink = execution.getVariable("GENGS_resourceLink")
356 if(isBlank(siResourceLink)){
357 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
358 msoLogger.debug(" Incoming GENGS_serviceInstanceId is: " + serviceInstanceId)
359 String serviceType = execution.getVariable("GENGS_serviceType")
360 msoLogger.debug(" Incoming GENGS_serviceType is: " + serviceType)
361 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
362 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
364 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
365 msoLogger.debug('AAI URI is: ' + aai_uri)
366 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")
368 msoLogger.debug("Incoming Service Instance Url is: " + siResourceLink)
369 String[] split = siResourceLink.split("/aai/")
370 serviceEndpoint = "/aai/" + split[1]
372 }else if(type.equalsIgnoreCase("allotted-resource")){
373 String siResourceLink = execution.getVariable("GENGS_resourceLink")
374 if(isBlank(siResourceLink)){
375 String allottedResourceId = execution.getVariable("GENGS_allottedResourceId")
376 msoLogger.debug(" Incoming GENGS_allottedResourceId is: " + allottedResourceId)
377 String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
378 msoLogger.debug(" Incoming GENGS_serviceInstanceId is: " + serviceInstanceId)
379 String serviceType = execution.getVariable("GENGS_serviceType")
380 msoLogger.debug(" Incoming GENGS_serviceType is: " + serviceType)
381 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
382 msoLogger.debug("Incoming Global Customer Id is: " + globalCustomerId)
384 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
385 msoLogger.debug('AAI URI is: ' + aai_uri)
386 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")
388 msoLogger.debug("Incoming Allotted-Resource Url is: " + siResourceLink)
389 String[] split = siResourceLink.split("/aai/")
390 serviceEndpoint = "/aai/" + split[1]
392 }else if(type.equalsIgnoreCase("service-subscription")){
393 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
394 String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
395 String serviceType = execution.getVariable("GENGS_serviceType")
396 serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
399 String serviceUrl = "${aai_endpoint}" + serviceEndpoint
401 execution.setVariable("GENGS_getServiceUrl", serviceUrl)
402 msoLogger.debug("GET Service AAI Path is: \n" + serviceUrl)
404 APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceUrl)
405 int responseCode = response.getStatusCode()
406 execution.setVariable("GENGS_getServiceResponseCode", responseCode)
407 msoLogger.debug(" GET Service response code is: " + responseCode)
408 msoLogger.debug("GenericGetService AAI Response Code: " + responseCode)
410 String aaiResponse = response.getResponseBodyAsString()
411 execution.setVariable("GENGS_getServiceResponse", aaiResponse)
412 msoLogger.debug("GenericGetService AAI Response: " + aaiResponse)
414 if(responseCode == 200 || responseCode == 202){
415 msoLogger.debug("GET Service Received a Good Response Code")
416 if(utils.nodeExists(aaiResponse, "service-instance") || utils.nodeExists(aaiResponse, "service-subscription")){
417 msoLogger.debug("GET Service Response Contains a service-instance" )
418 execution.setVariable("GENGS_FoundIndicator", true)
419 execution.setVariable("GENGS_service", aaiResponse)
420 execution.setVariable("WorkflowResponse", aaiResponse)
423 msoLogger.debug("GET Service Response Does NOT Contain Data" )
425 }else if(responseCode == 404){
426 msoLogger.debug("GET Service Received a Not Found (404) Response")
427 execution.setVariable("WorkflowResponse", " ") //for junits
430 msoLogger.debug(" GET Service Received a Bad Response: \n" + aaiResponse)
431 exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
432 throw new BpmnError("MSOWorkflowException")
435 msoLogger.debug("Rethrowing MSOWorkflowException")
438 msoLogger.debug(" Error encountered within GenericGetService GetServiceObject method!" + e)
439 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GenericGetService")
441 msoLogger.trace("COMPLETED GenericGetService GetServiceObject Process")
445 * An utility method which check whether a service(by name) is already present within a globalCustomerId or not.
446 * @param jsonResponse raw response received from AAI by searching ServiceInstance by Name.
447 * @param globalCustomerId
448 * @return {@code true} if globalCustomerId is found at 6th position within "resource-link", {@code false} in any other cases.
450 public boolean hasCustomerServiceInstance(String aaiResponse, final String globalCustomerId) {
451 if (isBlank(aaiResponse)) {
454 aaiResponse = utils.removeXmlNamespaces(aaiResponse)
455 ArrayList<String> linksArray = utils.getMultNodeObjects(aaiResponse, "resource-link")
456 if (linksArray == null || linksArray.size() == 0) {
459 for (String resourceLink : linksArray) {
460 int custStart = resourceLink.indexOf("customer/")
461 int custEnd = resourceLink.indexOf("/service-subscriptions/")
462 String receivedCustomerId = resourceLink.substring(custStart + 9, custEnd)
463 if (globalCustomerId.equals(receivedCustomerId)) {