/** * Adds message properties based on a device's registration information. * <p> * This methods simply invokes {@link #addProperties(Message, JsonObject, boolean)} with * with {@code true} as the value for the regAssertionRequired parameter. * * @param message The message to set the properties on. * @param registrationInfo The values to set. * @throws NullPointerException if any of the parameters is {@code null}. */ protected final void addProperties( final Message message, final JsonObject registrationInfo) { addProperties(message, registrationInfo, true); }
private Future<?> closeServiceClients() { return CompositeFuture.all( closeServiceClient(messagingClient), closeServiceClient(commandConnection), closeServiceClient(tenantServiceClient), closeServiceClient(registrationServiceClient), closeServiceClient(credentialsServiceClient)); }
@Override protected final Future<Void> stopInternal() { LOG.info("stopping protocol adapter"); final Future<Void> result = Future.future(); final Future<Void> doStopResult = Future.future(); doStop(doStopResult); doStopResult .compose(s -> closeServiceClients()) .recover(t -> { LOG.info("error while stopping protocol adapter", t); return Future.failedFuture(t); }).compose(s -> { result.complete(); LOG.info("successfully stopped protocol adapter"); }, result); return result; }
if (Strings.isNullOrEmpty(getTypeName())) { result.fail(new IllegalStateException("adapter does not define a typeName")); } else if (tenantServiceClient == null) { result.fail(new IllegalStateException("Command & Control service client must be set")); } else { connectToService(tenantServiceClient, "Tenant service"); connectToService(messagingClient, "AMQP Messaging Network"); connectToService(registrationServiceClient, "Device Registration service"); connectToService(credentialsServiceClient, "Credentials service"); connectToService( commandConnection, "Command and Control service", this::onCommandConnectionEstablished, this::onCommandConnectionLost); doStart(result);
Objects.requireNonNull(ttd); final Future<JsonObject> tokenTracker = getRegistrationAssertion(tenant, deviceId, authenticatedDevice, context); final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant, context); final Future<MessageSender> senderTracker = getEventSender(tenant); if (tenantConfigTracker.result().isAdapterEnabled(getTypeName())) { final MessageSender sender = senderTracker.result(); final Message msg = newMessage( ResourceIdentifier.from(EventConstants.EVENT_ENDPOINT, tenant, deviceId), senderTracker.result().isRegistrationAssertionRequired(),
/** * Checks whether a given device is registered and enabled. * * @param device The device to check. * @param context The currently active OpenTracing span that is used to * trace the retrieval of the assertion or {@code null} * if no span is currently active. * @return A future indicating the outcome. * The future will be succeeded if the device is registered and enabled. * Otherwise, the future will be failed with a {@link ServiceInvocationException}. */ protected final Future<Void> checkDeviceRegistration(final Device device, final SpanContext context) { Objects.requireNonNull(device); return getRegistrationAssertion( device.getTenantId(), device.getDeviceId(), null, context).map(assertion -> null); }
MessageHelper.addRegistrationAssertion(message, registrationAssertion); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_ORIG_ADAPTER, getTypeName()); if (getConfig().isDefaultsEnabled()) { final JsonObject defaults = registrationInfo.getJsonObject(RegistrationConstants.FIELD_DEFAULTS); if (defaults != null) { addDefaults(message, defaults); if (getConfig().isJmsVendorPropsEnabled()) { MessageHelper.addJmsVendorProperties(message);
/** * Connects to a Hono Service component using the configured client. * * @param client The Hono client for the service that is to be connected. * @param serviceName The name of the service that is to be connected (used for logging). * @return A future that will succeed once the connection has been established. The future will fail if the * connection cannot be established. * @throws NullPointerException if serviceName is {@code null}. * @throws IllegalArgumentException if client is {@code null}. */ protected final Future<HonoClient> connectToService(final HonoClient client, final String serviceName) { return connectToService(client, serviceName, onConnect -> {}, onConnectionLost -> {}); }
/** * Closes a command consumer for a device. * <p> * If no command consumer for the device is open, this method does nothing. * * @param tenantId The tenant that the device belongs to. * @param deviceId The identifier of the device. */ protected final void closeCommandConsumer(final String tenantId, final String deviceId) { getCommandConnection().closeCommandConsumer(tenantId, deviceId).otherwise(t -> { LOG.warn("cannot close command consumer [tenant-id: {}, device-id: {}]: {}", tenantId, deviceId, t.getMessage()); return null; }); }
Objects.requireNonNull(response); final Future<CommandResponseSender> senderTracker = createCommandResponseSender(tenantId, response.getReplyToId()); return senderTracker .compose(sender -> sender.sendCommandResponse(response, context))
Objects.requireNonNull(ttd); final Future<JsonObject> tokenTracker = getRegistrationAssertion(tenant, deviceId, authenticatedDevice, context); final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant, context); final Future<MessageSender> senderTracker = getEventSender(tenant); if (tenantConfigTracker.result().isAdapterEnabled(getTypeName())) { final MessageSender sender = senderTracker.result(); final Message msg = newMessage( ResourceIdentifier.from(EventConstants.EVENT_ENDPOINT, tenant, deviceId), senderTracker.result().isRegistrationAssertionRequired(),
/** * Checks whether a given device is registered and enabled. * * @param device The device to check. * @param context The currently active OpenTracing span that is used to * trace the retrieval of the assertion or {@code null} * if no span is currently active. * @return A future indicating the outcome. * The future will be succeeded if the device is registered and enabled. * Otherwise, the future will be failed with a {@link ServiceInvocationException}. */ protected final Future<Void> checkDeviceRegistration(final Device device, final SpanContext context) { Objects.requireNonNull(device); return getRegistrationAssertion( device.getTenantId(), device.getDeviceId(), null, context).map(assertion -> null); }
if (Strings.isNullOrEmpty(getTypeName())) { result.fail(new IllegalStateException("adapter does not define a typeName")); } else if (tenantServiceClient == null) { result.fail(new IllegalStateException("Command & Control service client must be set")); } else { connectToService(tenantServiceClient, "Tenant service"); connectToService(messagingClient, "AMQP Messaging Network"); connectToService(registrationServiceClient, "Device Registration service"); connectToService(credentialsServiceClient, "Credentials service"); connectToService( commandConnection, "Command and Control service", this::onCommandConnectionEstablished, this::onCommandConnectionLost); doStart(result);
MessageHelper.addRegistrationAssertion(message, registrationAssertion); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_ORIG_ADAPTER, getTypeName()); if (getConfig().isDefaultsEnabled()) { final JsonObject defaults = registrationInfo.getJsonObject(RegistrationConstants.FIELD_DEFAULTS); if (defaults != null) { addDefaults(message, defaults); if (getConfig().isJmsVendorPropsEnabled()) { MessageHelper.addJmsVendorProperties(message);
/** * Connects to a Hono Service component using the configured client. * * @param client The Hono client for the service that is to be connected. * @param serviceName The name of the service that is to be connected (used for logging). * @return A future that will succeed once the connection has been established. The future will fail if the * connection cannot be established. * @throws NullPointerException if serviceName is {@code null}. * @throws IllegalArgumentException if client is {@code null}. */ protected final Future<HonoClient> connectToService(final HonoClient client, final String serviceName) { return connectToService(client, serviceName, onConnect -> {}, onConnectionLost -> {}); }
/** * Closes a command consumer for a device. * <p> * If no command consumer for the device is open, this method does nothing. * * @param tenantId The tenant that the device belongs to. * @param deviceId The identifier of the device. */ protected final void closeCommandConsumer(final String tenantId, final String deviceId) { getCommandConnection().closeCommandConsumer(tenantId, deviceId).otherwise(t -> { LOG.warn("cannot close command consumer [tenant-id: {}, device-id: {}]: {}", tenantId, deviceId, t.getMessage()); return null; }); }
Objects.requireNonNull(response); final Future<CommandResponseSender> senderTracker = createCommandResponseSender(tenantId, response.getReplyToId()); return senderTracker .compose(sender -> sender.sendCommandResponse(response, context))
/** * Adds message properties based on a device's registration information. * <p> * This methods simply invokes {@link #addProperties(Message, JsonObject, boolean)} with * with {@code true} as the value for the regAssertionRequired parameter. * * @param message The message to set the properties on. * @param registrationInfo The values to set. * @throws NullPointerException if any of the parameters is {@code null}. */ protected final void addProperties( final Message message, final JsonObject registrationInfo) { addProperties(message, registrationInfo, true); }
/** * Verifies that the adapter successfully retrieves a registration assertion * for an existing device. * * @param ctx The vert.x test context. */ @Test public void testGetRegistrationAssertionSucceedsForExistingDevice(final TestContext ctx) { // GIVEN an adapter connected to a registration service final JsonObject assertionResult = newRegistrationAssertionResult("token"); when(registrationClient.assertRegistration(eq("device"), any())).thenReturn(Future.succeededFuture(assertionResult)); when(registrationClient.assertRegistration(eq("device"), any(), any())).thenReturn(Future.succeededFuture(assertionResult)); // WHEN an assertion for the device is retrieved adapter.getRegistrationAssertion("tenant", "device", null).setHandler(ctx.asyncAssertSuccess(result -> { // THEN the result contains the registration assertion ctx.assertEquals(assertionResult, result); })); adapter.getRegistrationAssertion("tenant", "device", null, mock(SpanContext.class)).setHandler(ctx.asyncAssertSuccess(result -> { // THEN the result contains the registration assertion ctx.assertEquals(assertionResult, result); })); }
@Override protected final Future<Void> stopInternal() { LOG.info("stopping protocol adapter"); final Future<Void> result = Future.future(); final Future<Void> doStopResult = Future.future(); doStop(doStopResult); doStopResult .compose(s -> closeServiceClients()) .recover(t -> { LOG.info("error while stopping protocol adapter", t); return Future.failedFuture(t); }).compose(s -> { result.complete(); LOG.info("successfully stopped protocol adapter"); }, result); return result; }