Search in sources :

Example 66 with Span

use of io.opentracing.Span in project hono by eclipse.

the class AbstractTenantManagementServiceTest method testNotificationOnDeleteTenant.

/**
 * Verifies that {@link AbstractTenantManagementService#deleteTenant(String, Optional, Span)} publishes the expected
 * notification.
 *
 * @param context The vert.x test context.
 */
@Test
public void testNotificationOnDeleteTenant(final VertxTestContext context) {
    final var notificationArgumentCaptor = ArgumentCaptor.forClass(TenantChangeNotification.class);
    tenantManagementService.createTenant(Optional.of(DEFAULT_TENANT_ID), new Tenant(), SPAN).compose(result -> tenantManagementService.deleteTenant(DEFAULT_TENANT_ID, Optional.empty(), SPAN)).onComplete(context.succeeding(result -> {
        context.verify(() -> {
            verify(eventBus, times(2)).publish(eq(NotificationEventBusSupport.getEventBusAddress(TenantChangeNotification.TYPE)), notificationArgumentCaptor.capture(), any());
            assertThat(notificationArgumentCaptor.getAllValues().size()).isEqualTo(2);
            final var notification = notificationArgumentCaptor.getValue();
            assertThat(notification).isNotNull();
            assertThat(notification.getChange()).isEqualTo(LifecycleChange.DELETE);
            assertThat(notification.getTenantId()).isEqualTo(DEFAULT_TENANT_ID);
            assertThat(notification.getCreationTime()).isNotNull();
            assertThat(notification.isEnabled()).isFalse();
        });
        context.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Tenant(org.eclipse.hono.service.management.tenant.Tenant) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) NoopSpan(io.opentracing.noop.NoopSpan) Id(org.eclipse.hono.service.management.Id) Mockito.mock(org.mockito.Mockito.mock) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Test(org.junit.jupiter.api.Test)

Example 67 with Span

use of io.opentracing.Span in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doSearchDevices.

private void doSearchDevices(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_SEARCH_DEVICES, getClass().getSimpleName()).start();
    final String tenantId = getTenantParam(ctx);
    final Future<Integer> pageSize = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_SIZE, DEFAULT_PAGE_SIZE, CONVERTER_INT, value -> value >= MIN_PAGE_SIZE && value <= MAX_PAGE_SIZE);
    final Future<Integer> pageOffset = getRequestParameter(ctx, RegistryManagementConstants.PARAM_PAGE_OFFSET, DEFAULT_PAGE_OFFSET, CONVERTER_INT, value -> value >= MIN_PAGE_OFFSET);
    final Future<List<Filter>> filters = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_FILTER_JSON, Filter.class);
    final Future<List<Sort>> sortOptions = decodeJsonFromRequestParameter(ctx, RegistryManagementConstants.PARAM_SORT_JSON, Sort.class);
    CompositeFuture.all(pageSize, pageOffset, filters, sortOptions).onSuccess(ok -> TracingHelper.TAG_TENANT_ID.set(span, tenantId)).compose(ok -> getService().searchDevices(tenantId, pageSize.result(), pageOffset.result(), filters.result(), sortOptions.result(), span)).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) List(java.util.List) Span(io.opentracing.Span)

Example 68 with Span

use of io.opentracing.Span in project hono by eclipse.

the class DelegatingDeviceManagementHttpEndpoint method doCreateDevice.

private void doCreateDevice(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_CREATE_DEVICE, getClass().getSimpleName()).start();
    final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
    final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), true));
    final Future<Device> device = fromPayload(ctx);
    CompositeFuture.all(tenantId, deviceId, device).compose(ok -> {
        final Optional<String> did = Optional.ofNullable(deviceId.result());
        TracingHelper.TAG_TENANT_ID.set(span, tenantId.result());
        did.ifPresent(s -> TracingHelper.TAG_DEVICE_ID.set(span, s));
        logger.debug("creating device [tenant: {}, device-id: {}]", tenantId.result(), did.orElse("<auto>"));
        return getService().createDevice(tenantId.result(), did, device.result(), span);
    }).onSuccess(operationResult -> writeResponse(ctx, operationResult, (responseHeaders, status) -> {
        Optional.ofNullable(operationResult.getPayload()).map(Id::getId).ifPresent(id -> responseHeaders.set(HttpHeaders.LOCATION, String.format("/%s/%s/%s", getName(), tenantId.result(), id)));
    }, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Filter(org.eclipse.hono.service.management.Filter) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) Sort(org.eclipse.hono.service.management.Sort) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) Span(io.opentracing.Span) Id(org.eclipse.hono.service.management.Id) Optional(java.util.Optional) Id(org.eclipse.hono.service.management.Id) Span(io.opentracing.Span)

Example 69 with Span

use of io.opentracing.Span in project hono by eclipse.

the class DelegatingCredentialsManagementHttpEndpoint method updateCredentials.

private void updateCredentials(final RoutingContext ctx) {
    final Span span = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx), SPAN_NAME_UPDATE_CREDENTIALS, getClass().getSimpleName()).start();
    final Future<String> tenantId = getRequestParameter(ctx, PARAM_TENANT_ID, getPredicate(config.getTenantIdPattern(), false));
    final Future<String> deviceId = getRequestParameter(ctx, PARAM_DEVICE_ID, getPredicate(config.getDeviceIdPattern(), false));
    final Future<List<CommonCredential>> updatedCredentials = fromPayload(ctx);
    CompositeFuture.all(tenantId, deviceId, updatedCredentials).compose(ok -> {
        TracingHelper.setDeviceTags(span, tenantId.result(), deviceId.result());
        logger.debug("updating {} credentials [tenant: {}, device-id: {}]", updatedCredentials.result().size(), tenantId.result(), deviceId.result());
        final Optional<String> resourceVersion = Optional.ofNullable(ctx.get(KEY_RESOURCE_VERSION));
        return getService().updateCredentials(tenantId.result(), deviceId.result(), updatedCredentials.result(), resourceVersion, span);
    }).onSuccess(operationResult -> writeResponse(ctx, operationResult, span)).onFailure(t -> failRequest(ctx, t, span)).onComplete(s -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DecodeException(io.vertx.core.json.DecodeException) AbstractDelegatingRegistryHttpEndpoint(org.eclipse.hono.service.management.AbstractDelegatingRegistryHttpEndpoint) Router(io.vertx.ext.web.Router) ClientErrorException(org.eclipse.hono.client.ClientErrorException) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) TracingHandler(org.eclipse.hono.service.http.TracingHandler) CompositeFuture(io.vertx.core.CompositeFuture) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Set(java.util.Set) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) HttpMethod(io.vertx.core.http.HttpMethod) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) Optional(java.util.Optional) List(java.util.List) Span(io.opentracing.Span)

Example 70 with Span

use of io.opentracing.Span in project hono by eclipse.

the class AbstractDeviceManagementService method deleteDevice.

@Override
public final Future<Result<Void>> deleteDevice(final String tenantId, final String deviceId, final Optional<String> resourceVersion, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(resourceVersion);
    Objects.requireNonNull(span);
    return this.tenantInformationService.tenantExists(tenantId, span).otherwise(t -> Result.from(ServiceInvocationException.extractStatusCode(t))).compose(result -> {
        switch(result.getStatus()) {
            case HttpURLConnection.HTTP_OK:
                break;
            case HttpURLConnection.HTTP_NOT_FOUND:
                span.log("tenant does not exist (anymore)");
                LOG.info("trying to delete device of non-existing tenant [tenant-id: {}, device-id: {}]", tenantId, deviceId);
                break;
            default:
                span.log(Map.of(Fields.EVENT, "could not determine tenant status", Tags.HTTP_STATUS.getKey(), result.getStatus()));
                LOG.info("could not determine tenant status [tenant-id: {}, code: {}]", tenantId, result.getStatus());
        }
        return processDeleteDevice(DeviceKey.from(tenantId, deviceId), resourceVersion, span);
    }).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new DeviceChangeNotification(LifecycleChange.DELETE, tenantId, deviceId, Instant.now(), false))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Tags(io.opentracing.tag.Tags) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) Sort(org.eclipse.hono.service.management.Sort) SearchResult(org.eclipse.hono.service.management.SearchResult) Map(java.util.Map) Fields(io.opentracing.log.Fields) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) NoopTenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.NoopTenantInformationService) Device(org.eclipse.hono.service.management.device.Device) DeviceWithId(org.eclipse.hono.service.management.device.DeviceWithId) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) UUID(java.util.UUID) Instant(java.time.Instant) Result(org.eclipse.hono.service.management.Result) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Id(org.eclipse.hono.service.management.Id) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)

Aggregations

Span (io.opentracing.Span)370 Future (io.vertx.core.Future)182 HttpURLConnection (java.net.HttpURLConnection)174 Tracer (io.opentracing.Tracer)123 JsonObject (io.vertx.core.json.JsonObject)119 ClientErrorException (org.eclipse.hono.client.ClientErrorException)117 Map (java.util.Map)116 SpanContext (io.opentracing.SpanContext)115 Objects (java.util.Objects)109 List (java.util.List)104 TracingHelper (org.eclipse.hono.tracing.TracingHelper)104 Optional (java.util.Optional)102 Promise (io.vertx.core.Promise)98 Test (org.junit.jupiter.api.Test)96 MessageHelper (org.eclipse.hono.util.MessageHelper)89 Vertx (io.vertx.core.Vertx)84 Constants (org.eclipse.hono.util.Constants)82 ServerErrorException (org.eclipse.hono.client.ServerErrorException)80 Truth.assertThat (com.google.common.truth.Truth.assertThat)79 Mockito.mock (org.mockito.Mockito.mock)79