/** * Trigger the creation of a <em>connected</em> event. * * @param remoteId The remote ID. * @param authenticatedDevice The (optional) authenticated device. * @return A failed future if an event producer is set but the event could not be published. Otherwise, a succeeded * event. * @see ConnectionEventProducer * @see ConnectionEventProducer#connected(ConnectionEventProducer.Context, String, String, Device, JsonObject) */ protected Future<?> sendConnectedEvent(final String remoteId, final Device authenticatedDevice) { if (this.connectionEventProducer != null) { return this.connectionEventProducer.connected(connectionEventProducerContext, remoteId, getTypeName(), authenticatedDevice, null); } else { return Future.succeededFuture(); } }
/** * Trigger the creation of a <em>connected</em> event. * * @param remoteId The remote ID. * @param authenticatedDevice The (optional) authenticated device. * @return A failed future if an event producer is set but the event could not be published. Otherwise, a succeeded * event. * @see ConnectionEventProducer * @see ConnectionEventProducer#connected(ConnectionEventProducer.Context, String, String, Device, JsonObject) */ protected Future<?> sendConnectedEvent(final String remoteId, final Device authenticatedDevice) { if (this.connectionEventProducer != null) { return this.connectionEventProducer.connected(connectionEventProducerContext, remoteId, getTypeName(), authenticatedDevice, null); } else { return Future.succeededFuture(); } }
/** * Trigger the creation of a <em>disconnected</em> event. * * @param remoteId The remote ID. * @param authenticatedDevice The (optional) authenticated device. * @return A failed future if an event producer is set but the event could not be published. Otherwise, a succeeded * event. * @see ConnectionEventProducer * @see ConnectionEventProducer#disconnected(ConnectionEventProducer.Context, String, String, Device, JsonObject) */ protected Future<?> sendDisconnectedEvent(final String remoteId, final Device authenticatedDevice) { if (this.connectionEventProducer != null) { return this.connectionEventProducer.disconnected(connectionEventProducerContext, remoteId, getTypeName(), authenticatedDevice, null); } else { return Future.succeededFuture(); } }
/** * Trigger the creation of a <em>disconnected</em> event. * * @param remoteId The remote ID. * @param authenticatedDevice The (optional) authenticated device. * @return A failed future if an event producer is set but the event could not be published. Otherwise, a succeeded * event. * @see ConnectionEventProducer * @see ConnectionEventProducer#disconnected(ConnectionEventProducer.Context, String, String, Device, JsonObject) */ protected Future<?> sendDisconnectedEvent(final String remoteId, final Device authenticatedDevice) { if (this.connectionEventProducer != null) { return this.connectionEventProducer.disconnected(connectionEventProducerContext, remoteId, getTypeName(), authenticatedDevice, null); } else { return Future.succeededFuture(); } }
/** * Gets the number of seconds after which this protocol adapter should * give up waiting for an upstream command for a device of a given tenant. * <p> * Protocol adapters may override this method to e.g. use a static value * for all tenants. * * @param tenant The tenant that the device belongs to. * @param deviceTtd The TTD value provided by the device. * @return A succeeded future that contains {@code null} if device TTD is {@code null}, * or otherwise the lesser of device TTD and the value returned by * {@link TenantObject#getMaxTimeUntilDisconnect(String)}. * @throws NullPointerException if tenant is {@code null}. */ protected Future<Integer> getTimeUntilDisconnect(final TenantObject tenant, final Integer deviceTtd) { Objects.requireNonNull(tenant); if (deviceTtd == null) { return Future.succeededFuture(); } else { return Future.succeededFuture(Math.min(tenant.getMaxTimeUntilDisconnect(getTypeName()), deviceTtd)); } }
/** * Gets the number of seconds after which this protocol adapter should * give up waiting for an upstream command for a device of a given tenant. * <p> * Protocol adapters may override this method to e.g. use a static value * for all tenants. * * @param tenant The tenant that the device belongs to. * @param deviceTtd The TTD value provided by the device. * @return A succeeded future that contains {@code null} if device TTD is {@code null}, * or otherwise the lesser of device TTD and the value returned by * {@link TenantObject#getMaxTimeUntilDisconnect(String)}. * @throws NullPointerException if tenant is {@code null}. */ protected Future<Integer> getTimeUntilDisconnect(final TenantObject tenant, final Integer deviceTtd) { Objects.requireNonNull(tenant); if (deviceTtd == null) { return Future.succeededFuture(); } else { return Future.succeededFuture(Math.min(tenant.getMaxTimeUntilDisconnect(getTypeName()), deviceTtd)); } }
/** * Checks if this adapter is enabled for a given tenant. * * @param tenantConfig The tenant to check for. * @return A succeeded future if this adapter is enabled for the tenant. * Otherwise the future will be failed with a {@link ClientErrorException}. */ protected final Future<TenantObject> isAdapterEnabled(final TenantObject tenantConfig) { if (tenantConfig.isAdapterEnabled(getTypeName())) { LOG.debug("protocol adapter [{}] is enabled for tenant [{}]", getTypeName(), tenantConfig.getTenantId()); return Future.succeededFuture(tenantConfig); } else { LOG.debug("protocol adapter [{}] is disabled for tenant [{}]", getTypeName(), tenantConfig.getTenantId()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "adapter disabled for tenant")); } }
/** * Checks if this adapter is enabled for a given tenant. * * @param tenantConfig The tenant to check for. * @return A succeeded future if this adapter is enabled for the tenant. * Otherwise the future will be failed with a {@link ClientErrorException}. */ protected final Future<TenantObject> isAdapterEnabled(final TenantObject tenantConfig) { if (tenantConfig.isAdapterEnabled(getTypeName())) { LOG.debug("protocol adapter [{}] is enabled for tenant [{}]", getTypeName(), tenantConfig.getTenantId()); return Future.succeededFuture(tenantConfig); } else { LOG.debug("protocol adapter [{}] is disabled for tenant [{}]", getTypeName(), tenantConfig.getTenantId()); return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN, "adapter disabled for tenant")); } }
/** * Creates a command consumer for a specific device. * * @param tenantId The tenant of the command receiver. * @param deviceId The device of the command receiver. * @param commandConsumer The handler to invoke for each command destined to the device. * @param closeHandler Called when the peer detaches the link. * @return Result of the receiver creation. */ protected final Future<MessageConsumer> createCommandConsumer( final String tenantId, final String deviceId, final Handler<CommandContext> commandConsumer, final Handler<Void> closeHandler) { return commandConnection.createCommandConsumer( tenantId, deviceId, commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); commandConsumer.handle(commandContext); }, closeHandler); }
/** * Creates a command consumer for a specific device. * * @param tenantId The tenant of the command receiver. * @param deviceId The device of the command receiver. * @param commandConsumer The handler to invoke for each command destined to the device. * @param closeHandler Called when the peer detaches the link. * @return Result of the receiver creation. */ protected final Future<MessageConsumer> createCommandConsumer( final String tenantId, final String deviceId, final Handler<CommandContext> commandConsumer, final Handler<Void> closeHandler) { return commandConnection.createCommandConsumer( tenantId, deviceId, commandContext -> { Tags.COMPONENT.set(commandContext.getCurrentSpan(), getTypeName()); commandConsumer.handle(commandContext); }, closeHandler); }
if (Strings.isNullOrEmpty(getTypeName())) { result.fail(new IllegalStateException("adapter does not define a typeName")); } else if (tenantServiceClient == null) {
if (Strings.isNullOrEmpty(getTypeName())) { result.fail(new IllegalStateException("adapter does not define a typeName")); } else if (tenantServiceClient == null) {
if (tenantConfigTracker.result().isAdapterEnabled(getTypeName())) { final MessageSender sender = senderTracker.result(); final Message msg = newMessage(
if (tenantConfigTracker.result().isAdapterEnabled(getTypeName())) { final MessageSender sender = senderTracker.result(); final Message msg = newMessage(
MessageHelper.addRegistrationAssertion(message, registrationAssertion); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_ORIG_ADAPTER, getTypeName()); if (getConfig().isDefaultsEnabled()) { final JsonObject defaults = registrationInfo.getJsonObject(RegistrationConstants.FIELD_DEFAULTS);
MessageHelper.addRegistrationAssertion(message, registrationAssertion); MessageHelper.addProperty(message, MessageHelper.APP_PROPERTY_ORIG_ADAPTER, getTypeName()); if (getConfig().isDefaultsEnabled()) { final JsonObject defaults = registrationInfo.getJsonObject(RegistrationConstants.FIELD_DEFAULTS);