Search in sources :

Example 1 with ServiceViewChangeEvent

use of io.github.microcks.event.ServiceViewChangeEvent in project microcks by microcks.

the class ServiceChangeEventPublisher method onApplicationEvent.

@Override
@Async
public void onApplicationEvent(ServiceChangeEvent event) {
    log.debug("Received a ServiceChangeEvent on " + event.getServiceId());
    ServiceView serviceView = null;
    if (event.getChangeType() != ChangeType.DELETED) {
        Service service = serviceRepository.findById(event.getServiceId()).orElse(null);
        if (service != null) {
            // Put messages into a map where key is operation name.
            Map<String, List<? extends Exchange>> messagesMap = new HashMap<>();
            for (Operation operation : service.getOperations()) {
                if (service.getType() == ServiceType.EVENT) {
                    // If an event, we should explicitly retrieve event messages.
                    List<UnidirectionalEvent> events = messageService.getEventByOperation(IdBuilder.buildOperationId(service, operation));
                    messagesMap.put(operation.getName(), events);
                } else {
                    // Otherwise we have traditional request / response pairs.
                    List<RequestResponsePair> pairs = messageService.getRequestResponseByOperation(IdBuilder.buildOperationId(service, operation));
                    messagesMap.put(operation.getName(), pairs);
                }
            }
            serviceView = new ServiceView(service, messagesMap);
        }
    }
    // Build and send a ServiceViewChangeEvent that wraps ServiceView.
    ServiceViewChangeEvent serviceViewChangeEvent = new ServiceViewChangeEvent(event.getServiceId(), serviceView, event.getChangeType(), System.currentTimeMillis());
    kafkaTemplate.send("microcks-services-updates", event.getServiceId(), serviceViewChangeEvent);
    log.debug("Processing of ServiceChangeEvent done !");
}
Also used : RequestResponsePair(io.github.microcks.domain.RequestResponsePair) ServiceView(io.github.microcks.domain.ServiceView) ServiceViewChangeEvent(io.github.microcks.event.ServiceViewChangeEvent) HashMap(java.util.HashMap) UnidirectionalEvent(io.github.microcks.domain.UnidirectionalEvent) Service(io.github.microcks.domain.Service) MessageService(io.github.microcks.service.MessageService) Operation(io.github.microcks.domain.Operation) Exchange(io.github.microcks.domain.Exchange) List(java.util.List) Async(org.springframework.scheduling.annotation.Async)

Example 2 with ServiceViewChangeEvent

use of io.github.microcks.event.ServiceViewChangeEvent in project microcks by microcks.

the class AsyncMockDefinitionUpdater method onServiceUpdate.

@Incoming("microcks-services-updates")
public void onServiceUpdate(ServiceViewChangeEvent serviceViewChangeEvent) {
    logger.info("Received a new change event [" + serviceViewChangeEvent.getChangeType() + "] for '" + serviceViewChangeEvent.getServiceId() + "', at " + serviceViewChangeEvent.getTimestamp());
    // Remove existing definitions or add/update existing for EVENT services.
    if (serviceViewChangeEvent.getChangeType().equals(ChangeType.DELETED)) {
        logger.info("Removing mock definitions for " + serviceViewChangeEvent.getServiceId());
        mockRepository.removeMockDefinitions(serviceViewChangeEvent.getServiceId());
        schemaRegistry.clearRegistryForService(serviceViewChangeEvent.getServiceId());
    } else {
        // Only deal with service of type EVENT...
        if (serviceViewChangeEvent.getServiceView() != null && serviceViewChangeEvent.getServiceView().getService().getType().equals(ServiceType.EVENT)) {
            // Browse and check operation regarding restricted frequencies and supported bindings.
            boolean scheduled = false;
            for (Operation operation : serviceViewChangeEvent.getServiceView().getService().getOperations()) {
                if (Arrays.asList(restrictedFrequencies).contains(operation.getDefaultDelay()) && operation.getBindings().keySet().stream().anyMatch(Arrays.asList(supportedBindings)::contains)) {
                    logger.info("Found '" + operation.getName() + "' as a candidate for async message mocking");
                    // Build an Async mock definition and store it into repository.
                    AsyncMockDefinition mockDefinition = new AsyncMockDefinition(serviceViewChangeEvent.getServiceView().getService(), operation, serviceViewChangeEvent.getServiceView().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());
                    scheduled = true;
                }
            }
            if (!scheduled) {
                logger.info("Ensure to un-schedule " + serviceViewChangeEvent.getServiceId() + " on this minion. Removing definitions.");
                mockRepository.removeMockDefinitions(serviceViewChangeEvent.getServiceId());
                schemaRegistry.clearRegistryForService(serviceViewChangeEvent.getServiceId());
            }
        }
    }
}
Also used : Inject(javax.inject.Inject) Arrays(java.util.Arrays) Operation(io.github.microcks.domain.Operation) ServiceType(io.github.microcks.domain.ServiceType) ChangeType(io.github.microcks.event.ChangeType) Logger(org.jboss.logging.Logger) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) UnidirectionalEvent(io.github.microcks.domain.UnidirectionalEvent) Collectors(java.util.stream.Collectors) ServiceViewChangeEvent(io.github.microcks.event.ServiceViewChangeEvent) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) UnidirectionalEvent(io.github.microcks.domain.UnidirectionalEvent) Operation(io.github.microcks.domain.Operation) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming)

Aggregations

Operation (io.github.microcks.domain.Operation)2 UnidirectionalEvent (io.github.microcks.domain.UnidirectionalEvent)2 ServiceViewChangeEvent (io.github.microcks.event.ServiceViewChangeEvent)2 Exchange (io.github.microcks.domain.Exchange)1 RequestResponsePair (io.github.microcks.domain.RequestResponsePair)1 Service (io.github.microcks.domain.Service)1 ServiceType (io.github.microcks.domain.ServiceType)1 ServiceView (io.github.microcks.domain.ServiceView)1 ChangeType (io.github.microcks.event.ChangeType)1 MessageService (io.github.microcks.service.MessageService)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 ApplicationScoped (javax.enterprise.context.ApplicationScoped)1 Inject (javax.inject.Inject)1 ConfigProperty (org.eclipse.microprofile.config.inject.ConfigProperty)1 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)1 Logger (org.jboss.logging.Logger)1 Async (org.springframework.scheduling.annotation.Async)1