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
Added write functionality for MQTT connections
Michael Rademakerabda6d1e9ee3
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.javaindex 9acbceac3..9052aaf6a 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/EventInterceptHandler.java@@ -35,11 +35,12 @@ import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.Constants;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.RenewEventSubscriptions;+import org.openremote.model.value.Values;+import java.nio.charset.Charset;import java.nio.charset.StandardCharsets;import java.util.HashMap;import java.util.Map;@@ -78,7 +79,8 @@ public class EventInterceptHandler extends AbstractInterceptHandler {InterceptDisconnectMessage.class,InterceptConnectionLostMessage.class,InterceptSubscribeMessage.class,- InterceptUnsubscribeMessage.class+ InterceptUnsubscribeMessage.class,+ InterceptPublishMessage.class};}@@ -159,6 +161,23 @@ public class EventInterceptHandler extends AbstractInterceptHandler {}}+ @Override+ public void onPublish(InterceptPublishMessage msg) {+ MqttConnection connection = mqttConnectionMap.get(msg.getClientID());+ if (connection != null) {+ String[] topicParts = msg.getTopicName().split(TOPIC_SEPARATOR);+ String assetId = topicParts[1];+ if (connection.assetSubscriptions.containsKey(assetId)) {+ String payloadContent = msg.getPayload().toString(Charset.defaultCharset());+ Values.parse(payloadContent).flatMap(Values::getObject).ifPresent(objectValue -> {+ Map<String, Object> headers = prepareHeaders(connection);+ AttributeEvent attributeEvent = new AttributeEvent(assetId, objectValue.keys()[0], objectValue.get(objectValue.keys()[0]).orElse(null));+ messageBrokerService.getProducerTemplate().sendBodyAndHeaders(ClientEventService.CLIENT_EVENT_QUEUE, attributeEvent, headers);+ });+ }+ }+ }+private Map<String, Object> prepareHeaders(MqttConnection connection) {Map<String, Object> headers = new HashMap<>();headers.put(ConnectionConstants.SESSION_KEY, connection.clientId);diff --git a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.javaindex be5a04ed9..7b29f32d5 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/KeycloakAuthorizatorPolicy.java@@ -6,6 +6,7 @@ import org.keycloak.adapters.rotation.AdapterTokenVerifier;import org.keycloak.common.VerificationException;import org.keycloak.exceptions.TokenNotActiveException;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.event.ClientEventService;@@ -13,9 +14,11 @@ import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.asset.AssetFilter;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.shared.EventSubscription;+import org.openremote.model.event.shared.TenantFilter;import org.openremote.model.security.ClientRole;import java.nio.charset.StandardCharsets;+import java.util.Arrays;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;@@ -71,22 +74,15 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}- String assetId = topic.getTokens().get(1).toString();- EventSubscription<AttributeEvent> subscription = new EventSubscription<>(- AttributeEvent.class,- new AssetFilter<AttributeEvent>().setRealm(connection.realm).setAssetIds(assetId)- );-+ AccessToken accessToken = null;try {- AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));- return clientEventService.authorizeEventSubscription(new AccessTokenAuthContext(connection.realm, accessToken), subscription);+ accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));} catch (VerificationException e) {if (e instanceof TokenNotActiveException) {String suppliedClientSecret = new String(connection.password, StandardCharsets.UTF_8);connection.accessToken = identityProvider.getExternalKeycloak().getAccessToken(connection.realm, new ClientCredentialsAuthForm(connection.username, suppliedClientSecret)).getToken();try {- AccessToken accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));- return clientEventService.authorizeEventSubscription(new AccessTokenAuthContext(connection.realm, accessToken), subscription);+ accessToken = AdapterTokenVerifier.verifyToken(connection.accessToken, identityProvider.getKeycloakDeployment(connection.realm, KEYCLOAK_CLIENT_ID));} catch (VerificationException verificationException) {LOG.log(Level.INFO, "Couldn't verify token", verificationException);return false;@@ -96,5 +92,17 @@ public class KeycloakAuthorizatorPolicy implements IAuthorizatorPolicy {return false;}}++ AuthContext authContext = new AccessTokenAuthContext(connection.realm, accessToken);+ if (Arrays.asList(roles).contains(ClientRole.WRITE_ASSETS)) { //write+ return identityProvider.canSubscribeWith(authContext, new TenantFilter(connection.realm), roles);+ } else { // read+ String assetId = topic.getTokens().get(1).toString();+ EventSubscription<AttributeEvent> subscription = new EventSubscription<>(+ AttributeEvent.class,+ new AssetFilter<AttributeEvent>().setRealm(connection.realm).setAssetIds(assetId)+ );+ 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 6f7b51a78..0358a76e9 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java@@ -19,7 +19,6 @@*/package org.openremote.manager.mqtt;-import com.google.inject.internal.cglib.proxy.$FixedValue;import io.moquette.BrokerConstants;import io.moquette.broker.Server;import io.moquette.broker.config.MemoryConfig;@@ -36,9 +35,7 @@ import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.security.ManagerKeycloakIdentityProvider;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.event.TriggeredEventSubscription;-import org.openremote.model.value.ArrayValue;import org.openremote.model.value.ObjectValue;-import org.openremote.model.value.Value;import org.openremote.model.value.Values;import java.nio.charset.Charset;@@ -62,8 +59,6 @@ public class MqttBrokerService implements ContainerService {public static final String ASSETS_TOPIC = "assets";public static final String TOPIC_SEPARATOR = "/";- public static final String PAYLOAD_SEPARATOR = ":";-protected ManagerKeycloakIdentityProvider identityProvider;protected ClientEventService clientEventService;protected MessageBrokerService messageBrokerService;@@ -139,8 +134,9 @@ public class MqttBrokerService implements ContainerService {}public void sendAttributeEvent(String clientId, AttributeEvent attributeEvent) {- ByteBuf payload = Unpooled.copiedBuffer(attributeEvent.getAttributeName() + PAYLOAD_SEPARATOR, Charset.defaultCharset());- attributeEvent.getValue().ifPresent(value -> payload.writeCharSequence(value.toJson(), Charset.defaultCharset()));+ ObjectValue payloadContent = Values.createObject();+ payloadContent.put(attributeEvent.getAttributeName(), attributeEvent.getValue().orElse(null));+ ByteBuf payload = Unpooled.copiedBuffer(payloadContent.toJson(), Charset.defaultCharset());MqttPublishMessage publishMessage = MqttMessageBuilders.publish().qos(MqttQoS.AT_MOST_ONCE)diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovyindex 9f61dab41..5b101a9cb 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttTest.groovy@@ -4,6 +4,7 @@ import com.google.common.collect.Listsimport io.moquette.BrokerConstantsimport org.openremote.container.util.UniqueIdentifierGeneratorimport org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.mqtt.MqttBrokerServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.manager.setup.builtin.ManagerDemoSetup@@ -14,13 +15,10 @@ import org.openremote.test.RawClientimport spock.lang.Specificationimport spock.util.concurrent.PollingConditions-import java.nio.charset.Charset-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.PAYLOAD_SEPARATORclass MqttTest extends Specification implements ManagerContainerTrait {def "Mqtt broker test"() {@@ -43,6 +41,7 @@ class MqttTest extends Specification implements ManagerContainerTrait {def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)def mqttBrokerService = container.getService(MqttBrokerService.class)def assetProcessingService = container.getService(AssetProcessingService.class)+ def assetStorageService = container.getService(AssetStorageService.class)def mqttClientId = managerDemoSetup.realmBuildingTenant + MQTT_CLIENT_ID_SEPARATOR + UniqueIdentifierGenerator.generateId()def clientId = MqttBrokerService.MQTT_CLIENT_ID_PREFIX + UniqueIdentifierGenerator.generateId(managerDemoSetup.realmBuildingTenant)def clientSecret = UniqueIdentifierGenerator.generateId(managerDemoSetup.realmBuildingTenant)@@ -128,7 +127,7 @@ class MqttTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an asset in another realm"- def topic = "assets/" + managerDemoSetup.thingId + "/" + managerDemoSetup.thingLightToggleAttributeName+ def topic = "assets/" + managerDemoSetup.thingIdremainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -149,7 +148,7 @@ class MqttTest extends Specification implements ManagerContainerTrait {}when: "a mqtt client subscribes to an non existing asset"- topic = "assets/dhajkdasjfgh/"+ topic = "assets/dhajkdasjfgh"remainingLength = 4 + topic.size() + 1 //plus one for the QoS byteclient@@ -208,6 +207,45 @@ class MqttTest extends Specification implements ManagerContainerTrait {assert mqttBrokerServiceAttributeEventCalls == 2}+ when: "a mqtt client publishes to an asset attribute which is readonly"+ topic = "assets/" + managerDemoSetup.apartment1HallwayId+ def payload = Values.createObject().put("motionSensor", 70).toJson()+ remainingLength = 2 + topic.size() + payload.length()++ //PUBLISH+ client.write(0x30) // MQTT Control Packet type(10) with QoS level 0+ .write(remainingLength.byteValue()) // Remaining Length+ .write(0x00, topic.size().byteValue()) // Topic Length+ .write(topic) // Topic+ .write(payload) // content+ .flush()++ then: "The value of the attribute shouldn't be updated"+ conditions.eventually {+ def asset = assetStorageService.find(managerDemoSetup.apartment1HallwayId)+ assert asset.getAttribute("motionSensor").get().valueAsNumber.orElse(0) == 50d+ }++ when: "a mqtt client publishes to an asset attribute"+ topic = "assets/" + managerDemoSetup.apartment1HallwayId+ payload = Values.createObject().put("lights", false).toJson()+ remainingLength = 2 + topic.size() + payload.length()++ //PUBLISH+ client.write(0x30) // MQTT Control Packet type(10) with QoS level 0+ .write(remainingLength.byteValue()) // Remaining Length+ .write(0x00, topic.size().byteValue()) // Topic Length+ .write(topic) // Topic+ .write(payload) // content+ .flush()++ then: "The value of the attribute should be updated and another publish event should be sent"+ conditions.eventually {+ def asset = assetStorageService.find(managerDemoSetup.apartment1HallwayId)+ assert !asset.getAttribute("lights").get().valueAsBoolean.orElse(true)+ assert mqttBrokerServiceAttributeEventCalls == 3+ }+when: "a mqtt client unsubscribes to an asset"topic = "assets/" + managerDemoSetup.apartment1HallwayIdremainingLength = 4 + topic.size()@@ -235,7 +273,7 @@ class MqttTest extends Specification implements ManagerContainerTrait {then: "No publish message should be sent"conditions.eventually {- assert mqttBrokerServiceAttributeEventCalls == 2+ assert mqttBrokerServiceAttributeEventCalls == 3}cleanup: "the server should be stopped"