Search in sources :

Example 1 with EntityNotFoundException

use of org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException in project hono by eclipse.

the class ManagementStore method update.

/**
 * Create a new tenant.
 * <p>
 * The operation may fail with a {@link org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException} if the
 * specified tenant does not exist.
 * <p>
 * The operation may fail with a {@link org.eclipse.hono.service.base.jdbc.store.DuplicateKeyException} if a
 * tenant with the ID or trust anchor already exists.
 * <p>
 * The operation may fail with an {@link org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException} if
 * an expected resource version was provided, but the current version did not match.
 *
 * @param tenantId The ID of the new tenant.
 * @param tenant The tenant information.
 * @param resourceVersion An optional resource version.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<Versioned<Void>> update(final String tenantId, final Tenant tenant, final Optional<String> resourceVersion, final SpanContext spanContext) {
    final var json = tenantToJson(tenant);
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "update tenant", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    final var nextVersion = UUID.randomUUID().toString();
    resourceVersion.ifPresent(version -> span.setTag("version", version));
    final Statement statement = resourceVersion.isPresent() ? this.updateVersionedStatement : this.updateStatement;
    return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> updateJsonField(connection, tenantId, statement, json, resourceVersion, nextVersion, span).flatMap(r -> {
        if (r.getUpdated() <= 0) {
            return Future.failedFuture(new EntityNotFoundException());
        } else {
            return Future.succeededFuture();
        }
    }).flatMap(x -> deleteAllTrustAnchors(connection, tenantId, span)).flatMap(r -> insertAllTrustAnchors(connection, tenantId, tenant, span))).map(new Versioned<Void>(nextVersion, null)).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) SpanContext(io.opentracing.SpanContext) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) JDBCClient(io.vertx.ext.jdbc.JDBCClient) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLOperations(io.vertx.ext.sql.SQLOperations) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) Span(io.opentracing.Span)

Example 2 with EntityNotFoundException

use of org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException in project hono by eclipse.

the class TableManagementStore method extractVersionForUpdate.

private static Future<String> extractVersionForUpdate(final ResultSet device, final Optional<String> resourceVersion) {
    final Optional<String> version = device.getRows(true).stream().map(o -> o.getString("version")).findAny();
    if (version.isEmpty()) {
        log.debug("No version or no row found -> entity not found");
        return Future.failedFuture(new EntityNotFoundException());
    }
    final var currentVersion = version.get();
    return resourceVersion.<// if we expect a certain version
    Future<String>>map(expected -> {
        // check ...
        if (expected.equals(currentVersion)) {
            // version matches, continue with current version
            return Future.succeededFuture(currentVersion);
        } else {
            // version does not match, abort
            return Future.failedFuture(new OptimisticLockingException());
        }
    }).orElseGet(() -> Future.succeededFuture(currentVersion));
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) LoggerFactory(org.slf4j.LoggerFactory) Supplier(java.util.function.Supplier) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Map(java.util.Map) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Timestamp(java.sql.Timestamp) Promise(io.vertx.core.Promise) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) OptimisticLockingException(org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JDBCClient(io.vertx.ext.jdbc.JDBCClient) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) ResultSet(io.vertx.ext.sql.ResultSet) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Collections(java.util.Collections) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) OptimisticLockingException(org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException)

Aggregations

Span (io.opentracing.Span)2 SpanContext (io.opentracing.SpanContext)2 Tracer (io.opentracing.Tracer)2 CompositeFuture (io.vertx.core.CompositeFuture)2 Future (io.vertx.core.Future)2 JsonObject (io.vertx.core.json.JsonObject)2 JDBCClient (io.vertx.ext.jdbc.JDBCClient)2 SQLConnection (io.vertx.ext.sql.SQLConnection)2 UpdateResult (io.vertx.ext.sql.UpdateResult)2 Optional (java.util.Optional)2 UUID (java.util.UUID)2 Collectors (java.util.stream.Collectors)2 Versioned (org.eclipse.hono.deviceregistry.util.Versioned)2 EntityNotFoundException (org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException)2 SQL (org.eclipse.hono.service.base.jdbc.store.SQL)2 Statement (org.eclipse.hono.service.base.jdbc.store.Statement)2 StatementConfiguration (org.eclipse.hono.service.base.jdbc.store.StatementConfiguration)2 Tenant (org.eclipse.hono.service.management.tenant.Tenant)2 TracingHelper (org.eclipse.hono.tracing.TracingHelper)2 Logger (org.slf4j.Logger)2