use of org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto in project hono by eclipse.
the class TableManagementStore method updateDevice.
/**
* Update device registration information.
* <p>
* This called the {@link #updateJsonField(DeviceKey, Statement, String, Optional, String, Span)} method
* with either the {@code updateRegistration} or {@code updateRegistrationVersioned}
* statement.
*
* @param key The key of the device to update.
* @param device The device data to store.
* @param resourceVersion The optional resource version.
* @param spanContext The span to contribute to.
* @return A future, tracking the outcome of the operation.
*/
public Future<Versioned<Void>> updateDevice(final DeviceKey key, final Device device, final Optional<String> resourceVersion, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "update device", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, key.getDeviceId()).start();
resourceVersion.ifPresent(version -> span.setTag("version", version));
final var memberOf = Optional.ofNullable(device.getMemberOf()).<Set<String>>map(HashSet::new).orElse(Collections.emptySet());
final JdbcBasedDeviceDto deviceDto = JdbcBasedDeviceDto.forUpdate(key, device, DeviceRegistryUtils.getUniqueIdentifier());
return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> readDeviceForUpdate(connection, key, context).compose(result -> extractVersionForUpdate(result, resourceVersion)).compose(version -> deleteGroups(connection, key, context).map(version)).compose(version -> createGroups(connection, key, memberOf, context).map(version)).compose(version -> this.updateRegistrationVersionedStatement.expand(map -> {
map.put("tenant_id", deviceDto.getTenantId());
map.put("device_id", deviceDto.getDeviceId());
map.put("data", deviceDto.getDeviceJson());
map.put("expected_version", version);
map.put("next_version", deviceDto.getVersion());
map.put("updated_on", Timestamp.from(deviceDto.getUpdatedOn()));
map.put("auto_provisioning_notification_sent", deviceDto.isAutoProvisioningNotificationSent());
}).trace(this.tracer, span.context()).update(connection).compose(TableManagementStore::checkUpdateOutcome).map(version))).map(x -> new Versioned<Void>(deviceDto.getVersion(), null)).onComplete(x -> span.finish());
}
use of org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto 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
* <= 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());
}
Aggregations