Skip to content

armgit / core

Public Imported from GitHub
0

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

Refactor of MQTT broker (asset/attribute topic convention has also been updated)

Rich Turner279496aae132
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 eb44d2dee..fb54306b8 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
@@ -157,9 +157,6 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
}
private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractNettyIOClient.class);
- protected final static long RECONNECT_DELAY_INITIAL_MILLIS = 1000L;
- protected final static long RECONNECT_DELAY_MAX_MILLIS = 5*60000L;
- protected final static long RECONNECT_DELAY_JITTER_MILLIS = 10000L;
protected final List<Consumer<T>> messageConsumers = new ArrayList<>();
protected final List<Consumer<ConnectionStatus>> connectionStatusConsumers = new ArrayList<>();
protected ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java
index 226005be4..a98bcf262 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/IOClient.java
@@ -30,6 +30,10 @@ import java.util.function.Consumer;
*/
public interface IOClient<T> {
+ long RECONNECT_DELAY_INITIAL_MILLIS = 1000L;
+ long RECONNECT_DELAY_MAX_MILLIS = 5*60000L;
+ long RECONNECT_DELAY_JITTER_MILLIS = 10000L;
+
/**
* Send a message over the wire
*/
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 c27024e3f..4e6a7326f 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
@@ -20,14 +20,18 @@
package org.openremote.agent.protocol.mqtt;
import com.hivemq.client.mqtt.MqttClient;
-import com.hivemq.client.mqtt.MqttClientState;
-import com.hivemq.client.mqtt.MqttWebSocketConfig;
-import com.hivemq.client.mqtt.MqttWebSocketConfigBuilder;
+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.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 org.openremote.agent.protocol.io.IOClient;
+import org.openremote.container.Container;
import org.openremote.container.util.UniqueIdentifierGenerator;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.auth.UsernamePassword;
@@ -39,6 +43,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -47,6 +53,7 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S>> {
+ public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractMQTT_IOClient.class);
protected String clientId;
protected String host;
protected int port;
@@ -56,13 +63,14 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
protected Mqtt3AsyncClient client;
protected final Set<Consumer<ConnectionStatus>> connectionStatusConsumers = new HashSet<>();
protected final Map<String, Set<Consumer<MQTTMessage<S>>>> topicConsumerMap = new HashMap<>();
- public static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractMQTT_IOClient.class);
+ protected ScheduledExecutorService executorService;
+ protected ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;
+ protected boolean disconnected = true; // Need to use this flag to cancel client reconnect task
protected AbstractMQTT_IOClient(String host, int port, boolean secure, UsernamePassword usernamePassword, URI websocketURI) {
this(UniqueIdentifierGenerator.generateId(), host, port, secure, usernamePassword, websocketURI);
}
- @SuppressWarnings("unchecked")
protected AbstractMQTT_IOClient(String clientId, String host, int port, boolean secure, UsernamePassword usernamePassword, URI websocketURI) {
this.clientId = clientId;
this.host = host;
@@ -70,10 +78,44 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
this.secure = secure;
this.usernamePassword = usernamePassword;
this.websocketURI = websocketURI;
+ this.executorService = Container.EXECUTOR_SERVICE;
Mqtt3ClientBuilder builder = MqttClient.builder()
.useMqttVersion3()
- .identifier(clientId);
+ .identifier(clientId)
+ .addConnectedListener(context -> {
+ LOG.info("Client is connected from the broker '" + getClientUri() + "'");
+ onConnectionStatusChanged(ConnectionStatus.CONNECTED);
+ })
+ .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();
+ }
+ }
+ 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());
+ }
+ onConnectionStatusChanged(userClosed ? ConnectionStatus.DISCONNECTED : ConnectionStatus.WAITING);
+ })
+ .automaticReconnect()
+ .initialDelay(RECONNECT_DELAY_INITIAL_MILLIS, TimeUnit.MILLISECONDS)
+ .maxDelay(RECONNECT_DELAY_MAX_MILLIS, TimeUnit.MILLISECONDS)
+ .applyAutomaticReconnect();
if (secure) {
builder = builder.sslWithDefaultConfig();
@@ -82,12 +124,11 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
if (websocketURI != null) {
builder = builder
.serverHost(websocketURI.getHost())
- .serverPort(websocketURI.getPort());
-
- MqttWebSocketConfigBuilder webSocketConfigBuilder = MqttWebSocketConfig.builder()
- .serverPath(websocketURI.getPath())
- .queryString(websocketURI.getQuery());
- builder = builder.webSocketConfig(webSocketConfigBuilder.build());
+ .serverPort(websocketURI.getPort())
+ .webSocketConfig()
+ .serverPath(websocketURI.getPath())
+ .queryString(websocketURI.getQuery())
+ .applyWebSocketConfig();
} else {
builder = builder
.serverHost(host)
@@ -99,6 +140,7 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
} catch (Exception e) {
LOG.log(Level.WARNING, "Invalid MQTT client config for client '" + getClientUri() + "'", e);
client = null;
+ connectionStatus = ConnectionStatus.ERROR;
}
}
@@ -134,9 +176,16 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
return;
}
- Set<Consumer<MQTTMessage<S>>> consumers = topicConsumerMap.computeIfAbsent(topic, (t) -> new HashSet<>());
- boolean initialise = consumers.isEmpty();
- consumers.add(messageConsumer);
+ boolean initialise;
+ Set<Consumer<MQTTMessage<S>>> consumers;
+
+ synchronized (topicConsumerMap) {
+ consumers = topicConsumerMap.computeIfAbsent(topic, (t) -> new HashSet<>());
+ synchronized (consumers) {
+ initialise = consumers.isEmpty();
+ consumers.add(messageConsumer);
+ }
+ }
if (initialise) {
addSubscription(
@@ -145,16 +194,26 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
if (!topicConsumerMap.containsKey(topic)) {
return;
}
-
- consumers.forEach(consumer -> {
- try {
- consumer.accept(message);
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Message consumer threw an exception", e);
- }
- });
+ synchronized (consumers) {
+ consumers.forEach(consumer -> {
+ try {
+ consumer.accept(message);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Message consumer threw an exception", e);
+ }
+ });
+ }
},
- () -> consumers.remove(messageConsumer));
+ () -> {
+ synchronized (consumers) {
+ consumers.remove(messageConsumer);
+ if (consumers.isEmpty()) {
+ synchronized (topicConsumerMap) {
+ topicConsumerMap.remove(topic);
+ }
+ }
+ }
+ });
}
}
@@ -164,20 +223,27 @@ 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.remove(messageConsumer) && consumers.isEmpty()) {
- removeSubscription(topic);
- return null;
- }
- return consumers;
- });
+ synchronized (topicConsumerMap) {
+ topicConsumerMap.computeIfPresent(topic, (t, consumers) -> {
+ //noinspection SynchronizationOnLocalVariableOrMethodParameter
+ synchronized (consumers) {
+ if (consumers.remove(messageConsumer) && consumers.isEmpty()) {
+ removeSubscription(topic);
+ return null;
+ }
+ return consumers;
+ }
+ });
+ }
}
@Override
public void removeAllMessageConsumers() {
- Set<String> topics = new HashSet<>(topicConsumerMap.keySet());
- topicConsumerMap.clear();
- topics.forEach(this::removeSubscription);
+ synchronized (topicConsumerMap) {
+ Set<String> topics = new HashSet<>(topicConsumerMap.keySet());
+ topicConsumerMap.clear();
+ topics.forEach(this::removeSubscription);
+ }
}
protected void addSubscription(String topic, Consumer<MQTTMessage<S>> onPublish, Runnable onFailure) {
@@ -236,38 +302,26 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
@Override
public ConnectionStatus getConnectionStatus() {
- if (client != null) {
- MqttClientState state = client.getState();
- switch (state) {
-
- case DISCONNECTED:
- return ConnectionStatus.DISCONNECTED;
- case CONNECTING:
- case CONNECTING_RECONNECT:
- return ConnectionStatus.CONNECTING;
- case CONNECTED:
- return ConnectionStatus.CONNECTED;
- case DISCONNECTED_RECONNECT:
- return ConnectionStatus.WAITING;
- }
- }
-
- return ConnectionStatus.ERROR;
+ return connectionStatus;
}
@Override
public void connect() {
- if (client == null) {
- LOG.info("Cannot connect as client is invalid '" + getClientUri() + "'");
- return;
- }
+ synchronized (this) {
+ if (getConnectionStatus() != ConnectionStatus.DISCONNECTED) {
+ LOG.finer("Must be disconnected and not in error before calling connect: " + getClientUri());
+ return;
+ }
- if (getConnectionStatus() != ConnectionStatus.DISCONNECTED) {
- LOG.info("Client must be disconnected before calling connect '" + getClientUri() + "'");
- return;
+ LOG.fine("Connecting MQTT Client: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.CONNECTING);
}
- Mqtt3ConnectBuilder.Send<CompletableFuture<Mqtt3ConnAck>> completableFutureSend = client.connectWith();
+ this.disconnected = false;
+
+ LOG.info("Establishing connection: " + getClientUri());
+
+ Mqtt3ConnectBuilder.Send<CompletableFuture<Mqtt3ConnAck>> completableFutureSend = client.connectWith().keepAlive(5);
if (usernamePassword != null) {
completableFutureSend = completableFutureSend.simpleAuth()
@@ -276,21 +330,22 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
.applySimpleAuth();
}
- completableFutureSend.send()
- .whenComplete((connAck, throwable) -> {
- if (throwable != null) {
- // Failure
- LOG.log(Level.WARNING, "Failed to connect to MQTT broker '" + getClientUri() + "'", throwable);
- } else {
- // Connected
- LOG.log(Level.INFO, "Connected to MQTT broker '" + getClientUri() + "'");
- connectionStatusConsumers.forEach(consumer -> {
- try {
- consumer.accept(ConnectionStatus.CONNECTED);
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Connection status consumer threw an exception", e);
- }
- });
+ completableFutureSend.send();
+ }
+
+ protected void onConnectionStatusChanged(ConnectionStatus connectionStatus) {
+ this.connectionStatus = connectionStatus;
+
+ executorService.submit(() -> {
+ synchronized (connectionStatusConsumers) {
+ connectionStatusConsumers.forEach(
+ consumer -> {
+ try {
+ consumer.accept(connectionStatus);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Connection status change handler threw an exception: " + getClientUri(), e);
+ }
+ });
}
});
}
@@ -298,25 +353,25 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
@Override
public void disconnect() {
if (client == null) {
- LOG.info("Cannot disconnect as client is invalid '" + getClientUri() + "'");
return;
}
- client.disconnect()
- .whenComplete((status, throwable) -> {
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.DISCONNECTED) {
+ LOG.finest("Already disconnected: " + getClientUri());
+ return;
+ }
+
+ LOG.finest("Disconnecting IO client: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTED);
+ }
+
+ this.disconnected = true;
+
+ client.disconnect().whenComplete((unused, throwable) -> {
+
if (throwable != null) {
- // Failure
- LOG.log(Level.WARNING, "Failed to disconnect from MQTT broker '" + getClientUri() + "'", throwable);
- } else {
- // Disconnected
- LOG.log(Level.INFO, "Disconnected from MQTT broker '" + getClientUri() + "'");
- connectionStatusConsumers.forEach(consumer -> {
- try {
- consumer.accept(ConnectionStatus.DISCONNECTED);
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Connection status consumer threw an exception", e);
- }
- });
+ LOG.info("Failed to disconnect");
}
});
}
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 eddc114fc..2dd993435 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
@@ -28,4 +28,12 @@ public class MQTTMessage<T> {
this.topic = topic;
this.payload = payload;
}
+
+ public String getTopic() {
+ return topic;
+ }
+
+ public T getPayload() {
+ return payload;
+ }
}
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 070b9d859..776c1ede5 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
@@ -26,7 +26,6 @@ import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeState;
-import org.openremote.model.protocol.ProtocolUtil;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.ValueUtil;
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
new file mode 100644
index 000000000..595eedb5e
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java
@@ -0,0 +1,495 @@
+/*
+ * Copyright 2021, 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.Token;
+import io.moquette.broker.subscriptions.Topic;
+import io.moquette.interception.messages.InterceptPublishMessage;
+import io.moquette.interception.messages.InterceptSubscribeMessage;
+import io.moquette.interception.messages.InterceptUnsubscribeMessage;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.handler.codec.mqtt.MqttMessageBuilders;
+import io.netty.handler.codec.mqtt.MqttPublishMessage;
+import io.netty.handler.codec.mqtt.MqttQoS;
+import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.security.AuthContext;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.security.ManagerIdentityService;
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.Container;
+import org.openremote.model.asset.AssetEvent;
+import org.openremote.model.asset.AssetFilter;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.event.shared.CancelEventSubscription;
+import org.openremote.model.event.shared.EventSubscription;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.ValueUtil;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+
+import static org.openremote.manager.mqtt.MqttBrokerService.prepareHeaders;
+import static org.openremote.model.Constants.ASSET_ID_REGEXP;
+import static org.openremote.model.syslog.SyslogCategory.API;
+
+public class DefaultMQTTHandler extends MQTTHandler {
+
+ public static final int PRIORITY = 1000;
+ public static final String ASSET_TOPIC = "asset";
+ public static final String ATTRIBUTE_TOPIC = "attribute";
+ public static final String ATTRIBUTE_WRITE_TOPIC = "writeattribute";
+ public static final String ATTRIBUTE_VALUE_TOPIC = "attributevalue";
+ 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));
+ }
+
+ public static boolean isAttributeWriteTopic(Topic topic) {
+ return ATTRIBUTE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));
+ }
+
+ public static boolean isAttributeValueWriteTopic(Topic topic) {
+ return ATTRIBUTE_VALUE_WRITE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));
+ }
+
+ public static boolean isAssetTopic(Topic topic) {
+ return ASSET_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));
+ }
+
+ @Override
+ public int getPriority() {
+ // This handler is intended to be the final handler but this can obviously be overridden by another handler
+ return PRIORITY;
+ }
+
+ @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 boolean topicMatches(Topic topic) {
+ return isAttributeTopic(topic) || isAssetTopic(topic) || isAttributeWriteTopic(topic) || isAttributeValueWriteTopic(topic);
+ }
+
+ @Override
+ protected Logger getLogger() {
+ return LOG;
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Override
+ public boolean canSubscribe(MqttConnection connection, Topic topic) {
+
+ if (!isKeycloak) {
+ LOG.fine("Identity provider is not keycloak");
+ return false;
+ }
+
+ AuthContext authContext = connection.getAuthContext();
+
+ if (authContext == null) {
+ LOG.fine("Anonymous connection not supported: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+
+ boolean isAttributeTopic = isAttributeTopic(topic);
+ boolean isAssetTopic = isAssetTopic(topic);
+
+ if (!isAssetTopic && !isAttributeTopic) {
+ LOG.fine("Topic must have 3 or more tokens and third token must be 'asset, attribute or attributevalue': topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+
+ if (isAssetTopic) {
+ if (topic.getTokens().size() < 4 || topic.getTokens().size() > 5) {
+ LOG.fine("Asset subscribe token count should be 4 or 5: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ if (topic.getTokens().size() == 4) {
+ if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 3))
+ && !TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))
+ && !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))) {
+ LOG.fine("Asset subscribe forth token must be an asset ID or wildcard: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ } else if (topic.getTokens().size() == 5) {
+ if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 3))) {
+ LOG.fine("Asset subscribe forth token must be an asset ID: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ if (!TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))
+ && !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))) {
+ LOG.fine("Asset subscribe fifth token must be a wildcard: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ }
+ } else {
+ // Attribute topic
+ if (topic.getTokens().size() < 5 || topic.getTokens().size() > 6) {
+ LOG.fine("Attribute subscribe token count should be 5 or 6: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ if (topic.getTokens().size() == 5) {
+ if (TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))) {
+ LOG.fine("Attribute subscribe multi level wildcard must be last token: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))
+ && !TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))
+ && !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 4))) {
+ LOG.fine("Attribute subscribe fifth token must be an asset ID or a wildcard: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ } else if (topic.getTokens().size() == 6) {
+ if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {
+ LOG.fine("Attribute subscribe fifth token must be an asset ID: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ if (!TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 5))
+ && !TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 5))) {
+ LOG.fine("Attribute subscribe sixth token must be a wildcard: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ }
+ }
+
+ // Build filter for the topic and verify that the filter is OK for given auth context
+ AssetFilter<?> filter = buildAssetFilter(connection, topic);
+
+ if (filter == null) {
+ LOG.info("Failed to process subscription topic: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+
+ EventSubscription<?> subscription = new EventSubscription(
+ isAssetTopic ? AssetEvent.class : AttributeEvent.class,
+ filter
+ );
+
+ if (!clientEventService.authorizeEventSubscription(authContext, subscription)) {
+ LOG.info("Subscription was not authorised for this user and topic: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+
+ return true;
+
+// Asset<?> asset;
+// if(identityProvider.isRestrictedUser(authContext.getUserId())) {
+// Optional<UserAsset> userAsset = assetStorageService.findUserAssets(connection.realm, authContext.getUserId(), assetId).stream().findFirst();
+// asset = userAsset.map(value -> assetStorageService.find(value.getId().getAssetId())).orElse(null);
+// } else {
+// asset = assetStorageService.find(assetId, false);
+// }
+//
+// if (asset == null) {
+// LOG.fine("Asset not found for topic '" + topic + "': " + connection);
+// return false;
+// }
+//
+// if (isAttributeTopic && topicTokenCountGreaterThan(topic, 4)
+// && !(Token.topic.getTokens().get(4)SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)))) {
+// String attributeName = topicTokens.get(4);
+//
+// if (!asset.hasAttribute(attributeName)) {
+// LOG.fine("Asset attribute not found for topic '" + topic + "': " + connection);
+// return false;
+// }
+// }
+ }
+
+ @Override
+ public boolean canPublish(MqttConnection connection, Topic topic) {
+
+ AuthContext authContext = connection.getAuthContext();
+
+ if (!isKeycloak) {
+ LOG.fine("Identity provider is not keycloak");
+ return false;
+ }
+
+ if (authContext == null) {
+ LOG.fine("Anonymous publish not supported: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+
+ if (isAttributeWriteTopic(topic)) {
+ if (topic.getTokens().size() != 3) {
+ LOG.fine("Publish attribute events topic should be {realm}/{clientId}/writeattribute: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ } else if (isAttributeValueWriteTopic(topic)) {
+ if (topic.getTokens().size() != 5 || !Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {
+ LOG.fine("Publish attribute value topic should be {realm}/{clientId}/writeattributevalue/{attributeName}/{assetId}: topic=" + topic + ", connection=" + connection);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Override
+ public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {
+ boolean isAssetTopic = isAssetTopic(topic);
+ String subscriptionId = msg.getTopicFilter(); // Use topic as subscription ID
+
+ AssetFilter filter = buildAssetFilter(connection, topic);
+ Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;
+
+ if (filter == null) {
+ LOG.fine("Invalid event filter generated for topic '" + topic + "': " + connection);
+ return;
+ }
+
+ EventSubscription subscription = new EventSubscription(
+ subscriptionClass,
+ filter,
+ subscriptionId
+ );
+
+ Consumer<SharedEvent> eventConsumer = getSubscriptionEventConsumer(connection, topic, msg.getRequestedQos());
+ connection.subscriptionHandlerMap.put(subscriptionId, eventConsumer);
+ Map<String, Object> headers = prepareHeaders(connection);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ @Override
+ public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {
+ String subscriptionId = topic.toString();
+ Consumer<? extends SharedEvent> eventConsumer = connection.subscriptionHandlerMap.remove(subscriptionId);
+
+ if (eventConsumer != null) {
+ boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);
+
+ Map<String, Object> headers = prepareHeaders(connection);
+ Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);
+ CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
+ }
+ }
+
+ @Override
+ public void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {
+ List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
+ boolean isValueWrite = topicTokens.get(2).equals(ATTRIBUTE_VALUE_WRITE_TOPIC);
+ String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);
+ AttributeEvent attributeEvent;
+
+ if (isValueWrite) {
+ String attributeName = topicTokens.get(3);
+ String assetId = topicTokens.get(4);
+ Object value = ValueUtil.parse(payloadContent).orElse(null);
+ attributeEvent = new AttributeEvent(assetId, attributeName, value);
+ } else {
+ attributeEvent = ValueUtil.parse(payloadContent, AttributeEvent.class).orElse(null);
+ }
+
+ if (attributeEvent == null) {
+ LOG.fine("Failed to parse payload for publish topic '" + topic + "': " + connection);
+ return;
+ }
+
+ Map<String, Object> headers = prepareHeaders(connection);
+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
+ }
+
+ public static AssetFilter<?> buildAssetFilter(MqttConnection connection, Topic topic) {
+ boolean isAttributeTopic = isAttributeTopic(topic);
+ boolean isAssetTopic = isAssetTopic(topic);
+
+ String realm = connection.getRealm();
+ List<String> assetIds = new ArrayList<>();
+ List<String> parentIds = new ArrayList<>();
+ List<String> paths = new ArrayList<>();
+ List<String> attributeNames = new ArrayList<>();
+ String firstTokenStr = topicTokenIndexToString(topic, 3);
+
+ if (isAssetTopic) {
+ if (topic.getTokens().size() == 4) {
+ if (TOKEN_MULTI_LEVEL_WILDCARD.equals(firstTokenStr)) {
+ //realm/clientId/asset/#
+ // No filtering required
+ } else if (TOKEN_SINGLE_LEVEL_WILDCARD.equals(firstTokenStr)) {
+ //realm/clientId/asset/+
+ parentIds.add(null);
+ } else {
+ //realm/clientId/asset/{assetId}
+ assetIds.add(firstTokenStr);
+ }
+ } else if (topic.getTokens().size() == 5) {
+ String secondTokenStr = topicTokenIndexToString(topic, 4);
+
+ if (TOKEN_MULTI_LEVEL_WILDCARD.equals(secondTokenStr)) {
+ //realm/clientId/asset/assetId/#
+ paths.add(firstTokenStr);
+ } else if (TOKEN_SINGLE_LEVEL_WILDCARD.equals(secondTokenStr)) {
+ //realm/clientId/asset/assetId/+
+ parentIds.add(firstTokenStr);
+ }
+ } else {
+ return null;
+ }
+ } else {
+ if (!TOKEN_SINGLE_LEVEL_WILDCARD.equals(firstTokenStr)) {
+ attributeNames.add(firstTokenStr);
+ }
+ if (topic.getTokens().size() == 5) {
+ String secondTokenStr = topicTokenIndexToString(topic, 4);
+ //realm/clientId/attribute/{attributeName|+}/{assetId|+|*}
+ if (TOKEN_MULTI_LEVEL_WILDCARD.equals(secondTokenStr)) {
+ //realm/clientId/attribute/+/#
+ // No filtering required
+ } else if (TOKEN_SINGLE_LEVEL_WILDCARD.equals(secondTokenStr)) {
+ //realm/clientId/attribute/+/+
+ parentIds.add(null);
+ } else {
+ //realm/clientId/attribute/+/{assetId}
+ assetIds.add(secondTokenStr);
+ }
+ } else if (topic.getTokens().size() == 6) {
+ //realm/clientId/attribute/{attributeName|+}/{assetId}/{+|*}
+ String thirdTokenStr = topicTokenIndexToString(topic, 5);
+
+ if (TOKEN_MULTI_LEVEL_WILDCARD.equals(thirdTokenStr)) {
+ paths.add(topicTokenIndexToString(topic, 4));
+ } else if (TOKEN_SINGLE_LEVEL_WILDCARD.equals(thirdTokenStr)) {
+ parentIds.add(topicTokenIndexToString(topic, 4));
+ }
+ } else {
+ return null;
+ }
+ }
+
+ AssetFilter<?> assetFilter = new AssetFilter<>().setRealm(realm);
+ if (!assetIds.isEmpty()) {
+ assetFilter.setAssetIds(assetIds.toArray(new String[0]));
+ }
+ if (!parentIds.isEmpty()) {
+ assetFilter.setParentIds(parentIds.toArray(new String[0]));
+ }
+ if (!paths.isEmpty()) {
+ assetFilter.setPath(paths.toArray(new String[0]));
+ }
+ if (!attributeNames.isEmpty()) {
+ assetFilter.setAttributeNames(attributeNames.toArray(new String[0]));
+ }
+ return assetFilter;
+ }
+
+ public void sendToSession(String sessionId, String topic, Object data, MqttQoS qoS) {
+ try {
+ ByteBuf payload = Unpooled.copiedBuffer(ValueUtil.asJSON(data).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)), Charset.defaultCharset());
+
+ MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
+ .qos(qoS)
+ .topicName(topic)
+ .payload(payload)
+ .build();
+
+ mqttBrokerService.mqttBroker.internalPublish(publishMessage, sessionId);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);
+ }
+ }
+
+ protected Consumer<SharedEvent> getSubscriptionEventConsumer(MqttConnection connection, Topic topic, MqttQoS mqttQoS) {
+ boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equalsIgnoreCase(topicTokenIndexToString(topic, 2));
+ boolean isAssetTopic = isAssetTopic(topic);
+
+ // Build topic expander (replace wildcards) so it isn't computed for each event
+ Function<SharedEvent, String> topicExpander;
+
+ if (isAssetTopic) {
+ String topicStr = topic.toString();
+ String replaceToken = topicStr.endsWith(TOKEN_MULTI_LEVEL_WILDCARD) ? TOKEN_MULTI_LEVEL_WILDCARD : topicStr.endsWith(TOKEN_SINGLE_LEVEL_WILDCARD) ? TOKEN_SINGLE_LEVEL_WILDCARD : null;
+ topicExpander = ev -> replaceToken != null ? topicStr.replace(replaceToken, ((AssetEvent)ev).getAssetId()) : topicStr;
+ } else {
+ String topicStr = topic.toString();
+ boolean injectAttributeName = TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3));
+
+ if (injectAttributeName) {
+ topicStr = topicStr.replaceFirst("\\"+ TOKEN_SINGLE_LEVEL_WILDCARD, "\\$");
+ }
+
+ String replaceToken = topicStr.endsWith(TOKEN_MULTI_LEVEL_WILDCARD) ? TOKEN_MULTI_LEVEL_WILDCARD : topicStr.endsWith(TOKEN_SINGLE_LEVEL_WILDCARD) ? TOKEN_SINGLE_LEVEL_WILDCARD : null;
+ String finalTopicStr = topicStr;
+ topicExpander = ev -> {
+ String expanded = replaceToken != null ? finalTopicStr.replace(replaceToken, ((AttributeEvent)ev).getAssetId()) : finalTopicStr;
+ if (injectAttributeName) {
+ expanded = expanded.replace("$", ((AttributeEvent)ev).getAttributeName());
+ }
+ return expanded;
+ };
+ }
+
+
+ return ev -> {
+
+ if (isAssetTopic) {
+ if (ev instanceof AssetEvent) {
+ sendToSession(connection.getClientId(), topicExpander.apply(ev), ev, mqttQoS);
+ }
+ } else {
+ if (ev instanceof AttributeEvent) {
+ AttributeEvent attributeEvent = (AttributeEvent) ev;
+
+ if (isValueSubscription) {
+ sendToSession(connection.getClientId(), topicExpander.apply(ev), attributeEvent.getValue().orElse(null), mqttQoS);
+ } else {
+ sendToSession(connection.getClientId(), topicExpander.apply(ev), ev, mqttQoS);
+ }
+ }
+ }
+ };
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java
deleted file mode 100644
index d1e0575ab..000000000
--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTCustomHandler.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright 2021, 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.*;
-import org.openremote.container.security.AuthContext;
-import org.openremote.model.Container;
-
-/**
- * This allows custom handlers to be discovered by the {@link MqttBrokerService} during system startup using the
- * {@link java.util.ServiceLoader} mechanism. This allows topic(s) can be handled in a custom way. Any instances must
- * have a no-arg constructor.
- */
-public interface MQTTCustomHandler {
-
- /**
- * Gets the priority of this handler which is used to determine the call order; handlers with a lower priority are
- * initialised and started first.
- */
- default int getPriority() {
- return 0;
- }
-
- /**
- * Provides a name to identify this custom handler for logging purposes etc.
- */
- String getName();
-
- /**
- * Called when the system starts to allow for initialisation.
- */
- void start(Container container) throws Exception;
-
- /**
- * Called when the system stops to allow for any cleanup.
- */
- void stop() throws Exception;
-
- /**
- * Should this handler be called to handle this topic; general security checks are performed before this handler
- * is called so this handler can focus on the required custom business logic. This handler should return null
- * if it doesn't want to allow/prevent the pub/sub.
- */
- boolean canSubscribe(AuthContext authContext, MqttConnection connection, Topic topic);
-
- boolean canPublish(AuthContext authContext, MqttConnection connection, Topic topic);
-
- /**
- * Will be called when any client connects
- */
- void onConnect(MqttConnection connection, InterceptConnectMessage msg);
-
- /**
- * Will be called when any client disconnects
- */
- void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg);
-
- /**
- * Will be called when any client loses connection
- */
- void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg);
-
- /**
- * Should return true if this handler handles the subscribe.
- */
- boolean onSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg);
-
- void onUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg);
-
- /**
- * Should return true if this handler handles the publish.
- */
- boolean onPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg);
-}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
new file mode 100644
index 000000000..3cfa7da88
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
@@ -0,0 +1,193 @@
+/*
+ * Copyright 2021, 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.*;
+import org.openremote.model.Container;
+
+import java.util.logging.Logger;
+
+/**
+ * This allows custom handlers to be discovered by the {@link MqttBrokerService} during system startup using the
+ * {@link java.util.ServiceLoader} mechanism. This allows topic(s) can be handled in a custom way. Any instances must
+ * have a no-arg constructor.
+ */
+public abstract class MQTTHandler {
+
+ public static final String TOKEN_MULTI_LEVEL_WILDCARD = "#";
+ public static final String TOKEN_SINGLE_LEVEL_WILDCARD = "+";
+
+ /**
+ * Gets the priority of this handler which is used to determine the call order; handlers with a lower priority are
+ * initialised and started first.
+ */
+ public int getPriority() {
+ return 0;
+ }
+
+ /**
+ * Provides a name to identify this custom handler for logging purposes etc.
+ */
+ public String getName() {
+ return this.getClass().getSimpleName();
+ }
+
+ /**
+ * Called when the system starts to allow for initialisation.
+ */
+ public void start(Container container) throws Exception {
+
+ }
+
+ /**
+ * Called when the system stops to allow for any cleanup.
+ */
+ public void stop() throws Exception {
+
+ }
+
+ /**
+ * Will be called when any client connects
+ */
+ public void onConnect(MqttConnection connection, InterceptConnectMessage msg) {
+
+ }
+
+ /**
+ * Will be called when any client disconnects
+ */
+ public void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg) {
+
+ }
+
+ /**
+ * Will be called when any client loses connection
+ */
+ public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {
+
+ }
+
+ /**
+ * Checks that topic is valid and has at least 3 tokens (generally first two tokens should be {realm}/{clientId} but
+ * it is up to the {@link #canSubscribe} and/or {@link #canPublish} methods to validate this).
+ */
+ public boolean handlesTopic(Topic topic) {
+ if (!topicTokenCountGreaterThan(topic, 2)) {
+ getLogger().finer("Topic must contain more than 2 tokens");
+ return false;
+ }
+ if (!topicMatches(topic)) {
+ getLogger().finer("Topic failed to match this handler");
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Checks that authenticated session and topic realm matches the authenticated user and also that topic client ID
+ * matches the connection client ID.
+ */
+ 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 (!topicRealmAndClientIdMatchesConnection(connection, topic)) {
+ getLogger().fine("Topic realm and client ID tokens 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 true;
+ }
+
+ /**
+ * Checks that authenticated sessions and topic realm matches the authenticated user and also that topic client ID
+ * matches the connection client ID.
+ */
+ public boolean checkCanPublish(MqttConnection connection, Topic topic) {
+ if (connection.getAuthContext() == null) {
+ getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ if (!topicRealmAndClientIdMatchesConnection(connection, topic)) {
+ getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ if (!canPublish(connection, topic)) {
+ getLogger().fine("Cannot publish to this topic, topic=" + topic + ", connection" + connection);
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Indicates if this handler will handle the specified topic; independent of whether it is a publish or subscribe.
+ * Should generally check the third token onwards unless {@link #handlesTopic} has been overridden.
+ */
+ protected abstract boolean topicMatches(Topic topic);
+
+ protected abstract Logger getLogger();
+
+ /**
+ * Called to authorise a subscription if {@link #handlesTopic} returned true; should return true if the subscription
+ * is allowed otherwise return false.
+ */
+ public abstract boolean canSubscribe(MqttConnection connection, Topic topic);
+
+ /**
+ * Called to authorise a publish if {@link #handlesTopic} returned true; should return true if the publish is
+ * allowed otherwise return false.
+ */
+ public abstract boolean canPublish(MqttConnection connection, Topic topic);
+
+ /**
+ * Called to handle subscribe if {@link #canSubscribe} returned true.
+ */
+ public abstract void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg);
+
+ /**
+ * Called to handle unsubscribe if {@link #handlesTopic} returned true.
+ */
+ public abstract void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg);
+
+ /**
+ * Called to handle publish if {@link #canPublish} returned true.
+ */
+ 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;
+ }
+
+ return connection.clientId.equals(topicTokenIndexToString(topic, 1));
+ }
+
+ public static boolean topicTokenCountGreaterThan(Topic topic, int size) {
+ return topic.getTokens() != null && topic.getTokens().size() > size;
+ }
+
+ public static String topicTokenIndexToString(Topic topic, int tokenNumber) {
+ return topicTokenCountGreaterThan(topic, tokenNumber) ? topic.getTokens().get(tokenNumber).toString() : null;
+ }
+}
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 c96905ac1..b92d8ceab 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -24,13 +24,9 @@ import io.moquette.broker.Server;
import io.moquette.broker.config.MemoryConfig;
import io.moquette.broker.security.IAuthenticator;
import io.moquette.interception.InterceptHandler;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import io.netty.handler.codec.mqtt.MqttMessageBuilders;
-import io.netty.handler.codec.mqtt.MqttPublishMessage;
-import io.netty.handler.codec.mqtt.MqttQoS;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.web.ConnectionConstants;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerIdentityService;
@@ -38,24 +34,20 @@ import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.Constants;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetFilter;
-import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.TriggeredEventSubscription;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.security.Tenant;
import org.openremote.model.security.User;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
-import org.openremote.model.util.ValueUtil;
-import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
-import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.util.stream.StreamSupport.stream;
@@ -73,17 +65,12 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
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 ASSET_TOPIC = "asset";
- public static final String ATTRIBUTE_TOPIC = "attribute";
- public static final String ATTRIBUTE_VALUE_TOPIC = "attributevalue";
- public static final String SINGLE_LEVEL_WILDCARD = "+";
- public static final String MULTI_LEVEL_WILDCARD = "#";
-
protected ManagerKeycloakIdentityProvider identityProvider;
protected ClientEventService clientEventService;
protected MessageBrokerService messageBrokerService;
- protected Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();
- protected List<MQTTCustomHandler> customHandlers = new ArrayList<>();
+ protected ScheduledExecutorService executorService;
+ protected final Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();
+ protected List<MQTTHandler> customHandlers = new ArrayList<>();
protected boolean active;
protected String host;
@@ -103,6 +90,7 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
clientEventService = container.getService(ClientEventService.class);
ManagerIdentityService identityService = container.getService(ManagerIdentityService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
+ executorService = container.getExecutorService();
if (!identityService.isKeycloakEnabled()) {
LOG.warning("MQTT connections are not supported when not using Keycloak identity provider");
@@ -123,10 +111,10 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
.choice()
.when(body().isInstanceOf(TriggeredEventSubscription.class))
.process(exchange -> {
- String sessionKey = getSessionKey(exchange);
+ String clientId = getSessionKey(exchange);
@SuppressWarnings("unchecked")
TriggeredEventSubscription<SharedEvent> triggeredEventSubscription = exchange.getIn().getBody(TriggeredEventSubscription.class);
- MqttConnection connection = clientIdConnectionMap.get(sessionKey);
+ MqttConnection connection = clientIdConnectionMap.get(clientId);
if (connection == null) {
return;
@@ -145,16 +133,16 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
Properties properties = new Properties();
properties.setProperty(BrokerConstants.HOST_PROPERTY_NAME, host);
properties.setProperty(BrokerConstants.PORT_PROPERTY_NAME, String.valueOf(port));
- properties.setProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, String.valueOf(false));
- List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService, clientIdConnectionMap));
+ properties.setProperty(BrokerConstants.ALLOW_ANONYMOUS_PROPERTY_NAME, String.valueOf(true));
+ List<? extends InterceptHandler> interceptHandlers = Collections.singletonList(new ORInterceptHandler(this, identityProvider, messageBrokerService));
// Load custom handlers
- this.customHandlers = stream(ServiceLoader.load(MQTTCustomHandler.class).spliterator(), false)
- .sorted(Comparator.comparingInt(MQTTCustomHandler::getPriority))
+ this.customHandlers = stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)
+ .sorted(Comparator.comparingInt(MQTTHandler::getPriority))
.collect(Collectors.toList());
// Start each custom handler
- for (MQTTCustomHandler handler : customHandlers) {
+ for (MQTTHandler handler : customHandlers) {
try {
handler.start(container);
} catch (Exception e) {
@@ -172,8 +160,8 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
mqttBroker.stopServer();
LOG.fine("Stopped MQTT broker");
- stream(ServiceLoader.load(MQTTCustomHandler.class).spliterator(), false)
- .sorted(Comparator.comparingInt(MQTTCustomHandler::getPriority).reversed())
+ stream(ServiceLoader.load(MQTTHandler.class).spliterator(), false)
+ .sorted(Comparator.comparingInt(MQTTHandler::getPriority).reversed())
.forEach(handler -> {
try {
handler.stop();
@@ -183,28 +171,9 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
});
}
- public Iterable<MQTTCustomHandler> getCustomHandlers() {
- return customHandlers;
- }
-
- public void sendToSession(String sessionId, String topic, Object data, MqttQoS qoS) {
- try {
- ByteBuf payload = Unpooled.copiedBuffer(ValueUtil.asJSON(data).orElseThrow(() -> new IllegalStateException("Failed to convert payload to JSON string: " + data)), Charset.defaultCharset());
-
- MqttPublishMessage publishMessage = MqttMessageBuilders.publish()
- .qos(qoS)
- .topicName(topic)
- .payload(payload)
- .build();
-
- mqttBroker.internalPublish(publishMessage, sessionId);
- } catch (Exception e) {
- LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);
- }
- }
-
/**
- * Validates an incoming connection and if rejected it will close the connection without calling intercept handler
+ * Validates an incoming authenticated connection and if rejected it will close the connection without calling
+ * the intercept handler.
*/
@Override
public boolean checkValid(String clientId, String username, byte[] password) {
@@ -216,15 +185,13 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
}
String suppliedClientSecret = password != null ? new String(password, StandardCharsets.UTF_8) : null;
- if (clientIdConnectionMap.containsKey(clientId)) {
- LOG.info("Client with this ID already connected");
- return false;
- }
+ // Removed client ID check as it won't execute for anonymous connections; single connection per user is more
+ // important
if (TextUtil.isNullOrEmpty(realm)
|| TextUtil.isNullOrEmpty(username)
|| TextUtil.isNullOrEmpty(suppliedClientSecret)) {
- LOG.fine("Realm, client ID and/or client secret missing so this is an anonymous session with limited capabilities: " + clientId);
+ LOG.fine("Realm, client ID and/or client secret missing so this is an anonymous session: " + clientId);
return true;
}
@@ -237,150 +204,41 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
User user = identityProvider.getUserByUsername(realm, User.SERVICE_ACCOUNT_PREFIX + username);
if (user == null || user.getEnabled() == null || !user.getEnabled() || TextUtil.isNullOrEmpty(user.getSecret())) {
- LOG.warning("User not found, disabled or doesn't support client credentials grant type");
+ LOG.warning("User not found, disabled or doesn't support client credentials grant type: username=" + username);
return false;
}
return suppliedClientSecret.equals(user.getSecret());
}
- public static boolean isAttributeTopic(List<String> tokens) {
- return tokens.get(2).equals(ATTRIBUTE_TOPIC) || tokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC);
+ public Iterable<MQTTHandler> getCustomHandlers() {
+ return customHandlers;
}
- public static boolean isAssetTopic(List<String> tokens) {
- return tokens.get(2).equals(ASSET_TOPIC);
+ public MqttConnection getConnection(String clientId) {
+ synchronized (clientIdConnectionMap) {
+ return clientIdConnectionMap.get(clientId);
+ }
}
- public static AssetFilter<?> buildAssetFilter(MqttConnection connection, List<String> topicTokens) {
- if (topicTokens == null || topicTokens.isEmpty()) {
- return null;
+ public MqttConnection removeConnection(String clientId) {
+ synchronized (clientIdConnectionMap) {
+ return clientIdConnectionMap.remove(clientId);
}
+ }
- boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
- boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);
-
- String realm = connection.getRealm();
- List<String> assetIds = new ArrayList<>();
- List<String> parentIds = new ArrayList<>();
- List<String> paths = new ArrayList<>();
- List<String> attributeNames = new ArrayList<>();
-
- String assetId = Pattern.matches(Constants.ASSET_ID_REGEXP, topicTokens.get(3)) ? topicTokens.get(3) : null;
- int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);
- int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);
-
- if (!isAssetTopic && !isAttributeTopic) {
- return null;
- }
-
- if (topicTokens.size() == 4) {
- if (isAssetTopic) {
- if (multiLevelIndex == 3) {
- //realm/clientId/.../#
- // No asset filtering required
- } else if (singleLevelIndex == 3) {
- //realm/clientId/.../+
- parentIds.add(null);
- } else {
- //realm/clientId/.../assetId
- assetIds.add(assetId);
- }
- } else {
- if (assetId != null) {
- //realm/clientId/attribute/assetId
- assetIds.add(assetId);
- } else if (singleLevelIndex == 3) {
- //realm/clientId/attribute/+
- parentIds.add(null);
- } else if (multiLevelIndex != 3) {
- //realm/clientId/attribute/attributeName
- attributeNames.add(topicTokens.get(3));
- }
- }
- } else if (topicTokens.size() == 5) {
- if (isAssetTopic) {
- if (multiLevelIndex == 4) {
- //realm/clientId/asset/assetId/#
- paths.add(assetId);
- } else if (singleLevelIndex == 4) {
- //realm/clientId/asset/assetId/+
- parentIds.add(assetId);
- } else {
- return null;
- }
- } else {
- if (assetId != null) {
- if (multiLevelIndex == 4) {
- //realm/clientId/attribute/assetId/#
- paths.add(assetId);
- } else if (singleLevelIndex == 4) {
- //realm/clientId/attribute/assetId/+
- parentIds.add(assetId);
- } else {
- assetIds.add(assetId);
-
- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)) ? null : topicTokens.get(4);
- if (attributeName != null) {
- //realm/clientId/attribute/assetId/attributeName
- attributeNames.add(topicTokens.get(4));
- } else {
- return null;
- }
- }
- } else {
- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)) ? null : topicTokens.get(4);
- if (attributeName != null) {
- attributeNames.add(attributeName);
- if (multiLevelIndex == 4) {
- return null; //no topic allowed after multilevel wildcard
- } else if (singleLevelIndex == 4) {
- //realm/clientId/attribute/+/attributeName
- parentIds.add(null);
- } else {
- return null;
- }
- } else {
- return null;
- }
- }
- }
- } else if (topicTokens.size() == 6) {
- if (isAssetTopic || assetId == null) {
- return null;
- }
- String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(5)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(5)) ? null : topicTokens.get(5);
- if (attributeName != null) {
- attributeNames.add(attributeName);
- if (multiLevelIndex == 4) {
- return null; //no topic allowed after multilevel wildcard
- } else if (singleLevelIndex == 4) {
- //realm/clientId/attribute/assetId/+/attributeName
- parentIds.add(assetId);
- } else {
- return null;
- }
- } else {
- return null;
- }
- } else {
- return null;
+ public void addConnection(String clientId, MqttConnection connection) {
+ synchronized (clientIdConnectionMap) {
+ clientIdConnectionMap.put(clientId, connection);
}
+ }
- AssetFilter<?> assetFilter = new AssetFilter<>().setRealm(realm);
- if (!assetIds.isEmpty()) {
- assetFilter.setAssetIds(assetIds.toArray(new String[0]));
- }
- if (!parentIds.isEmpty()) {
- assetFilter.setParentIds(parentIds.toArray(new String[0]));
- }
- if(!paths.isEmpty()) {
- assetFilter.setPath(paths.toArray(new String[0]));
- }
- if (!attributeNames.isEmpty()) {
- assetFilter.setAttributeNames(attributeNames.toArray(new String[0]));
- }
- return assetFilter;
+ 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());
+ return headers;
}
protected void onSubscriptionTriggered(MqttConnection connection, TriggeredEventSubscription<SharedEvent> triggeredEventSubscription) {
@@ -392,41 +250,4 @@ public class MqttBrokerService implements ContainerService, IAuthenticator {
}
});
}
-
- protected Consumer<SharedEvent> getEventConsumer(MqttConnection connection, String topic, boolean isValueSubscription, MqttQoS mqttQoS) {
- return ev -> {
- List<String> topicTokens = Arrays.asList(topic.split("/"));
- int wildCardIndex = Math.max(topicTokens.indexOf(MULTI_LEVEL_WILDCARD), topicTokens.indexOf(SINGLE_LEVEL_WILDCARD));
-
- if (ev instanceof AssetEvent) {
- AssetEvent assetEvent = (AssetEvent) ev;
- if (wildCardIndex > 0) {
- topicTokens.set(wildCardIndex, assetEvent.getAssetId());
- }
- sendToSession(connection.getClientId(), String.join("/", topicTokens), ev, mqttQoS);
- }
-
- if (ev instanceof AttributeEvent) {
- AttributeEvent attributeEvent = (AttributeEvent) ev;
- if (wildCardIndex > 0) {
- if (wildCardIndex == 1) { // attribute/<wildcard>
- topicTokens.set(wildCardIndex, attributeEvent.getAssetId());
- } else if (wildCardIndex == 2) {
- if (topicTokens.size() == 3) { // attribute/assetId/<wildcard>
- topicTokens.set(wildCardIndex, attributeEvent.getAttributeName());
- } else { // attribute/parentId/<wildcard>/attributeName
- topicTokens.set(wildCardIndex, attributeEvent.getAssetId());
- }
- } else if (wildCardIndex == 3) { //attribute/parentId/assetId/<wildcard>
- topicTokens.set(wildCardIndex, attributeEvent.getAttributeName());
- }
- }
- if (isValueSubscription) {
- sendToSession(connection.getClientId(), String.join("/", topicTokens), attributeEvent.getValue().orElse(null), mqttQoS);
- } else {
- sendToSession(connection.getClientId(), String.join("/", topicTokens), ev, mqttQoS);
- }
- }
- };
- }
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
index bdb685478..a16b55d88 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
@@ -1,5 +1,10 @@
package org.openremote.manager.mqtt;
+import org.keycloak.adapters.rotation.AdapterTokenVerifier;
+import org.keycloak.common.VerificationException;
+import org.keycloak.representations.AccessToken;
+import org.openremote.container.security.AuthContext;
+import org.openremote.container.security.keycloak.AccessTokenAuthContext;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.auth.OAuthClientCredentialsGrant;
import org.openremote.model.auth.OAuthGrant;
@@ -10,8 +15,11 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;
+import java.util.logging.Level;
import java.util.logging.Logger;
+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
+
/**
* Handles access token generation and tracks all subscriptions for the connection
*/
@@ -25,19 +33,21 @@ public class MqttConnection {
protected final Map<String, Consumer<SharedEvent>> subscriptionHandlerMap = new HashMap<>();
protected final String clientId;
protected Supplier<String> tokenSupplier;
+ protected ManagerKeycloakIdentityProvider identityProvider;
public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password) {
this.realm = realm;
this.username = username;
this.password = password;
this.clientId = clientId;
- String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();
+ this.identityProvider = identityProvider;
credentials = !TextUtil.isNullOrEmpty(realm)
&& !TextUtil.isNullOrEmpty(username)
&& !TextUtil.isNullOrEmpty(password);
if (credentials) {
+ String tokenEndpointUri = identityProvider.getTokenUri(realm).toString();
OAuthGrant grant = new OAuthClientCredentialsGrant(tokenEndpointUri, username, password, null);
tokenSupplier = identityProvider.getAccessTokenSupplier(grant);
} else {
@@ -69,6 +79,24 @@ public class MqttConnection {
return tokenSupplier.get();
}
+ public AuthContext getAuthContext() {
+ AuthContext authContext;
+
+ if (!credentials) {
+ return null;
+ }
+
+ try {
+ AccessToken accessToken = AdapterTokenVerifier.verifyToken(getAccessToken(), identityProvider.getKeycloakDeployment(realm, KEYCLOAK_CLIENT_ID));
+ authContext = accessToken != null ? new AccessTokenAuthContext(realm, accessToken) : null;
+ } catch (VerificationException e) {
+ LOG.log(Level.INFO, "Couldn't verify token: " + this, e);
+ return null;
+ }
+
+ return authContext;
+ }
+
/**
* This is MQTT client ID not to be confused with OAuth client ID
*/
@@ -85,7 +113,7 @@ public class MqttConnection {
return this.getClass().getSimpleName() + "{" +
"realm='" + realm + '\'' +
", username='" + username + '\'' +
- ", sessionId='" + clientId + '\'' +
+ ", clientId='" + clientId + '\'' +
'}';
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
index 3c21e6739..4105f72be 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
@@ -1,35 +1,14 @@
package org.openremote.manager.mqtt;
import io.moquette.broker.security.IAuthorizatorPolicy;
-import io.moquette.broker.subscriptions.Token;
import io.moquette.broker.subscriptions.Topic;
-import org.keycloak.adapters.rotation.AdapterTokenVerifier;
-import org.keycloak.common.VerificationException;
-import org.keycloak.representations.AccessToken;
-import org.openremote.container.security.AuthContext;
-import org.openremote.container.security.keycloak.AccessTokenAuthContext;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
-import org.openremote.model.Constants;
-import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetFilter;
-import org.openremote.model.asset.UserAsset;
-import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.event.shared.EventSubscription;
-import org.openremote.model.security.ClientRole;
import org.openremote.model.syslog.SyslogCategory;
-import java.util.List;
-import java.util.Optional;
-import java.util.logging.Level;
import java.util.logging.Logger;
-import java.util.regex.Pattern;
-import java.util.stream.Collectors;
-import static org.openremote.manager.mqtt.MqttBrokerService.*;
-import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
import static org.openremote.model.syslog.SyslogCategory.API;
public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
@@ -53,167 +32,54 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
@Override
public boolean canWrite(Topic topic, String username, String clientId) {
- String[] realmAndUsername = username.split(":");
- String realm = realmAndUsername[0];
- username = realmAndUsername[1];
+ String realm = null;
+
+ if (username != null) {
+ String[] realmAndUsername = username.split(":");
+ realm = realmAndUsername[0];
+ username = realmAndUsername[1];
+ }
return verifyRights(topic, clientId, realm, username, true);
}
@Override
public boolean canRead(Topic topic, String username, String clientId) {
- String[] realmAndUsername = username.split(":");
- String realm = realmAndUsername[0];
- username = realmAndUsername[1];
+ String realm = null;
+
+ if (username != null) {
+ String[] realmAndUsername = username.split(":");
+ realm = realmAndUsername[0];
+ username = realmAndUsername[1];
+ }
return verifyRights(topic, clientId, realm, username, false);
}
@SuppressWarnings({"rawtypes", "unchecked"})
protected boolean verifyRights(Topic topic, String sessionId, String realm, String username, boolean isWrite) {
MqttConnection connection = brokerService.clientIdConnectionMap.get(sessionId);
- AuthContext authContext;
if (connection == null) {
LOG.warning("No connection found: sessionId=" + sessionId);
return false;
}
- try {
- AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.getAccessToken(), identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));
- authContext = accessToken != null ? new AccessTokenAuthContext(connection.realm, accessToken) : null;
- } catch (VerificationException e) {
- LOG.log(Level.FINE, "Couldn't verify token: " + connection, e);
- return false;
- }
-
- if (authContext == null) {
- LOG.warning("Failed to get auth context for connection: " + connection);
- return false;
- }
-
- boolean handled = false;
-
// See if a custom handler wants to handle this topic pub/sub
- for (MQTTCustomHandler handler : brokerService.getCustomHandlers()) {
- if (isWrite) {
- handled = handler.canPublish(authContext, connection, topic);
- } else {
- handled = handler.canSubscribe(authContext, connection, topic);
- }
+ for (MQTTHandler handler : brokerService.getCustomHandlers()) {
+ if (handler.handlesTopic(topic)) {
+ LOG.fine("Passing topic to handler for " + (isWrite ? "pub" : "sub") + ": handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ boolean result;
- if (handled) {
- LOG.info("Custom handler has allowed pub/sub: handler=" + handler.getName() + ", topic=" + topic + ", publish=" + isWrite + ", connection=" + connection);
- break;
- }
- }
-
- if (handled) {
- return true;
- }
-
- List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
-
- if (!connection.realm.equals(topicTokens.get(0))) {
- LOG.warning("Topic should start with the realm of the connection: " + connection);
- return false;
- }
-
- if (!connection.clientId.equals(topicTokens.get(1))) {
- LOG.warning("Second topic level should match clientId: " + connection);
- return false;
- }
-
- boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
- boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);
-
- if (!isAssetTopic && !isAttributeTopic) {
- LOG.fine("Topic starts with invalid value '" + topic + "': " + connection);
- return false;
- }
-
- if (topicTokens.size() > 3) {
- String assetId = topicTokens.get(3);
- if (Pattern.matches(Constants.ASSET_ID_REGEXP, assetId)) {
-
- Asset<?> asset;
- if(identityProvider.isRestrictedUser(authContext)) {
- Optional<UserAsset> userAsset = assetStorageService.findUserAssets(connection.realm, authContext.getUserId(), assetId).stream().findFirst();
- asset = userAsset.map(value -> assetStorageService.find(value.getId().getAssetId())).orElse(null);
+ if (isWrite) {
+ result = handler.checkCanPublish(connection, topic);
} else {
- asset = assetStorageService.find(assetId);
- }
-
- if (asset == null) {
- LOG.fine("Asset not found for topic '" + topic + "': " + connection);
- return false;
- }
-
- if (isAttributeTopic && topicTokens.size() > 4
- && !(SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(4)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(4)))) {
- String attributeName = topicTokens.get(4);
-
- if (!asset.hasAttribute(attributeName)) {
- LOG.fine("Asset attribute not found for topic '" + topic + "': " + connection);
- return false;
- }
+ result = handler.checkCanSubscribe(connection, topic);
}
- }
- } else if (!isWrite) {
- LOG.fine("Topic must contain at least three tokens '" + topic + "': " + connection);
- return false;
- }
-
- if (isWrite) {
-
- if (topicTokens.contains(SINGLE_LEVEL_WILDCARD) || topicTokens.contains(MULTI_LEVEL_WILDCARD)) {
- LOG.fine("Write does not support wildcards '" + topic + "': " + connection);
- return false;
- }
-
- if (isAssetTopic) {
- LOG.fine("Cannot write assets '" + topic + "': " + connection);
- return false;
- }
-
-
- if (topicTokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC) && topicTokens.size() != 5) {
- LOG.info("Write request to attribute value topic should contain clientId, asset ID and attribute name only, topic '" + topic + "': " + connection);
- return false;
- }
-
- if (topicTokens.get(2).equals(ATTRIBUTE_TOPIC) && topicTokens.size() != 3) {
- LOG.info("Write request to attribute topic should not contain any other tokens, topic '" + topic + "': " + connection);
- return false;
- }
-
- // Check we have the correct write role
- if (!authContext.hasResourceRole(ClientRole.WRITE_ATTRIBUTES.getValue(), username)) {
- LOG.info("Write request to attribute topic but user doesn't have required role, topic '" + topic + "': " + connection);
- return false;
- }
-
- // Security of attribute write will be handled by the asset processing service so don't need to do anything here
- } else { // read
-
- // Build filter for the topic and verify that the filter is OK for given auth context
- AssetFilter filter = buildAssetFilter(connection, topicTokens);
- Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;
-
- if (filter == null) {
- LOG.info("Invalid event filter generated for topic '" + topic + "': " + connection);
- return false;
- }
- EventSubscription subscription = new EventSubscription(
- subscriptionClass,
- filter
- );
-
- if (!clientEventService.authorizeEventSubscription(authContext, subscription)) {
- LOG.info("Generated event filter was not authorised for topic '" + topic + "': " + connection);
- return false;
+ return result;
}
}
- return true;
+ LOG.fine("No handler has allowed " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", connection=" + connection);
+ return false;
}
}
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 3781594ad..6888a6e70 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java
@@ -20,38 +20,19 @@
package org.openremote.manager.mqtt;
import com.google.api.client.util.Charsets;
-import io.moquette.broker.subscriptions.Token;
import io.moquette.broker.subscriptions.Topic;
import io.moquette.interception.AbstractInterceptHandler;
import io.moquette.interception.messages.*;
-import org.keycloak.adapters.rotation.AdapterTokenVerifier;
-import org.keycloak.common.VerificationException;
-import org.keycloak.representations.AccessToken;
import org.openremote.container.message.MessageBrokerService;
-import org.openremote.container.security.keycloak.AccessTokenAuthContext;
import org.openremote.container.web.ConnectionConstants;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
-import org.openremote.model.Constants;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetFilter;
-import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.event.shared.CancelEventSubscription;
-import org.openremote.model.event.shared.EventSubscription;
-import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.syslog.SyslogCategory;
-import org.openremote.model.util.ValueUtil;
-import java.nio.charset.StandardCharsets;
-import java.util.HashMap;
-import java.util.List;
import java.util.Map;
-import java.util.function.Consumer;
-import java.util.logging.Level;
import java.util.logging.Logger;
-import java.util.stream.Collectors;
-import static org.openremote.manager.mqtt.MqttBrokerService.*;
+import static org.openremote.manager.mqtt.MqttBrokerService.prepareHeaders;
import static org.openremote.model.syslog.SyslogCategory.API;
public class ORInterceptHandler extends AbstractInterceptHandler {
@@ -60,16 +41,13 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
protected final MqttBrokerService brokerService;
protected final MessageBrokerService messageBrokerService;
protected final ManagerKeycloakIdentityProvider identityProvider;
- protected final Map<String, MqttConnection> sessionIdConnectionMap;
public ORInterceptHandler(MqttBrokerService brokerService,
ManagerKeycloakIdentityProvider identityProvider,
- MessageBrokerService messageBrokerService,
- Map<String, MqttConnection> sessionIdConnectionMap) {
+ MessageBrokerService messageBrokerService) {
this.brokerService = brokerService;
this.identityProvider = identityProvider;
this.messageBrokerService = messageBrokerService;
- this.sessionIdConnectionMap = sessionIdConnectionMap;
}
@Override
@@ -93,7 +71,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
public void onConnect(InterceptConnectMessage msg) {
String realm = null;
String username = null;
- String password = msg.getPassword() != null ? new String(msg.getPassword(), Charsets.UTF_8) : null;
+ String password = msg.isPasswordFlag() ? new String(msg.getPassword(), Charsets.UTF_8) : null;
if (msg.getUsername() != null) {
String[] realmAndUsername = msg.getUsername().split(":");
@@ -103,7 +81,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password);
- sessionIdConnectionMap.put(connection.getClientId(), connection);
+ brokerService.addConnection(connection.getClientId(), connection);
Map<String, Object> headers = prepareHeaders(connection);
headers.put(ConnectionConstants.SESSION_OPEN, true);
messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);
@@ -115,7 +93,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
@Override
public void onDisconnect(InterceptDisconnectMessage msg) {
- MqttConnection connection = sessionIdConnectionMap.remove(msg.getClientID());
+ MqttConnection connection = brokerService.removeConnection(msg.getClientID());
if (connection != null) {
Map<String, Object> headers = prepareHeaders(connection);
@@ -130,7 +108,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
@Override
public void onConnectionLost(InterceptConnectionLostMessage msg) {
- MqttConnection connection = sessionIdConnectionMap.remove(msg.getClientID());
+ MqttConnection connection = brokerService.removeConnection(msg.getClientID());
if (connection != null) {
Map<String, Object> headers = prepareHeaders(connection);
@@ -143,11 +121,10 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
}
}
- @SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void onSubscribe(InterceptSubscribeMessage msg) {
- MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+ MqttConnection connection = brokerService.getConnection(msg.getClientID());
if (connection == null) {
LOG.info("No connection found: clientID=" + msg.getClientID());
@@ -156,44 +133,19 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
Topic topic = Topic.asTopic(msg.getTopicFilter());
- for (MQTTCustomHandler handler : brokerService.getCustomHandlers()) {
- if (handler.onSubscribe(connection, topic, msg)) {
- LOG.info("Custom handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
- return;
+ for (MQTTHandler handler : brokerService.getCustomHandlers()) {
+ if (handler.handlesTopic(topic)) {
+ LOG.info("Handler has handled subscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ handler.doSubscribe(connection, topic, msg);
+ break;
}
}
-
- List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
- boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);
- boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);
- boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(2));
- String subscriptionId = msg.getTopicFilter(); // Use topic as subscription ID
-
- AssetFilter filter = buildAssetFilter(connection, topicTokens);
- Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;
-
- if (filter == null) {
- LOG.fine("Invalid event filter generated for topic '" + topic + "': " + connection);
- return;
- }
-
- EventSubscription subscription = new EventSubscription(
- subscriptionClass,
- filter,
- subscriptionId
- );
-
- Consumer<SharedEvent> eventConsumer = brokerService.getEventConsumer(connection, subscriptionId, isValueSubscription, msg.getRequestedQos());
- connection.subscriptionHandlerMap.put(subscriptionId, eventConsumer);
- Map<String, Object> headers = prepareHeaders(connection);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);
}
- @SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void onUnsubscribe(InterceptUnsubscribeMessage msg) {
- MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+ MqttConnection connection = brokerService.getConnection(msg.getClientID());
if (connection == null) {
LOG.info("No connection found: clientID=" + msg.getClientID());
@@ -202,19 +154,12 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
Topic topic = Topic.asTopic(msg.getTopicFilter());
- // Notify all custom handlers
- brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onUnsubscribe(connection, topic, msg));
-
- String subscriptionId = topic.toString();
- Consumer<? extends SharedEvent> eventConsumer = connection.subscriptionHandlerMap.remove(subscriptionId);
-
- if (eventConsumer != null) {
- boolean isAssetTopic = subscriptionId.startsWith(ASSET_TOPIC);
-
- Map<String, Object> headers = prepareHeaders(connection);
- Class<SharedEvent> subscriptionClass = (Class) (isAssetTopic ? AssetEvent.class : AttributeEvent.class);
- CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(subscriptionClass, subscriptionId);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);
+ for (MQTTHandler handler : brokerService.getCustomHandlers()) {
+ if (handler.handlesTopic(topic)) {
+ LOG.info("Handler has handled unsubscribe: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ handler.doUnsubscribe(connection, topic, msg);
+ break;
+ }
}
}
@@ -223,7 +168,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
String[] realmAndUsername = msg.getUsername().split(":");
String realm = realmAndUsername[0];
String username = realmAndUsername[1];
- MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());
+ MqttConnection connection = brokerService.getConnection(msg.getClientID());
if (connection == null) {
LOG.warning("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());
@@ -232,50 +177,12 @@ public class ORInterceptHandler extends AbstractInterceptHandler {
Topic topic = Topic.asTopic(msg.getTopicName());
- for (MQTTCustomHandler handler : brokerService.getCustomHandlers()) {
- if (handler.onPublish(connection, topic, msg)) {
- LOG.info("Custom handler has handled publish: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
- return;
- }
- }
-
- List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());
- boolean isValueWrite = topicTokens.get(2).equals(ATTRIBUTE_VALUE_TOPIC);
- String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);
- AttributeEvent attributeEvent;
-
- if (isValueWrite) {
- String assetId = topicTokens.get(3);
- String attributeName = topicTokens.get(4);
- Object value = ValueUtil.parse(payloadContent).orElse(null);
- attributeEvent = new AttributeEvent(assetId, attributeName, value);
- } else {
- attributeEvent = ValueUtil.parse(payloadContent, AttributeEvent.class).orElse(null);
- }
-
- if (attributeEvent == null) {
- LOG.fine("Failed to parse payload for publish topic '" + topic + "': " + connection);
- return;
- }
-
- Map<String, Object> headers = prepareHeaders(connection);
- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);
- }
-
- protected 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);
-
- try {
- String token = connection.getAccessToken();
- if (token != null) {
- AccessToken accessToken = AdapterTokenVerifier.verifyToken(token, identityProvider.getKeycloakDeployment(connection.realm, connection.getUsername()));
- headers.put(Constants.AUTH_CONTEXT, new AccessTokenAuthContext(connection.realm, accessToken));
+ for (MQTTHandler handler : brokerService.getCustomHandlers()) {
+ if (handler.handlesTopic(topic)) {
+ LOG.info("Handler has handled publish: handler=" + handler.getName() + ", topic=" + topic + ", connection=" + connection);
+ handler.doPublish(connection, topic, msg);
+ break;
}
- } catch (VerificationException e) {
- LOG.log(Level.FINE, "Couldn't verify token", e);
}
- return headers;
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ClientProvisioningCustomHandler.java b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java
similarity index 55%
rename from manager/src/main/java/org/openremote/manager/mqtt/ClientProvisioningCustomHandler.java
rename to manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java
index d5aa26428..65a637cff 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ClientProvisioningCustomHandler.java
+++ b/manager/src/main/java/org/openremote/manager/provisioning/UserAssetProvisioningMQTTHandler.java
@@ -17,56 +17,40 @@
* 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;
+package org.openremote.manager.provisioning;
import io.moquette.broker.subscriptions.Topic;
-import io.moquette.interception.messages.*;
-import org.openremote.container.security.AuthContext;
-import org.openremote.model.Container;
+import io.moquette.interception.messages.InterceptPublishMessage;
+import io.moquette.interception.messages.InterceptSubscribeMessage;
+import io.moquette.interception.messages.InterceptUnsubscribeMessage;
+import org.openremote.manager.mqtt.MQTTHandler;
+import org.openremote.manager.mqtt.MqttConnection;
import org.openremote.model.syslog.SyslogCategory;
-import java.util.Arrays;
import java.util.logging.Logger;
import static org.openremote.model.syslog.SyslogCategory.API;
-public class ClientProvisioningCustomHandler implements MQTTCustomHandler {
-
- private static final Logger LOG = SyslogCategory.getLogger(API, ClientProvisioningCustomHandler.class);
- public static final String TOP_LEVEL_TOPIC = "client";
-
- @Override
- public String getName() {
- return this.getClass().getSimpleName();
- }
-
- @Override
- public void start(Container container) throws Exception {
-
- }
-
- @Override
- public void stop() throws Exception {
-
- }
-
- @Override
- public void onConnect(MqttConnection connection, InterceptConnectMessage msg) {
+/**
+ * This {@link MQTTHandler} is responsible for provisioning service users and assets
+ */
+public class UserAssetProvisioningMQTTHandler extends MQTTHandler {
- }
+ private static final Logger LOG = SyslogCategory.getLogger(API, UserAssetProvisioningMQTTHandler.class);
+ public static final String TOP_LEVEL_TOPIC = "provisioning";
@Override
- public void onDisconnect(MqttConnection connection, InterceptDisconnectMessage msg) {
-
+ public boolean topicMatches(Topic topic) {
+ return false;
}
@Override
- public void onConnectionLost(MqttConnection connection, InterceptConnectionLostMessage msg) {
-
+ protected Logger getLogger() {
+ return LOG;
}
@Override
- public boolean canSubscribe(AuthContext authContext, MqttConnection connection, Topic topic) {
+ public boolean canSubscribe(MqttConnection connection, Topic topic) {
if (!TOP_LEVEL_TOPIC.equals(topic.headToken().toString())) {
return false;
}
@@ -84,22 +68,22 @@ public class ClientProvisioningCustomHandler implements MQTTCustomHandler {
}
@Override
- public boolean canPublish(AuthContext authContext, MqttConnection connection, Topic topic) {
- return false;
- }
+ public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {
- @Override
- public boolean onSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {
- return false;
}
@Override
- public void onUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {
+ public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {
}
@Override
- public boolean onPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {
+ public boolean canPublish(MqttConnection connection, Topic topic) {
return false;
}
+
+ @Override
+ public void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {
+
+ }
}
diff --git a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTCustomHandler b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTCustomHandler
deleted file mode 100644
index 1c4f22add..000000000
--- a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTCustomHandler
+++ /dev/null
@@ -1 +0,0 @@
-org.openremote.manager.mqtt.ClientProvisioningCustomHandler
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
new file mode 100644
index 000000000..39e1cd182
--- /dev/null
+++ b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler
@@ -0,0 +1,2 @@
+org.openremote.manager.mqtt.DefaultMQTTHandler
+org.openremote.manager.provisioning.UserAssetProvisioningMQTTHandler
diff --git a/manager/src/main/resources/logging.properties b/manager/src/main/resources/logging.properties
index 56377a12e..e046d0cc1 100644
--- a/manager/src/main/resources/logging.properties
+++ b/manager/src/main/resources/logging.properties
@@ -99,3 +99,4 @@ org.apache.camel.management.level=WARNING
org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
org.apache.camel.impl.DefaultShutdownStrategy.level=SEVERE
org.jeasy.rules.level=WARNING
+io.moquette.broker.level=WARNING
diff --git a/model/src/main/java/org/openremote/model/asset/AssetEvent.java b/model/src/main/java/org/openremote/model/asset/AssetEvent.java
index 41d8f9aca..56a7ba95f 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetEvent.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetEvent.java
@@ -57,6 +57,10 @@ public class AssetEvent extends SharedEvent implements AssetInfo {
return asset.id;
}
+ public String getAssetName() {
+ return asset.name;
+ }
+
@Override
public String getRealm() {
return asset.realm;
diff --git a/test/src/main/groovy/org/openremote/test/MqttClient.groovy b/test/src/main/groovy/org/openremote/test/MqttClient.groovy
deleted file mode 100644
index 6e2288f50..000000000
--- a/test/src/main/groovy/org/openremote/test/MqttClient.groovy
+++ /dev/null
@@ -1,194 +0,0 @@
-package org.openremote.test
-
-import io.netty.bootstrap.Bootstrap
-import io.netty.buffer.ByteBuf
-import io.netty.buffer.Unpooled
-import io.netty.channel.Channel
-import io.netty.channel.ChannelHandler
-import io.netty.channel.ChannelHandlerContext
-import io.netty.channel.ChannelInboundHandlerAdapter
-import io.netty.channel.ChannelInitializer
-import io.netty.channel.ChannelOption
-import io.netty.channel.ChannelPipeline
-import io.netty.channel.EventLoopGroup
-import io.netty.channel.nio.NioEventLoopGroup
-import io.netty.channel.socket.SocketChannel
-import io.netty.channel.socket.nio.NioSocketChannel
-import io.netty.util.ReferenceCountUtil
-import org.openremote.model.asset.agent.ConnectionStatus
-
-import java.util.concurrent.CountDownLatch
-import java.util.concurrent.Semaphore
-import java.util.concurrent.TimeUnit
-
-import static io.netty.channel.ChannelFutureListener.CLOSE_ON_FAILURE
-
-class RawClient {
-
- @ChannelHandler.Sharable
- class RawMessageHandler extends ChannelInboundHandlerAdapter {
-
- @Override
- void channelRead(ChannelHandlerContext ctx, Object message) {
- try {
- ByteBuf byteBuf = (ByteBuf) message
- int readBytes = byteBuf.readableBytes()
- heapBuffer.writeBytes(byteBuf)
- readableBytesSem.release(readBytes)
- } finally {
- ReferenceCountUtil.release(message)
- }
- }
-
- @Override
- void channelInactive(ChannelHandlerContext ctx) {
- disconnectLatch.countDown()
- ctx.close().addListener(CLOSE_ON_FAILURE)
- }
- }
-
- final RawMessageHandler handler
- EventLoopGroup workerGroup
- Channel m_channel
-
- private boolean connected
- private ByteBuf heapBuffer
- private CountDownLatch disconnectLatch
- private final Semaphore readableBytesSem
-
- @SuppressWarnings("FutureReturnValueIgnored")
- private RawClient(String host, int port) {
- handler = new RawMessageHandler()
- heapBuffer = Unpooled.buffer(128)
- disconnectLatch = new CountDownLatch(1)
- readableBytesSem = new Semaphore(0, true)
-
- workerGroup = new NioEventLoopGroup()
-
- try {
- Bootstrap b = new Bootstrap()
- b.group(workerGroup)
- b.channel(NioSocketChannel.class)
- b.option(ChannelOption.SO_KEEPALIVE, true)
- b.handler(new ChannelInitializer<SocketChannel>() {
-
- @Override
- void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline pipeline = ch.pipeline()
- pipeline.addLast("handler", handler)
- }
- })
-
- // Start the client.
- m_channel = b.connect(host, port).sync().channel()
-
- m_channel.closeFuture().addListener({
- this.connected = false
- })
-
- this.connected = true
- } catch (Exception ex) {
- workerGroup.shutdownGracefully()
- }
- }
-
- static RawClient connect(String host, int port) {
- return new RawClient(host, port)
- }
-
- boolean isConnected() {
- return this.connected
- }
-
- RawClient write(int... bytes) {
- ByteBuf buff = Unpooled.buffer(bytes.length)
- buff.clear()
- for (int b : bytes) {
- buff.writeByte((byte) b)
- }
- m_channel.write(buff).addListener(CLOSE_ON_FAILURE)
- return this
- }
-
- RawClient writeWithSize(String str) {
- ByteBuf buff = Unpooled.buffer(str.length() + 2)
- buff.writeBytes(encodeString(str))
- m_channel.write(buff).addListener(CLOSE_ON_FAILURE)
- return this
- }
-
- /**
- * Write just String bytes not length
- */
- RawClient write(String str) {
- ByteBuf out = Unpooled.buffer(str.length())
- byte[] raw
- try {
- raw = str.getBytes("UTF-8")
- // NB every Java platform has got UTF-8 encoding by default, so this
- // exception are never raised.
- } catch (UnsupportedEncodingException ex) {
- throw new IllegalStateException(ex)
- }
- out.writeBytes(raw)
- m_channel.write(out).addListener(CLOSE_ON_FAILURE)
- return this
- }
-
- RawClient flush() {
- m_channel.flush()
- return this
- }
-
- RawClient read(int expectedByte) {
- try {
- readableBytesSem.acquire(1)
- } catch (InterruptedException e) {
- throw new IllegalStateException("Interrupted while waiting data")
- }
- byte b = heapBuffer.readByte()
- if (b != expectedByte) {
- throw new IllegalStateException(String.format("Expected byte 0x%02X but found 0x%02X", b, expectedByte))
- }
- return this
- }
-
- /**
- * Expect the closing of the underling channel, with timeout
- */
- void closed(long timeout) throws InterruptedException {
- disconnectLatch.await(timeout, TimeUnit.MILLISECONDS)
- }
-
- /**
- * Expect closing the connect with unbound time
- */
- void closed() throws InterruptedException {
- disconnectLatch.await()
- }
-
- /**
- * Return the IoBuffer with string encoded as MSB, LSB and UTF-8 encoded string content.
- */
- static ByteBuf encodeString(String str) {
- byte[] raw
- try {
- raw = str.getBytes("UTF-8")
- // NB every Java platform has got UTF-8 encoding by default, so this
- // exception are never raised.
- } catch (UnsupportedEncodingException ex) {
- return null
- }
- return encodeFixedLengthContent(raw)
- }
-
- /**
- * Return the IoBuffer with string encoded as MSB, LSB and bytes array content.
- */
- static ByteBuf encodeFixedLengthContent(byte[] content) {
- ByteBuf out = Unpooled.buffer(2)
- out.writeShort(content.length)
- out.writeBytes(content)
- return out
- }
-}
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 711b647d3..a5b1759a8 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -2,63 +2,47 @@ package org.openremote.test.mqtt
import com.google.common.collect.Lists
import io.moquette.BrokerConstants
-import io.netty.handler.codec.mqtt.MqttQoS
+import org.openremote.agent.protocol.mqtt.MQTTMessage
+import org.openremote.agent.protocol.mqtt.MQTT_IOClient
import org.openremote.agent.protocol.simulator.SimulatorProtocol
import org.openremote.container.util.UniqueIdentifierGenerator
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.mqtt.DefaultMQTTHandler
+import org.openremote.manager.mqtt.MQTTHandler
import org.openremote.manager.mqtt.MqttBrokerService
-import org.openremote.manager.mqtt.MqttConnection
import org.openremote.manager.setup.SetupService
import org.openremote.model.asset.AssetEvent
+import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeEvent
+import org.openremote.model.auth.UsernamePassword
+import org.openremote.model.event.shared.SharedEvent
import org.openremote.model.util.ValueUtil
import org.openremote.test.ManagerContainerTrait
-import org.openremote.test.RawClient
import org.openremote.test.setup.KeycloakTestSetup
import org.openremote.test.setup.ManagerTestSetup
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
+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.*
+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.model.value.ValueType.TEXT
-// TODO: Change to use MQTT client once implemented
class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def "Mqtt broker event test"() {
given: "the container environment is started"
- def mqttBrokerServiceAssetEventCalls = 0
- def mqttBrokerServiceAttributeEventCalls = 0
- def mqttBrokerServiceAttributeValueCalls = 0
-
- def spyMqttBrokerService = Spy(MqttBrokerService) {
- getEventConsumer(_ as MqttConnection, _ as String, _ as Boolean, _ as MqttQoS) >> {
- connection, topic, isValueSubscription, qos ->
- return {
- if (isValueSubscription) {
- if (it instanceof AttributeEvent) {
- mqttBrokerServiceAttributeValueCalls++
- }
- } else {
- if (it instanceof AttributeEvent) {
- mqttBrokerServiceAttributeEventCalls++
- } else if (it instanceof AssetEvent) {
- mqttBrokerServiceAssetEventCalls++
- }
- }
- }
- }
- }
-
- def conditions = new PollingConditions(timeout: 10, initialDelay: 1, delay: 0.2)
- def services = Lists.newArrayList(defaultServices())
- services.replaceAll { it instanceof MqttBrokerService ? spyMqttBrokerService : it }
- def container = startContainer(defaultConfig(), services)
+ List<SharedEvent> receivedEvents = []
+ List<Object> receivedValues = []
+
+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.1, delay: 0.2)
+ def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
def mqttBrokerService = container.getService(MqttBrokerService.class)
@@ -71,193 +55,89 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
def mqttHost = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)
def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)
- when: "a mqtt client connects with wrong realm credentials"
+ when: "a mqtt client connects with invalid credentials"
def wrongUsername = "master:" + KeycloakTestSetup.serviceUserId
- def payloadLength = mqttClientId.size() + wrongUsername.size() + password.size()
- def remainingLength = 16 + payloadLength
- def client = RawClient.connect(mqttHost, mqttPort)
- // CONNECT
- .write(0x10) // MQTT Control Packet type(1)
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x04) // Protocol Name Length
- .write("MQTT") // Protocol Name
- .write(0x04) // The value of the Protocol Level field for the version 3.1.1 of the protocol is 4 (0x04)
-
- // Connect Flags
- // User Name Flag(1)
- // Password Flag(1)
- // Will Retain(0)
- // Will QoS(00)
- // Will Flag(0)
- // Clean Session(1)
- // Reserved(0)
- .write(0xC2)
- .write(0x00, 0x00) // Keep Alive
-
- // Payload
- .write(0x00, mqttClientId.size().byteValue()) // Client Identifier Length
- .write(mqttClientId) // Client Identifier
- .write(0x00, wrongUsername.size().byteValue())
- .write(wrongUsername)
- .write(0x00, password.size().byteValue())
- .write(password)
- .flush()
-
- then: "no connection should be made"
+ MQTT_IOClient client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, new UsernamePassword(wrongUsername, password), null)
+ client.connect()
+
+ then: "the client connection status should be in error"
conditions.eventually {
- assert !client.isConnected()
- assert mqttBrokerService.clientIdConnectionMap.size() == 0
+ assert client.getConnectionStatus() == ConnectionStatus.WAITING
}
- when: "a mqtt client connects"
- payloadLength = mqttClientId.size() + username.size() + password.size()
- remainingLength = 16 + payloadLength
- client = RawClient.connect(mqttHost, mqttPort)
- // CONNECT
- .write(0x10) // MQTT Control Packet type(1)
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x04) // Protocol Name Length
- .write("MQTT") // Protocol Name
- .write(0x04) // The value of the Protocol Level field for the version 3.1.1 of the protocol is 4 (0x04)
-
- // Connect Flags
- // User Name Flag(1)
- // Password Flag(1)
- // Will Retain(0)
- // Will QoS(00)
- // Will Flag(0)
- // Clean Session(1)
- // Reserved(0)
- .write(0xC2)
- .write(0x00, 0x00) // Keep Alive
-
- // Payload
- .write(0x00, mqttClientId.size().byteValue()) // Client Identifier Length
- .write(mqttClientId) // Client Identifier
- .write(0x00, username.size().byteValue())
- .write(username)
- .write(0x00, password.size().byteValue())
- .write(password)
- .flush()
+ when: "a mqtt client connects with valid credentials"
+ client.disconnect()
+ mqttClientId = UniqueIdentifierGenerator.generateId()
+ client = new MQTT_IOClient(mqttClientId, mqttHost, mqttPort, false, new UsernamePassword(username, password), null)
+ client.connect()
then: "mqtt connection should exist"
conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null
}
when: "a mqtt client subscribes to an asset in another realm"
- def topic = keycloakTestSetup.tenantCity.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.thingId
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
-
- then: "No subscription should exist"
- conditions.eventually {
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
- }
-
- when: "a mqtt client subscribes to an non existing asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/2F6uvhLm8w110jug2caukV"
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ def topic = "${keycloakTestSetup.tenantCity.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.thingId".toString()
+ client.addMessageConsumer(topic, {msg -> })
then: "No subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes with clientId missing"
- topic = keycloakTestSetup.tenantBuilding.realm + "/attribute/" + managerTestSetup.apartment1HallwayId
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()
+ client.addMessageConsumer(topic, {msg ->})
then: "No subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
when: "a mqtt client subscribes with different clientId"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + UniqueIdentifierGenerator.generateId() + "/attribute/" + managerTestSetup.apartment1HallwayId
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ def newClientId = UniqueIdentifierGenerator.generateId()
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$newClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()
+ client.addMessageConsumer(topic, {msg ->})
then: "No subscription should exist"
conditions.eventually {
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
+ assert client.topicConsumerMap.get(topic) == null // Consumer added and removed on failure
+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null
+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.isEmpty()
}
- when: "a mqtt client subscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ when: "a mqtt client subscribes to all attributes of an asset"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$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)
+ receivedEvents.add(event.get())
+ }
+ client.addMessageConsumer(topic, eventConsumer)
then: "A subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) != null
+ assert client.topicConsumerMap.get(topic).size() == 1
+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
- when: "An asset attribute changed the client is subscribed on"
+ when: "An attribute event occurs for a subscribed attribute"
def attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 50)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
then: "A publish event message should be sent"
conditions.eventually {
- assert mqttBrokerServiceAttributeEventCalls == 1
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 50
}
+ receivedEvents.clear()
when: "Another asset attribute changed the client is subscribed on"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", true)
@@ -265,412 +145,294 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
then: "A second publish event message should be sent"
conditions.eventually {
- assert mqttBrokerServiceAttributeEventCalls == 2
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "presenceDetected"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(false) == true
}
+ receivedEvents.clear()
when: "a mqtt client publishes to an asset attribute which is readonly"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId
- def payload = ValueUtil.asJSON(ValueUtil.createJsonObject().put("motionSensor", 70)).orElse(null)
- remainingLength = 2 + topic.size() + payload.length()
-
- //PUBLISH
- client.write(0x30) // MQTT Control Packet type(10) with QoS level 0
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(payload) // content
- .flush()
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()
+ def payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 70)).get()
+ client.sendMessage(new MQTTMessage<String>(topic, payload))
then: "The value of the attribute shouldn't be updated"
- conditions.eventually {
+ new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {
def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)
assert asset.getAttribute("motionSensor").get().value.orElse(0) == 50d
}
- when: "a mqtt client publishes to an asset attribute"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/lights"
- payload = ValueUtil.asJSON(false).orElse(null)
- remainingLength = 2 + topic.size() + payload.length()
-
- //PUBLISH
- client.write(0x30) // MQTT Control Packet type(10) with QoS level 0
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(payload) // content
- .flush()
+ when: "a mqtt client publishes to an asset attribute which is not readonly"
+ payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "lights", false)).get()
+ client.sendMessage(new MQTTMessage<String>(topic, payload))
- then: "The value of the attribute should be updated and another publish event should be sent"
+ then: "the value of the attribute should be updated and the client should have received the event"
conditions.eventually {
def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)
assert !asset.getAttribute("lights").get().value.orElse(true)
- assert mqttBrokerServiceAttributeEventCalls == 3
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "lights"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(true) == false
}
+ receivedEvents.clear()
- when: "a mqtt client unsubscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId
- remainingLength = 4 + topic.size()
-
- client
- // UNSUBSCRIBE
- .write(0xA2) // MQTT Control Packet type(10) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
+ when: "a mqtt client publishes to an asset attribute value which is not readonly"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/lights/$managerTestSetup.apartment1HallwayId".toString()
+ payload = ValueUtil.asJSON(true).orElse(null)
+ client.sendMessage(new MQTTMessage<String>(topic, payload))
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .flush()
+ then: "the value of the attribute should be updated and the client should have received the event"
+ conditions.eventually {
+ def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)
+ assert asset.getAttribute("lights").get().value.orElse(false)
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "lights"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(false) == true
+ }
+ receivedEvents.clear()
+ when: "a mqtt client unsubscribes from an asset"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()
+ client.removeMessageConsumer(topic, eventConsumer)
then: "No subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) == null
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
- when: "Another asset attribute changed without any subscriptions"
+ when: "another asset attribute changed without any subscriptions"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", false)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
then: "No publish event message should be sent"
- conditions.eventually {
- assert mqttBrokerServiceAttributeEventCalls == 3
+ new PollingConditions(initialDelay: 1, delay: 1, timeout: 10).eventually {
+ assert receivedEvents.size() == 0
}
- when: "a mqtt client subscribes to an asset attribute"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ when: "a mqtt client subscribes to a specific asset attribute"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/motionSensor/$managerTestSetup.apartment1HallwayId".toString()
+ client.addMessageConsumer(topic, eventConsumer)
then: "the subscription should be created"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) != null
+ assert client.topicConsumerMap.get(topic).size() == 1
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
}
- when: "that attribute changed"
+ when: "that attribute changes"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 30)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
- then: "A publish event message should be sent"
+ then: "a publish event message should be sent to the client"
conditions.eventually {
- assert mqttBrokerServiceAttributeEventCalls == 4
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 30
}
+ receivedEvents.clear()
- when: "Another asset attribute changed without any subscriptions on that attribute"
+ when: "another asset attribute changed without any subscriptions on that attribute"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", true)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
- then: "No publish event message should be sent"
- conditions.eventually {
- assert mqttBrokerServiceAttributeEventCalls == 4
+ then: "no publish event message should be sent"
+ new PollingConditions(initialDelay: 1, delay: 1, timeout: 10).eventually {
+ assert receivedEvents.size() == 0
}
- when: "a mqtt client unsubscribes to an asset attribute"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
- remainingLength = 4 + topic.size()
-
- client
- // UNSUBSCRIBE
- .write(0xA2) // MQTT Control Packet type(10) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .flush()
-
+ when: "a mqtt client unsubscribes from an asset attribute"
+ client.removeMessageConsumer(topic, eventConsumer)
then: "No subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) == null
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
}
- when: "a mqtt client subscribes with multilevel on asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attribute/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
+ when: "a mqtt client subscribes to attributes for descendants of an asset"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId/$MQTTHandler.TOKEN_MULTI_LEVEL_WILDCARD".toString()
+ client.addMessageConsumer(topic, eventConsumer)
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
-
- then: "A subscription should exist"
+ then: "a subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) != null
+ assert client.topicConsumerMap.get(topic).size() == 1
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
- when: "An child asset attribute changed the client is subscribed on"
+ when: "a child asset of the subscription attribute event occurs"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 40)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
- then: "A publish event message should be sent"
+ then: "a publish event message should be sent"
conditions.eventually {
- assert mqttBrokerServiceAttributeEventCalls == 5
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 40
}
+ receivedEvents.clear()
when: "a mqtt client subscribes to an asset attribute value"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC/motionSensor/$managerTestSetup.apartment1HallwayId".toString()
+ Consumer<MQTTMessage<String>> valueConsumer = { msg ->
+ receivedValues.add(msg.payload)
+ }
+ client.addMessageConsumer(topic, valueConsumer)
then: "the subscription should be created"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) != null
+ assert client.topicConsumerMap.get(topic).size() == 1
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
}
- when: "that attribute changed"
- attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 40)
+ when: "a subscribed attribute changes"
+ attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 50)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
- then: "A publish value message should be sent"
+ then: "A publish value message should be sent to both subscriptions"
conditions.eventually {
- assert mqttBrokerServiceAttributeValueCalls == 1
+ assert receivedValues.size() == 1
+ assert receivedValues.get(0) == "50"
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 50
}
+ receivedEvents.clear()
+ receivedValues.clear()
- when: "Another asset attribute changed without any subscriptions on that attribute"
+ when: "a subscribed attribute changes"
attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "presenceDetected", false)
((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)
- then: "No publish value message should be sent"
+ then: "only the wildcard subscription should have received a publish"
conditions.eventually {
- assert mqttBrokerServiceAttributeValueCalls == 1
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AttributeEvent
+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId
+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "presenceDetected"
+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(true) == false
+ assert receivedValues.size() == 0
}
- when: "a mqtt client unsubscribes to an asset attribute value"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"
- remainingLength = 4 + topic.size()
-
- client
- // UNSUBSCRIBE
- .write(0xA2) // MQTT Control Packet type(10) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
+ when: "the client unsubscribes from the attribute value topic"
+ client.removeMessageConsumer(topic, valueConsumer)
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .flush()
-
-
- then: "No subscription should exist"
+ then: "only one subscription should remain"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) == null
+ assert client.topicConsumerMap.size() == 1
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
- when: " a client disconnects"
- client
- // DISCONNECT
- .write(0xE0) // MQTT Control Packet type(14) with QoS level 0
- .write(0.byteValue()) // Remaining Length
- .flush()
+ when: "a client disconnects"
+ client.disconnect()
- then: "no connection should be left"
+ then: "the client status should be disconnected"
conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.DISCONNECTED
assert mqttBrokerService.clientIdConnectionMap.size() == 0
}
- when: "a mqtt client reconnects"
- payloadLength = mqttClientId.size() + username.size() + password.size()
- remainingLength = 16 + payloadLength
- client = RawClient.connect(mqttHost, mqttPort)
- // CONNECT
- .write(0x10) // MQTT Control Packet type(1)
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x04) // Protocol Name Length
- .write("MQTT") // Protocol Name
- .write(0x04) // The value of the Protocol Level field for the version 3.1.1 of the protocol is 4 (0x04)
-
- // Connect Flags
- // User Name Flag(1)
- // Password Flag(1)
- // Will Retain(0)
- // Will QoS(00)
- // Will Flag(0)
- // Clean Session(1)
- // Reserved(0)
- .write(0xC2)
- .write(0x00, 0x00) // Keep Alive
-
- // Payload
- .write(0x00, mqttClientId.size().byteValue()) // Client Identifier Length
- .write(mqttClientId) // Client Identifier
- .write(0x00, username.size().byteValue())
- .write(username)
- .write(0x00, password.size().byteValue())
- .write(password)
- .flush()
+ when: "the client reconnects"
+ receivedEvents.clear()
+ client.connect()
- then: "mqtt connection should exist"
+ then: "the connection should exist"
conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId) != null
}
- when: "a mqtt client subscribes to an asset in another realm"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.thingId
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
-
- then: "No subscription should exist"
- conditions.eventually {
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
- }
-
- when: "a mqtt client subscribes to an non existing asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/2F6uvhLm8w110jug2caukV"
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
-
- then: "No subscription should exist"
- conditions.eventually {
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 0
- }
-
- when: "a mqtt client subscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.apartment1HallwayId
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ when: "a mqtt client subscribes to assets that are direct children of the realm"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ASSET_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD".toString()
+ client.addMessageConsumer(topic, eventConsumer)
then: "A subscription should exist"
conditions.eventually {
- assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
}
- when: "An asset is updated with a new attribute"
- def asset1 = assetStorageService.find(managerTestSetup.apartment1HallwayId)
- asset1.addAttributes(new Attribute<>("temp", TEXT))
+ 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)
then: "A publish event message should be sent"
conditions.eventually {
- assert mqttBrokerServiceAssetEventCalls == 1
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AssetEvent
+ assert (receivedEvents.get(0) as AssetEvent).assetId == managerTestSetup.smartBuildingId
+ assert (receivedEvents.get(0) as AssetEvent).asset.attributes.get("temp") != null
+ assert (receivedEvents.get(0) as AssetEvent).asset.attributes.get("temp").flatMap(){it.getValue()}.orElse(null) == "hello world"
}
+ receivedEvents.clear()
- when: "a mqtt client subscribes for child assets of an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.apartment1Id + "/" + MULTI_LEVEL_WILDCARD
- remainingLength = 4 + topic.size() + 1 //plus one for the QoS byte
-
- client
- // SUBSCRIBE
- .write(0x82) // MQTT Control Packet type(8) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
-
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .write(0x01) // QoS level 1
- .flush()
+ when: "the client subscribes to descendant assets of a specific asset"
+ topic = "${keycloakTestSetup.tenantBuilding.realm}/$mqttClientId/$DefaultMQTTHandler.ASSET_TOPIC/$managerTestSetup.apartment1Id/$MQTTHandler.TOKEN_MULTI_LEVEL_WILDCARD".toString()
+ client.addMessageConsumer(topic, eventConsumer)
- and: "a second subscription is in place"
+ then: "the subscription should exist"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) != null
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 2
+ assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.containsKey(topic)
}
- and: "a asset is added as child to the subcribed asset"
+ when: "an asset is added as a descendant to the subscribed asset"
def childAsset = new ThingAsset("child")
- .setParentId(managerTestSetup.apartment1Id)
+ .setParentId(managerTestSetup.apartment1LivingroomId)
.setRealm(managerTestSetup.realmBuildingTenant);
childAsset = assetStorageService.merge(childAsset);
then: "another event should be sent"
conditions.eventually {
- assert mqttBrokerServiceAssetEventCalls == 2
+ assert receivedEvents.size() == 1
+ assert receivedEvents.get(0) instanceof AssetEvent
+ assert (receivedEvents.get(0) as AssetEvent).assetId == childAsset.id
+ assert (receivedEvents.get(0) as AssetEvent).assetName == childAsset.name
}
+ receivedEvents.clear()
- when: "a mqtt client unsubscribes to an asset"
- topic = keycloakTestSetup.tenantBuilding.realm + "/" + mqttClientId + "/asset/" + managerTestSetup.apartment1HallwayId
- remainingLength = 4 + topic.size()
-
- client
- // UNSUBSCRIBE
- .write(0xA2) // MQTT Control Packet type(10) with QoS level 1
- .write(remainingLength.byteValue()) // Remaining Length
- .write(0x00, 0x10) // MessageId
+ when: "the mqtt client unsubscribes from the multilevel topic"
+ client.removeMessageConsumer(topic, eventConsumer)
- // Payload
- .write(0x00, topic.size().byteValue()) // Topic Length
- .write(topic) // Topic
- .flush()
-
-
- then: "Only one subscription should be left"
+ then: "only one subscription should be left"
conditions.eventually {
+ assert client.topicConsumerMap.get(topic) == null
assert mqttBrokerService.clientIdConnectionMap.get(mqttClientId).subscriptionHandlerMap.size() == 1
}
+ when: "the descendant asset is modified"
+ childAsset.addAttributes(new Attribute<>("temp2", TEXT))
+ childAsset = assetStorageService.merge(childAsset)
+
+ then: "A publish event message should not be sent"
+ new PollingConditions(initialDelay: 1, delay: 1, timeout: 10).eventually {
+ assert receivedEvents.size() == 0
+ }
+
when: "MQTT client disconnects"
- client
- // DISCONNECT
- .write(0xE0) // MQTT Control Packet type(14) with QoS level 0
- .write(0.byteValue()) // Remaining Length
- .flush()
+ client.disconnect()
then: "no connection should be left"
conditions.eventually {
+ assert client.getConnectionStatus() == ConnectionStatus.DISCONNECTED
assert mqttBrokerService.clientIdConnectionMap.size() == 0
}
}
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 2fa3f085b..90d3711f7 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
@@ -112,8 +112,8 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr
new Attribute<>("readWriteTargetTemp", NUMBER)
.addMeta(
new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)
- .setSubscriptionTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/${managerTestSetup.apartment1LivingroomId}/targetTemperature")
- .setPublishTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/${managerTestSetup.apartment1LivingroomId}/targetTemperature")
+ .setSubscriptionTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/targetTemperature/${managerTestSetup.apartment1LivingroomId}")
+ .setPublishTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/targetTemperature/${managerTestSetup.apartment1LivingroomId}")
.setWriteValue("${Protocol.DYNAMIC_VALUE_PLACEHOLDER}")
))
)
diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
new file mode 100644
index 000000000..35b1950ec
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2019, 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.test.provisioning
+
+import io.moquette.BrokerConstants
+import org.junit.Ignore
+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.simulator.SimulatorProtocol
+import org.openremote.container.util.UniqueIdentifierGenerator
+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.setup.SetupService
+import org.openremote.model.Constants
+import org.openremote.model.asset.agent.Agent
+import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.asset.agent.Protocol
+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.test.ManagerContainerTrait
+import org.openremote.test.setup.KeycloakTestSetup
+import org.openremote.test.setup.ManagerTestSetup
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+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.model.value.MetaItemType.AGENT_LINK
+import static org.openremote.model.value.ValueType.NUMBER
+
+@Ignore
+class UserAndAssetProvisioningTest extends Specification implements ManagerContainerTrait {
+
+ @SuppressWarnings("GroovyAccessibility")
+ def "Check websocket client protocol and linked attribute deployment"() {
+
+ given: "expected conditions"
+ 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)
+ 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 = getString(container.getConfig(), MQTT_SERVER_LISTEN_HOST, BrokerConstants.HOST)
+ def mqttPort = getInteger(container.getConfig(), MQTT_SERVER_LISTEN_PORT, BrokerConstants.PORT)
+
+ when: "an MQTT client agent is created to connect to this tests manager"
+ def clientId = UniqueIdentifierGenerator.generateId()
+ def agent = new MQTTAgent("Test agent")
+ .setRealm(Constants.MASTER_REALM)
+ .setClientId(clientId)
+ .setHost(mqttHost)
+ .setPort(mqttPort)
+ .setUsernamePassword(new UsernamePassword(keycloakTestSetup.tenantBuilding.realm + ":" + KeycloakTestSetup.serviceUserId, KeycloakTestSetup.serviceUserSecret))
+
+ and: "the agent is added to the asset service"
+ agent = assetStorageService.merge(agent)
+
+ then: "the protocol should authenticate and the agent status should become CONNECTED"
+ conditions.eventually {
+ agent = assetStorageService.find(agent.id, Agent.class)
+ assert agent.getAgentStatus().orElse(null) == ConnectionStatus.CONNECTED
+ }
+
+ when: "an asset is created with attributes linked to the agent"
+ def asset = new ThingAsset("Test Asset")
+ .setParent(agent)
+ .addOrReplaceAttributes(
+ // write attribute value
+ new Attribute<>("readWriteTargetTemp", NUMBER)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)
+ .setSubscriptionTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/${managerTestSetup.apartment1LivingroomId}/targetTemperature")
+ .setPublishTopic("${keycloakTestSetup.tenantBuilding.realm}/$clientId/attributevalue/${managerTestSetup.apartment1LivingroomId}/targetTemperature")
+ .setWriteValue("${Protocol.DYNAMIC_VALUE_PLACEHOLDER}")
+ ))
+ )
+
+ and: "the asset is merged into the asset service"
+ asset = assetStorageService.merge(asset)
+
+ then: "the linked attributes should be correctly linked"
+ conditions.eventually {
+ assert !(agentService.getProtocolInstance(agent.id) as MQTTProtocol).protocolMessageConsumers.isEmpty()
+ }
+
+ when: "the source attribute are updated"
+ ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(new AttributeEvent(managerTestSetup.apartment1LivingroomId, "targetTemperature", 99d))
+
+ then: "the values should be stored in the database"
+ conditions.eventually {
+ def livingRoom = assetStorageService.find(managerTestSetup.apartment1LivingroomId)
+ assert livingRoom != null
+ assert livingRoom.getAttribute("targetTemperature", Double.class).flatMap{it.value}.orElse(0d) == 99d
+ }
+
+ then: "the linked attributes should also have the updated values of the subscribed attributes"
+ conditions.eventually {
+ asset = assetStorageService.find(asset.getId(), true)
+ assert asset.getAttribute("readWriteTargetTemp").get().getValue().orElse(null) == 99d
+ }
+
+ when: "a linked attribute value is updated"
+ def attributeEvent = new AttributeEvent(asset.id,
+ "readWriteTargetTemp",
+ 19.5)
+ assetProcessingService.sendAttributeEvent(attributeEvent)
+
+ then: "the linked targetTemperature attribute should contain this written value (it should have been written to the target temp attribute and then read back again)"
+ conditions.eventually {
+ asset = assetStorageService.find(asset.getId(), true)
+ assert asset.getAttribute("readWriteTargetTemp", Double.class).flatMap{it.getValue()}.orElse(null) == 19.5d
+ }
+ }
+}