Search in sources :

Example 6 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedCommandResponseSender method sendCommandResponse.

@Override
public Future<Void> sendCommandResponse(final TenantObject tenant, final RegistrationAssertion device, final CommandResponse response, final SpanContext context) {
    Objects.requireNonNull(tenant);
    Objects.requireNonNull(device);
    Objects.requireNonNull(response);
    if (log.isTraceEnabled()) {
        log.trace("publish command response [{}]", response);
    }
    final String topic = new HonoTopic(HonoTopic.Type.COMMAND_RESPONSE, response.getTenantId()).toString();
    final Span span = startChildSpan("forward Command response", topic, response.getTenantId(), response.getDeviceId(), context);
    if (response.getMessagingType() != getMessagingType()) {
        span.log(String.format("using messaging type %s instead of type %s used for the original command", getMessagingType(), response.getMessagingType()));
    }
    return sendAndWaitForOutcome(topic, response.getTenantId(), response.getDeviceId(), response.getPayload(), getHeaders(response, tenant, device), span).onComplete(ar -> span.finish());
}
Also used : HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Span(io.opentracing.Span)

Example 7 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaApplicationClientImplTest method createConsumer.

private Future<MessageConsumer> createConsumer(final String tenantId, final Type type, final Handler<DownstreamMessage<KafkaMessageContext>> msgHandler, final Handler<Throwable> closeHandler) {
    final String topic = new HonoTopic(type, tenantId).toString();
    final TopicPartition topicPartition = new TopicPartition(topic, 0);
    mockConsumer.updateBeginningOffsets(Map.of(topicPartition, ((long) 0)));
    mockConsumer.updatePartitions(topicPartition, KafkaMockConsumer.DEFAULT_NODE);
    mockConsumer.setRebalancePartitionAssignmentAfterSubscribe(List.of(topicPartition));
    switch(type) {
        case TELEMETRY:
            return client.createTelemetryConsumer(tenantId, msgHandler, closeHandler);
        case EVENT:
            return client.createEventConsumer(tenantId, msgHandler, closeHandler);
        default:
            return client.createCommandResponseConsumer(tenantId, null, msgHandler, closeHandler);
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic)

Example 8 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedEventSenderTest method testSendEventCreatesCorrectRecord.

/**
 * Verifies that the Kafka record is created as expected.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testSendEventCreatesCorrectRecord(final VertxTestContext ctx) {
    // GIVEN a sender
    final String contentType = "text/plain";
    final String payload = "the-payload";
    final Map<String, Object> properties = Map.of("foo", "bar", MessageHelper.SYS_HEADER_PROPERTY_TTL, 5);
    final var span = TracingMockSupport.mockSpan();
    final var tracer = TracingMockSupport.mockTracer(span);
    final var mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final var factory = newProducerFactory(mockProducer);
    final var sender = new KafkaBasedEventSender(vertxMock, factory, kafkaProducerConfig, true, tracer);
    // WHEN sending a message
    sender.sendEvent(tenant, device, contentType, Buffer.buffer(payload), properties, null).onComplete(ctx.succeeding(t -> {
        ctx.verify(() -> {
            // THEN the producer record is created from the given values...
            final var producerRecord = mockProducer.history().get(0);
            assertThat(producerRecord.key()).isEqualTo(device.getDeviceId());
            assertThat(producerRecord.topic()).isEqualTo(new HonoTopic(HonoTopic.Type.EVENT, tenant.getTenantId()).toString());
            assertThat(producerRecord.value().toString()).isEqualTo(payload);
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), "foo", "bar");
            KafkaClientUnitTestHelper.assertUniqueHeaderWithExpectedValue(producerRecord.headers(), MessageHelper.SYS_HEADER_PROPERTY_TTL, 5000L);
            // ...AND contains the standard headers
            KafkaClientUnitTestHelper.assertStandardHeaders(producerRecord, device.getDeviceId(), contentType, QoS.AT_LEAST_ONCE.ordinal());
            verify(span).finish();
        });
        ctx.completeNow();
    }));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) KafkaClientUnitTestHelper(org.eclipse.hono.kafka.test.KafkaClientUnitTestHelper) Tracer(io.opentracing.Tracer) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Mockito.verify(org.mockito.Mockito.verify) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Buffer(io.vertx.core.buffer.Buffer) QoS(org.eclipse.hono.util.QoS) MockProducer(org.apache.kafka.clients.producer.MockProducer) Mockito.mock(org.mockito.Mockito.mock) TenantObject(org.eclipse.hono.util.TenantObject) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Test(org.junit.jupiter.api.Test)

Example 9 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class AbstractKafkaBasedDownstreamSender method removeTenantTopicBasedProducerMetrics.

private void removeTenantTopicBasedProducerMetrics(final KafkaProducer<String, Buffer> producer, final String tenantId) {
    final HonoTopic topic = new HonoTopic(getTopicType(), tenantId);
    KafkaProducerHelper.removeTopicMetrics(producer, Stream.of(topic.toString()));
}
Also used : HonoTopic(org.eclipse.hono.client.kafka.HonoTopic)

Example 10 with HonoTopic

use of org.eclipse.hono.client.kafka.HonoTopic in project hono by eclipse.

the class KafkaBasedTelemetrySender method sendTelemetry.

@Override
public Future<Void> sendTelemetry(final TenantObject tenant, final RegistrationAssertion device, final QoS qos, final String contentType, final Buffer payload, final Map<String, Object> properties, final SpanContext context) {
    Objects.requireNonNull(tenant);
    Objects.requireNonNull(device);
    Objects.requireNonNull(qos);
    if (log.isTraceEnabled()) {
        log.trace("send telemetry data [tenantId: {}, deviceId: {}, qos: {}, contentType: {}, properties: {}]", tenant.getTenantId(), device.getDeviceId(), qos, contentType, properties);
    }
    final HonoTopic topic = new HonoTopic(HonoTopic.Type.TELEMETRY, tenant.getTenantId());
    final Map<String, Object> propsWithDefaults = addDefaults(topic.getType().endpoint, tenant, device, qos, contentType, payload, properties);
    final String topicName = topic.toString();
    final Span currentSpan = startSpan("forward Telemetry", topicName, tenant.getTenantId(), device.getDeviceId(), qos == QoS.AT_MOST_ONCE ? References.FOLLOWS_FROM : References.CHILD_OF, context);
    final var outcome = sendAndWaitForOutcome(topic.toString(), tenant.getTenantId(), device.getDeviceId(), payload, propsWithDefaults, currentSpan).onComplete(ar -> currentSpan.finish());
    if (qos == QoS.AT_MOST_ONCE) {
        return Future.succeededFuture();
    } else {
        return outcome;
    }
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) Span(io.opentracing.Span)

Aggregations

HonoTopic (org.eclipse.hono.client.kafka.HonoTopic)35 Buffer (io.vertx.core.buffer.Buffer)23 Test (org.junit.jupiter.api.Test)17 KafkaHeader (io.vertx.kafka.client.producer.KafkaHeader)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Truth.assertThat (com.google.common.truth.Truth.assertThat)8 Span (io.opentracing.Span)8 VertxTestContext (io.vertx.junit5.VertxTestContext)8 Vertx (io.vertx.core.Vertx)7 VertxExtension (io.vertx.junit5.VertxExtension)7 Future (io.vertx.core.Future)6 Promise (io.vertx.core.Promise)6 DownstreamMessage (org.eclipse.hono.application.client.DownstreamMessage)6 CachingKafkaProducerFactory (org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory)6 MessagingKafkaProducerConfigProperties (org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties)6 MessageHelper (org.eclipse.hono.util.MessageHelper)6 Tracer (io.opentracing.Tracer)5 NoopSpan (io.opentracing.noop.NoopSpan)5 HttpURLConnection (java.net.HttpURLConnection)5