Skip to content

armgit / core

Public Imported from GitHub
0

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

Added MQTT last will funcitonality (#782)

Rich Turner8528ead99e18
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 b689f8581..b7b5f2a32 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
@@ -37,6 +37,7 @@ import org.openremote.container.util.UniqueIdentifierGenerator;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.auth.UsernamePassword;
import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.ValueUtil;
import java.net.URI;
import java.util.HashMap;
@@ -73,11 +74,11 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
protected boolean disconnected = true; // Need to use this flag to cancel client reconnect task
protected Consumer<String> topicSubscribeFailureConsumer;
- protected AbstractMQTT_IOClient(String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI) {
- this(UniqueIdentifierGenerator.generateId(), host, port, secure, cleanSession, usernamePassword, websocketURI);
+ protected AbstractMQTT_IOClient(String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill) {
+ this(UniqueIdentifierGenerator.generateId(), host, port, secure, cleanSession, usernamePassword, websocketURI, lastWill);
}
- protected AbstractMQTT_IOClient(String clientId, String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI) {
+ protected AbstractMQTT_IOClient(String clientId, String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill) {
this.clientId = clientId;
this.host = host;
this.port = port;
@@ -142,6 +143,14 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
.serverPort(port);
}
+ if (lastWill != null) {
+ builder.willPublish()
+ .topic(lastWill.getTopic())
+ .payload(ValueUtil.getStringCoerced(lastWill.getPayload()).orElse("").getBytes())
+ .retain(lastWill.isRetain())
+ .applyWillPublish();
+ }
+
try {
client = builder.buildAsync();
} catch (Exception e) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java
index 099caa3b8..d234a19c3 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java
@@ -19,10 +19,9 @@
*/
package org.openremote.agent.protocol.mqtt;
+import com.fasterxml.jackson.databind.node.BaseJsonNode;
import org.openremote.agent.protocol.io.IOAgent;
import org.openremote.model.asset.agent.AgentDescriptor;
-import org.openremote.model.attribute.Attribute;
-import org.openremote.model.util.ModelIgnore;
import org.openremote.model.value.AttributeDescriptor;
import org.openremote.model.value.ValueType;
@@ -40,6 +39,9 @@ public class MQTTAgent extends IOAgent<MQTTAgent, MQTTProtocol, MQTTAgentLink> {
public static final AttributeDescriptor<Boolean> WEBSOCKET_MODE = new AttributeDescriptor<>("websocketMode", ValueType.BOOLEAN);
public static final AttributeDescriptor<String> WEBSOCKET_PATH = new AttributeDescriptor<>("websocketPath", ValueType.TEXT);
public static final AttributeDescriptor<String> WEBSOCKET_QUERY = new AttributeDescriptor<>("websocketQuery", ValueType.TEXT);
+ public static final AttributeDescriptor<String> LAST_WILL_TOPIC = new AttributeDescriptor<>("lastWillTopic", ValueType.TEXT);
+ public static final AttributeDescriptor<BaseJsonNode> LAST_WILL_PAYLOAD = new AttributeDescriptor<>("lastWillPayload", ValueType.JSON);
+ public static final AttributeDescriptor<Boolean> LAST_WILL_RETAIN = new AttributeDescriptor<>("lastWillRetain", ValueType.BOOLEAN);
public static final AgentDescriptor<MQTTAgent, MQTTProtocol, MQTTAgentLink> DESCRIPTOR = new AgentDescriptor<>(
MQTTAgent.class, MQTTProtocol.class, MQTTAgentLink.class
@@ -113,4 +115,31 @@ public class MQTTAgent extends IOAgent<MQTTAgent, MQTTProtocol, MQTTAgentLink> {
getAttributes().getOrCreate(WEBSOCKET_QUERY).setValue(websocketQuery);
return this;
}
+
+ public Optional<String> getLastWillTopic() {
+ return getAttributes().getValue(LAST_WILL_TOPIC);
+ }
+
+ public MQTTAgent setLastWillTopic(String lastWillTopic) {
+ getAttributes().getOrCreate(LAST_WILL_TOPIC).setValue(lastWillTopic);
+ return this;
+ }
+
+ public Optional<BaseJsonNode> getLastWillPayload() {
+ return getAttributes().getValue(LAST_WILL_PAYLOAD);
+ }
+
+ public MQTTAgent setLastWillPayload(BaseJsonNode lastWillPayload) {
+ getAttributes().getOrCreate(LAST_WILL_PAYLOAD).setValue(lastWillPayload);
+ return this;
+ }
+
+ public Optional<Boolean> isLastWillRetain() {
+ return getAttributes().getValue(LAST_WILL_RETAIN);
+ }
+
+ public MQTTAgent setLastWillRetain(boolean lastWillRetain) {
+ getAttributes().getOrCreate(LAST_WILL_RETAIN).setValue(lastWillRetain);
+ return this;
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.java
new file mode 100644
index 000000000..e9dc2647b
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2022, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol.mqtt;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.databind.JsonNode;
+
+/**
+ * Represents last will configuration for a client
+ */
+public class MQTTLastWill {
+ String topic;
+ JsonNode payload;
+ boolean retain;
+
+ @JsonCreator
+ public MQTTLastWill(String topic, JsonNode payload, boolean retain) {
+ this.topic = topic;
+ this.payload = payload;
+ this.retain = retain;
+ }
+
+ public String getTopic() {
+ return topic;
+ }
+
+ public JsonNode getPayload() {
+ return payload;
+ }
+
+ public boolean isRetain() {
+ return retain;
+ }
+}
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 34cd47582..633337ffb 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
@@ -19,6 +19,7 @@
*/
package org.openremote.agent.protocol.mqtt;
+import com.fasterxml.jackson.databind.node.BaseJsonNode;
import org.apache.http.client.utils.URIBuilder;
import org.openremote.container.util.UniqueIdentifierGenerator;
import org.openremote.model.attribute.Attribute;
@@ -99,7 +100,16 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA
websocketURI = builder.build();
}
- return new MQTT_IOClient(agent.getClientId().orElseGet(UniqueIdentifierGenerator::generateId), host, port, agent.isSecureMode().orElse(false), !agent.isResumeSession().orElse(false), agent.getUsernamePassword().orElse(null), websocketURI);
+ MQTTLastWill lastWill = null;
+
+ if (agent.getLastWillTopic().isPresent()) {
+ String topic = agent.getLastWillTopic().get();
+ BaseJsonNode payload = agent.getLastWillPayload().orElse(null);
+ boolean retain = agent.isLastWillRetain().orElse(false);
+ lastWill = new MQTTLastWill(topic, payload, retain);
+ }
+
+ return new MQTT_IOClient(agent.getClientId().orElseGet(UniqueIdentifierGenerator::generateId), host, port, agent.isSecureMode().orElse(false), !agent.isResumeSession().orElse(false), agent.getUsernamePassword().orElse(null), websocketURI, lastWill);
}
@Override
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 93fd7b531..cca96208f 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
@@ -26,12 +26,12 @@ 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) {
- super(host, port, secure, cleanSession, usernamePassword, websocketURI);
+ public MQTT_IOClient(String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill) {
+ super(host, port, secure, cleanSession, usernamePassword, websocketURI, lastWill);
}
- public MQTT_IOClient(String clientId, String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI) {
- super(clientId, host, port, secure, cleanSession, usernamePassword, websocketURI);
+ public MQTT_IOClient(String clientId, String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill) {
+ super(clientId, host, port, secure, cleanSession, usernamePassword, websocketURI, lastWill);
}
@Override
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 6d4b09a81..9ddf3145a 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
@@ -61,7 +61,7 @@ import static org.openremote.model.syslog.SyslogCategory.API;
*/
public class DefaultMQTTHandler extends MQTTHandler {
- public static final int PRIORITY = 1000;
+ public static final int PRIORITY = Integer.MIN_VALUE + 1000;
public static final String ASSET_TOPIC = "asset";
public static final String ATTRIBUTE_TOPIC = "attribute";
public static final String ATTRIBUTE_WRITE_TOPIC = "writeattribute";
@@ -69,11 +69,6 @@ public class DefaultMQTTHandler extends MQTTHandler {
public static final String ATTRIBUTE_VALUE_WRITE_TOPIC = "writeattributevalue";
private static final Logger LOG = SyslogCategory.getLogger(API, DefaultMQTTHandler.class);
protected AssetStorageService assetStorageService;
- protected ClientEventService clientEventService;
- protected MqttBrokerService mqttBrokerService;
- protected MessageBrokerService messageBrokerService;
- protected ManagerKeycloakIdentityProvider identityProvider;
- protected boolean isKeycloak;
public static boolean isAttributeTopic(Topic topic) {
return ATTRIBUTE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2)) || ATTRIBUTE_VALUE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));
@@ -100,23 +95,11 @@ public class DefaultMQTTHandler extends MQTTHandler {
@Override
public void start(Container container) throws Exception {
super.start(container);
- ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);
assetStorageService = container.getService(AssetStorageService.class);
- clientEventService = container.getService(ClientEventService.class);
- messageBrokerService = container.getService(MessageBrokerService.class);
- mqttBrokerService = container.getService(MqttBrokerService.class);
-
- if (!identityService.isKeycloakEnabled()) {
- LOG.warning("MQTT connections are not supported when not using Keycloak identity provider");
- isKeycloak = false;
- } else {
- isKeycloak = true;
- identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();
- }
}
@Override
- public void onConnect(MqttConnection connection, InterceptConnectMessage msg) {
+ public boolean onConnect(MqttConnection connection, InterceptConnectMessage msg) {
super.onConnect(connection, msg);
// Moquette is awful and client ID rather than socket descriptors are used everywhere so cannot distinguish
@@ -138,6 +121,7 @@ public class DefaultMQTTHandler extends MQTTHandler {
headers.put(ConnectionConstants.SESSION_TERMINATOR, closeRunnable);
messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
LOG.fine("Connected: " + connection);
+ return true;
}
@Override
@@ -504,9 +488,7 @@ public class DefaultMQTTHandler extends MQTTHandler {
mqttBrokerService.publishMessage(topicExpander.apply(ev), ev, mqttQoS);
}
} else {
- if (ev instanceof AttributeEvent) {
- AttributeEvent attributeEvent = (AttributeEvent) ev;
-
+ if (ev instanceof AttributeEvent attributeEvent) {
if (isValueSubscription) {
mqttBrokerService.publishMessage(topicExpander.apply(ev), attributeEvent.getValue().orElse(null), mqttQoS);
} else {
@@ -516,13 +498,4 @@ public class DefaultMQTTHandler extends MQTTHandler {
}
};
}
-
- public static Map<String, Object> prepareHeaders(MqttConnection connection) {
- Map<String, Object> headers = new HashMap<>();
- headers.put(ConnectionConstants.SESSION_KEY, connection.getClientId());
- headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);
- headers.put(Constants.AUTH_CONTEXT, connection.getAuthContext());
- headers.put(Constants.REALM_PARAM_NAME, connection.getRealm());
- return headers;
- }
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/LastWillMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/LastWillMQTTHandler.java
new file mode 100644
index 000000000..e761991b3
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/mqtt/LastWillMQTTHandler.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2022, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.manager.mqtt;
+
+import io.moquette.broker.subscriptions.Topic;
+import io.moquette.interception.messages.InterceptConnectMessage;
+import io.moquette.interception.messages.InterceptPublishMessage;
+import io.moquette.interception.messages.InterceptSubscribeMessage;
+import io.moquette.interception.messages.InterceptUnsubscribeMessage;
+import org.openremote.model.Constants;
+import org.openremote.model.Container;
+import org.openremote.model.security.ClientRole;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.TextUtil;
+
+import java.util.logging.Logger;
+
+import static org.openremote.model.syslog.SyslogCategory.API;
+
+/**
+ * This handler supports last will publishes and subscriptions; the publish is verified at connect time as normal
+ * {@link #canPublish} method isn't called for last will publishes. Clients can subscribe to any last will message so
+ * the {@link org.openremote.model.security.ClientRole#READ_LAST_WILL} should be used sparingly.
+ * Topic format is `{REALM}/lastwill/{IDENTIFIER}` the IDENTIFIER can be any string and wildcard subscriptions are allowed.
+ */
+// TODO: Introduce role for read/write last will
+public class LastWillMQTTHandler extends MQTTHandler {
+
+ public static final int PRIORITY = DefaultMQTTHandler.PRIORITY + 1000;
+ public static final String LAST_WILL_TOKEN = "last_will";
+ private static final Logger LOG = SyslogCategory.getLogger(API, LastWillMQTTHandler.class);
+
+ @Override
+ public void start(Container container) throws Exception {
+ super.start(container);
+ mqttBrokerService = container.getService(MqttBrokerService.class);
+ }
+
+ @Override
+ public boolean handlesTopic(Topic topic) {
+ // Skip standard checks
+ return topicMatches(topic);
+ }
+
+ @Override
+ protected boolean topicMatches(Topic topic) {
+ return isLastWillTopic(topic);
+ }
+
+ @Override
+ protected Logger getLogger() {
+ return LOG;
+ }
+
+ @Override
+ public boolean onConnect(MqttConnection connection, InterceptConnectMessage msg) {
+ super.onConnect(connection, msg);
+
+ // Force disconnect if last will topic is in a different realm
+ if (msg.isWillFlag()) {
+ Topic topic = TextUtil.isNullOrEmpty(msg.getWillTopic()) ? null : Topic.asTopic(msg.getWillTopic());
+ if (topic == null || !isLastWillTopic(topic)) {
+ getLogger().warning("Last will topic must be in the format `{REALM}/" + LAST_WILL_TOKEN + "/IDENTIFIER' client will be disconnected: " + connection);
+ Runnable closeRunnable = mqttBrokerService.getForceDisconnectRunnable(connection.getClientId());
+ closeRunnable.run();
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public boolean checkCanSubscribe(MqttConnection connection, Topic topic) {
+ if (connection.getAuthContext() == null) {
+ getLogger().fine("Anonymous connection subscriptions not supported by this handler, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ if (!topicRealmMatchesConnection(connection, topic)) {
+ getLogger().fine("Topic realm token must match the connection, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ if (!canSubscribe(connection, topic)) {
+ getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public boolean checkCanPublish(MqttConnection connection, Topic topic) {
+ // Last will publish doesn't reach here so we verify it at the connect stage and we just disallow client side
+ // publishes using this handler
+ return false;
+ }
+
+ @Override
+ public boolean canSubscribe(MqttConnection connection, Topic topic) {
+ return isLastWillTopic(topic) && connection.getAuthContext().hasResourceRole(ClientRole.READ_LAST_WILL.getValue(), Constants.KEYCLOAK_CLIENT_ID);
+ }
+
+ @Override
+ public boolean canPublish(MqttConnection connection, Topic topic) {
+ return false;
+ }
+
+ @Override
+ public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {
+ // Nothing to do here - standard broker routing between subscribers and publishers
+ }
+
+ @Override
+ public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {
+ // Nothing to do here - standard broker routing between subscribers and publishers
+ }
+
+ @Override
+ public void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {
+ // Nothing to do here - standard broker routing between subscribers and publishers
+ }
+
+ protected static boolean isLastWillTopic(Topic topic) {
+ return LAST_WILL_TOKEN.equals(topicTokenIndexToString(topic, 1))
+ && topic.getTokens().size() == 3;
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
index 706b1958c..d09b2ba87 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
@@ -21,8 +21,16 @@ package org.openremote.manager.mqtt;
import io.moquette.broker.subscriptions.Topic;
import io.moquette.interception.messages.*;
+import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.web.ConnectionConstants;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.security.ManagerIdentityService;
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.Constants;
import org.openremote.model.Container;
+import java.util.HashMap;
+import java.util.Map;
import java.util.logging.Logger;
/**
@@ -34,6 +42,11 @@ public abstract class MQTTHandler {
public static final String TOKEN_MULTI_LEVEL_WILDCARD = "#";
public static final String TOKEN_SINGLE_LEVEL_WILDCARD = "+";
+ protected ClientEventService clientEventService;
+ protected MqttBrokerService mqttBrokerService;
+ protected MessageBrokerService messageBrokerService;
+ protected ManagerKeycloakIdentityProvider identityProvider;
+ protected boolean isKeycloak;
/**
* Gets the priority of this handler which is used to determine the call order; handlers with a lower priority are
@@ -54,7 +67,18 @@ public abstract class MQTTHandler {
* Called when the system starts to allow for initialisation.
*/
public void start(Container container) throws Exception {
-
+ mqttBrokerService = container.getService(MqttBrokerService.class);
+ clientEventService = container.getService(ClientEventService.class);
+ messageBrokerService = container.getService(MessageBrokerService.class);
+ ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);
+
+ if (!identityService.isKeycloakEnabled()) {
+ getLogger().warning("MQTT connections are not supported when not using Keycloak identity provider");
+ isKeycloak = false;
+ } else {
+ isKeycloak = true;
+ identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();
+ }
}
/**
@@ -65,10 +89,10 @@ public abstract class MQTTHandler {
}
/**
- * Will be called when any client connects
+ * Will be called when any client connects; if returns false then subsequent handlers will not be called
*/
- public void onConnect(MqttConnection connection, InterceptConnectMessage msg) {
-
+ public boolean onConnect(MqttConnection connection, InterceptConnectMessage msg) {
+ return true;
}
/**
@@ -110,7 +134,7 @@ public abstract class MQTTHandler {
getLogger().fine("Anonymous connection subscriptions not supported by this handler, topic=" + topic + ", connection" + connection);
return false;
}
- if (!topicRealmAndClientIdMatchesConnection(connection, topic)) {
+ if (!topicRealmMatchesConnection(connection, topic) || !topicClientIdMatchesConnection(connection, topic)) {
getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", connection" + connection);
return false;
}
@@ -130,7 +154,7 @@ public abstract class MQTTHandler {
getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic + ", connection" + connection);
return false;
}
- if (!topicRealmAndClientIdMatchesConnection(connection, topic)) {
+ if (!topicRealmMatchesConnection(connection, topic) || !topicClientIdMatchesConnection(connection, topic)) {
getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", connection" + connection);
return false;
}
@@ -176,11 +200,11 @@ public abstract class MQTTHandler {
*/
public abstract void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg);
- public static boolean topicRealmAndClientIdMatchesConnection(MqttConnection connection, Topic topic) {
- if (!connection.realm.equals(topicTokenIndexToString(topic, 0))) {
- return false;
- }
+ public static boolean topicRealmMatchesConnection(MqttConnection connection, Topic topic) {
+ return connection.realm.equals(topicTokenIndexToString(topic, 0));
+ }
+ public static boolean topicClientIdMatchesConnection(MqttConnection connection, Topic topic) {
return connection.clientId.equals(topicTokenIndexToString(topic, 1));
}
@@ -191,4 +215,13 @@ public abstract class MQTTHandler {
public static String topicTokenIndexToString(Topic topic, int tokenNumber) {
return topicTokenCountGreaterThan(topic, tokenNumber) ? topic.getTokens().get(tokenNumber).toString() : null;
}
+
+ public static Map<String, Object> prepareHeaders(MqttConnection connection) {
+ Map<String, Object> headers = new HashMap<>();
+ headers.put(ConnectionConstants.SESSION_KEY, connection.getClientId());
+ headers.put(ClientEventService.HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);
+ headers.put(Constants.AUTH_CONTEXT, connection.getAuthContext());
+ headers.put(Constants.REALM_PARAM_NAME, connection.getRealm());
+ return headers;
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
index b2baf1e93..f7a90d079 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
@@ -86,7 +86,12 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
brokerService.addConnection(connection.getClientId(), connection);
// Notify all custom handlers
- brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnect(connection, msg));
+ for (MQTTHandler handler : brokerService.getCustomHandlers()) {
+ if (!handler.onConnect(connection, msg)) {
+ LOG.info("Handler returned false from onConnect so not passing to other handlers: " + handler.getName());
+ break;
+ }
+ }
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java
index 2c48edc8a..e9ef5f76d 100644
--- a/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java
@@ -144,13 +144,21 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {
@Override
public boolean checkCanSubscribe(MqttConnection connection, Topic topic) {
// Skip standard checks
- return canSubscribe(connection, topic);
+ if (!canSubscribe(connection, topic)) {
+ getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ return true;
}
@Override
public boolean checkCanPublish(MqttConnection connection, Topic topic) {
// Skip standard checks
- return canPublish(connection, topic);
+ if (!canPublish(connection, topic)) {
+ getLogger().fine("Cannot publish to this topic, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ return true;
}
@Override
diff --git a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler
index 39e1cd182..a3854e4bc 100644
--- a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler
+++ b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler
@@ -1,2 +1,3 @@
org.openremote.manager.mqtt.DefaultMQTTHandler
+org.openremote.manager.mqtt.LastWillMQTTHandler
org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler
diff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.java
index 7871a0802..cf2769941 100644
--- a/model/src/main/java/org/openremote/model/Constants.java
+++ b/model/src/main/java/org/openremote/model/Constants.java
@@ -34,6 +34,7 @@ public interface Constants {
String READ_MAP_ROLE = "read:map";
String READ_ASSETS_ROLE = "read:assets";
String READ_RULES_ROLE = "read:rules";
+ String READ_LAST_WILL = "read:lastwill";
String WRITE_USER_ROLE = "write:user";
String WRITE_ADMIN_ROLE = "write:admin";
String WRITE_LOGS_ROLE = "write:logs";
diff --git a/model/src/main/java/org/openremote/model/security/ClientRole.java b/model/src/main/java/org/openremote/model/security/ClientRole.java
index 42a4ffd9c..ee0e8d5d3 100644
--- a/model/src/main/java/org/openremote/model/security/ClientRole.java
+++ b/model/src/main/java/org/openremote/model/security/ClientRole.java
@@ -39,6 +39,7 @@ public enum ClientRole {
READ_MAP(Constants.READ_MAP_ROLE, "View map"),
READ_ASSETS(Constants.READ_ASSETS_ROLE, "Read asset data"),
READ_RULES(Constants.READ_RULES_ROLE, "Read rulesets"),
+ READ_LAST_WILL(Constants.READ_LAST_WILL, "Subscribe to last will messages"),
WRITE_USER(Constants.WRITE_USER_ROLE, "Write data of the authenticated user"),
WRITE_ADMIN(Constants.WRITE_ADMIN_ROLE, "Write system settings, realms, and users"),
@@ -53,7 +54,8 @@ public enum ClientRole {
READ_USERS,
READ_MAP,
READ_ASSETS,
- READ_RULES
+ READ_RULES,
+ READ_LAST_WILL
}),
WRITE("write", "Write all data", new ClientRole[]{
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 5309deedf..32df2f43f 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -1,7 +1,12 @@
package org.openremote.test.mqtt
-
+import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3AsyncClientView
+import com.hivemq.client.internal.mqtt.mqtt3.Mqtt3ClientConfigView
+import com.hivemq.client.mqtt.MqttClientConfig
+import com.hivemq.client.mqtt.MqttClientConnectionConfig
import io.moquette.BrokerConstants
+import io.netty.channel.socket.SocketChannel
+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.agent.protocol.simulator.SimulatorProtocol
@@ -10,6 +15,7 @@ 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.LastWillMQTTHandler
import org.openremote.manager.mqtt.MQTTHandler
import org.openremote.manager.mqtt.MqttBrokerService
import org.openremote.manager.setup.SetupService
@@ -59,7 +65,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "a mqtt client connects with invalid credentials"
def wrongUsername = "master:" + keycloakTestSetup.serviceUser.username
- MQTT_IOClient client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(wrongUsername, password), null)
+ MQTT_IOClient client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(wrongUsername, password), null, null)
client.connect()
then: "the client connection status should be in error"
@@ -70,7 +76,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
when: "a mqtt client connects with valid credentials"
client.disconnect()
mqttClientId = UniqueIdentifierGenerator.generateId()
- client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null)
+ client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, null)
client.connect()
then: "mqtt connection should exist"
@@ -441,12 +447,46 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
assert receivedEvents.size() == 0
}
- when: "MQTT client disconnects"
+ when: "another client connects with a last will configured"
+ newClientId = "newClient"
+ MQTT_IOClient newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/${LastWillMQTTHandler.LAST_WILL_TOKEN}/new_device", ValueUtil.parse("123").orElse(null), false))
+ newClient.connect()
+
+ then: "the client should be connected"
+ conditions.eventually {
+ assert newClient.getConnectionStatus() == ConnectionStatus.CONNECTED
+ }
+
+ when: "the existing client subscribes to the last will topic"
+ MQTTMessage<String> receivedLastWillMessage
+ client.addMessageConsumer("${keycloakTestSetup.realmBuilding.name}/${LastWillMQTTHandler.LAST_WILL_TOKEN}/#", {receivedLastWillMessage = it})
+
+ and: "the new client gets abruptly disconnected"
+ def existingConnection = mqttBrokerService.clientIdConnectionMap.get(newClientId)
+// ((NioSocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).config().setOption(ChannelOption.SO_LINGER, 0I)
+ ((SocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)newClient.client).clientConfig).delegate).connectionConfig.get()).channel).close()
+
+ then: "the client should reconnect"
+ conditions.eventually {
+ assert mqttBrokerService.clientIdConnectionMap.get(newClientId) != null
+ assert mqttBrokerService.clientIdConnectionMap.get(newClientId) !== existingConnection
+ }
+
+ and: "the existing client should have received the last will message"
+ conditions.eventually {
+ assert receivedLastWillMessage != null
+ assert receivedLastWillMessage.topic == "${keycloakTestSetup.realmBuilding.name}/${LastWillMQTTHandler.LAST_WILL_TOKEN}/new_device"
+ assert receivedLastWillMessage.payload == "123"
+ }
+
+ when: "both MQTT clients disconnect"
client.disconnect()
+ newClient.disconnect()
- then: "no connection should be left"
+ then: "no connections should be left"
conditions.eventually {
assert client.getConnectionStatus() == ConnectionStatus.DISCONNECTED
+ assert newClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED
assert mqttBrokerService.clientIdConnectionMap.size() == 0
}
}
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 bc9cb0585..db7fb6e59 100644
--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
@@ -150,7 +150,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
List<String> subscribeFailures = []
List<ConnectionStatus> connectionStatuses = []
Consumer<String> subscribeFailureCallback = {String topic -> subscribeFailures.add(topic)}
- MQTT_IOClient device1Client = new MQTT_IOClient(mqttDevice1ClientId, mqttHost, mqttPort, false, false, null, null)
+ MQTT_IOClient device1Client = new MQTT_IOClient(mqttDevice1ClientId, mqttHost, mqttPort, false, false, null, null, null)
device1Client.setTopicSubscribeFailureConsumer(subscribeFailureCallback)
device1Client.addConnectionStatusConsumer({connectionStatus ->
connectionStatuses.add(connectionStatus)})
@@ -205,7 +205,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
then: "the client should reconnect"
conditions.eventually {
assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != null
- assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) != existingConnection
+ assert mqttBrokerService.clientIdConnectionMap.get(mqttDevice1ClientId) !== existingConnection
}
then: "the subscriptions should succeed"
@@ -384,7 +384,7 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerConta
def deviceNRequestTopic = "$PROVISIONING_TOKEN/$deviceNUniqueId/$REQUEST_TOKEN".toString()
def deviceNResponseTopic = "$PROVISIONING_TOKEN/$deviceNUniqueId/$RESPONSE_TOKEN".toString()
def mqttDeviceNClientId = UniqueIdentifierGenerator.generateId("deviceN")
- MQTT_IOClient deviceNClient = new MQTT_IOClient(mqttDeviceNClientId, mqttHost, mqttPort, false, false, null, null)
+ MQTT_IOClient deviceNClient = new MQTT_IOClient(mqttDeviceNClientId, mqttHost, mqttPort, false, false, null, null, null)
deviceNClient.setTopicSubscribeFailureConsumer(subscribeFailureCallback)
deviceNClient.connect()
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index 6e00a4901..4e4684e49 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -274,12 +274,12 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
def lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION), AttributeEvent.Source.CLIENT)
- and: "the engine fires at least one more time"
+ then: "the engine fires at least one more time"
conditions.eventually {
assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
}
- and: "the console device moves outside the home geofence again (as defined in the rule)"
+ when: "the console device moves outside the home geofence again (as defined in the rule)"
assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(0d, 0d)), AttributeEvent.Source.CLIENT)
then: "another notification should have been sent to the console"
diff --git a/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java b/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java
index f8c0b2d41..993a16938 100644
--- a/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java
+++ b/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java
@@ -113,7 +113,7 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {
realmBuilding.getName(),
serviceUser.getId(),
Constants.KEYCLOAK_CLIENT_ID,
- Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES).map(ClientRole::getValue).toArray(String[]::new)
+ Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES, ClientRole.READ_LAST_WILL).map(ClientRole::getValue).toArray(String[]::new)
);
}
}