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
MQTT broker updates
Rich Turner7504f39fe616
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.javaindex 3a118fc66..f4de426e7 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java@@ -70,7 +70,7 @@ public class MqttBrokerService implements ContainerService {public static final String ASSET_TOPIC = "asset";public static final String ATTRIBUTE_TOPIC = "attribute";- public static final String ATTRIBUTE_VALUE_TOPIC = "value";+ public static final String ATTRIBUTE_VALUE_TOPIC = "attributevalue";public static final String SINGLE_LEVEL_WILDCARD = "+";public static final String MULTI_LEVEL_WILDCARD = "#";@@ -182,7 +182,7 @@ public class MqttBrokerService implements ContainerService {}public static boolean isAttributeTopic(List<String> tokens) {- return tokens.get(0).equals(ATTRIBUTE_TOPIC);+ return tokens.get(0).equals(ATTRIBUTE_TOPIC) || tokens.get(0).equals(ATTRIBUTE_VALUE_TOPIC);}public static boolean isAssetTopic(List<String> tokens) {@@ -195,56 +195,101 @@ public class MqttBrokerService implements ContainerService {}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> attributeNames = new ArrayList<>();String assetId = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(1)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(1)) ? null : topicTokens.get(1);+ int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);+ int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);- if (isAttributeTopic) {- int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);+ if (!isAssetTopic && !isAttributeTopic) {+ return null;+ }- if (assetId != null) {- int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);- if (multiLevelIndex == -1) {- if (singleLevelIndex == -1) { //attribute/assetId/- assetIds.add(assetId);- } else {- parentIds.add(assetId);- }- if (topicTokens.size() > 2) { //attribute/assetId/attributeName- if (singleLevelIndex == -1) { //attribute/assetId/attributeName- attributeNames.add(topicTokens.get(2));- } else if (singleLevelIndex == 2 && topicTokens.size() > 3 && !topicTokens.get(3).equals(SINGLE_LEVEL_WILDCARD)) { // else attribute/assetId/+/attributeName- attributeNames.add(topicTokens.get(3));- } // else attribute/assetId/+ which should return all attributes- }- } else if (multiLevelIndex == 2) { //attribute/assetId/#+ if (topicTokens.size() == 2) {+ if (multiLevelIndex == 1) {+ //.../#+ // No asset filtering required+ } else if (singleLevelIndex == 1) {+ //.../++ parentIds.add(null);+ } else {+ //.../assetId+ assetIds.add(assetId);+ }+ } else if (topicTokens.size() == 3) {+ if (isAssetTopic) {+ if (multiLevelIndex == 2) {+ //asset/assetId/#+ // TODO: Implement this once asset filter supports it+ parentIds.add(assetId);+ } else if (singleLevelIndex == 2) {+ //asset/assetId/+parentIds.add(assetId);+ } else {+ return null;}} else {- if (singleLevelIndex == 1) { //attribute/+- if (topicTokens.size() > 2) { //attribute/+/attributeName- attributeNames.add(topicTokens.get(2));+ if (assetId != null) {+ if (multiLevelIndex == 2) {+ //attribute/assetId/#+ // TODO: Implement this once asset filter supports it+ parentIds.add(assetId);+ } else if (singleLevelIndex == 2) {+ //attribute/assetId/++ parentIds.add(assetId);+ } else {+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)) ? null : topicTokens.get(2);+ if (attributeName != null) {+ //attribute/assetId/attributeName+ attributeNames.add(topicTokens.get(2));+ } else {+ return null;+ }+ }+ } else {+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(2)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(2)) ? null : topicTokens.get(2);+ if (attributeName != null) {+ attributeNames.add(attributeName);+ if (multiLevelIndex == 2) {+ //attribute/#/attributeName+ // No asset filtering required+ } else if (singleLevelIndex == 2) {+ //attribute/+/attributeName+ parentIds.add(null);+ } else {+ return null;+ }+ } else {+ return null;}}}-- } else if (assetId != null) {- int multiLevelIndex = topicTokens.indexOf(MULTI_LEVEL_WILDCARD);- int singleLevelIndex = topicTokens.indexOf(SINGLE_LEVEL_WILDCARD);-- if (multiLevelIndex == -1) {- assetIds.add(assetId);- if (singleLevelIndex == 2) { //asset/assetId/+- parentIds.add(assetId);- } else if (singleLevelIndex == -1) { //asset/assetId- assetIds.add(assetId);+ } else if (topicTokens.size() == 4) {+ if (isAssetTopic || assetId == null) {+ return null;+ }+ String attributeName = SINGLE_LEVEL_WILDCARD.equals(topicTokens.get(3)) || MULTI_LEVEL_WILDCARD.equals(topicTokens.get(3)) ? null : topicTokens.get(3);+ if (attributeName != null) {+ attributeNames.add(attributeName);+ if (multiLevelIndex == 2) {+ //attribute/#/attributeName+ // No asset filtering required+ } else if (singleLevelIndex == 2) {+ //attribute/+/attributeName+ parentIds.add(null);+ } else {+ return null;}- } else if (multiLevelIndex == 2) { //asset/assetId/#- parentIds.add(assetId);+ } else {+ return null;}+ } else {+ return null;}AssetFilter<?> assetFilter = new AssetFilter<>().setRealm(realm);diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.javaindex b93f0770a..da7e3b160 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java@@ -77,12 +77,12 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {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.INFO, "Couldn't verify token: " + connection, e);+ LOG.log(Level.FINE, "Couldn't verify token: " + connection, e);return false;}if (authContext == null) {- LOG.info("Failed to get auth context for connection: " + connection);+ LOG.warning("Failed to get auth context for connection: " + connection);return false;}@@ -94,7 +94,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());if (topicTokens.size() < 2) {- LOG.info("Topic may not be empty and should have the following format: //asset/... or //attribute/...");+ LOG.fine("Topic must contain at least two tokens '" + topic + "': " + connection);return false;}@@ -102,7 +102,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);if (!isAssetTopic && !isAttributeTopic) {- LOG.info("Topic should start with 'asset' or 'attribute': " + connection);+ LOG.fine("Topic starts with invalid value '" + topic + "': " + connection);return false;}@@ -112,7 +112,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {Asset<?> asset = assetStorageService.find(assetId);if (asset == null) {- LOG.info("Asset not found for topic '" + topic + "': " + connection);+ LOG.fine("Asset not found for topic '" + topic + "': " + connection);return false;}@@ -121,7 +121,7 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {String attributeName = topicTokens.get(2);if (!asset.hasAttribute(attributeName)) {- LOG.info("Asset attribute not found for topic '" + topic + "': " + connection);+ LOG.fine("Asset attribute not found for topic '" + topic + "': " + connection);return false;}}@@ -130,41 +130,33 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {if (isWrite) {if (topicTokens.contains(SINGLE_LEVEL_WILDCARD) || topicTokens.contains(MULTI_LEVEL_WILDCARD)) {- LOG.info("Write does not support wildcards in topic: " + connection);+ LOG.fine("Write does not support wildcards '" + topic + "': " + connection);return false;}- // Check we have the correct write role- if (isAssetTopic && !authContext.hasResourceRole(ClientRole.WRITE_ASSETS.getValue(), username)) {- LOG.info("Write request to asset topic but user doesn't have required role: " + connection);- return false;- } else if (isAttributeTopic && !authContext.hasResourceRole(ClientRole.WRITE_ATTRIBUTES.getValue(), username)) {- LOG.info("Write request to attribute topic but user doesn't have required role: " + connection);+ if (isAssetTopic) {+ LOG.fine("Cannot write assets '" + topic + "': " + connection);return false;}- // Check if this is a restricted user- boolean isRestrictedUser = identityProvider.isRestrictedUser(authContext.getUserId());- if (isAssetTopic) {- if (topicTokens.size() > 1) {- LOG.info("Write request to asset topic shouldn't contain any topic parts: " + connection);- return false;- }+ if (topicTokens.get(0).equals(ATTRIBUTE_VALUE_TOPIC) && topicTokens.size() != 3) {+ LOG.info("Write request to attribute value topic should contain asset ID and attribute name only, topic '" + topic + "': " + connection);+ return false;+ }- if (isRestrictedUser) {- LOG.info("Restricted users cannot write assets: " + connection);- return false;- }- } else {- // Must be of form //attribute/assetId/attributeName or //attribute/assetId/attributeName/value- if (!(topicTokens.size() == 3 || (topicTokens.size() == 4 && ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(3))))) {- LOG.info("Write request to attribute topic should contain asset ID and attribute name only: " + connection);- return false;- }+ if (topicTokens.get(0).equals(ATTRIBUTE_TOPIC) && topicTokens.size() != 1) {+ LOG.info("Write request to attribute topic should not contain any other tokens, 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+ // 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 contextdiff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.javaindex 14dad1789..5c0e51bc1 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java@@ -19,7 +19,6 @@*/package org.openremote.manager.mqtt;-import com.fasterxml.jackson.databind.JsonNode;import com.google.api.client.util.Charsets;import io.moquette.broker.subscriptions.Token;import io.moquette.broker.subscriptions.Topic;@@ -37,7 +36,6 @@ 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.attribute.AttributeRef;import org.openremote.model.event.shared.CancelEventSubscription;import org.openremote.model.event.shared.EventSubscription;import org.openremote.model.event.shared.SharedEvent;@@ -103,7 +101,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_OPEN, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);- LOG.info("Connected: " + connection);+ LOG.fine("Connected: " + connection);// No topic info here (not sure what willTopic is??) so just notify all custom handlersbrokerService.customHandlers.forEach(customHandler -> customHandler.onConnect(connection, msg));@@ -117,7 +115,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_CLOSE, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);- LOG.info("Connection closed: " + connection);+ LOG.fine("Connection closed: " + connection);// No topic info here so just notify all custom handlersbrokerService.customHandlers.forEach(customHandler -> customHandler.onDisconnect(connection, msg));@@ -132,7 +130,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);- LOG.info("Connection lost: " + connection);+ LOG.fine("Connection lost: " + connection);// No topic info here so just notify all custom handlersbrokerService.customHandlers.forEach(customHandler -> customHandler.onConnectionLost(connection, msg));@@ -164,14 +162,14 @@ public class ORInterceptHandler extends AbstractInterceptHandler {List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());boolean isAttributeTopic = MqttBrokerService.isAttributeTopic(topicTokens);boolean isAssetTopic = MqttBrokerService.isAssetTopic(topicTokens);- boolean isValueSubscription = isAttributeTopic && topic.toString().endsWith(ATTRIBUTE_VALUE_TOPIC);+ boolean isValueSubscription = ATTRIBUTE_VALUE_TOPIC.equals(topicTokens.get(0));String subscriptionId = msg.getTopicFilter(); // Use topic as subscription IDAssetFilter filter = buildAssetFilter(connection, topicTokens);Class subscriptionClass = isAssetTopic ? AssetEvent.class : AttributeEvent.class;if (filter == null) {- LOG.info("Invalid event filter generated for topic '" + topic + "': " + connection);+ LOG.fine("Invalid event filter generated for topic '" + topic + "': " + connection);return;}@@ -197,7 +195,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());if (connection == null) {- LOG.info("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());+ LOG.warning("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());return;}@@ -230,7 +228,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {MqttConnection connection = sessionIdConnectionMap.get(msg.getClientID());if (connection == null) {- LOG.info("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());+ LOG.warning("No connection found: realm=" + realm + ", username=" + username + ", clientID=" + msg.getClientID());return;}@@ -243,47 +241,26 @@ public class ORInterceptHandler extends AbstractInterceptHandler {}List<String> topicTokens = topic.getTokens().stream().map(Token::toString).collect(Collectors.toList());+ boolean isValueWrite = topicTokens.get(0).equals(ATTRIBUTE_VALUE_TOPIC);+ String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);+ AttributeEvent attributeEvent = null;- if (topicTokens.size() > 1) {+ if (isValueWrite) {String assetId = topicTokens.get(1);- AttributeRef attributeRef = null;- if (topicTokens.size() > 2) { //attribute specific- attributeRef = new AttributeRef(assetId, topicTokens.get(2));- }- if (attributeRef == null) {- String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);- Values.parse(payloadContent).flatMap(Values::asJSONObject).ifPresent(objectValue -> {- Map<String, Object> headers = prepareHeaders(connection);- Map.Entry<String, JsonNode> firstElem = objectValue.fields().next();- AttributeEvent attributeEvent = new AttributeEvent(assetId, firstElem.getKey(), firstElem.getValue());- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);- });- } else {- String payloadContent = msg.getPayload().toString(StandardCharsets.UTF_8);- Object value = null;- if (Character.isLetter(payloadContent.charAt(0))) {- if (payloadContent.equals(Boolean.TRUE.toString())) {- value = true;- }- if (payloadContent.equals(Boolean.FALSE.toString())) {- value = false;- }- payloadContent = '"' + payloadContent + '"';- }- if (value == null) {- value = Values.parse(payloadContent);- }- if (value == null) {- value = payloadContent;- }-- Map<String, Object> headers = prepareHeaders(connection);- AttributeEvent attributeEvent = new AttributeEvent(assetId, attributeRef.getName(), value);- messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);- }+ String attributeName = topicTokens.get(2);+ Object value = Values.parse(payloadContent).orElse(null);+ attributeEvent = new AttributeEvent(assetId, attributeName, value);} else {- LOG.warning("Couldn't process message for topic: " + topic);+ attributeEvent = Values.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) {@@ -298,7 +275,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {headers.put(Constants.AUTH_CONTEXT, new AccessTokenAuthContext(connection.realm, accessToken));}} catch (VerificationException e) {- LOG.log(Level.WARNING, "Couldn't verify token", e);+ LOG.log(Level.FINE, "Couldn't verify token", e);}return headers;}diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 15aa9cb8f..f854e8cb0 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -26,6 +26,7 @@ import static org.openremote.container.util.MapAccess.getStringimport static org.openremote.manager.mqtt.MqttBrokerService.*import static org.openremote.model.value.ValueType.TEXT+// TODO: Change to use MQTT client once implementedclass MqttBrokerTest extends Specification implements ManagerContainerTrait {def "Mqtt broker event test"() {@@ -244,8 +245,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client publishes to an asset attribute"- topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/lights"- payload = Values.asJSON(Values.createJsonObject().put("lights", false)).orElse(null)+ topic = "attributevalue/" + managerTestSetup.apartment1HallwayId + "/lights"+ payload = Values.asJSON(false).orElse(null)remainingLength = 2 + topic.size() + payload.length()//PUBLISH@@ -384,7 +385,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset attribute value"- topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor/value"+ topic = "attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -424,7 +425,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset attribute value"- topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor/value"+ topic = "attributevalue/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size()client