Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

MQTT agent subscription threading and renewal bug fix (#1915)

Rich Turner9ffd8f178166
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java
index a9ce9f186..322902a79 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java
@@ -185,12 +185,20 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtoco
throw new IllegalStateException("IO client for protocol should not be null");
}
- client.addConnectionStatusConsumer(this::onConnectionStatusChanged);
- client.addMessageConsumer(this::onMessageReceived);
+ addConnectionStatusConsumer(client);
+ addMessageConsumer(client);
this.client = client;
return client;
}
+ protected void addConnectionStatusConsumer(W client) {
+ client.addConnectionStatusConsumer(this::onConnectionStatusChanged);
+ }
+
+ protected void addMessageConsumer(W client) {
+ client.addMessageConsumer(this::onMessageReceived);
+ }
+
/**
* Called when the {@link IOClient} {@link ConnectionStatus} changes
*/
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
index 39599d63b..5899e58b4 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java
@@ -242,7 +242,9 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
Future<Void> connectFuture = doConnect();
waitForConnectFuture(connectFuture);
execution.recordResult(null);
- }).whenComplete((result, ex) -> {
+ });
+
+ connectRetry.whenComplete((result, ex) -> {
if (ex != null) {
// Cleanup resources
disconnect();
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
index bace4bc21..07a1518c9 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
@@ -22,70 +22,120 @@ package org.openremote.agent.protocol.mqtt;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.MqttClientSslConfig;
import com.hivemq.client.mqtt.MqttClientSslConfigBuilder;
-import com.hivemq.client.mqtt.MqttClientState;
import com.hivemq.client.mqtt.datatypes.MqttQos;
import com.hivemq.client.mqtt.exceptions.ConnectionClosedException;
import com.hivemq.client.mqtt.exceptions.ConnectionFailedException;
-import com.hivemq.client.mqtt.lifecycle.MqttDisconnectSource;
+import com.hivemq.client.mqtt.exceptions.MqttClientStateException;
import com.hivemq.client.mqtt.mqtt3.Mqtt3AsyncClient;
import com.hivemq.client.mqtt.mqtt3.Mqtt3ClientBuilder;
import com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3ConnAckException;
import com.hivemq.client.mqtt.mqtt3.exceptions.Mqtt3DisconnectException;
-import com.hivemq.client.mqtt.mqtt3.lifecycle.Mqtt3ClientDisconnectedContext;
import com.hivemq.client.mqtt.mqtt3.message.connect.Mqtt3ConnectBuilder;
import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;
-import com.hivemq.client.mqtt.mqtt3.message.subscribe.Mqtt3Subscribe;
import com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck;
import com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAckReturnCode;
+import dev.failsafe.Failsafe;
+import dev.failsafe.RetryPolicy;
+import jakarta.validation.constraints.NotNull;
import org.openremote.agent.protocol.io.IOClient;
import org.openremote.container.Container;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.auth.UsernamePassword;
import org.openremote.model.syslog.SyslogCategory;
-import org.openremote.model.util.Pair;
-import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.model.util.ValueUtil;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import java.net.URI;
-import java.util.HashSet;
-import java.util.Optional;
-import java.util.Set;
+import java.time.Duration;
+import java.util.*;
import java.util.concurrent.*;
-import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
-import static org.openremote.agent.protocol.io.AbstractNettyIOClient.RECONNECT_DELAY_INITIAL_MILLIS;
-import static org.openremote.agent.protocol.io.AbstractNettyIOClient.RECONNECT_DELAY_MAX_MILLIS;
import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+/**
+ * HiveMQ client re-subscribing has no callback mechanism so we cannot track which subscriptions fail on re-connect
+ * so we have to manually handle reconnect and re-subscribe until this issue is resolved, if this is ever implemented
+ * then automaticReconnect could be reinstated - see version history if/when needed
+ * (see <a href="https://github.com/hivemq/hivemq-mqtt-client/issues/510"></a>)
+ */
public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S>> {
+ protected static final class TopicSubscriptionInfo<S> {
+ MqttQos qos;
+ CompletableFuture<Boolean> subAckFuture;
+ Set<Consumer<MQTTMessage<S>>> consumers = new HashSet<>();
+
+ TopicSubscriptionInfo(CompletableFuture<Boolean> subAckFuture, MqttQos qos, Consumer<MQTTMessage<S>> consumer) {
+ this.subAckFuture = subAckFuture;
+ this.qos = qos;
+ this.consumers.add(consumer);
+ }
+
+ /**
+ * Add a consumer for this topic
+ */
+ void addConsumer(Consumer<MQTTMessage<S>> consumer) {
+ synchronized (this) {
+ consumers.add(consumer);
+ }
+ }
+
+ /**
+ * Remove a consumer
+ * @return true if no consumers remaining
+ */
+ boolean removeConsumer(Consumer<MQTTMessage<S>> consumer) {
+ synchronized (this) {
+ return consumers.remove(consumer) && consumers.isEmpty();
+ }
+ }
+
+ void consume(MQTTMessage<S> message) {
+ synchronized (this) {
+ consumers.forEach(consumer -> consumer.accept(message));
+ }
+ }
+
+ boolean isSubFailed() {
+ return !subAckFuture.getNow(false);
+ }
+
+ boolean isSubDone() {
+ return subAckFuture.isDone();
+ }
+
+ void updateSubAck(@NotNull CompletableFuture<Boolean> subAckFuture) {
+ this.subAckFuture = subAckFuture;
+ }
+ }
+
public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractMQTT_IOClient.class);
+ public static long RECONNECT_DELAY_INITIAL_MILLIS = 1000L;
+ public static long RECONNECT_DELAY_MAX_MILLIS = 5*60000L;
+ protected CompletableFuture<Void> connectRetry;
+ protected int connectTimeout = 5000;
+ protected ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;
protected String clientId;
protected String host;
protected int port;
protected boolean secure;
protected boolean cleanSession;
+ protected boolean resubscribeIfSessionPresent;
+ protected boolean removeConsumersOnSubscriptionFailure;
+ protected boolean retrySubscriptionFailuresOnReconnect = true;
protected UsernamePassword usernamePassword;
protected URI websocketURI;
protected Mqtt3AsyncClient client;
protected final Set<Consumer<ConnectionStatus>> connectionStatusConsumers = new CopyOnWriteArraySet<>();
- protected final ConcurrentMap<String, Pair<MqttQos, Set<Consumer<MQTTMessage<S>>>>> topicConsumerMap = new ConcurrentHashMap<>();
- protected ScheduledExecutorService executorService;
- protected boolean disconnected = true; // Need to use this flag to cancel client reconnect task
- protected final AtomicBoolean connected = new AtomicBoolean(false); // Used for subscriptions
+ protected final Map<String, TopicSubscriptionInfo<S>> topicConsumerMap = new HashMap<>();
protected Consumer<String> topicSubscribeFailureConsumer;
- protected ConnectionStatus currentStatus;
- protected MqttQos publishQos = MqttQos.AT_LEAST_ONCE;
- protected MqttQos subscribeQos = MqttQos.AT_LEAST_ONCE;
-
- protected AbstractMQTT_IOClient(String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill, KeyManagerFactory keyManagerFactory, TrustManagerFactory trustManagerFactory) {
- this(UniqueIdentifierGenerator.generateId(), host, port, secure, cleanSession, usernamePassword, websocketURI, lastWill, keyManagerFactory, trustManagerFactory);
- }
+ protected MqttQos publishQos = MqttQos.AT_MOST_ONCE;
+ protected MqttQos subscribeQos = MqttQos.AT_MOST_ONCE;
+ protected String clientUri;
protected AbstractMQTT_IOClient(String clientId, String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill, KeyManagerFactory keyManagerFactory, TrustManagerFactory trustManagerFactory) {
this.clientId = clientId;
@@ -95,41 +145,41 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
this.cleanSession = cleanSession;
this.usernamePassword = usernamePassword;
this.websocketURI = websocketURI;
- this.executorService = Container.SCHEDULED_EXECUTOR;
+
+ if (websocketURI != null) {
+ clientUri = "mqtt_" + websocketURI + "?clientId=" + clientId;
+ } else {
+ clientUri = "mqtt" + (secure ? "s://" : "://") + host + ":" + port + "/?clientId=" + clientId;
+ }
Mqtt3ClientBuilder builder = MqttClient.builder()
.useMqttVersion3()
.identifier(clientId)
- .addConnectedListener(context -> onConnectionStatusChanged(null))
.addDisconnectedListener(context -> {
- boolean userClosed = context.getSource() == MqttDisconnectSource.USER;
- if (disconnected) {
- context.getReconnector().reconnect(false);
- } else {
- if (this.usernamePassword != null) {
- ((Mqtt3ClientDisconnectedContext) context).getReconnector().connectWith()
- .simpleAuth()
- .username(usernamePassword.getUsername())
- .password(usernamePassword.getPassword().getBytes())
- .applySimpleAuth()
- .applyConnect();
+ // Remove all subscriptions so we can add them again on connect and get a completable future for them
+ // this allows us to track any failed subscriptions on reconnect
+ boolean reconnect = false;
+
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.CONNECTED) {
+ onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+ reconnect = true;
}
}
- if (context.getCause() instanceof Mqtt3DisconnectException) {
- LOG.info("Client disconnect '" + getClientUri() + "': initiator=" + context.getSource());
- } else if (context.getCause() instanceof Mqtt3ConnAckException) {
- LOG.info("Connection rejected by the broker '" + getClientUri() + "': reasonCode=" + ((Mqtt3ConnAckException)context.getCause()).getMqttMessage().getReturnCode() + ", initiator=" + context.getSource());
- } else if (context.getCause() instanceof ConnectionClosedException) {
- LOG.info("Connection closed by " + context.getSource() + " '" + getClientUri() + "': initiator=" + context.getSource());
- } else if (context.getCause() instanceof ConnectionFailedException) {
- LOG.log(Level.INFO, "Connection failed '" + getClientUri() + "': initiator=" + context.getSource(), context.getCause());
+
+ if (reconnect) {
+ if (context.getCause() instanceof Mqtt3DisconnectException) {
+ LOG.info("Connection error uri=" + getClientUri() + ", initiator=" + context.getSource());
+ } else if (context.getCause() instanceof Mqtt3ConnAckException) {
+ LOG.info("Connection rejected uri=" + getClientUri() + ", reasonCode=" + ((Mqtt3ConnAckException)context.getCause()).getMqttMessage().getReturnCode());
+ } else if (context.getCause() instanceof ConnectionClosedException) {
+ LOG.info("Connection closed uri=" + getClientUri() + ", initiator=" + context.getSource());
+ } else if (context.getCause() instanceof ConnectionFailedException) {
+ LOG.info("Connection failed uri=" + getClientUri() + ", initiator=" + context.getSource() + ", message=" + context.getCause().getMessage());
+ }
+ doReconnect();
}
- onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
- })
- .automaticReconnect()
- .initialDelay(RECONNECT_DELAY_INITIAL_MILLIS, TimeUnit.MILLISECONDS)
- .maxDelay(RECONNECT_DELAY_MAX_MILLIS, TimeUnit.MILLISECONDS)
- .applyAutomaticReconnect();
+ });
if (secure) {
MqttClientSslConfigBuilder sslBuilder = MqttClientSslConfig.builder();
@@ -166,8 +216,9 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
try {
client = builder.buildAsync();
} catch (Exception e) {
- LOG.log(Level.WARNING, "Invalid MQTT client config for client '" + getClientUri() + "'", e);
+ LOG.log(Level.WARNING, "Invalid config uri=" + getClientUri(), e);
client = null;
+ connectionStatus = ConnectionStatus.ERROR;
}
}
@@ -181,28 +232,50 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
return this;
}
+ public boolean isResubscribeIfSessionPresent() {
+ return resubscribeIfSessionPresent;
+ }
+
+ public AbstractMQTT_IOClient<S> setResubscribeIfSessionPresent(boolean resubscribeIfSessionPresent) {
+ this.resubscribeIfSessionPresent = resubscribeIfSessionPresent;
+ return this;
+ }
+
+ public boolean isRemoveConsumersOnSubscriptionFailure() {
+ return removeConsumersOnSubscriptionFailure;
+ }
+
+ public AbstractMQTT_IOClient<S> setRemoveConsumersOnSubscriptionFailure(boolean removeConsumersOnSubscriptionFailure) {
+ this.removeConsumersOnSubscriptionFailure = removeConsumersOnSubscriptionFailure;
+ return this;
+ }
+
+ public boolean isRetrySubscriptionFailuresOnReconnect() {
+ return retrySubscriptionFailuresOnReconnect;
+ }
+
+ public AbstractMQTT_IOClient<S> setRetrySubscriptionFailuresOnReconnect(boolean retrySubscriptionFailuresOnReconnect) {
+ this.retrySubscriptionFailuresOnReconnect = retrySubscriptionFailuresOnReconnect;
+ return this;
+ }
+
@Override
public void sendMessage(MQTTMessage<S> message) {
if (client == null) {
- LOG.info("Cannot send message as client is invalid: " + getClientUri());
+ LOG.info("Cannot send message as client is not valid, uri=" + getClientUri());
return;
}
- if (getConnectionStatus() != ConnectionStatus.CONNECTED) {
- LOG.info("Cannot send message as client is not connected: " + getClientUri());
- return;
- }
-
- // TODO: Might need to put some timeout logic here as this call can block if client becomes disconnected see (https://github.com/hivemq/hivemq-mqtt-client/issues/554)
client.publishWith()
.topic(message.getTopic())
.payload(messageToBytes(message.getPayload()))
- .qos(Optional.ofNullable(message.getQos()).map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(MqttQos.AT_LEAST_ONCE))
+ .qos(Optional.ofNullable(message.getQos()).map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(publishQos))
.send()
+ .orTimeout(5000, TimeUnit.MILLISECONDS)
.whenComplete((publish, throwable) -> {
if (throwable != null) {
// Failure
- LOG.log(Level.INFO, "Failed to publish to MQTT broker '" + getClientUri() + "'", throwable);
+ LOG.log(Level.INFO, "Failed to publish uri=" + getClientUri() + ", topic=" + message.getTopic(), throwable);
} else {
// Success
LOG.finest("Published message to MQTT broker '" + getClientUri() + "'");
@@ -215,29 +288,28 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
addMessageConsumer("#", null, messageConsumer);
}
- public boolean addMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
- return addMessageConsumer(topic, null, messageConsumer);
+ public void addMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
+ addMessageConsumer(topic, subscribeQos, messageConsumer);
}
- public boolean addMessageConsumer(String topic, MqttQos qos, Consumer<MQTTMessage<S>> messageConsumer) {
+ public void addMessageConsumer(String topic, MqttQos qos, Consumer<MQTTMessage<S>> messageConsumer) {
if (client == null) {
- return false;
+ return;
}
- Pair<MqttQos, Set<Consumer<MQTTMessage<S>>>> consumers = topicConsumerMap.computeIfAbsent(topic, t ->
- new Pair<>(qos != null ? qos : MqttQos.AT_LEAST_ONCE, new HashSet<>()));
-
- if (consumers.getValue().isEmpty()) {
- // Create the subscription on the client
- if (doClientSubscription(topic)) {
- consumers.getValue().add(messageConsumer);
- return true;
- } else {
- return false;
- }
- } else {
- consumers.getValue().add(messageConsumer);
- return true;
+ LOG.fine("Adding message consumer uri=" + getClientUri() + ", topic=" + topic);
+
+ synchronized (topicConsumerMap) {
+ topicConsumerMap.compute(
+ topic,
+ (t, subInfo) -> {
+ if (subInfo == null) {
+ subInfo = new TopicSubscriptionInfo<>(doSubscribe(qos, topic), qos, messageConsumer);
+ } else {
+ subInfo.addConsumer(messageConsumer);
+ }
+ return subInfo;
+ });
}
}
@@ -246,60 +318,66 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
}
protected void onSubscribeFailed(String topic) {
+ if (removeConsumersOnSubscriptionFailure) {
+ LOG.fine("Removing subscription consumers uri=" + getClientUri() + ", topic=" + topic);
+ synchronized (topicConsumerMap) {
+ this.topicConsumerMap.remove(topic);
+ }
+ }
if (this.topicSubscribeFailureConsumer != null) {
this.topicSubscribeFailureConsumer.accept(topic);
}
}
- protected boolean doClientSubscription(String topic) {
- synchronized (connected) {
- if (!connected.get()) {
- // Just return true and let connection logic sort out actual subscription
- return true;
- }
- }
-
- Consumer<MQTTMessage<S>> messageConsumer = message -> {
- Pair<MqttQos, Set<Consumer<MQTTMessage<S>>>> topicConsumers = topicConsumerMap.get(topic);
- if (topicConsumers != null) {
- topicConsumers.getValue().forEach(consumer -> {
+ protected CompletableFuture<Boolean> doSubscribe(MqttQos qos, String topic) {
+ CompletableFuture<Mqtt3SubAck> subAckFuture = client.subscribeWith().topicFilter(topic).qos(qos).callback(
+ publish -> {
try {
- consumer.accept(message);
+ TopicSubscriptionInfo<S> subscriptionInfo = topicConsumerMap.get(topic);
+ if (subscriptionInfo != null) {
+ String topicStr = publish.getTopic().toString();
+ S payload = messageFromBytes(publish.getPayloadAsBytes());
+ MQTTMessage<S> message = new MQTTMessage<>(topicStr, payload);
+ subscriptionInfo.consume(message);
+ }
} catch (Exception e) {
- LOG.log(Level.WARNING, "Message consumer threw an exception", e);
+ LOG.log(Level.WARNING, "Failed to process incoming message uri=" + getClientUri() + ", topic=" + topic, e);
}
- });
- }
- };
+ }
+ ).executor(Container.EXECUTOR)
+ .send();
- Pair<MqttQos, Set<Consumer<MQTTMessage<S>>>> topicConsumers = topicConsumerMap.get(topic);
- try {
- Mqtt3Subscribe subscribeMessage = Mqtt3Subscribe.builder()
- .topicFilter(topic)
- .qos(topicConsumers.getKey())
- .build();
-
- Mqtt3SubAck subAck = client.subscribe(subscribeMessage, publish -> {
- try {
- String topicStr = publish.getTopic().toString();
- S payload = messageFromBytes(publish.getPayloadAsBytes());
- MQTTMessage<S> message = new MQTTMessage<>(topicStr, payload);
- messageConsumer.accept(message);
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Failed to process published message on client '" + getClientUri() + "'", e);
+ return subAckFuture.handle((subAck, throwable) -> {
+ if (throwable != null) {
+ LOG.log(Level.SEVERE, "Subscribe failed uri=" + getClientUri() + ", topic=" + topic + ", error=" + throwable.getMessage());
+ Container.EXECUTOR.execute(() -> onSubscribeFailed(topic));
+ if (throwable instanceof CancellationException) {
+ subAckFuture.cancel(true);
}
- }).get();
+ return false;
+ }
if (subAck.getReturnCodes().contains(Mqtt3SubAckReturnCode.FAILURE)) {
- throw new Exception("Server returned failure code for subscription");
+ LOG.log(Level.WARNING, "Subscribe rejected by server uri=" + getClientUri() + ", topic=" + topic);
+ Container.EXECUTOR.execute(() -> onSubscribeFailed(topic));
+ return false;
}
- LOG.fine("Subscribed to topic '" + topic + "' on client '" + getClientUri() + "'");
+ LOG.log(Level.FINE, "Subscribe success uri=" + getClientUri() + ", topic=" + topic);
return true;
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Failed to subscribe to topic '" + topic + "' on client '" + getClientUri() + "': " + e.getMessage());
- executorService.execute(() -> onSubscribeFailed(topic));
- }
- topicConsumerMap.remove(topic);
- return false;
+ });
+ }
+
+ protected void doUnsubscribe(String topic) {
+ LOG.finest("Unsubscribing uri=" + getClientUri() + ", topic=" + topic);
+ client.unsubscribeWith()
+ .topicFilter(topic)
+ .send()
+ .whenComplete((subAck, throwable) -> {
+ if (throwable != null) {
+ LOG.log(Level.WARNING, "Unsubscribe failed uri=" + getClientUri() + ", topic=" + topic, throwable);
+ } else {
+ LOG.fine("Unsubscribe success uri=" + getClientUri() + ", topic=" + topic);
+ }
+ });
}
@Override
@@ -308,35 +386,22 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
}
public void removeMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
- topicConsumerMap.computeIfPresent(topic, (t, consumers) -> {
- if (consumers.getValue().remove(messageConsumer) && consumers.getValue().isEmpty()) {
- removeSubscription(topic);
- return null;
- }
- return consumers;
- });
+ synchronized (topicConsumerMap) {
+ topicConsumerMap.computeIfPresent(topic, (t, subscriptionInfo) -> {
+ if (subscriptionInfo.removeConsumer(messageConsumer)) {
+ doUnsubscribe(topic);
+ return null;
+ }
+ return subscriptionInfo;
+ });
+ }
}
@Override
public void removeAllMessageConsumers() {
Set<String> topics = new HashSet<>(topicConsumerMap.keySet());
topicConsumerMap.clear();
- topics.forEach(this::removeSubscription);
- }
-
- protected void removeSubscription(String topic) {
- if (client != null) {
- client.unsubscribeWith()
- .topicFilter(topic)
- .send()
- .whenComplete((subAck, throwable) -> {
- if (throwable != null) {
- LOG.log(Level.WARNING, "Failed to unsubscribe to topic '" + topic + "' on client '" + getClientUri() + "'", throwable);
- } else {
- LOG.fine("Unsubscribed from topic '" + topic + "' on client '" + getClientUri() + "'");
- }
- });
- }
+ topics.forEach(this::doUnsubscribe);
}
@Override
@@ -356,82 +421,112 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
@Override
public ConnectionStatus getConnectionStatus() {
- if (client == null) {
- return ConnectionStatus.ERROR;
- }
- MqttClientState state = client.getState();
- if (state == MqttClientState.CONNECTED) {
- return ConnectionStatus.CONNECTED;
- }
- if (state == MqttClientState.DISCONNECTED) {
- return ConnectionStatus.DISCONNECTED;
- }
-
- // We cannot differentiate between connecting and waiting with the callbacks provided so just use waiting state
- return ConnectionStatus.WAITING;
+ return connectionStatus;
}
@Override
public void connect() {
synchronized (this) {
- if (client == null) {
- return;
- }
- if (!disconnected) {
+ if (connectionStatus != ConnectionStatus.DISCONNECTED) {
LOG.finest("Must be disconnected before calling connect: " + getClientUri());
return;
}
- LOG.fine("Connecting MQTT Client: " + getClientUri());
- this.disconnected = false;
+ LOG.fine("Connecting client: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.CONNECTING);
}
+ scheduleDoConnect(100);
+ }
+
+ protected void waitForConnectFuture(Future<Void> connectFuture) throws Exception {
+ connectFuture.get(getConnectTimeoutMillis()+1000L, TimeUnit.MILLISECONDS);
+ }
+ public int getConnectTimeoutMillis() {
+ return this.connectTimeout;
+ }
+
+ protected void doReconnect() {
+ doDisconnect();
+ scheduleDoConnect(5000);
+ }
+
+ protected Future<Void> doConnect() {
LOG.info("Establishing connection: " + getClientUri());
Mqtt3ConnectBuilder.Send<CompletableFuture<Mqtt3ConnAck>> completableFutureSend = client.connectWith()
- .cleanSession(true) // Always clean session as there's some inconsistency in how HiveMQ client handles this on reconnects
- .keepAlive(5);
+ .cleanSession(cleanSession)
+ .keepAlive(10);
if (usernamePassword != null) {
completableFutureSend = completableFutureSend.simpleAuth()
- .username(usernamePassword.getUsername())
- .password(usernamePassword.getPassword().getBytes())
- .applySimpleAuth();
+ .username(usernamePassword.getUsername())
+ .password(usernamePassword.getPassword().getBytes())
+ .applySimpleAuth();
}
- completableFutureSend.send().whenComplete((connAck, throwable) -> {
- if (throwable != null) {
- LOG.log(Level.INFO, "Connection failed:" + getClientUri(), throwable.getMessage());
- } else {
- synchronized (connected) {
- connected.set(true);
-
- if (!this.cleanSession && !connAck.isSessionPresent()) {
- // Need to re-instate the subscriptions as HiveMQ client doesn't do it
- // Re-add all subscriptions
- topicConsumerMap.keySet().forEach(this::doClientSubscription);
- }
+ CompletableFuture<Mqtt3ConnAck> connAckFuture = completableFutureSend.send();
+
+ connAckFuture.whenComplete((connAck, throwable) -> {
+ if (connAck != null) {
+ LOG.fine("Connected code=" + connAck.getReturnCode() + ", sessionPresent=" + connAck.isSessionPresent());
+ boolean retryFailures = isRetrySubscriptionFailuresOnReconnect();
+ boolean retryAll = !connAck.isSessionPresent() || resubscribeIfSessionPresent;
+
+ if (retryAll || retryFailures) {
+ Container.EXECUTOR.execute(() -> {
+ // Get all failed or completed subscriptions and retry as needed
+ synchronized (topicConsumerMap) {
+ topicConsumerMap.entrySet()
+ .stream()
+ .filter(topicSubscriptionInfo ->
+ topicSubscriptionInfo.getValue().isSubDone()
+ && (retryAll || topicSubscriptionInfo.getValue().isSubFailed()))
+ .forEach(topicSubscriptionInfo -> {
+ LOG.finest("Resubscribing uri=" + getClientUri() + ", topic=" + topicSubscriptionInfo.getKey());
+ topicSubscriptionInfo.getValue()
+ .updateSubAck(
+ doSubscribe(
+ topicSubscriptionInfo.getValue().qos,
+ topicSubscriptionInfo.getKey()
+ )
+ );
+ });
+ }
+ });
+ }
+ } else if (throwable != null) {
+ if (throwable instanceof CancellationException) {
+ LOG.info("Connection cancelled uri=" + getClientUri());
+ } else {
+ LOG.info("Connection failed uri=" + getClientUri() + ", error=" + throwable.getMessage());
}
}
});
+
+ CompletableFuture<Void> voidFuture = connAckFuture.thenAccept(result -> {});
+
+ // Propagate cancellation from voidFuture to original
+ voidFuture.whenComplete((result, exception) -> {
+ if (voidFuture.isCancelled()) {
+ connAckFuture.cancel(true);
+ }
+ });
+ return voidFuture;
}
- /**
- * Allows an override to be provided as {@link #getConnectionStatus} doesn't always return the correct status when a
- * server initiated disconnect occurs (some sort of timing issue).
- */
- protected void onConnectionStatusChanged(ConnectionStatus statusOverride) {
- ConnectionStatus status = statusOverride != null ? statusOverride : getConnectionStatus();
- if (currentStatus == status) {
+ protected void onConnectionStatusChanged(ConnectionStatus connectionStatus) {
+ if (this.connectionStatus == connectionStatus) {
return;
}
- currentStatus = status;
- LOG.info("Client '" + getClientUri() + "' connection status changed: " + status);
-
+ this.connectionStatus = connectionStatus;
+ if (!connectionStatusConsumers.isEmpty()) {
+ LOG.finest("Notifying connection status consumers: count=" + connectionStatusConsumers.size());
+ }
connectionStatusConsumers.forEach(
consumer -> {
try {
- consumer.accept(status);
+ consumer.accept(connectionStatus);
} catch (Exception e) {
LOG.log(Level.WARNING, "Connection status change handler threw an exception: " + getClientUri(), e);
}
@@ -445,38 +540,80 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
}
synchronized (this) {
- if (disconnected) {
- LOG.finest("Already disconnected: " + getClientUri());
+ if (connectionStatus == ConnectionStatus.DISCONNECTED || connectionStatus == ConnectionStatus.DISCONNECTING) {
+ LOG.finest("Already disconnected or disconnecting: " + getClientUri());
return;
}
- LOG.finest("Disconnecting IO client: " + getClientUri());
- this.disconnected = true;
+ LOG.fine("Disconnecting: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTING);
}
- client.disconnect().whenComplete((unused, throwable) -> {
- connected.set(false);
-
- if (throwable != null) {
- LOG.log(Level.INFO, "Disconnect error '" + getClientUri() + "':" + throwable.getMessage());
- }
- if (this.cleanSession) {
- removeAllMessageConsumers();
+ try {
+ if (connectRetry != null) {
+ connectRetry.cancel(true);
+ connectRetry = null;
}
+ } catch (Exception ignored) {}
+ doDisconnect();
+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
+ }
- onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
+ protected void doDisconnect() {
+ LOG.finest("Performing disconnect: " + getClientUri());
+ client.disconnect().whenComplete((unused, throwable) -> {
+ if (throwable != null && !(throwable instanceof MqttClientStateException)) {
+ LOG.log(Level.WARNING, "Failed to disconnect gracefully: " + getClientUri(), throwable);
+ } else {
+ LOG.finest("Disconnect done: " + getClientUri());
+ }
});
}
@Override
public String getClientUri() {
- if (websocketURI != null) {
- return "mqtt_" + websocketURI + "?clientId=" + clientId;
- }
- return "mqtt" + (secure ? "s://" : "://" ) + host + ":" + port + "/?clientId=" + clientId;
+ return clientUri;
}
public abstract byte[] messageToBytes(S message);
public abstract S messageFromBytes(byte[] bytes);
+
+ protected void scheduleDoConnect(long initialDelay) {
+ long delay = Math.max(initialDelay, RECONNECT_DELAY_INITIAL_MILLIS);
+ long maxDelay = Math.max(delay+1, RECONNECT_DELAY_MAX_MILLIS);
+
+ RetryPolicy<Object> retryPolicy = RetryPolicy.builder()
+ .withJitter(Duration.ofMillis(delay))
+ .withBackoff(Duration.ofMillis(delay), Duration.ofMillis(maxDelay))
+ .handle(Exception.class)
+ .onRetryScheduled((execution) ->
+ LOG.info("Re-connection scheduled in '" + execution.getDelay() + "' for: " + getClientUri()))
+ .onFailedAttempt((execution) -> {
+ LOG.info("Connection attempt failed '" + execution.getAttemptCount() + "' for: " + getClientUri() + ", error=" + (execution.getLastException() != null ? execution.getLastException().getMessage() : null));
+ doDisconnect();
+ })
+ .withMaxRetries(Integer.MAX_VALUE)
+ .build();
+
+ connectRetry = Failsafe.with(retryPolicy).with(Container.EXECUTOR).runAsyncExecution((execution) -> {
+
+ LOG.fine("Connection attempt '" + (execution.getAttemptCount()+1) + "' for: " + getClientUri());
+ // Connection future should timeout so we just wait for it but add additional timeout just in case
+ Future<Void> connectFuture = doConnect();
+ waitForConnectFuture(connectFuture);
+ execution.recordResult(null);
+ });
+
+ connectRetry.whenComplete((result, ex) -> {
+ if (ex == null) {
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.CONNECTING) {
+ LOG.fine("Connection attempt success: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.CONNECTED);
+ }
+ }
+ }
+ });
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.java
index 08b2dd2d8..300c7861a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTMessage.java
@@ -45,4 +45,13 @@ public class MQTTMessage<T> {
}
public Integer getQos() { return qos; }
+
+ @Override
+ public String toString() {
+ return "MQTTMessage{" +
+ "topic='" + topic + '\'' +
+ ", payload=" + payload +
+ ", qos=" + qos +
+ '}';
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java
index 885d2d6c2..00f165a7f 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java
@@ -64,7 +64,7 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA
updateLinkedAttribute(new AttributeRef(assetId, attribute.getName()), msg.payload);
}
};
- client.addMessageConsumer(topic, agentLink.getQos().map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(null), messageConsumer);
+ client.addMessageConsumer(topic, Optional.of(agentLink.getQos().orElse(agent.getSubscribeQoS().orElse(0))).map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(MqttQos.AT_MOST_ONCE), messageConsumer);
protocolMessageConsumers.put(new AttributeRef(assetId, attribute.getName()), messageConsumer);
});
}
@@ -87,14 +87,6 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA
});
}
- @Override
- protected MQTT_IOClient createIoClient() throws Exception {
- MQTT_IOClient client = super.createIoClient();
- // Don't want the default message consumer, topic specific consumers will do the message routing for us
- client.removeAllMessageConsumers();
- return client;
- }
-
@Override
protected MQTT_IOClient doCreateIoClient() throws Exception {
String host = agent.getHost().orElse(null);
@@ -142,6 +134,11 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA
return client;
}
+ @Override
+ protected void addMessageConsumer(MQTT_IOClient client) {
+ // Don't want the default message consumer, topic specific consumers will do the message routing for us
+ }
+
@Override
protected void onMessageReceived(MQTTMessage<String> message) {
// This isn't used instead messages are targeted by topic
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.java
index 21d8212a3..78f177ace 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTT_IOClient.java
@@ -28,10 +28,6 @@ import java.nio.charset.StandardCharsets;
public class MQTT_IOClient extends AbstractMQTT_IOClient<String> {
- public MQTT_IOClient(String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill, KeyManagerFactory keyManagerFactory, TrustManagerFactory trustManagerFactory) {
- super(host, port, secure, cleanSession, usernamePassword, websocketURI, lastWill, keyManagerFactory, trustManagerFactory);
- }
-
public MQTT_IOClient(String clientId, String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill, KeyManagerFactory keyManagerFactory, TrustManagerFactory trustManagerFactory) {
super(clientId, host, port, secure, cleanSession, usernamePassword, websocketURI, lastWill, keyManagerFactory, trustManagerFactory );
}
diff --git a/gradle.properties b/gradle.properties
index cbe71dc52..8e59afc97 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -54,14 +54,14 @@ niftyModbusVersion = 0.17.0
reflectionsVersion=0.9.12
swaggerVersion=2.2.29
swaggeruiVersion = 5.18.2
-hiveMQClientVersion=1.3.4
+hiveMQClientVersion=1.3.7
bluetoothVersion = 0.62
nashornVersion=15.6
failsafeVersion=3.3.2
suncalcVersion=3.11
snapshotsRepoUrl=https://central.sonatype.com/repository/maven-snapshots/
releasesRepoUrl=https://ossrh-staging-api.central.sonatype.com/service/local/
-activeMQVersion = 2.40.0
+activeMQVersion = 2.41.0
jbossLoggingManagerVersion=2.1.19.Final
greenmailVersion=2.1.2
prometheusVersion=1.3.6
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
index 488a55efc..ab19a64f6 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
@@ -387,10 +387,6 @@ public class DefaultMQTTHandler extends MQTTHandler {
public void onUserAssetLinksChanged(RemotingConnection connection, List<PersistenceEvent<UserAssetLink>> changes) {
String sessionKey = getSessionKey(connection);
if (sessionSubscriptionConsumers.containsKey(sessionKey)) {
- if (changes.stream().allMatch(pe -> pe.getCause() == PersistenceEvent.Cause.CREATE)) {
- // Do nothing if only links have been added
- return;
- }
LOG.info("User asset links have changed for a connected user with active subscriptions so force disconnecting them: " + connectionToString(connection));
mqttBrokerService.doForceDisconnect(connection);
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
index ee25a7c84..ff12aa5de 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
@@ -25,6 +25,7 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.netty.channel.ChannelId;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;
+import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ClientSession;
@@ -38,6 +39,7 @@ import org.apache.activemq.artemis.core.config.MetricsConfiguration;
import org.apache.activemq.artemis.core.config.WildcardConfiguration;
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl;
import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration;
+import org.apache.activemq.artemis.core.protocol.mqtt.MQTTStateManager;
import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;
import org.apache.activemq.artemis.core.remoting.FailureListener;
import org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnection;
@@ -105,6 +107,8 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
public static final String MQTT_SERVER_LISTEN_HOST = "MQTT_SERVER_LISTEN_HOST";
public static final String MQTT_SERVER_LISTEN_PORT = "MQTT_SERVER_LISTEN_PORT";
public static final String ANONYMOUS_USERNAME = "anonymous";
+ // Allow 5 min durable session but this will not enable retained topics etc. as we delete queues aggressively for now
+ public static final int DEFAULT_SESSION_EXPIRY_MILLIS = 300000;
protected final WildcardConfiguration wildcardConfiguration = new WildcardConfiguration();
protected static final System.Logger LOG = System.getLogger(MQTTBrokerService.class.getName() + "." + API.name());
@@ -169,11 +173,12 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
// Create server config
serverConfiguration = new ConfigurationImpl();
+ serverConfiguration.setMqttSessionScanInterval(10000);
serverConfiguration.addAcceptorConfiguration("in-vm", "vm://0?protocols=core");
String serverURI = new URIBuilder().setScheme("tcp").setHost(host).setPort(port)
.setParameter("protocols", "MQTT")
- .setParameter("allowLinkStealing", "true")
- .setParameter("defaultMqttSessionExpiryInterval", "0") // Don't support retained sessions
+ .setParameter("allowLinkStealing", "false") // Preventing this ensures previous connection/session is properly cleaned up before a reconnect
+ .setParameter("defaultMqttSessionExpiryInterval", Integer.toString(DEFAULT_SESSION_EXPIRY_MILLIS))
.build().toString();
serverConfiguration.addAcceptorConfiguration("tcp", serverURI);
serverConfiguration.registerBrokerPlugin(this);
@@ -188,7 +193,8 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
serverConfiguration.setWildCardConfiguration(wildcardConfiguration);
serverConfiguration.setLiteralMatchMarkers("()");
- // Configure global address settings - aggressively cleanup queues (don't support resumable sessions)
+ // Configure global address settings - aggressively cleanup queues (don't support retained messages)
+ serverConfiguration.addQueueConfiguration(QueueConfiguration.of(wildcardConfiguration.getAnyWordsString()).setDurable(false));
serverConfiguration.addAddressSetting(wildcardConfiguration.getAnyWordsString(),
new AddressSettings()
.setDeadLetterAddress(SimpleString.of("ActiveMQ.DLQ"))
@@ -381,8 +387,6 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
- // TODO: Force delete session (don't allow retained/durable sessions)
-
connectionIDConnectionMap.remove(getConnectionIDString(connection));
if (connection.getClientID() != null) {
@@ -502,8 +506,16 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
}
protected void doForceDisconnect(RemotingConnection connection) {
- LOG.log(DEBUG, "Force disconnecting client connection: " + connectionToString(connection));
+ LOG.log(DEBUG, () -> "Force disconnecting client connection: " + connectionToString(connection));
connection.disconnect(false);
+ // Destroy session for force disconnects
+ try {
+ if (MQTTStateManager.getInstance(server.getActiveMQServer()).removeSessionState(connection.getClientID()) != null) {
+ LOG.log(TRACE, () -> "Removed session state for client connection: " + connectionToString(connection));
+ }
+ } catch (Exception e) {
+ LOG.log(INFO, () -> "Failed to get server instance to clear session for client connection: " + connectionToString(connection));
+ }
((SecurityStoreImpl)server.getActiveMQServer().getSecurityStore()).invalidateAuthorizationCache();
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java
index ab6ce501d..d06298702 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java
@@ -271,12 +271,22 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {
@Override
public void onConnectionLost(RemotingConnection connection) {
- provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));
+ provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> {
+ if (connections.remove(connection)) {
+ // Remove sessions as durable sessions will never reconnect if client ID already subscribed to attribute topics
+ mqttBrokerService.doForceDisconnect(connection);
+ }
+ });
}
@Override
public void onDisconnect(RemotingConnection connection) {
- provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> connections.remove(connection));
+ provisioningConfigAuthenticatedConnectionMap.values().forEach(connections -> {
+ if (connections.remove(connection)) {
+ // Remove sessions as durable sessions will never reconnect if client ID already subscribed to attribute topics
+ mqttBrokerService.doForceDisconnect(connection);
+ }
+ });
}
protected void processProvisioningRequest(RemotingConnection connection, Topic topic, ByteBuf body) {
@@ -541,18 +551,16 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {
}
protected void forceClientDisconnects(long provisioningConfigId) {
- provisioningConfigAuthenticatedConnectionMap.computeIfPresent(provisioningConfigId, (id, connections) -> {
- // Force disconnect of each connection and the disconnect handler will remove the connection from the map
- connections.forEach(connection -> {
- try {
- LOG.fine("Force disconnecting client that is using provisioning config ID '" + provisioningConfigId + "': " + MQTTBrokerService.connectionToString(connection));
- connection.disconnect(false);
- } catch (Exception e) {
- getLogger().log(Level.WARNING, "Failed to disconnect client: " + MQTTBrokerService.connectionToString(connection), e);
- }
- });
- connections.clear();
- return connections;
+
+ Set<RemotingConnection> connections = provisioningConfigAuthenticatedConnectionMap.remove(provisioningConfigId);
+
+ // Force disconnect of each connection and the disconnect handler will remove the connection from the map
+ connections.forEach(connection -> {
+ try {
+ mqttBrokerService.doForceDisconnect(connection);
+ } catch (Exception e) {
+ getLogger().log(Level.WARNING, "Failed to disconnect client: " + MQTTBrokerService.connectionToString(connection), e);
+ }
});
}
}
diff --git a/manager/src/main/resources/logging-dev.properties b/manager/src/main/resources/logging-dev.properties
index e5dd0d375..dff331bd4 100644
--- a/manager/src/main/resources/logging-dev.properties
+++ b/manager/src/main/resources/logging-dev.properties
@@ -28,7 +28,7 @@ java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogForm
.level=INFO
# The root logger category of OpenRemote
-org.openremote.level=FINE
+org.openremote.level=FINEST
# Processing time logging
#org.openremote.manager.asset.AssetProcessingService.level=FINE
@@ -36,8 +36,6 @@ org.openremote.level=FINE
#org.openremote.manager.rules.level=FINE
# Audit logging
-# Set to fine to log MQTT connections
-#org.openremote.manager.mqtt.level=FINE
# Debug Artemis MQTT connections
#org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil.level=ALL
# Set to fine to log WS connections
diff --git a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
index faee961cf..3dc780537 100644
--- a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
+++ b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
@@ -323,6 +323,10 @@ public class ManagerTestSetup extends ManagerSetup {
/* ############################ ROOMS ############################## */
RoomAsset apartment1Livingroom = createDemoApartmentRoom(apartment1, "Living Room 1");
+ apartment1Livingroom.getAttribute(Asset.NOTES).ifPresent(notes -> notes.addMeta(
+ new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)
+ ));
apartment1Livingroom.getAttributes().addOrReplace(
new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454213, 51.446884)),
new Attribute<>("lightsCeiling", NUMBER, 0d)
@@ -357,6 +361,10 @@ public class ManagerTestSetup extends ManagerSetup {
apartment1LivingroomId = apartment1Livingroom.getId();
RoomAsset apartment1Kitchen = createDemoApartmentRoom(apartment1, "Kitchen 1");
+ apartment1Kitchen.getAttribute(Asset.NOTES).ifPresent(notes -> notes.addMeta(
+ new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)
+ ));
apartment1Kitchen.getAttributes().addOrReplace(
new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454122, 51.446800)),
new Attribute<>("lights", BOOLEAN, true)
@@ -385,6 +393,10 @@ public class ManagerTestSetup extends ManagerSetup {
apartment1KitchenId = apartment1Kitchen.getId();
RoomAsset apartment1Hallway = createDemoApartmentRoom(apartment1, "Hallway 1");
+ apartment1Hallway.getAttribute(Asset.NOTES).ifPresent(notes -> notes.addMeta(
+ new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)
+ ));
apartment1Hallway.getAttributes().addOrReplace(
new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454342, 51.446762)),
new Attribute<>("lights", BOOLEAN, true)
@@ -397,6 +409,10 @@ public class ManagerTestSetup extends ManagerSetup {
apartment1HallwayId = apartment1Hallway.getId();
RoomAsset apartment1Bedroom1 = createDemoApartmentRoom(apartment1, "Bedroom 1");
+ apartment1Bedroom1.getAttribute(Asset.NOTES).ifPresent(notes -> notes.addMeta(
+ new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)
+ ));
apartment1Bedroom1.getAttributes().addOrReplace(
new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454332, 51.446830)),
new Attribute<>("lights", BOOLEAN, true)
@@ -417,6 +433,10 @@ public class ManagerTestSetup extends ManagerSetup {
RoomAsset apartment1Bathroom = new RoomAsset("Bathroom 1");
apartment1Bathroom.setParent(apartment1);
+ apartment1Bathroom.getAttribute(Asset.NOTES).ifPresent(notes -> notes.addMeta(
+ new MetaItem<>(ACCESS_RESTRICTED_READ, true),
+ new MetaItem<>(ACCESS_RESTRICTED_WRITE, true)
+ ));
apartment1Bathroom.getAttributes().addOrReplace(
new Attribute<>(Asset.LOCATION, new GeoJSONPoint(5.454227,51.446753)),
new Attribute<>("lights", BOOLEAN, true)
@@ -600,7 +620,17 @@ public class ManagerTestSetup extends ManagerSetup {
assetStorageService.storeUserAssetLinks(List.of(
new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),
keycloakTestSetup.serviceUser2.getId(),
- apartment1HallwayId)));
+ apartment1HallwayId),
+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),
+ keycloakTestSetup.serviceUser2.getId(),
+ apartment1Bedroom1Id),
+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),
+ keycloakTestSetup.serviceUser2.getId(),
+ apartment1KitchenId),
+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),
+ keycloakTestSetup.serviceUser2.getId(),
+ apartment1LivingroomId)
+ ));
// ################################ Realm smartcity ###################################
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
index 999a02ef1..8c0f7e9fc 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
@@ -635,7 +635,7 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
apartment1Livingroom.id == managerTestSetup.apartment1LivingroomId
apartment1Livingroom.name == "Living Room 1"
def resultAttributes = apartment1Livingroom.getAttributes()
- resultAttributes.size() == 7
+ resultAttributes.size() == 8
def currentTemperatureAttr = apartment1Livingroom.getAttribute("currentTemperature").orElse(null)
currentTemperatureAttr.getType() == NUMBER
!currentTemperatureAttr.getValue().isPresent()
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
index e67484350..89458e5f4 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
@@ -119,7 +119,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets.size() == 6
assets.get(0).id == managerTestSetup.apartment1Id
assets.get(1).id == managerTestSetup.apartment1LivingroomId
- assets.get(1).getAttributes().size() == 7
+ assets.get(1).getAttributes().size() == 8
!assets.get(1).getAttribute("motionSensor").isPresent()
!assets.get(1).getAttribute("currentTemperature").get().getValue().isPresent()
!assets.get(1).getAttribute("currentTemperature").get().meta.isEmpty()
@@ -361,7 +361,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets.find {it.id == managerTestSetup.apartment1Id} != null
def livingroom = assets.find {it.id == managerTestSetup.apartment1LivingroomId}
livingroom != null
- livingroom.getAttributes().size() == 7
+ livingroom.getAttributes().size() == 8
!livingroom.getAttribute("currentTemperature").get().getValue().isPresent()
!livingroom.getAttribute("currentTemperature").get().meta.isEmpty()
!livingroom.getAttribute("targetTemperature").get().getValue().isPresent()
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy
index d5b386142..7fdd6f989 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy
@@ -64,7 +64,7 @@ class AssetUserLinkingTest extends Specification implements ManagerContainerTrai
def userAssetLinks = assetResource.getUserAssetLinks(null, keycloakTestSetup.realmBuilding.name, null, null)
then: "result should match"
- userAssetLinks.length == 10
+ userAssetLinks.length == 13
userAssetLinks.any {
it.id.realm == keycloakTestSetup.realmBuilding.name &&
it.id.userId == keycloakTestSetup.testuser3Id &&
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
index 1a7426a93..56605136b 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -9,27 +9,26 @@ import org.openremote.agent.protocol.mqtt.MQTTLastWill
import org.openremote.agent.protocol.mqtt.MQTTMessage
import org.openremote.agent.protocol.mqtt.MQTT_IOClient
import org.openremote.manager.asset.AssetProcessingService
-import org.openremote.model.attribute.MetaItem
-import org.openremote.model.util.UniqueIdentifierGenerator
-import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetStorageService
-import org.openremote.manager.event.ClientEventService
import org.openremote.manager.mqtt.DefaultMQTTHandler
-import org.openremote.manager.mqtt.MQTTHandler
import org.openremote.manager.mqtt.MQTTBrokerService
+import org.openremote.manager.mqtt.MQTTHandler
import org.openremote.manager.setup.SetupService
import org.openremote.model.asset.AssetEvent
import org.openremote.model.asset.UserAssetLink
import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.asset.impl.RoomAsset
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeEvent
+import org.openremote.model.attribute.MetaItem
import org.openremote.model.auth.UsernamePassword
import org.openremote.model.event.shared.SharedEvent
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.model.util.ValueUtil
-import org.openremote.test.ManagerContainerTrait
import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.setup.integration.ManagerTestSetup
+import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
@@ -38,9 +37,7 @@ import java.util.function.Consumer
import static org.openremote.container.util.MapAccess.getInteger
import static org.openremote.container.util.MapAccess.getString
-import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_HOST
-import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_PORT
-import static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDString
+import static org.openremote.manager.mqtt.MQTTBrokerService.*
import static org.openremote.model.value.MetaItemType.ACCESS_RESTRICTED_READ
import static org.openremote.model.value.ValueType.TEXT
@@ -60,9 +57,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def mqttBrokerService = container.getService(MQTTBrokerService.class)
def assetStorageService = container.getService(AssetStorageService.class)
def assetProcessingService = container.getService(AssetProcessingService.class)
- def clientEventService = container.getService(ClientEventService.class)
def defaultMQTTHandler = mqttBrokerService.getCustomHandlers().find {it instanceof DefaultMQTTHandler} as DefaultMQTTHandler
- def agentService = container.getService(AgentService.class)
def mqttClientId = UniqueIdentifierGenerator.generateId()
def username = keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser.username // realm and OAuth client id
def password = keycloakTestSetup.serviceUser.secret
@@ -77,7 +72,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the client connection status should be in error"
conditions.eventually {
- assert client.getConnectionStatus() == ConnectionStatus.WAITING
+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTING
}
when: "a mqtt client connects with valid credentials"
@@ -93,7 +88,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "the client disconnects"
- def currentConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
+ mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
client.disconnect()
then: "the client resources should be freed"
@@ -103,8 +98,11 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a mqtt client connects with valid credentials"
+ List<String> subFailures = new CopyOnWriteArrayList<>()
mqttClientId = UniqueIdentifierGenerator.generateId()
client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, null)
+ client.setTopicSubscribeFailureConsumer {subFailures.add(it)}
+ client.setRemoveConsumersOnSubscriptionFailure(true)
client.connect()
then: "mqtt connection should exist"
@@ -119,6 +117,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
+ assert subFailures.size() == 1
+ assert subFailures[0] == topic
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -130,6 +130,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
+ assert subFailures.size() == 2
+ assert subFailures[1] == topic
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -142,6 +144,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "No subscription should exist"
conditions.eventually {
+ assert subFailures.size() == 3
+ assert subFailures[2] == topic
assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
@@ -151,7 +155,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "a mqtt client subscribes to all attributes of an asset"
topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()
Consumer<MQTTMessage<String>> eventConsumer = { msg ->
- def event = ValueUtil.parse(msg.payload, SharedEvent.class)
+ def event = ValueUtil.parse(msg.payload as String, SharedEvent.class)
receivedEvents.add(event.get())
}
client.addMessageConsumer(topic, eventConsumer)
@@ -159,7 +163,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "A subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
- assert client.topicConsumerMap.get(topic).value.size() == 1
+ assert client.topicConsumerMap.get(topic).consumers.size() == 1
assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -269,7 +273,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the subscription should be created"
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
- assert client.topicConsumerMap.get(topic).value.size() == 1
+ assert client.topicConsumerMap.get(topic).consumers.size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 1
@@ -315,7 +319,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "a subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
- assert client.topicConsumerMap.get(topic).value.size() == 1
+ assert client.topicConsumerMap.get(topic).consumers.size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 1
@@ -345,7 +349,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "the subscription should be created"
conditions.eventually {
assert client.topicConsumerMap.get(topic) != null
- assert client.topicConsumerMap.get(topic).value.size() == 1
+ assert client.topicConsumerMap.get(topic).consumers.size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 2
@@ -407,10 +411,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
receivedEvents.clear()
client.connect()
- then: "the connection should exist"
+ then: "the connection should exist and the previous subscription should be recreated"
conditions.eventually {
assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
- assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id).size() == 1
+ assert client.topicConsumerMap.size() == 1
+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 1
}
when: "a mqtt client subscribes to assets that are direct children of the realm"
@@ -421,13 +428,13 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
- assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 1
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 2
}
when: "an asset is updated with a new attribute"
def asset1 = assetStorageService.find(managerTestSetup.smartBuildingId)
asset1.addAttributes(new Attribute<>("temp", TEXT, "hello world"))
- asset1 = assetStorageService.merge(asset1)
+ assetStorageService.merge(asset1)
then: "A publish event message should be sent"
conditions.eventually {
@@ -448,7 +455,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
assert client.topicConsumerMap.get(topic) != null
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
- assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 2
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 3
}
when: "an asset is added as a descendant to the subscribed asset"
@@ -469,12 +476,12 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "the mqtt client unsubscribes from the multilevel topic"
client.removeMessageConsumer(topic, eventConsumer)
- then: "only one subscription should be left"
+ then: "only two subscriptions should be left"
conditions.eventually {
assert client.topicConsumerMap.get(topic) == null
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
- assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 1
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 2
}
when: "the descendant asset is modified"
@@ -498,11 +505,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
assert newClient.getConnectionStatus() == ConnectionStatus.CONNECTED
}
- when: "the existing client subscribes to attribute events in the hallway"
- topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()
- client.addMessageConsumer(topic, eventConsumer)
-
- and: "the new client publishes to a an attribute topic"
+ when: "the new client publishes to an attribute topic"
+ receivedEvents.clear()
topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/${managerTestSetup.apartment1HallwayId}".toString()
payload = "170"
newClient.sendMessage(new MQTTMessage<String>(topic, payload))
@@ -571,6 +575,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def asset = assetStorageService.find(managerTestSetup.apartment2LivingroomId)
def currentCO2Level = asset.getAttribute("co2Level").flatMap{it.getValue()}.orElse(0d)
newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username2, password2), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/co2Level/$managerTestSetup.apartment2LivingroomId".toString(), "1000", false))
+ newClient.setTopicSubscribeFailureConsumer {subFailures.add(it)}
newClient.connect()
then: "the client should be connected"
@@ -597,12 +602,15 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
}
when: "a restricted mqtt client subscribes to an unlinked asset"
- topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1KitchenId".toString()
+ topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1BathroomId".toString()
newClient.addMessageConsumer(topic, {msg ->})
- then: "no subscription should exist"
+ then: "the subscription should fail but the consumer should still exist (as setRemoveConsumersOnSubscriptionFailure=false)"
conditions.eventually {
- assert newClient.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
+ assert subFailures.size() == 4
+ assert subFailures[3] == topic
+ assert newClient.topicConsumerMap.get(topic) != null
+ assert newClient.topicConsumerMap.get(topic).consumers.size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
}
@@ -614,6 +622,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "a subscription should exist"
conditions.eventually {
assert newClient.topicConsumerMap.get(topic) != null
+ assert newClient.topicConsumerMap.get(topic).consumers.size() == 1
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 1
@@ -626,10 +635,12 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
keycloakTestSetup.serviceUser2.getId(),
managerTestSetup.apartment1BathroomId)))
- then: "the existing connection should not have been terminated"
- new PollingConditions(initialDelay: 2, timeout: 10, delay: 1).eventually {
+ then: "the existing connection should be terminated and the client should reconnect and the previously failed subscription should now succeed"
+ conditions.eventually {
assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1
- assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0].is(existingConnection)
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] !== existingConnection
+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
}
when: "a user asset link is removed for a connected restricted user"
@@ -639,7 +650,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
keycloakTestSetup.serviceUser2.getId(),
managerTestSetup.apartment1HallwayId)))
- then: "the existing connection should be terminated and the client should reconnect"
+ then: "the existing connection should be terminated and the client should reconnect and the previously failed subscription should be tried again but also fail"
conditions.eventually {
assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1
assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] !== existingConnection
@@ -651,43 +662,48 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "no subscriptions should exist in the client"
assert newClient.topicConsumerMap.isEmpty()
+ Consumer<MQTTMessage<String>> restrictedEventConsumer = { msg ->
+ def event = ValueUtil.parse(msg.payload as String, SharedEvent.class)
+ restrictedReceivedEvents.add(event.get())
+ }
+
when: "the restricted mqtt client subscribes to a now unlinked asset"
topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()
- newClient.addMessageConsumer(topic, {msg ->})
+ newClient.addMessageConsumer(topic, restrictedEventConsumer)
then: "no subscription should exist"
conditions.eventually {
- assert newClient.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
+ assert subFailures.size() == 6
+ assert subFailures[5] == topic
def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
}
- Consumer<MQTTMessage<String>> restrictedEventConsumer = { msg ->
- def event = ValueUtil.parse(msg.payload, SharedEvent.class)
- restrictedReceivedEvents.add(event.get())
- }
+ when: "the previously failed message consumer is removed"
+ newClient.removeMessageConsumer(topic, restrictedEventConsumer)
- when: "a restricted mqtt client subscribes to the attribute wildcard topic"
+ and: "a restricted mqtt client subscribes to the attribute wildcard topic"
topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$MQTTHandler.TOKEN_MULTI_LEVEL_WILDCARD".toString()
newClient.addMessageConsumer(topic, restrictedEventConsumer)
then: "a subscription should exist"
conditions.eventually {
assert newClient.topicConsumerMap.get(topic) != null
+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
}
when: "an unlinked asset attribute event occurs"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 40)
assetProcessingService.sendAttributeEvent(attributeEvent)
-
then: "the restricted client should not receive the event"
conditions.eventually {
assert restrictedReceivedEvents.size() == 0
}
when: "a linked asset attribute event occurs"
- attributeEvent = new AttributeEvent(managerTestSetup.apartment1BathroomId, "notes", "This is a test note")
+ attributeEvent = new AttributeEvent(managerTestSetup.apartment1BathroomId, RoomAsset.ROOM_NUMBER, 1)
assetProcessingService.sendAttributeEvent(attributeEvent)
then: "the restricted client should not receive the event since the attribute is not restricted"
@@ -695,10 +711,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
assert restrictedReceivedEvents.size() == 0
}
-
when: "a linked asset attribute has its restricted meta configuration set to true"
def bathroomAsset = assetStorageService.find(managerTestSetup.apartment1BathroomId)
- bathroomAsset.getAttributes().get("notes").get().addOrReplaceMeta(new MetaItem<>(ACCESS_RESTRICTED_READ, true))
+ bathroomAsset.getAttributes().get(RoomAsset.ROOM_NUMBER).get().addOrReplaceMeta(new MetaItem<>(ACCESS_RESTRICTED_READ, true))
assetStorageService.merge(bathroomAsset)
then: "the restricted client should still be connected"
@@ -714,10 +729,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
restrictedReceivedEvents.clear()
when: "another attribute event occurs on the restricted read attribute"
- attributeEvent = new AttributeEvent(managerTestSetup.apartment1BathroomId, "notes", "This is another test note")
+ attributeEvent = new AttributeEvent(managerTestSetup.apartment1BathroomId, RoomAsset.ROOM_NUMBER, 2)
assetProcessingService.sendAttributeEvent(attributeEvent)
-
then: "the restricted client should receive the triggered attribute event"
conditions.eventually {
assert restrictedReceivedEvents.size() == 1
@@ -727,7 +741,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "the restricted read meta item is removed"
bathroomAsset = assetStorageService.find(managerTestSetup.apartment1BathroomId)
- bathroomAsset.getAttributes().addOrReplace(new Attribute<>("notes", TEXT, "Set via attribute merge"))
+ bathroomAsset.getAttributes().addOrReplace(new Attribute<>(RoomAsset.ROOM_NUMBER, 2))
assetStorageService.merge(bathroomAsset)
then: "the restricted client should not receive the triggered attribute event"
@@ -737,7 +751,6 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
newClient.removeAllMessageConsumers()
restrictedReceivedEvents.clear()
-
when: "a restricted mqtt client subscribes to the asset wildcard topic"
topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ASSET_TOPIC/$MQTTHandler.TOKEN_MULTI_LEVEL_WILDCARD".toString()
newClient.addMessageConsumer(topic, restrictedEventConsumer)
@@ -749,7 +762,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "a linked asset event occurs"
bathroomAsset = assetStorageService.find(managerTestSetup.apartment1BathroomId)
- bathroomAsset.getAttributes().get("notes").get().setValue("Set via asset merge")
+ bathroomAsset.getAttributes().get(RoomAsset.ROOM_NUMBER).get().setValue(2)
assetStorageService.merge(bathroomAsset)
then: "the restricted client should receive the triggered asset event"
@@ -757,20 +770,19 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
assert restrictedReceivedEvents.size() == 1
}
-
and: "the event should only contain attributes that are read restricted"
conditions.eventually {
assert restrictedReceivedEvents.size() == 1
def event = restrictedReceivedEvents.get(0)
assert event instanceof AssetEvent
def assetEvent = event as AssetEvent
- assert !assetEvent.getAsset().hasAttribute("notes")
+ assert !assetEvent.getAsset().hasAttribute(RoomAsset.ROOM_NUMBER)
}
restrictedReceivedEvents.clear()
when: "the restricted read meta item is added again"
bathroomAsset = assetStorageService.find(managerTestSetup.apartment1BathroomId)
- bathroomAsset.getAttributes().get("notes").get().addOrReplaceMeta(new MetaItem<>(ACCESS_RESTRICTED_READ, true))
+ bathroomAsset.getAttributes().get(RoomAsset.ROOM_NUMBER).get().addOrReplaceMeta(new MetaItem<>(ACCESS_RESTRICTED_READ, true))
assetStorageService.merge(bathroomAsset)
then: "the restricted client should receive the triggered asset event"
@@ -779,8 +791,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def event = restrictedReceivedEvents.get(0)
assert event instanceof AssetEvent
def assetEvent = event as AssetEvent
- assert assetEvent.getAsset().getAttribute("notes").isPresent()
- assert assetEvent.getAsset().getAttribute("notes").get().getValue().orElse(null) == "Set via asset merge"
+ assert assetEvent.getAsset().getAttribute(RoomAsset.ROOM_NUMBER).isPresent()
+ assert assetEvent.getAsset().getAttribute(RoomAsset.ROOM_NUMBER).get().getValue().orElse(null) == 2
}
restrictedReceivedEvents.clear()
@@ -803,7 +815,6 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "no subscriptions should exist in the client"
assert newClient.topicConsumerMap.isEmpty()
-
when: "the restricted mqtt client subscribes to the asset wildcard topic again"
topic = "${keycloakTestSetup.realmBuilding.name}/$newClientId/$DefaultMQTTHandler.ASSET_TOPIC/$MQTTHandler.TOKEN_MULTI_LEVEL_WILDCARD".toString()
newClient.addMessageConsumer(topic, restrictedEventConsumer)
@@ -815,7 +826,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "an asset event occurs on the now unlinked asset"
bathroomAsset = assetStorageService.find(managerTestSetup.apartment1BathroomId)
- bathroomAsset.getAttributes().get("notes").get().setValue("Set via asset merge")
+ bathroomAsset.getAttributes().get(RoomAsset.ROOM_NUMBER).get().setValue(2)
assetStorageService.merge(bathroomAsset)
then: "the restricted client should not receive the triggered asset event"
diff --git a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
index 3f6c01952..2b374c643 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
@@ -19,7 +19,11 @@
*/
package org.openremote.test.protocol.mqtt
-import com.hivemq.client.mqtt.datatypes.MqttQos
+import com.hivemq.client.internal.mqtt.MqttClientConnectionConfig
+import com.hivemq.client.internal.mqtt.handler.disconnect.MqttDisconnectUtil
+import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientView
+import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3ClientConfigView
+import com.hivemq.client.mqtt.MqttClientConfig
import org.bouncycastle.asn1.x500.X500Name
import org.bouncycastle.cert.X509v3CertificateBuilder
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter
@@ -30,20 +34,18 @@ import org.bouncycastle.pkcs.PKCS10CertificationRequest
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder
import org.bouncycastle.util.io.pem.PemObject
import org.bouncycastle.util.io.pem.PemWriter
-import org.openremote.agent.protocol.mqtt.MQTTAgent
-import org.openremote.agent.protocol.mqtt.MQTTAgentLink
-import org.openremote.agent.protocol.mqtt.MQTTProtocol
-import org.openremote.agent.protocol.mqtt.MQTT_IOClient
+import org.openremote.agent.protocol.mqtt.*
import org.openremote.agent.protocol.simulator.SimulatorProtocol
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
-import org.openremote.manager.event.ClientEventService
import org.openremote.manager.mqtt.DefaultMQTTHandler
import org.openremote.manager.mqtt.MQTTBrokerService
import org.openremote.manager.security.KeyStoreServiceImpl
import org.openremote.manager.setup.SetupService
import org.openremote.model.Constants
+import org.openremote.model.asset.Asset
+import org.openremote.model.asset.UserAssetLink
import org.openremote.model.asset.agent.Agent
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.asset.impl.ThingAsset
@@ -53,6 +55,7 @@ import org.openremote.model.attribute.MetaItem
import org.openremote.model.auth.UsernamePassword
import org.openremote.model.query.filter.NumberPredicate
import org.openremote.model.util.UniqueIdentifierGenerator
+import org.openremote.model.util.ValueUtil
import org.openremote.model.value.JsonPathFilter
import org.openremote.model.value.ValueFilter
import org.openremote.setup.integration.KeycloakTestSetup
@@ -73,16 +76,214 @@ import java.security.PrivateKey
import java.security.cert.Certificate
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate
+import java.util.concurrent.CopyOnWriteArrayList
+import java.util.function.Consumer
import static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDString
import static org.openremote.model.Constants.MASTER_REALM
import static org.openremote.model.value.MetaItemType.AGENT_LINK
-import static org.openremote.model.value.ValueType.BOOLEAN
-import static org.openremote.model.value.ValueType.JSON
-import static org.openremote.model.value.ValueType.NUMBER
+import static org.openremote.model.value.ValueType.*
class MQTTClientProtocolTest extends Specification implements ManagerContainerTrait {
+ def setupSpec() {
+ startContainer(defaultConfig(), defaultServices())
+ }
+
+ def "Check MQTT client"() {
+ given: "expected conditions"
+ def conditions = new PollingConditions(timeout: 20, delay: 0.1)
+ def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def mqttBrokerService = container.getService(MQTTBrokerService.class)
+ def defaultMQTTHandler = mqttBrokerService.getCustomHandlers().find {it instanceof DefaultMQTTHandler} as DefaultMQTTHandler
+
+ when: "a hiveMQ client is created to connect to our own broker"
+ List<String> failedSubs = new CopyOnWriteArrayList()
+ List<MQTTMessage<String>> received = new CopyOnWriteArrayList<>()
+ def clientId = "ortest1"
+ def host = "localhost"
+ def port = 1883
+ def secure = false
+ def counter = 0
+ def username = "${keycloakTestSetup.realmBuilding.name}:${keycloakTestSetup.serviceUser2.username}"
+ def password = keycloakTestSetup.serviceUser2.secret
+ def subscriptions = [
+ "${keycloakTestSetup.realmBuilding.name}/${clientId}/attribute/notes/${managerTestSetup.apartment1BathroomId}".toString(),
+ "${keycloakTestSetup.realmBuilding.name}/${clientId}/attribute/notes/${managerTestSetup.apartment1HallwayId}".toString(),
+ "${keycloakTestSetup.realmBuilding.name}/${clientId}/attribute/notes/${managerTestSetup.apartment1Bedroom1Id}".toString(),
+ "${keycloakTestSetup.realmBuilding.name}/${clientId}/attribute/notes/${managerTestSetup.apartment1KitchenId}".toString(),
+ "${keycloakTestSetup.realmBuilding.name}/${clientId}/attribute/notes/${managerTestSetup.apartment1LivingroomId}".toString()
+ ]
+ Consumer<MQTTMessage<String>> consumer = { it ->
+ LOG.info("Received on topic=${it.topic}")
+ received.add(it)
+ }
+ def client = new MQTT_IOClient(
+ clientId,
+ host,
+ port,
+ secure,
+ false,
+ new UsernamePassword(username, password),
+ null,
+ new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/${clientId}/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/notes/${managerTestSetup.apartment1HallwayId}".toString(), "\"LAST WILL\"".toString(), false),
+ null,
+ null
+ )
+ client.setResubscribeIfSessionPresent(false)
+ client.setTopicSubscribeFailureConsumer {
+ LOG.info("Subscription failed: $it")
+ failedSubs.add(it)
+ }
+ client.addConnectionStatusConsumer {
+ LOG.info("Connection status changed: $it")
+ }
+
+ and: "subscriptions are added before connect"
+ client.addMessageConsumer(subscriptions[0], consumer)
+
+ and: "the client connects"
+ client.connect()
+
+ then: "the client should be connected"
+ conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
+ }
+
+ when: "more subscriptions are added"
+ client.addMessageConsumer(subscriptions[1], consumer)
+ client.addMessageConsumer(subscriptions[2], consumer)
+ client.addMessageConsumer(subscriptions[3], consumer)
+ client.addMessageConsumer(subscriptions[4], consumer)
+
+ then: "all consumers should be in place bathroom sub should have failed (because user is not linked to the bathroom)"
+ conditions.eventually {
+ assert client.topicConsumerMap.size() == 5
+ assert failedSubs.size() == 1
+ assert failedSubs[0] == subscriptions[0]
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1
+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 4
+ }
+
+ and: "subscribed attributes are updated"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1BathroomId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1HallwayId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1Bedroom1Id, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1KitchenId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1LivingroomId, Asset.NOTES, Integer.toString(counter++)))
+
+ then: "4 messages should have been received"
+ conditions.eventually {
+ assert received.size() == 4
+ assert received.any {it.topic == subscriptions[1] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "1"}
+ assert received.any {it.topic == subscriptions[2] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "2"}
+ assert received.any {it.topic == subscriptions[3] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "3"}
+ assert received.any {it.topic == subscriptions[4] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "4"}
+ }
+
+ when: "the user asset links are updated to include the bathroom"
+ def existingConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ assetStorageService.storeUserAssetLinks(List.of(
+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),
+ keycloakTestSetup.serviceUser2.getId(),
+ managerTestSetup.apartment1BathroomId)
+ ))
+
+ then: "the client should have disconnected and reconnected"
+ conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
+ assert !mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).isEmpty()
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] != existingConnection
+ }
+
+ then: "all subscriptions including the bathroom should be in place"
+ conditions.eventually {
+ assert client.topicConsumerMap.size() == 5
+ assert client.topicConsumerMap.keySet().stream().allMatch {subscriptions.contains(it)}
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1
+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 5
+ }
+
+ when: "subscribed attributes are updated"
+ counter = 0
+ received.clear()
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1BathroomId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1HallwayId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1Bedroom1Id, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1KitchenId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1LivingroomId, Asset.NOTES, Integer.toString(counter++)))
+
+ then: "5 messages should have been received"
+ conditions.eventually {
+ assert received.size() == 5
+ assert received.any {it.topic == subscriptions[0] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "0"}
+ assert received.any {it.topic == subscriptions[1] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "1"}
+ assert received.any {it.topic == subscriptions[2] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "2"}
+ assert received.any {it.topic == subscriptions[3] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "3"}
+ assert received.any {it.topic == subscriptions[4] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "4"}
+ }
+
+ when: "the client is disconnected due to a connection error"
+ existingConnection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ received.clear()
+ MqttDisconnectUtil.close(((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)client.client).clientConfig).delegate).connectionConfig.get()).channel, "Connection Error")
+
+ then: "the last will message should have been sent"
+ conditions.eventually {
+ def hallway = assetStorageService.find(managerTestSetup.apartment1HallwayId)
+ assert hallway.getAttribute(Asset.NOTES).flatMap{it.value}.orElse("") == "LAST WILL"
+ }
+
+ then: "the client should reconnect"
+ conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
+ assert !mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).isEmpty()
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0] !== existingConnection
+ }
+
+
+ then: "all subscriptions including the bathroom should be in place"
+ conditions.eventually {
+ assert client.topicConsumerMap.size() == 5
+ assert client.topicConsumerMap.keySet().stream().allMatch {subscriptions.contains(it)}
+ assert mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id).size() == 1
+ def connection = mqttBrokerService.getUserConnections(keycloakTestSetup.serviceUser2.id)[0]
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
+ assert defaultMQTTHandler.sessionSubscriptionConsumers.get(getConnectionIDString(connection)).size() == 5
+ }
+
+ when: "subscribed attributes are updated"
+ counter = 0
+ received.clear()
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1BathroomId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1HallwayId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1Bedroom1Id, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1KitchenId, Asset.NOTES, Integer.toString(counter++)))
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment1LivingroomId, Asset.NOTES, Integer.toString(counter++)))
+
+ then: "5 messages should have been received"
+ conditions.eventually {
+ assert received.size() == 5
+ assert received.any {it.topic == subscriptions[0] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "0"}
+ assert received.any {it.topic == subscriptions[1] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "1"}
+ assert received.any {it.topic == subscriptions[2] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "2"}
+ assert received.any {it.topic == subscriptions[3] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "3"}
+ assert received.any {it.topic == subscriptions[4] && ValueUtil.parse(it.payload, AttributeEvent.class).get().value.orElse(null) == "4"}
+ }
+
+ cleanup: "remove the client"
+ if (client != null) {
+ client.disconnect()
+ }
+ }
+
@SuppressWarnings("GroovyAccessibility")
def "Check MQTT client protocol and linked attribute deployment"() {
@@ -90,7 +291,7 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
and: "the container starts"
- def container = startContainer(defaultConfig(), defaultServices())
+
def assetStorageService = container.getService(AssetStorageService.class)
def assetProcessingService = container.getService(AssetProcessingService.class)
def agentService = container.getService(AgentService.class)
@@ -98,7 +299,6 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
def defaultMQTTHandler = brokerService.getCustomHandlers().find{it instanceof DefaultMQTTHandler} as DefaultMQTTHandler
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
- def clientEventService = container.getService(ClientEventService.class)
def mqttHost = brokerService.host
def mqttPort = brokerService.port
@@ -142,7 +342,6 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
assert !(agentService.getProtocolInstance(agent.id) as MQTTProtocol).protocolMessageConsumers.isEmpty()
assert ((agentService.getProtocolInstance(agent.id) as MQTTProtocol).client as MQTT_IOClient).topicConsumerMap.size() == 1
assert ((agentService.getProtocolInstance(agent.id) as MQTTProtocol).client as MQTT_IOClient).topicConsumerMap.get("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}".toString()) != null
- assert ((agentService.getProtocolInstance(agent.id) as MQTTProtocol).client as MQTT_IOClient).topicConsumerMap.get("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}".toString()).key == MqttQos.AT_LEAST_ONCE
def connection = brokerService.getConnectionFromClientID(clientId)
assert connection != null
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -193,7 +392,6 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
assert !(agentService.getProtocolInstance(agent.id) as MQTTProtocol).protocolMessageConsumers.isEmpty()
assert ((agentService.getProtocolInstance(agent.id) as MQTTProtocol).client as MQTT_IOClient).topicConsumerMap.size() == 1
assert ((agentService.getProtocolInstance(agent.id) as MQTTProtocol).client as MQTT_IOClient).topicConsumerMap.get("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}".toString()) != null
- assert ((agentService.getProtocolInstance(agent.id) as MQTTProtocol).client as MQTT_IOClient).topicConsumerMap.get("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}".toString()).key == MqttQos.EXACTLY_ONCE
def connection = brokerService.getConnectionFromClientID(clientId)
assert connection != null
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -206,7 +404,6 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
when: "the container starts"
- def container = startContainer(defaultConfig(), defaultServices())
def assetStorageService = container.getService(AssetStorageService.class)
def assetProcessingService = container.getService(AssetProcessingService.class)
def brokerService = container.getService(MQTTBrokerService.class)
@@ -365,11 +562,8 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
def keyStoreService = container.getService(KeyStoreServiceImpl.class)
def mqttHost = "test.mosquitto.org"
def mqttPort = 8884
-
-
def keystorePassword = "secret"
def keyPassword = "secret"
-
def aliasName = "testalias"
def keyAlias = Constants.MASTER_REALM + "." + aliasName
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
index 25e6b065c..623abfb7d 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
@@ -253,7 +253,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
assert (receivedMessages.get(0) as TriggeredEventSubscription).events.size() == 1
assert (receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) instanceof AssetEvent
assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).id == managerTestSetup.apartment1LivingroomId
- assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.size() == 7
+ assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.size() == 8
assert !((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.contains("testAttribute")
}
diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
index 06b51a667..b58c7aba0 100644
--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
@@ -97,8 +97,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
def realm = new Realm()
realm.setName("test")
realm.setDisplayName("Test")
- realm.setEnabled(true);
- realm.setDuplicateEmailsAllowed(true);
+ realm.setEnabled(true)
+ realm.setDuplicateEmailsAllowed(true)
realm.setRememberMe(true)
realm = identityService.getIdentityProvider().createRealm(realm)
(identityService.getIdentityProvider() as KeycloakIdentityProvider).getRealms(realmsResource -> {
@@ -158,11 +158,16 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
def mqttDevice1SnoopClientId = UniqueIdentifierGenerator.generateId("device1snoop")
List<String> subscribeFailures = new CopyOnWriteArrayList<>()
List<ConnectionStatus> connectionStatuses = new CopyOnWriteArrayList<>()
- Consumer<String> subscribeFailureCallback = {String topic -> subscribeFailures.add(topic)}
+ Consumer<String> subscribeFailureCallback = {String topic ->
+ subscribeFailures.add(topic)
+ LOG.info("device1Client failed to subscribe to topic: ${topic}")
+ }
device1Client = new MQTT_IOClient(mqttDevice1ClientId, mqttHost, mqttPort, false, false, null, null, null)
device1SnoopClient = new MQTT_IOClient(mqttDevice1SnoopClientId, mqttHost, mqttPort, false, false, null, null, null)
device1Client.setTopicSubscribeFailureConsumer(subscribeFailureCallback)
+ device1Client.setRemoveConsumersOnSubscriptionFailure(true)
device1Client.addConnectionStatusConsumer({connectionStatus ->
+ LOG.info("Device 1 connection status changed: $connectionStatus")
connectionStatuses.add(connectionStatus)})
device1Client.connect()
@@ -183,11 +188,12 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null
- assert device1Client.topicConsumerMap.get(device1ResponseTopic).value.size() == 1
+ assert device1Client.topicConsumerMap.get(device1ResponseTopic).consumers.size() == 1
}
when: "an eavesdropping client connects"
device1SnoopClient.setTopicSubscribeFailureConsumer(subscribeFailureCallback)
+ device1SnoopClient.setRemoveConsumersOnSubscriptionFailure(true)
device1SnoopClient.connect()
then: "it should be connected"
@@ -196,11 +202,11 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
}
when: "the eavesdropping client subscribes to the provisioning response topic"
- def result = device1SnoopClient.addMessageConsumer(device1ResponseTopic, device1MessageConsumer)
+ device1SnoopClient.addMessageConsumer(device1ResponseTopic, device1MessageConsumer)
then: "the subscription should have failed"
conditions.eventually {
- assert !result
+ assert subscribeFailures.last == device1ResponseTopic
assert device1SnoopClient.topicConsumerMap.get(device1ResponseTopic) == null
assert subscribeFailures.size() == 1
}
@@ -209,7 +215,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert device1Client.getConnectionStatus() == ConnectionStatus.CONNECTED
assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null
- assert device1Client.topicConsumerMap.get(device1ResponseTopic).value.size() == 1
+ assert device1Client.topicConsumerMap.get(device1ResponseTopic).consumers.size() == 1
}
when: "the client publishes a valid x509 certificate that has been signed by the CA stored in the provisioning config"
@@ -255,7 +261,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert device1Client.topicConsumerMap.get(device1ResponseTopic) != null
- assert device1Client.topicConsumerMap.get(device1ResponseTopic).value.size() == 1
+ assert device1Client.topicConsumerMap.get(device1ResponseTopic).consumers.size() == 1
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null
}
@@ -324,8 +330,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert device1Client.topicConsumerMap.get(assetSubscriptionTopic) != null
assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic) != null
- assert device1Client.topicConsumerMap.get(assetSubscriptionTopic).value.size() == 1
- assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic).value.size() == 1
+ assert device1Client.topicConsumerMap.get(assetSubscriptionTopic).consumers.size() == 1
+ assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic).consumers.size() == 1
connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
assert connection != null
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -412,6 +418,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null
connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
assert connection != null
+ assert !defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
assert subscribeFailures.size() == 2
assert subscribeFailures.contains(assetSubscriptionTopic)
assert subscribeFailures.contains(attributeSubscriptionTopic)
@@ -450,8 +457,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
conditions.eventually {
assert device1Client.topicConsumerMap.get(assetSubscriptionTopic) != null
assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic) != null
- assert device1Client.topicConsumerMap.get(assetSubscriptionTopic).value.size() == 1
- assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic).value.size() == 1
+ assert device1Client.topicConsumerMap.get(assetSubscriptionTopic).consumers.size() == 1
+ assert device1Client.topicConsumerMap.get(attributeSubscriptionTopic).consumers.size() == 1
connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
assert connection != null
assert defaultMQTTHandler.sessionSubscriptionConsumers.containsKey(getConnectionIDString(connection))
@@ -483,7 +490,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
- assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).value.size() == 1
+ assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -532,7 +539,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
- assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).value.size() == 1
+ assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -581,7 +588,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
- assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).value.size() == 1
+ assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -617,17 +624,19 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
device1Responses.clear()
connectionStatuses.clear()
+ subscribeFailures.clear()
provisioningConfig.setDisabled(true)
provisioningConfig = provisioningService.merge(provisioningConfig)
then: "already connected client that was authenticated should be disconnected, then reconnect and should fail to re-subscribe to asset and attribute events"
conditions.eventually {
assert connectionStatuses.size() >= 1
- assert connectionStatuses.get(0) == ConnectionStatus.DISCONNECTED
+ assert connectionStatuses.get(0) == ConnectionStatus.CONNECTING
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != null
assert mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId) != existingConnection
connection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
assert connection != null
+ subscribeFailures.size() == 2
}
when: "the re-connected client re-authenticates"
@@ -663,7 +672,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
- assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).value.size() == 1
+ assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -763,7 +772,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the subscriptions should succeed"
conditions.eventually {
assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic) != null
- assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).value.size() == 1
+ assert deviceNClient.topicConsumerMap.get(deviceNResponseTopic).consumers.size() == 1
assert mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId) != null
}
@@ -783,7 +792,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
when: "a client user is re-enabled"
existingConnection = mqttBrokerService.getConnectionFromClientID(mqttDevice1ClientId)
- def existingNConnection = mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId)
+ mqttBrokerService.getConnectionFromClientID(mqttDeviceNClientId)
device1User.setEnabled(true)
device1User = identityService.getIdentityProvider().createUpdateUser(managerTestSetup.realmBuildingName, device1User, null, true)