use of org.eclipse.hono.service.management.Result 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.service.management.Result 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.service.management.Result in project hono by eclipse.
the class AbstractTenantManagementService method updateTenant.
@Override
public final Future<OperationResult<Void>> updateTenant(final String tenantId, final Tenant tenantObj, final Optional<String> resourceVersion, final Span span) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(tenantObj);
Objects.requireNonNull(resourceVersion);
Objects.requireNonNull(span);
final Promise<Void> tenantCheck = Promise.promise();
try {
tenantObj.assertTrustAnchorIdUniquenessAndCreateMissingIds();
tenantCheck.complete();
} catch (final IllegalStateException e) {
log.debug("error updating tenant", e);
TracingHelper.logError(span, e);
tenantCheck.fail(new ClientErrorException(tenantId, HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage()));
}
return tenantCheck.future().compose(ok -> processUpdateTenant(tenantId, tenantObj, resourceVersion, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.UPDATE, tenantId, Instant.now(), tenantObj.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class DeviceAndGatewayAutoProvisionerTest method testProvisionFailsWhenEventNotificationFails.
@SuppressWarnings("unchecked")
private void testProvisionFailsWhenEventNotificationFails(final VertxTestContext ctx, final boolean isGateway, final String expectedDeviceId) throws CertificateEncodingException {
configureTenant(isGateway, null);
final JsonObject clientContext = new JsonObject().put(CredentialsConstants.FIELD_CLIENT_CERT, cert.getEncoded());
when(deviceManagementService.createDevice(eq(tenantId), any(), any(), any())).thenReturn(Future.succeededFuture(OperationResult.ok(HttpURLConnection.HTTP_CREATED, Id.of(deviceId), Optional.empty(), Optional.empty())));
when(deviceManagementService.updateDevice(eq(tenantId), eq(expectedDeviceId), any(), any(), any())).thenReturn(Future.succeededFuture(OperationResult.empty(HttpURLConnection.HTTP_NO_CONTENT)));
when(credentialsManagementService.updateCredentials(eq(tenantId), eq(expectedDeviceId), any(), any(), any())).thenReturn(Future.succeededFuture(OperationResult.empty(HttpURLConnection.HTTP_NO_CONTENT)));
// WHEN sending an auto-provisioning event fails
when(sender.sendEvent(any(TenantObject.class), any(RegistrationAssertion.class), anyString(), any(), any(Map.class), any())).thenReturn(Future.failedFuture(ServiceInvocationException.create(HttpURLConnection.HTTP_INTERNAL_ERROR, "error sending event")));
// WHEN provisioning a device/gateway from a certificate
deviceAndGatewayAutoProvisioner.provisionIfEnabled(tenantId, tenant, subjectDn, clientContext, NoopSpan.INSTANCE).onComplete(ctx.succeeding(result -> {
// VERIFY that the status code corresponds to an error.
assertThat(result.isError()).isTrue();
assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_INTERNAL_ERROR);
ctx.completeNow();
}));
}
use of org.eclipse.hono.service.management.Result in project hono by eclipse.
the class DeviceAndGatewayAutoProvisionerTest method testDeviceRegistrationIsRemovedWhenAutoProvisionFails.
/**
* Verifies that if auto-provisioning fails, then the device registered during auto-provisioning process is removed.
*
* @param ctx The vert.x test context.
* @throws CertificateEncodingException if the certificate cannot be encoded.
*/
@Test
public void testDeviceRegistrationIsRemovedWhenAutoProvisionFails(final VertxTestContext ctx) throws CertificateEncodingException {
// GIVEN a tenant CA with auto-provisioning enabled
tenant.getTrustedCertificateAuthorities().get(0).setAutoProvisioningEnabled(true);
final JsonObject clientContext = new JsonObject().put(CredentialsConstants.FIELD_CLIENT_CERT, cert.getEncoded());
when(deviceManagementService.createDevice(eq(tenantId), any(), any(), any())).thenReturn(Future.succeededFuture(OperationResult.ok(HttpURLConnection.HTTP_CREATED, Id.of(deviceId), Optional.empty(), Optional.empty())));
when(credentialsManagementService.updateCredentials(eq(tenantId), eq(deviceId), any(), any(), any())).thenReturn(Future.succeededFuture(OperationResult.empty(HttpURLConnection.HTTP_INTERNAL_ERROR)));
when(deviceManagementService.deleteDevice(eq(tenantId), eq(deviceId), any(), any())).thenReturn(Future.succeededFuture(Result.from(HttpURLConnection.HTTP_NO_CONTENT)));
// WHEN provisioning a device from a certificate
deviceAndGatewayAutoProvisioner.provisionIfEnabled(tenantId, tenant, subjectDn, clientContext, NoopSpan.INSTANCE).onComplete(ctx.succeeding(result -> {
ctx.verify(() -> {
// THEN the device is registered
verify(deviceManagementService).createDevice(eq(tenantId), any(), any(), any());
// WHEN update credentials fails
verify(credentialsManagementService).updateCredentials(eq(tenantId), eq(deviceId), any(), any(), any());
// THEN the device registration is deleted
verify(deviceManagementService).deleteDevice(eq(tenantId), eq(deviceId), any(), any());
assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_INTERNAL_ERROR);
});
ctx.completeNow();
}));
}
Aggregations