Search in sources :

Example 1 with RequestResponseResult

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();
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) CommandRouterConstants(org.eclipse.hono.util.CommandRouterConstants) HashMap(java.util.HashMap) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Tags(io.opentracing.tag.Tags) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) StatusCodeMapper(org.eclipse.hono.client.StatusCodeMapper) Duration(java.time.Duration) Map(java.util.Map) Pair(org.eclipse.hono.util.Pair) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) HonoConnection(org.eclipse.hono.client.HonoConnection) AbstractRequestResponseServiceClient(org.eclipse.hono.client.amqp.AbstractRequestResponseServiceClient) Logger(org.slf4j.Logger) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) RequestResponseClient(org.eclipse.hono.client.amqp.RequestResponseClient) CachingClientFactory(org.eclipse.hono.client.impl.CachingClientFactory) Set(java.util.Set) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Clock(java.time.Clock) Optional(java.util.Optional) Span(io.opentracing.Span) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Span(io.opentracing.Span)

Example 2 with RequestResponseResult

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();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) CommandRouterConstants(org.eclipse.hono.util.CommandRouterConstants) HashMap(java.util.HashMap) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Tags(io.opentracing.tag.Tags) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) StatusCodeMapper(org.eclipse.hono.client.StatusCodeMapper) Duration(java.time.Duration) Map(java.util.Map) Pair(org.eclipse.hono.util.Pair) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) HonoConnection(org.eclipse.hono.client.HonoConnection) AbstractRequestResponseServiceClient(org.eclipse.hono.client.amqp.AbstractRequestResponseServiceClient) Logger(org.slf4j.Logger) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) RequestResponseClient(org.eclipse.hono.client.amqp.RequestResponseClient) CachingClientFactory(org.eclipse.hono.client.impl.CachingClientFactory) Set(java.util.Set) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Clock(java.time.Clock) Optional(java.util.Optional) Span(io.opentracing.Span) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) JsonObject(io.vertx.core.json.JsonObject) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) JsonObject(io.vertx.core.json.JsonObject) Span(io.opentracing.Span) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with RequestResponseResult

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();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) DecodeException(io.vertx.core.json.DecodeException) LoggerFactory(org.slf4j.LoggerFactory) CommandRouterConstants(org.eclipse.hono.util.CommandRouterConstants) HashMap(java.util.HashMap) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Tags(io.opentracing.tag.Tags) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) StatusCodeMapper(org.eclipse.hono.client.StatusCodeMapper) Duration(java.time.Duration) Map(java.util.Map) Pair(org.eclipse.hono.util.Pair) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) HonoConnection(org.eclipse.hono.client.HonoConnection) AbstractRequestResponseServiceClient(org.eclipse.hono.client.amqp.AbstractRequestResponseServiceClient) Logger(org.slf4j.Logger) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) RequestResponseClient(org.eclipse.hono.client.amqp.RequestResponseClient) CachingClientFactory(org.eclipse.hono.client.impl.CachingClientFactory) Set(java.util.Set) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) ApplicationProperties(org.apache.qpid.proton.amqp.messaging.ApplicationProperties) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) CommandRouterClient(org.eclipse.hono.client.command.CommandRouterClient) Clock(java.time.Clock) Optional(java.util.Optional) Span(io.opentracing.Span) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) JsonObject(io.vertx.core.json.JsonObject) RequestResponseResult(org.eclipse.hono.util.RequestResponseResult) Span(io.opentracing.Span)

Aggregations

Span (io.opentracing.Span)3 SpanContext (io.opentracing.SpanContext)3 Tags (io.opentracing.tag.Tags)3 CompositeFuture (io.vertx.core.CompositeFuture)3 Future (io.vertx.core.Future)3 Buffer (io.vertx.core.buffer.Buffer)3 DecodeException (io.vertx.core.json.DecodeException)3 JsonArray (io.vertx.core.json.JsonArray)3 JsonObject (io.vertx.core.json.JsonObject)3 HttpURLConnection (java.net.HttpURLConnection)3 Clock (java.time.Clock)3 Duration (java.time.Duration)3 Instant (java.time.Instant)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3