use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpoint method doCreateDevice.
private void doCreateDevice(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_CREATE_DEVICE, getClass().getSimpleName()).start();
final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), true));
final Future<Device> device = fromPayload(ctx);
CompositeFuture.all(tenantId, deviceId, device).compose(ok -> {
final Optional<String> did = Optional.ofNullable(deviceId.result());
TracingHelper.TAG_TENANT_ID.set(span, tenantId.result());
did.ifPresent(s -> TracingHelper.TAG_DEVICE_ID.set(span, s));
logger.debug("creating device [tenant: {}, device-id: {}]", tenantId.result(), did.orElse("<auto>"));
return getService().createDevice(tenantId.result(), did, device.result(), span);
}).onSuccess(operationResult -> writeResponse(ctx, operationResult, (responseHeaders, status) -> {
Optional.ofNullable(operationResult.getPayload()).map(Id::getId).ifPresent(id -> responseHeaders.set(HttpHeaders.LOCATION, String.format("/%s/%s/%s", getName(), tenantId.result(), id)));
}, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
use of org.eclipse.hono.service.management.Id 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.Id 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.Id in project hono by eclipse.
the class DelegatingDeviceManagementHttpEndpointTest method testCreateDeviceUsesIdFromUriParam.
/**
* Verifies that the endpoint uses the device ID provided in a request's URI
* for creating a device.
*/
@SuppressWarnings("unchecked")
@Test
public void testCreateDeviceUsesIdFromUriParam() {
final JsonObject json = new JsonObject().put(RegistryManagementConstants.FIELD_DOWNSTREAM_MESSAGE_MAPPER, "my-mapper").put(RegistryManagementConstants.FIELD_EXT, new JsonObject().put("custom", "value"));
requestBody = json.toBuffer();
when(service.createDevice(anyString(), any(Optional.class), any(Device.class), any(Span.class))).thenAnswer(invocation -> {
final Optional<String> deviceId = invocation.getArgument(1);
return Future.succeededFuture(OperationResult.ok(HttpURLConnection.HTTP_CREATED, Id.of(deviceId.get()), Optional.empty(), Optional.empty()));
});
final HttpServerResponse response = newResponse();
final HttpServerRequest request = newRequest(HttpMethod.POST, "/v1/devices/mytenant/mydeviceid", requestHeaders, requestParams, response);
router.handle(request);
verify(response).setStatusCode(HttpURLConnection.HTTP_CREATED);
verify(response).write(argThat((Buffer buffer) -> "mydeviceid".equals(buffer.toJsonObject().getString(RegistryManagementConstants.FIELD_ID))));
verify(service).createDevice(eq("mytenant"), argThat(deviceId -> "mydeviceid".equals(deviceId.get())), argThat(device -> {
return "my-mapper".equals(device.getDownstreamMessageMapper()) && "value".equals(device.getExtensions().get("custom"));
}), any(Span.class));
}
use of org.eclipse.hono.service.management.Id in project hono by eclipse.
the class DelegatingTenantManagementHttpEndpoint method getTenant.
private void getTenant(final RoutingContext ctx) {
final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_GET_TENANT, getClass().getSimpleName()).start();
getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false)).compose(tenantId -> {
TracingHelper.TAG_TENANT_ID.set(span, tenantId);
logger.debug("retrieving tenant [id: {}]", tenantId);
return getService().readTenant(tenantId, span);
}).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Aggregations