use of io.github.microcks.domain.Binding in project microcks by microcks.
the class AsyncAPIImporter method extractOperations.
/**
* Extract the list of operations from Specification.
*/
private List<Operation> extractOperations() throws MockRepositoryImportException {
List<Operation> results = new ArrayList<>();
// Iterate on specification "channels" nodes.
Iterator<Entry<String, JsonNode>> channels = spec.path("channels").fields();
while (channels.hasNext()) {
Entry<String, JsonNode> channel = channels.next();
String channelName = channel.getKey();
// Iterate on specification path, "verbs" nodes.
Iterator<Entry<String, JsonNode>> verbs = channel.getValue().fields();
while (verbs.hasNext()) {
Entry<String, JsonNode> verb = verbs.next();
String verbName = verb.getKey();
// Only deal with real verbs for now.
if (VALID_VERBS.contains(verbName)) {
String operationName = verbName.toUpperCase() + " " + channelName.trim();
Operation operation = new Operation();
operation.setName(operationName);
operation.setMethod(verbName.toUpperCase());
// Complete operation properties if any.
if (verb.getValue().has(MetadataExtensions.MICROCKS_OPERATION_EXTENSION)) {
MetadataExtractor.completeOperationProperties(operation, verb.getValue().path(MetadataExtensions.MICROCKS_OPERATION_EXTENSION));
}
// Deal with dispatcher stuffs.
if (operation.getDispatcher() == null && channelHasParts(channelName)) {
operation.setDispatcher(DispatchStyles.URI_PARTS);
operation.setDispatcherRules(DispatchCriteriaHelper.extractPartsFromURIPattern(channelName));
} else {
operation.addResourcePath(channelName);
}
// We have to look also for bindings. First at the upper channel level.
if (channel.getValue().has("bindings")) {
Iterator<String> bindingNames = channel.getValue().path("bindings").fieldNames();
while (bindingNames.hasNext()) {
String bindingName = bindingNames.next();
JsonNode binding = channel.getValue().path("bindings").path(bindingName);
switch(bindingName) {
case "ws":
Binding b = retrieveOrInitOperationBinding(operation, BindingType.WS);
if (binding.has("method")) {
b.setMethod(binding.path("method").asText());
}
break;
case "amqp":
b = retrieveOrInitOperationBinding(operation, BindingType.AMQP);
if (binding.has("is")) {
String is = binding.path("is").asText();
if ("queue".equals(is)) {
b.setDestinationType("queue");
JsonNode queue = binding.get("queue");
b.setDestinationName(queue.get("name").asText());
} else if ("routingKey".equals(is)) {
JsonNode exchange = binding.get("exchange");
b.setDestinationType(exchange.get("type").asText());
}
}
break;
}
}
}
// Then look for bindings at the operation level.
if (verb.getValue().has("bindings")) {
Iterator<String> bindingNames = verb.getValue().path("bindings").fieldNames();
while (bindingNames.hasNext()) {
String bindingName = bindingNames.next();
JsonNode binding = verb.getValue().path("bindings").path(bindingName);
switch(bindingName) {
case "kafka":
break;
case "mqtt":
Binding b = retrieveOrInitOperationBinding(operation, BindingType.MQTT);
if (binding.has("qos")) {
b.setQoS(binding.path("qos").asText());
}
if (binding.has("retain")) {
b.setPersistent(binding.path("retain").asBoolean());
}
break;
case "amqp1":
b = retrieveOrInitOperationBinding(operation, BindingType.AMQP1);
if (binding.has("destinationName")) {
b.setDestinationName(binding.path("destinationName").asText());
}
if (binding.has("destinationType")) {
b.setDestinationType(binding.path("destinationType").asText());
}
break;
}
}
}
// Then look for bindings at the message level.
JsonNode messageBody = verb.getValue().path("message");
messageBody = followRefIfAny(messageBody);
if (messageBody.has("bindings")) {
Iterator<String> bindingNames = messageBody.path("bindings").fieldNames();
while (bindingNames.hasNext()) {
String bindingName = bindingNames.next();
JsonNode binding = messageBody.path("bindings").path(bindingName);
switch(bindingName) {
case "kafka":
Binding b = retrieveOrInitOperationBinding(operation, BindingType.KAFKA);
if (binding.has("key")) {
b.setKeyType(binding.path("key").path("type").asText());
}
break;
case "mqtt":
case "amqp1":
break;
}
}
}
results.add(operation);
}
}
}
return results;
}
use of io.github.microcks.domain.Binding 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;
}
}
}
}
}
use of io.github.microcks.domain.Binding in project microcks by microcks.
the class AsyncAPIImporter method retrieveOrInitOperationBinding.
/**
*/
private static Binding retrieveOrInitOperationBinding(Operation operation, BindingType type) {
Binding binding = null;
if (operation.getBindings() != null) {
binding = operation.getBindings().get(type);
}
if (binding == null) {
binding = new Binding(type);
operation.addBinding(type.toString(), binding);
}
return binding;
}
Aggregations