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));
}
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));
}
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));
}
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));
}
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();
})));
}
Aggregations