Search in sources :

Example 1 with AsyncMockDefinition

use of io.github.microcks.minion.async.AsyncMockDefinition in project microcks by microcks.

the class WebSocketProducerManager method onOpen.

@OnOpen
public void onOpen(Session session, @PathParam("service") String service, @PathParam("version") String version) {
    logger.infof("New WebSocket session opening on service {%s} - {%s}. Checking if mocked...", service, version);
    // If service or version were encoded with '+' instead of '%20', remove them.
    if (service.contains("+")) {
        service = service.replace('+', ' ');
    }
    if (version.contains("+")) {
        version = version.replace('+', ' ');
    }
    Set<AsyncMockDefinition> definitions = asyncMockRepository.getMockDefinitionsByServiceAndVersion(service, version);
    if (definitions != null && !definitions.isEmpty()) {
        sessionRegistry.putSession(session);
    } else {
        try {
            logger.infof("No mock available on '%s', closing the session", session.getRequestURI().toString());
            session.close(new CloseReason(CloseReason.CloseCodes.CANNOT_ACCEPT, "No mock available on " + session.getRequestURI()));
        } catch (Exception e) {
            logger.infof("Caught an exception while rejecting a WebSocket opening on unmanaged '%'", session.getRequestURI().toString());
        }
    }
}
Also used : CloseReason(javax.websocket.CloseReason) AsyncMockDefinition(io.github.microcks.minion.async.AsyncMockDefinition) OnOpen(javax.websocket.OnOpen)

Example 2 with AsyncMockDefinition

use of io.github.microcks.minion.async.AsyncMockDefinition in project microcks by microcks.

the class ProducerManager method produceAsyncMockMessagesAt.

/**
 * Produce all the async mock messages corresponding to specified frequency.
 * @param frequency The frequency to emit messages for
 */
public void produceAsyncMockMessagesAt(Long frequency) {
    logger.info("Producing async mock messages for frequency: " + frequency);
    Set<AsyncMockDefinition> mockDefinitions = mockRepository.getMockDefinitionsByFrequency(frequency);
    for (AsyncMockDefinition definition : mockDefinitions) {
        logger.debugf("Processing definition of service {%s}", definition.getOwnerService().getName() + ':' + definition.getOwnerService().getVersion());
        for (String binding : definition.getOperation().getBindings().keySet()) {
            // Ensure this minion supports this binding.
            if (Arrays.asList(supportedBindings).contains(binding)) {
                Binding bindingDef = definition.getOperation().getBindings().get(binding);
                switch(BindingType.valueOf(binding)) {
                    case KAFKA:
                        for (EventMessage eventMessage : definition.getEventMessages()) {
                            String topic = kafkaProducerManager.getTopicName(definition, eventMessage);
                            String key = String.valueOf(System.currentTimeMillis());
                            String message = renderEventMessageContent(eventMessage);
                            // Check it Avro binary is expected, we should convert to bytes.
                            if (Constants.AVRO_BINARY_CONTENT_TYPES.contains(eventMessage.getMediaType())) {
                                // Build the name of expected schema.
                                String schemaName = IdBuilder.buildResourceFullName(definition.getOwnerService(), definition.getOperation());
                                String schemaContent = schemaRegistry.getSchemaEntryContent(definition.getOwnerService(), schemaName);
                                try {
                                    if (Constants.REGISTRY_AVRO_ENCODING.equals(defaultAvroEncoding) && kafkaProducerManager.isRegistryEnabled()) {
                                        logger.debug("Using a registry and converting message to Avro record");
                                        GenericRecord avroRecord = AvroUtil.jsonToAvroRecord(message, schemaContent);
                                        kafkaProducerManager.publishMessage(topic, key, avroRecord, kafkaProducerManager.renderEventMessageHeaders(TemplateEngineFactory.getTemplateEngine(), eventMessage.getHeaders()));
                                    } else {
                                        logger.debug("Converting message to Avro bytes array");
                                        byte[] avroBinary = AvroUtil.jsonToAvro(message, schemaContent);
                                        kafkaProducerManager.publishMessage(topic, key, avroBinary, kafkaProducerManager.renderEventMessageHeaders(TemplateEngineFactory.getTemplateEngine(), eventMessage.getHeaders()));
                                    }
                                } catch (Exception e) {
                                    logger.errorf("Exception while converting {%s} to Avro using schema {%s}", message, schemaContent, e);
                                }
                            } else {
                                kafkaProducerManager.publishMessage(topic, key, message, kafkaProducerManager.renderEventMessageHeaders(TemplateEngineFactory.getTemplateEngine(), eventMessage.getHeaders()));
                            }
                        }
                        break;
                    case MQTT:
                        for (EventMessage eventMessage : definition.getEventMessages()) {
                            String topic = mqttProducerManager.getTopicName(definition, eventMessage);
                            String message = renderEventMessageContent(eventMessage);
                            mqttProducerManager.publishMessage(topic, message);
                        }
                        break;
                    case WS:
                        for (EventMessage eventMessage : definition.getEventMessages()) {
                            String channel = wsProducerManager.getRequestURI(definition, eventMessage);
                            String message = renderEventMessageContent(eventMessage);
                            wsProducerManager.publishMessage(channel, message, eventMessage.getHeaders());
                        }
                        break;
                    case AMQP:
                        for (EventMessage eventMessage : definition.getEventMessages()) {
                            String destinationName = amqpProducerManager.getDestinationName(definition, eventMessage);
                            String message = renderEventMessageContent(eventMessage);
                            amqpProducerManager.publishMessage(bindingDef.getDestinationType(), destinationName, message, amqpProducerManager.renderEventMessageHeaders(TemplateEngineFactory.getTemplateEngine(), eventMessage.getHeaders()));
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
Also used : Binding(io.github.microcks.domain.Binding) EventMessage(io.github.microcks.domain.EventMessage) GenericRecord(org.apache.avro.generic.GenericRecord) AsyncMockDefinition(io.github.microcks.minion.async.AsyncMockDefinition)

Aggregations

AsyncMockDefinition (io.github.microcks.minion.async.AsyncMockDefinition)2 Binding (io.github.microcks.domain.Binding)1 EventMessage (io.github.microcks.domain.EventMessage)1 CloseReason (javax.websocket.CloseReason)1 OnOpen (javax.websocket.OnOpen)1 GenericRecord (org.apache.avro.generic.GenericRecord)1