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
Bug fixes and cleanup of event processing
Rich Turnerc3d0843241e6
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex 885f28f56..6b5609d10 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -503,7 +503,12 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerattribute.getName(),attribute.getValue().orElse(null),timerService.getCurrentTimeMillis()- ).setParentId(asset.getParentId()).setRealm(asset.getRealm()).setPath(asset.getPath())+ )+ .setParentId(asset.getParentId())+ .setRealm(asset.getRealm())+ .setPath(asset.getPath())+ .setAccessRestrictedRead(attribute.getMetaValue(MetaItemType.ACCESS_RESTRICTED_READ).orElse(false))+ .setAccessPublicRead(attribute.getMetaValue(MetaItemType.ACCESS_PUBLIC_READ).orElse(false)));}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 32b27b07c..4e5d898ff 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -54,6 +54,7 @@ import org.openremote.model.security.User;import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;+import org.openremote.model.value.MetaItemType;import org.postgresql.util.PGobject;import javax.persistence.EntityManager;@@ -190,7 +191,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicfilter.setRestrictedEvents(true);}- // Restricted user can only subscribe to assets they are linked to+ // Restricted user can only subscribe to assets they are linked to so go fetch these+ // TODO: Update asset IDs when user asset links are modifiedboolean skipAssetIdCheck = false;if (isRestricted && (filter.getAssetIds() == null || filter.getAssetIds().length == 0)) {filter.setAssetIds(@@ -213,7 +215,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// Restricted users can only get events for their linked assetsif (!assetStorageService.isUserAsset(userId, assetId))return false;- // TODO Restricted clients should only receive events for PROTECTED attributes!} else {// Regular users can only get events for assets in their realmif (!asset.getRealm().equals(realm))@@ -392,26 +393,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicString sessionKey = ClientEventService.getSessionKey(exchange);AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);boolean isAttributeRead = !TextUtil.isNullOrEmpty(attributeName);- boolean isAnonymous = authContext == null;- boolean isSuperUser = authContext != null && authContext.isSuperUser();- boolean isRestricted = identityService.getIdentityProvider().isRestrictedUser(authContext);-- Access access = isAnonymous ? PUBLIC : isRestricted ? PROTECTED : PRIVATE;-- // Superuser can get all, User must have role- if (!isSuperUser && access != PUBLIC && !authContext.hasResourceRole(ClientRole.READ_ASSETS.getValue(), authContext.getClientId())) {- LOG.fine("User must have '" + ClientRole.READ_ASSETS.getValue() + "' role to read assets and/or attributes");- return;- }+ String requestRealm = exchange.getIn().getHeader(Constants.REALM_PARAM_NAME, String.class);AssetQuery assetQuery = new AssetQuery().ids(assetId)- .select(new Select().excludePath(false).excludeParentInfo(true))- .access(access);+ .select(new Select().excludePath(false).excludeParentInfo(true));- if (access == PROTECTED) {- assetQuery.userIds(authContext.getUserId());- }+ assetQuery = prepareAssetQuery(assetQuery, authContext, requestRealm);Asset<?> asset = find(assetQuery);@@ -422,10 +410,21 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicif (isAttributeRead) {Attribute<?> assetAttribute = asset.getAttributes().get(attributeName).orElse(null);if (assetAttribute != null) {- response = new AttributeEvent(assetId, attributeName, assetAttribute.getValue().orElse(null), assetAttribute.getTimestamp().orElse(0L));++ // Check access constraints+ if (assetQuery.access == null+ || assetQuery.access == PRIVATE+ || (assetQuery.access == PUBLIC && assetAttribute.getMetaValue(ACCESS_PUBLIC_READ).orElse(false))+ || (assetQuery.access == PROTECTED && assetAttribute.getMetaValue(ACCESS_RESTRICTED_READ).orElse(false))) {+ response = new AttributeEvent(assetId, attributeName, assetAttribute.getValue().orElse(null), assetAttribute.getTimestamp().orElse(0L));+ }}} else {- response = new AssetEvent(AssetEvent.Cause.READ, asset, null);++ // Check access constraints+ if (assetQuery.access != PUBLIC || asset.isAccessPublicRead()) {+ response = new AssetEvent(AssetEvent.Cause.READ, asset, null);+ }}if (response != null) {@@ -834,19 +833,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicupdatedAsset = gatewayService.mergeGatewayAsset(gatewayId, asset);} else {updatedAsset = em.merge(asset);- if (existingAsset == null) {- if (LOG.isLoggable(Level.FINER)) {- LOG.finer("Asset created: " + updatedAsset.toStringAll());- } else {- LOG.fine("Asset created: " + updatedAsset);- }- } else {- if (LOG.isLoggable(Level.FINER)) {- LOG.finer("Asset updated: new = " + updatedAsset.toStringAll() + ", old = " + existingAsset.toStringAll());- } else {- LOG.fine("Asset updated: " + updatedAsset);- }- }}if (user != null) {@@ -947,10 +933,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}assets.sort(Comparator.comparingInt((Asset<?> asset) -> asset.getPath() == null ? 0 : asset.getPath().length).reversed());- assets.forEach(asset -> {- em.remove(asset);- LOG.fine("Asset deleted: " + asset);- });+ assets.forEach(em::remove);em.flush();});} catch (Exception e) {@@ -1342,6 +1325,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn;}+ if (LOG.isLoggable(Level.FINER)) {+ LOG.finer("Asset created: " + loadedAsset.toStringAll());+ } else {+ LOG.fine("Asset created: " + loadedAsset);+ }+clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.CREATE, loadedAsset, null));@@ -1370,6 +1359,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic// Fully load the assetloadedAsset = find(new AssetQuery().ids(asset.getId()));+ if (loadedAsset == null) {+ return;+ }++ LOG.finer("Asset updated: " + persistenceEvent);+clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, updatedProperties));@@ -1406,6 +1401,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicbreak;case DELETE:+ if (LOG.isLoggable(Level.FINER)) {+ LOG.finer("Asset deleted: " + asset.toStringAll());+ } else {+ LOG.fine("Asset deleted: " + asset);+ }+clientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.DELETE, asset, null));diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.javaindex edddb90fb..59e9aae95 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java@@ -72,6 +72,8 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements Cpublic void upsertValue(String assetId, String attributeName, Object value, LocalDateTime timestamp) throws IllegalStateException {persistenceService.doTransaction(em ->em.unwrap(Session.class).doWork(connection -> {++ getLogger().finest("Storing datapoint for: id=" + assetId + ", name=" + attributeName + ", timestamp=" + timestamp + ", value=" + value);PreparedStatement st;try {diff --git a/manager/src/main/resources/logging-dev.properties b/manager/src/main/resources/logging-dev.propertiesindex 3f1ccf29e..7cd292f73 100644--- a/manager/src/main/resources/logging-dev.properties+++ b/manager/src/main/resources/logging-dev.properties@@ -88,6 +88,7 @@ org.hibernate.type.descriptor.sql.BasicBinder.level=WARNINGorg.openremote.container.web.level=INFOorg.openremote.manager.server.level=INFO+org.openremote.manager.datapoint.level=FINEST# Low-level debugging overrides, most of them are to stop chatty categoriesorg.hibernate.level=WARNINGdiff --git a/model/src/main/java/org/openremote/model/asset/AssetEvent.java b/model/src/main/java/org/openremote/model/asset/AssetEvent.javaindex 9ed711ac0..94e9ec2c4 100644--- a/model/src/main/java/org/openremote/model/asset/AssetEvent.java+++ b/model/src/main/java/org/openremote/model/asset/AssetEvent.java@@ -93,6 +93,16 @@ public class AssetEvent extends SharedEvent implements AssetInfo {return updatedProperties;}+ @Override+ public boolean canAccessPublicRead() {+ return asset.isAccessPublicRead();+ }++ @Override+ public boolean canAccessRestrictedRead() {+ return true;+ }+@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.javaindex 20b05de35..e9b063393 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java@@ -20,6 +20,7 @@package org.openremote.model.attribute;import com.fasterxml.jackson.annotation.JsonCreator;+import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.annotation.JsonProperty;import org.openremote.model.event.shared.AssetInfo;import org.openremote.model.event.shared.SharedEvent;@@ -73,6 +74,10 @@ public class AttributeEvent extends SharedEvent implements AssetInfo {protected String realm;protected String parentId;protected String[] path;+ @JsonIgnore+ protected boolean publicRead;+ @JsonIgnore+ protected boolean restrictedRead;public <T> AttributeEvent(String assetId, AttributeDescriptor<T> attributeDescriptor, T value) {this(assetId, attributeDescriptor.getName(), value);@@ -167,6 +172,26 @@ public class AttributeEvent extends SharedEvent implements AssetInfo {return getAttributeState().getValue();}+ public AttributeEvent setAccessPublicRead(boolean publicRead) {+ this.publicRead = publicRead;+ return this;+ }++ public AttributeEvent setAccessRestrictedRead(boolean restrictedRead) {+ this.restrictedRead = restrictedRead;+ return this;+ }++ @Override+ public boolean canAccessPublicRead() {+ return publicRead;+ }++ @Override+ public boolean canAccessRestrictedRead() {+ return restrictedRead;+ }+@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.javaindex 7828e314c..f2946a909 100644--- a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java+++ b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java@@ -19,6 +19,7 @@*/package org.openremote.model.event.shared;+import com.fasterxml.jackson.annotation.JsonIgnore;import com.fasterxml.jackson.annotation.JsonSubTypes;import com.fasterxml.jackson.annotation.JsonTypeInfo;import org.openremote.model.asset.*;@@ -75,5 +76,4 @@ public abstract class SharedEvent extends Event {public boolean canAccessRestrictedRead() {return false;}-}diff --git a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovyindex a39ad492b..586a3c687 100644--- a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy+++ b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy@@ -336,6 +336,7 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaibatteryAsset.setEnergyLevel(130d)batteryAsset = assetStorageService.merge(batteryAsset)optimisationService.runOptimisation(managerTestSetup.electricityOptimisationAssetId, optimisationTime.plus((long)optimiser.intervalSize*60*3, ChronoUnit.MINUTES))+ println("DONE")then: "the optimisation saving should have decreased by the same amount (i.e. cost)"conditions.eventually {diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovyindex f4bfd2612..57f99bda7 100644--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy@@ -195,9 +195,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {when: "a regular user sends a push notification to an entire realm"testuser2NotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"WebApplicationException ex = thrown()- ex.response.status == 403+ ex.response.status == 400when: "the admin user sends a notification to a user in a different realm with emailNotificationsDisabled set to true"notification.targets = [new Notification.Target(Notification.TargetType.USER, keycloakTestSetup.testuser2Id)]@@ -223,24 +223,24 @@ class NotificationTest extends Specification implements ManagerContainerTrait {notification.targets = [new Notification.Target(Notification.TargetType.USER, keycloakTestSetup.testuser2Id)]testuser1NotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"ex = thrown()- ex.response.status == 403+ ex.response.status == 400when: "a restricted user sends a push notification to another user in the same realm"notification.targets = [new Notification.Target(Notification.TargetType.USER, keycloakTestSetup.testuser2Id)]testuser3NotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"ex = thrown()- ex.response.status == 403+ ex.response.status == 400when: "an anonymous user sends a push notification to a user"anonymousNotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"ex = thrown()- ex.response.status == 403+ ex.response.status == 400when: "the admin user sends a push notification to the console assets in building realm"notification.targets = [new Notification.Target(Notification.TargetType.ASSET, testuser2Console.id),@@ -259,9 +259,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {when: "a regular user sends a push notification to the console assets in a different realm"testuser1NotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"ex = thrown()- ex.response.status == 403+ ex.response.status == 400when: "a regular user sends a push notification to the console assets in the same realm"advancePseudoClock(1, TimeUnit.HOURS, container)@@ -285,9 +285,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {when: "a restricted user sends a push notification to the console assets in the same realm"testuser3NotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"ex = thrown()- ex.response.status == 403+ ex.response.status == 400when: "a restricted user sends a push notification to some consoles linked to them and some not linked to them"notification.targets = [new Notification.Target(Notification.TargetType.ASSET, testuser2Console.id),@@ -296,9 +296,9 @@ class NotificationTest extends Specification implements ManagerContainerTrait {new Notification.Target(Notification.TargetType.ASSET, anonymousConsole.id)]testuser3NotificationResource.sendNotification(null, notification)- then: "access should be forbidden"+ then: "no notification should have been sent"ex = thrown()- ex.response.status == 403+ ex.response.status == 400and: "no new notifications should have been sent"notificationIds.size() == 14