use of io.vertx.core.Promise in project hono by eclipse.
the class AmqpServiceBase method startSecureServer.
private Future<Void> startSecureServer() {
if (isSecurePortEnabled()) {
final int securePort = determineSecurePort();
final Promise<Void> result = Promise.promise();
final ProtonServerOptions options = createServerOptions();
server = createProtonServer(options).connectHandler(this::onRemoteConnectionOpen).listen(securePort, getConfig().getBindAddress(), bindAttempt -> {
if (bindAttempt.succeeded()) {
if (getPort() == getPortDefaultValue()) {
log.info("server listens on standard secure port [{}:{}]", getBindAddress(), getPort());
} else {
log.warn("server listens on non-standard secure port [{}:{}], default is {}", getBindAddress(), getPort(), getPortDefaultValue());
}
result.complete();
} else {
log.error("cannot bind to secure port", bindAttempt.cause());
result.fail(bindAttempt.cause());
}
});
return result.future();
} else {
log.info("secure port is not enabled");
return Future.succeededFuture();
}
}
use of io.vertx.core.Promise in project hono by eclipse.
the class Application method doStart.
@Override
protected void doStart() {
if (!(authenticationService instanceof Verticle)) {
throw new IllegalStateException("Authentication service must be a vert.x Verticle");
}
LOG.info("adding common tags to meter registry");
meterRegistry.config().commonTags(MetricsTags.forService(Constants.SERVICE_NAME_DEVICE_REGISTRY));
LOG.info("deploying {} {} instances ...", appConfig.getMaxInstances(), getComponentName());
final CompletableFuture<Void> startup = new CompletableFuture<>();
// deploy authentication service (once only)
final Promise<String> authServiceDeploymentTracker = Promise.promise();
vertx.deployVerticle((Verticle) authenticationService, authServiceDeploymentTracker);
// deploy notification sender (once only)
final Promise<String> notificationSenderDeploymentTracker = Promise.promise();
vertx.deployVerticle(new WrappedLifecycleComponentVerticle(notificationSender), notificationSenderDeploymentTracker);
// deploy AMQP 1.0 server
final Promise<String> amqpServerDeploymentTracker = Promise.promise();
vertx.deployVerticle(() -> amqpServerFactory.newServer(), new DeploymentOptions().setInstances(appConfig.getMaxInstances()), amqpServerDeploymentTracker);
// deploy HTTP server
final Promise<String> httpServerDeploymentTracker = Promise.promise();
vertx.deployVerticle(() -> httpServerFactory.newServer(), new DeploymentOptions().setInstances(appConfig.getMaxInstances()), httpServerDeploymentTracker);
CompositeFuture.all(authServiceDeploymentTracker.future(), notificationSenderDeploymentTracker.future(), amqpServerDeploymentTracker.future(), httpServerDeploymentTracker.future()).onSuccess(ok -> registerHealthCheckProvider(authenticationService)).compose(s -> healthCheckServer.start()).onSuccess(ok -> startup.complete(null)).onFailure(t -> startup.completeExceptionally(t));
startup.join();
}
use of io.vertx.core.Promise in project hono by eclipse.
the class AmqpUploadTestBase method testUploadMessages.
// ------------------------------------------< private methods >---
private void testUploadMessages(final String tenantId, final ProtonQoS senderQoS) throws InterruptedException {
final VertxTestContext messageSending = new VertxTestContext();
final Function<Handler<Void>, Future<Void>> receiver = callback -> {
return createConsumer(tenantId, msg -> {
if (log.isTraceEnabled()) {
log.trace("received message [{}]: {}", msg.getContentType(), msg.getPayload().toString());
}
messageSending.verify(() -> {
DownstreamMessageAssertions.assertTelemetryMessageProperties(msg, tenantId);
assertThat(msg.getQos()).isEqualTo(AmqpUploadTestBase.getQoS(senderQoS));
assertAdditionalMessageProperties(msg);
callback.handle(null);
});
}).mapEmpty();
};
doUploadMessages(messageSending, receiver, payload -> {
final Message msg = ProtonHelper.message();
MessageHelper.setPayload(msg, "opaque/binary", payload);
msg.setAddress(getEndpointName());
final Promise<Void> sendingComplete = Promise.promise();
final Handler<ProtonSender> sendMsgHandler = replenishedSender -> {
replenishedSender.sendQueueDrainHandler(null);
switch(senderQoS) {
case AT_LEAST_ONCE:
replenishedSender.send(msg, delivery -> {
if (Accepted.class.isInstance(delivery.getRemoteState())) {
sendingComplete.complete();
} else {
sendingComplete.fail(AmqpErrorException.from(delivery.getRemoteState()));
}
});
break;
case AT_MOST_ONCE:
replenishedSender.send(msg);
sendingComplete.complete();
break;
}
};
context.runOnContext(go -> {
if (sender.getCredit() <= 0) {
log.trace("wait for credit ...");
sender.sendQueueDrainHandler(sendMsgHandler);
} else {
sendMsgHandler.handle(sender);
}
});
return sendingComplete.future();
});
}
use of io.vertx.core.Promise in project hono by eclipse.
the class AmqpUploadTestBase method testAutoProvisioningViaGateway.
/**
* Verifies that an edge device is auto-provisioned if it connects via a gateway equipped with the corresponding
* authority.
*
* @param ctx The Vert.x test context.
*/
@Test
@Timeout(timeUnit = TimeUnit.SECONDS, value = 10)
public void testAutoProvisioningViaGateway(final VertxTestContext ctx) {
final String tenantId = helper.getRandomTenantId();
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device gateway = new Device().setAuthorities(Collections.singleton(RegistryManagementConstants.AUTHORITY_AUTO_PROVISIONING_ENABLED));
final String username = IntegrationTestSupport.getUsername(gatewayId, tenantId);
final String edgeDeviceId = helper.getRandomDeviceId(tenantId);
final Promise<Void> provisioningNotificationReceived = Promise.promise();
helper.createAutoProvisioningMessageConsumers(ctx, provisioningNotificationReceived, tenantId, edgeDeviceId).compose(ok -> helper.registry.addDeviceForTenant(tenantId, new Tenant(), gatewayId, gateway, DEVICE_PASSWORD)).compose(ok -> connectToAdapter(username, DEVICE_PASSWORD)).compose(con -> createProducer(null, ProtonQoS.AT_LEAST_ONCE)).compose(sender -> {
final Message msg = ProtonHelper.message("apFoobar");
msg.setContentType("text/plain");
msg.setAddress(String.format("%s/%s/%s", getEndpointName(), tenantId, edgeDeviceId));
final Promise<Void> result = Promise.promise();
sender.send(msg, delivery -> {
ctx.verify(() -> assertThat(delivery.getRemoteState()).isInstanceOf(Accepted.class));
result.complete();
});
return result.future();
}).compose(ok -> provisioningNotificationReceived.future()).compose(ok -> helper.registry.getRegistrationInfo(tenantId, edgeDeviceId)).onComplete(ctx.succeeding(registrationResult -> {
ctx.verify(() -> {
final var info = registrationResult.bodyAsJsonObject();
IntegrationTestSupport.assertDeviceStatusProperties(info.getJsonObject(RegistryManagementConstants.FIELD_STATUS), true);
});
ctx.completeNow();
}));
}
use of io.vertx.core.Promise in project hono by eclipse.
the class IntegrationTestSupport method createAutoProvisioningMessageConsumers.
/**
* Create an event and a telemetry consumer which verify that at least one empty notification and at
* least one further message, be it an event or telemetry, was received according to the specification
* of gateway-based auto-provisioning.
*
* @param ctx The test context to fail if the notification event does not contain all required properties.
* @param provisioningNotificationReceived The promise to complete, once the provisioning notification has been
* received.
* @param tenantId The tenant for which the consumer should be created.
* @param deviceId The id of the device which sent the messages.
*
* @return A succeeded future if the message consumers have been created successfully.
*/
public Future<Void> createAutoProvisioningMessageConsumers(final VertxTestContext ctx, final Promise<Void> provisioningNotificationReceived, final String tenantId, final String deviceId) {
final Checkpoint messagesReceived = ctx.checkpoint(2);
return applicationClient.createEventConsumer(tenantId, msg -> {
ctx.verify(() -> {
assertThat(msg.getDeviceId()).isEqualTo(deviceId);
if (msg.getContentType().equals(EventConstants.CONTENT_TYPE_DEVICE_PROVISIONING_NOTIFICATION)) {
assertThat(msg.getTenantId()).isEqualTo(tenantId);
assertThat(getRegistrationStatus(msg)).isEqualTo(EventConstants.RegistrationStatus.NEW.name());
messagesReceived.flag();
provisioningNotificationReceived.complete();
} else {
messagesReceived.flag();
}
});
}, close -> {
}).compose(ok -> applicationClient.createTelemetryConsumer(tenantId, msg -> {
ctx.verify(() -> {
if (!msg.getContentType().equals(EventConstants.CONTENT_TYPE_DEVICE_PROVISIONING_NOTIFICATION)) {
messagesReceived.flag();
}
});
}, close -> {
})).mapEmpty();
}
Aggregations