class WorkflowAction {

    protected String validateServiceResourceIdInAAI(String generatedResourceId, String instanceName,
            RequestDetails reqDetails) throws DuplicateNameException {
        String globalCustomerId = reqDetails.getSubscriberInfo().getGlobalSubscriberId();
        String serviceType = reqDetails.getRequestParameters().getSubscriptionServiceType();
        if (instanceName != null) {
            Optional<ServiceInstance> serviceInstanceAAI =
                    bbInputSetupUtils.getAAIServiceInstanceByName(globalCustomerId, serviceType, instanceName);
            if (serviceInstanceAAI.isPresent()) {
                if (serviceInstanceAAI.get().getModelVersionId()
                        .equalsIgnoreCase(reqDetails.getModelInfo().getModelVersionId())) {
                    return serviceInstanceAAI.get().getServiceInstanceId();
                } else {
                    throw new DuplicateNameException(SERVICE_INSTANCE, String.format(NAME_EXISTS_WITH_DIFF_VERSION_ID,
                            instanceName, reqDetails.getModelInfo().getModelVersionId()));
                }
            } else {
                ServiceInstances aaiServiceInstances =
                        bbInputSetupUtils.getAAIServiceInstancesGloballyByName(instanceName);
                if (aaiServiceInstances != null) {
                    if (aaiServiceInstances.getServiceInstance() != null
                            && !aaiServiceInstances.getServiceInstance().isEmpty()) {
                        if (aaiServiceInstances.getServiceInstance().size() > 1) {
                            throw new DuplicateNameException(SERVICE_INSTANCE,
                                    String.format(NAME_EXISTS_MULTIPLE, instanceName));
                        } else {
                            ServiceInstance si = aaiServiceInstances.getServiceInstance().stream().findFirst().get();
                            Map<String, String> keys =
                                    bbInputSetupUtils.getURIKeysFromServiceInstance(si.getServiceInstanceId());

                            throw new DuplicateNameException(SERVICE_INSTANCE, String.format(
                                    NAME_EXISTS_WITH_DIFF_COMBINATION, instanceName,
                                    keys.get(AAIFluentTypeBuilder.Types.CUSTOMER.getUriParams().globalCustomerId),
                                    keys.get(
                                            AAIFluentTypeBuilder.Types.SERVICE_SUBSCRIPTION.getUriParams().serviceType),
                                    si.getModelVersionId()));
                        }
                    }
                }
            }
        }
        return generatedResourceId;
    }

}