Search in sources :

Example 1 with DeviceChangeNotification

use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.

the class AbstractDeviceManagementService method deleteDevice.

@Override
public final Future<Result<Void>> deleteDevice(final String tenantId, final String deviceId, final Optional<String> resourceVersion, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(resourceVersion);
    Objects.requireNonNull(span);
    return this.tenantInformationService.tenantExists(tenantId, span).otherwise(t -> Result.from(ServiceInvocationException.extractStatusCode(t))).compose(result -> {
        switch(result.getStatus()) {
            case HttpURLConnection.HTTP_OK:
                break;
            case HttpURLConnection.HTTP_NOT_FOUND:
                span.log("tenant does not exist (anymore)");
                LOG.info("trying to delete device of non-existing tenant [tenant-id: {}, device-id: {}]", tenantId, deviceId);
                break;
            default:
                span.log(Map.of(Fields.EVENT, "could not determine tenant status", Tags.HTTP_STATUS.getKey(), result.getStatus()));
                LOG.info("could not determine tenant status [tenant-id: {}, code: {}]", tenantId, result.getStatus());
        }
        return processDeleteDevice(DeviceKey.from(tenantId, deviceId), resourceVersion, span);
    }).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new DeviceChangeNotification(LifecycleChange.DELETE, tenantId, deviceId, Instant.now(), false))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Tags(io.opentracing.tag.Tags) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Fields(io.opentracing.log.Fields) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) NoopTenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.NoopTenantInformationService) Device(org.eclipse.hono.service.management.device.Device) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) UUID(java.util.UUID) Instant(java.time.Instant) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Id(org.eclipse.hono.service.management.Id) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)

Example 2 with DeviceChangeNotification

use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.

the class AbstractDeviceManagementService method createDevice.

@Override
public final Future<OperationResult<Id>> createDevice(final String tenantId, final Optional<String> deviceId, final Device device, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(device);
    Objects.requireNonNull(span);
    final String deviceIdValue = deviceId.orElseGet(() -> generateDeviceId(tenantId));
    return this.tenantInformationService.tenantExists(tenantId, span).compose(result -> result.isError() ? Future.failedFuture(ServiceInvocationException.create(tenantId, result.getStatus(), "tenant does not exist", null)) : processCreateDevice(DeviceKey.from(result.getPayload(), deviceIdValue), device, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new DeviceChangeNotification(LifecycleChange.CREATE, tenantId, deviceIdValue, Instant.now(), device.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Tags(io.opentracing.tag.Tags) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Fields(io.opentracing.log.Fields) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) NoopTenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.NoopTenantInformationService) Device(org.eclipse.hono.service.management.device.Device) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) UUID(java.util.UUID) Instant(java.time.Instant) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Id(org.eclipse.hono.service.management.Id) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)

Example 3 with DeviceChangeNotification

use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnDeviceDeletedNotification.

/**
 * Verifies that the adapter closes the connection to an authenticated device when a notification
 * about the deletion of registration data of that device has been received.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceConnectionIsClosedOnDeviceDeletedNotification(final VertxTestContext ctx) {
    final Device device = new Device("tenant", "deviceId");
    testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new DeviceChangeNotification(LifecycleChange.DELETE, "tenant", "deviceId", Instant.now(), true));
}
Also used : Device(org.eclipse.hono.auth.Device) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Test(org.junit.jupiter.api.Test)

Example 4 with DeviceChangeNotification

use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testDeviceConnectionIsClosedOnDeviceDisabledNotification.

/**
 * Verifies that the adapter closes the connection to an authenticated device when a notification
 * has been received that the device has been disabled.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceConnectionIsClosedOnDeviceDisabledNotification(final VertxTestContext ctx) {
    final Device device = new Device("tenant", "deviceId");
    testDeviceConnectionIsClosedOnDeviceOrTenantChangeNotification(ctx, device, new DeviceChangeNotification(LifecycleChange.UPDATE, "tenant", "deviceId", Instant.now(), false));
}
Also used : Device(org.eclipse.hono.auth.Device) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Test(org.junit.jupiter.api.Test)

Example 5 with DeviceChangeNotification

use of org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification in project hono by eclipse.

the class ProtonBasedCredentialsClientTest method testDeviceChangeNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes credentials of a device from the cache if it receives a notification about a
 * change in that device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testDeviceChangeNotificationRemovesValueFromCache(final VertxTestContext ctx) {
    final String tenantId = "the-tenant-id";
    final String deviceId = "the-device-id";
    givenAClient(cache);
    final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(DeviceChangeNotification.TYPE);
    final Set<AnnotatedCacheKey<?>> expectedCacheRemovals = new HashSet<>();
    // GIVEN a client with a cache containing credentials of two tenants
    client.start().compose(v -> addResultToCache("other-tenant", deviceId, "other")).compose(v -> addResultToCache(tenantId, "other-device", "other")).compose(v -> addResultToCache(tenantId, deviceId, "auth-id1")).map(expectedCacheRemovals::add).compose(v -> addResultToCache(tenantId, deviceId, "auth-id2")).map(expectedCacheRemovals::add).onComplete(ctx.succeeding(ok -> ctx.verify(() -> {
        // WHEN receiving a notification about a change on the device
        sendViaEventBusMock(new DeviceChangeNotification(LifecycleChange.UPDATE, tenantId, deviceId, Instant.now(), false), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for all credentials of the changed device and not for other devices
        ctx.verify(() -> verify(cache).invalidateAll(expectedCacheRemovals));
        ctx.completeNow();
    })));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Tags(io.opentracing.tag.Tags) Timeout(io.vertx.junit5.Timeout) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsResult(org.eclipse.hono.util.CredentialsResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Cache(com.github.benmanes.caffeine.cache.Cache) Constants(org.eclipse.hono.util.Constants) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Message(org.apache.qpid.proton.message.Message) HonoConnection(org.eclipse.hono.client.HonoConnection) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) ProtonHelper(io.vertx.proton.ProtonHelper) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

DeviceChangeNotification (org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)11 Instant (java.time.Instant)8 AllDevicesOfTenantDeletedNotification (org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification)8 Test (org.junit.jupiter.api.Test)8 Future (io.vertx.core.Future)7 Vertx (io.vertx.core.Vertx)7 LifecycleChange (org.eclipse.hono.notification.deviceregistry.LifecycleChange)7 Span (io.opentracing.Span)6 HttpURLConnection (java.net.HttpURLConnection)6 NotificationEventBusSupport (org.eclipse.hono.notification.NotificationEventBusSupport)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 Tracer (io.opentracing.Tracer)4 EventBus (io.vertx.core.eventbus.EventBus)4 JsonObject (io.vertx.core.json.JsonObject)4 Cache (com.github.benmanes.caffeine.cache.Cache)3 Tags (io.opentracing.tag.Tags)3 Handler (io.vertx.core.Handler)3 Timeout (io.vertx.junit5.Timeout)3 VertxExtension (io.vertx.junit5.VertxExtension)3 VertxTestContext (io.vertx.junit5.VertxTestContext)3