Search in sources :

Example 6 with TenantClient

use of org.eclipse.hono.client.registry.TenantClient in project hono by eclipse.

the class AbstractProtocolAdapterApplication method prometheusResourceLimitChecks.

/**
 * Creates resource limit checks based on data retrieved from a Prometheus server
 * via its HTTP API.
 *
 * @param config The configuration properties.
 * @param tenantClient The client to use for retrieving tenant configuration data.
 * @throws NullPointerException if any of the parameters are {@code null}-
 * @return The checks.
 */
protected ResourceLimitChecks prometheusResourceLimitChecks(final PrometheusBasedResourceLimitChecksConfig config, final TenantClient tenantClient) {
    Objects.requireNonNull(config);
    Objects.requireNonNull(tenantClient);
    if (config.isHostConfigured()) {
        final WebClientOptions webClientOptions = new WebClientOptions();
        webClientOptions.setConnectTimeout(config.getConnectTimeout());
        webClientOptions.setDefaultHost(config.getHost());
        webClientOptions.setDefaultPort(config.getPort());
        webClientOptions.setTrustOptions(config.getTrustOptions());
        webClientOptions.setKeyCertOptions(config.getKeyCertOptions());
        webClientOptions.setSsl(config.isTlsEnabled());
        final var webClient = WebClient.create(vertx, webClientOptions);
        final var cacheTimeout = Duration.ofSeconds(config.getCacheTimeout());
        final Caffeine<Object, Object> builder = Caffeine.newBuilder().executor(Executors.newSingleThreadExecutor(r -> {
            final var t = new Thread(r);
            t.setDaemon(true);
            return t;
        })).initialCapacity(config.getCacheMinSize()).maximumSize(config.getCacheMaxSize()).expireAfterWrite(cacheTimeout).refreshAfterWrite(cacheTimeout.dividedBy(2));
        return new PrometheusBasedResourceLimitChecks(builder.buildAsync(new ConnectedDevicesAsyncCacheLoader(webClient, config, tracer)), builder.buildAsync(new ConnectionDurationAsyncCacheLoader(webClient, config, tracer)), builder.buildAsync(new DataVolumeAsyncCacheLoader(webClient, config, tracer)), tenantClient, tracer);
    } else {
        return new NoopResourceLimitChecks();
    }
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) MessagingKafkaConsumerConfigProperties(org.eclipse.hono.client.kafka.consumer.MessagingKafkaConsumerConfigProperties) MessagingKafkaProducerConfigProperties(org.eclipse.hono.client.kafka.producer.MessagingKafkaProducerConfigProperties) ProtonBasedDeviceRegistrationClient(org.eclipse.hono.client.registry.amqp.ProtonBasedDeviceRegistrationClient) KafkaBasedEventSender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedEventSender) KafkaBasedCommandResponseSender(org.eclipse.hono.client.command.kafka.KafkaBasedCommandResponseSender) LoggerFactory(org.slf4j.LoggerFactory) LoggingConnectionEventProducer(org.eclipse.hono.adapter.monitoring.LoggingConnectionEventProducer) CommandRouterCommandConsumerFactory(org.eclipse.hono.client.command.CommandRouterCommandConsumerFactory) PrometheusBasedResourceLimitChecksConfig(org.eclipse.hono.adapter.resourcelimits.PrometheusBasedResourceLimitChecksConfig) KafkaAdminClientOptions(org.eclipse.hono.client.kafka.KafkaAdminClientOptions) NoopResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.NoopResourceLimitChecks) AbstractServiceApplication(org.eclipse.hono.service.quarkus.AbstractServiceApplication) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) ProtonBasedInternalCommandConsumer(org.eclipse.hono.client.command.amqp.ProtonBasedInternalCommandConsumer) MessagingType(org.eclipse.hono.util.MessagingType) Duration(java.time.Duration) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) RegistrationResult(org.eclipse.hono.util.RegistrationResult) DataVolumeAsyncCacheLoader(org.eclipse.hono.adapter.resourcelimits.DataVolumeAsyncCacheLoader) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonBasedCredentialsClient(org.eclipse.hono.client.registry.amqp.ProtonBasedCredentialsClient) KafkaConsumerOptions(org.eclipse.hono.client.kafka.consumer.KafkaConsumerOptions) NotificationConstants(org.eclipse.hono.notification.NotificationConstants) ConnectionEventProducerConfig(org.eclipse.hono.adapter.monitoring.ConnectionEventProducerConfig) MessagingClientProviders(org.eclipse.hono.adapter.MessagingClientProviders) EventSender(org.eclipse.hono.client.telemetry.EventSender) CredentialsResult(org.eclipse.hono.util.CredentialsResult) ProtonBasedNotificationReceiver(org.eclipse.hono.client.notification.amqp.ProtonBasedNotificationReceiver) CachingKafkaProducerFactory(org.eclipse.hono.client.kafka.producer.CachingKafkaProducerFactory) TenantClient(org.eclipse.hono.client.registry.TenantClient) Caches(org.eclipse.hono.service.cache.Caches) Future(io.vertx.core.Future) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) ConfigMapping(io.smallrye.config.ConfigMapping) List(java.util.List) KafkaProducerFactory(org.eclipse.hono.client.kafka.producer.KafkaProducerFactory) ConnectionDurationAsyncCacheLoader(org.eclipse.hono.adapter.resourcelimits.ConnectionDurationAsyncCacheLoader) Buffer(io.vertx.core.buffer.Buffer) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Optional(java.util.Optional) MicrometerKafkaClientMetricsSupport(org.eclipse.hono.client.kafka.metrics.MicrometerKafkaClientMetricsSupport) ConnectionEventProducerOptions(org.eclipse.hono.adapter.monitoring.ConnectionEventProducerOptions) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) KafkaBasedTelemetrySender(org.eclipse.hono.client.telemetry.kafka.KafkaBasedTelemetrySender) WebClient(io.vertx.ext.web.client.WebClient) ProtonBasedCommandResponseSender(org.eclipse.hono.client.command.amqp.ProtonBasedCommandResponseSender) ProtonBasedTenantClient(org.eclipse.hono.client.registry.amqp.ProtonBasedTenantClient) CompletableFuture(java.util.concurrent.CompletableFuture) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) ClientOptions(org.eclipse.hono.config.quarkus.ClientOptions) Cache(com.github.benmanes.caffeine.cache.Cache) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) Inject(javax.inject.Inject) CompositeFuture(io.vertx.core.CompositeFuture) CommonKafkaClientOptions(org.eclipse.hono.client.kafka.CommonKafkaClientOptions) KafkaClientMetricsSupport(org.eclipse.hono.client.kafka.metrics.KafkaClientMetricsSupport) KafkaBasedInternalCommandConsumer(org.eclipse.hono.client.command.kafka.KafkaBasedInternalCommandConsumer) KafkaMetricsOptions(org.eclipse.hono.client.kafka.metrics.KafkaMetricsOptions) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) PrometheusBasedResourceLimitCheckOptions(org.eclipse.hono.adapter.resourcelimits.PrometheusBasedResourceLimitCheckOptions) KafkaAdminClientConfigProperties(org.eclipse.hono.client.kafka.KafkaAdminClientConfigProperties) PrometheusBasedResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.PrometheusBasedResourceLimitChecks) WrappedLifecycleComponentVerticle(org.eclipse.hono.util.WrappedLifecycleComponentVerticle) HonoConnection(org.eclipse.hono.client.HonoConnection) ProtonBasedCommandRouterClient(org.eclipse.hono.client.command.amqp.ProtonBasedCommandRouterClient) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) NotificationReceiver(org.eclipse.hono.notification.NotificationReceiver) NotificationKafkaConsumerConfigProperties(org.eclipse.hono.client.notification.kafka.NotificationKafkaConsumerConfigProperties) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) TenantResult(org.eclipse.hono.util.TenantResult) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) AbstractProtocolAdapterBase(org.eclipse.hono.adapter.AbstractProtocolAdapterBase) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) KafkaBasedNotificationReceiver(org.eclipse.hono.client.notification.kafka.KafkaBasedNotificationReceiver) KafkaProducerOptions(org.eclipse.hono.client.kafka.producer.KafkaProducerOptions) RequestResponseClientOptions(org.eclipse.hono.client.RequestResponseClientOptions) TenantObject(org.eclipse.hono.util.TenantObject) NoopKafkaClientMetricsSupport(org.eclipse.hono.client.kafka.metrics.NoopKafkaClientMetricsSupport) DeploymentOptions(io.vertx.core.DeploymentOptions) ProtonBasedDownstreamSender(org.eclipse.hono.client.telemetry.amqp.ProtonBasedDownstreamSender) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) ConnectedDevicesAsyncCacheLoader(org.eclipse.hono.adapter.resourcelimits.ConnectedDevicesAsyncCacheLoader) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) HonoEventConnectionEventProducer(org.eclipse.hono.adapter.monitoring.HonoEventConnectionEventProducer) CredentialsObject(org.eclipse.hono.util.CredentialsObject) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) PrometheusBasedResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.PrometheusBasedResourceLimitChecks) ConnectionDurationAsyncCacheLoader(org.eclipse.hono.adapter.resourcelimits.ConnectionDurationAsyncCacheLoader) ConnectedDevicesAsyncCacheLoader(org.eclipse.hono.adapter.resourcelimits.ConnectedDevicesAsyncCacheLoader) DataVolumeAsyncCacheLoader(org.eclipse.hono.adapter.resourcelimits.DataVolumeAsyncCacheLoader) TenantObject(org.eclipse.hono.util.TenantObject) CredentialsObject(org.eclipse.hono.util.CredentialsObject) NoopResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.NoopResourceLimitChecks)

Example 7 with TenantClient

use of org.eclipse.hono.client.registry.TenantClient in project hono by eclipse.

the class KafkaBasedInternalCommandConsumerTest method setUp.

/**
 * Sets up fixture.
 */
@BeforeEach
public void setUp() {
    final Admin kafkaAdminClient = mock(Admin.class);
    @SuppressWarnings("unchecked") final KafkaConsumer<String, Buffer> kafkaConsumer = mock(KafkaConsumer.class);
    final String adapterInstanceId = "adapterInstanceId";
    final Span span = TracingMockSupport.mockSpan();
    final Tracer tracer = TracingMockSupport.mockTracer(span);
    context = VertxMockSupport.mockContext(mock(Vertx.class));
    commandHandlers = new CommandHandlers();
    tenantClient = mock(TenantClient.class);
    doAnswer(invocation -> {
        final String tenantId = invocation.getArgument(0);
        return Future.succeededFuture(TenantObject.from(tenantId));
    }).when(tenantClient).get(anyString(), any());
    commandResponseSender = mock(CommandResponseSender.class);
    when(commandResponseSender.sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), any())).thenReturn(Future.succeededFuture());
    internalCommandConsumer = new KafkaBasedInternalCommandConsumer(context, kafkaAdminClient, kafkaConsumer, "testClientId", tenantClient, commandResponseSender, adapterInstanceId, commandHandlers, tracer);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) CommandHandlers(org.eclipse.hono.client.command.CommandHandlers) Tracer(io.opentracing.Tracer) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CommandResponse(org.eclipse.hono.client.command.CommandResponse) Admin(org.apache.kafka.clients.admin.Admin) Span(io.opentracing.Span) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) TenantClient(org.eclipse.hono.client.registry.TenantClient) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 8 with TenantClient

use of org.eclipse.hono.client.registry.TenantClient 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)

Example 9 with TenantClient

use of org.eclipse.hono.client.registry.TenantClient in project hono by eclipse.

the class PrometheusBasedResourceLimitChecksTest method setup.

/**
 * Sets up the fixture.
 */
@SuppressWarnings("unchecked")
@BeforeEach
public void setup() {
    connectionCountCache = mock(AsyncLoadingCache.class);
    when(connectionCountCache.get(any(LimitedResourceKey.class))).thenAnswer(i -> {
        final var count = new CompletableFuture<Long>();
        count.complete(10L);
        return count;
    });
    connectionDurationCache = mock(AsyncLoadingCache.class);
    when(connectionDurationCache.get(any(LimitedResourceKey.class))).then(invocation -> {
        final var count = new CompletableFuture<Long>();
        count.complete(10L);
        return count;
    });
    dataVolumeCache = mock(AsyncLoadingCache.class);
    when(dataVolumeCache.get(any(LimitedResourceKey.class))).then(invocation -> {
        final var count = new CompletableFuture<Long>();
        count.complete(10L);
        return count;
    });
    span = TracingMockSupport.mockSpan();
    tracer = TracingMockSupport.mockTracer(span);
    tenantClient = mock(TenantClient.class);
    when(tenantClient.get(anyString(), any())).thenAnswer(i -> {
        final String tenantId = i.getArgument(0);
        final var result = TenantObject.from(tenantId);
        return Future.succeededFuture(result);
    });
    limitChecksImpl = new PrometheusBasedResourceLimitChecks(connectionCountCache, connectionDurationCache, dataVolumeCache, tenantClient, tracer);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AsyncLoadingCache(com.github.benmanes.caffeine.cache.AsyncLoadingCache) TenantClient(org.eclipse.hono.client.registry.TenantClient) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with TenantClient

use of org.eclipse.hono.client.registry.TenantClient in project hono by eclipse.

the class KafkaBasedMappingAndDelegatingCommandHandlerTest method setUp.

/**
 * Sets up fixture.
 */
@BeforeEach
public void setUp() {
    tenantId = UUID.randomUUID().toString();
    deviceId = UUID.randomUUID().toString();
    adapterInstanceId = UUID.randomUUID().toString();
    final TenantClient tenantClient = mock(TenantClient.class);
    when(tenantClient.get(eq(tenantId), any())).thenReturn(Future.succeededFuture(TenantObject.from(tenantId)));
    commandTargetMapper = mock(CommandTargetMapper.class);
    when(commandTargetMapper.getTargetGatewayAndAdapterInstance(eq(tenantId), eq(deviceId), any())).thenReturn(Future.succeededFuture(createTargetAdapterInstanceJson(deviceId, adapterInstanceId)));
    internalCommandSender = mock(KafkaBasedInternalCommandSender.class);
    when(internalCommandSender.sendCommand(any(CommandContext.class), anyString())).thenReturn(Future.succeededFuture());
    final KafkaBasedCommandResponseSender kafkaBasedCommandResponseSender = mock(KafkaBasedCommandResponseSender.class);
    vertx = mock(Vertx.class);
    final Context context = VertxMockSupport.mockContext(vertx);
    final KafkaCommandProcessingQueue commandQueue = new KafkaCommandProcessingQueue(context);
    final CommandRouterMetrics metrics = mock(CommandRouterMetrics.class);
    when(metrics.startTimer()).thenReturn(Timer.start());
    final Tracer tracer = TracingMockSupport.mockTracer(TracingMockSupport.mockSpan());
    cmdHandler = new KafkaBasedMappingAndDelegatingCommandHandler(vertx, tenantClient, commandQueue, commandTargetMapper, internalCommandSender, kafkaBasedCommandResponseSender, metrics, tracer);
}
Also used : Context(io.vertx.core.Context) CommandContext(org.eclipse.hono.client.command.CommandContext) VertxTestContext(io.vertx.junit5.VertxTestContext) KafkaBasedCommandContext(org.eclipse.hono.client.command.kafka.KafkaBasedCommandContext) CommandRouterMetrics(org.eclipse.hono.commandrouter.CommandRouterMetrics) CommandTargetMapper(org.eclipse.hono.commandrouter.CommandTargetMapper) KafkaBasedCommandResponseSender(org.eclipse.hono.client.command.kafka.KafkaBasedCommandResponseSender) CommandContext(org.eclipse.hono.client.command.CommandContext) KafkaBasedCommandContext(org.eclipse.hono.client.command.kafka.KafkaBasedCommandContext) Tracer(io.opentracing.Tracer) TenantClient(org.eclipse.hono.client.registry.TenantClient) Vertx(io.vertx.core.Vertx) KafkaBasedInternalCommandSender(org.eclipse.hono.client.command.kafka.KafkaBasedInternalCommandSender) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TenantClient (org.eclipse.hono.client.registry.TenantClient)10 Tracer (io.opentracing.Tracer)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Span (io.opentracing.Span)3 SpanContext (io.opentracing.SpanContext)3 Context (io.vertx.core.Context)3 Vertx (io.vertx.core.Vertx)3 Buffer (io.vertx.core.buffer.Buffer)3 CommandResponseSender (org.eclipse.hono.client.command.CommandResponseSender)3 DeviceRegistrationClient (org.eclipse.hono.client.registry.DeviceRegistrationClient)3 CommandTargetMapper (org.eclipse.hono.commandrouter.CommandTargetMapper)3 TenantObject (org.eclipse.hono.util.TenantObject)3 Future (io.vertx.core.Future)2 JsonObject (io.vertx.core.json.JsonObject)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 List (java.util.List)2 Objects (java.util.Objects)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CommandRouterClient (org.eclipse.hono.client.command.CommandRouterClient)2 KafkaBasedCommandResponseSender (org.eclipse.hono.client.command.kafka.KafkaBasedCommandResponseSender)2