Search in sources :

Example 6 with Statement

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

the class TableManagementStore method getDeviceCount.

/**
 * Gets the number of devices that are registered for a tenant.
 *
 * @param tenantId The tenant to count devices for.
 * @param spanContext The span to contribute to.
 * @return A future tracking the outcome of the operation.
 * @throws NullPointerException if tenant is {@code null}.
 */
public Future<Integer> getDeviceCount(final String tenantId, final SpanContext spanContext) {
    Objects.requireNonNull(tenantId);
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "get device count", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
    final var expanded = this.countDevicesOfTenantStatement.expand(params -> {
        params.put("tenant_id", tenantId);
    });
    log.debug("count - statement: {}", expanded);
    return expanded.trace(this.tracer, span.context()).query(this.client).map(r -> {
        final var entries = r.getRows(true);
        switch(entries.size()) {
            case 1:
                final Integer count = entries.get(0).getInteger("DEVICECOUNT");
                log.debug("found {} devices registered for tenant [tenant-id: {}]", count, tenantId);
                return count;
            default:
                throw new IllegalStateException("Could not count devices of tenant");
        }
    }).onComplete(x -> span.finish());
}
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) Span(io.opentracing.Span)

Example 7 with Statement

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

the class TableManagementStore method createDevice.

/**
 * Creates a new device.
 * <p>
 * This method executes the {@code create} statement, providing the named parameters
 * {@code tenant_id}, {@code device_id}, {@code version}, and {@code data}.
 * <p>
 * It returns the plain update result. In case a device with the same ID already
 * exists, the underlying database must throw an {@link java.sql.SQLException}, indicating
 * a duplicate entity or constraint violation. This will be translated into a
 * failed future with an {@link org.eclipse.hono.service.base.jdbc.store.DuplicateKeyException}.
 *
 * @param key The key of the device to create.
 * @param device The device data.
 * @param tenant The configuration of the tenant that the device belongs to.
 * @param globalDevicesPerTenantLimit The globally defined maximum number of devices per tenant. A value
 *                                    &lt;= 0 will be interpreted as no limit being defined.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<Versioned<Void>> createDevice(final DeviceKey key, final Device device, final Tenant tenant, final int globalDevicesPerTenantLimit, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "create device", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, key.getDeviceId()).start();
    final JdbcBasedDeviceDto deviceDto = JdbcBasedDeviceDto.forCreation(key, device, DeviceRegistryUtils.getUniqueIdentifier());
    return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> {
        final var expanded = this.createStatement.expand(params -> {
            params.put("tenant_id", deviceDto.getTenantId());
            params.put("device_id", deviceDto.getDeviceId());
            params.put("version", deviceDto.getVersion());
            params.put("data", deviceDto.getDeviceJson());
            params.put("created", Timestamp.from(deviceDto.getCreationTime()));
            params.put("auto_provisioned", deviceDto.isAutoProvisioned());
        });
        log.debug("createDevice - statement: {}", expanded);
        return getDeviceCount(key.getTenantId(), span.context()).compose(currentDeviceCount -> tenant.checkDeviceLimitReached(key.getTenantId(), currentDeviceCount, globalDevicesPerTenantLimit)).compose(ok -> expanded.trace(this.tracer, context).update(this.client).recover(SQL::translateException)).compose(x -> createGroups(connection, key, new HashSet<>(device.getMemberOf()), context));
    }).map(new Versioned<Void>(deviceDto.getVersion(), null)).onComplete(x -> span.finish());
}
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) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) Span(io.opentracing.Span) SQL(org.eclipse.hono.service.base.jdbc.store.SQL) HashSet(java.util.HashSet)

Example 8 with Statement

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

the class TableAdapterStore method resolveGroupMembers.

/**
 * Resolve a list of group members.
 *
 * @param tenantId The tenant the device belongs to.
 * @param viaGroups The viaGroups list of a device. This list contains the ids of groups.
 * @param spanContext The span to contribute to.
 *
 * @return A future tracking the outcome of the operation.
 *
 * @see org.eclipse.hono.deviceregistry.service.device.AbstractRegistrationService#resolveGroupMembers(String, JsonArray, Span)
 */
public Future<Set<String>> resolveGroupMembers(final String tenantId, final Set<String> viaGroups, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "resolve group members", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).withTag("via_groups", String.join(", ", viaGroups)).start();
    final var expanded = this.resolveGroupsStatement.expand(params -> {
        params.put("tenant_id", tenantId);
        params.put("group_ids", convertToArrayValue(viaGroups));
    });
    log.debug("resolveGroupMembers - statement: {}", expanded);
    return expanded.trace(this.tracer, span.context()).query(this.client).flatMap(r -> {
        final var entries = r.getRows(true);
        span.log(Map.of("event", "read result", "rows", entries.size()));
        return Future.succeededFuture(entries.stream().map(o -> o.getString("device_id")).filter(Objects::nonNull).collect(Collectors.toSet()));
    }).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Device(org.eclipse.hono.service.management.device.Device) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) CredentialKey(org.eclipse.hono.deviceregistry.service.credentials.CredentialKey) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) 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) Map(java.util.Map) ResultSet(io.vertx.ext.sql.ResultSet) Optional(java.util.Optional) Span(io.opentracing.Span) TracingHelper(org.eclipse.hono.tracing.TracingHelper) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Span(io.opentracing.Span)

Example 9 with Statement

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

the class TableAdapterStore method findCredentials.

/**
 * Find credentials for a device.
 *
 * @param key The credentials key to look for.
 * @param spanContext The span context.
 *
 * @return A future tracking the outcome of the operation.
 */
public Future<Optional<CredentialsReadResult>> findCredentials(final CredentialKey key, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "find credentials", getClass().getSimpleName()).withTag(TracingHelper.TAG_AUTH_ID, key.getAuthId()).withTag(TracingHelper.TAG_CREDENTIALS_TYPE, key.getType()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).start();
    final var expanded = this.findCredentialsStatement.expand(params -> {
        params.put("tenant_id", key.getTenantId());
        params.put("type", key.getType());
        params.put("auth_id", key.getAuthId());
    });
    log.debug("findCredentials - statement: {}", expanded);
    return expanded.trace(this.tracer, span.context()).query(this.client).<Optional<CredentialsReadResult>>flatMap(r -> {
        final var entries = r.getRows(true);
        span.log(Map.of("event", "read result", "rows", entries.size()));
        final Set<String> deviceIds = entries.stream().map(o -> o.getString("device_id")).filter(Objects::nonNull).collect(Collectors.toSet());
        final int num = deviceIds.size();
        if (num <= 0) {
            return Future.succeededFuture(Optional.empty());
        } else if (num > 1) {
            TracingHelper.logError(span, "Found multiple entries for a single device");
            return Future.failedFuture(new IllegalStateException("Found multiple entries for a single device"));
        }
        // we know now that we have exactly one entry
        final String deviceId = deviceIds.iterator().next();
        final List<CommonCredential> credentials = entries.stream().map(o -> o.getString("data")).map(s -> Json.decodeValue(s, CommonCredential.class)).collect(Collectors.toList());
        return Future.succeededFuture(Optional.of(new CredentialsReadResult(deviceId, credentials, Optional.empty())));
    }).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Device(org.eclipse.hono.service.management.device.Device) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) CredentialKey(org.eclipse.hono.deviceregistry.service.credentials.CredentialKey) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) 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) Map(java.util.Map) ResultSet(io.vertx.ext.sql.ResultSet) Optional(java.util.Optional) Span(io.opentracing.Span) TracingHelper(org.eclipse.hono.tracing.TracingHelper) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Set(java.util.Set) ResultSet(io.vertx.ext.sql.ResultSet) Objects(java.util.Objects) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) List(java.util.List) Span(io.opentracing.Span)

Example 10 with Statement

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

the class AbstractTenantStore method readTenantBy.

/**
 * Read a tenant, using the provided statement.
 *
 * @param operations The operations to use.
 * @param expanded The statement to use.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
protected Future<Optional<TenantReadResult>> readTenantBy(final SQLOperations operations, final ExpandedStatement expanded, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "read tenant by", getClass().getSimpleName()).start();
    return expanded.trace(this.tracer, span.context()).query(operations).<Optional<TenantReadResult>>flatMap(r -> {
        final var entries = r.getRows(true);
        span.log(Map.of("event", "read result", "rows", entries.size()));
        switch(entries.size()) {
            case 0:
                return Future.succeededFuture(Optional.empty());
            case 1:
                return Future.succeededFuture(entries.get(0)).map(entry -> {
                    final var id = entry.getString("tenant_id");
                    final var tenant = Json.decodeValue(entry.getString("data"), Tenant.class);
                    final var version = Optional.ofNullable(entry.getString("version"));
                    return Optional.of(new TenantReadResult(id, tenant, version));
                });
            default:
                return Future.failedFuture(new IllegalStateException("Found multiple entries for a single tenant"));
        }
    }).flatMap(result -> {
        if (result.isPresent()) {
            return fillTrustAnchors(operations, result.get(), span.context()).map(Optional::ofNullable);
        } else {
            return Future.succeededFuture(result);
        }
    }).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) LoggerFactory(org.slf4j.LoggerFactory) AbstractStore(org.eclipse.hono.service.base.jdbc.store.AbstractStore) 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) ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) SpanContext(io.opentracing.SpanContext) List(java.util.List) JDBCClient(io.vertx.ext.jdbc.JDBCClient) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Map(java.util.Map) ResultSet(io.vertx.ext.sql.ResultSet) 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) Optional(java.util.Optional) Span(io.opentracing.Span)

Aggregations

Span (io.opentracing.Span)10 SpanContext (io.opentracing.SpanContext)10 Tracer (io.opentracing.Tracer)10 Future (io.vertx.core.Future)10 JDBCClient (io.vertx.ext.jdbc.JDBCClient)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 SQL (org.eclipse.hono.service.base.jdbc.store.SQL)10 Statement (org.eclipse.hono.service.base.jdbc.store.Statement)10 StatementConfiguration (org.eclipse.hono.service.base.jdbc.store.StatementConfiguration)10 TracingHelper (org.eclipse.hono.tracing.TracingHelper)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 Json (io.vertx.core.json.Json)8 JsonObject (io.vertx.core.json.JsonObject)8 ResultSet (io.vertx.ext.sql.ResultSet)8 List (java.util.List)8 Map (java.util.Map)8 Tenant (org.eclipse.hono.service.management.tenant.Tenant)8 CompositeFuture (io.vertx.core.CompositeFuture)6