Search in sources :

Example 6 with OperationResult

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

the class AbstractDeviceManagementService method updateDevice.

@Override
public final Future<OperationResult<Id>> updateDevice(final String tenantId, final String deviceId, final Device device, final Optional<String> resourceVersion, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(device);
    Objects.requireNonNull(resourceVersion);
    Objects.requireNonNull(span);
    return this.tenantInformationService.tenantExists(tenantId, span).compose(result -> result.isError() ? Future.failedFuture(ServiceInvocationException.create(tenantId, result.getStatus(), "tenant does not exist", null)) : processUpdateDevice(DeviceKey.from(result.getPayload(), deviceId), device, resourceVersion, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new DeviceChangeNotification(LifecycleChange.UPDATE, tenantId, deviceId, 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 7 with OperationResult

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

the class DelegatingCredentialsManagementHttpEndpoint method getCredentialsForDevice.

private void getCredentialsForDevice(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_GET_CREDENTIALS, 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("getting credentials [tenant: {}, device-id: {}]]", tenantId.result(), deviceId.result());
        return getService().readCredentials(tenantId.result(), deviceId.result(), span);
    }).map(operationResult -> {
        if (operationResult.isOk()) {
            // we need to map the payload from List to JsonArray because
            // Jackson does not include the credential's type property in the
            // JSON when serializing a plain java List<?> object.
            final JsonArray credentialsArray = operationResult.getPayload().stream().map(credentials -> JsonObject.mapFrom(credentials)).collect(JsonArray::new, JsonArray::add, JsonArray::addAll);
            return OperationResult.ok(operationResult.getStatus(), credentialsArray, operationResult.getCacheDirective(), operationResult.getResourceVersion());
        } else {
            return operationResult;
        }
    }).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) 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) 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) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) JsonArray(io.vertx.core.json.JsonArray) Span(io.opentracing.Span)

Aggregations

Span (io.opentracing.Span)7 Future (io.vertx.core.Future)7 Vertx (io.vertx.core.Vertx)7 HttpURLConnection (java.net.HttpURLConnection)7 List (java.util.List)7 Objects (java.util.Objects)7 Optional (java.util.Optional)7 OperationResult (org.eclipse.hono.service.management.OperationResult)7 Instant (java.time.Instant)5 ClientErrorException (org.eclipse.hono.client.ClientErrorException)5 DeviceRegistryUtils (org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils)5 NotificationEventBusSupport (org.eclipse.hono.notification.NotificationEventBusSupport)5 Promise (io.vertx.core.Promise)4 ServerErrorException (org.eclipse.hono.client.ServerErrorException)4 LifecycleChange (org.eclipse.hono.notification.deviceregistry.LifecycleChange)4 Filter (org.eclipse.hono.service.management.Filter)4 Id (org.eclipse.hono.service.management.Id)4 Result (org.eclipse.hono.service.management.Result)4 SearchResult (org.eclipse.hono.service.management.SearchResult)4 Sort (org.eclipse.hono.service.management.Sort)4