Search in sources :

Example 1 with ConnectorException

use of io.github.microcks.minion.async.client.ConnectorException in project microcks by microcks.

the class AsyncMinionApp method onStart.

/**
 * Application startup method.
 */
void onStart(@Observes StartupEvent ev) {
    // We need to retrieve Keycloak server from Microcks config.
    KeycloakConfig config = microcksAPIConnector.getKeycloakConfig();
    logger.infof("Microcks Keycloak server url {%s} and realm {%s}", config.getAuthServerUrl(), config.getRealm());
    String keycloakEndpoint = config.getAuthServerUrl() + "/realms/" + config.getRealm() + "/protocol/openid-connect/token";
    if (!keycloakAuthURL.isEmpty() && keycloakAuthURL.get().length() > 0) {
        logger.infof("Use locally defined Keycloak Auth URL: %s", keycloakAuthURL);
        keycloakEndpoint = keycloakAuthURL.get() + "/realms/" + config.getRealm() + "/protocol/openid-connect/token";
    }
    try {
        // First retrieve an authentication token before fetching async messages to publish.
        String oauthToken;
        if (config.isEnabled()) {
            // We've got a full Keycloak config, attempt an authent.
            oauthToken = keycloakConnector.connectAndGetOAuthToken(keycloakEndpoint);
            logger.info("Authentication to Keycloak server succeed!");
        } else {
            // No realm config, probably a dev mode - use a fake token.
            oauthToken = "<anonymous-admin-token>";
            logger.info("Keycloak protection is not enabled, using a fake token");
        }
        int page = 0;
        boolean fetchServices = true;
        while (fetchServices) {
            List<Service> services = microcksAPIConnector.listServices("Bearer " + oauthToken, page, SERVICES_FETCH_SIZE);
            for (Service service : services) {
                logger.debug("Found service " + service.getName() + " - " + service.getVersion());
                if (service.getType().equals(ServiceType.EVENT)) {
                    // Find the operations matching this minion constraints..
                    List<Operation> operations = service.getOperations().stream().filter(o -> Arrays.asList(restrictedFrequencies).contains(o.getDefaultDelay())).filter(o -> o.getBindings().keySet().stream().anyMatch(Arrays.asList(supportedBindings)::contains)).collect(Collectors.toList());
                    if (operations.size() > 0) {
                        logger.info("Found " + operations.size() + " candidate operations in " + service.getName() + " - " + service.getVersion());
                        ServiceView serviceView = microcksAPIConnector.getService("Bearer " + oauthToken, service.getId(), true);
                        for (Operation operation : operations) {
                            AsyncMockDefinition mockDefinition = new AsyncMockDefinition(serviceView.getService(), operation, serviceView.getMessagesMap().get(operation.getName()).stream().filter(e -> e instanceof UnidirectionalEvent).map(e -> ((UnidirectionalEvent) e).getEventMessage()).collect(Collectors.toList()));
                            mockRepository.storeMockDefinition(mockDefinition);
                            schemaRegistry.updateRegistryForService(mockDefinition.getOwnerService());
                        }
                    }
                }
            }
            if (services.size() < SERVICES_FETCH_SIZE) {
                fetchServices = false;
            }
            page++;
        }
        logger.info("Starting scheduling of all producer jobs...");
        producerScheduler.scheduleAllProducerJobs();
    } catch (ConnectorException ce) {
        logger.error("Cannot authenticate to Keycloak server and thus enable to call Microcks API" + "to get Async APIs to mocks...", ce);
        throw new RuntimeException("Unable to start the Minion due to connection exception");
    } catch (IOException ioe) {
        logger.error("IOException while communicating with Keycloak or Microcks API", ioe);
        throw new RuntimeException("Unable to start the Minion due to IO exception");
    }
}
Also used : KeycloakConnector(io.github.microcks.minion.async.client.KeycloakConnector) Arrays(java.util.Arrays) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) Logger(org.jboss.logging.Logger) ServiceView(io.github.microcks.domain.ServiceView) MicrocksAPIConnector(io.github.microcks.minion.async.client.MicrocksAPIConnector) IOException(java.io.IOException) UnidirectionalEvent(io.github.microcks.domain.UnidirectionalEvent) ConnectorException(io.github.microcks.minion.async.client.ConnectorException) Collectors(java.util.stream.Collectors) KeycloakConfig(io.github.microcks.minion.async.client.KeycloakConfig) Inject(javax.inject.Inject) Service(io.github.microcks.domain.Service) List(java.util.List) Operation(io.github.microcks.domain.Operation) ServiceType(io.github.microcks.domain.ServiceType) Observes(javax.enterprise.event.Observes) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StartupEvent(io.quarkus.runtime.StartupEvent) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) ServiceView(io.github.microcks.domain.ServiceView) UnidirectionalEvent(io.github.microcks.domain.UnidirectionalEvent) Service(io.github.microcks.domain.Service) Operation(io.github.microcks.domain.Operation) IOException(java.io.IOException) ConnectorException(io.github.microcks.minion.async.client.ConnectorException) KeycloakConfig(io.github.microcks.minion.async.client.KeycloakConfig)

Aggregations

Operation (io.github.microcks.domain.Operation)1 Service (io.github.microcks.domain.Service)1 ServiceType (io.github.microcks.domain.ServiceType)1 ServiceView (io.github.microcks.domain.ServiceView)1 UnidirectionalEvent (io.github.microcks.domain.UnidirectionalEvent)1 ConnectorException (io.github.microcks.minion.async.client.ConnectorException)1 KeycloakConfig (io.github.microcks.minion.async.client.KeycloakConfig)1 KeycloakConnector (io.github.microcks.minion.async.client.KeycloakConnector)1 MicrocksAPIConnector (io.github.microcks.minion.async.client.MicrocksAPIConnector)1 StartupEvent (io.quarkus.runtime.StartupEvent)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Observes (javax.enterprise.event.Observes)1 Inject (javax.inject.Inject)1 ConfigProperty (org.eclipse.microprofile.config.inject.ConfigProperty)1 RestClient (org.eclipse.microprofile.rest.client.inject.RestClient)1