Search in sources :

Example 21 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class ProtonBasedDownstreamSenderTest method testSenderClientCreationErrorIsMappedToServerErrorOnSendTelemetry.

/**
 * Verifies that a ClientErrorException that occurs when creating an AMQP sender link is mapped to a
 * ServerErrorException with status 503 by the <em>sendTelemetry</em> method.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testSenderClientCreationErrorIsMappedToServerErrorOnSendTelemetry(final VertxTestContext ctx) {
    final TenantObject tenant = TenantObject.from(Constants.DEFAULT_TENANT, true);
    final RegistrationAssertion device = new RegistrationAssertion("4711");
    testSenderClientCreationErrorIsMappedToServerErrorOnSending(ctx, () -> sender.sendTelemetry(tenant, device, QoS.AT_MOST_ONCE, null, null, null, span.context()));
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class ProtonBasedDownstreamSenderTest method testSenderClientCreationErrorIsMappedToServerErrorOnSendEvent.

/**
 * Verifies that a ClientErrorException that occurs when creating an AMQP sender link is mapped to a
 * ServerErrorException with status 503 by the <em>sendEvent</em> method.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testSenderClientCreationErrorIsMappedToServerErrorOnSendEvent(final VertxTestContext ctx) {
    final TenantObject tenant = TenantObject.from(Constants.DEFAULT_TENANT, true);
    final RegistrationAssertion device = new RegistrationAssertion("4711");
    testSenderClientCreationErrorIsMappedToServerErrorOnSending(ctx, () -> sender.sendEvent(tenant, device, null, null, null, span.context()));
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 23 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class ProtonBasedDownstreamSenderTest method testSendEventMarksMessageAsDurable.

/**
 * Verifies that the proton message being transferred when sending an event is marked as durable.
 */
@Test
public void testSendEventMarksMessageAsDurable() {
    // WHEN sending an event
    final TenantObject tenant = TenantObject.from(Constants.DEFAULT_TENANT, true);
    final RegistrationAssertion device = new RegistrationAssertion("4711");
    sender.sendEvent(tenant, device, "text/plain", Buffer.buffer("hello"), null, span.context());
    verify(protonSender).send(argThat(Message::isDurable), VertxMockSupport.anyHandler());
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class AbstractProtocolAdapterBase method getRegistrationAssertion.

@Override
public final Future<RegistrationAssertion> getRegistrationAssertion(final String tenantId, final String deviceId, final Device authenticatedDevice, final SpanContext context) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    final Future<String> gatewayId = getGatewayId(tenantId, deviceId, authenticatedDevice);
    return gatewayId.compose(gwId -> getRegistrationClient().assertRegistration(tenantId, deviceId, gwId, context)).onSuccess(assertion -> {
        // the updateLastGateway invocation shouldn't delay or possibly fail the surrounding operation
        // so don't wait for the outcome here
        updateLastGateway(assertion, tenantId, deviceId, authenticatedDevice, context).onFailure(t -> {
            log.warn("failed to update last gateway [tenantId: {}, deviceId: {}]", tenantId, deviceId, t);
        });
    }).recover(error -> {
        final int errorCode = ServiceInvocationException.extractStatusCode(error);
        if (errorCode == HttpURLConnection.HTTP_NOT_FOUND) {
            return Future.failedFuture(new DeviceDisabledOrNotRegisteredException(tenantId, errorCode));
        } else if (errorCode == HttpURLConnection.HTTP_FORBIDDEN) {
            return Future.failedFuture(new GatewayDisabledOrNotRegisteredException(tenantId, errorCode));
        } else {
            return Future.failedFuture(error);
        }
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) Context(io.vertx.core.Context) NoopResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.NoopResourceLimitChecks) TelemetrySender(org.eclipse.hono.client.telemetry.TelemetrySender) MessagingType(org.eclipse.hono.util.MessagingType) Map(java.util.Map) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) JsonObject(io.vertx.core.json.JsonObject) EventSender(org.eclipse.hono.client.telemetry.EventSender) CommandContext(org.eclipse.hono.client.command.CommandContext) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) TenantClient(org.eclipse.hono.client.registry.TenantClient) MessageHelper(org.eclipse.hono.util.MessageHelper) ServiceBaseUtils(org.eclipse.hono.service.util.ServiceBaseUtils) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Objects(java.util.Objects) TrustOptions(io.vertx.core.net.TrustOptions) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Optional(java.util.Optional) QoS(org.eclipse.hono.util.QoS) ConnectionLimitManager(org.eclipse.hono.adapter.limiting.ConnectionLimitManager) Lifecycle(org.eclipse.hono.util.Lifecycle) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) TenantDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.TenantDisabledOrNotRegisteredException) ConnectionEventProducer(org.eclipse.hono.adapter.monitoring.ConnectionEventProducer) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) CommandResponseSender(org.eclipse.hono.client.command.CommandResponseSender) ArrayList(java.util.ArrayList) DeviceDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.DeviceDisabledOrNotRegisteredException) CompositeFuture(io.vertx.core.CompositeFuture) Status(io.vertx.ext.healthchecks.Status) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) GatewayDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.GatewayDisabledOrNotRegisteredException) TelemetryExecutionContext(org.eclipse.hono.util.TelemetryExecutionContext) Strings(org.eclipse.hono.util.Strings) ProtocolAdapterProperties(org.eclipse.hono.config.ProtocolAdapterProperties) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Sample(io.micrometer.core.instrument.Timer.Sample) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) CommandConsumerFactory(org.eclipse.hono.client.command.CommandConsumerFactory) ValidityBasedTrustOptions(org.eclipse.hono.service.auth.ValidityBasedTrustOptions) ServiceClient(org.eclipse.hono.client.util.ServiceClient) ConnectionAttemptOutcome(org.eclipse.hono.service.metric.MetricsTags.ConnectionAttemptOutcome) AbstractServiceBase(org.eclipse.hono.service.AbstractServiceBase) ResourceLimitChecks(org.eclipse.hono.adapter.resourcelimits.ResourceLimitChecks) DeviceDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.DeviceDisabledOrNotRegisteredException) GatewayDisabledOrNotRegisteredException(org.eclipse.hono.client.registry.GatewayDisabledOrNotRegisteredException)

Example 25 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class AbstractMessageSenderConnectionEventProducer method sendNotificationEvent.

private Future<Void> sendNotificationEvent(final Context context, final Device authenticatedDevice, final String protocolAdapter, final String remoteId, final String cause, final JsonObject data, final SpanContext spanContext) {
    if (authenticatedDevice == null) {
        // we only handle authenticated devices
        return Future.succeededFuture();
    }
    final String tenantId = authenticatedDevice.getTenantId();
    final String deviceId = authenticatedDevice.getDeviceId();
    return context.getTenantClient().get(tenantId, spanContext).compose(tenant -> {
        final JsonObject payload = new JsonObject();
        payload.put("cause", cause);
        payload.put("remote-id", remoteId);
        payload.put("source", protocolAdapter);
        if (data != null) {
            payload.put("data", data);
        }
        return Optional.ofNullable(context.getMessageSenderClient()).map(client -> client.sendEvent(tenant, new RegistrationAssertion(deviceId), EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, payload.toBuffer(), null, spanContext)).orElseGet(Future::succeededFuture);
    });
}
Also used : Optional(java.util.Optional) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) JsonObject(io.vertx.core.json.JsonObject) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) SpanContext(io.opentracing.SpanContext) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future)

Aggregations

RegistrationAssertion (org.eclipse.hono.util.RegistrationAssertion)42 Future (io.vertx.core.Future)31 HttpURLConnection (java.net.HttpURLConnection)29 MessageHelper (org.eclipse.hono.util.MessageHelper)29 Map (java.util.Map)27 Span (io.opentracing.Span)26 TenantObject (org.eclipse.hono.util.TenantObject)26 Buffer (io.vertx.core.buffer.Buffer)24 Constants (org.eclipse.hono.util.Constants)22 ServerErrorException (org.eclipse.hono.client.ServerErrorException)21 Device (org.eclipse.hono.auth.Device)20 Test (org.junit.jupiter.api.Test)20 ClientErrorException (org.eclipse.hono.client.ClientErrorException)18 Handler (io.vertx.core.Handler)17 Objects (java.util.Objects)17 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)17 HashMap (java.util.HashMap)16 Command (org.eclipse.hono.client.command.Command)16 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 SpanContext (io.opentracing.SpanContext)15