Search in sources :

Example 1 with CommandRouterClient

use of org.eclipse.hono.client.command.CommandRouterClient in project hono by eclipse.

the class AbstractProtocolAdapterApplication method setCollaborators.

/**
 * Sets collaborators required by all protocol adapters.
 *
 * @param adapter The adapter to set the collaborators on.
 * @throws NullPointerException if adapter is {@code null}
 * @throws IllegalStateException if no connection to the Command Router service has been configured.
 */
protected void setCollaborators(final AbstractProtocolAdapterBase<?> adapter) {
    Objects.requireNonNull(adapter);
    final DeviceRegistrationClient registrationClient = registrationClient();
    final MessagingClientProvider<TelemetrySender> telemetrySenderProvider = new MessagingClientProvider<>();
    final MessagingClientProvider<EventSender> eventSenderProvider = new MessagingClientProvider<>();
    final MessagingClientProvider<CommandResponseSender> commandResponseSenderProvider = new MessagingClientProvider<>();
    final KafkaClientMetricsSupport kafkaClientMetricsSupport = kafkaClientMetricsSupport(kafkaMetricsOptions);
    final var tenantClient = tenantClient();
    if (kafkaEventConfig.isConfigured()) {
        LOG.info("Kafka client configuration present, adding Kafka messaging clients");
        final KafkaProducerFactory<String, Buffer> factory = CachingKafkaProducerFactory.sharedFactory(vertx);
        factory.setMetricsSupport(kafkaClientMetricsSupport);
        telemetrySenderProvider.setClient(new KafkaBasedTelemetrySender(vertx, factory, kafkaTelemetryConfig, protocolAdapterProperties.isDefaultsEnabled(), tracer));
        eventSenderProvider.setClient(new KafkaBasedEventSender(vertx, factory, kafkaEventConfig, protocolAdapterProperties.isDefaultsEnabled(), tracer));
        commandResponseSenderProvider.setClient(new KafkaBasedCommandResponseSender(vertx, factory, kafkaCommandResponseConfig, tracer));
    }
    if (downstreamSenderConfig.isHostConfigured()) {
        telemetrySenderProvider.setClient(downstreamSender());
        eventSenderProvider.setClient(downstreamSender());
        commandResponseSenderProvider.setClient(new ProtonBasedCommandResponseSender(HonoConnection.newConnection(vertx, commandResponseSenderConfig(), tracer), messageSamplerFactory, protocolAdapterProperties.isJmsVendorPropsEnabled()));
    }
    final MessagingClientProviders messagingClientProviders = new MessagingClientProviders(telemetrySenderProvider, eventSenderProvider, commandResponseSenderProvider);
    if (commandRouterConfig.isHostConfigured()) {
        final CommandRouterClient commandRouterClient = commandRouterClient();
        adapter.setCommandRouterClient(commandRouterClient);
        final CommandRouterCommandConsumerFactory commandConsumerFactory = commandConsumerFactory(commandRouterClient);
        if (commandConsumerConfig.isHostConfigured()) {
            commandConsumerFactory.registerInternalCommandConsumer((id, handlers) -> new ProtonBasedInternalCommandConsumer(commandConsumerConnection(), id, handlers));
        }
        final CommandResponseSender kafkaCommandResponseSender = messagingClientProviders.getCommandResponseSenderProvider().getClient(MessagingType.kafka);
        if (kafkaCommandInternalConfig.isConfigured() && kafkaCommandConfig.isConfigured() && kafkaCommandResponseSender != null) {
            commandConsumerFactory.registerInternalCommandConsumer((id, handlers) -> new KafkaBasedInternalCommandConsumer(vertx, kafkaCommandInternalConfig, kafkaCommandConfig, tenantClient, kafkaCommandResponseSender, id, handlers, tracer).setMetricsSupport(kafkaClientMetricsSupport));
        }
        adapter.setCommandConsumerFactory(commandConsumerFactory);
    } else {
        throw new IllegalStateException("No Command Router connection configured");
    }
    adapter.setMessagingClientProviders(messagingClientProviders);
    Optional.ofNullable(connectionEventProducer()).ifPresent(adapter::setConnectionEventProducer);
    adapter.setCredentialsClient(credentialsClient());
    adapter.setHealthCheckServer(healthCheckServer);
    adapter.setRegistrationClient(registrationClient);
    adapter.setResourceLimitChecks(prometheusResourceLimitChecks(resourceLimitChecksConfig, tenantClient));
    adapter.setTenantClient(tenantClient);
    adapter.setTracer(tracer);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) KafkaBasedTelemetrySender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedTelemetrySender) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) ProtonBasedCommandRouterClient(org.eclipse.hono.client.command.amqp.ProtonBasedCommandRouterClient) KafkaBasedCommandResponseSender(org.eclipse.hono.client.command.kafka.KafkaBasedCommandResponseSender) ProtonBasedDeviceRegistrationClient(org.eclipse.hono.client.registry.amqp.ProtonBasedDeviceRegistrationClient) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) ProtonBasedCommandResponseSender(org.eclipse.hono.client.command.amqp.ProtonBasedCommandResponseSender) KafkaBasedEventSender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedEventSender) MicrometerKafkaClientMetricsSupport(org.eclipse.hono.client.kafka.metrics.MicrometerKafkaClientMetricsSupport) KafkaClientMetricsSupport(org.eclipse.hono.client.kafka.metrics.KafkaClientMetricsSupport) NoopKafkaClientMetricsSupport(org.eclipse.hono.client.kafka.metrics.NoopKafkaClientMetricsSupport) CommandRouterCommandConsumerFactory(org.eclipse.hono.client.command.CommandRouterCommandConsumerFactory) MessagingClientProviders(org.eclipse.hono.adapter.MessagingClientProviders) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) KafkaBasedInternalCommandConsumer(org.eclipse.hono.client.command.kafka.KafkaBasedInternalCommandConsumer) KafkaBasedCommandResponseSender(org.eclipse.hono.client.command.kafka.KafkaBasedCommandResponseSender) ProtonBasedCommandResponseSender(org.eclipse.hono.client.command.amqp.ProtonBasedCommandResponseSender) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) KafkaBasedEventSender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedEventSender) EventSender(org.eclipse.hono.client.telemetry.EventSender) ProtonBasedInternalCommandConsumer(org.eclipse.hono.client.command.amqp.ProtonBasedInternalCommandConsumer) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) KafkaBasedTelemetrySender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedTelemetrySender)

Example 2 with CommandRouterClient

use of org.eclipse.hono.client.command.CommandRouterClient in project hono by eclipse.

the class ProtocolAdapterMockSupport method createCommandRouterClientMock.

private CommandRouterClient createCommandRouterClientMock() {
    final CommandRouterClient client = mock(CommandRouterClient.class);
    when(client.start()).thenReturn(Future.succeededFuture());
    when(client.stop()).thenReturn(Future.succeededFuture());
    return client;
}
Also used : CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient)

Example 3 with CommandRouterClient

use of org.eclipse.hono.client.command.CommandRouterClient in project hono by eclipse.

the class AbstractProtocolAdapterBaseTest method testGetRegistrationAssertionSucceedsForExistingDevice.

/**
 * Verifies that the adapter successfully retrieves a registration assertion
 * for an existing device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetRegistrationAssertionSucceedsForExistingDevice(final VertxTestContext ctx) {
    // GIVEN an adapter connected to a registration service
    final RegistrationAssertion assertionResult = newRegistrationAssertionResult("device");
    assertionResult.setAuthorizedGateways(List.of("gw", "gw2"));
    when(registrationClient.assertRegistration(eq("tenant"), eq("device"), any(), any())).thenReturn(Future.succeededFuture(assertionResult));
    when(commandRouterClient.setLastKnownGatewayForDevice(anyString(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    // WHEN an assertion for the device is retrieved
    adapter.getRegistrationAssertion("tenant", "device", new Device("tenant", "gw"), mock(SpanContext.class)).onComplete(ctx.succeeding(result -> {
        ctx.verify(() -> {
            // THEN the result contains the registration assertion
            assertThat(result.getDeviceId()).isEqualTo("device");
            // and the last known gateway has been updated
            verify(commandRouterClient).setLastKnownGatewayForDevice(eq("tenant"), eq("device"), eq("gw"), any());
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TenantConstants(org.eclipse.hono.util.TenantConstants) Context(io.vertx.core.Context) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) MessagingType(org.eclipse.hono.util.MessagingType) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) EventSender(org.eclipse.hono.client.telemetry.EventSender) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) TenantClient(org.eclipse.hono.client.registry.TenantClient) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Commands(org.eclipse.hono.client.command.Commands) Constants(org.eclipse.hono.util.Constants) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) TelemetryExecutionContext(org.eclipse.hono.util.TelemetryExecutionContext) HttpUtils(org.eclipse.hono.service.http.HttpUtils) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MessagingClient(org.eclipse.hono.util.MessagingClient) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Mockito.verify(org.mockito.Mockito.verify) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) CommandConsumerFactory(org.eclipse.hono.client.command.CommandConsumerFactory) Mockito.never(org.mockito.Mockito.never) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Device(org.eclipse.hono.auth.Device) Test(org.junit.jupiter.api.Test)

Example 4 with CommandRouterClient

use of org.eclipse.hono.client.command.CommandRouterClient in project hono by eclipse.

the class AbstractProtocolAdapterBaseTest method setup.

/**
 * Sets up the fixture.
 */
@BeforeEach
public void setup() {
    tenantClient = mock(TenantClient.class);
    when(tenantClient.start()).thenReturn(Future.succeededFuture());
    registrationClient = mock(DeviceRegistrationClient.class);
    when(registrationClient.start()).thenReturn(Future.succeededFuture());
    credentialsClient = mock(CredentialsClient.class);
    when(credentialsClient.start()).thenReturn(Future.succeededFuture());
    amqpTelemetrySender = mockMessagingClient(TelemetrySender.class, MessagingType.amqp);
    when(amqpTelemetrySender.start()).thenReturn(Future.succeededFuture());
    kafkaTelemetrySender = mockMessagingClient(TelemetrySender.class, MessagingType.kafka);
    when(kafkaTelemetrySender.start()).thenReturn(Future.succeededFuture());
    amqpEventSender = mockMessagingClient(EventSender.class, MessagingType.amqp);
    when(amqpEventSender.start()).thenReturn(Future.succeededFuture());
    kafkaEventSender = mockMessagingClient(EventSender.class, MessagingType.kafka);
    when(kafkaEventSender.start()).thenReturn(Future.succeededFuture());
    commandConsumerFactory = mock(CommandConsumerFactory.class);
    when(commandConsumerFactory.start()).thenReturn(Future.succeededFuture());
    amqpCommandResponseSender = mockMessagingClient(CommandResponseSender.class, MessagingType.amqp);
    when(amqpCommandResponseSender.start()).thenReturn(Future.succeededFuture());
    kafkaCommandResponseSender = mockMessagingClient(CommandResponseSender.class, MessagingType.kafka);
    when(kafkaCommandResponseSender.start()).thenReturn(Future.succeededFuture());
    final var telemetrySenderProvider = new MessagingClientProvider<TelemetrySender>().setClient(amqpTelemetrySender).setClient(kafkaTelemetrySender);
    final var eventSenderProvider = new MessagingClientProvider<EventSender>().setClient(amqpEventSender).setClient(kafkaEventSender);
    final var commandResponseSenderProvider = new MessagingClientProvider<CommandResponseSender>().setClient(amqpCommandResponseSender).setClient(kafkaCommandResponseSender);
    messagingClientProviders = new MessagingClientProviders(telemetrySenderProvider, eventSenderProvider, commandResponseSenderProvider);
    commandRouterClient = mock(CommandRouterClient.class);
    when(commandRouterClient.start()).thenReturn(Future.succeededFuture());
    properties = new ProtocolAdapterProperties();
    adapter = newProtocolAdapter(properties, ADAPTER_NAME);
    setCollaborators(adapter);
    vertx = mock(Vertx.class);
    VertxMockSupport.runTimersImmediately(vertx);
    context = mock(Context.class);
    adapter.init(vertx, context);
}
Also used : Context(io.vertx.core.Context) VertxTestContext(io.vertx.junit5.VertxTestContext) TelemetryExecutionContext(org.eclipse.hono.util.TelemetryExecutionContext) SpanContext(io.opentracing.SpanContext) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) CommandConsumerFactory(org.eclipse.hono.client.command.CommandConsumerFactory) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) Vertx(io.vertx.core.Vertx) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) TenantClient(org.eclipse.hono.client.registry.TenantClient) EventSender(org.eclipse.hono.client.telemetry.EventSender) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CommandRouterClient (org.eclipse.hono.client.command.CommandRouterClient)4 CommandResponseSender (org.eclipse.hono.client.command.CommandResponseSender)3 DeviceRegistrationClient (org.eclipse.hono.client.registry.DeviceRegistrationClient)3 EventSender (org.eclipse.hono.client.telemetry.EventSender)3 TelemetrySender (org.eclipse.hono.client.telemetry.TelemetrySender)3 MessagingClientProvider (org.eclipse.hono.client.util.MessagingClientProvider)3 SpanContext (io.opentracing.SpanContext)2 Context (io.vertx.core.Context)2 Vertx (io.vertx.core.Vertx)2 Buffer (io.vertx.core.buffer.Buffer)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 CommandConsumerFactory (org.eclipse.hono.client.command.CommandConsumerFactory)2 CredentialsClient (org.eclipse.hono.client.registry.CredentialsClient)2 TenantClient (org.eclipse.hono.client.registry.TenantClient)2 ProtocolAdapterProperties (org.eclipse.hono.config.ProtocolAdapterProperties)2 TelemetryExecutionContext (org.eclipse.hono.util.TelemetryExecutionContext)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1