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 improvements (#414)
Michael Rademaker5a72eb74199c
diff --git a/docker-compose.yml b/docker-compose.ymlindex 7609016d8..b36b41138 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -13,7 +13,7 @@ version: '2.4'services:proxy:- image: openremote/proxy:${PROXY_VERSION:-2.1.2.1}+ image: openremote/proxy:${PROXY_VERSION:-2.1.2.2}restart: alwaysdepends_on:manager:diff --git a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.javaindex 2e591c9a4..7cb8eb489 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java@@ -20,6 +20,7 @@package org.openremote.manager.mqtt;import com.fasterxml.jackson.databind.JsonNode;+import io.moquette.broker.subscriptions.Token;import io.moquette.interception.AbstractInterceptHandler;import io.moquette.interception.messages.*;import org.keycloak.adapters.rotation.AdapterTokenVerifier;@@ -32,23 +33,26 @@ 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.Asset;+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;import org.openremote.model.util.TextUtil;import org.openremote.model.value.Values;import java.nio.charset.Charset;import java.nio.charset.StandardCharsets;+import java.util.Arrays;import java.util.HashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;-import static org.openremote.manager.mqtt.MqttBrokerService.ASSET_ATTRIBUTE_VALUE_TOPIC;-import static org.openremote.manager.mqtt.MqttBrokerService.TOPIC_SEPARATOR;+import static org.openremote.manager.mqtt.MqttBrokerService.*;import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;public class EventInterceptHandler extends AbstractInterceptHandler {@@ -60,8 +64,8 @@ public class EventInterceptHandler extends AbstractInterceptHandler {protected final Map<String, MqttConnection> mqttConnectionMap;public EventInterceptHandler(ManagerKeycloakIdentityProvider managerKeycloakIdentityProvider,- MessageBrokerService messageBrokerService,- Map<String, MqttConnection> mqttConnectionMap) {+ MessageBrokerService messageBrokerService,+ Map<String, MqttConnection> mqttConnectionMap) {this.identityProvider = managerKeycloakIdentityProvider;this.messageBrokerService = messageBrokerService;@@ -123,46 +127,98 @@ public class EventInterceptHandler extends AbstractInterceptHandler {if (connection != null) {String[] topicParts = interceptSubscribeMessage.getTopicFilter().split(TOPIC_SEPARATOR);if (topicParts.length > 1) {- String assetId = topicParts[1];- AttributeRef attributeRef = null;+ boolean isAttributeTopic = topicParts[0].equals(ATTRIBUTE_TOPIC);boolean isValueSubscription = false;- if (topicParts.length > 2) { //attribute specific- attributeRef = new AttributeRef(assetId, topicParts[2]);++ String secondTopicLevel = topicParts[1];+ String assetId = null;+ if (!secondTopicLevel.equals(SINGLE_LEVEL_WILDCARD) && !secondTopicLevel.equals(MULTI_LEVEL_WILDCARD)) {+ assetId = secondTopicLevel;}- if (topicParts.length == 4 && topicParts[3].equals(ASSET_ATTRIBUTE_VALUE_TOPIC)) {++ if (topicParts[topicParts.length - 1].equals(ATTRIBUTE_VALUE_TOPIC)) {isValueSubscription = true;}String subscriptionId;- if (attributeRef == null) {- subscriptionId = connection.assetSubscriptions.remove(assetId);+ if (isValueSubscription) {+ subscriptionId = connection.attributeValueSubscriptions.remove(interceptSubscribeMessage.getTopicFilter());} else {- if (isValueSubscription) {- subscriptionId = connection.assetAttributeValueSubscriptions.remove(attributeRef);- } else {- subscriptionId = connection.assetAttributeSubscriptions.remove(attributeRef);- }+ subscriptionId = connection.assetSubscriptions.remove(interceptSubscribeMessage.getTopicFilter());+ }+ if (TextUtil.isNullOrEmpty(subscriptionId)) {+ subscriptionId = String.valueOf(connection.getNextSubscriptionId());}- AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm).setAssetIds(assetId);- EventSubscription<AttributeEvent> subscription = new EventSubscription<>(- AttributeEvent.class,- attributeAssetFilter,- TextUtil.isNullOrEmpty(subscriptionId) ? String.valueOf(connection.getNextSubscriptionId()) : subscriptionId);+ EventSubscription<?> subscription;+ if (isAttributeTopic) {+ AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm);+ int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);- if (attributeRef == null) { //attribute specific- connection.assetSubscriptions.put(assetId, subscription.getSubscriptionId());- } else {- attributeAssetFilter.setAttributeNames(attributeRef.getName());- if (isValueSubscription) {- connection.assetAttributeValueSubscriptions.put(attributeRef, subscription.getSubscriptionId());+ if (assetId != null) {+ int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);+ if (multiLevelIndex == -1) {+ if (singleLevelIndex == -1) { //attribute/assetId/+ attributeAssetFilter.setAssetIds(assetId);+ } else {+ attributeAssetFilter.setParentIds(assetId);+ }+ if (topicParts.length > 2) { //attribute/assetId/attributeName+ if (singleLevelIndex == -1) { //attribute/assetId/attributeName+ attributeAssetFilter.setAttributeNames(topicParts[2]);+ } else if (singleLevelIndex == 2 && topicParts.length > 3 && !topicParts[3].equals(SINGLE_LEVEL_WILDCARD)) { // else attribute/assetId/+/attributeName+ attributeAssetFilter.setAttributeNames(topicParts[3]);+ } // else attribute/assetId/+ which should return all attributes+ }+ } else if (multiLevelIndex == 2) { //attribute/assetId/#+ attributeAssetFilter.setParentIds(assetId);+ }} else {- connection.assetAttributeSubscriptions.put(attributeRef, subscription.getSubscriptionId());+ if (singleLevelIndex == 1) { //attribute/++ if (topicParts.length > 2) { //attribute/+/attributeName+ attributeAssetFilter.setAttributeNames(topicParts[2]);+ }+ }+ }++ subscription = new EventSubscription<>(+ AttributeEvent.class,+ attributeAssetFilter,+ subscriptionId+ );+ } else {+ AssetFilter<AssetEvent> assetFilter = new AssetFilter<AssetEvent>().setRealm(connection.realm);+ if (assetId != null) {+ int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);+ int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);++ if (multiLevelIndex == -1) {+ assetFilter.setAssetIds(assetId);+ if (singleLevelIndex == 2) { //asset/assetId/++ assetFilter.setParentIds(assetId);+ } else if (singleLevelIndex == -1) { //asset/assetId+ assetFilter.setAssetIds(assetId);+ }+ } else if (multiLevelIndex == 2) { //asset/assetId/#+ assetFilter.setParentIds(assetId);+ }}++ subscription = new EventSubscription<>(+ AssetEvent.class,+ assetFilter,+ subscriptionId+ );+ }++ if (isValueSubscription) {+ connection.attributeValueSubscriptions.put(interceptSubscribeMessage.getTopicFilter(), subscription.getSubscriptionId());+ } else {+ connection.assetSubscriptions.put(interceptSubscribeMessage.getTopicFilter(), subscription.getSubscriptionId());}Map<String, Object> headers = prepareHeaders(connection);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, subscription, headers);- } else {+ } else {LOG.info("Couldn't process message for topic: " + interceptSubscribeMessage.getTopicFilter());}} else {@@ -176,24 +232,25 @@ public class EventInterceptHandler extends AbstractInterceptHandler {if (connection != null) {String[] topicParts = interceptUnsubscribeMessage.getTopicFilter().split(TOPIC_SEPARATOR);if (topicParts.length > 1) {- String assetId = topicParts[1];String subscriptionId;- if (topicParts.length > 2) { //attribute specific- if (topicParts.length == 4 && topicParts[3].equals(ASSET_ATTRIBUTE_VALUE_TOPIC)) {- subscriptionId = connection.assetAttributeValueSubscriptions.remove(new AttributeRef(assetId, topicParts[2]));- } else {- subscriptionId = connection.assetAttributeSubscriptions.remove(new AttributeRef(assetId, topicParts[2]));- }+ boolean isValueSubscription = topicParts[topicParts.length - 1].equals(ATTRIBUTE_VALUE_TOPIC);+ if (isValueSubscription) {+ subscriptionId = connection.attributeValueSubscriptions.remove(interceptUnsubscribeMessage.getTopicFilter());} else {- subscriptionId = connection.assetSubscriptions.remove(assetId);+ subscriptionId = connection.assetSubscriptions.remove(interceptUnsubscribeMessage.getTopicFilter());}if (subscriptionId != null) {Map<String, Object> headers = prepareHeaders(connection);- CancelEventSubscription cancelEventSubscription = new CancelEventSubscription(AttributeEvent.class, subscriptionId);+ CancelEventSubscription cancelEventSubscription;+ if (topicParts[0].equals(ATTRIBUTE_TOPIC)) {+ cancelEventSubscription = new CancelEventSubscription(AttributeEvent.class, subscriptionId);+ } else {+ cancelEventSubscription = new CancelEventSubscription(AssetEvent.class, subscriptionId);+ }messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, cancelEventSubscription, headers);}- } else {+ } else {LOG.info("Couldn't process message for topic: " + interceptUnsubscribeMessage.getTopicFilter());}} else {diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.javaindex c4a04695c..2fea815af 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java@@ -8,12 +8,12 @@ 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.ClientCredentialsAuthForm;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.asset.Asset;+import org.openremote.model.asset.AssetEvent;import org.openremote.model.asset.AssetFilter;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.shared.EventSubscription;@@ -22,12 +22,12 @@ import org.openremote.model.security.ClientRole;import java.nio.charset.StandardCharsets;import java.util.Arrays;+import java.util.List;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;-import static org.openremote.manager.mqtt.MqttBrokerService.ASSETS_TOPIC;-import static org.openremote.manager.mqtt.MqttBrokerService.ASSET_ATTRIBUTE_VALUE_TOPIC;+import static org.openremote.manager.mqtt.MqttBrokerService.*;import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {@@ -61,41 +61,44 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {protected boolean verifyRights(Topic topic, String username, String clientId, ClientRole... roles) {MqttConnection connection = mqttConnectionMap.get(clientId);if (connection == null) {- LOG.info("No connection found for clientId: " + clientId);+ LOG.warning("No connection found for clientId: " + clientId);return false;}if (!connection.username.equals(username)) {- LOG.info("Username mismatch");+ LOG.warning("Username mismatch");return false;}if (topic.isEmpty() || topic.getTokens().size() < 2) {- LOG.info("Topic may not be empty and should have the following format: assets/{assetId}(optional: /{attributeName})");+ LOG.warning("Topic may not be empty and should have the following format: asset/... or attribute/...");return false;}- if (!topic.headToken().toString().equals(ASSETS_TOPIC)) {- LOG.info("Topic should have the following format: assets/{assetId}(optional: /{attributeName})");- return false;- }+ boolean isAttributeTopic = topic.headToken().toString().equals(ATTRIBUTE_TOPIC);- if(topic.getTokens().size() == 4 && topic.getTokens().stream().noneMatch(token -> token.toString().equals(ASSET_ATTRIBUTE_VALUE_TOPIC))) {- LOG.info("Topic for raw values should end with '" + ASSET_ATTRIBUTE_VALUE_TOPIC + "'");+ if (!topic.headToken().toString().equals(ASSET_TOPIC) && !isAttributeTopic) {+ LOG.warning("Topic should start with 'asset' or 'attribute'");return false;}Token token = topic.getTokens().get(1);- Asset<?> asset = assetStorageService.find(token.toString());- if(asset == null) {- LOG.log(Level.INFO, "Asset not found");- return false;+ Asset<?> asset = null;+ if (!token.toString().equals(SINGLE_LEVEL_WILDCARD) && !token.toString().equals(MULTI_LEVEL_WILDCARD)) {+ asset = assetStorageService.find(token.toString());+ if (asset == null) {+ LOG.warning("Asset not found");+ return false;+ }}- if(topic.getTokens().size() > 2) {++ if (isAttributeTopic && topic.getTokens().size() > 2) {token = topic.getTokens().get(2);- if (!asset.getAttribute(token.toString()).isPresent()) {- LOG.log(Level.INFO, "Attribute not found on asset");- return false;+ if (!token.toString().equals(SINGLE_LEVEL_WILDCARD) && !token.toString().equals(MULTI_LEVEL_WILDCARD)) {+ if (asset == null || !asset.getAttribute(token.toString()).isPresent()) {+ LOG.warning("Attribute not found on asset");+ return false;+ }}}@@ -108,7 +111,7 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {try {accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));} catch (VerificationException verificationException) {- LOG.log(Level.INFO, "Couldn't verify token", verificationException);+ LOG.log(Level.WARNING, "Couldn't verify token", verificationException);return false;}}@@ -118,16 +121,74 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {return identityProvider.canSubscribeWith(authContext, new TenantFilter(connection.realm), roles);} else { // readString[] topicParts = topic.getTokens().stream().map(Token::toString).toArray(String[]::new);- String assetId = topicParts[1];- AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm).setAssetIds(assetId);- if (topicParts.length >= 3) { //attribute specific- attributeAssetFilter.setAttributeNames(topicParts[2]);++ if (isAttributeTopic) {+ AssetFilter<AttributeEvent> attributeAssetFilter = new AssetFilter<AttributeEvent>().setRealm(connection.realm);++ int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);++ if (asset != null) {+ int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);+ if (multiLevelIndex == -1) {+ if (singleLevelIndex == -1) { //attribute/assetId/+ attributeAssetFilter.setAssetIds(asset.getId());+ } else {+ attributeAssetFilter.setParentIds(asset.getId());+ }+ if (topicParts.length > 2) { //attribute/assetId/attributeName+ if (singleLevelIndex == -1) { //attribute/assetId/attributeName+ attributeAssetFilter.setAttributeNames(topicParts[2]);+ } else if (singleLevelIndex == 2 && topicParts.length > 3 && !topicParts[3].equals(SINGLE_LEVEL_WILDCARD)) { // else attribute/assetId/+/attributeName+ attributeAssetFilter.setAttributeNames(topicParts[3]);+ } // else attribute/assetId/+ which should return all attributes+ }+ } else if (multiLevelIndex == 2) { //attribute/assetId/#+ attributeAssetFilter.setParentIds(asset.getParentId());+ }+ } else {+ if (singleLevelIndex == 1) { //attribute/++ if (topicParts.length > 2) { //attribute/+/attributeName+ attributeAssetFilter.setAttributeNames(topicParts[2]);+ } else {+ LOG.warning("Using single level must be followed with attribute name when not using assetId");+ return false;+ }+ }+ }++ EventSubscription<AttributeEvent> subscription = new EventSubscription<>(+ AttributeEvent.class,+ attributeAssetFilter+ );+ return clientEventService.authorizeEventSubscription(authContext, subscription);+ } else {+ AssetFilter<AssetEvent> assetFilter = new AssetFilter<AssetEvent>().setRealm(connection.realm);+ if (asset != null) {+ int multiLevelIndex = Arrays.asList(topicParts).indexOf(MULTI_LEVEL_WILDCARD);+ int singleLevelIndex = Arrays.asList(topicParts).indexOf(SINGLE_LEVEL_WILDCARD);++ if (multiLevelIndex == -1) {+ assetFilter.setAssetIds(asset.getId());+ if (singleLevelIndex == 2) { //asset/assetId/++ assetFilter.setParentIds(asset.getId());+ } else if (singleLevelIndex == -1) { //asset/assetId+ assetFilter.setAssetIds(asset.getId());+ } else {+ LOG.warning("Using single level can be only used after assetId for asset events");+ return false;+ }+ } else if (multiLevelIndex == 2) { //asset/assetId/#+ assetFilter.setParentIds(asset.getParentId());+ }+ }++ EventSubscription<AssetEvent> subscription = new EventSubscription<>(+ AssetEvent.class,+ assetFilter+ );+ return clientEventService.authorizeEventSubscription(authContext, subscription);}- EventSubscription<AttributeEvent> subscription = new EventSubscription<>(- AttributeEvent.class,- attributeAssetFilter- );- return clientEventService.authorizeEventSubscription(authContext, subscription);}}}+diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.javaindex b664d3b8d..7df817dbb 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java@@ -36,7 +36,9 @@ 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.asset.AssetEvent;import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.event.Event;import org.openremote.model.event.TriggeredEventSubscription;import org.openremote.model.value.Values;@@ -58,9 +60,12 @@ public class MqttBrokerService implements ContainerService {public static final String MQTT_SERVER_LISTEN_HOST = "MQTT_SERVER_LISTEN_HOST";public static final String MQTT_SERVER_LISTEN_PORT = "MQTT_SERVER_LISTEN_PORT";- public static final String ASSETS_TOPIC = "assets";+ public static final String ASSET_TOPIC = "asset";+ public static final String ATTRIBUTE_TOPIC = "attribute";public static final String TOPIC_SEPARATOR = "/";- public static final String ASSET_ATTRIBUTE_VALUE_TOPIC = "value";+ public static final String ATTRIBUTE_VALUE_TOPIC = "value";+ public static final String SINGLE_LEVEL_WILDCARD = "+";+ public static final String MULTI_LEVEL_WILDCARD = "#";protected ManagerKeycloakIdentityProvider identityProvider;protected ClientEventService clientEventService;@@ -112,16 +117,40 @@ public class MqttBrokerService implements ContainerService {.process(exchange -> {String sessionKey = getSessionKey(exchange);@SuppressWarnings("unchecked")- TriggeredEventSubscription<AttributeEvent> triggeredEventSubscription = (TriggeredEventSubscription<AttributeEvent>) exchange.getIn().getBody(TriggeredEventSubscription.class);+ TriggeredEventSubscription<?> triggeredEventSubscription = exchange.getIn().getBody(TriggeredEventSubscription.class);triggeredEventSubscription.getEvents().forEach(event -> {- MqttConnection mqttConnection = mqttConnectionMap.get(sessionKey);- if (mqttConnection != null) {- if (mqttConnection.assetSubscriptions.containsKey(event.getAssetId()) || mqttConnection.assetAttributeSubscriptions.containsKey(event.getAttributeRef())) {- sendAttributeEvent(sessionKey, event);+ if (event.getEventType().equals(Event.getEventType(AssetEvent.class))) {+ AssetEvent assetEvent = (AssetEvent) event;+ MqttConnection mqttConnection = mqttConnectionMap.get(sessionKey);+ if (mqttConnection != null) {+ Optional<String> topic = mqttConnection.assetSubscriptions.entrySet()+ .stream()+ .filter(entry -> triggeredEventSubscription.getSubscriptionId().equals(entry.getValue()))+ .map(Map.Entry::getKey)+ .findFirst();++ topic.ifPresent(topicValue -> sendAssetEvent(sessionKey, topicValue, assetEvent));}- if (mqttConnection.assetAttributeValueSubscriptions.containsKey(event.getAttributeRef())) {- sendAttributeValue(sessionKey, event);+ } else {+ AttributeEvent attributeEvent = (AttributeEvent) event;+ MqttConnection mqttConnection = mqttConnectionMap.get(sessionKey);+ if (mqttConnection != null) {+ Optional<String> topic = mqttConnection.assetSubscriptions.entrySet()+ .stream()+ .filter(entry -> triggeredEventSubscription.getSubscriptionId().equals(entry.getValue()))+ .map(Map.Entry::getKey)+ .findFirst();++ topic.ifPresent(topicValue -> sendAttributeEvent(sessionKey, topicValue, attributeEvent));++ topic = mqttConnection.attributeValueSubscriptions.entrySet()+ .stream()+ .filter(entry -> triggeredEventSubscription.getSubscriptionId().equals(entry.getValue()))+ .map(Map.Entry::getKey)+ .findFirst();++ topic.ifPresent(topicValue -> sendAttributeValue(sessionKey, topicValue, attributeEvent));}}});@@ -152,13 +181,28 @@ public class MqttBrokerService implements ContainerService {LOG.fine("Stopped MQTT broker");}- protected void sendAttributeEvent(String clientId, AttributeEvent attributeEvent) {+ protected void sendAssetEvent(String clientId, String topic, AssetEvent assetEvent) {try {- ByteBuf payload = Unpooled.copiedBuffer(Values.JSON.writeValueAsString(attributeEvent), Charset.defaultCharset());+ ByteBuf payload = Unpooled.copiedBuffer(Values.JSON.writeValueAsString(assetEvent), Charset.defaultCharset());++ MqttPublishMessage publishMessage = MqttMessageBuilders.publish()+ .qos(MqttQoS.AT_MOST_ONCE)+ .topicName(topic)+ .payload(payload)+ .build();++ mqttBroker.internalPublish(publishMessage, clientId);+ } catch (JsonProcessingException e) {+ LOG.log(Level.WARNING, "Couldn't send AttributeEvent to MQTT client", e);+ }+ }+ protected void sendAttributeEvent(String clientId, String topic, AttributeEvent attributeEvent) {+ try {+ ByteBuf payload = Unpooled.copiedBuffer(Values.JSON.writeValueAsString(attributeEvent), Charset.defaultCharset());MqttPublishMessage publishMessage = MqttMessageBuilders.publish().qos(MqttQoS.AT_MOST_ONCE)- .topicName(ASSETS_TOPIC + TOPIC_SEPARATOR + attributeEvent.getAssetId())+ .topicName(topic).payload(payload).build();@@ -168,12 +212,12 @@ public class MqttBrokerService implements ContainerService {}}- public void sendAttributeValue(String clientId, AttributeEvent attributeEvent) {+ public void sendAttributeValue(String clientId, String topic, AttributeEvent attributeEvent) {ByteBuf payload = Unpooled.copiedBuffer(Values.asJSON(attributeEvent.getValue()).orElse(""), Charset.defaultCharset());MqttPublishMessage publishMessage = MqttMessageBuilders.publish().qos(MqttQoS.AT_MOST_ONCE)- .topicName(ASSETS_TOPIC + TOPIC_SEPARATOR + attributeEvent.getAssetId() + TOPIC_SEPARATOR + attributeEvent.getAttributeName())+ .topicName(topic).payload(payload).build();diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.javaindex f719fc83c..c4535c0d3 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java@@ -14,8 +14,7 @@ public class MqttConnection {protected final String username;protected final byte[] password;protected final Map<String, String> assetSubscriptions;- protected final Map<AttributeRef, String> assetAttributeSubscriptions;- protected final Map<AttributeRef, String> assetAttributeValueSubscriptions;+ protected final Map<String, String> attributeValueSubscriptions;protected String accessToken;protected int subscriptionId;@@ -30,8 +29,7 @@ public class MqttConnection {this.username = username;this.password = password;this.assetSubscriptions = new HashMap<>();- this.assetAttributeSubscriptions = new HashMap<>();- assetAttributeValueSubscriptions = new HashMap<>();+ attributeValueSubscriptions = new HashMap<>();this.subscriptionId = 0;}@@ -60,18 +58,13 @@ public class MqttConnection {}- public Map<String,String> getAssetSubscriptions() {+ public Map<String, String> getAssetSubscriptions() {return this.assetSubscriptions;}- public Map<AttributeRef,String> getAssetAttributeSubscriptions() {- return this.assetAttributeSubscriptions;- }--- public Map<AttributeRef,String> getAssetAttributeValueSubscriptions() {- return this.assetAttributeValueSubscriptions;+ public Map<String, String> getAttributeValueSubscriptions() {+ return this.attributeValueSubscriptions;}@@ -79,7 +72,8 @@ public class MqttConnection {return this.accessToken;}+public int getSubscriptionId() {return this.subscriptionId;- }+ }}diff --git a/mvp/mvp-docker-compose.yml b/mvp/mvp-docker-compose.ymlindex 5c7a2c1ee..bdacb893f 100644--- a/mvp/mvp-docker-compose.yml+++ b/mvp/mvp-docker-compose.yml@@ -28,7 +28,7 @@ volumes:services:proxy:- image: openremote/proxy:${PROXY_VERSION:-2.1.2.1}+ image: openremote/proxy:${PROXY_VERSION:-2.1.2.2}networks:- public- privatediff --git a/profile/deploy.yml b/profile/deploy.ymlindex d6f36ab01..41c4ed46f 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -33,7 +33,7 @@ services:# map a Docker volume or host directory to access/store proxy configuration files# and SSL certificates.proxy:- image: openremote/proxy:${PROXY_VERSION:-2.1.2.1}+ image: openremote/proxy:${PROXY_VERSION:-2.1.2.2}restart: always# Override the haproxy.cfg with custom file#volumes:diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 0de1e5c7c..d08b6ee84 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -5,17 +5,19 @@ import io.moquette.BrokerConstantsimport org.openremote.agent.protocol.simulator.SimulatorProtocolimport org.openremote.container.util.UniqueIdentifierGeneratorimport org.openremote.manager.agent.AgentService-import org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.mqtt.MqttBrokerServiceimport org.openremote.manager.setup.SetupService+import org.openremote.model.asset.AssetEvent+import org.openremote.model.asset.impl.ThingAsset+import org.openremote.model.attribute.Attributeimport org.openremote.test.setup.KeycloakTestSetupimport org.openremote.test.setup.ManagerTestSetupimport org.openremote.model.attribute.AttributeEvent-import org.openremote.model.attribute.AttributeRefimport org.openremote.model.value.Valuesimport org.openremote.test.ManagerContainerTraitimport org.openremote.test.RawClient+import spock.lang.Sharedimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions@@ -23,27 +25,34 @@ import static org.openremote.container.util.MapAccess.*import static org.openremote.manager.mqtt.KeycloakAuthenticator.MQTT_CLIENT_ID_SEPARATORimport static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_HOSTimport static org.openremote.manager.mqtt.MqttBrokerService.MQTT_SERVER_LISTEN_PORT+import static org.openremote.manager.mqtt.MqttBrokerService.MULTI_LEVEL_WILDCARD+import static org.openremote.manager.mqtt.MqttBrokerService.TOPIC_SEPARATOR+import static org.openremote.model.value.ValueType.TEXTclass MqttBrokerTest extends Specification implements ManagerContainerTrait {- def "Mqtt broker test"() {+ def "Mqtt broker event test"() {given: "the container environment is started"+ def mqttBrokerServiceAssetEventCalls = 0def mqttBrokerServiceAttributeEventCalls = 0def mqttBrokerServiceAttributeValueCalls = 0+def spyMqttBrokerService = Spy(MqttBrokerService) {- sendAttributeEvent(_ as String, _ as AttributeEvent) >> {- clientId, attributeEvent ->+ sendAssetEvent(_ as String, _ as String, _ as AssetEvent) >> {+ clientId, topic, assetEvent ->+ mqttBrokerServiceAssetEventCalls+++ }+ sendAttributeEvent(_ as String, _ as String, _ as AttributeEvent) >> {+ clientId, topic, attributeEvent ->mqttBrokerServiceAttributeEventCalls++- callRealMethod()}- sendAttributeValue(_ as String, _ as AttributeEvent) >> {- clientId, attributeEvent ->+ sendAttributeValue(_ as String, _ as String, _ as AttributeEvent) >> {+ clientId, topic, attributeEvent ->mqttBrokerServiceAttributeValueCalls++- callRealMethod()}}- def conditions = new PollingConditions(timeout: 20, delay: 0.2)+ def conditions = new PollingConditions(timeout: 10, delay: 0.2)def services = Lists.newArrayList(defaultServices())services.replaceAll { it instanceof MqttBrokerService ? spyMqttBrokerService : it }def container = startContainer(defaultConfig(), services)@@ -81,8 +90,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {.write(0x00, 0x00) // Keep Alive// Payload- .write(0x00, "client1".size().byteValue()) // Client Identifier Length- .write("client1") // Client Identifier+ .write(0x00, managerTestSetup.realmBuildingTenant.size().byteValue()) // Client Identifier Length+ .write(managerTestSetup.realmBuildingTenant) // Client Identifier.write(0x00, clientId.size().byteValue()).write(clientId).write(0x00, clientSecret.size().byteValue())@@ -131,7 +140,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset in another realm"- def topic = "assets/" + managerTestSetup.thingId+ def topic = "attribute/" + managerTestSetup.thingIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -152,7 +161,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an non existing asset"- topic = "assets/dhajkdasjfgh"+ topic = "attribute/dhajkdasjfgh"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -173,7 +182,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset"- topic = "assets/" + managerTestSetup.apartment1HallwayId+ topic = "attribute/" + managerTestSetup.apartment1HallwayIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -190,7 +199,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "A subscription should exist"conditions.eventually {- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() > 0+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1}when: "An asset attribute changed the client is subscribed on"@@ -212,7 +221,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client publishes to an asset attribute which is readonly"- topic = "assets/" + managerTestSetup.apartment1HallwayId+ topic = "attribute/" + managerTestSetup.apartment1HallwayIddef payload = Values.asJSON(Values.createJsonObject().put("motionSensor", 70)).orElse(null)remainingLength = 2 + topic.size() + payload.length()@@ -231,7 +240,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client publishes to an asset attribute"- topic = "assets/" + managerTestSetup.apartment1HallwayId+ topic = "attribute/" + managerTestSetup.apartment1HallwayIdpayload = Values.asJSON(Values.createJsonObject().put("lights", false)).orElse(null)remainingLength = 2 + topic.size() + payload.length()@@ -251,7 +260,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset"- topic = "assets/" + managerTestSetup.apartment1HallwayId+ topic = "attribute/" + managerTestSetup.apartment1HallwayIdremainingLength = 4 + topic.size()client@@ -281,7 +290,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset attribute"- topic = "assets/" + managerTestSetup.apartment1HallwayId + "/motionSensor"+ topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -296,9 +305,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {.write(0x01) // QoS level 1.flush()- then: "the subscription should be established"+ then: "the subscription should be created"conditions.eventually {- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeSubscriptions.containsKey(new AttributeRef(managerTestSetup.apartment1HallwayId, "motionSensor"))+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.containsKey(topic)}when: "that attribute changed"@@ -320,7 +329,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset attribute"- topic = "assets/" + managerTestSetup.apartment1HallwayId + "/motionSensor"+ topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor"remainingLength = 4 + topic.size()client@@ -337,11 +346,41 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeSubscriptions.size() == 0+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0+ }++ when: "a mqtt client subscribes with multilevel on asset"+ topic = "attribute/" + managerTestSetup.apartment1Id + TOPIC_SEPARATOR + 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()++ then: "A subscription should exist"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1+ }++ when: "An child asset attribute changed the client is subscribed on"+ attributeEvent = new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 40)+ ((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(attributeEvent)++ then: "A publish event message should be sent"+ conditions.eventually {+ assert mqttBrokerServiceAttributeEventCalls == 5}when: "a mqtt client subscribes to an asset attribute value"- topic = "assets/" + managerTestSetup.apartment1HallwayId + "/motionSensor/value"+ topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor/value"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -356,9 +395,9 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {.write(0x01) // QoS level 1.flush()- then: "the subscription should be established"+ then: "the subscription should be created"conditions.eventually {- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeValueSubscriptions.containsKey(new AttributeRef(managerTestSetup.apartment1HallwayId, "motionSensor"))+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).attributeValueSubscriptions.containsKey(topic)}when: "that attribute changed"@@ -380,7 +419,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client unsubscribes to an asset attribute value"- topic = "assets/" + managerTestSetup.apartment1HallwayId + "/motionSensor/value"+ topic = "attribute/" + managerTestSetup.apartment1HallwayId + "/motionSensor/value"remainingLength = 4 + topic.size()client@@ -397,7 +436,193 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {then: "No subscription should exist"conditions.eventually {- assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetAttributeValueSubscriptions.size() == 0+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).attributeValueSubscriptions.size() == 0+ }++ when: " a client disconnects"+ client+ // DISCONNECT+ .write(0xE0) // MQTT Control Packet type(14) with QoS level 0+ .write(0.byteValue()) // Remaining Length+ .flush()++ then: "no connection should be left"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.size() == 0+ }++ when: "a mqtt client reconnects"+ payloadLength = mqttClientId.size() + clientId.size() + clientSecret.size()+ remainingLength = 16 + payloadLength+ client = RawClient.connect(mqttHost, mqttPort).isConnected()+ // 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, clientId.size().byteValue())+ .write(clientId)+ .write(0x00, clientSecret.size().byteValue())+ .write(clientSecret)+ .flush()++ then: "mqtt connection should exist"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId) != null+ }++ when: "a mqtt client subscribes to an asset in another realm"+ topic = "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.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0+ }++ when: "a mqtt client subscribes to an non existing asset"+ topic = "asset/dhajkdasjfgh"+ 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.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 0+ }++ when: "a mqtt client subscribes to an asset"+ topic = "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()++ then: "A subscription should exist"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1+ }++ when: "An asset is updated with a new attribute"+ def asset1 = assetStorageService.find(managerTestSetup.apartment1HallwayId)+ asset1.addAttributes(new Attribute<>("temp", TEXT))+ asset1 = assetStorageService.merge(asset1)++ then: "A publish event message should be sent"+ conditions.eventually {+ assert mqttBrokerServiceAssetEventCalls == 1+ }++ when: "a mqtt client subscribes for child assets of an asset"+ topic = "asset/" + managerTestSetup.apartment1Id + TOPIC_SEPARATOR + 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()++ and: "a second subscription is in place"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 2+ }++ and: "a asset is added as child to the subcribed asset"+ def childAsset = new ThingAsset("child")+ .setParentId(managerTestSetup.apartment1Id)+ .setRealm(managerTestSetup.realmBuildingTenant);+ childAsset = assetStorageService.merge(childAsset);++ then: "another event should be sent"+ conditions.eventually {+ assert mqttBrokerServiceAssetEventCalls == 2+ }++ when: "a mqtt client unsubscribes to an asset"+ topic = "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++ // Payload+ .write(0x00, topic.size().byteValue()) // Topic Length+ .write(topic) // Topic+ .flush()+++ then: "Only one subscription should be left"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.get(mqttClientId).assetSubscriptions.size() == 1+ }++ when: "MQTT client disconnects"+ client+ // DISCONNECT+ .write(0xE0) // MQTT Control Packet type(14) with QoS level 0+ .write(0.byteValue()) // Remaining Length+ .flush()++ then: "no connection should be left"+ conditions.eventually {+ assert mqttBrokerService.mqttConnectionMap.size() == 0}}}