Search in sources :

Example 6 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedTenantClientTest method addResultToCache.

private Future<AnnotatedCacheKey<?>> addResultToCache(final String tenantId) {
    final TenantResult<TenantObject> tenantResult = TenantResult.from(HttpURLConnection.HTTP_OK, new TenantObject(tenantId, false));
    @SuppressWarnings("unchecked") final ArgumentCaptor<AnnotatedCacheKey<?>> responseCacheKey = ArgumentCaptor.forClass(AnnotatedCacheKey.class);
    when(cache.getIfPresent(responseCacheKey.capture())).thenReturn(tenantResult);
    return client.get(tenantId, null).map(tenantObject -> {
        final AnnotatedCacheKey<?> cacheKey = responseCacheKey.getValue();
        cacheKey.putAttribute("tenant-id", tenantId);
        cacheBackingMap.put(cacheKey, tenantResult);
        return cacheKey;
    });
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey)

Example 7 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedDeviceRegistrationClient method assertRegistration.

@Override
public Future<RegistrationAssertion> assertRegistration(final String tenantId, final String deviceId, final String gatewayId, final SpanContext context) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    final AnnotatedCacheKey<CacheKey> responseCacheKey = new AnnotatedCacheKey<>(new CacheKey(tenantId, deviceId, gatewayId));
    final Span span = newChildSpan(context, "assert Device Registration");
    TracingHelper.setDeviceTags(span, tenantId, deviceId);
    TracingHelper.TAG_GATEWAY_ID.set(span, gatewayId);
    return getResponseFromCache(responseCacheKey, span).recover(t -> getOrCreateClient(tenantId).compose(client -> {
        final Map<String, Object> properties = createDeviceIdProperties(deviceId);
        if (gatewayId != null) {
            properties.put(MessageHelper.APP_PROPERTY_GATEWAY_ID, gatewayId);
        }
        return client.createAndSendRequest(RegistrationConstants.ACTION_ASSERT, properties, null, RegistrationConstants.CONTENT_TYPE_APPLICATION_JSON, this::getRequestResponseResult, span);
    }).map(registrationResult -> {
        addToCache(responseCacheKey, registrationResult);
        return registrationResult;
    })).recover(t -> {
        Tags.HTTP_STATUS.set(span, ServiceInvocationException.extractStatusCode(t));
        TracingHelper.logError(span, t);
        return Future.failedFuture(t);
    }).map(registrationResult -> {
        Tags.HTTP_STATUS.set(span, registrationResult.getStatus());
        if (registrationResult.isError()) {
            Tags.ERROR.set(span, Boolean.TRUE);
        }
        switch(registrationResult.getStatus()) {
            case HttpURLConnection.HTTP_OK:
                final JsonObject payload = registrationResult.getPayload();
                try {
                    return payload.mapTo(RegistrationAssertion.class);
                } catch (final DecodeException e) {
                    if (log.isDebugEnabled()) {
                        log.debug("registration service returned invalid response:{}{}", System.lineSeparator(), payload.encodePrettily());
                    }
                    TracingHelper.logError(span, "registration service returned invalid response", e);
                    throw new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR, "registration service returned invalid response");
                }
            case HttpURLConnection.HTTP_NOT_FOUND:
                throw new ClientErrorException(registrationResult.getStatus(), "device unknown or disabled");
            case HttpURLConnection.HTTP_FORBIDDEN:
                throw new ClientErrorException(registrationResult.getStatus(), "gateway unknown, disabled or not authorized to act on behalf of device");
            default:
                throw StatusCodeMapper.from(registrationResult);
        }
    }).onComplete(o -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) DecodeException(io.vertx.core.json.DecodeException) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) LoggerFactory(org.slf4j.LoggerFactory) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Cache(com.github.benmanes.caffeine.cache.Cache) Constants(org.eclipse.hono.util.Constants) Tags(io.opentracing.tag.Tags) StatusCodeMapper(org.eclipse.hono.client.StatusCodeMapper) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) Map(java.util.Map) RegistrationResult(org.eclipse.hono.util.RegistrationResult) 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) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Logger(org.slf4j.Logger) RequestResponseClient(org.eclipse.hono.client.amqp.RequestResponseClient) CachingClientFactory(org.eclipse.hono.client.impl.CachingClientFactory) ServerErrorException(org.eclipse.hono.client.ServerErrorException) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) 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) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) Buffer(io.vertx.core.buffer.Buffer) Span(io.opentracing.Span) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServerErrorException(org.eclipse.hono.client.ServerErrorException) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) Span(io.opentracing.Span) Map(java.util.Map) DecodeException(io.vertx.core.json.DecodeException) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey)

Example 8 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedDeviceRegistrationClient method removeResultsForDeviceFromCache.

@SuppressWarnings("unchecked")
private void removeResultsForDeviceFromCache(final String tenantId, final String deviceId) {
    removeFromCacheByPattern(key -> {
        final CacheKey cacheKey = ((AnnotatedCacheKey<CacheKey>) key).getKey();
        final boolean tenantMatches = cacheKey.tenantId.equals(tenantId);
        final boolean deviceOrGatewayMatches = cacheKey.deviceId.equals(deviceId) || Objects.equals(cacheKey.gatewayId, deviceId);
        return tenantMatches && deviceOrGatewayMatches;
    });
}
Also used : AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey)

Example 9 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedTenantClient method get.

@Override
public Future<TenantObject> get(final String tenantId, final SpanContext parent) {
    Objects.requireNonNull(tenantId);
    final AnnotatedCacheKey<String> responseCacheKey = new AnnotatedCacheKey<>(tenantId);
    final Span span = newChildSpan(parent, "get Tenant by ID");
    span.setTag(MessageHelper.APP_PROPERTY_TENANT_ID, tenantId);
    return get(responseCacheKey, () -> new JsonObject().put(TenantConstants.FIELD_PAYLOAD_TENANT_ID, tenantId), span);
}
Also used : JsonObject(io.vertx.core.json.JsonObject) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) Span(io.opentracing.Span)

Example 10 with AnnotatedCacheKey

use of org.eclipse.hono.client.util.AnnotatedCacheKey in project hono by eclipse.

the class ProtonBasedTenantClient method get.

@Override
public Future<TenantObject> get(final X500Principal subjectDn, final SpanContext parent) {
    Objects.requireNonNull(subjectDn);
    final String subjectDnRfc2253 = subjectDn.getName(X500Principal.RFC2253);
    final AnnotatedCacheKey<X500Principal> responseCacheKey = new AnnotatedCacheKey<>(subjectDn);
    final Span span = newChildSpan(parent, "get Tenant by subject DN");
    TAG_SUBJECT_DN.set(span, subjectDnRfc2253);
    return get(responseCacheKey, () -> new JsonObject().put(TenantConstants.FIELD_PAYLOAD_SUBJECT_DN, subjectDnRfc2253), span);
}
Also used : X500Principal(javax.security.auth.x500.X500Principal) JsonObject(io.vertx.core.json.JsonObject) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) Span(io.opentracing.Span)

Aggregations

AnnotatedCacheKey (org.eclipse.hono.client.util.AnnotatedCacheKey)14 JsonObject (io.vertx.core.json.JsonObject)12 Span (io.opentracing.Span)10 Cache (com.github.benmanes.caffeine.cache.Cache)8 Future (io.vertx.core.Future)8 HttpURLConnection (java.net.HttpURLConnection)8 HonoConnection (org.eclipse.hono.client.HonoConnection)8 SendMessageSampler (org.eclipse.hono.client.SendMessageSampler)8 NotificationEventBusSupport (org.eclipse.hono.notification.NotificationEventBusSupport)8 AllDevicesOfTenantDeletedNotification (org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification)8 DeviceChangeNotification (org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)7 LifecycleChange (org.eclipse.hono.notification.deviceregistry.LifecycleChange)7 CacheDirective (org.eclipse.hono.util.CacheDirective)7 Constants (org.eclipse.hono.util.Constants)7 MessageHelper (org.eclipse.hono.util.MessageHelper)7 Truth.assertThat (com.google.common.truth.Truth.assertThat)6 Tracer (io.opentracing.Tracer)6 Handler (io.vertx.core.Handler)6 Vertx (io.vertx.core.Vertx)6 EventBus (io.vertx.core.eventbus.EventBus)6