Search in sources :

Example 6 with Id

use of org.eclipse.hono.service.management.Id in project hono by eclipse.

the class DelegatingTenantManagementHttpEndpoint method createTenant.

private void createTenant(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_CREATE_TENANT, getClass().getSimpleName()).start();
    final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), true));
    final Future<Tenant> payload = fromPayload(ctx);
    CompositeFuture.all(tenantId, payload).compose(ok -> {
        final Optional<String> tid = Optional.ofNullable(tenantId.result());
        tid.ifPresent(s -> TracingHelper.TAG_TENANT_ID.set(span, s));
        logger.debug("creating tenant [{}]", tid.orElse("<auto>"));
        return getService().createTenant(tid, payload.result(), span);
    }).onSuccess(operationResult -> writeResponse(ctx, operationResult, (responseHeaders, status) -> {
        if (status == HttpURLConnection.HTTP_CREATED) {
            Optional.ofNullable(operationResult.getPayload()).map(Id::getId).ifPresent(id -> {
                final String location = String.format("/%s/%s", getName(), id);
                responseHeaders.set(HttpHeaders.LOCATION, location);
            });
        }
    }, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) Optional(java.util.Optional) Id(org.eclipse.hono.service.management.Id) Span(io.opentracing.Span)

Example 7 with Id

use of org.eclipse.hono.service.management.Id in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doGetDevice.

private void doGetDevice(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_GET_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(), false));
    CompositeFuture.all(tenantId, deviceId).compose(ok -> {
        TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
        logger.debug("retrieving device [tenant: {}, device-id: {}]", tenantId.result(), deviceId.result());
        return getService().readDevice(tenantId.result(), deviceId.result(), span);
    }).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) Span(io.opentracing.Span)

Example 8 with Id

use of org.eclipse.hono.service.management.Id in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doUpdateDevice.

private void doUpdateDevice(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_UPDATE_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(), false));
    final Future<Device> device = fromPayload(ctx);
    CompositeFuture.all(tenantId, deviceId, device).compose(ok -> {
        TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
        logger.debug("updating device [tenant: {}, device-id: {}]", tenantId.result(), deviceId.result());
        final Optional<String> resourceVersion = Optional.ofNullable(ctx.get(KEY_RESOURCE_VERSION));
        return getService().updateDevice(tenantId.result(), deviceId.result(), device.result(), resourceVersion, span);
    }).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) Optional(java.util.Optional) Span(io.opentracing.Span)

Example 9 with Id

use of org.eclipse.hono.service.management.Id in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doDeleteDevice.

private void doDeleteDevice(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_REMOVE_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(), false));
    CompositeFuture.all(tenantId, deviceId).compose(ok -> {
        TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
        logger.debug("removing device [tenant: {}, device-id: {}]", tenantId.result(), deviceId.result());
        final Optional<String> resourceVersion = Optional.ofNullable(ctx.get(KEY_RESOURCE_VERSION));
        return getService().deleteDevice(tenantId.result(), deviceId.result(), resourceVersion, span);
    }).onSuccess(result -> writeResponse(ctx, result, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) Optional(java.util.Optional) Span(io.opentracing.Span)

Example 10 with Id

use of org.eclipse.hono.service.management.Id in project hono by eclipse.

the class AbstractTenantManagementService method createTenant.

@Override
public final Future<OperationResult<Id>> createTenant(final Optional<String> tenantId, final Tenant tenantObj, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(tenantObj);
    Objects.requireNonNull(span);
    final Promise<Void> tenantCheck = Promise.promise();
    try {
        tenantObj.assertTrustAnchorIdUniquenessAndCreateMissingIds();
        tenantCheck.complete();
    } catch (final IllegalStateException e) {
        log.debug("error creating tenant", e);
        TracingHelper.logError(span, e);
        tenantCheck.fail(new ClientErrorException(tenantId.orElse("N/A"), HttpURLConnection.HTTP_BAD_REQUEST, e.getMessage()));
    }
    final String tenantIdValue = tenantId.orElseGet(this::createId);
    return tenantCheck.future().compose(ok -> processCreateTenant(tenantIdValue, tenantObj, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new TenantChangeNotification(LifecycleChange.CREATE, tenantIdValue, Instant.now(), tenantObj.isEnabled()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId.get()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) 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) ClientErrorException(org.eclipse.hono.client.ClientErrorException) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification)

Aggregations

Span (io.opentracing.Span)12 Future (io.vertx.core.Future)12 Vertx (io.vertx.core.Vertx)12 HttpURLConnection (java.net.HttpURLConnection)12 List (java.util.List)12 Optional (java.util.Optional)12 Filter (org.eclipse.hono.service.management.Filter)12 Id (org.eclipse.hono.service.management.Id)12 Sort (org.eclipse.hono.service.management.Sort)12 Objects (java.util.Objects)11 HttpHeaders (io.vertx.core.http.HttpHeaders)7 HttpMethod (io.vertx.core.http.HttpMethod)7 JsonObject (io.vertx.core.json.JsonObject)7 Router (io.vertx.ext.web.Router)7 ServiceConfigProperties (org.eclipse.hono.config.ServiceConfigProperties)7 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)7 CompositeFuture (io.vertx.core.CompositeFuture)6 Promise (io.vertx.core.Promise)6 ClientErrorException (org.eclipse.hono.client.ClientErrorException)6 OperationResult (org.eclipse.hono.service.management.OperationResult)6