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 refactor (last will attribute write now possible) (#786)
Rich Turner66b1af8f5b5d
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.javaindex e9dc2647b..0689654e4 100644--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.java+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTLastWill.java@@ -48,4 +48,13 @@ public class MQTTLastWill {public boolean isRetain() {return retain;}++ @Override+ public String toString() {+ return getClass().getSimpleName() + "{" ++ "topic='" + topic + '\'' ++ ", retain=" + retain ++ ", hasPayload=" + (payload != null ? "true" : "false") ++ '}';+ }}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.javaindex 9ddf3145a..ab99aeea7 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -22,7 +22,10 @@ package org.openremote.manager.mqtt;import io.moquette.broker.subscriptions.Token;import io.moquette.broker.subscriptions.Topic;import io.moquette.interception.messages.*;-import io.netty.handler.codec.mqtt.MqttQoS;+import io.netty.buffer.ByteBuf;+import io.netty.buffer.Unpooled;+import io.netty.handler.codec.mqtt.*;+import org.openremote.agent.protocol.mqtt.MQTTLastWill;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.security.AuthContext;import org.openremote.container.web.ConnectionConstants;@@ -120,7 +123,6 @@ public class DefaultMQTTHandler extends MQTTHandler {Runnable closeRunnable = mqttBrokerService.getForceDisconnectRunnable(connection.getClientId());headers.put(ConnectionConstants.SESSION_TERMINATOR, closeRunnable);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);- LOG.fine("Connected: " + connection);return true;}@@ -131,7 +133,6 @@ public class DefaultMQTTHandler extends MQTTHandler {Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_CLOSE, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);- LOG.fine("Connection closed: " + connection);}@Override@@ -140,7 +141,26 @@ public class DefaultMQTTHandler extends MQTTHandler {Map<String, Object> headers = prepareHeaders(connection);headers.put(ConnectionConstants.SESSION_CLOSE_ERROR, true);messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, null, headers);- LOG.fine("Connection lost: " + connection);++ // Try and publish last will if configured+ if (connection.hasLastWill()) {+ // Pass through standard canPublish/publish+ MQTTLastWill lastWill = connection.getLastWill();+ Topic lastWillTopic = Topic.asTopic(lastWill.getTopic());++ if (handlesTopic(lastWillTopic)) {++ getLogger().info("Processing last will for client: " + connection);++ if (canPublish(connection, lastWillTopic)) {+ MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, true, MqttQoS.EXACTLY_ONCE, false, 0);+ MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(lastWill.getTopic(), 0);+ ByteBuf payload = Unpooled.wrappedBuffer(lastWill.getPayload().toString().getBytes());+ InterceptPublishMessage message = new InterceptPublishMessage(new MqttPublishMessage(fixedHeader, varHeader, payload), msg.getClientID(), msg.getUsername());+ doPublish(connection, lastWillTopic, message);+ }+ }+ }}@Override@@ -294,14 +314,16 @@ public class DefaultMQTTHandler extends MQTTHandler {LOG.fine("Publish attribute events topic should be {realm}/{clientId}/writeattribute: topic=" + topic + ", connection=" + connection);return false;}+ return true;} else if (isAttributeValueWriteTopic(topic)) {if (topic.getTokens().size() != 5 || !Pattern.matches(ASSET_ID_REGEXP, topicTokenIndexToString(topic, 4))) {LOG.fine("Publish attribute value topic should be {realm}/{clientId}/writeattributevalue/{attributeName}/{assetId}: topic=" + topic + ", connection=" + connection);return false;}+ return true;}- return true;+ return false;}@SuppressWarnings({"rawtypes", "unchecked"})diff --git a/manager/src/main/java/org/openremote/manager/mqtt/LastWillMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/LastWillMQTTHandler.javadeleted file mode 100644index e761991b3..000000000--- a/manager/src/main/java/org/openremote/manager/mqtt/LastWillMQTTHandler.java+++ /dev/null@@ -1,142 +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 io.moquette.broker.subscriptions.Topic;-import io.moquette.interception.messages.InterceptConnectMessage;-import io.moquette.interception.messages.InterceptPublishMessage;-import io.moquette.interception.messages.InterceptSubscribeMessage;-import io.moquette.interception.messages.InterceptUnsubscribeMessage;-import org.openremote.model.Constants;-import org.openremote.model.Container;-import org.openremote.model.security.ClientRole;-import org.openremote.model.syslog.SyslogCategory;-import org.openremote.model.util.TextUtil;--import java.util.logging.Logger;--import static org.openremote.model.syslog.SyslogCategory.API;--/**- * This handler supports last will publishes and subscriptions; the publish is verified at connect time as normal- * {@link #canPublish} method isn't called for last will publishes. Clients can subscribe to any last will message so- * the {@link org.openremote.model.security.ClientRole#READ_LAST_WILL} should be used sparingly.- * Topic format is `{REALM}/lastwill/{IDENTIFIER}` the IDENTIFIER can be any string and wildcard subscriptions are allowed.- */-// TODO: Introduce role for read/write last will-public class LastWillMQTTHandler extends MQTTHandler {-- public static final int PRIORITY = DefaultMQTTHandler.PRIORITY + 1000;- public static final String LAST_WILL_TOKEN = "last_will";- private static final Logger LOG = SyslogCategory.getLogger(API, LastWillMQTTHandler.class);-- @Override- public void start(Container container) throws Exception {- super.start(container);- mqttBrokerService = container.getService(MqttBrokerService.class);- }-- @Override- public boolean handlesTopic(Topic topic) {- // Skip standard checks- return topicMatches(topic);- }-- @Override- protected boolean topicMatches(Topic topic) {- return isLastWillTopic(topic);- }-- @Override- protected Logger getLogger() {- return LOG;- }-- @Override- public boolean onConnect(MqttConnection connection, InterceptConnectMessage msg) {- super.onConnect(connection, msg);-- // Force disconnect if last will topic is in a different realm- if (msg.isWillFlag()) {- Topic topic = TextUtil.isNullOrEmpty(msg.getWillTopic()) ? null : Topic.asTopic(msg.getWillTopic());- if (topic == null || !isLastWillTopic(topic)) {- getLogger().warning("Last will topic must be in the format `{REALM}/" + LAST_WILL_TOKEN + "/IDENTIFIER' client will be disconnected: " + connection);- Runnable closeRunnable = mqttBrokerService.getForceDisconnectRunnable(connection.getClientId());- closeRunnable.run();- return false;- }- }- return true;- }-- @Override- public boolean checkCanSubscribe(MqttConnection connection, Topic topic) {- if (connection.getAuthContext() == null) {- getLogger().fine("Anonymous connection subscriptions not supported by this handler, topic=" + topic + ", connection" + connection);- return false;- }- if (!topicRealmMatchesConnection(connection, topic)) {- getLogger().fine("Topic realm token must match the connection, topic=" + topic + ", connection" + connection);- return false;- }- if (!canSubscribe(connection, topic)) {- getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", connection" + connection);- return false;- }- return true;- }-- @Override- public boolean checkCanPublish(MqttConnection connection, Topic topic) {- // Last will publish doesn't reach here so we verify it at the connect stage and we just disallow client side- // publishes using this handler- return false;- }-- @Override- public boolean canSubscribe(MqttConnection connection, Topic topic) {- return isLastWillTopic(topic) && connection.getAuthContext().hasResourceRole(ClientRole.READ_LAST_WILL.getValue(), Constants.KEYCLOAK_CLIENT_ID);- }-- @Override- public boolean canPublish(MqttConnection connection, Topic topic) {- return false;- }-- @Override- public void doSubscribe(MqttConnection connection, Topic topic, InterceptSubscribeMessage msg) {- // Nothing to do here - standard broker routing between subscribers and publishers- }-- @Override- public void doUnsubscribe(MqttConnection connection, Topic topic, InterceptUnsubscribeMessage msg) {- // Nothing to do here - standard broker routing between subscribers and publishers- }-- @Override- public void doPublish(MqttConnection connection, Topic topic, InterceptPublishMessage msg) {- // Nothing to do here - standard broker routing between subscribers and publishers- }-- protected static boolean isLastWillTopic(Topic topic) {- return LAST_WILL_TOKEN.equals(topicTokenIndexToString(topic, 1))- && topic.getTokens().size() == 3;- }-}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.javaindex 031f01e65..5a3edfbe5 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java@@ -3,6 +3,7 @@ package org.openremote.manager.mqtt;import org.keycloak.adapters.rotation.AdapterTokenVerifier;import org.keycloak.common.VerificationException;import org.keycloak.representations.AccessToken;+import org.openremote.agent.protocol.mqtt.MQTTLastWill;import org.openremote.container.security.AuthContext;import org.openremote.container.security.keycloak.AccessTokenAuthContext;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;@@ -33,12 +34,14 @@ public class MqttConnection {protected ManagerKeycloakIdentityProvider identityProvider;protected long connectionTime;protected boolean obsolete;+ protected MQTTLastWill lastWill;- public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password, boolean cleanSession, long connectionTime) {+ public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password, boolean cleanSession, long connectionTime, MQTTLastWill lastWill) {this.cleanSession = cleanSession;this.clientId = clientId;this.identityProvider = identityProvider;this.connectionTime = connectionTime;+ this.lastWill = lastWill;setCredentials(realm, username, password);}@@ -100,6 +103,14 @@ public class MqttConnection {return credentials;}+ public boolean hasLastWill() {+ return lastWill != null;+ }++ public MQTTLastWill getLastWill() {+ return lastWill;+ }+public void setCredentials(String realm, String username, String password) {this.realm = realm;@@ -131,6 +142,7 @@ public class MqttConnection {", username='" + username + '\'' +", clientId='" + clientId + '\'' +", obsolete='" + obsolete + '\'' ++ ", lastWill='" + lastWill + '\'' +'}';}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.javaindex f7a90d079..ea271ca30 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORInterceptHandler.java@@ -23,12 +23,14 @@ import com.google.api.client.util.Charsets;import io.moquette.broker.subscriptions.Topic;import io.moquette.interception.AbstractInterceptHandler;import io.moquette.interception.messages.*;+import org.openremote.agent.protocol.mqtt.MQTTLastWill;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.timer.TimerService;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.ValueUtil;-import java.util.HashMap;+import java.util.Optional;import java.util.WeakHashMap;import java.util.logging.Logger;@@ -74,6 +76,7 @@ public class ORInterceptHandler extends AbstractInterceptHandler {public void onConnect(InterceptConnectMessage msg) {String realm = null;String username = null;+ MQTTLastWill lastWill = null;String password = msg.isPasswordFlag() ? new String(msg.getPassword(), Charsets.UTF_8) : null;if (msg.getUsername() != null) {@@ -82,7 +85,13 @@ public class ORInterceptHandler extends AbstractInterceptHandler {username = realmAndUsername[1];}- MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password, msg.isCleanSession(), timerService.getCurrentTimeMillis());+ if (msg.isWillFlag()) {+ Optional<String> payload = msg.getWillMessage() != null ? Optional.of(new String(msg.getWillMessage())) : Optional.empty();+ lastWill = new MQTTLastWill(msg.getWillTopic(), payload.flatMap(ValueUtil::parse).orElse(null), msg.isWillRetain());+ }++ MqttConnection connection = new MqttConnection(identityProvider, msg.getClientID(), realm, username, password, msg.isCleanSession(), timerService.getCurrentTimeMillis(), lastWill);+ LOG.fine("Client connect: " + connection);brokerService.addConnection(connection.getClientId(), connection);// Notify all custom handlers@@ -94,19 +103,17 @@ public class ORInterceptHandler extends AbstractInterceptHandler {}}+ // This is actively initiated by the client when it is gracefully disconnecting@Overridepublic void onDisconnect(InterceptDisconnectMessage msg) {MqttConnection connection;- synchronized (disconnectedConnections) {- connection = disconnectedConnections.remove(msg.getClientID());- }+ connection = brokerService.clearConnectionSession(msg.getClientID());if (connection != null) {+ LOG.fine("Client disconnect: " + connection);+// No topic info here so just notify all custom handlers- MqttConnection finalConnection = connection;- brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onDisconnect(finalConnection, msg));- } else {- connection = brokerService.clearConnectionSession(msg.getClientID());+ brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onDisconnect(connection, msg));// Store weak reference to connection for onConnectionLostsynchronized (disconnectedConnections) {@@ -115,8 +122,11 @@ public class ORInterceptHandler extends AbstractInterceptHandler {}}+ // This is called on graceful and ungraceful disconnects (should really only be called on ungraceful so we need to+ // mimic that here)@Overridepublic void onConnectionLost(InterceptConnectionLostMessage msg) {+// This can fire after onConnect which causes the connection to be removed; seems that onDisconnect also gets// called when connection is lost and that is called first and before onConnect so we'll just use that for nowMqttConnection connection;@@ -124,16 +134,14 @@ public class ORInterceptHandler extends AbstractInterceptHandler {connection = disconnectedConnections.remove(msg.getClientID());}- if (connection != null) {+ if (connection == null) {+ // Indicates that client didn't initiate graceful DISCONNECT (i.e. true connection lost)// No topic info here so just notify all custom handlers- MqttConnection finalConnection = connection;- brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnectionLost(finalConnection, msg));- } else {connection = brokerService.clearConnectionSession(msg.getClientID());-- // Store weak reference to connection for onDisconnect- synchronized (disconnectedConnections) {- disconnectedConnections.put(msg.getClientID(), connection);+ if (connection != null) {+ LOG.fine("Client connection lost: " + connection);+ MqttConnection finalConnection = connection;+ brokerService.getCustomHandlers().forEach(customHandler -> customHandler.onConnectionLost(finalConnection, msg));}}}diff --git a/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java b/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.javaindex 6328f405b..eaa312029 100644--- a/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java+++ b/manager/src/main/java/org/openremote/manager/setup/ManagerSetup.java@@ -146,7 +146,8 @@ public class ManagerSetup implements Setup {room.getAttributes().addOrReplace(new Attribute<>("motionSensor", INTEGER).addMeta(new MetaItem<>(LABEL, "Motion sensor"),new MetaItem<>(READ_ONLY, true), new MetaItem<>(RULE_STATE, true),- new MetaItem<>(STORE_DATA_POINTS)),+ new MetaItem<>(STORE_DATA_POINTS),+ new MetaItem<>(ACCESS_RESTRICTED_WRITE)),new Attribute<>("presenceDetected", BOOLEAN).addMeta(new MetaItem<>(LABEL, "Presence detected"),new MetaItem<>(RULE_STATE, true), new MetaItem<>(ACCESS_RESTRICTED_READ, true),new MetaItem<>(READ_ONLY, true), new MetaItem<>(STORE_DATA_POINTS, true)),diff --git a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandlerindex a3854e4bc..39e1cd182 100644--- a/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler+++ b/manager/src/main/resources/META-INF/services/org.openremote.manager.mqtt.MQTTHandler@@ -1,3 +1,2 @@org.openremote.manager.mqtt.DefaultMQTTHandler-org.openremote.manager.mqtt.LastWillMQTTHandlerorg.openremote.manager.provisioning.UserAssetProvisioningMQTTHandlerdiff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.javaindex cf2769941..7871a0802 100644--- a/model/src/main/java/org/openremote/model/Constants.java+++ b/model/src/main/java/org/openremote/model/Constants.java@@ -34,7 +34,6 @@ public interface Constants {String READ_MAP_ROLE = "read:map";String READ_ASSETS_ROLE = "read:assets";String READ_RULES_ROLE = "read:rules";- String READ_LAST_WILL = "read:lastwill";String WRITE_USER_ROLE = "write:user";String WRITE_ADMIN_ROLE = "write:admin";String WRITE_LOGS_ROLE = "write:logs";diff --git a/model/src/main/java/org/openremote/model/security/ClientRole.java b/model/src/main/java/org/openremote/model/security/ClientRole.javaindex ee0e8d5d3..42a4ffd9c 100644--- a/model/src/main/java/org/openremote/model/security/ClientRole.java+++ b/model/src/main/java/org/openremote/model/security/ClientRole.java@@ -39,7 +39,6 @@ public enum ClientRole {READ_MAP(Constants.READ_MAP_ROLE, "View map"),READ_ASSETS(Constants.READ_ASSETS_ROLE, "Read asset data"),READ_RULES(Constants.READ_RULES_ROLE, "Read rulesets"),- READ_LAST_WILL(Constants.READ_LAST_WILL, "Subscribe to last will messages"),WRITE_USER(Constants.WRITE_USER_ROLE, "Write data of the authenticated user"),WRITE_ADMIN(Constants.WRITE_ADMIN_ROLE, "Write system settings, realms, and users"),@@ -54,8 +53,7 @@ public enum ClientRole {READ_USERS,READ_MAP,READ_ASSETS,- READ_RULES,- READ_LAST_WILL+ READ_RULES}),WRITE("write", "Write all data", new ClientRole[]{diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovyindex 026cfc2d8..633bbb76a 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy@@ -64,7 +64,7 @@ class AssetUserLinkingTest extends Specification implements ManagerContainerTraidef userAssetLinks = assetResource.getUserAssetLinks(null, keycloakTestSetup.realmBuilding.name, null, null)then: "result should match"- userAssetLinks.length == 9+ userAssetLinks.length == 10userAssetLinks.any {it.id.realm == keycloakTestSetup.realmBuilding.name &&it.id.userId == keycloakTestSetup.testuser3Id &&diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 32df2f43f..60107b805 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -15,7 +15,6 @@ import org.openremote.manager.agent.AgentServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.event.ClientEventServiceimport org.openremote.manager.mqtt.DefaultMQTTHandler-import org.openremote.manager.mqtt.LastWillMQTTHandlerimport org.openremote.manager.mqtt.MQTTHandlerimport org.openremote.manager.mqtt.MqttBrokerServiceimport org.openremote.manager.setup.SetupService@@ -162,7 +161,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}receivedEvents.clear()- when: "a mqtt client publishes to an asset attribute which is readonly"+ when: "a mqtt client publishes to an asset attribute"topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()def payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 70)).get()client.sendMessage(new MQTTMessage<String>(topic, payload))@@ -179,7 +178,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}receivedEvents.clear()- when: "a mqtt client publishes to an asset attribute which is not readonly"+ when: "a mqtt client publishes to an asset attribute"payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "lights", false)).get()client.sendMessage(new MQTTMessage<String>(topic, payload))@@ -195,7 +194,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {}receivedEvents.clear()- when: "a mqtt client publishes to an asset attribute value which is not readonly"+ when: "a mqtt client publishes to an asset attribute value"topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/lights/$managerTestSetup.apartment1HallwayId".toString()payload = ValueUtil.asJSON(true).orElse(null)client.sendMessage(new MQTTMessage<String>(topic, payload))@@ -447,9 +446,11 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert receivedEvents.size() == 0}- when: "another client connects with a last will configured"+ when: "another client connects with a last will configured to write to an attribute"newClientId = "newClient"- MQTT_IOClient newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/${LastWillMQTTHandler.LAST_WILL_TOKEN}/new_device", ValueUtil.parse("123").orElse(null), false))+ username = keycloakTestSetup.realmBuilding.name + ":" + keycloakTestSetup.serviceUser2.username // realm and OAuth client id+ password = keycloakTestSetup.serviceUser2.secret+ MQTT_IOClient newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/motionSensor/$managerTestSetup.apartment1HallwayId".toString(), ValueUtil.parse("1000").orElse(null), false))newClient.connect()then: "the client should be connected"@@ -457,9 +458,26 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert newClient.getConnectionStatus() == ConnectionStatus.CONNECTED}- when: "the existing client subscribes to the last will topic"- MQTTMessage<String> receivedLastWillMessage- client.addMessageConsumer("${keycloakTestSetup.realmBuilding.name}/${LastWillMQTTHandler.LAST_WILL_TOKEN}/#", {receivedLastWillMessage = it})+ when: "the existing client subscribes to attribute events in the hallway"+ topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_TOPIC/$MQTTHandler.TOKEN_SINGLE_LEVEL_WILDCARD/$managerTestSetup.apartment1HallwayId".toString()+ client.addMessageConsumer(topic, eventConsumer)++ and: "the new client publishes to a an attribute topic"+ topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()+ payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 170)).get()+ client.sendMessage(new MQTTMessage<String>(topic, payload))++ then: "The value of the attribute should be updated and the first client should have received the event"+ new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {+ def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)+ assert asset.getAttribute("motionSensor").get().value.orElse(0) == 170d+ assert receivedEvents.size() == 1+ assert receivedEvents.get(0) instanceof AttributeEvent+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 170d+ }+ receivedEvents.clear()and: "the new client gets abruptly disconnected"def existingConnection = mqttBrokerService.clientIdConnectionMap.get(newClientId)@@ -472,11 +490,71 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert mqttBrokerService.clientIdConnectionMap.get(newClientId) !== existingConnection}- and: "the existing client should have received the last will message"+ and: "The last will message should have updated the value of the attribute and the first client should have received the event"+ new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {+ def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)+ assert asset.getAttribute("motionSensor").get().value.orElse(0) == 1000d+ assert receivedEvents.size() == 1+ assert receivedEvents.get(0) instanceof AttributeEvent+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 1000d+ }+ receivedEvents.clear()++ when: "the new client tries to publish again"+ topic = "${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_WRITE_TOPIC".toString()+ payload = ValueUtil.asJSON(new AttributeEvent(managerTestSetup.apartment1HallwayId, "motionSensor", 170)).get()+ client.sendMessage(new MQTTMessage<String>(topic, payload))++ then: "The value of the attribute should be updated and the first client should have received the event"+ new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {+ def asset = assetStorageService.find(managerTestSetup.apartment1HallwayId)+ assert asset.getAttribute("motionSensor").get().value.orElse(0) == 170d+ assert receivedEvents.size() == 1+ assert receivedEvents.get(0) instanceof AttributeEvent+ assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 170d+ }+ receivedEvents.clear()++ when: "the new client disconnects"+ newClient.disconnect()++ then: "the status should show as disconnected"+ conditions.eventually {+ assert newClient.getConnectionStatus() == ConnectionStatus.DISCONNECTED+ assert mqttBrokerService.clientIdConnectionMap.size() == 1+ }++ when: "the new client reconnects using a last will topic for an unauthorised asset/attribute"+ def asset = assetStorageService.find(managerTestSetup.apartment2LivingroomId)+ def currentCO2Level = asset.getAttribute("co2Level").flatMap{it.getValue()}.orElse(0d)+ newClient = new MQTT_IOClient(newClientId, mqttHost, mqttPort, false, true, new UsernamePassword(username, password), null, new MQTTLastWill("${keycloakTestSetup.realmBuilding.name}/$mqttClientId/$DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC/co2Level/$managerTestSetup.apartment2LivingroomId".toString(), ValueUtil.parse("1000").orElse(null), false))+ newClient.connect()++ then: "the client should be connected"conditions.eventually {- assert receivedLastWillMessage != null- assert receivedLastWillMessage.topic == "${keycloakTestSetup.realmBuilding.name}/${LastWillMQTTHandler.LAST_WILL_TOKEN}/new_device"- assert receivedLastWillMessage.payload == "123"+ assert newClient.getConnectionStatus() == ConnectionStatus.CONNECTED+ assert mqttBrokerService.clientIdConnectionMap.get(newClientId) != null+ }++ when: "the new client gets abruptly disconnected"+ existingConnection = mqttBrokerService.clientIdConnectionMap.get(newClientId)+// ((NioSocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)device1Client.client).clientConfig).delegate).connectionConfig.get()).channel).config().setOption(ChannelOption.SO_LINGER, 0I)+ ((SocketChannel)((MqttClientConnectionConfig)((MqttClientConfig)((Mqtt3ClientConfigView)((Mqtt3AsyncClientView)newClient.client).clientConfig).delegate).connectionConfig.get()).channel).close()++ then: "the client should reconnect"+ conditions.eventually {+ assert mqttBrokerService.clientIdConnectionMap.get(newClientId) != null+ assert mqttBrokerService.clientIdConnectionMap.get(newClientId) !== existingConnection+ }++ then: "The last will message should not have updated the value of the attribute"+ new PollingConditions(initialDelay: 1, timeout: 10, delay: 1).eventually {+ def asst = assetStorageService.find(managerTestSetup.apartment2LivingroomId)+ assert asst.getAttribute("co2Level").get().value.orElse(-100) == currentCO2Level}when: "both MQTT clients disconnect"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 9e95c5cf7..e16784708 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@@ -29,6 +29,7 @@ import org.openremote.manager.agent.AgentServiceimport org.openremote.manager.asset.AssetProcessingServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.event.ClientEventService+import org.openremote.manager.mqtt.DefaultMQTTHandlerimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.Constantsimport org.openremote.model.asset.agent.Agent@@ -55,7 +56,7 @@ import static org.openremote.model.value.ValueType.NUMBERclass MQTTClientProtocolTest extends Specification implements ManagerContainerTrait {@SuppressWarnings("GroovyAccessibility")- def "Check websocket client protocol and linked attribute deployment"() {+ def "Check MQTT client protocol and linked attribute deployment"() {given: "expected conditions"def conditions = new PollingConditions(timeout: 10, delay: 0.2)@@ -97,8 +98,8 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTrnew Attribute<>("readWriteTargetTemp", NUMBER).addMeta(new MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id)- .setSubscriptionTopic("${keycloakTestSetup.realmBuilding.name}/$clientId/attributevalue/targetTemperature/${managerTestSetup.apartment1LivingroomId}")- .setPublishTopic("${keycloakTestSetup.realmBuilding.name}/$clientId/attributevalue/targetTemperature/${managerTestSetup.apartment1LivingroomId}")+ .setSubscriptionTopic("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}")+ .setPublishTopic("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}").setWriteValue("${Protocol.DYNAMIC_VALUE_PLACEHOLDER}"))))@@ -111,7 +112,7 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTrassert !(agentService.getProtocolInstance(agent.id) as MQTTProtocol).protocolMessageConsumers.isEmpty()}- when: "the source attribute are updated"+ when: "the attribute referenced in the agent link is updated"((SimulatorProtocol)agentService.getProtocolInstance(managerTestSetup.apartment1ServiceAgentId)).updateSensor(new AttributeEvent(managerTestSetup.apartment1LivingroomId, "targetTemperature", 99d))then: "the values should be stored in the database"@@ -121,13 +122,13 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTrassert livingRoom.getAttribute("targetTemperature", Double.class).flatMap{it.value}.orElse(0d) == 99d}- then: "the linked attributes should also have the updated values of the subscribed attributes"+ then: "the agent linked attribute should also have the updated value of the subscribed attribute"conditions.eventually {asset = assetStorageService.find(asset.getId(), true)assert asset.getAttribute("readWriteTargetTemp").get().getValue().orElse(null) == 99d}- when: "a linked attribute value is updated"+ when: "the agent linked attribute is updated"def attributeEvent = new AttributeEvent(asset.id,"readWriteTargetTemp",19.5)diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovyindex 4e4684e49..3953baec6 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy@@ -591,14 +591,14 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {assert thingAsset.getAttribute("sunset").get().getValue().orElse(0) == 100}- when: "the updated attribute is cleared"- thingAsset.getAttribute("sunset").get().setValue(null)- thingAsset = assetStorageService.merge(thingAsset)-- and: "time advances past the next sunrise"+ when: "time advances past the next sunrise"sunTimes = sunsetCalculator.on(sunTimes.getSet()).execute()advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getRise()).getSeconds(), TimeUnit.SECONDS, container)+ and: "the updated attribute is cleared"+ thingAsset.getAttribute("sunset").get().setValue(null)+ thingAsset = assetStorageService.merge(thingAsset)+then: "the rule engine should have fired again and the rule should not have triggered"conditions.eventually {assert realmBuildingEngine.lastFireTimestamp > lastFireTimestampdiff --git a/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy b/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovyindex 52ddc7b85..7a777b27b 100644--- a/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy+++ b/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy@@ -88,22 +88,24 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {users = regularUserResource.query(null, new UserQuery().realm(new RealmPredicate(keycloakTestSetup.realmMaster.name)))then: "only non system users of the users realm should be returned"- users.size() == 4+ users.size() == 5users.count {it.isSystemAccount() && it.username == KeycloakIdentityProvider.MANAGER_CLIENT_ID} == 0users.find {it.id == keycloakTestSetup.testuser2Id} != nullusers.find {it.id == keycloakTestSetup.testuser3Id} != nullusers.find {it.id == keycloakTestSetup.buildingUserId} != nullusers.find {it.isServiceAccount() && it.id == keycloakTestSetup.serviceUser.id} != null+ users.find {it.isServiceAccount() && it.id == keycloakTestSetup.serviceUser2.id} != nullwhen: "a request is made for all users ordered by username"users = regularUserResource.query(null, new UserQuery().realm(new RealmPredicate(keycloakTestSetup.realmMaster.name)).orderBy(new UserQuery.OrderBy(UserQuery.OrderBy.Property.USERNAME, false)))then: "only non system users of the users realm should be returned in username order"- users.size() == 4+ users.size() == 5users[0].username == "building"users[1].username == keycloakTestSetup.serviceUser.username- users[2].username == "testuser2"- users[3].username == "testuser3"+ users[2].username == keycloakTestSetup.serviceUser2.username+ users[3].username == "testuser2"+ users[4].username == "testuser3"when: "a request is made for subset of users ordered by username descending"users = regularUserResource.query(null, new UserQuery().realm(new RealmPredicate(keycloakTestSetup.realmMaster.name)).orderBy(new UserQuery.OrderBy(UserQuery.OrderBy.Property.USERNAME, true)).limit(2))@@ -114,7 +116,7 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {users[1].username == "testuser2"when: "a request is made for another subset of users ordered by username descending"- users = regularUserResource.query(null, new UserQuery().realm(new RealmPredicate(keycloakTestSetup.realmMaster.name)).orderBy(new UserQuery.OrderBy(UserQuery.OrderBy.Property.USERNAME, true)).limit(2).offset(2))+ users = regularUserResource.query(null, new UserQuery().realm(new RealmPredicate(keycloakTestSetup.realmMaster.name)).orderBy(new UserQuery.OrderBy(UserQuery.OrderBy.Property.USERNAME, true)).limit(2).offset(3))then: "the correct users should be returned"users.size() == 2@@ -125,9 +127,11 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {users = adminUserResource.query(null, new UserQuery().select(new UserQuery.Select().excludeRegularUsers(true)).realm(new RealmPredicate(keycloakTestSetup.realmBuilding.name)))then: "only service users of the requested realm should be returned"- users.size() == 1+ users.size() == 2users.find {it.isServiceAccount() && it.id == keycloakTestSetup.serviceUser.id} != null+ users.find {it.isServiceAccount() && it.id == keycloakTestSetup.serviceUser2.id} != nullusers.find {it.isServiceAccount() && it.id == keycloakTestSetup.serviceUser.id}.secret != null+ users.find {it.isServiceAccount() && it.id == keycloakTestSetup.serviceUser2.id}.secret != null}def "Get and update roles"() {diff --git a/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java b/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.javaindex 993a16938..780288cc8 100644--- a/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java+++ b/test/src/test/java/org/openremote/test/setup/KeycloakTestSetup.java@@ -59,6 +59,7 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {public Realm realmEnergy;public Realm realmCity;public User serviceUser;+ public User serviceUser2;public KeycloakTestSetup(Container container) {super(container);@@ -102,18 +103,29 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {keycloakProvider.updateUserRoles(realmCity.getName(), smartCityUserId, "account"); // Remove all roles for account client/*- * Service user client+ * Service user clients*/serviceUser = new User().setServiceAccount(true).setEnabled(true)- .setUsername("org/openremote/test");+ .setUsername("serviceuser");serviceUser = keycloakProvider.createUpdateUser(realmBuilding.getName(), serviceUser, UniqueIdentifierGenerator.generateId("serviceusertest"));keycloakProvider.updateUserRoles(realmBuilding.getName(),serviceUser.getId(),Constants.KEYCLOAK_CLIENT_ID,- Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES, ClientRole.READ_LAST_WILL).map(ClientRole::getValue).toArray(String[]::new)+ Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES).map(ClientRole::getValue).toArray(String[]::new)+ );+ serviceUser2 = new User()+ .setServiceAccount(true)+ .setEnabled(true)+ .setUsername("serviceuser2");+ serviceUser2 = keycloakProvider.createUpdateUser(realmBuilding.getName(), serviceUser2, UniqueIdentifierGenerator.generateId("serviceuser2test"));+ keycloakProvider.updateUserRoles(+ realmBuilding.getName(),+ serviceUser2.getId(),+ Constants.KEYCLOAK_CLIENT_ID,+ Stream.of(ClientRole.READ_ASSETS, ClientRole.WRITE_ASSETS, ClientRole.WRITE_ATTRIBUTES).map(ClientRole::getValue).toArray(String[]::new));}}diff --git a/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java b/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.javaindex 0beb4a07c..5c3f84471 100644--- a/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java+++ b/test/src/test/java/org/openremote/test/setup/ManagerTestSetup.java@@ -40,6 +40,7 @@ import org.openremote.model.value.ValueType;import org.openremote.model.value.impl.ColourRGB;import java.util.Arrays;+import java.util.List;import static org.openremote.manager.datapoint.AssetDatapointService.OR_DATA_POINTS_MAX_AGE_DAYS_DEFAULT;import static org.openremote.model.Constants.*;@@ -597,10 +598,16 @@ public class ManagerTestSetup extends ManagerSetup {keycloakTestSetup.buildingUserId,apartment2BathroomId)));+ assetStorageService.storeUserAssetLinks(List.of(+ new UserAssetLink(keycloakTestSetup.realmBuilding.getName(),+ keycloakTestSetup.serviceUser2.getId(),+ apartment1HallwayId)));+// ################################ Make users restricted ###################################ManagerIdentityProvider identityProvider = identityService.getIdentityProvider();identityProvider.updateUserRealmRoles(realmBuilding.getName(), keycloakTestSetup.testuser3Id, identityProvider.addRealmRoles(realmBuilding.getName(), keycloakTestSetup.testuser3Id, RESTRICTED_USER_REALM_ROLE));identityProvider.updateUserRealmRoles(realmBuilding.getName(), keycloakTestSetup.buildingUserId, identityProvider.addRealmRoles(realmBuilding.getName(), keycloakTestSetup.buildingUserId, RESTRICTED_USER_REALM_ROLE));+ identityProvider.updateUserRealmRoles(realmBuilding.getName(), keycloakTestSetup.serviceUser2.getId(), identityProvider.addRealmRoles(realmBuilding.getName(), keycloakTestSetup.serviceUser2.getId(), RESTRICTED_USER_REALM_ROLE));// ################################ Realm smartcity ###################################