use of org.eclipse.hono.util.RequestResponseResult in project hono by eclipse.
the class ProtonBasedCommandRouterClient method enableCommandRouting.
@Override
public Future<Void> enableCommandRouting(final List<String> tenantIds, final SpanContext context) {
Objects.requireNonNull(tenantIds);
if (tenantIds.isEmpty()) {
return Future.succeededFuture();
}
final Span currentSpan = newChildSpan(context, "enable command routing");
currentSpan.log(Map.of("no_of_tenants", tenantIds.size()));
final JsonArray payload = new JsonArray(tenantIds);
final Future<RequestResponseResult<JsonObject>> resultTracker = getOrCreateClient(tenantIds.get(0)).compose(client -> client.createAndSendRequest(CommandRouterConstants.CommandRouterAction.ENABLE_COMMAND_ROUTING.getSubject(), null, payload.toBuffer(), MessageHelper.CONTENT_TYPE_APPLICATION_JSON, this::getRequestResponseResult, currentSpan));
return mapResultAndFinishSpan(resultTracker, result -> {
switch(result.getStatus()) {
case HttpURLConnection.HTTP_NO_CONTENT:
log.info("successfully enabled routing of commands for {} tenants in Command Router", tenantIds.size());
return null;
default:
final ServiceInvocationException e = StatusCodeMapper.from(result);
log.info("failed to enable routing of commands in Command Router", e);
throw e;
}
}, currentSpan).mapEmpty();
}
use of org.eclipse.hono.util.RequestResponseResult in project hono by eclipse.
the class ProtonBasedCommandRouterClient method setLastKnownGateways.
/**
* For a given list of device and gateway combinations, sets the gateway as the last gateway that acted on behalf
* of the device.
*
* @param tenantId The tenant id.
* @param deviceToGatewayMap The map associating device identifiers with the corresponding last gateway.
* @param context The currently active OpenTracing span context or {@code null} if no span is currently active.
* An implementation should use this as the parent for any span it creates for tracing
* the execution of this operation.
* @return A future indicating the outcome of the operation.
* <p>
* The future will be succeeded if the entries were successfully set.
* Otherwise the future will be failed with a {@code org.eclipse.hono.client.ServiceInvocationException}.
* The outcome is indeterminate if any of the entries cannot be processed by the Command Router service
* implementation. In such a case, client code should assume that none of the entries have been updated.
*/
protected Future<Void> setLastKnownGateways(final String tenantId, final Map<String, String> deviceToGatewayMap, final SpanContext context) {
final Span currentSpan;
final Future<RequestResponseResult<JsonObject>> resultTracker;
if (deviceToGatewayMap.size() == 1) {
// use single entry operation so that traces with device and gateway are created
final Map.Entry<String, String> entry = deviceToGatewayMap.entrySet().iterator().next();
final String deviceId = entry.getKey();
final String gatewayId = entry.getValue();
final Map<String, Object> properties = createDeviceIdProperties(deviceId);
properties.put(MessageHelper.APP_PROPERTY_GATEWAY_ID, gatewayId);
currentSpan = newChildSpan(context, "set last known gateway for device");
TracingHelper.setDeviceTags(currentSpan, tenantId, deviceId);
currentSpan.setTag(MessageHelper.APP_PROPERTY_GATEWAY_ID, gatewayId);
resultTracker = getOrCreateClient(tenantId).compose(client -> client.createAndSendRequest(CommandRouterConstants.CommandRouterAction.SET_LAST_KNOWN_GATEWAY.getSubject(), properties, null, null, this::getRequestResponseResult, currentSpan));
} else {
currentSpan = newChildSpan(context, "set last known gateways for tenant devices");
TracingHelper.setDeviceTags(currentSpan, tenantId, null);
currentSpan.log(Map.of("no_of_entries", deviceToGatewayMap.size()));
final JsonObject payload = new JsonObject();
deviceToGatewayMap.forEach(payload::put);
resultTracker = getOrCreateClient(tenantId).compose(client -> client.createAndSendRequest(CommandRouterConstants.CommandRouterAction.SET_LAST_KNOWN_GATEWAY.getSubject(), null, payload.toBuffer(), MessageHelper.CONTENT_TYPE_APPLICATION_JSON, this::getRequestResponseResult, currentSpan));
}
return mapResultAndFinishSpan(resultTracker, result -> {
switch(result.getStatus()) {
case HttpURLConnection.HTTP_NO_CONTENT:
return null;
default:
throw StatusCodeMapper.from(result);
}
}, currentSpan).onFailure(thr -> log.debug("failed to set last known gateway(s) for tenant [{}]", tenantId, thr)).mapEmpty();
}
use of org.eclipse.hono.util.RequestResponseResult in project hono by eclipse.
the class ProtonBasedCommandRouterClient method registerCommandConsumer.
@Override
public Future<Void> registerCommandConsumer(final String tenantId, final String deviceId, final String adapterInstanceId, final Duration lifespan, final SpanContext context) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(deviceId);
Objects.requireNonNull(adapterInstanceId);
final int lifespanSeconds = lifespan != null && lifespan.getSeconds() <= Integer.MAX_VALUE ? (int) lifespan.getSeconds() : -1;
final Map<String, Object> properties = createDeviceIdProperties(deviceId);
properties.put(MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, adapterInstanceId);
properties.put(MessageHelper.APP_PROPERTY_LIFESPAN, lifespanSeconds);
final Span currentSpan = newChildSpan(context, "register command consumer");
TracingHelper.setDeviceTags(currentSpan, tenantId, deviceId);
currentSpan.setTag(MessageHelper.APP_PROPERTY_ADAPTER_INSTANCE_ID, adapterInstanceId);
currentSpan.setTag(MessageHelper.APP_PROPERTY_LIFESPAN, lifespanSeconds);
final Future<RequestResponseResult<JsonObject>> resultTracker = getOrCreateClient(tenantId).compose(client -> client.createAndSendRequest(CommandRouterConstants.CommandRouterAction.REGISTER_COMMAND_CONSUMER.getSubject(), properties, null, null, this::getRequestResponseResult, currentSpan));
return mapResultAndFinishSpan(resultTracker, result -> {
switch(result.getStatus()) {
case HttpURLConnection.HTTP_NO_CONTENT:
return null;
default:
throw StatusCodeMapper.from(result);
}
}, currentSpan).mapEmpty();
}
Aggregations