[MSO-8] Second step of the rebase for MSO
[so.git] / bpmn / MSOCommonBPMN / src / main / groovy / org / openecomp / mso / bpmn / common / scripts / GenericGetService.groovy
index 304f33e..70ce1ba 100644 (file)
-/*-
- * ============LICENSE_START=======================================================
- * OPENECOMP - MSO
- * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.openecomp.mso.bpmn.common.scripts
-
-import static org.apache.commons.lang3.StringUtils.*
-
-import org.apache.commons.lang3.*
-import org.camunda.bpm.engine.delegate.BpmnError
-import org.camunda.bpm.engine.runtime.Execution
-import org.openecomp.mso.rest.APIResponse
-import org.springframework.web.util.UriUtils
-
-
-/**
- * This class supports the GenericGetService Sub Flow.
- * This Generic sub flow can be used by any flow for accomplishing
- * the goal of getting a Service-Instance or Service-Subscription (from AAI).
- * The calling flow must set the GENGS_type variable as "service-instance"
- * or "service-subscription".
- *
- * When using to Get a Service-Instance:
- * If the global-customer-id and service-type are not provided
- * this flow executes a query to get the service- Url using the
- * Service  Id or Name (whichever is provided).
- *
- * When using to Get a Service-Subscription:
- * The global-customer-id and service-type must be
- * provided.
- *
- * Upon successful completion of this sub flow the
- * GENGS_SuccessIndicator will be true and the query response payload
- * will be set to GENGS_service.  An MSOWorkflowException will
- * be thrown upon unsuccessful completion or if an error occurs
- * at any time during this sub flow. Please map variables
- * to the corresponding variable names below.
- *
- * Note - If this sub flow receives a Not Found (404) response
- * from AAI at any time this will be considered an acceptable
- * successful response however the GENGS_FoundIndicator
- * will be set to false. This variable will allow the calling flow
- * to distinguish between the two Success scenarios,
- * "Success where service- is found" and
- * "Success where service- is NOT found".
- *
- *
- * Incoming Variables (Service-Instance):
- * @param - GENGS_serviceInstanceId or @param - GENGS_serviceInstanceName
- * @param - GENGS_type
- * @param (Optional) - GENGS_serviceType
- * @param (Optional) - GENGS_globalCustomerId
- *
- * Incoming Variables (Service-Subscription):
- * @param - GENGS_type
- * @param - GENGS_serviceType
- * @param - GENGS_globalCustomerId
- *
- *
- * Outgoing Variables:
- * @param - GENGS_service
- * @param - GENGS_SuccessIndicator
- * @param - GENGS_FoundIndicator
- * @param - WorkflowException
- */
-class GenericGetService extends AbstractServiceTaskProcessor{
-
-       String Prefix = "GENGS_"
-       ExceptionUtil exceptionUtil = new ExceptionUtil()
-
-       /**
-        * This method validates the incoming variables and
-        * determines the subsequent event based on which
-        * variables the calling flow provided.
-        *
-        * @param - execution
-        */
-       public void preProcessRequest(Execution execution) {
-               def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
-               execution.setVariable("prefix",Prefix)
-               utils.log("DEBUG", " *** STARTED GenericGetService PreProcessRequest Process*** ", isDebugEnabled)
-
-               execution.setVariable("GENGS_obtainServiceInstanceUrl", false)
-               execution.setVariable("GENGS_obtainServiceInstanceUrlByName", false)
-               execution.setVariable("GENGS_SuccessIndicator", false)
-               execution.setVariable("GENGS_FoundIndicator", false)
-               execution.setVariable("GENGS_siResourceLink", null)
-
-               try{
-                       // Get Variables
-                       String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
-                       String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
-                       String serviceType = execution.getVariable("GENGS_serviceType")
-                       String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
-                       String type = execution.getVariable("GENGS_type")
-
-                       if(type != null){
-                               utils.log("DEBUG", "Incoming GENGS_type is: " + type, isDebugEnabled)
-                               if(type.equalsIgnoreCase("service-instance")){
-                                       if(isBlank(serviceInstanceId) && isBlank(serviceInstanceName)){
-                                               utils.log("DEBUG", "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.", isDebugEnabled)
-                                               exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")
-                                       }else{
-                                               utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)
-                                               utils.log("DEBUG", "Incoming Service Instance Name is: " + serviceInstanceName, isDebugEnabled)
-                                               if(isBlank(globalCustomerId) || isBlank(serviceType)){
-                                                       execution.setVariable("GENGS_obtainServiceInstanceUrl", true)
-                                                       if(isBlank(serviceInstanceId)){
-                                                               execution.setVariable("GENGS_obtainServiceInstanceUrlByName", true)
-                                                       }
-                                               }else{
-                                                       utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
-                                                       utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
-                                               }
-                                       }
-                               }else if(type.equalsIgnoreCase("service-subscription")){
-                                       if(isBlank(serviceType) || isBlank(globalCustomerId)){
-                                               utils.log("DEBUG", "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.", isDebugEnabled)
-                                               exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")
-                                       }else{
-                                               utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)
-                                               utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
-                                       }
-                               }else{
-                                       exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")
-                               }
-                       }else{
-                               exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENGS_type is null. Variable is Required.")
-                       }
-
-               }catch(BpmnError b){
-                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
-                       throw b
-               }catch(Exception e){
-                       utils.log("DEBUG", "Internal Error encountered within GenericGetService PreProcessRequest method!" + e, isDebugEnabled)
-                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetService PreProcessRequest")
-
-               }
-               utils.log("DEBUG", "*** COMPLETED GenericGetService PreProcessRequest Process ***", isDebugEnabled)
-       }
-
-       /**
-        * This method obtains the Url to the provided service instance
-        * using the Service Instance Id.
-        *
-        * @param - execution
-        */
-       public void obtainServiceInstanceUrlById(Execution execution){
-               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
-               execution.setVariable("prefix",Prefix)
-               utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)
-               try {
-                       String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
-                       utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Id: " + serviceInstanceId, isDebugEnabled)
-
-                       AaiUtil aaiUriUtil = new AaiUtil(this)
-                       String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
-                       String aai_endpoint = execution.getVariable("URN_aai_endpoint")
-                       
-                       utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)
-                       String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-id:EQUALS:${serviceInstanceId}"
-
-                       //String url = "${aai_endpoint}${path}"  host name needs to be removed from property
-                       String url = "${path}"
-                       execution.setVariable("GENGS_obtainSIUrlPath", url)
-
-                       APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
-                       int responseCode = response.getStatusCode()
-                       execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
-                       utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)
-                       utils.logAudit("GenericGetService AAI GET Response Code: " + responseCode)
-
-                       String aaiResponse = response.getResponseBodyAsString()
-                       aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
-                       execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
-                       utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
-                       //Process Response
-                       if(responseCode == 200){
-                               utils.log("DEBUG", "  Query for Service Instance Url Received a Good Response Code", isDebugEnabled)
-                               execution.setVariable("GENGS_SuccessIndicator", true)
-                               if(utils.nodeExists(aaiResponse, "result-data")){
-                                       utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)
-                                       execution.setVariable("GENGS_FoundIndicator", true)
-                                       String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")
-                                       execution.setVariable("GENGS_siResourceLink", resourceLink)
-                               }else{
-                                       utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)
-                                       execution.setVariable("WorkflowResponse", "  ") //for junits
-                               }
-                       }else if(responseCode == 404){
-                               utils.log("DEBUG", "  Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)
-                               execution.setVariable("GENGS_SuccessIndicator", true)
-                               execution.setVariable("WorkflowResponse", "  ") //for junits
-                       }else{
-                               utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
-                               exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
-                               throw new BpmnError("MSOWorkflowException")
-                       }
-               }catch(BpmnError b){
-                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
-                       throw b
-               }catch(Exception e){
-                       utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlById method!" + e, isDebugEnabled)
-                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlById")
-               }
-               utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)
-       }
-
-       /**
-        * This method obtains the Url to the provided service instance
-        * using the Service Instance Name.
-        *
-        * @param - execution
-        */
-       public void obtainServiceInstanceUrlByName(Execution execution){
-               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
-               execution.setVariable("prefix",Prefix)
-               utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)
-               try {
-                       String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")
-                       utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Name " + serviceInstanceName, isDebugEnabled)
-
-                       AaiUtil aaiUriUtil = new AaiUtil(this)
-                       String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)
-                       String aai_endpoint = execution.getVariable("URN_aai_endpoint")
-                       String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-name:EQUALS:${serviceInstanceName}"
-
-                       //String url = "${aai_endpoint}${path}"  host name needs to be removed from property
-                       String url = "${path}"
-                       execution.setVariable("GENGS_obtainSIUrlPath", url)
-
-                       utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)
-                       APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)
-                       int responseCode = response.getStatusCode()
-                       execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)
-                       utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)
-                       utils.logAudit("GenericGetService AAI Response Code: " + responseCode)
-                       
-                       String aaiResponse = response.getResponseBodyAsString()
-                       aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
-                       execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)
-                       utils.logAudit("GenericGetService AAI Response: " + aaiResponse) 
-                       //Process Response
-                       if(responseCode == 200){
-                               utils.log("DEBUG", "  Query for Service Instance Url Received a Good Response Code", isDebugEnabled)
-                               execution.setVariable("GENGS_SuccessIndicator", true)
-                               if(utils.nodeExists(aaiResponse, "result-data")){
-                                       utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)
-                                       execution.setVariable("GENGS_FoundIndicator", true)
-                                       String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")
-                                       execution.setVariable("GENGS_siResourceLink", resourceLink)
-                               }else{
-                                       utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)
-                                       execution.setVariable("WorkflowResponse", "  ") //for junits
-                               }
-                       }else if(responseCode == 404){
-                               utils.log("DEBUG", "  Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)
-                               execution.setVariable("GENGS_SuccessIndicator", true)
-                               execution.setVariable("WorkflowResponse", "  ") //for junits
-                       }else{
-                               utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)
-                               exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
-                               throw new BpmnError("MSOWorkflowException")
-                       }
-               }catch(BpmnError b){
-                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
-                       throw b
-               }catch(Exception e){
-                       utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlByName method!" + e, isDebugEnabled)
-                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlByName")
-               }
-               utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)
-       }
-
-
-       /**
-        * This method executes a GET call to AAI to obtain the
-        * service-instance or service-subscription
-        *
-        * @param - execution
-        */
-       public void getServiceObject(Execution execution){
-               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
-               execution.setVariable("prefix",Prefix)
-               utils.log("DEBUG", " *** STARTED GenericGetService GetServiceObject Process*** ", isDebugEnabled)
-               try {
-                       String type = execution.getVariable("GENGS_type")
-                       AaiUtil aaiUriUtil = new AaiUtil(this)
-                       String aai_endpoint = execution.getVariable("URN_aai_endpoint")
-                       String serviceEndpoint = ""
-                       
-                       utils.logAudit("GenericGetService getServiceObject AAI Endpoint: " + aai_endpoint)
-                       if(type.equalsIgnoreCase("service-instance")){
-                               String siResourceLink = execution.getVariable("GENGS_siResourceLink")
-                               if(isBlank(siResourceLink)){
-                                       String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")
-                                       utils.log("DEBUG", " Incoming GENGS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)
-                                       String serviceType = execution.getVariable("GENGS_serviceType")
-                                       utils.log("DEBUG", " Incoming GENGS_serviceType is: " + serviceType, isDebugEnabled)
-                                       String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
-                                       utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)
-
-                                       String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
-                                       logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)
-                                       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")
-                               }else{
-                                       utils.log("DEBUG", "Incoming Service Instance Resource Link is: " + siResourceLink, isDebugEnabled)
-                                       String[] split = siResourceLink.split("/aai/")
-                                       serviceEndpoint = "/aai/" + split[1]
-                               }
-                       }else if(type.equalsIgnoreCase("service-subscription")){
-                               String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
-                               String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")
-                               String serviceType = execution.getVariable("GENGS_serviceType")
-                               serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")
-                       }
-
-                       String serviceUrl = "${aai_endpoint}" + serviceEndpoint
-
-                       execution.setVariable("GENGS_getServiceUrl", serviceUrl)
-                       utils.log("DEBUG", "GET Service AAI Path is: \n" + serviceUrl, isDebugEnabled)
-
-                       APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceUrl)
-                       int responseCode = response.getStatusCode()
-                       execution.setVariable("GENGS_getServiceResponseCode", responseCode)
-                       utils.log("DEBUG", "  GET Service response code is: " + responseCode, isDebugEnabled)
-                       utils.logAudit("GenericGetService AAI Response Code: " + responseCode)
-                       
-                       String aaiResponse = response.getResponseBodyAsString()
-                       aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)
-                       execution.setVariable("GENGS_getServiceResponse", aaiResponse)
-                       utils.logAudit("GenericGetService AAI Response: " + aaiResponse)
-                       
-                       //Process Response
-                       if(responseCode == 200 || responseCode == 202){
-                               utils.log("DEBUG", "GET Service Received a Good Response Code", isDebugEnabled)
-                               if(utils.nodeExists(aaiResponse, "service-instance") || utils.nodeExists(aaiResponse, "service-subscription")){
-                                       utils.log("DEBUG", "GET Service Response Contains a service-instance" , isDebugEnabled)
-                                       execution.setVariable("GENGS_FoundIndicator", true)
-                                       execution.setVariable("GENGS_service", aaiResponse)
-                                       execution.setVariable("WorkflowResponse", aaiResponse)
-
-                               }else{
-                                       utils.log("DEBUG", "GET Service Response Does NOT Contain Data" , isDebugEnabled)
-                               }
-                       }else if(responseCode == 404){
-                               utils.log("DEBUG", "GET Service Received a Not Found (404) Response", isDebugEnabled)
-                               execution.setVariable("WorkflowResponse", "  ") //for junits
-                       }
-                       else{
-                               utils.log("DEBUG", "  GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)
-                               exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)
-                               throw new BpmnError("MSOWorkflowException")
-                       }
-               }catch(BpmnError b){
-                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)
-                       throw b
-               }catch(Exception e){
-                       utils.log("DEBUG", " Error encountered within GenericGetService GetServiceObject method!" + e, isDebugEnabled)
-                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GenericGetService")
-               }
-               utils.log("DEBUG", " *** COMPLETED GenericGetService GetServiceObject Process*** ", isDebugEnabled)
-       }
-
-}
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * OPENECOMP - MSO\r
+ * ================================================================================\r
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.openecomp.mso.bpmn.common.scripts\r
+\r
+import static org.apache.commons.lang3.StringUtils.*\r
+\r
+import org.apache.commons.lang3.*\r
+import org.camunda.bpm.engine.delegate.BpmnError\r
+import org.camunda.bpm.engine.runtime.Execution\r
+import org.openecomp.mso.rest.APIResponse\r
+import org.springframework.web.util.UriUtils\r
+\r
+\r
+/**\r
+ * This class supports the GenericGetService Sub Flow.\r
+ * This Generic sub flow can be used by any flow for accomplishing\r
+ * the goal of getting a Service-Instance or Service-Subscription (from AAI).\r
+ * The calling flow must set the GENGS_type variable as "service-instance"\r
+ * or "service-subscription".\r
+ *\r
+ * When using to Get a Service-Instance:\r
+ * If the global-customer-id and service-type are not provided\r
+ * this flow executes a query to get the service- Url using the\r
+ * Service  Id or Name (whichever is provided).\r
+ *\r
+ * When using to Get a Service-Subscription:\r
+ * The global-customer-id and service-type must be\r
+ * provided.\r
+ *\r
+ * Upon successful completion of this sub flow the\r
+ * GENGS_SuccessIndicator will be true and the query response payload\r
+ * will be set to GENGS_service.  An MSOWorkflowException will\r
+ * be thrown upon unsuccessful completion or if an error occurs\r
+ * at any time during this sub flow. Please map variables\r
+ * to the corresponding variable names below.\r
+ *\r
+ * Note - If this sub flow receives a Not Found (404) response\r
+ * from AAI at any time this will be considered an acceptable\r
+ * successful response however the GENGS_FoundIndicator\r
+ * will be set to false. This variable will allow the calling flow\r
+ * to distinguish between the two Success scenarios,\r
+ * "Success where service- is found" and\r
+ * "Success where service- is NOT found".\r
+ *\r
+ *\r
+ * Variable Mapping Below:\r
+ *\r
+ * In Mapping Variables:\r
+ *   For Service-Instance:\r
+ *     @param - GENGS_serviceInstanceId or @param - GENGS_serviceInstanceName\r
+ *     @param - GENGS_type\r
+ *     @param (Optional) - GENGS_serviceType\r
+ *     @param (Optional) - GENGS_globalCustomerId\r
+ *\r
+ *   For Service-Subscription:\r
+ *     @param - GENGS_type\r
+ *     @param - GENGS_serviceType\r
+ *     @param - GENGS_globalCustomerId\r
+ *\r
+ *\r
+ * Out Mapping Variables:\r
+ *    @param - GENGS_service\r
+ *    @param - GENGS_FoundIndicator\r
+ *    @param - WorkflowException\r
+ *\r
+ *\r
+ */\r
+class GenericGetService extends AbstractServiceTaskProcessor{\r
+\r
+       String Prefix = "GENGS_"\r
+       ExceptionUtil exceptionUtil = new ExceptionUtil()\r
+\r
+       /**\r
+        * This method validates the incoming variables and\r
+        * determines the subsequent event based on which\r
+        * variables the calling flow provided.\r
+        *\r
+        * @param - execution\r
+        */\r
+       public void preProcessRequest(Execution execution) {\r
+               def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
+               execution.setVariable("prefix",Prefix)\r
+               utils.log("DEBUG", " *** STARTED GenericGetService PreProcessRequest Process*** ", isDebugEnabled)\r
+\r
+               execution.setVariable("GENGS_obtainServiceInstanceUrl", false)\r
+               execution.setVariable("GENGS_obtainServiceInstanceUrlByName", false)\r
+               execution.setVariable("GENGS_SuccessIndicator", false)\r
+               execution.setVariable("GENGS_FoundIndicator", false)\r
+               execution.setVariable("GENGS_siResourceLink", null)\r
+\r
+               try{\r
+                       // Get Variables\r
+                       String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")\r
+                       String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")\r
+                       String serviceType = execution.getVariable("GENGS_serviceType")\r
+                       String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")\r
+                       String type = execution.getVariable("GENGS_type")\r
+\r
+                       if(type != null){\r
+                               utils.log("DEBUG", "Incoming GENGS_type is: " + type, isDebugEnabled)\r
+                               if(type.equalsIgnoreCase("service-instance")){\r
+                                       if(isBlank(serviceInstanceId) && isBlank(serviceInstanceName)){\r
+                                               utils.log("DEBUG", "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.", isDebugEnabled)\r
+                                               exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming serviceInstanceId and serviceInstanceName are null. ServiceInstanceId or ServiceInstanceName is required to Get a service-instance.")\r
+                                       }else{\r
+                                               utils.log("DEBUG", "Incoming Service Instance Id is: " + serviceInstanceId, isDebugEnabled)\r
+                                               utils.log("DEBUG", "Incoming Service Instance Name is: " + serviceInstanceName, isDebugEnabled)\r
+                                               if(isBlank(globalCustomerId) || isBlank(serviceType)){\r
+                                                       execution.setVariable("GENGS_obtainServiceInstanceUrl", true)\r
+                                                       if(isBlank(serviceInstanceId)){\r
+                                                               execution.setVariable("GENGS_obtainServiceInstanceUrlByName", true)\r
+                                                       }\r
+                                               }else{\r
+                                                       utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)\r
+                                                       utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)\r
+                                               }\r
+                                       }\r
+                               }else if(type.equalsIgnoreCase("service-subscription")){\r
+                                       if(isBlank(serviceType) || isBlank(globalCustomerId)){\r
+                                               utils.log("DEBUG", "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.", isDebugEnabled)\r
+                                               exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming ServiceType or GlobalCustomerId is null. These variables are required to Get a service-subscription.")\r
+                                       }else{\r
+                                               utils.log("DEBUG", "Incoming Service Type is: " + serviceType, isDebugEnabled)\r
+                                               utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)\r
+                                       }\r
+                               }else{\r
+                                       exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Incoming Type is Invalid. Please Specify Type as service-instance or service-subscription")\r
+                               }\r
+                       }else{\r
+                               exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Incoming GENGS_type is null. Variable is Required.")\r
+                       }\r
+\r
+               }catch(BpmnError b){\r
+                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
+                       throw b\r
+               }catch(Exception e){\r
+                       utils.log("DEBUG", "Internal Error encountered within GenericGetService PreProcessRequest method!" + e, isDebugEnabled)\r
+                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in GenericGetService PreProcessRequest")\r
+\r
+               }\r
+               utils.log("DEBUG", "*** COMPLETED GenericGetService PreProcessRequest Process ***", isDebugEnabled)\r
+       }\r
+\r
+       /**\r
+        * This method obtains the Url to the provided service instance\r
+        * using the Service Instance Id.\r
+        *\r
+        * @param - execution\r
+        */\r
+       public void obtainServiceInstanceUrlById(Execution execution){\r
+               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
+               execution.setVariable("prefix",Prefix)\r
+               utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)\r
+               try {\r
+                       String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")\r
+                       utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Id: " + serviceInstanceId, isDebugEnabled)\r
+\r
+                       AaiUtil aaiUriUtil = new AaiUtil(this)\r
+                       String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)\r
+                       String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
+                       \r
+                       utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)\r
+                       String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-id:EQUALS:${serviceInstanceId}"\r
+\r
+                       //String url = "${aai_endpoint}${path}"  host name needs to be removed from property\r
+                       String url = "${path}"\r
+                       execution.setVariable("GENGS_obtainSIUrlPath", url)\r
+\r
+                       APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)\r
+                       int responseCode = response.getStatusCode()\r
+                       execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)\r
+                       utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)\r
+                       utils.logAudit("GenericGetService AAI GET Response Code: " + responseCode)\r
+\r
+                       String aaiResponse = response.getResponseBodyAsString()\r
+                       aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
+                       execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)\r
+                       utils.logAudit("GenericGetService AAI Response: " + aaiResponse)\r
+                       //Process Response\r
+                       if(responseCode == 200){\r
+                               utils.log("DEBUG", "  Query for Service Instance Url Received a Good Response Code", isDebugEnabled)\r
+                               execution.setVariable("GENGS_SuccessIndicator", true)\r
+                               if(utils.nodeExists(aaiResponse, "result-data")){\r
+                                       utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)\r
+                                       execution.setVariable("GENGS_FoundIndicator", true)\r
+                                       String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")\r
+                                       execution.setVariable("GENGS_siResourceLink", resourceLink)\r
+                               }else{\r
+                                       utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)\r
+                                       execution.setVariable("WorkflowResponse", "  ") //for junits\r
+                               }\r
+                       }else if(responseCode == 404){\r
+                               utils.log("DEBUG", "  Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)\r
+                               execution.setVariable("GENGS_SuccessIndicator", true)\r
+                               execution.setVariable("WorkflowResponse", "  ") //for junits\r
+                       }else{\r
+                               utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)\r
+                               exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
+                               throw new BpmnError("MSOWorkflowException")\r
+                       }\r
+               }catch(BpmnError b){\r
+                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
+                       throw b\r
+               }catch(Exception e){\r
+                       utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlById method!" + e, isDebugEnabled)\r
+                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlById")\r
+               }\r
+               utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlById Process*** ", isDebugEnabled)\r
+       }\r
+\r
+       /**\r
+        * This method obtains the Url to the provided service instance\r
+        * using the Service Instance Name.\r
+        *\r
+        * @param - execution\r
+        */\r
+       public void obtainServiceInstanceUrlByName(Execution execution){\r
+               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
+               execution.setVariable("prefix",Prefix)\r
+               utils.log("DEBUG", " *** STARTED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)\r
+               try {\r
+                       String serviceInstanceName = execution.getVariable("GENGS_serviceInstanceName")\r
+                       utils.log("DEBUG", " Querying Node for Service-Instance URL by using Service-Instance Name " + serviceInstanceName, isDebugEnabled)\r
+\r
+                       AaiUtil aaiUriUtil = new AaiUtil(this)\r
+                       String aai_uri = aaiUriUtil.getSearchNodesQueryEndpoint(execution)\r
+                       String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
+                       String path = "${aai_uri}?search-node-type=service-instance&filter=service-instance-name:EQUALS:${serviceInstanceName}"\r
+\r
+                       //String url = "${aai_endpoint}${path}"  host name needs to be removed from property\r
+                       String url = "${path}"\r
+                       execution.setVariable("GENGS_obtainSIUrlPath", url)\r
+\r
+                       utils.logAudit("GenericGetService AAI Endpoint: " + aai_endpoint)\r
+                       APIResponse response = aaiUriUtil.executeAAIGetCall(execution, url)\r
+                       int responseCode = response.getStatusCode()\r
+                       execution.setVariable("GENGS_obtainSIUrlResponseCode", responseCode)\r
+                       utils.log("DEBUG", "  GET Service Instance response code is: " + responseCode, isDebugEnabled)\r
+                       utils.logAudit("GenericGetService AAI Response Code: " + responseCode)\r
+                       \r
+                       String aaiResponse = response.getResponseBodyAsString()\r
+                       aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
+                       execution.setVariable("GENGS_obtainSIUrlResponse", aaiResponse)\r
+                       utils.logAudit("GenericGetService AAI Response: " + aaiResponse) \r
+                       //Process Response\r
+                       if(responseCode == 200){\r
+                               utils.log("DEBUG", "  Query for Service Instance Url Received a Good Response Code", isDebugEnabled)\r
+                               execution.setVariable("GENGS_SuccessIndicator", true)\r
+                               if(utils.nodeExists(aaiResponse, "result-data")){\r
+                                       utils.log("DEBUG", "Query for Service Instance Url Response Does Contain Data" , isDebugEnabled)\r
+                                       execution.setVariable("GENGS_FoundIndicator", true)\r
+                                       String resourceLink = utils.getNodeText1(aaiResponse, "resource-link")\r
+                                       execution.setVariable("GENGS_siResourceLink", resourceLink)\r
+                               }else{\r
+                                       utils.log("DEBUG", "Query for Service Instance Url Response Does NOT Contains Data" , isDebugEnabled)\r
+                                       execution.setVariable("WorkflowResponse", "  ") //for junits\r
+                               }\r
+                       }else if(responseCode == 404){\r
+                               utils.log("DEBUG", "  Query for Service Instance Received a Not Found (404) Response", isDebugEnabled)\r
+                               execution.setVariable("GENGS_SuccessIndicator", true)\r
+                               execution.setVariable("WorkflowResponse", "  ") //for junits\r
+                       }else{\r
+                               utils.log("DEBUG", "Query for Service Instance Received a BAD REST Response: \n" + aaiResponse, isDebugEnabled)\r
+                               exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
+                               throw new BpmnError("MSOWorkflowException")\r
+                       }\r
+               }catch(BpmnError b){\r
+                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
+                       throw b\r
+               }catch(Exception e){\r
+                       utils.log("ERROR", " Error encountered within GenericGetService ObtainServiceInstanceUrlByName method!" + e, isDebugEnabled)\r
+                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During ObtainServiceInstanceUrlByName")\r
+               }\r
+               utils.log("DEBUG", " *** COMPLETED GenericGetService ObtainServiceInstanceUrlByName Process*** ", isDebugEnabled)\r
+       }\r
+\r
+\r
+       /**\r
+        * This method executes a GET call to AAI to obtain the\r
+        * service-instance or service-subscription\r
+        *\r
+        * @param - execution\r
+        */\r
+       public void getServiceObject(Execution execution){\r
+               def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
+               execution.setVariable("prefix",Prefix)\r
+               utils.log("DEBUG", " *** STARTED GenericGetService GetServiceObject Process*** ", isDebugEnabled)\r
+               try {\r
+                       String type = execution.getVariable("GENGS_type")\r
+                       AaiUtil aaiUriUtil = new AaiUtil(this)\r
+                       String aai_endpoint = execution.getVariable("URN_aai_endpoint")\r
+                       String serviceEndpoint = ""\r
+                       \r
+                       utils.logAudit("GenericGetService getServiceObject AAI Endpoint: " + aai_endpoint)\r
+                       if(type.equalsIgnoreCase("service-instance")){\r
+                               String siResourceLink = execution.getVariable("GENGS_siResourceLink")\r
+                               if(isBlank(siResourceLink)){\r
+                                       String serviceInstanceId = execution.getVariable("GENGS_serviceInstanceId")\r
+                                       utils.log("DEBUG", " Incoming GENGS_serviceInstanceId is: " + serviceInstanceId, isDebugEnabled)\r
+                                       String serviceType = execution.getVariable("GENGS_serviceType")\r
+                                       utils.log("DEBUG", " Incoming GENGS_serviceType is: " + serviceType, isDebugEnabled)\r
+                                       String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")\r
+                                       utils.log("DEBUG", "Incoming Global Customer Id is: " + globalCustomerId, isDebugEnabled)\r
+\r
+                                       String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)\r
+                                       logDebug('AAI URI is: ' + aai_uri, isDebugEnabled)\r
+                                       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")\r
+                               }else{\r
+                                       utils.log("DEBUG", "Incoming Service Instance Resource Link is: " + siResourceLink, isDebugEnabled)\r
+                                       String[] split = siResourceLink.split("/aai/")\r
+                                       serviceEndpoint = "/aai/" + split[1]\r
+                               }\r
+                       }else if(type.equalsIgnoreCase("service-subscription")){\r
+                               String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)\r
+                               String globalCustomerId = execution.getVariable("GENGS_globalCustomerId")\r
+                               String serviceType = execution.getVariable("GENGS_serviceType")\r
+                               serviceEndpoint = "${aai_uri}/" + UriUtils.encode(globalCustomerId,"UTF-8") + "/service-subscriptions/service-subscription/" + UriUtils.encode(serviceType,"UTF-8")\r
+                       }\r
+\r
+                       String serviceUrl = "${aai_endpoint}" + serviceEndpoint\r
+\r
+                       execution.setVariable("GENGS_getServiceUrl", serviceUrl)\r
+                       utils.log("DEBUG", "GET Service AAI Path is: \n" + serviceUrl, isDebugEnabled)\r
+\r
+                       APIResponse response = aaiUriUtil.executeAAIGetCall(execution, serviceUrl)\r
+                       int responseCode = response.getStatusCode()\r
+                       execution.setVariable("GENGS_getServiceResponseCode", responseCode)\r
+                       utils.log("DEBUG", "  GET Service response code is: " + responseCode, isDebugEnabled)\r
+                       utils.logAudit("GenericGetService AAI Response Code: " + responseCode)\r
+                       \r
+                       String aaiResponse = response.getResponseBodyAsString()\r
+                       aaiResponse = StringEscapeUtils.unescapeXml(aaiResponse)\r
+                       execution.setVariable("GENGS_getServiceResponse", aaiResponse)\r
+                       utils.logAudit("GenericGetService AAI Response: " + aaiResponse)\r
+                       \r
+                       //Process Response\r
+                       if(responseCode == 200 || responseCode == 202){\r
+                               utils.log("DEBUG", "GET Service Received a Good Response Code", isDebugEnabled)\r
+                               if(utils.nodeExists(aaiResponse, "service-instance") || utils.nodeExists(aaiResponse, "service-subscription")){\r
+                                       utils.log("DEBUG", "GET Service Response Contains a service-instance" , isDebugEnabled)\r
+                                       execution.setVariable("GENGS_FoundIndicator", true)\r
+                                       execution.setVariable("GENGS_service", aaiResponse)\r
+                                       execution.setVariable("WorkflowResponse", aaiResponse)\r
+\r
+                               }else{\r
+                                       utils.log("DEBUG", "GET Service Response Does NOT Contain Data" , isDebugEnabled)\r
+                               }\r
+                       }else if(responseCode == 404){\r
+                               utils.log("DEBUG", "GET Service Received a Not Found (404) Response", isDebugEnabled)\r
+                               execution.setVariable("WorkflowResponse", "  ") //for junits\r
+                       }\r
+                       else{\r
+                               utils.log("DEBUG", "  GET Service Received a Bad Response: \n" + aaiResponse, isDebugEnabled)\r
+                               exceptionUtil.MapAAIExceptionToWorkflowExceptionGeneric(execution, aaiResponse, responseCode)\r
+                               throw new BpmnError("MSOWorkflowException")\r
+                       }\r
+               }catch(BpmnError b){\r
+                       utils.log("DEBUG", "Rethrowing MSOWorkflowException", isDebugEnabled)\r
+                       throw b\r
+               }catch(Exception e){\r
+                       utils.log("DEBUG", " Error encountered within GenericGetService GetServiceObject method!" + e, isDebugEnabled)\r
+                       exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured During GenericGetService")\r
+               }\r
+               utils.log("DEBUG", " *** COMPLETED GenericGetService GetServiceObject Process*** ", isDebugEnabled)\r
+       }\r
+\r
+}\r