100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Enhancement: MQTT agent wildcard topic support (#1972)
rainerhitzc194f7e8c81b
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.javaindex 07a1518c9..368bfc199 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@@ -397,6 +397,14 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S}}+ public void removeMessageConsumers(String topic) {+ synchronized (topicConsumerMap) {+ Optional.ofNullable(topicConsumerMap.remove(topic)).ifPresent(topicConsumer -> {+ doUnsubscribe(topic);+ });+ }+ }+@Overridepublic void removeAllMessageConsumers() {Set<String> topics = new HashSet<>(topicConsumerMap.keySet());diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.javaindex cb7c702e5..cbde2fc61 100644--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgent.java@@ -23,14 +23,16 @@ import jakarta.persistence.Entity;import org.openremote.agent.protocol.io.IOAgent;import org.openremote.model.asset.agent.Agent;import org.openremote.model.asset.agent.AgentDescriptor;-import org.openremote.model.http.HTTPMethod;import org.openremote.model.value.AttributeDescriptor;import org.openremote.model.value.ValueConstraint;import org.openremote.model.value.ValueDescriptor;import org.openremote.model.value.ValueType;+import java.util.Arrays;import java.util.Optional;+import static org.openremote.model.util.TextUtil.isNullOrEmpty;+@Entitypublic class MQTTAgent extends IOAgent<MQTTAgent, MQTTProtocol, MQTTAgentLink> {@@ -52,6 +54,7 @@ public class MQTTAgent extends IOAgent<MQTTAgent, MQTTProtocol, MQTTAgentLink> {public static final AttributeDescriptor<String> LAST_WILL_TOPIC = new AttributeDescriptor<>("lastWillTopic", ValueType.TEXT);public static final AttributeDescriptor<String> LAST_WILL_PAYLOAD = new AttributeDescriptor<>("lastWillPayload", ValueType.TEXT);public static final AttributeDescriptor<Boolean> LAST_WILL_RETAIN = new AttributeDescriptor<>("lastWillRetain", ValueType.BOOLEAN);+ public static final AttributeDescriptor<String[]> WILDCARD_SUBSCRIPTION_TOPICS = new AttributeDescriptor<>("wildcardSubscriptionTopics", ValueType.TEXT.asArray());public static final AgentDescriptor<MQTTAgent, MQTTProtocol, MQTTAgentLink> DESCRIPTOR = new AgentDescriptor<>(MQTTAgent.class, MQTTProtocol.class, MQTTAgentLink.class@@ -179,4 +182,19 @@ public class MQTTAgent extends IOAgent<MQTTAgent, MQTTProtocol, MQTTAgentLink> {getAttributes().getOrCreate(SUBSCRIBE_QOS).setValue(subscribeQos);return this;}++ public Optional<String[]> getWildcardSubscriptionTopics() {+ return getAttributes().getValue(WILDCARD_SUBSCRIPTION_TOPICS).map(array ->+ Arrays.stream(array)+ .filter(s -> !isNullOrEmpty(s))+ .map(String::trim)+ .distinct()+ .toArray(String[]::new)+ );+ }++ public MQTTAgent setWildcardSubscriptionTopics(String[] wildcards) {+ getAttributes().getOrCreate(WILDCARD_SUBSCRIPTION_TOPICS).setValue(wildcards);+ return this;+ }}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.javaindex 00f165a7f..08ec29fc8 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@@ -20,20 +20,24 @@package org.openremote.agent.protocol.mqtt;import com.hivemq.client.mqtt.datatypes.MqttQos;+import jakarta.validation.constraints.NotNull;import org.apache.http.client.utils.URIBuilder;import org.openremote.model.Container;-import org.openremote.model.protocol.ProtocolUtil;-import org.openremote.model.security.KeyStoreService;-import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.protocol.ProtocolUtil;+import org.openremote.model.protocol.mqtt.Topic;+import org.openremote.model.security.KeyStoreService;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.UniqueIdentifierGenerator;import org.openremote.model.util.ValueUtil;-import javax.net.ssl.*;+import javax.net.ssl.KeyManagerFactory;+import javax.net.ssl.TrustManagerFactory;import java.net.URI;import java.util.*;+import java.util.concurrent.ConcurrentHashMap;import java.util.function.Consumer;import java.util.logging.Logger;@@ -44,7 +48,8 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTAprivate static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, MQTTProtocol.class);public static final String PROTOCOL_DISPLAY_NAME = "MQTT Client";protected final Map<AttributeRef, Consumer<MQTTMessage<String>>> protocolMessageConsumers = new HashMap<>();-+ protected final Map<String, Map<String, Consumer<MQTTMessage<String>>>> wildcardTopicConsumerMap = new HashMap<>();+ protected final Set<Topic> wildcardTopics = new HashSet<>();protected KeyStoreService keyStoreService;protected MQTTProtocol(MQTTAgent agent) {@@ -53,7 +58,11 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA@Overrideprotected void doLinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) throws RuntimeException {- agentLink.getSubscriptionTopic().ifPresent(topic -> {+ agentLink.getSubscriptionTopic().ifPresent(topicStr -> {++ Topic topic = Topic.parse(topicStr);+ Topic matchedWildcardTopic = findMatchingWildcardTopic(topic);+Consumer<String> genericConsumer = ProtocolUtil.createGenericAttributeMessageConsumer(assetId, attribute, agentLink, timerService::getCurrentTimeMillis, this::updateLinkedAttribute);@@ -64,8 +73,13 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTAupdateLinkedAttribute(new AttributeRef(assetId, attribute.getName()), msg.payload);}};- client.addMessageConsumer(topic, Optional.of(agentLink.getQos().orElse(agent.getSubscribeQoS().orElse(0))).map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(MqttQos.AT_MOST_ONCE), messageConsumer);- protocolMessageConsumers.put(new AttributeRef(assetId, attribute.getName()), messageConsumer);++ if (matchedWildcardTopic != null) {+ addWildcardMessageConsumer(matchedWildcardTopic.toString(), topicStr, Optional.of(agentLink.getQos().orElse(agent.getSubscribeQoS().orElse(0))).map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(MqttQos.AT_MOST_ONCE), messageConsumer);+ } else {+ client.addMessageConsumer(topicStr, Optional.of(agentLink.getQos().orElse(agent.getSubscribeQoS().orElse(0))).map(qos -> qos > 2 || qos < 0 ? null : qos).map(MqttQos::fromCode).orElse(MqttQos.AT_MOST_ONCE), messageConsumer);+ protocolMessageConsumers.put(new AttributeRef(assetId, attribute.getName()), messageConsumer);+ }});}@@ -73,16 +87,37 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTAprotected void doStart(Container container) throws Exception {keyStoreService = container.getService(KeyStoreService.class);if (keyStoreService == null) throw new Exception("Couldn't load KeyStoreService");++ agent.getWildcardSubscriptionTopics().ifPresent(wildcardTopicStrs -> {+ try {+ List<Topic> wildcardTopics = Arrays.stream(wildcardTopicStrs).map(Topic::parse)+ .peek(t -> {+ if (!t.hasWildcard()) {+ throw new IllegalArgumentException("Wildcard topic must contain at least one wildcard: " + t);+ }+ }).toList();+ this.wildcardTopics.addAll(wildcardTopics);+ } catch (Exception e) {+ LOG.warning("Failed to parse wildcard topic list: " + e.getMessage());+ throw e;+ }+ });+super.doStart(container);}@Overrideprotected void doUnlinkAttribute(String assetId, Attribute<?> attribute, MQTTAgentLink agentLink) {- agentLink.getSubscriptionTopic().ifPresent(topic -> {+ agentLink.getSubscriptionTopic().ifPresent(topicStr -> {AttributeRef attributeRef = new AttributeRef(assetId, attribute.getName());Consumer<MQTTMessage<String>> messageConsumer = protocolMessageConsumers.remove(attributeRef);if (messageConsumer != null) {- client.removeMessageConsumer(topic, messageConsumer);+ client.removeMessageConsumer(topicStr, messageConsumer);+ } else {+ Topic topic = Topic.parse(topicStr);+ Optional.ofNullable(findMatchingWildcardTopic(topic)).ifPresent(wildcardTopic ->+ removeWildcardMessageConsumer(wildcardTopic.toString(), topicStr)+ );}});}@@ -161,4 +196,47 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTApublic String getProtocolName() {return "MQTT Client";}++ protected void addWildcardMessageConsumer(String wildcardTopic, String topic, MqttQos qos, Consumer<MQTTMessage<String>> messageConsumer) {+ LOG.fine("Adding wildcard message consumer uri=" + client.getClientUri() + ", topic=" + wildcardTopic);++ synchronized (wildcardTopicConsumerMap) {+ wildcardTopicConsumerMap.compute(+ wildcardTopic,+ (t, subConsumers) -> {+ if (subConsumers == null) {+ subConsumers = new ConcurrentHashMap<>();+ Map<String, Consumer<MQTTMessage<String>>> finalSubConsumers = subConsumers;+ client.addMessageConsumer(wildcardTopic, qos, msg ->+ Optional.ofNullable(finalSubConsumers.get(msg.getTopic()))+ .ifPresent(consumer -> consumer.accept(msg)));+ }+ subConsumers.put(topic, messageConsumer);+ return subConsumers;+ });+ }+ }++ protected void removeWildcardMessageConsumer(String wildcardTopic, String topic) {+ LOG.fine("Removing wildcard message consumer uri=" + client.getClientUri() + ", topic=" + wildcardTopic);++ synchronized (wildcardTopicConsumerMap) {+ wildcardTopicConsumerMap.computeIfPresent(+ wildcardTopic,+ (t, subConsumers) -> {+ if (subConsumers.remove(topic) != null && subConsumers.isEmpty()) {+ client.removeMessageConsumers(wildcardTopic);+ return null;+ }+ return subConsumers;+ });+ }+ }++ protected Topic findMatchingWildcardTopic(@NotNull Topic topic) {+ return wildcardTopics.stream()+ .filter(wildcardTopic -> wildcardTopic.matches(topic))+ .findFirst()+ .orElse(null);+ }}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.javaindex f95d528a4..cad228d37 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java@@ -19,7 +19,9 @@*/package org.openremote.manager.mqtt;+import org.apache.activemq.artemis.core.config.WildcardConfiguration;import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration;+import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;import org.apache.activemq.artemis.core.security.CheckType;import org.apache.activemq.artemis.core.security.Role;import org.apache.activemq.artemis.core.server.ActiveMQServer;@@ -31,6 +33,7 @@ import org.openremote.container.security.keycloak.KeycloakIdentityProvider;import org.openremote.manager.security.AuthorisationService;import org.openremote.manager.security.MultiTenantJaasCallbackHandler;import org.openremote.manager.security.RemotingConnectionPrincipal;+import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.syslog.SyslogCategory;import javax.security.auth.Subject;@@ -74,6 +77,10 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {this.config = configuration;}+ protected static Topic fromAddress(String address, WildcardConfiguration wildcardConfiguration) throws IllegalArgumentException {+ return Topic.parse(MQTTUtil.getMqttTopicFromCoreAddress(address, wildcardConfiguration));+ }+@Overridepublic Subject authenticate(String user, String password, RemotingConnection remotingConnection, String securityDomain) {try {@@ -155,7 +162,7 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {try {// Get MQTT topic from address- topic = Topic.fromAddress(address, brokerService.getWildcardConfiguration());+ topic = fromAddress(address, brokerService.getWildcardConfiguration());} catch (IllegalArgumentException e) {LOG.log(Level.FINE, "Invalid topic provided by client '" + address, e);return false;diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.javaindex 1998131d6..0780d5413 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java@@ -38,6 +38,7 @@ import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeMap;import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.AttributePredicate;import org.openremote.model.query.filter.NameValuePredicate;diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.javaindex ab19a64f6..24e26ec95 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -36,6 +36,7 @@ import org.openremote.model.asset.UserAssetLink;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.Event;import org.openremote.model.event.shared.EventSubscription;+import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.ValueUtil;@@ -47,7 +48,6 @@ 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.asset.AssetProcessingService.ATTRIBUTE_EVENT_PROCESSOR;import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;@@ -156,18 +156,18 @@ public class DefaultMQTTHandler extends MQTTHandler {}if (isAssetTopic) {- if (topic.getTokens().size() < 4 || topic.getTokens().size() > 5) {+ if (topic.getTokens().length < 4 || topic.getTokens().length > 5) {LOG.finest("Asset subscribe token count should be 4 or 5: topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));return false;}- if (topic.getTokens().size() == 4) {+ if (topic.getTokens().length == 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 + ", " + mqttBrokerService.connectionToString(connection));return false;}- } else if (topic.getTokens().size() == 5) {+ } else if (topic.getTokens().length == 5) {if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 3))) {LOG.fine("Asset subscribe forth token must be an asset ID: topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));return false;@@ -180,11 +180,11 @@ public class DefaultMQTTHandler extends MQTTHandler {}} else {// Attribute topic- if (topic.getTokens().size() < 5 || topic.getTokens().size() > 6) {+ if (topic.getTokens().length < 5 || topic.getTokens().length > 6) {LOG.fine("Attribute subscribe token count should be 5 or 6: topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));return false;}- if (topic.getTokens().size() == 5) {+ if (topic.getTokens().length == 5) {if (TOKEN_MULTI_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3))) {LOG.fine("Attribute subscribe multi level wildcard must be last token: topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));return false;@@ -195,7 +195,7 @@ public class DefaultMQTTHandler extends MQTTHandler {LOG.fine("Attribute subscribe fifth token must be an asset ID or a wildcard: topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));return false;}- } else if (topic.getTokens().size() == 6) {+ } else if (topic.getTokens().length == 6) {if (!Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {LOG.fine("Attribute subscribe fifth token must be an asset ID: topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));return false;@@ -225,7 +225,7 @@ public class DefaultMQTTHandler extends MQTTHandler {return false;}- String subscriptionId = topic.getString() + authContext.getUserId();+ String subscriptionId = topic + authContext.getUserId();// Add the event subscription to the intermediary cache, override any existing entryeventSubscriptionCache.put(subscriptionId, subscription);@@ -250,7 +250,7 @@ public class DefaultMQTTHandler extends MQTTHandler {}if (isAttributeValueWriteTopic(topic) || isAttributeWriteTopic(topic)) {- if (topic.getTokens().size() != 5 || !Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {+ if (topic.getTokens().length != 5 || !Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {LOG.finer("Invalid publish topic: topic=" + topic + ", connection=" + connectionToString(connection));return false;}@@ -262,7 +262,7 @@ public class DefaultMQTTHandler extends MQTTHandler {// Check cacheConcurrentHashSet<String> act = authorizationCache.getIfPresent(cacheKey);- if (act != null && act.contains(topic.getString())) {+ if (act != null && act.contains(topic.toString())) {return true;}@@ -284,7 +284,7 @@ public class DefaultMQTTHandler extends MQTTHandler {authorizationCache.put(cacheKey, set);}}- set.add(topic.getString());+ set.add(topic.toString());return true;}@@ -294,7 +294,7 @@ public class DefaultMQTTHandler extends MQTTHandler {public void onSubscribe(RemotingConnection connection, Topic topic) {AuthContext authContext = getAuthContextFromConnection(connection).get();- String subscriptionId = topic.getString() + authContext.getUserId();+ String subscriptionId = topic + authContext.getUserId();// Get the authorized event subscription from the intermediary cacheEventSubscription<?> subscription = eventSubscriptionCache.getIfPresent(subscriptionId);@@ -312,7 +312,7 @@ public class DefaultMQTTHandler extends MQTTHandler {synchronized (sessionSubscriptionConsumers) {// Create subscription consumer and track it for future removal requestsMap<String, Consumer<? extends Event>> subscriptionConsumers = sessionSubscriptionConsumers.computeIfAbsent(sessionKey, (s) -> new HashMap<>());- subscriptionConsumers.put(topic.getString(), consumer);+ subscriptionConsumers.put(topic.toString(), consumer);clientEventService.addSubscription(subscription, consumer);LOG.finest(() -> "Client event subscription created for topic '" + topic + "': " + connectionToString(connection));}@@ -347,7 +347,7 @@ public class DefaultMQTTHandler extends MQTTHandler {@Overridepublic void onPublish(RemotingConnection connection, Topic topic, ByteBuf body) {- List<String> topicTokens = topic.getTokens();+ String[] topicTokens = topic.getTokens();String payloadContent = body.toString(StandardCharsets.UTF_8);AttributeEvent attributeEvent;@@ -392,9 +392,9 @@ public class DefaultMQTTHandler extends MQTTHandler {}}- protected static AttributeEvent buildAttributeEvent(List<String> topicTokens, Object value, Long timestamp) {- String attributeName = topicTokens.get(3);- String assetId = topicTokens.get(4);+ protected static AttributeEvent buildAttributeEvent(String[] topicTokens, Object value, Long timestamp) {+ String attributeName = topicTokens[3];+ String assetId = topicTokens[4];return new AttributeEvent(assetId, attributeName, value, timestamp).setSource(DefaultMQTTHandler.class.getSimpleName());}@@ -409,7 +409,7 @@ public class DefaultMQTTHandler extends MQTTHandler {String firstTokenStr = topicTokenIndexToString(topic, 3);if (isAssetTopic) {- if (topic.getTokens().size() == 4) {+ if (topic.getTokens().length == 4) {if (TOKEN_MULTI_LEVEL_WILDCARD.equals(firstTokenStr)) {//realm/clientId/asset/#// No filtering required@@ -420,7 +420,7 @@ public class DefaultMQTTHandler extends MQTTHandler {//realm/clientId/asset/{assetId}assetIds.add(firstTokenStr);}- } else if (topic.getTokens().size() == 5) {+ } else if (topic.getTokens().length == 5) {String secondTokenStr = topicTokenIndexToString(topic, 4);if (TOKEN_MULTI_LEVEL_WILDCARD.equals(secondTokenStr)) {@@ -437,7 +437,7 @@ public class DefaultMQTTHandler extends MQTTHandler {if (!TOKEN_SINGLE_LEVEL_WILDCARD.equals(firstTokenStr)) {attributeNames.add(firstTokenStr);}- if (topic.getTokens().size() == 5) {+ if (topic.getTokens().length == 5) {String secondTokenStr = topicTokenIndexToString(topic, 4);//realm/clientId/attribute/{attributeName|+}/{assetId|+|*}if (TOKEN_MULTI_LEVEL_WILDCARD.equals(secondTokenStr)) {@@ -450,7 +450,7 @@ public class DefaultMQTTHandler extends MQTTHandler {//realm/clientId/attribute/+/{assetId}assetIds.add(secondTokenStr);}- } else if (topic.getTokens().size() == 6) {+ } else if (topic.getTokens().length == 6) {//realm/clientId/attribute/{attributeName|+}/{assetId}/{+|*}String thirdTokenStr = topicTokenIndexToString(topic, 5);diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.javaindex ff12aa5de..fc72929a4 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java@@ -75,6 +75,7 @@ import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.UserAssetLink;+import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.security.User;import org.openremote.model.util.Debouncer;import org.openremote.model.util.TextUtil;diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.javaindex b3809420b..f816e480e 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java@@ -44,6 +44,7 @@ import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.Container;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.UserAssetLink;+import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.util.ValueUtil;import javax.security.auth.Subject;@@ -358,11 +359,11 @@ public abstract class MQTTHandler {}public static boolean topicTokenCountGreaterThan(Topic topic, int size) {- return topic.getTokens() != null && topic.getTokens().size() > size;+ return topic.getTokens() != null && topic.getTokens().length > size;}public static String topicTokenIndexToString(Topic topic, int tokenNumber) {- return topicTokenCountGreaterThan(topic, tokenNumber) ? topic.getTokens().get(tokenNumber) : null;+ return topicTokenCountGreaterThan(topic, tokenNumber) ? topic.getTokens()[tokenNumber] : null;}protected static Subject getSubjectFromConnection(RemotingConnection connection) {diff --git a/manager/src/main/java/org/openremote/manager/mqtt/Topic.java b/manager/src/main/java/org/openremote/manager/mqtt/Topic.javadeleted file mode 100644index 793097758..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/Topic.java+++ /dev/null@@ -1,79 +0,0 @@-/*- * Copyright 2022, OpenRemote Inc.- *- * See the CONTRIBUTORS.txt file in the distribution for a- * full listing of individual contributors.- *- * This program is free software: you can redistribute it and/or modify- * it under the terms of the GNU Affero General Public License as- * published by the Free Software Foundation, either version 3 of the- * License, or (at your option) any later version.- *- * This program is distributed in the hope that it will be useful,- * but WITHOUT ANY WARRANTY; without even the implied warranty of- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- * GNU Affero General Public License for more details.- *- * You should have received a copy of the GNU Affero General Public License- * along with this program. If not, see <http://www.gnu.org/licenses/>.- */-package org.openremote.manager.mqtt;--import org.apache.activemq.artemis.core.config.WildcardConfiguration;-import org.apache.activemq.artemis.core.protocol.mqtt.MQTTUtil;-import org.openremote.model.util.TextUtil;--import java.util.Arrays;-import java.util.Collections;-import java.util.List;--public class Topic {-- public static final String SEPARATOR = "/";- public static final String SINGLE_LEVEL_TOKEN = "+";- public static final String MULTI_LEVEL_TOKEN = "#";- public static final Topic EMPTY_TOPIC = new Topic(SEPARATOR, Collections.emptyList());-- protected String topic;- protected List<String> tokens;-- public static Topic fromAddress(String address, WildcardConfiguration wildcardConfiguration) throws IllegalArgumentException {- return Topic.parse(MQTTUtil.getMqttTopicFromCoreAddress(address, wildcardConfiguration));- }-- public static Topic parse(String topic) throws IllegalArgumentException {- if (TextUtil.isNullOrEmpty(topic) || SEPARATOR.equals(topic)) {- return EMPTY_TOPIC;- }-- int multiLevelPos = topic.indexOf(MULTI_LEVEL_TOKEN);- if (multiLevelPos >= 0 && multiLevelPos < topic.length() - 1) {- throw new IllegalArgumentException("Multilevel wildcard token must be at the end of the topic");- }-- List<String> tokens = Arrays.asList(topic.split(SEPARATOR));- return new Topic(topic, tokens);- }-- protected Topic(String topic, List<String> tokens) {- this.topic = topic;- this.tokens = tokens;- }-- public String getString() {- return topic;- }-- public boolean hasWildcard() {- return topic.contains(SINGLE_LEVEL_TOKEN) || topic.contains(MULTI_LEVEL_TOKEN);- }-- public List<String> getTokens() {- return tokens;- }-- @Override- public String toString() {- return topic;- }-}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.javaindex d06298702..7ba7dc304 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java@@ -39,6 +39,7 @@ import org.openremote.model.Container;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.asset.UserAssetLink;+import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.provisioning.*;import org.openremote.model.security.ClientRole;import org.openremote.model.security.User;@@ -188,7 +189,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {@Overridepublic boolean topicMatches(Topic topic) {return isProvisioningTopic(topic)- && topic.getTokens().size() == 3+ && topic.getTokens().length == 3&& (isRequestTopic(topic) || isResponseTopic(topic));}@@ -210,7 +211,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {if (allowed) {// Only allow if there is no existing subscription for this topic- RemotingConnection existingConnection = responseSubscribedConnections.get(topic.getString());+ RemotingConnection existingConnection = responseSubscribedConnections.get(topic.toString());if (existingConnection != null) {LOG.warning("Subscription already exists possible eavesdropping");allowed = false;@@ -222,12 +223,12 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {@Overridepublic void onSubscribe(RemotingConnection connection, Topic topic) {- responseSubscribedConnections.put(topic.getString(), connection);+ responseSubscribedConnections.put(topic.toString(), connection);}@Overridepublic void onUnsubscribe(RemotingConnection connection, Topic topic) {- responseSubscribedConnections.remove(topic.getString());+ responseSubscribedConnections.remove(topic.toString());}@Overridediff --git a/model/src/main/java/org/openremote/model/protocol/mqtt/Topic.java b/model/src/main/java/org/openremote/model/protocol/mqtt/Topic.javanew file mode 100644index 000000000..76c345d4c--- /dev/null+++ b/model/src/main/java/org/openremote/model/protocol/mqtt/Topic.java@@ -0,0 +1,125 @@+/*+ * Copyright 2022, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.protocol.mqtt;++import jakarta.validation.constraints.NotNull;+import org.openremote.model.util.TextUtil;++import java.util.Objects;++public class Topic {++ public static final String SEPARATOR = "/";+ public static final String SINGLE_LEVEL_TOKEN = "+";+ public static final String MULTI_LEVEL_TOKEN = "#";+ public static final Topic EMPTY_TOPIC = new Topic(SEPARATOR, new String[0]);++ protected String topicStr;+ protected String[] tokens;++ public static Topic parse(String topic) throws IllegalArgumentException {+ if (TextUtil.isNullOrEmpty(topic) || SEPARATOR.equals(topic)) {+ return EMPTY_TOPIC;+ }++ int multiLevelPos = topic.indexOf(MULTI_LEVEL_TOKEN);+ if (multiLevelPos >= 0 && multiLevelPos < topic.length() - 1) {+ throw new IllegalArgumentException("Multilevel wildcard token must be at the end of the topic");+ }++ String[] tokens = topic.split(SEPARATOR);+ return new Topic(topic, tokens);+ }++ protected Topic(String topicStr, String[] tokens) {+ this.topicStr = topicStr;+ this.tokens = tokens;+ }++ public boolean hasWildcard() {+ return topicStr.contains(SINGLE_LEVEL_TOKEN) || topicStr.contains(MULTI_LEVEL_TOKEN);+ }++ public String[] getTokens() {+ return tokens;+ }++ @Override+ public boolean equals(Object o) {+ if (o == null || getClass() != o.getClass()) return false;+ Topic topic = (Topic) o;+ return Objects.equals(topicStr, topic.topicStr);+ }++ @Override+ public int hashCode() {+ return Objects.hashCode(topicStr);+ }++ @Override+ public String toString() {+ return topicStr;+ }++ /**+ * Determines if the specified topic matches this topic; can be a simple equality match or if this topic contains+ * wildcards then determines if the specified topic is covered by the wildcards+ */+ public boolean matches(String topic) {+ return matches(Topic.parse(topic));+ }++ /**+ * Determines if the specified topic matches this topic; can be a simple equality match or if this topic contains+ * wildcards then determines if the specified topic is covered by the wildcards+ */+ public boolean matches(@NotNull Topic topic) {+ if (topic == null || topic.hasWildcard()) {+ throw new IllegalArgumentException("Topic cannot be null or contain wildcards");+ }++ if (Objects.equals(this.topicStr, topic.topicStr)) {+ return true;+ }++ int i = 0;+ for (; i < tokens.length; i++) {+ String w = tokens[i];++ if (w.equals("#")) {+ return true;+ }++ if (i >= topic.tokens.length) {+ return false;+ }++ if (w.equals("+")) {+ continue;+ }++ if (!w.equals(topic.tokens[i])) {+ return false;+ }+ }++ return i == topic.tokens.length;+ }+}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.groovyindex 2b374c643..97f28f9a3 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@@ -521,6 +521,144 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTr}}+ def "Check MQTT client protocol wildcard subscriptions"() {++ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)++ when: "the container starts"+ def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def agentService = container.getService(AgentService.class)+ def brokerService = container.getService(MQTTBrokerService.class)+ def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def mqttHost = brokerService.host+ def mqttPort = brokerService.port+ def mqttAgentClientId = UniqueIdentifierGenerator.generateId()++ and: "test assets are created"+ def testThing1 = new ThingAsset("Test thing 1")+ .setRealm(keycloakTestSetup.realmBuilding.name)+ .addOrReplaceAttributes(+ new Attribute<Object>("temperature", NUMBER)+ )+ .addOrReplaceAttributes(+ new Attribute<Object>("humidity", NUMBER)+ )+ def testThing2 = new ThingAsset("Test thing 2")+ .setRealm(keycloakTestSetup.realmBuilding.name)+ .addOrReplaceAttributes(+ new Attribute<Object>("pressure", NUMBER)+ )+ def testThing3 = new ThingAsset("Test thing 3")+ .setRealm(keycloakTestSetup.realmBuilding.name)+ .addOrReplaceAttributes(+ new Attribute<Object>("uvIndex", NUMBER)+ )++ and: "the test assets are added to the asset service"+ testThing1 = assetStorageService.merge(testThing1)+ testThing2 = assetStorageService.merge(testThing2)+ testThing3 = assetStorageService.merge(testThing3)++ and: "a MQTT agent is created"+ def wildcardTopic1 = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/+/${testThing1.id}".toString()+ def wildcardTopic2 = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/+/${testThing2.id}".toString()+ def agent = new MQTTAgent("Test agent")+ .setRealm(MASTER_REALM)+ .setClientId(mqttAgentClientId)+ .setHost(mqttHost)+ .setPort(mqttPort)+ .setUsernamePassword(new UsernamePassword(keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser.username, keycloakTestSetup.serviceUser.secret))+ .setWildcardSubscriptionTopics([wildcardTopic1, wildcardTopic2] as String[])++ 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+ }++ and: "there should be wildcard topics"+ conditions.eventually {+ def protocol = (MQTTProtocol)agentService.getProtocolInstance(agent.id)+ assert protocol.wildcardTopics.size() == 2+ assert protocol.wildcardTopics.any {it.toString() == wildcardTopic1}+ assert protocol.wildcardTopics.any {it.toString() == wildcardTopic2}+ }++ when: "an asset is created with attributes linked to the agent"+ def temperatureTopic = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/temperature/${testThing1.id}".toString()+ def humidityTopic = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/humidity/${testThing1.id}".toString()+ def pressureTopic = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/pressure/${testThing2.id}".toString()+ def uvIndexTopic = "${keycloakTestSetup.realmBuilding.name}/$mqttAgentClientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/uvIndex/${testThing3.id}".toString()+ def asset = new ThingAsset("Test Asset")+ .setParent(agent)+ .addOrReplaceAttributes(+ new Attribute<>("temperature", NUMBER)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic(temperatureTopic)+ )),+ new Attribute<>("humidity", NUMBER)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic(humidityTopic)+ )),+ new Attribute<>("pressure", NUMBER)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic(pressureTopic)+ )),+ new Attribute<>("uvIndex", NUMBER)+ .addMeta(+ new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)+ .setSubscriptionTopic(uvIndexTopic)+ ))+ )++ and: "the asset is merged into the asset service"+ asset = assetStorageService.merge(asset)++ then: "there should be wildcard subscriptions and regular subscriptions"+ conditions.eventually {+ def protocol = (MQTTProtocol)agentService.getProtocolInstance(agent.id)+ def client = protocol.@client+ assert client.topicConsumerMap.containsKey(wildcardTopic1)+ assert client.topicConsumerMap.containsKey(wildcardTopic2)+ assert client.topicConsumerMap.containsKey(uvIndexTopic)+ assert protocol.wildcardTopicConsumerMap.size() == 2+ assert protocol.wildcardTopicConsumerMap.get(wildcardTopic1).size() == 2+ assert protocol.wildcardTopicConsumerMap.get(wildcardTopic1).containsKey(temperatureTopic)+ assert protocol.wildcardTopicConsumerMap.get(wildcardTopic1).containsKey(humidityTopic)+ assert protocol.wildcardTopicConsumerMap.get(wildcardTopic2).size() == 1+ assert protocol.wildcardTopicConsumerMap.get(wildcardTopic2).containsKey(pressureTopic)+ }++ when: "mqtt data is published"+ def attributeEvent = new AttributeEvent(testThing1.id, "temperature", 19.5)+ assetProcessingService.sendAttributeEvent(attributeEvent)+ attributeEvent = new AttributeEvent(testThing1.id, "humidity", 85.0)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ attributeEvent = new AttributeEvent(testThing2.id, "pressure", 1013.25)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ attributeEvent = new AttributeEvent(testThing3.id, "uvIndex", 3.0)+ assetProcessingService.sendAttributeEvent(attributeEvent)++ then: "asset attribute values should be updated"+ conditions.eventually {+ asset = assetStorageService.find(asset.id, true)+ assert asset.getAttribute("temperature").flatMap { it.value }.map { it == 19.5 }.orElse(false)+ assert asset.getAttribute("humidity").flatMap { it.value }.map { it == 85 }.orElse(false)+ assert asset.getAttribute("pressure").flatMap { it.value }.map { it == 1013.25 }.orElse(false)+ assert asset.getAttribute("uvIndex").flatMap { it.value }.map { it == 3.0 }.orElse(false)+ }+ }+/*** This test is not fully done right now, as a non-internet-requiring test would require mTLS functionality within the* OpenRemote broker, something that will eventually be done.