Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Rules global lock removed and various other minor changes

Rich Turnerc6e5395ca6c1
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 38e5591cd..877affbca 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -42,12 +42,7 @@ import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.Pair;
import org.openremote.model.util.TextUtil;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
+import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
index e3d017378..8fcbd6951 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
@@ -46,10 +46,8 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.CopyOnWriteArraySet;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -70,9 +68,10 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
protected URI websocketURI;
protected Mqtt3AsyncClient client;
protected final Set<Consumer<ConnectionStatus>> connectionStatusConsumers = new CopyOnWriteArraySet<>();
- protected final Map<String, Set<Consumer<MQTTMessage<S>>>> topicConsumerMap = new HashMap<>();
+ protected final ConcurrentMap<String, Set<Consumer<MQTTMessage<S>>>> topicConsumerMap = new ConcurrentHashMap<>();
protected ScheduledExecutorService executorService;
protected boolean disconnected = true; // Need to use this flag to cancel client reconnect task
+ protected final AtomicBoolean connected = new AtomicBoolean(false); // Used for subscriptions
protected Consumer<String> topicSubscribeFailureConsumer;
protected AbstractMQTT_IOClient(String host, int port, boolean secure, boolean cleanSession, UsernamePassword usernamePassword, URI websocketURI, MQTTLastWill lastWill) {
@@ -190,14 +189,12 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
addMessageConsumer("#", messageConsumer);
}
- public synchronized boolean addMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
+ public boolean addMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
if (client == null) {
return false;
}
- Set<Consumer<MQTTMessage<S>>> consumers;
-
- consumers = topicConsumerMap.computeIfAbsent(topic, (t) -> new CopyOnWriteArraySet<>());
+ Set<Consumer<MQTTMessage<S>>> consumers = topicConsumerMap.computeIfAbsent(topic, (t) -> new HashSet<>());
if (consumers.isEmpty()) {
// Create the subscription on the client
@@ -224,7 +221,14 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
}
}
- protected synchronized boolean doClientSubscription(String topic, Set<Consumer<MQTTMessage<S>>> consumers) {
+ protected boolean doClientSubscription(String topic, Set<Consumer<MQTTMessage<S>>> consumers) {
+ synchronized (connected) {
+ if (!connected.get()) {
+ // Just return true and let connection logic sort out actual subscription
+ return true;
+ }
+ }
+
Consumer<MQTTMessage<S>> messageConsumer = message -> {
if (!topicConsumerMap.containsKey(topic)) {
return;
@@ -238,11 +242,6 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
});
};
- if (getConnectionStatus() != ConnectionStatus.CONNECTED) {
- // Just return true and let connection logic sort out actual subscription
- return true;
- }
-
try {
Mqtt3SubAck subAck = client.subscribeWith()
.topicFilter(topic)
@@ -273,7 +272,7 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
removeMessageConsumer("#", messageConsumer);
}
- public synchronized void removeMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
+ public void removeMessageConsumer(String topic, Consumer<MQTTMessage<S>> messageConsumer) {
topicConsumerMap.computeIfPresent(topic, (t, consumers) -> {
if (consumers.remove(messageConsumer) && consumers.isEmpty()) {
removeSubscription(topic);
@@ -284,7 +283,7 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
}
@Override
- public synchronized void removeAllMessageConsumers() {
+ public void removeAllMessageConsumers() {
Set<String> topics = new HashSet<>(topicConsumerMap.keySet());
topicConsumerMap.clear();
topics.forEach(this::removeSubscription);
@@ -339,11 +338,18 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
@Override
public void connect() {
- if (client == null) {
- return;
- }
+ synchronized (this) {
+ if (client == null) {
+ return;
+ }
+ if (!disconnected) {
+ LOG.finer("Must be disconnected before calling connect: " + getClientUri());
+ return;
+ }
- this.disconnected = false;
+ LOG.fine("Connecting MQTT Client: " + getClientUri());
+ this.disconnected = false;
+ }
LOG.info("Establishing connection: " + getClientUri());
@@ -362,20 +368,22 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
if (throwable != null) {
LOG.log(Level.INFO, "Connection failed:" + getClientUri(), throwable);
} else {
+ synchronized (connected) {
+ connected.set(true);
+
+ if (!this.cleanSession && !connAck.isSessionPresent()) {
+ // Need to re-instate the subscriptions as HiveMQ client doesn't do it
+ Map<String, Set<Consumer<MQTTMessage<S>>>> existingSubscriptions = new HashMap<>(topicConsumerMap);
- if (!this.cleanSession && !connAck.isSessionPresent()) {
- // Need to re-instate the subscriptions as HiveMQ client doesn't do it
- executorService.execute(() -> {
- // Re-add all subscriptions
- synchronized (this) {
- // Clone the map as subscribe failures will modify the map
- new HashMap<>(topicConsumerMap).forEach((topic, consumers) -> {
+ executorService.execute(() -> {
+ // Re-add all subscriptions
+ existingSubscriptions.forEach((topic, consumers) -> {
if (!doClientSubscription(topic, consumers)) {
topicConsumerMap.remove(topic);
}
});
- }
- });
+ });
+ }
}
}
});
@@ -403,12 +411,19 @@ public abstract class AbstractMQTT_IOClient<S> implements IOClient<MQTTMessage<S
return;
}
- LOG.finest("Disconnecting IO client: " + getClientUri());
- client.disconnect();
+ synchronized (this) {
+ if (disconnected) {
+ LOG.finest("Already disconnected: " + getClientUri());
+ return;
+ }
- this.disconnected = true;
+ LOG.finest("Disconnecting IO client: " + getClientUri());
+ this.disconnected = true;
+ }
client.disconnect().whenComplete((unused, throwable) -> {
+ connected.set(false);
+
if (throwable instanceof MqttClientStateException) {
// Likely already disconnected
return;
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.java
index 11be60e97..5bdcf6ebe 100644
--- a/container/src/main/java/org/openremote/container/Container.java
+++ b/container/src/main/java/org/openremote/container/Container.java
@@ -160,11 +160,11 @@ public class Container implements org.openremote.model.Container {
LOG.info(">>> Starting runtime container...");
try {
for (ContainerService service : getServices()) {
- LOG.fine("Initializing service: " + service);
+ LOG.info("Initializing service: " + service.getClass().getName());
service.init(Container.this);
}
for (ContainerService service : getServices()) {
- LOG.fine("Starting service: " + service);
+ LOG.info("Starting service: " + service.getClass().getName());
service.start(Container.this);
}
} catch (Exception ex) {
@@ -183,7 +183,7 @@ public class Container implements org.openremote.model.Container {
Collections.reverse(servicesToStop);
try {
for (ContainerService service : servicesToStop) {
- LOG.fine("Stopping service: " + service);
+ LOG.fine("Stopping service: " + service.getClass().getName());
service.stop(this);
}
} catch (Exception ex) {
diff --git a/container/src/main/java/org/openremote/container/concurrent/GlobalLock.java b/container/src/main/java/org/openremote/container/concurrent/GlobalLock.java
index 18cf73e90..65869806f 100644
--- a/container/src/main/java/org/openremote/container/concurrent/GlobalLock.java
+++ b/container/src/main/java/org/openremote/container/concurrent/GlobalLock.java
@@ -19,18 +19,20 @@
*/
package org.openremote.container.concurrent;
+import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import java.util.logging.Logger;
-import static java.util.logging.Level.FINEST;
+import static java.util.logging.Level.INFO;
/**
* A global reentrant exclusive lock, use convenience methods {@link #withLock} and {@link #withLockReturning}.
*/
public class GlobalLock {
+ protected static final int LOCK_WARNING_MILLIS = 10000;
private static final Logger LOG = Logger.getLogger(GlobalLock.class.getName());
/**
@@ -52,6 +54,10 @@ public class GlobalLock {
return "Unknown executing " + info;
}
+ String getOwnerThreadName() {
+ return Optional.ofNullable(super.getOwner()).map(Thread::getName).orElse(null);
+ }
+
public boolean tryLock(String info, long timeout, TimeUnit unit) throws InterruptedException {
boolean result = super.tryLock(timeout, unit);
if (result) {
@@ -101,8 +107,10 @@ public class GlobalLock {
*/
static public <R> R withLockReturning(String info, Supplier<R> supplier) {
try {
+ long startTime = System.currentTimeMillis();
if (lock.tryLock(info, getLockTimeoutMillis(), TimeUnit.MILLISECONDS)) {
LOG.finest("+ Acquired lock (count: " + lock.getHoldCount() + "): " + info);
+ String threadName = lock.getOwnerThreadName();
try {
return supplier.get();
} finally {
@@ -112,6 +120,12 @@ public class GlobalLock {
} else {
lock.unlock();
}
+ long duration = System.currentTimeMillis() - startTime;
+ if (duration > LOCK_WARNING_MILLIS) {
+ LOG.warning("Lock duration was above warning threshold for thread (" + threadName + "): " + duration + "ms");
+ } else {
+ LOG.fine("Lock duration for thread (" + threadName + "): " + duration + "ms");
+ }
}
} else {
throw new IllegalStateException(
@@ -119,8 +133,8 @@ public class GlobalLock {
);
}
} catch (InterruptedException ex) {
- LOG.log(FINEST, "Interrupted while waiting for lock: " + info);
+ LOG.log(INFO, "Interrupted while waiting for lock: " + info);
return null;
}
}
-}
\ No newline at end of file
+}
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
index e181597ca..2df3b7f72 100644
--- a/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceService.java
@@ -38,6 +38,7 @@ import org.openremote.model.apps.ConsoleAppConfig;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetDescriptor;
import org.openremote.model.asset.UserAssetLink;
+import org.openremote.model.asset.impl.UnknownAsset;
import org.openremote.model.dashboard.Dashboard;
import org.openremote.model.datapoint.AssetDatapoint;
import org.openremote.model.gateway.GatewayConnection;
@@ -318,6 +319,7 @@ public class PersistenceService implements ContainerService, Consumer<Persistenc
entityClasses.add("org.openremote.container.util");
// Get asset sub type entities from asset model
+ entityClasses.add(UnknownAsset.class.getName()); // This doesn't have an asset descriptor which is why it is specifically added
Arrays.stream(ValueUtil.getAssetDescriptors(null))
.map(AssetDescriptor::getType)
.filter(assetClass -> assetClass.getAnnotation(Entity.class) != null)
diff --git a/container/src/main/java/org/openremote/container/security/IdentityService.java b/container/src/main/java/org/openremote/container/security/IdentityService.java
index 982c9711c..2c2b57c54 100644
--- a/container/src/main/java/org/openremote/container/security/IdentityService.java
+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java
@@ -41,7 +41,7 @@ import static org.openremote.container.util.MapAccess.*;
*/
public abstract class IdentityService implements ContainerService {
- public static final int PRIORITY = PersistenceService.PRIORITY - 10;
+ public static final int PRIORITY = PersistenceService.PRIORITY + 10;
private static final Logger LOG = Logger.getLogger(IdentityService.class.getName());
public static final String OR_IDENTITY_PROVIDER = "OR_IDENTITY_PROVIDER";
public static final String OR_IDENTITY_PROVIDER_DEFAULT = "keycloak";
diff --git a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java
index 42147ed7d..84d323c0b 100644
--- a/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java
+++ b/container/src/main/java/org/openremote/container/security/keycloak/KeycloakIdentityProvider.java
@@ -69,7 +69,8 @@ import java.util.logging.Logger;
import static java.util.concurrent.TimeUnit.MINUTES;
import static javax.ws.rs.core.Response.Status.Family.REDIRECTION;
import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
-import static org.openremote.container.util.MapAccess.*;
+import static org.openremote.container.util.MapAccess.getInteger;
+import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.container.web.WebClient.getTarget;
import static org.openremote.container.web.WebService.pathStartsWithHandler;
import static org.openremote.model.Constants.*;
@@ -80,7 +81,6 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
// an access token from authentication directly, which gives us full access to import/delete
// demo data as needed.
public static final String ADMIN_CLI_CLIENT_ID = "admin-cli";
- public static final String MANAGER_CLIENT_ID = "manager-keycloak";
public static final List<String> DEFAULT_CLIENTS = Arrays.asList(
"account",
ADMIN_CLI_CLIENT_ID,
@@ -187,8 +187,6 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
);
httpClient = WebClient.registerDefaults(clientBuilder).build();
- setActiveCredentials(getDefaultKeycloakGrant(container));
-
keycloakDeploymentCache = createKeycloakDeploymentCache();
keycloakConfigResolver = request -> {
@@ -222,6 +220,42 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
@Override
public void start(Container container) {
+
+ OAuthGrant credentials = getStoredCredentials(container);
+
+ if (credentials != null) {
+ LOG.info("Found stored credentials so attempting to use them");
+ if (!setActiveCredentials(credentials)) {
+ LOG.warning("Stored keycloak credentials are not valid, falling back to admin user using " + OR_ADMIN_PASSWORD);
+ credentials = getDefaultKeycloakGrant(container);
+ } else {
+ credentials = null;
+ }
+ } else {
+ LOG.info("No stored credentials so using " + OR_ADMIN_PASSWORD);
+ credentials = getDefaultKeycloakGrant(container);
+ }
+
+ if (credentials != null) {
+ if (!setActiveCredentials(credentials)) {
+ String msg = "Credentials don't work so cannot continue";
+ LOG.warning(msg);
+ throw new RuntimeException(msg);
+ } else {
+ LOG.info(OR_ADMIN_PASSWORD + " credentials are valid so creating/recreating stored credentials");
+ credentials = generateStoredCredentials(container);
+ if (credentials != null) {
+ LOG.info("Stored credentials successfully generated so using them");
+ if (!setActiveCredentials(credentials)) {
+ String msg = "Something went wrong trying to use the new stored credentials, cannot proceed";
+ LOG.warning(msg);
+ throw new RuntimeException(msg);
+ }
+ } else {
+ LOG.info("Failed to generate stored credentials will continue using " + OR_ADMIN_PASSWORD);
+ }
+ }
+ }
}
@Override
@@ -341,13 +375,13 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
}
/**
- * Update the active credentials used to interact with keycloak; the token endpoint will be overwritten with this
+ * Update the active credentials used to perform keycloak API actions; the token endpoint will be overwritten with this
* instances keycloak server URI and for the master realm.
*/
- public synchronized void setActiveCredentials(OAuthGrant grant) {
+ public synchronized boolean setActiveCredentials(OAuthGrant grant) {
if (Objects.equals(this.oAuthGrant, grant)) {
- return;
+ return true;
}
this.oAuthGrant = grant;
@@ -363,8 +397,33 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
keycloakTarget = targetBuilder.build();
realmsResourcePool.clear();
LOG.info("Keycloak proxy URI set to: " + proxyURI);
+ LOG.info("Validating keycloak credentials");
+ try {
+ getRealms(realmsResource -> {
+ realmsResource.realm(MASTER_REALM).toRepresentation();
+ LOG.info("Credentials are valid");
+ return null;
+ });
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Credentials are invalid", e);
+ return false;
+ }
+
+ return true;
}
+ /**
+ * Get any stored credentials for connecting to the Keycloak admin API
+ * @return credentials or null if none defined.
+ */
+ protected abstract OAuthGrant getStoredCredentials(Container container);
+
+ /**
+ * Generate and store credentials for connecting to the Keycloak admin API
+ * @return credentials or null if generation/storage failed.
+ */
+ protected abstract OAuthGrant generateStoredCredentials(Container container);
+
protected LoadingCache<KeycloakRealmClient, KeycloakDeployment> createKeycloakDeploymentCache() {
CacheLoader<KeycloakRealmClient, KeycloakDeployment> loader =
new CacheLoader<KeycloakRealmClient, KeycloakDeployment>() {
diff --git a/container/src/main/java/org/openremote/container/util/LogUtil.java b/container/src/main/java/org/openremote/container/util/LogUtil.java
index 7caa1ef47..b741c91a3 100644
--- a/container/src/main/java/org/openremote/container/util/LogUtil.java
+++ b/container/src/main/java/org/openremote/container/util/LogUtil.java
@@ -51,42 +51,43 @@ public class LogUtil {
public static void configureLogging() throws ExceptionInInitializerError {
// If no JUL configuration is provided
if (System.getProperty("java.util.logging.config.file") == null) {
+ boolean logConfigFound = false;
+ Path loggingConfigFile = !TextUtil.isNullOrEmpty(System.getenv(OR_LOGGING_CONFIG_FILE)) ? Paths.get(System.getenv(OR_LOGGING_CONFIG_FILE)) : null;
+
// Load the logging configuration file specified with an environment variable
- if (!TextUtil.isNullOrEmpty(System.getenv(OR_LOGGING_CONFIG_FILE))) {
- Path loggingConfigFile = Paths.get(System.getenv(OR_LOGGING_CONFIG_FILE));
- if (!Files.isReadable(loggingConfigFile)) {
- throw new ExceptionInInitializerError("OR_LOGGING_CONFIG_FILE is not readable: " + loggingConfigFile.toAbsolutePath());
- }
- try (InputStream is = Files.newInputStream(loggingConfigFile)) {
+ if (loggingConfigFile != null) {
+ if (Files.isReadable(loggingConfigFile)) {
System.out.println("Using logging configuration: " + loggingConfigFile.toAbsolutePath());
- LogManager.getLogManager().readConfiguration(is);
- } catch (Exception ex) {
- throw new ExceptionInInitializerError(ex);
- }
- } else {
- // Try to find /deployment/manager/logging.properties
- if (Files.isReadable(Paths.get("/deployment/manager/logging.properties"))) {
- try (InputStream is = Files.newInputStream(Paths.get("/deployment/manager/logging.properties"))) {
- System.out.println("Using logging configuration: /deployment/manager/logging.properties");
+
+ try (InputStream is = Files.newInputStream(loggingConfigFile)) {
LogManager.getLogManager().readConfiguration(is);
+ logConfigFound = true;
} catch (Exception ex) {
throw new ExceptionInInitializerError(ex);
}
- } else {
- // Or load a default configuration from the classpath
- String devModeStr = System.getenv(OR_DEV_MODE);
- boolean isDevMode = devModeStr == null || "TRUE".equals(devModeStr.toUpperCase(Locale.ROOT));
- String loggingFile = isDevMode ? "logging-dev.properties" : "logging.properties";
- try (InputStream is = Container.class.getClassLoader().getResourceAsStream(loggingFile)) {
- if (is != null) {
- System.out.println("Using logging configuration on classpath: " + loggingFile);
- LogManager.getLogManager().readConfiguration(is);
- }
- } catch (Exception ex) {
- throw new ExceptionInInitializerError(ex);
+ }
+ }
+
+ if (!logConfigFound) {
+ // Look for logging config on the classpath (load a default configuration if none specified)
+ String devModeStr = System.getenv(OR_DEV_MODE);
+ boolean isDevMode = devModeStr == null || "TRUE".equals(devModeStr.toUpperCase(Locale.ROOT));
+ String loggingFile = loggingConfigFile != null ? loggingConfigFile.toString() : isDevMode ? "logging-dev.properties" : "logging.properties";
+
+ try (InputStream is = Container.class.getClassLoader().getResourceAsStream(loggingFile)) {
+ if (is != null) {
+ System.out.println("Using logging configuration on classpath: " + loggingFile);
+ logConfigFound = true;
+ LogManager.getLogManager().readConfiguration(is);
}
+ } catch (Exception ex) {
+ throw new ExceptionInInitializerError(ex);
}
}
+
+ if (!logConfigFound) {
+ throw new ExceptionInInitializerError("OR_LOGGING_CONFIG_FILE is not readable: " + loggingConfigFile);
+ }
}
}
}
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
index 5d3da840e..534c6ccc0 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
@@ -319,7 +319,9 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
.process(exchange -> withLock(getClass().getSimpleName() + "::processFromAssetQueue", () -> {
AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);
- LOG.finest("Processing: " + event);
+ if (LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("Processing: " + event);
+ }
if (event.getAssetId() == null || event.getAssetId().isEmpty())
return;
if (event.getAttributeName() == null || event.getAttributeName().isEmpty())
@@ -460,11 +462,12 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
String attributeStr = "Asset ID=" + asset.getId() + ", Asset name=" + asset.getName() + ", " + attribute;
- LOG.fine(">>> Processing start: " + attributeStr);
+ LOG.finest(">>> Processing start: " + attributeStr);
// Need to record time here otherwise an infinite loop generated inside one of the processors means the timestamp
// is not updated so tests can't then detect the problem.
lastProcessedEventTimestamp = System.currentTimeMillis();
+ String consumerProcessor = null;
boolean complete = false;
for (AssetUpdateProcessor processor : processors) {
@@ -481,15 +484,20 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
);
}
if (complete) {
- LOG.fine("<== Processor " + processor + " completely consumed: " + attributeStr);
+ if (LOG.isLoggable(Level.FINEST)) {
+ consumerProcessor = processor.toString();
+ LOG.finest("<== Processor " + consumerProcessor + " completely consumed (time since processing started=" + (System.currentTimeMillis() - lastProcessedEventTimestamp) + "ms): " + attributeStr);
+ }
break;
} else {
- LOG.finest("<== Processor " + processor + " done with: " + attributeStr);
+ if (LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("<== Processor " + processor + " done (time since processing started=" + (System.currentTimeMillis() - lastProcessedEventTimestamp) + "ms): " + attributeStr);
+ }
}
}
if (!complete) {
- LOG.fine("No processor consumed the update completely, storing: " + attributeStr);
+ LOG.finest("No processor consumed the update completely, storing (time since processing started=" + (System.currentTimeMillis() - lastProcessedEventTimestamp) + "ms): " + attributeStr);
if (!assetStorageService.updateAttributeValue(em, asset, attribute)) {
throw new AssetProcessingException(
STATE_STORAGE_FAILED, "database update failed, no rows updated"
@@ -497,7 +505,11 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
}
- LOG.fine("<<< Processing complete: " + attributeStr);
+ if (consumerProcessor != null) {
+ LOG.fine("<<< Processing complete in " + (System.currentTimeMillis() - lastProcessedEventTimestamp) + "ms consumed by '" + consumerProcessor +"': " + attributeStr);
+ } else {
+ LOG.fine("<<< Processing complete in " + (System.currentTimeMillis() - lastProcessedEventTimestamp) + "ms: " + attributeStr);
+ }
return complete;
}
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index c977de532..63c5e6cee 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -168,7 +168,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
String realm = requestRealm != null ? requestRealm : !isAnonymous ? auth.getAuthenticatedRealmName() : null;
if (realm == null) {
- LOG.fine("Anonymous subscriptions must specify a realm");
+ LOG.info("Anonymous AssetInfo subscriptions must specify a realm");
return false;
}
@@ -253,7 +253,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
Predicate<Object> namePredicate = asPredicateOrTrue(currentMillisSupplier, attributePredicate.name);
Predicate<Object> valuePredicate = asPredicateOrTrue(currentMillisSupplier, attributePredicate.value);
List<Attribute<?>> matchedAttributes = asset.getAttributes().stream()
- .filter(attr -> namePredicate.test(attr.getName())).collect(Collectors.toList());
+ .filter(attr -> namePredicate.test(attr.getName())).toList();
matches = true;
@@ -329,12 +329,12 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
if (event instanceof ReadAssetEvent readAssetEvent) {
if (readAssetEvent.getAssetQuery() == null) {
- LOG.fine("Read asset event must specify an asset ID");
+ LOG.info("Read asset event must specify an asset ID");
return false;
}
} else if (event instanceof ReadAttributeEvent readAttributeEvent) {
if (readAttributeEvent.getAssetQuery() == null) {
- LOG.fine("Read attribute event must specify an asset ID");
+ LOG.info("Read attribute event must specify an asset ID");
return false;
}
}
@@ -562,12 +562,10 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
}
public Asset<?> find(EntityManager em, AssetQuery query) {
+ query.limit = 1;
List<Asset<?>> result = findAll(em, query);
- if (result.size() == 0)
+ if (result.isEmpty())
return null;
- if (result.size() > 1) {
- throw new IllegalArgumentException("Query returned more than one asset");
- }
return result.get(0);
}
@@ -639,10 +637,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
@SuppressWarnings("unchecked")
public <T extends Asset<?>> T merge(T asset, boolean overrideVersion, boolean skipGatewayCheck, String userName) throws IllegalStateException, ConstraintViolationException {
- LOG.fine("Merging asset: " + asset);
+ if (LOG.isLoggable(Level.FINER)) {
+ LOG.finer("Merging asset: " + asset);
+ }
return persistenceService.doReturningTransaction(em -> {
+ long startTime = System.currentTimeMillis();
String gatewayId = gatewayService.getLocallyRegisteredGatewayId(asset.getId(), asset.getParentId());
if (!skipGatewayCheck && gatewayId != null) {
@@ -653,7 +654,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// Validate realm
if (asset.getRealm() == null) {
String msg = "Asset realm must be set : asset=" + asset;
- LOG.fine(msg);
+ LOG.warning(msg);
throw new IllegalStateException(msg);
}
@@ -674,18 +675,18 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// Verify type has not been changed
if (!existingAsset.getType().equals(asset.getType())) {
String msg = "Asset type cannot be changed: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
if (!existingAsset.getRealm().equals(asset.getRealm())) {
String msg = "Asset realm cannot be changed: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
// Update timestamp on modified attributes this allows fast equality checking
- asset.getAttributes().stream().forEach(attr -> {
+ asset.getAttributes().stream().forEach(attr ->
existingAsset.getAttribute(attr.getName()).ifPresent(existingAttr -> {
// If attribute is modified make sure the timestamp is also updated to allow simple equality
if (!attr.deepEquals(existingAttr) && attr.getTimestamp().orElse(0L) <= existingAttr.getTimestamp().orElse(0L)) {
@@ -693,8 +694,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// we will always ensure a delta of >= 1ms
attr.setTimestamp(Math.max(existingAttr.getTimestamp().orElse(0L)+1, timerService.getCurrentTimeMillis()));
}
- });
- });
+ }));
// If this is real merge and desired, copy the persistent version number over the detached
// version, so the detached state always wins and this update will go through and ignore
@@ -706,13 +706,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
if (!identityService.getIdentityProvider().realmExists(asset.getRealm())) {
String msg = "Asset realm not found or is inactive: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
if (asset.getParentId() != null && asset.getParentId().equals(asset.getId())) {
String msg = "Asset parent cannot be the asset: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
@@ -725,21 +725,21 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// The parent must exist
if (parent == null) {
String msg = "Asset parent not found: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
// The parent can not be a child of the asset
if (parent.pathContains(asset.getId())) {
String msg = "Asset parent cannot be a descendant of the asset: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
// The parent should be in the same realm
if (!parent.getRealm().equals(asset.getRealm())) {
String msg = "Asset parent must be in the same realm: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
@@ -748,7 +748,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
String childAssetType = parent.getAttributes().getValue(GroupAsset.CHILD_ASSET_TYPE)
.orElseThrow(() -> {
String msg = "Asset parent is of type GROUP but the childAssetType attribute is invalid: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
return new IllegalStateException(msg);
});
@@ -763,7 +763,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
if (!typeMatch) {
String msg = "Asset type does not match parent GROUP asset's childAssetType attribute: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
}
@@ -775,7 +775,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
.map(childAssetTypeString -> TextUtil.isNullOrEmpty(childAssetTypeString) ? null : childAssetTypeString)
.orElseThrow(() -> {
String msg = "Asset of type GROUP childAssetType attribute must be a valid string: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
return new IllegalStateException(msg);
});
@@ -783,13 +783,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
.getChildAssetType()
.orElseThrow(() -> {
String msg = "Asset of type GROUP childAssetType attribute must be a valid string: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
return new IllegalStateException(msg);
}) : childAssetType;
if (!childAssetType.equals(existingChildAssetType)) {
String msg = "Asset of type GROUP so childAssetType attribute cannot be changed: asset=" + asset;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
}
@@ -809,13 +809,17 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
user = identityService.getIdentityProvider().getUserByUsername(asset.getRealm(), userName);
if (user == null) {
String msg = "User not found: " + userName;
- LOG.fine(msg);
+ LOG.info(msg);
throw new IllegalStateException(msg);
}
}
T updatedAsset = em.merge(asset);
+ if (LOG.isLoggable(Level.FINE)) {
+ LOG.fine("Asset merge took (ms): " + (System.currentTimeMillis() - startTime));
+ }
+
if (user != null) {
createUserAssetLinks(em, Collections.singletonList(new UserAssetLink(user.getRealm(), user.getId(), updatedAsset.getId())));
}
@@ -1239,6 +1243,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
@SuppressWarnings("unchecked")
protected List<Asset<?>> findAll(EntityManager em, AssetQuery query) {
+ long startMillis = System.currentTimeMillis();
if (query.access == null)
query.access = PRIVATE;
@@ -1272,7 +1277,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
boolean containsCalendarPredicate = queryAndContainsCalendarPredicate.value;
if (containsCalendarPredicate && (query.select != null && (query.select.attributes == null))) {
- LOG.fine("Asset query contains a calendar event predicate which requires the attribute values and types to be included in the select (as calendar event predicate is applied post DB query)");
+ LOG.info("Asset query contains a calendar event predicate which requires the attribute values and types to be included in the select (as calendar event predicate is applied post DB query)");
throw new IllegalArgumentException("Asset query contains a calendar event predicate which requires the attribute values and types to be included in the select (as calendar event predicate is applied post DB query)");
}
@@ -1300,6 +1305,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// });
org.hibernate.query.Query<Object[]> jpql = em.createNativeQuery(querySql.querySql, Asset.class).unwrap(org.hibernate.query.Query.class);
+
querySql.apply(em, jpql);
List<Asset<?>> assets = (List<Asset<?>>)(Object)jpql.getResultList();
@@ -1307,6 +1313,10 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
return assets.stream().filter(asset -> calendarEventPredicateMatches(timerService::getCurrentTimeMillis, query, asset)).toList();
}
+ if (LOG.isLoggable(Level.FINE)) {
+ LOG.fine("Asset query took " + (System.currentTimeMillis() - startMillis) + "ms: return count=" + assets.size());
+ }
+
return assets;
}
@@ -1779,18 +1789,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
return containsCalendarPredicate;
}
- /**
- * Resolves the concrete {@link Asset} types that are covered by the supplied asset types
- */
- protected static String[] getResolvedAssetTypes(Class<? extends Asset<?>>[] assetClasses) {
- return Arrays.stream(assetClasses)
- .flatMap(assetClass ->
- Arrays.stream(ValueUtil.getAssetClasses(null)).filter(assetClass::isAssignableFrom))
- .map(Class::getSimpleName)
- .distinct()
- .toArray(String[]::new);
- }
-
protected static boolean addAttributePredicateGroupQuery(StringBuilder sb, List<ParameterBinder> binders, int groupIndex, Consumer<String> selectInserter, LogicGroup<AttributePredicate> attributePredicateGroup, Supplier<Long> timeProvider) {
boolean containsCalendarPredicate = false;
diff --git a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java
index a523059b6..cf8f06a67 100644
--- a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java
@@ -41,11 +41,13 @@ import org.openremote.model.query.filter.StringPredicate;
import org.openremote.model.query.filter.RealmPredicate;
import org.openremote.model.security.Realm;
import org.openremote.model.util.TextUtil;
+import org.openremote.model.util.ValueUtil;
import javax.ws.rs.BadRequestException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Objects;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.model.value.MetaItemType.*;
@@ -92,19 +94,45 @@ public class ConsoleResourceImpl extends ManagerWebResource implements ConsoleRe
consoleAsset = (ConsoleAsset) existingAsset;
}
+ boolean mergeConsole = false;
+
if (consoleAsset == null) {
- consoleAsset = initConsoleAsset(consoleRegistration, true, true);
+ mergeConsole = true;
+ consoleAsset = initConsoleAsset(consoleRegistration);
consoleAsset.setRealm(getRequestRealmName());
consoleAsset.setParentId(getConsoleParentAssetId(getRequestRealmName()));
consoleAsset.setId(consoleRegistration.getId());
+ if (!isAuthenticated()) {
+ // Anonymous registration
+ consoleAsset.setAccessPublicRead(true);
+ }
+ }
+
+ if (mergeConsole || !Objects.equals(consoleAsset.getConsoleName().orElse(null), consoleRegistration.getName())) {
+ mergeConsole = true;
+ consoleAsset.setConsoleName(consoleRegistration.getName());
+ }
+
+ boolean providersChanged = mergeConsole || !consoleAsset.getConsoleProviders().map(providers ->
+ providers.equals(consoleRegistration.getProviders())).orElseGet(() -> consoleRegistration.getProviders() != null);
+ if (providersChanged) {
+ mergeConsole = true;
+ consoleAsset.setConsoleProviders(new ConsoleProviders(consoleRegistration.getProviders()));
+ }
+
+ if (mergeConsole || !Objects.equals(consoleAsset.getConsoleVersion().orElse(null), consoleRegistration.getVersion())) {
+ mergeConsole = true;
+ consoleAsset.setConsoleVersion(consoleRegistration.getVersion());
}
- consoleAsset.setConsoleName(consoleRegistration.getName())
- .setConsoleVersion(consoleRegistration.getVersion())
- .setConsoleProviders(new ConsoleProviders(consoleRegistration.getProviders()))
- .setConsolePlatform(consoleRegistration.getPlatform());
+ if (mergeConsole || !Objects.equals(consoleAsset.getConsolePlatform().orElse(null), consoleRegistration.getPlatform())) {
+ mergeConsole = true;
+ consoleAsset.setConsolePlatform(consoleRegistration.getPlatform());
+ }
- consoleAsset = assetStorageService.merge(consoleAsset);
+ if (mergeConsole) {
+ consoleAsset = assetStorageService.merge(consoleAsset);
+ }
consoleRegistration.setId(consoleAsset.getId());
// If authenticated link the console to this user
@@ -115,21 +143,8 @@ public class ConsoleResourceImpl extends ManagerWebResource implements ConsoleRe
return consoleRegistration;
}
- public static ConsoleAsset initConsoleAsset(ConsoleRegistration consoleRegistration, boolean allowPublicLocationWrite, boolean allowRestrictedLocationWrite) {
- ConsoleAsset consoleAsset = new ConsoleAsset(consoleRegistration.getName());
-
- consoleAsset.getAttributes().getOrCreate(Asset.LOCATION).addOrReplaceMeta(new MetaItem<>(RULE_STATE));
-
- if (allowPublicLocationWrite) {
- consoleAsset.getAttributes().getOrCreate(Asset.LOCATION).addOrReplaceMeta(new MetaItem<>(ACCESS_PUBLIC_WRITE, true));
- }
- if (allowRestrictedLocationWrite) {
- consoleAsset.getAttributes().getOrCreate(Asset.LOCATION).addOrReplaceMeta(new MetaItem<>(ACCESS_RESTRICTED_WRITE, true));
- }
-
- consoleAsset.setAccessPublicRead(true);
-
- return consoleAsset;
+ public static ConsoleAsset initConsoleAsset(ConsoleRegistration consoleRegistration) {
+ return new ConsoleAsset(consoleRegistration.getName());
}
public String getConsoleParentAssetId(String realm) {
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java
index f0c936e9e..b5f7916a0 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java
@@ -31,6 +31,7 @@ import org.keycloak.adapters.KeycloakDeployment;
import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
import org.openremote.manager.security.AuthorisationService;
import org.openremote.manager.security.MultiTenantJaasCallbackHandler;
+import org.openremote.manager.security.RemotingConnectionPrincipal;
import org.openremote.model.syslog.SyslogCategory;
import javax.security.auth.Subject;
@@ -131,6 +132,8 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {
if (subject != null) {
// Ensure subject is available when afterCreateConnection is fired
remotingConnection.setSubject(subject);
+
+ subject.getPrincipals().add(new RemotingConnectionPrincipal(remotingConnection));
}
return subject;
@@ -171,7 +174,19 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {
}
KeycloakSecurityContext securityContext = KeycloakIdentityProvider.getSecurityContext(subject);
- RemotingConnection connection = brokerService.getConnectionFromSubjectAndTopic(subject, topic);
+ String topicClientID = MQTTHandler.topicClientID(topic);
+
+ if (topicClientID == null) {
+ LOG.warning("Client ID not found but it must be included as the second token in the topic: topic=" + topic);
+ return false;
+ }
+
+ RemotingConnection connection = RemotingConnectionPrincipal.getRemotingConnectionFromSubject(subject);
+
+ if (connection == null) {
+ LOG.warning("Failed to find connection for the specified client ID: clientID=" + topicClientID);
+ return false;
+ }
if (isWrite && topic.hasWildcard()) {
return false;
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
index ed0af0335..1cd16d618 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
@@ -193,7 +193,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
config.setPersistenceEnabled(false);
// TODO: Make auto provisioning clients disconnect and reconnect with credentials or pass through X.509 certificates for auth
- // Temp to prevent breaking existing auto provisioning API
+ // Disable auth cache so we can inject RemotingConnection into the subject as a principal otherwise we don't have access to the connection during authorisation so we cannot check topic contains the clientID
// Disable caching so we can support auto provisioning sessions without having to reconnect - we need to re-evaluate this and clients should probably send X.509 through to the broker
config.setAuthenticationCacheSize(0);
config.setAuthorizationCacheSize(0);
@@ -243,7 +243,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
ServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");
ClientSessionFactory factory = serverLocator.createSessionFactory();
String internalClientID = UniqueIdentifierGenerator.generateId("Internal client");
- internalSession = (ClientSessionInternal) factory.createSession(null, null, false, true, true, serverLocator.isPreAcknowledge(), serverLocator.getAckBatchSize(), internalClientID);
+ internalSession = (ClientSessionInternal) factory.createSession(null, null, false, true, true, true, serverLocator.getAckBatchSize(), internalClientID);
ServerSession serverSession = server.getActiveMQServer().getSessionByID(internalSession.getName());
serverSession.disableSecurity();
internalSession.start();
@@ -375,7 +375,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
clientIDConnectionMap.put(remotingConnection.getClientID(), remotingConnection);
if (!connectionIDConnectionMap.containsKey(connectionID)) {
- LOG.info("Client connection created: " + connectionToString(remotingConnection ));
+ LOG.info("Client connection created: " + connectionToString(remotingConnection));
connectionIDConnectionMap.put(connectionID, remotingConnection);
for (MQTTHandler handler : getCustomHandlers()) {
handler.onConnect(remotingConnection);
@@ -460,6 +460,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
protected void doForceDisconnect(RemotingConnection connection) {
LOG.info("Force disconnecting client connection: " + connectionToString(connection));
+ removeTransientCredentials(connection);
connection.disconnect(false);
}
@@ -545,38 +546,15 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,
return connection;
}
- // TODO: Remove this if/when ActiveMQ implements https://issues.apache.org/jira/browse/ARTEMIS-4059
-
- /**
- * Tries to find the correct {@link RemotingConnection} for the specified subject with the assumption that the
- * client ID is contained in the address (this is important for multiple connections from the same subject)
- */
- protected RemotingConnection getConnectionFromSubjectAndTopic(Subject subject, Topic topic) {
- String clientID = MQTTHandler.topicClientID(topic);
-
- if (clientID == null) {
- return null;
- }
-
- return connectionIDConnectionMap.values().stream().filter(connection ->
- connection.getSubject() == subject && Objects.equals(clientID, connection.getClientID())
- )
- .findFirst()
- .orElse(null);
- }
-
// TODO: Remove this
public void addTransientCredentials(RemotingConnection connection, Triple<String, String, String> userIDNameAndPassword) {
LOG.fine("Adding transient user credentials: connection ID=" + connection.getID() + " ,username=" + userIDNameAndPassword.getMiddle());
transientCredentials.put(connection.getID(), userIDNameAndPassword);
- ((SecurityStoreImpl) server.getActiveMQServer().getSecurityStore()).invalidateAuthenticationCache();
}
// TODO: Remove this
public void removeTransientCredentials(RemotingConnection connection) {
LOG.fine("Removing transient user credentials: connection ID=" + connection.getID());
transientCredentials.remove(connection.getID());
- ((SecurityStoreImpl) server.getActiveMQServer().getSecurityStore()).invalidateAuthenticationCache();
- ((SecurityStoreImpl) server.getActiveMQServer().getSecurityStore()).invalidateAuthorizationCache();
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
index d9eb42918..0bfac3dfa 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java
@@ -108,7 +108,7 @@ public abstract class MQTTHandler {
try {
getLogger().fine("Adding publish consumer for topic '" + topic + "': handler=" + getName());
String coreTopic = MQTTUtil.convertMqttTopicFilterToCoreAddress(topic, mqttBrokerService.wildcardConfiguration);
- mqttBrokerService.internalSession.createQueue(new QueueConfiguration(coreTopic).setRoutingType(RoutingType.ANYCAST).setAutoCreateAddress(true).setAutoCreated(true));
+ mqttBrokerService.internalSession.createQueue(new QueueConfiguration(coreTopic).setRoutingType(RoutingType.ANYCAST).setPurgeOnNoConsumers(true).setAutoCreateAddress(true).setAutoCreated(true));
ClientConsumer consumer = mqttBrokerService.internalSession.createConsumer(coreTopic);
consumer.setMessageHandler(message -> {
Topic publishTopic = Topic.parse(MQTTUtil.convertCoreAddressToMqttTopicFilter(message.getAddress(), mqttBrokerService.wildcardConfiguration));
@@ -122,7 +122,6 @@ public abstract class MQTTHandler {
getLogger().info("Client published to '" + publishTopic + "': " + mqttBrokerService.connectionToString(connection));
onPublish(connection, publishTopic, message.getReadOnlyBodyBuffer().byteBuf());
- getLogger().info("Client published '" + topic + "': " + mqttBrokerService.connectionToString(connection));
});
} catch (ActiveMQException e) {
getLogger().log(Level.WARNING, "Failed to create handler consumer for topic '" + topic + "': handler=" + getName(), e);
@@ -196,11 +195,11 @@ public abstract class MQTTHandler {
*/
public boolean checkCanPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {
if (securityContext == null) {
- getLogger().fine("Anonymous connection publishes not supported by this handler, topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));
+ getLogger().fine("Anonymous connection publishes not supported by this handler topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));
return false;
}
if (!topicRealmAllowed(securityContext, topic) || !topicClientIdMatches(connection, topic)) {
- getLogger().fine("Topic realm and client ID tokens must match the connection, topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));
+ getLogger().fine("Topic realm and client ID tokens must match the connection topic=" + topic + ", " + mqttBrokerService.connectionToString(connection));
return false;
}
return canPublish(connection, securityContext, topic);
diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
index c82d81e94..eeec7efdd 100644
--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java
@@ -22,7 +22,6 @@ package org.openremote.manager.rules;
import com.fasterxml.jackson.databind.JsonNode;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetStorageService;
-import org.openremote.model.asset.impl.ThingAsset;
import org.openremote.model.attribute.MetaMap;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.LogicGroup;
@@ -47,11 +46,18 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {
final protected AssetQuery query;
final protected TimerService timerService;
final protected AssetStorageService assetStorageService;
+ final protected List<String> resolvedAssetTypes;
public AssetQueryPredicate(TimerService timerService, AssetStorageService assetStorageService, AssetQuery query) {
this.timerService = timerService;
this.assetStorageService = assetStorageService;
this.query = query;
+
+ if (query.types != null && query.types.length > 0) {
+ resolvedAssetTypes = Arrays.asList(AssetQuery.getResolvedAssetTypes(query.types));
+ } else {
+ resolvedAssetTypes = null;
+ }
}
@Override
@@ -79,14 +85,8 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {
}
}
- if (query.types != null && query.types.length > 0) {
- if (Arrays.stream(query.types).noneMatch(type ->
- type.isAssignableFrom(
- ValueUtil.getAssetDescriptor(assetState.getAssetType())
- .orElse(ThingAsset.DESCRIPTOR).getType()))
- ) {
- return false;
- }
+ if (resolvedAssetTypes != null && !resolvedAssetTypes.contains(assetState.getAssetType())) {
+ return false;
}
if (query.paths != null && query.paths.length > 0) {
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java
index b995b6270..4f5fd307f 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesBuilder.java
@@ -25,8 +25,6 @@ import org.jeasy.rules.core.RuleBuilder;
import java.util.ArrayList;
import java.util.List;
-import static org.openremote.manager.rules.RulesEngine.RULES_FIRED_LOG;
-
/**
* Call {@link #add()} to add rules.
*/
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
index c509bbf92..39bfea459 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java
@@ -19,6 +19,7 @@
*/
package org.openremote.manager.rules;
+import org.apache.commons.lang3.time.DurationFormatUtils;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.RuleListener;
@@ -40,7 +41,6 @@ import org.openremote.model.asset.Asset;
import org.openremote.model.query.filter.GeofencePredicate;
import org.openremote.model.query.filter.LocationAttributePredicate;
import org.openremote.model.rules.*;
-import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
import java.util.*;
@@ -50,9 +50,7 @@ import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
-import java.util.stream.Collectors;
-import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.model.rules.RulesetStatus.*;
public class RulesEngine<T extends Ruleset> {
@@ -97,15 +95,13 @@ public class RulesEngine<T extends Ruleset> {
// Separate logger for execution of rules
public static final Logger RULES_LOG = Logger.getLogger("org.openremote.rules.Rules");
- // Separate logger for fired rules
- public static final Logger RULES_FIRED_LOG = SyslogCategory.getLogger(SyslogCategory.RULES, "org.openremote.rules.RulesFired");
-
// Separate logger for periodic stats printer
public static final Logger STATS_LOG = Logger.getLogger("org.openremote.rules.RulesEngineStats");
protected static BiConsumer<RulesEngine<?>, RulesetDeployment> UNPAUSE_SCHEDULER = RulesEngine::scheduleUnpause;
// Here to facilitate testing
protected static BiConsumer<RulesEngine<?>, RulesetDeployment> PAUSE_SCHEDULER = RulesEngine::schedulePause;
final protected TimerService timerService;
+ final protected RulesService rulesService;
final protected ScheduledExecutorService executorService;
final protected AssetStorageService assetStorageService;
final protected ClientEventService clientEventService;
@@ -138,6 +134,7 @@ public class RulesEngine<T extends Ruleset> {
protected boolean disableTemporaryFactExpiration = false;
public RulesEngine(TimerService timerService,
+ RulesService rulesService,
ManagerIdentityService identityService,
ScheduledExecutorService executorService,
AssetStorageService assetStorageService,
@@ -150,6 +147,7 @@ public class RulesEngine<T extends Ruleset> {
RulesEngineId<T> id,
AssetLocationPredicateProcessor assetLocationPredicatesConsumer) {
this.timerService = timerService;
+ this.rulesService = rulesService;
this.executorService = executorService;
this.assetStorageService = assetStorageService;
this.clientEventService = clientEventService;
@@ -203,14 +201,14 @@ public class RulesEngine<T extends Ruleset> {
/**
* @return a shallow copy of the asset state facts.
*/
- public Set<AssetState<?>> getAssetStates() {
+ public synchronized Set<AssetState<?>> getAssetStates() {
return new HashSet<>(facts.getAssetStates());
}
/**
* @return a shallow copy of the asset event facts.
*/
- public List<TemporaryFact<AssetState<?>>> getAssetEvents() {
+ public synchronized List<TemporaryFact<AssetState<?>>> getAssetEvents() {
return new ArrayList<>(facts.getAssetEvents());
}
@@ -283,7 +281,7 @@ public class RulesEngine<T extends Ruleset> {
} else {
deployment.updateValidity();
if (deployment.hasExpired()) {
- LOG.fine("Ruleset validity period has expired: " + ruleset.getName());
+ LOG.finer("Ruleset validity period has expired: " + ruleset.getName());
deployment.setStatus(EXPIRED);
publishRulesetStatus(deployment);
compiled = true;
@@ -342,12 +340,12 @@ public class RulesEngine<T extends Ruleset> {
}
if (deployments.size() == 0) {
- LOG.finest("No rulesets so nothing to start");
+ LOG.finer("No rulesets so nothing to start");
return;
}
if (!canStart()) {
- LOG.fine("Cannot start rules engine one or more rulesets in an error state");
+ LOG.info("Cannot start rules engine one or more rulesets in an error state");
return;
}
@@ -359,20 +357,20 @@ public class RulesEngine<T extends Ruleset> {
updateDeploymentInfo();
publishRulesEngineStatus();
- scheduleFire();
+ scheduleFire(true);
// Start a background stats printer if INFO level logging is enabled
- if (STATS_LOG.isLoggable(Level.INFO) || STATS_LOG.isLoggable(Level.FINEST)) {
+ if (STATS_LOG.isLoggable(Level.FINE) || STATS_LOG.isLoggable(Level.FINEST)) {
if (STATS_LOG.isLoggable(Level.FINEST)) {
- LOG.info("On " + this + ", enabling periodic statistics output at INFO level every 30 seconds on category: " + STATS_LOG.getName());
+ LOG.info("On " + this + ", enabling periodic statistics output at FINEST level every 30 seconds on category: " + STATS_LOG.getName());
} else {
- LOG.info("On " + this + ", enabling periodic full memory dump at FINEST level every 30 seconds on category: " + STATS_LOG.getName());
+ LOG.info("On " + this + ", enabling periodic full memory dump at FINE level every 30 seconds on category: " + STATS_LOG.getName());
}
statsTimer = executorService.scheduleAtFixedRate(this::printSessionStats, 3, 30, TimeUnit.SECONDS);
}
}
- protected void trackLocationPredicates(boolean track) {
+ protected synchronized void trackLocationPredicates(boolean track) {
if (trackLocationPredicates == track) {
return;
}
@@ -420,7 +418,7 @@ public class RulesEngine<T extends Ruleset> {
publishRulesEngineStatus();
}
- protected void startRuleset(RulesetDeployment deployment) {
+ protected synchronized void startRuleset(RulesetDeployment deployment) {
if (!running) {
return;
}
@@ -451,7 +449,7 @@ public class RulesEngine<T extends Ruleset> {
}
}
- protected void stopRuleset(RulesetDeployment deployment) {
+ protected synchronized void stopRuleset(RulesetDeployment deployment) {
if (!running) {
return;
}
@@ -463,44 +461,62 @@ public class RulesEngine<T extends Ruleset> {
}
}
- public void scheduleFire() {
- withLock(toString() + "::scheduleFire", () -> {
- // Schedule a firing within the guaranteed expiration time (so not immediately), and
- // only if the last firing is done. This effectively limits how often the rules engine
- // will fire, only once within the guaranteed minimum expiration time.
- if (fireTimer == null || fireTimer.isDone()) {
- LOG.fine("Scheduling rules firing on: " + this);
- fireTimer = executorService.schedule(
- () -> withLock(RulesEngine.this.toString() + "::fire", () -> {
-
- fireTimer = null;
-
- // Are temporary facts present before rules are fired?
- boolean hadTemporaryFactsBefore = facts.hasTemporaryFacts();
-
- // Process rules for all deployments
- fireAllDeployments();
-
- // If there are temporary facts, or if there were some before and
- // now they are gone, schedule a new firing to guarantee processing
- // of expired and removed temporary facts
- if ((facts.hasTemporaryFacts() || (hadTemporaryFactsBefore && !facts.hasTemporaryFacts()))
- && !disableTemporaryFactExpiration) {
- LOG.fine("Temporary facts require firing rules on: " + this);
- executorService.submit(this::scheduleFire);
- } else if (!disableTemporaryFactExpiration) {
- LOG.fine("No temporary facts present/changed when firing rules on: " + this);
- }
-
- }),
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS,
- TimeUnit.MILLISECONDS
- );
+ // TODO: Add ability to subscribe to specific events from a ruleset so the ruleset triggers only when appropriate
+ /**
+ * Queues actual firing of rules; if facts have changed then firing occurs in a shorter time frame than if we just
+ * need to re-evaluate {@link TemporaryFact}s. This effectively limits how often the rules engine will fire, only
+ * once within the guaranteed minimum expiration time.
+ */
+ protected synchronized void scheduleFire(boolean quickFire) {
+ boolean timerRunning = fireTimer != null && !fireTimer.isDone();
+
+ if (timerRunning) {
+ if (!quickFire) {
+ // Firing is already scheduled
+ return;
+ } else {
+ if (fireTimer.getDelay(TimeUnit.MILLISECONDS) <= rulesService.quickFireMillis) {
+ // Firing is already going to occur within time frame
+ return;
+ } else {
+ // Cancel the existing timer
+ fireTimer.cancel(false);
+ }
}
- });
+ }
+
+ long fireTimeMillis = quickFire ? rulesService.quickFireMillis : rulesService.tempFactExpirationMillis;
+
+ LOG.finer("Scheduling rules firing in " + fireTimeMillis + "ms on: " + this);
+ fireTimer = executorService.schedule(
+ () -> {
+ synchronized (RulesEngine.this) {
+ // Are temporary facts present before rules are fired?
+ boolean hadTemporaryFactsBefore = facts.hasTemporaryFacts();
+
+ // Process rules for all deployments
+ fireAllDeployments();
+
+ // If there are temporary facts, or if there were some before and
+ // now they are gone, schedule a new firing to guarantee processing
+ // of expired and removed temporary facts
+ if ((facts.hasTemporaryFacts() || (hadTemporaryFactsBefore && !facts.hasTemporaryFacts()))
+ && !disableTemporaryFactExpiration) {
+ // Schedule call to schedule fire so the fireTimer shows as done
+ executorService.schedule(() -> scheduleFire(false), 10, TimeUnit.MILLISECONDS);
+ } else if (!disableTemporaryFactExpiration) {
+ LOG.finest("No temporary facts present/changed when firing rules on: " + this);
+ }
+
+ fireTimer = null;
+ }
+ },
+ fireTimeMillis,
+ TimeUnit.MILLISECONDS
+ );
}
- private void fireDeployments(Collection<RulesetDeployment> deploymentList) {
+ private void doFire(Collection<RulesetDeployment> deploymentList) {
if (!running) {
return;
}
@@ -511,13 +527,14 @@ public class RulesEngine<T extends Ruleset> {
// Remove any expired temporary facts
facts.removeExpiredTemporaryFacts();
+ long executionTotalMillis = 0L;
for (RulesetDeployment deployment : deploymentList) {
try {
if (deployment.getStatus() == DEPLOYED) {
- RULES_LOG.fine("Executing rules of: " + deployment);
+ LOG.finer("Executing rules of: " + deployment);
// If full detail logging is enabled
// Log asset states and events before firing
@@ -529,9 +546,12 @@ public class RulesEngine<T extends Ruleset> {
long startTimestamp = timerService.getCurrentTimeMillis();
lastFireTimestamp = startTimestamp;
engine.fire(deployment.getRules(), facts);
- RULES_FIRED_LOG.fine("Rules deployment '" + deployment.getName() + "' executed in: " + (timerService.getCurrentTimeMillis() - startTimestamp) + "ms");
+ long executionMillis = (timerService.getCurrentTimeMillis() - startTimestamp);
+ executionTotalMillis += executionMillis;
+ LOG.fine("Rules deployment '" + deployment.getName() + "' executed in: " + executionMillis + "ms");
+ } else {
+ LOG.finer("Rules deployment '" + deployment.getName() + "' skipped as status is: " + deployment.getStatus());
}
-
} catch (Exception ex) {
LOG.log(Level.SEVERE, "On " + RulesEngine.this + ", error executing rules of: " + deployment, ex);
@@ -551,19 +571,19 @@ public class RulesEngine<T extends Ruleset> {
}
trackLocationPredicates(false);
- }
- protected void fireAllDeployments() {
- fireDeployments(deployments.values());
+ if (executionTotalMillis > 100) {
+ LOG.warning("Rules firing took " + executionTotalMillis + "ms on: " + this);
+ } else {
+ LOG.fine("Rules firing took " + executionTotalMillis + "ms on: " + this);
+ }
}
- protected void fireAllDeploymentsWithPredictedData() {
- withLock(toString() + "::fireAllDeploymentsWithPredictedData", () -> {
- fireDeployments(deployments.values().stream().filter(RulesetDeployment::isTriggerOnPredictedData).collect(Collectors.toList()));
- });
+ protected void fireAllDeployments() {
+ doFire(deployments.values());
}
- protected void notifyAssetStatesChanged(AssetStateChangeEvent event) {
+ protected synchronized void notifyAssetStatesChanged(AssetStateChangeEvent event) {
for (RulesetDeployment deployment : deployments.values()) {
if (!deployment.isError()) {
deployment.onAssetStatesChanged(facts, event);
@@ -571,30 +591,30 @@ public class RulesEngine<T extends Ruleset> {
}
}
- public void updateOrInsertAssetState(AssetState<?> assetState, boolean insert) {
+ public synchronized void updateOrInsertAssetState(AssetState<?> assetState, boolean insert) {
facts.putAssetState(assetState);
// Make sure location predicate tracking is activated before notifying the deployments otherwise they won't report location predicates
trackLocationPredicates(trackLocationPredicates || (insert && assetState.getName().equals(Asset.LOCATION.getName())));
notifyAssetStatesChanged(new AssetStateChangeEvent(insert ? PersistenceEvent.Cause.CREATE : PersistenceEvent.Cause.UPDATE, assetState));
if (running) {
- scheduleFire();
+ scheduleFire(true);
}
}
- public void removeAssetState(AssetState<?> assetState) {
+ public synchronized void removeAssetState(AssetState<?> assetState) {
facts.removeAssetState(assetState);
// Make sure location predicate tracking is activated before notifying the deployments otherwise they won't report location predicates
trackLocationPredicates(trackLocationPredicates || assetState.getName().equals(Asset.LOCATION.getName()));
notifyAssetStatesChanged(new AssetStateChangeEvent(PersistenceEvent.Cause.DELETE, assetState));
if (running) {
- scheduleFire();
+ scheduleFire(true);
}
}
- public void insertAssetEvent(long expiresMillis, AssetState<?> assetState) {
+ public synchronized void insertAssetEvent(long expiresMillis, AssetState<?> assetState) {
facts.insertAssetEvent(expiresMillis, assetState);
if (running) {
- scheduleFire();
+ scheduleFire(true);
}
}
@@ -606,24 +626,22 @@ public class RulesEngine<T extends Ruleset> {
);
}
- protected void printSessionStats() {
- withLock(toString() + "::printSessionStats", () -> {
- Collection<AssetState<?>> assetStateFacts = facts.getAssetStates();
- Collection<TemporaryFact<AssetState<?>>> assetEventFacts = facts.getAssetEvents();
- Map<String, Object> namedFacts = facts.getNamedFacts();
- Collection<Object> anonFacts = facts.getAnonymousFacts();
- long temporaryFactsCount = facts.getTemporaryFacts().count();
- long total = assetStateFacts.size() + assetEventFacts.size() + namedFacts.size() + anonFacts.size();
- STATS_LOG.fine("Engine stats for '" + this + "', in memory facts are Total: " + total
- + ", AssetState: " + assetStateFacts.size()
- + ", AssetEvent: " + assetEventFacts.size()
- + ", Named: " + namedFacts.size()
- + ", Anonymous: " + anonFacts.size()
- + ", Temporary: " + temporaryFactsCount);
-
- // Additional details if FINEST is enabled
- facts.logFacts(STATS_LOG, Level.FINEST);
- });
+ protected synchronized void printSessionStats() {
+ Collection<AssetState<?>> assetStateFacts = facts.getAssetStates();
+ Collection<TemporaryFact<AssetState<?>>> assetEventFacts = facts.getAssetEvents();
+ Map<String, Object> namedFacts = facts.getNamedFacts();
+ Collection<Object> anonFacts = facts.getAnonymousFacts();
+ long temporaryFactsCount = facts.getTemporaryFacts().count();
+ long total = assetStateFacts.size() + assetEventFacts.size() + namedFacts.size() + anonFacts.size();
+ STATS_LOG.fine("Engine stats for '" + this + "', in memory facts are Total: " + total
+ + ", AssetState: " + assetStateFacts.size()
+ + ", AssetEvent: " + assetEventFacts.size()
+ + ", Named: " + namedFacts.size()
+ + ", Anonymous: " + anonFacts.size()
+ + ", Temporary: " + temporaryFactsCount);
+
+ // Additional details if FINEST is enabled
+ facts.logFacts(STATS_LOG, Level.FINEST);
}
/**
@@ -644,54 +662,48 @@ public class RulesEngine<T extends Ruleset> {
}
protected void publishRulesEngineStatus() {
- withLock(getClass().getSimpleName() + "::publishRulesEngineStatus", () -> {
-
- String engineId = id == null ? null : id.getRealm().orElse(id.getAssetId().orElse(null));
- int compilationErrors = getCompilationErrorDeploymentCount();
- int executionErrors = getExecutionErrorDeploymentCount();
- RulesEngineInfo engineInfo = new RulesEngineInfo(
- getStatus(),
- compilationErrors,
- executionErrors);
-
- RulesEngineStatusEvent event = new RulesEngineStatusEvent(
- timerService.getCurrentTimeMillis(),
- engineId,
- engineInfo
- );
+ String engineId = id == null ? null : id.getRealm().orElse(id.getAssetId().orElse(null));
+ int compilationErrors = getCompilationErrorDeploymentCount();
+ int executionErrors = getExecutionErrorDeploymentCount();
+ RulesEngineInfo engineInfo = new RulesEngineInfo(
+ getStatus(),
+ compilationErrors,
+ executionErrors);
+
+ RulesEngineStatusEvent event = new RulesEngineStatusEvent(
+ timerService.getCurrentTimeMillis(),
+ engineId,
+ engineInfo
+ );
- LOG.finer("Publishing rules engine status event: " + event);
+ LOG.finer("Publishing rules engine status event: " + event);
- // Notify clients
- clientEventService.publishEvent(event);
- });
+ // Notify clients
+ clientEventService.publishEvent(event);
}
protected void publishRulesetStatus(RulesetDeployment deployment) {
- withLock(getClass().getSimpleName() + "::publishRulesetStatus", () -> {
-
- Ruleset ruleset = deployment.ruleset;
- String engineId = id == null ? null : id.getRealm().orElse(id.getAssetId().orElse(null));
+ Ruleset ruleset = deployment.ruleset;
+ String engineId = id == null ? null : id.getRealm().orElse(id.getAssetId().orElse(null));
- ruleset.setStatus(deployment.getStatus());
- ruleset.setError(deployment.getErrorMessage());
+ ruleset.setStatus(deployment.getStatus());
+ ruleset.setError(deployment.getErrorMessage());
- RulesetChangedEvent event = new RulesetChangedEvent(
- timerService.getCurrentTimeMillis(),
- engineId,
- ruleset
- );
+ RulesetChangedEvent event = new RulesetChangedEvent(
+ timerService.getCurrentTimeMillis(),
+ engineId,
+ ruleset
+ );
- LOG.finer("Publishing ruleset status event: " + event);
+ LOG.finer("Publishing ruleset status event: " + event);
- // Notify clients
- clientEventService.publishEvent(event);
- });
+ // Notify clients
+ clientEventService.publishEvent(event);
}
protected void schedulePause(RulesetDeployment deployment) {
long delay = deployment.getValidTo() - timerService.getCurrentTimeMillis();
- LOG.info("Scheduling pause of ruleset at '" + new Date(deployment.getValidTo()).toString() + "' (" + delay + "ms): " + deployment.ruleset.getName());
+ LOG.info("Scheduling pause of ruleset at '" + new Date(deployment.getValidTo()) + " for " + DurationFormatUtils.formatDurationHMS(delay) +": " + deployment.ruleset.getName());
pauseTimers.put(deployment.getId(), executorService.schedule(() -> pauseRuleset(deployment), delay, TimeUnit.MILLISECONDS));
}
@@ -702,21 +714,18 @@ public class RulesEngine<T extends Ruleset> {
return;
}
- withLock(getClass().getSimpleName() + ":pauseRuleset", () -> {
- LOG.info("Pausing ruleset: " + deployment.getRuleset().getName());
- stopRuleset(deployment);
- deployment.updateValidity();
-
- if (deployment.hasExpired()) {
- deployment.setStatus(EXPIRED);
- publishRulesetStatus(deployment);
- } else {
- deployment.setStatus(PAUSED);
- publishRulesetStatus(deployment);
- UNPAUSE_SCHEDULER.accept(this, deployment);
- }
- });
+ LOG.info("Pausing ruleset: " + deployment.getRuleset().getName());
+ stopRuleset(deployment);
+ deployment.updateValidity();
+ if (deployment.hasExpired()) {
+ deployment.setStatus(EXPIRED);
+ publishRulesetStatus(deployment);
+ } else {
+ deployment.setStatus(PAUSED);
+ publishRulesetStatus(deployment);
+ UNPAUSE_SCHEDULER.accept(this, deployment);
+ }
}
protected void scheduleUnpause(RulesetDeployment deployment) {
@@ -732,10 +741,8 @@ public class RulesEngine<T extends Ruleset> {
return;
}
- withLock(getClass().getSimpleName() + "::unpauseRuleset", () -> {
- LOG.info("Un-pausing ruleset: " + deployment.getRuleset().getName());
- startRuleset(deployment);
- });
+ LOG.info("Un-pausing ruleset: " + deployment.getRuleset().getName());
+ startRuleset(deployment);
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
index b4ecc6ac6..62994332d 100644
--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java
+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java
@@ -21,8 +21,6 @@ package org.openremote.manager.rules;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerService;
-import org.openremote.model.Constants;
-import org.openremote.model.PersistenceEvent;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetProcessingException;
@@ -34,13 +32,15 @@ import org.openremote.manager.datapoint.AssetPredictedDatapointService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.notification.NotificationService;
-import org.openremote.manager.webhook.WebhookService;
import org.openremote.manager.rules.flow.FlowResourceImpl;
import org.openremote.manager.rules.geofence.GeofenceAssetAdapter;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
+import org.openremote.manager.webhook.WebhookService;
+import org.openremote.model.Constants;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
+import org.openremote.model.PersistenceEvent;
import org.openremote.model.asset.Asset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeEvent.Source;
@@ -55,27 +55,26 @@ import org.openremote.model.security.Realm;
import org.openremote.model.util.Pair;
import org.openremote.model.util.TextUtil;
import org.openremote.model.util.TimeUtil;
-import org.openremote.model.util.ValueUtil;
import org.openremote.model.value.MetaItemType;
import javax.persistence.EntityManager;
import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledExecutorService;
-import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
-import static java.util.logging.Level.*;
-import static java.util.stream.Collectors.toList;
+import static java.util.logging.Level.SEVERE;
import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;
import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;
+import static org.openremote.container.util.MapAccess.getInteger;
import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.manager.gateway.GatewayService.isNotForGateway;
-import static org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes;
/**
* Manages {@link RulesEngine}s for stored {@link Ruleset}s and processes asset attribute updates.
@@ -104,9 +103,20 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
public static final int PRIORITY = LOW_PRIORITY;
public static final String OR_RULE_EVENT_EXPIRES = "OR_RULE_EVENT_EXPIRES";
public static final String OR_RULE_EVENT_EXPIRES_DEFAULT = "PT1H";
+ /**
+ * This value defines the periodic firing of the rules engines, and therefore
+ * has an impact on system load. If a temporary fact has a shorter expiration
+ * time, it's not guaranteed to be removed within that time. Any time-based
+ * operation, such as matching temporary facts in a sliding time window, must
+ * be designed with this margin in mind.
+ */
+ public static final String OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS = "OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS";
+ public static final int OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS_DEFAULT = 50000; // Just under a minute to catch 1 min timer rules
+ public static final String OR_RULES_QUICK_FIRE_MILLIS = "OR_RULES_QUICK_FIRE_MILLIS";
+ public static final int OR_RULES_QUICK_FIRE_MILLIS_DEFAULT = 3000;
private static final Logger LOG = Logger.getLogger(RulesService.class.getName());
- protected final Map<String, RulesEngine<RealmRuleset>> realmEngines = new HashMap<>();
- protected final Map<String, RulesEngine<AssetRuleset>> assetEngines = new HashMap<>();
+ protected final ConcurrentMap<String, RulesEngine<RealmRuleset>> realmEngines = new ConcurrentHashMap<>();
+ protected final ConcurrentMap<String, RulesEngine<AssetRuleset>> assetEngines = new ConcurrentHashMap<>();
protected List<GeofenceAssetAdapter> geofenceAssetAdapters = new ArrayList<>();
protected TimerService timerService;
protected ScheduledExecutorService executorService;
@@ -124,14 +134,16 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
protected RulesEngine<GlobalRuleset> globalEngine;
protected Realm[] realms;
protected AssetLocationPredicateProcessor locationPredicateRulesConsumer;
- protected Map<RulesEngine<?>, List<RulesEngine.AssetStateLocationPredicates>> engineAssetLocationPredicateMap = new HashMap<>();
- protected Set<String> assetsWithModifiedLocationPredicates = new HashSet<>();
+ protected ConcurrentMap<RulesEngine<?>, List<RulesEngine.AssetStateLocationPredicates>> engineAssetLocationPredicateMap = new ConcurrentHashMap<>();
+ protected final Set<String> assetsWithModifiedLocationPredicates = new HashSet<>();
// Keep global list of asset states that have been pushed to any engines
// The objects are already in memory inside the rule engines but keeping them
// here means we can quickly insert facts into newly started engines
- protected Set<AssetState<?>> assetStates = new HashSet<>();
- protected Set<AssetState<?>> preInitAssetStates = new HashSet<>();
+ protected final Set<AssetState<?>> assetStates = new HashSet<>();
+ protected final Set<AssetState<?>> preInitAssetStates = new HashSet<>();
protected long defaultEventExpiresMillis = 1000*60*60;
+ protected long tempFactExpirationMillis;
+ protected long quickFireMillis;
protected boolean initDone;
protected boolean startDone;
@@ -156,6 +168,9 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
clientEventService = container.getService(ClientEventService.class);
gatewayService = container.getService(GatewayService.class);
+ tempFactExpirationMillis = getInteger(container.getConfig(), OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS, OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS_DEFAULT);
+ quickFireMillis = getInteger(container.getConfig(), OR_RULES_QUICK_FIRE_MILLIS, OR_RULES_QUICK_FIRE_MILLIS_DEFAULT);
+
if (initDone) {
return;
}
@@ -341,7 +356,9 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
globalEngine = null;
}
- assetStates.clear();
+ synchronized (assetStates) {
+ assetStates.clear();
+ }
});
for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {
@@ -418,20 +435,17 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
}
public GeofenceDefinition[] getAssetGeofences(String assetId) {
- return withLockReturning(getClass().getSimpleName() + "::getAssetGeofences", () -> {
-
- LOG.finest("Requesting geofences for asset: " + assetId);
+ LOG.finest("Requesting geofences for asset: " + assetId);
- for (GeofenceAssetAdapter geofenceAdapter : geofenceAssetAdapters) {
- GeofenceDefinition[] geofences = geofenceAdapter.getAssetGeofences(assetId);
- if (geofences != null) {
- LOG.finest("Retrieved geofences from geofence adapter '" + geofenceAdapter.getName() + "' for asset: " + assetId);
- return geofences;
- }
+ for (GeofenceAssetAdapter geofenceAdapter : geofenceAssetAdapters) {
+ GeofenceDefinition[] geofences = geofenceAdapter.getAssetGeofences(assetId);
+ if (geofences != null) {
+ LOG.finest("Retrieved geofences from geofence adapter '" + geofenceAdapter.getName() + "' for asset: " + assetId);
+ return geofences;
}
+ }
- return new GeofenceDefinition[0];
- });
+ return new GeofenceDefinition[0];
}
protected void processRealmChange(Realm realm, PersistenceEvent.Cause cause) {
@@ -521,18 +535,19 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
.stream()
.filter(RulesService::attributeIsRuleState).toList();
+ // Just retract all old attributes rather than compare every value that might cause asset state to mutate
oldStateAttributes.forEach(attribute -> {
AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);
- LOG.finer("Asset was persisted (" + persistenceEvent.getCause() + "), retracting fact: " + assetState);
+ LOG.finer("Asset was persisted (" + persistenceEvent.getCause() + "), retracting obsolete fact: " + assetState);
retractAssetState(assetState);
});
- // Insert new states
+ // Insert new states for new or changed attributes
newStateAttributes.forEach(attribute -> {
- AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);
- LOG.finer("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + assetState);
- updateAssetState(assetState);
- });
+ AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);
+ LOG.finer("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + assetState);
+ updateAssetState(assetState);
+ });
break;
}
case DELETE:
@@ -566,8 +581,10 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
RulesEngine<GlobalRuleset> engine = deployGlobalRuleset((GlobalRuleset) ruleset);
if (isNewEngine) {
- // Push all existing facts into the engine
- assetStates.forEach(assetState -> engine.updateOrInsertAssetState(assetState, true));
+ synchronized (assetStates) {
+ // Push all existing facts into the engine
+ assetStates.forEach(assetState -> engine.updateOrInsertAssetState(assetState, true));
+ }
}
engine.start();
@@ -579,11 +596,13 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
if (isNewEngine) {
// Push all existing facts into the engine
- assetStates.forEach(assetState -> {
- if (assetState.getRealm().equals(((RealmRuleset) ruleset).getRealm())) {
- engine.updateOrInsertAssetState(assetState, true);
- }
- });
+ synchronized (assetStates) {
+ assetStates.forEach(assetState -> {
+ if (assetState.getRealm().equals(((RealmRuleset) ruleset).getRealm())) {
+ engine.updateOrInsertAssetState(assetState, true);
+ }
+ });
+ }
}
engine.start();
@@ -617,6 +636,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
if (globalEngine == null) {
globalEngine = new RulesEngine<>(
timerService,
+ this,
identityService,
executorService,
assetStorageService,
@@ -658,6 +678,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
.computeIfAbsent(ruleset.getRealm(), (realm) ->
new RulesEngine<>(
timerService,
+ this,
identityService,
executorService,
assetStorageService,
@@ -735,6 +756,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
.computeIfAbsent(ruleset.getAssetId(), (assetId) ->
new RulesEngine<>(
timerService,
+ this,
identityService,
executorService,
assetStorageService,
@@ -769,39 +791,22 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
}
protected void insertAssetEvent(AssetState<?> assetState, long expiresMillis) {
- withLock(getClass().getSimpleName() + "::insertAssetEvent", () -> {
- // Get the chain of rule engines that we need to pass through
- List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());
-
- // Check that all engines in the scope are available
- if (rulesEngines.stream().anyMatch(RulesEngine::isError)) {
- LOG.severe("At least one rules engine is in an error state, skipping: " + assetState);
- if (LOG.isLoggable(FINEST)) {
- for (RulesEngine<?> rulesEngine : rulesEngines) {
- if (rulesEngine.isError()) {
- LOG.log(FINEST, "Rules engine error state: " + rulesEngine, rulesEngine.getError());
- }
- }
- }
- return;
- }
+ // Get the chain of rule engines that we need to pass through
+ List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());
- // Pass through each engine
- for (RulesEngine<?> rulesEngine : rulesEngines) {
- rulesEngine.insertAssetEvent(expiresMillis, assetState);
- }
- });
+ // Pass through each engine
+ for (RulesEngine<?> rulesEngine : rulesEngines) {
+ rulesEngine.insertAssetEvent(expiresMillis, assetState);
+ }
}
protected void updateAssetState(AssetState<?> assetState) {
- withLock(getClass().getSimpleName() + "::updateAssetState", () -> {
- // TODO: implement rules processing error state handling
-
- LOG.finer("Updating asset state: " + assetState);
+ LOG.finer("Updating asset state: " + assetState);
- // Get the chain of rule engines that we need to pass through
- List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());
+ // Get the chain of rule engines that we need to pass through
+ List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());
+ synchronized (assetStates) {
// Remove asset state with same attribute ref as new state, add new state
boolean inserted = !assetStates.remove(assetState);
assetStates.add(assetState);
@@ -810,32 +815,31 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
for (RulesEngine<?> rulesEngine : rulesEngines) {
rulesEngine.updateOrInsertAssetState(assetState, inserted);
}
- });
+ }
}
protected void retractAssetState(AssetState<?> assetState) {
// Get the chain of rule engines that we need to pass through
List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());
- // Remove asset state with same attribute ref
- assetStates.remove(assetState);
+ synchronized (assetStates) {
+ // Remove asset state with same attribute ref
+ assetStates.remove(assetState);
- if (rulesEngines.size() == 0) {
- LOG.finer("Ignoring as there are no matching rules engines: " + assetState);
- }
-
- // Pass through each rules engine
- for (RulesEngine<?> rulesEngine : rulesEngines) {
- rulesEngine.removeAssetState(assetState);
+ // Pass through each rules engine
+ for (RulesEngine<?> rulesEngine : rulesEngines) {
+ rulesEngine.removeAssetState(assetState);
+ }
}
}
protected List<AssetState<?>> getAssetStatesInScope(String assetId) {
- return withLockReturning(getClass().getSimpleName() + "::getAssetStatesInScope", () ->
- assetStates
+ synchronized (assetStates) {
+ return assetStates
.stream()
.filter(assetState -> Arrays.asList(assetState.getPath()).contains(assetId))
- .collect(Collectors.toList()));
+ .collect(Collectors.toList());
+ }
}
protected List<RulesEngine<?>> getEnginesInScope(String realm, String[] assetPath) {
@@ -881,7 +885,9 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
* have modified {@link LocationAttributePredicate}s and to notify the {@link GeofenceAssetAdapter}s.
*/
protected void onEngineLocationRulesChanged(RulesEngine<?> rulesEngine, List<RulesEngine.AssetStateLocationPredicates> newEngineAssetStateLocationPredicates) {
- withLock(getClass().getSimpleName() + "::onEngineLocationRulesChanged", () -> {
+
+ synchronized (assetsWithModifiedLocationPredicates) {
+
int initialModifiedCount = assetsWithModifiedLocationPredicates.size();
if (newEngineAssetStateLocationPredicates == null) {
@@ -953,11 +959,12 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
if (assetsWithModifiedLocationPredicates.size() != initialModifiedCount) {
processModifiedGeofences();
}
- });
+ }
}
protected void processModifiedGeofences() {
- withLock(getClass().getSimpleName() + "::processModifiedGeofences", () -> {
+
+ synchronized (assetsWithModifiedLocationPredicates) {
LOG.finest("Processing geofence modifications: modified asset geofence count=" + assetsWithModifiedLocationPredicates.size());
try {
@@ -973,9 +980,9 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
engineAssetLocationPredicateMap.forEach((rulesEngine, engineAssetStateLocationPredicates) ->
engineAssetStateLocationPredicates.stream().filter(
- assetStateLocationPredicates ->
- assetStateLocationPredicates.getAssetId().equals(
- assetId))
+ assetStateLocationPredicates ->
+ assetStateLocationPredicates.getAssetId().equals(
+ assetId))
.findFirst()
.ifPresent(
assetStateLocationPredicate -> {
@@ -1002,7 +1009,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
// Clear modified assets ready for next batch
assetsWithModifiedLocationPredicates.clear();
}
- });
+ }
}
protected Optional<RulesetDeployment> getRulesetDeployment(Long rulesetId) {
@@ -1038,7 +1045,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse
String realm = assetStates.get(0).getRealm();
String[] assetPaths = assetStates.stream().flatMap(assetState -> Arrays.stream(assetState.getPath())).toArray(String[]::new);
for (RulesEngine<?> rulesEngine : getEnginesInScope(realm, assetPaths)) {
- rulesEngine.fireAllDeploymentsWithPredictedData();
+ rulesEngine.scheduleFire(false);
}
}
}
diff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
index 032b2fb78..229b87ac9 100644
--- a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java
@@ -42,15 +42,12 @@ import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.ValueUtil;
import java.util.*;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
-import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;
import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;
import static org.openremote.manager.gateway.GatewayService.isNotForGateway;
@@ -78,9 +75,9 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe
protected GatewayService gatewayService;
protected ManagerIdentityService identityService;
protected ScheduledExecutorService executorService;
- protected Map<String, String> consoleIdRealmMap;
+ protected ConcurrentMap<String, String> consoleIdRealmMap = new ConcurrentHashMap<>();
protected ScheduledFuture<?> notifyAssetsScheduledFuture;
- protected Set<String> notifyAssets;
+ protected final Set<String> notifyAssets = new HashSet<>();
@Override
public int getPriority() {
@@ -100,9 +97,6 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe
@Override
public void start(Container container) throws Exception {
- // Find all console assets that use this adapter
- consoleIdRealmMap = new HashMap<>();
-
assetStorageService.findAll(
new AssetQuery()
.select(new AssetQuery.Select().attributes(ConsoleAsset.CONSOLE_PROVIDERS.getName()))
@@ -141,15 +135,9 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe
@Override
public void processLocationPredicates(List<RulesEngine.AssetStateLocationPredicates> modifiedAssetLocationPredicates) {
+ AtomicBoolean notifierDebounce = new AtomicBoolean(false);
- withLock(getClass().getSimpleName() + "::processLocationPredicates", () -> {
-
- AtomicBoolean notifierDebounce = new AtomicBoolean(false);
-
- if (notifyAssets == null) {
- notifyAssets = new HashSet<>(modifiedAssetLocationPredicates.size());
- }
-
+ synchronized (notifyAssets) {
// Remove all entries that relate to consoles that are compatible with this adapter
modifiedAssetLocationPredicates.removeIf(assetStateLocationPredicates -> {
boolean remove = consoleIdRealmMap.containsKey(assetStateLocationPredicates.getAssetId());
@@ -197,17 +185,17 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe
if (notifierDebounce.get()) {
if (notifyAssetsScheduledFuture == null || notifyAssetsScheduledFuture.cancel(false)) {
- notifyAssetsScheduledFuture = executorService.schedule(() ->
- withLock(getClass().getSimpleName() + "::notifyAssets",
- () -> {
- notifyAssetGeofencesChanged(notifyAssets);
- notifyAssets = null;
- notifyAssetsScheduledFuture = null;
- }),
+ notifyAssetsScheduledFuture = executorService.schedule(() -> {
+ synchronized (notifyAssets) {
+ notifyAssetGeofencesChanged(notifyAssets);
+ notifyAssets.clear();
+ notifyAssetsScheduledFuture = null;
+ }
+ },
NOTIFY_ASSETS_DEBOUNCE_MILLIS, TimeUnit.MILLISECONDS);
}
}
- });
+ }
}
@Override
@@ -283,24 +271,22 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe
protected void processConsoleAssetChange(PersistenceEvent<ConsoleAsset> persistenceEvent) {
ConsoleAsset asset = persistenceEvent.getEntity();
- withLock(getClass().getSimpleName() + "::processAssetChange", () -> {
- switch (persistenceEvent.getCause()) {
+ switch (persistenceEvent.getCause()) {
- case CREATE:
- case UPDATE:
-
- if (isLinkedToORConsoleGeofenceAdapter(asset)) {
- consoleIdRealmMap.put(asset.getId(), asset.getRealm());
- } else {
- consoleIdRealmMap.remove(asset.getId());
- }
- break;
- case DELETE:
+ case CREATE:
+ case UPDATE:
+ if (isLinkedToORConsoleGeofenceAdapter(asset)) {
+ consoleIdRealmMap.put(asset.getId(), asset.getRealm());
+ } else {
consoleIdRealmMap.remove(asset.getId());
- break;
- }
- });
+ }
+ break;
+ case DELETE:
+
+ consoleIdRealmMap.remove(asset.getId());
+ break;
+ }
}
protected static boolean isLinkedToORConsoleGeofenceAdapter(ConsoleAsset asset) {
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
index 1da1a1657..57c5ac127 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -33,6 +33,7 @@ import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
import org.openremote.container.timer.TimerService;
+import org.openremote.container.util.UniqueIdentifierGenerator;
import org.openremote.container.web.WebService;
import org.openremote.manager.apps.ConsoleAppService;
import org.openremote.manager.asset.AssetStorageService;
@@ -42,6 +43,7 @@ import org.openremote.model.Container;
import org.openremote.model.PersistenceEvent;
import org.openremote.model.asset.Asset;
import org.openremote.model.auth.OAuthGrant;
+import org.openremote.model.auth.OAuthPasswordGrant;
import org.openremote.model.event.shared.RealmFilter;
import org.openremote.model.gateway.GatewayConnection;
import org.openremote.model.provisioning.ProvisioningConfig;
@@ -111,12 +113,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
public void init(Container container) {
super.init(container);
this.container = container;
- OAuthGrant grant = loadCredentials();
-
- // Update the keycloak proxy credentials to use stored credentials
- if (grant != null) {
- setActiveCredentials(grant);
- }
String hostname = getString(container.getConfig(), Constants.OR_HOSTNAME, null);
int port = getInteger(container.getConfig(), Constants.OR_SSL_PORT, -1); // Should just be called port
@@ -158,6 +154,66 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
}
+ @Override
+ protected OAuthGrant getStoredCredentials(Container container) {
+ // Try and load keycloak proxy credentials from the file system
+ String grantFile = getString(container.getConfig(), OR_KEYCLOAK_GRANT_FILE, OR_KEYCLOAK_GRANT_FILE_DEFAULT);
+ Path grantPath = TextUtil.isNullOrEmpty(grantFile) ? null : Paths.get(grantFile);
+ OAuthGrant grant = null;
+
+ if (grantPath != null && Files.isReadable(grantPath)) {
+ LOG.info("Loading OR_KEYCLOAK_GRANT_FILE: " + grantFile);
+
+ try (InputStream is = Files.newInputStream(grantPath)) {
+ String grantJson = IOUtils.toString(is, StandardCharsets.UTF_8);
+ grant = ValueUtil.parse(grantJson, OAuthGrant.class).orElseGet(() -> {
+ LOG.warning("Failed to load OR_KEYCLOAK_GRANT_FILE: " + grantFile);
+ return null;
+ });
+ } catch (Exception ex) {
+ throw new ExceptionInInitializerError(ex);
+ }
+ }
+ return grant;
+ }
+
+ @Override
+ protected OAuthGrant generateStoredCredentials(Container container) {
+ String grantFile = getString(container.getConfig(), OR_KEYCLOAK_GRANT_FILE, OR_KEYCLOAK_GRANT_FILE_DEFAULT);
+
+ if (TextUtil.isNullOrEmpty(grantFile)) {
+ return null;
+ }
+
+ // Create a new super user for the keycloak proxy so admin user can be modified if desired
+ User keycloakProxyUser = new User()
+ .setUsername(MANAGER_CLIENT_ID)
+ .setEnabled(true)
+ .setSystemAccount(true);
+ String password = UniqueIdentifierGenerator.generateId();
+ keycloakProxyUser = createUpdateUser(MASTER_REALM, keycloakProxyUser, password, true);
+
+ // Make this proxy user a super user by giving them admin realm role
+ updateUserRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), addRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), REALM_ADMIN_ROLE));
+
+ // Use same details as default keycloak grant but change the username and password to our new user
+ OAuthPasswordGrant grant = getDefaultKeycloakGrant(container);
+ grant.setUsername(keycloakProxyUser.getUsername()).setPassword(password);
+ Path grantPath = Paths.get(grantFile);
+
+ try {
+ Files.write(grantPath, ValueUtil.asJSON(grant).orElse("null").getBytes(),
+ StandardOpenOption.CREATE,
+ StandardOpenOption.WRITE,
+ StandardOpenOption.TRUNCATE_EXISTING);
+ } catch (Exception e) {
+ LOG.info("Failed to write " + OR_KEYCLOAK_GRANT_FILE + ": " + grantFile);
+ return null;
+ }
+
+ return grant;
+ }
+
@Override
protected void addClientRedirectUris(String client, List<String> redirectUrls, boolean devMode) {
// Callback URL used by Manager web client authentication, any relative path to "ourselves" is fine
@@ -955,52 +1011,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
});
}
- /**
- * Load keycloak proxy credentials from file system
- */
- public OAuthGrant loadCredentials() {
- // Try and load keycloak proxy credentials from file
- String grantFile = getString(container.getConfig(), OR_KEYCLOAK_GRANT_FILE, OR_KEYCLOAK_GRANT_FILE_DEFAULT);
- Path grantPath = TextUtil.isNullOrEmpty(grantFile) ? null : Paths.get(grantFile);
- OAuthGrant grant = null;
-
- if (grantPath != null && Files.isReadable(grantPath)) {
- LOG.info("Loading OR_KEYCLOAK_GRANT_FILE: " + grantFile);
-
- try (InputStream is = Files.newInputStream(grantPath)) {
- String grantJson = IOUtils.toString(is, StandardCharsets.UTF_8);
- grant = ValueUtil.parse(grantJson, OAuthGrant.class).orElseGet(() -> {
- LOG.warning("Failed to load OR_KEYCLOAK_GRANT_FILE: " + grantFile);
- return null;
- });
- } catch (Exception ex) {
- throw new ExceptionInInitializerError(ex);
- }
- }
- return grant;
- }
-
- /**
- * Save Keycloak proxy credentials to the file system
- */
- public void saveCredentials(OAuthGrant grant) {
- String grantFile = getString(container.getConfig(), OR_KEYCLOAK_GRANT_FILE, OR_KEYCLOAK_GRANT_FILE_DEFAULT);
-
- if (TextUtil.isNullOrEmpty(grantFile)) {
- return;
- }
- Path grantPath = Paths.get(grantFile);
-
- try {
- Files.write(grantPath, ValueUtil.asJSON(grant).orElse("null").getBytes(),
- StandardOpenOption.CREATE,
- StandardOpenOption.WRITE,
- StandardOpenOption.TRUNCATE_EXISTING);
- } catch (Exception e) {
- LOG.info("Failed to write OR_KEYCLOAK_GRANT_FILE: " + grantFile);
- }
- }
-
public ClientRepresentation generateOpenRemoteClientRepresentation() {
ClientRepresentation client = new ClientRepresentation();
client.setClientId(KEYCLOAK_CLIENT_ID);
diff --git a/manager/src/main/java/org/openremote/manager/security/RemotingConnectionPrincipal.java b/manager/src/main/java/org/openremote/manager/security/RemotingConnectionPrincipal.java
new file mode 100644
index 000000000..6e3472292
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/security/RemotingConnectionPrincipal.java
@@ -0,0 +1,56 @@
+/*
+ * 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.security;
+
+import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
+
+import javax.security.auth.Subject;
+import java.security.Principal;
+
+/**
+ * A {@link Principal} for associating an ActiveMQ {@link RemotingConnection} with the {@link javax.security.auth.Subject}
+ */
+// TODO: Remove this if/when ActiveMQ implements https://issues.apache.org/jira/browse/ARTEMIS-4059
+public class RemotingConnectionPrincipal implements Principal {
+
+ protected RemotingConnection remotingConnection;
+
+ public RemotingConnectionPrincipal(RemotingConnection remotingConnection) {
+ this.remotingConnection = remotingConnection;
+ }
+
+ @Override
+ public String getName() {
+ return null;
+ }
+
+ public RemotingConnection getRemotingConnection() {
+ return remotingConnection;
+ }
+
+ public static RemotingConnection getRemotingConnectionFromSubject(Subject subject) {
+ return subject != null ? subject.getPrincipals()
+ .stream()
+ .filter(p -> p instanceof RemotingConnectionPrincipal)
+ .findFirst()
+ .map(p -> ((RemotingConnectionPrincipal)p).getRemotingConnection())
+ .orElse(null) : null;
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java b/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java
index 229fe202e..7933fcc5e 100644
--- a/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/KeycloakCleanSetup.java
@@ -27,8 +27,7 @@ import java.util.Arrays;
import java.util.logging.Logger;
import static org.openremote.container.security.keycloak.KeycloakIdentityProvider.DEFAULT_CLIENTS;
-import static org.openremote.model.Constants.MASTER_REALM;
-import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER;
+import static org.openremote.model.Constants.*;
public class KeycloakCleanSetup extends AbstractKeycloakSetup {
@@ -41,9 +40,6 @@ public class KeycloakCleanSetup extends AbstractKeycloakSetup {
@Override
public void onStart() throws Exception {
super.onStart();
-
- // Switch keycloak proxy back to admin cli
- keycloakProvider.setActiveCredentials(keycloakProvider.getDefaultKeycloakGrant(container));
doClean();
}
@@ -58,7 +54,7 @@ public class KeycloakCleanSetup extends AbstractKeycloakSetup {
LOG.info("Deleting all non-master admin users");
Arrays.stream(keycloakProvider.queryUsers(new UserQuery().realm(new RealmPredicate(MASTER_REALM)))).forEach(user -> {
- if (!user.getUsername().equals(MASTER_REALM_ADMIN_USER)) {
+ if (!user.getUsername().equals(MASTER_REALM_ADMIN_USER) && !user.getUsername().equals(MANAGER_CLIENT_ID)) {
LOG.info("Deleting user: " + user);
keycloakProvider.deleteUser(MASTER_REALM, user.getId());
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java b/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java
index f7b407029..fd7ecf249 100644
--- a/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/KeycloakInitSetup.java
@@ -19,16 +19,13 @@
*/
package org.openremote.manager.setup;
-import org.openremote.container.util.UniqueIdentifierGenerator;
import org.openremote.model.Container;
-import org.openremote.model.auth.OAuthPasswordGrant;
import org.openremote.model.security.ClientRole;
import org.openremote.model.security.Realm;
import org.openremote.model.security.User;
import java.util.logging.Logger;
-import static org.openremote.container.security.keycloak.KeycloakIdentityProvider.MANAGER_CLIENT_ID;
import static org.openremote.model.Constants.*;
public class KeycloakInitSetup extends AbstractKeycloakSetup {
@@ -62,22 +59,5 @@ public class KeycloakInitSetup extends AbstractKeycloakSetup {
// Give admin all roles on application client level
keycloakProvider.updateUserRoles(MASTER_REALM, adminUser.getId(), KEYCLOAK_CLIENT_ID, ClientRole.READ.getValue(), ClientRole.WRITE.getValue());
-
- // Create a new super user for the keycloak proxy so admin user can be modified if desired
- User keycloakProxyUser = new User()
- .setUsername(MANAGER_CLIENT_ID)
- .setEnabled(true)
- .setSystemAccount(true);
- String password = UniqueIdentifierGenerator.generateId();
- keycloakProxyUser = keycloakProvider.createUpdateUser(MASTER_REALM, keycloakProxyUser, password, true);
-
- // Make this proxy user a super user by giving them admin realm role
- keycloakProvider.updateUserRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), keycloakProvider.addRealmRoles(MASTER_REALM, keycloakProxyUser.getId(), REALM_ADMIN_ROLE));
-
- // Update the grant to match the proxy user and make this grant active
- OAuthPasswordGrant grant = keycloakProvider.getDefaultKeycloakGrant(container);
- grant.setUsername(keycloakProxyUser.getUsername()).setPassword(password);
- keycloakProvider.setActiveCredentials(grant);
- keycloakProvider.saveCredentials(grant);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/setup/SetupService.java b/manager/src/main/java/org/openremote/manager/setup/SetupService.java
index 4f9e4bc49..2bdd59c47 100644
--- a/manager/src/main/java/org/openremote/manager/setup/SetupService.java
+++ b/manager/src/main/java/org/openremote/manager/setup/SetupService.java
@@ -20,6 +20,7 @@
package org.openremote.manager.setup;
import org.openremote.container.persistence.PersistenceService;
+import org.openremote.container.security.IdentityService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
@@ -49,7 +50,7 @@ public class SetupService implements ContainerService {
@Override
public int getPriority() {
- return PersistenceService.PRIORITY + 10; // Start just after persistence service
+ return IdentityService.PRIORITY + 10;
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/syslog/SyslogService.java b/manager/src/main/java/org/openremote/manager/syslog/SyslogService.java
index 50b4f8519..7b134f936 100644
--- a/manager/src/main/java/org/openremote/manager/syslog/SyslogService.java
+++ b/manager/src/main/java/org/openremote/manager/syslog/SyslogService.java
@@ -19,6 +19,8 @@
*/
package org.openremote.manager.syslog;
+import org.openremote.container.timer.TimerService;
+import org.openremote.container.util.MapAccess;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
import org.openremote.container.persistence.PersistenceService;
@@ -32,6 +34,7 @@ import org.openremote.model.syslog.SyslogLevel;
import org.openremote.model.util.Pair;
import javax.persistence.TypedQuery;
+import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
@@ -44,18 +47,23 @@ import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
+import static java.time.temporal.ChronoUnit.DAYS;
+
/**
* Act as a JUL handler, publishes (some) log messages on the client event bus, stores
* (some, depending on {@link SyslogConfig}) log messages in the database.
*/
public class SyslogService extends Handler implements ContainerService {
+ public static final String OR_SYSLOG_LOG_LEVEL = "OR_SYSLOG_LOG_LEVEL";
+ public static final SyslogLevel OR_SYSLOG_LOG_LEVEL_DEFAULT = SyslogLevel.INFO;
+ public static final String OR_SYSLOG_MAX_AGE_DAYS = "OR_SYSLOG_MAX_AGE_DAYS";
+ public static final int OR_SYSLOG_MAX_AGE_DAYS_DEFAULT = 5;
private static final Logger LOG = Logger.getLogger(SyslogService.class.getName());
protected ScheduledExecutorService executorService;
protected PersistenceService persistenceService;
protected ClientEventService clientEventService;
-
protected SyslogConfig config;
final protected List<SyslogEvent> batch = new ArrayList<>();
@@ -90,34 +98,44 @@ public class SyslogService extends Handler implements ContainerService {
);
}
- // Default config: Store all INFO messages for five days
- config = new SyslogConfig(
- SyslogLevel.INFO, SyslogCategory.values(), 60 * 24 * 5
- );
+ int maxAgeDays = MapAccess.getInteger(container.getConfig(), OR_SYSLOG_MAX_AGE_DAYS, OR_SYSLOG_MAX_AGE_DAYS_DEFAULT);
+ SyslogLevel syslogLevel = Optional.ofNullable(MapAccess.getString(container.getConfig(), OR_SYSLOG_LOG_LEVEL, null)).map(SyslogLevel::valueOf).orElse(OR_SYSLOG_LOG_LEVEL_DEFAULT);
+ config = new SyslogConfig(syslogLevel, SyslogCategory.values(), maxAgeDays * 24 * 60);
}
@Override
public void start(Container container) throws Exception {
if (persistenceService != null) {
+
+ TimerService timerService = container.getService(TimerService.class);
+
// Flush batch every 3 seconds (wait 10 seconds for database (schema) to be ready in dev mode)
flushBatchFuture = executorService.scheduleAtFixedRate(this::flushBatch, 10, 3, TimeUnit.SECONDS);
- // Clear outdated events every minute
- deleteOldFuture = executorService.scheduleAtFixedRate(() -> {
- // Not ready on startup
- if (persistenceService.getEntityManagerFactory() == null)
- return;
- final int maxAgeMinutes;
- synchronized (batch) {
- maxAgeMinutes = config.getStoredMaxAgeMinutes();
- }
- persistenceService.doTransaction(em -> {
- em.createQuery(
- "delete from SyslogEvent e " +
- "where e.timestamp < now() - make_interval(0, 0, 0, 0, 0, :minutes, 0)"
- ).setParameter("minutes", maxAgeMinutes).executeUpdate();
- });
- }, 60, 60, TimeUnit.SECONDS);
+ // Clear outdated events once a day
+ if (config.getStoredMaxAgeMinutes() > 0) {
+ // Schedule purge at approximately 3AM daily
+ long initialDelay = ChronoUnit.MILLIS.between(
+ timerService.getNow(),
+ timerService.getNow().truncatedTo(DAYS).plus(27, ChronoUnit.HOURS));
+
+ deleteOldFuture = executorService.scheduleAtFixedRate(
+ this::purgeSyslog,
+ initialDelay,
+ Duration.ofDays(1).toMillis(), TimeUnit.MILLISECONDS
+ );
+ }
+ }
+ }
+
+ protected void purgeSyslog() {
+ try {
+ persistenceService.doTransaction(em -> em.createQuery(
+ "delete from SyslogEvent e " +
+ "where e.timestamp < now() - make_interval(0, 0, 0, 0, 0, :minutes, 0)"
+ ).setParameter("minutes", config.getStoredMaxAgeMinutes()).executeUpdate());
+ } catch (Throwable e) {
+ LOG.log(Level.WARNING, "Failed to purge syslog events", e);
}
}
diff --git a/manager/src/main/resources/META-INF/services/org.openremote.model.EntityClassProvider b/manager/src/main/resources/META-INF/services/org.openremote.model.EntityClassProvider
deleted file mode 100644
index e69de29bb..000000000
diff --git a/manager/src/main/resources/logging-dev.properties b/manager/src/main/resources/logging-dev.properties
index 772fb962c..7f31da063 100644
--- a/manager/src/main/resources/logging-dev.properties
+++ b/manager/src/main/resources/logging-dev.properties
@@ -10,7 +10,7 @@ handlers=java.util.logging.ConsoleHandler, \
# Log extra data to file for debug purposes
#handlers=java.util.logging.ConsoleHandler, \
-# java.util.logging.FileHandler \
+# java.util.logging.FileHandler, \
# org.openremote.manager.syslog.SyslogService
###############################################################################
diff --git a/manager/src/main/resources/logging-trace.properties b/manager/src/main/resources/logging-trace.properties
new file mode 100644
index 000000000..bced75ba7
--- /dev/null
+++ b/manager/src/main/resources/logging-trace.properties
@@ -0,0 +1,114 @@
+# A logging configuration to give traceability of where time is being spent
+# in the system for identifying potential issues - Uses a file handler to avoid
+# generating too much console handler traffic
+
+###############################################################################
+# Output handlers
+###############################################################################
+
+# Log on STDOUT and internal syslog service (monitoring in Manager UI)
+handlers=java.util.logging.ConsoleHandler, \
+ java.util.logging.FileHandler, \
+ org.openremote.manager.syslog.SyslogService
+
+###############################################################################
+# Console handler
+###############################################################################
+
+# Set to FINEST to get all enabled log categories on console handler
+java.util.logging.ConsoleHandler.level=INFO
+
+# Console format, you can colorize this in IntelliJ IDEA "Run Application " console
+# with the GrepConsole plugin and after copying the configuration file of your
+# choice to ~/Library/Preferences/IntelliJ*/options/GrepConsole.xml:
+#
+# - tools/intellijTheme-Default-GrepConsole.xml
+# - tools/intellijTheme-Darcula-GrepConsole.xml
+#
+java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogFormatter
+
+###############################################################################
+# File handler
+###############################################################################
+
+# Set to FINEST to get all enabled log categories on file handler
+java.util.logging.FileHandler.level=FINEST
+
+# Output file and format
+java.util.logging.FileHandler.pattern=/deployment/openremote.log
+java.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter
+# Rotate between 10 log files
+java.util.logging.FileHandler.count=10
+# 10 mb per file
+java.util.logging.FileHandler.limit=10000000
+# Set to false to clear log on restart
+java.util.logging.FileHandler.append=false
+
+###############################################################################
+# Categories
+###############################################################################
+
+# The root logger category, the default for all categories not configured here
+.level=INFO
+
+# The root logger category of OpenRemote, FINE logs much more than INFO
+org.openremote.level=INFO
+
+# Rule execution logging
+#
+# - Use "global java.util.logging.Logger LOG;" in rule RHS to access this logger
+# - Set to FINEST to automatically log all fact additions, removals, and updates
+# - Set to INFO and prefix your rule name with "-" or "--" to log execution details
+org.openremote.rules.Rules.level=INFO
+
+# Set to FINE to get info about each processed event, including consumer and time taken to process
+org.openremote.manager.asset.AssetProcessingService.level=FINE
+
+# Set to FINE to get info about how long a thread holds the global lock
+org.openremote.container.concurrent.GlobalLock.level=FINE
+
+# Set to FINE to get info about how long each engine fire takes to process
+org.openremote.rules.RulesEngine.level=FINE
+
+# Set to FINE to get rules engine statistics periodically, FINEST to dump all facts
+org.openremote.rules.RulesEngineStats.level=OFF
+
+# Set to FINE to get CRUD query times (including hydration)
+org.openremote.manager.asset.AssetStorageService.level=FINE
+
+# Set to INFO or higher to debug internal messaging
+org.openremote.container.message.MessageBrokerContext.level=WARNING
+
+# Set to FINE to log full exception stack traces
+org.openremote.container.web.WebServiceExceptions.level=INFO
+
+# Set to SEVERE to log invalid access tokens
+org.keycloak.adapters.BearerTokenRequestAuthenticator.level=OFF
+
+# Set this to FINEST to log outgoing HTTP traffic
+org.apache.http.level=WARNING
+org.apache.http.impl.conn.DefaultClientConnection.level=WARNING
+org.apache.http.client.protocol.level=SEVERE
+
+# Set this to FINEST to log SQL execution
+org.hibernate.SQL.level=WARNING
+org.hibernate.type.descriptor.sql.BasicBinder.level=WARNING
+
+# Log only attribute event processing begin/end, useful to debug many events
+#org.openremote.container.web.level=FINE
+#org.openremote.manager.server.level=FINE
+#org.openremote.manager.server.asset.AssetProcessingService.level=FINE
+
+# Low-level debugging overrides, most of them are to stop chatty categories
+org.hibernate.level=WARNING
+org.hibernate.cfg.beanvalidation.TypeSafeActivator.level=SEVERE
+com.zaxxer.hikari.level=WARNING
+javax.management.level=WARNING
+org.xnio.level=WARNING
+org.apache.camel.level=INFO
+org.apache.camel.management.level=WARNING
+org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
+org.apache.camel.impl.DefaultShutdownStrategy.level=SEVERE
+org.jeasy.rules.level=WARNING
+org.postgresql.jdbc.level=INFO
+org.apache.activemq.audit.level=WARNING
diff --git a/manager/src/main/resources/logging.properties b/manager/src/main/resources/logging.properties
index 496c541f0..e2c69b240 100644
--- a/manager/src/main/resources/logging.properties
+++ b/manager/src/main/resources/logging.properties
@@ -48,7 +48,7 @@ org.openremote.rules.RulesEngineStats.level=OFF
org.openremote.container.message.MessageBrokerContext.level=WARNING
# Set to FINE to log full exception stack traces
-org.openremote.container.web.WebServiceExceptions.level=FINE
+org.openremote.container.web.WebServiceExceptions.level=INFO
# Set to SEVERE to log invalid access tokens
org.keycloak.adapters.BearerTokenRequestAuthenticator.level=OFF
diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20221219_HelperFunctions.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20221219_HelperFunctions.sql
new file mode 100644
index 000000000..a751ad8cb
--- /dev/null
+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20221219_HelperFunctions.sql
@@ -0,0 +1,44 @@
+/*
+ Some helper functions for performing CRUD operations on attributes and/or meta items
+ */
+CREATE OR REPLACE FUNCTION REMOVE_ATTRIBUTES(ASSET_ROW asset, VARIADIC ATTRIBUTE_NAMES text[]) RETURNS boolean AS $$
+BEGIN
+ UPDATE asset a
+ SET attributes = a.attributes - ATTRIBUTE_NAMES
+ WHERE a.ID = ASSET_ROW.ID;
+ RETURN FOUND;
+END
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION ADD_ATTRIBUTE(ASSET_ROW asset, ATTRIBUTE_NAME text, ATTRIBUTE_TYPE text, VALUE jsonb, TIME_STAMP timestamp with time zone, META jsonb) RETURNS boolean AS $$
+BEGIN
+ UPDATE asset a
+ SET attributes = a.attributes || (
+ SELECT jsonb_build_object(ATTRIBUTE_NAME, jsonb_build_object(
+ 'name', ATTRIBUTE_NAME,
+ 'type', ATTRIBUTE_TYPE,
+ 'value', VALUE,
+ 'timestamp', (extract(epoch from TIME_STAMP AT TIME ZONE 'UTC') * 1000)::bigint,
+ 'meta', '{}'::jsonb || coalesce(META, '{}'::jsonb))))
+ WHERE a.id = ASSET_ROW.id;
+ RETURN FOUND;
+END
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION REMOVE_META(ASSET_ROW asset, ATTRIBUTE_NAME text, VARIADIC META_NAMES text[]) RETURNS boolean AS $$
+BEGIN
+ UPDATE asset a
+ SET attributes = (a.attributes - ATTRIBUTE_NAME) || jsonb_build_object(ATTRIBUTE_NAME, (((a.attributes -> ATTRIBUTE_NAME) - 'meta') || jsonb_build_object('meta', (a.attributes -> ATTRIBUTE_NAME -> 'meta' || '{}'::jsonb) - META_NAMES)))
+ WHERE a.ID = ASSET_ROW.ID;
+ RETURN FOUND;
+END
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION ADD_UPDATE_META(ASSET_ROW asset, ATTRIBUTE_NAME text, META jsonb) RETURNS boolean AS $$
+BEGIN
+ UPDATE asset a
+ SET attributes = (a.attributes - ATTRIBUTE_NAME) || jsonb_build_object(ATTRIBUTE_NAME, (((a.attributes -> ATTRIBUTE_NAME) - 'meta') || jsonb_build_object('meta', (a.attributes -> ATTRIBUTE_NAME -> 'meta') || META || '{}'::jsonb)))
+ WHERE a.id = ASSET_ROW.id;
+ RETURN FOUND;
+END
+$$ LANGUAGE plpgsql;
diff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.java
index 0371fc8ef..89a660e11 100644
--- a/model/src/main/java/org/openremote/model/Constants.java
+++ b/model/src/main/java/org/openremote/model/Constants.java
@@ -25,6 +25,7 @@ import org.openremote.model.util.TsIgnore;
public interface Constants {
String KEYCLOAK_CLIENT_ID = "openremote";
+ String MANAGER_CLIENT_ID = "manager-keycloak";
String MASTER_REALM = "master";
String MASTER_REALM_ADMIN_USER = "admin";
String REALM_ADMIN_ROLE = "admin";
diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.java
index d734f8859..b4466e0d6 100644
--- a/model/src/main/java/org/openremote/model/asset/Asset.java
+++ b/model/src/main/java/org/openremote/model/asset/Asset.java
@@ -25,7 +25,6 @@ import org.hibernate.annotations.*;
import org.openremote.model.Constants;
import org.openremote.model.IdentifiableEntity;
import org.openremote.model.asset.impl.ThingAsset;
-import org.openremote.model.asset.impl.UnknownAsset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeMap;
import org.openremote.model.attribute.MetaMap;
@@ -208,7 +207,7 @@ import static org.openremote.model.Constants.*;
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE", discriminatorType = STRING)
@Check(constraints = "ID != PARENT_ID")
-@JsonTypeInfo(include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true, use = JsonTypeInfo.Id.CUSTOM, defaultImpl = UnknownAsset.class)
+@JsonTypeInfo(include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true, use = JsonTypeInfo.Id.CUSTOM, defaultImpl = ThingAsset.class)
@JsonTypeIdResolver(AssetTypeIdResolver.class)
@AssetValid(groups = Asset.AssetSave.class)
@DynamicUpdate
diff --git a/model/src/main/java/org/openremote/model/asset/agent/Agent.java b/model/src/main/java/org/openremote/model/asset/agent/Agent.java
index cfd75e942..3865dbf38 100644
--- a/model/src/main/java/org/openremote/model/asset/agent/Agent.java
+++ b/model/src/main/java/org/openremote/model/asset/agent/Agent.java
@@ -20,7 +20,6 @@
package org.openremote.model.asset.agent;
import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.impl.UnknownAsset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.MetaItem;
import org.openremote.model.auth.OAuthGrant;
@@ -281,7 +280,7 @@ public abstract class Agent<T extends Agent<T, U, V>, U extends Protocol<T>, V e
Arrays.stream(info.getAttributeDescriptors())
.anyMatch(ad -> ad.getName().equals(attributeName)))
.orElse(false)
- && ValueUtil.getAssetInfo(UnknownAsset.class)
+ && ValueUtil.getAssetInfo(Asset.class)
.map(typeInfo -> Arrays.stream(typeInfo.getAttributeDescriptors()).noneMatch(ad -> ad.getName().equals(attributeName)))
.orElse(false);
}
diff --git a/model/src/main/java/org/openremote/model/asset/impl/UnknownAsset.java b/model/src/main/java/org/openremote/model/asset/impl/UnknownAsset.java
index 44383aad8..a44899eb6 100644
--- a/model/src/main/java/org/openremote/model/asset/impl/UnknownAsset.java
+++ b/model/src/main/java/org/openremote/model/asset/impl/UnknownAsset.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020, OpenRemote Inc.
+ * Copyright 2022, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
@@ -20,23 +20,23 @@
package org.openremote.model.asset.impl;
import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.AssetDescriptor;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
/**
- * Fall back for unknown assets
+ * This is only needed so JPA can deserialise an Asset whose type doesn't match an entity otherwise it is un-used
*/
-@DiscriminatorValue("non null")
@Entity
-public class UnknownAsset extends Asset<UnknownAsset> {
-
- public static final AssetDescriptor<UnknownAsset> DESCRIPTOR = new AssetDescriptor<>("help-circle", null, UnknownAsset.class);
-
+@DiscriminatorValue("not null")
+public class UnknownAsset extends Asset<ThingAsset> {
/**
* For use by hydrators (i.e. JPA/Jackson)
*/
protected UnknownAsset() {
}
+
+ public UnknownAsset(String name) {
+ super(name);
+ }
}
diff --git a/model/src/main/java/org/openremote/model/console/ConsoleProvider.java b/model/src/main/java/org/openremote/model/console/ConsoleProvider.java
index 586f79abe..4112f0f3d 100644
--- a/model/src/main/java/org/openremote/model/console/ConsoleProvider.java
+++ b/model/src/main/java/org/openremote/model/console/ConsoleProvider.java
@@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.Serializable;
+import java.util.Objects;
public class ConsoleProvider implements Serializable {
protected String version;
@@ -78,4 +79,17 @@ public class ConsoleProvider implements Serializable {
public ObjectNode getData() {
return data;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ ConsoleProvider that = (ConsoleProvider) o;
+ return requiresPermission == that.requiresPermission && hasPermission == that.hasPermission && success == that.success && enabled == that.enabled && disabled == that.disabled && Objects.equals(version, that.version) && Objects.equals(data, that.data);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(version, requiresPermission, hasPermission, success, enabled, disabled, data);
+ }
}
diff --git a/model/src/main/java/org/openremote/model/jackson/AssetTypeIdResolver.java b/model/src/main/java/org/openremote/model/jackson/AssetTypeIdResolver.java
index b81ea826d..f4b0ef094 100644
--- a/model/src/main/java/org/openremote/model/jackson/AssetTypeIdResolver.java
+++ b/model/src/main/java/org/openremote/model/jackson/AssetTypeIdResolver.java
@@ -25,7 +25,7 @@ import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetDescriptor;
-import org.openremote.model.asset.impl.UnknownAsset;
+import org.openremote.model.asset.impl.ThingAsset;
import org.openremote.model.util.ValueUtil;
import java.io.IOException;
@@ -57,9 +57,10 @@ public class AssetTypeIdResolver extends TypeIdResolverBase {
return JsonTypeInfo.Id.CUSTOM;
}
+ @SuppressWarnings({"unchecked", "rawtypes"})
@Override
public JavaType typeFromId(DatabindContext context, String id) throws IOException {
- AssetDescriptor<?> descriptor = ValueUtil.getAssetDescriptor(id).orElse(UnknownAsset.DESCRIPTOR);
- return context.constructType(descriptor.getType());
+ Class<?> assetClass = ValueUtil.getAssetDescriptor(id).map(AssetDescriptor::getType).orElse((Class) ThingAsset.class);
+ return context.constructType(assetClass);
}
}
diff --git a/model/src/main/java/org/openremote/model/query/AssetQuery.java b/model/src/main/java/org/openremote/model/query/AssetQuery.java
index c8e36de68..f46eaaad2 100644
--- a/model/src/main/java/org/openremote/model/query/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/query/AssetQuery.java
@@ -19,18 +19,13 @@
*/
package org.openremote.model.query;
-import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.databind.util.StdConverter;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetDescriptor;
-import org.openremote.model.asset.agent.Agent;
import org.openremote.model.query.filter.*;
import org.openremote.model.util.ValueUtil;
import java.io.Serializable;
-import java.util.Arrays;
-import java.util.Comparator;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -185,39 +180,52 @@ public class AssetQuery implements Serializable {
public PathPredicate[] paths;
public RealmPredicate realm;
public String[] userIds;
- @JsonSerialize(contentConverter = AssetClassToStringConverter.class)
- @JsonDeserialize(contentConverter = StringToAssetClassConverter.class)
- public Class<? extends Asset<?>>[] types;
+ public String[] types;
public LogicGroup<AttributePredicate> attributes;
// Ordering
public OrderBy orderBy;
public int limit;
- public static class AssetClassToStringConverter extends StdConverter<Class<? extends Asset<?>>, String> {
-
- @Override
- public String convert(Class<? extends Asset<?>> value) {
- return value.getSimpleName();
- }
- }
-
- public static class StringToAssetClassConverter extends StdConverter<String, Class<? extends Asset<?>>> {
-
- @SuppressWarnings("unchecked")
- @Override
- public Class<? extends Asset<?>> convert(String value) {
- if (Agent.class.getSimpleName().equals(value)) {
- return (Class<? extends Asset<?>>)(Class<?>) Agent.class;
- }
- if (Asset.class.getSimpleName().equals(value)) {
- return (Class<? extends Asset<?>>)(Class<?>) Asset.class;
- }
-
- return ValueUtil.getAssetDescriptor(value).map(AssetDescriptor::getType).orElse(null);
- }
+ public AssetQuery() {
}
- public AssetQuery() {
+ /**
+ * Resolves the concrete {@link Asset} types that are covered by the supplied asset types
+ */
+ @SuppressWarnings("unchecked")
+ public static String[] getResolvedAssetTypes(String[] assetTypes) {
+
+ List<Class<? extends Asset<?>>> assetClasses = Arrays.asList(ValueUtil.getAssetClasses(null));
+ Set<String> assetSubTypes = new HashSet<>(Arrays.asList(assetTypes));
+
+ Arrays.stream(assetTypes)
+ .map(assetType ->
+ ValueUtil.getAssetClass(assetType).orElseGet(() -> {
+ // Try and resolve this type to a concrete asset super type class - should be in the type hierarchy
+ // of one of the registered asset types if it is a valid type
+ Class<? extends Asset<?>> matchedClass = null;
+
+ for (Class<?> c : assetClasses) {
+ Class<?> currentClass = c;
+ while (Asset.class.isAssignableFrom(currentClass)) {
+ if (currentClass.getSimpleName().equals(assetType)) {
+ matchedClass = (Class)currentClass;
+ break;
+ }
+ currentClass = currentClass.getSuperclass();
+ }
+ if (matchedClass != null) {
+ break;
+ }
+ }
+
+ return matchedClass;
+ }))
+ .filter(Objects::nonNull)
+ .flatMap(assetClass -> assetClasses.stream().filter(assetClass::isAssignableFrom).map(Class::getSimpleName))
+ .forEach(assetSubTypes::add);
+
+ return assetSubTypes.toArray(String[]::new);
}
public AssetQuery recursive(boolean recursive) {
@@ -289,32 +297,30 @@ public class AssetQuery implements Serializable {
@SuppressWarnings("unchecked")
@SafeVarargs
- public final AssetQuery types(AssetDescriptor<? extends Asset<?>>... types) {
+ public final <T extends Asset<?>> AssetQuery types(AssetDescriptor<T>...types) {
if (types == null || types.length == 0) {
this.types = null;
return this;
}
- this.types = Arrays.stream(types).map(AssetDescriptor::getType)
- .toArray(size -> (Class<? extends Asset<?>>[])new Class<?>[size]);
-
+ this.types = Arrays.stream(types).map(AssetDescriptor::getType).map(Class::getSimpleName)
+ .toArray(String[]::new);
return this;
}
- @SuppressWarnings("unchecked")
public final <T extends Asset<?>> AssetQuery types(Class<T> type) {
- this.types = new Class[] {type};
+ this.types = new String[] {type.getSimpleName()};
return this;
}
@SafeVarargs
- public final AssetQuery types(Class<? extends Asset<?>>... types) {
+ public final AssetQuery types(Class<? extends Asset<?>>...types) {
if (types == null || types.length == 0) {
this.types = null;
return this;
}
- this.types = types;
+ this.types = Arrays.stream(types).map(Class::getSimpleName).toArray(String[]::new);
return this;
}
diff --git a/model/src/main/java/org/openremote/model/rules/Ruleset.java b/model/src/main/java/org/openremote/model/rules/Ruleset.java
index 665f347f0..56f7c50fa 100644
--- a/model/src/main/java/org/openremote/model/rules/Ruleset.java
+++ b/model/src/main/java/org/openremote/model/rules/Ruleset.java
@@ -231,7 +231,7 @@ public abstract class Ruleset {
}
public boolean isContinueOnError() {
- return Optional.ofNullable(getMeta().get(CONTINUE_ON_ERROR)).map(node -> node.asBoolean(false)).orElse(false);
+ return Optional.ofNullable(getMeta().get(CONTINUE_ON_ERROR)).map(node -> node.asBoolean(true)).orElse(true);
}
public Ruleset setContinueOnError(boolean continueOnError) {
@@ -250,6 +250,7 @@ public abstract class Ruleset {
return this;
}
+ // TODO: Unify triggers for rulesets
public boolean isTriggerOnPredictedData() {
return Optional.ofNullable(getMeta().get(TRIGGER_ON_PREDICTED_DATA)).map(node -> node.asBoolean(false)).orElse(false);
}
diff --git a/model/src/main/java/org/openremote/model/rules/TemporaryFact.java b/model/src/main/java/org/openremote/model/rules/TemporaryFact.java
index d7c398f9d..8afb9884d 100644
--- a/model/src/main/java/org/openremote/model/rules/TemporaryFact.java
+++ b/model/src/main/java/org/openremote/model/rules/TemporaryFact.java
@@ -33,16 +33,6 @@ import java.time.temporal.ChronoUnit;
@TsIgnore
public class TemporaryFact<T> extends Event {
- /**
- * This value defines the periodic firing of the rules engines, and therefore
- * has an impact on system load. If a temporary fact has a shorter expiration
- * time, it's not guaranteed to be removed within that time. Any time-based
- * operation, such as matching temporary facts in a sliding time window, must
- * be designed with this margin in mind.
- */
- // TODO This is not true, need to add GlobalLockTimeout
- public static int GUARANTEED_MIN_EXPIRATION_MILLIS = 3000;
-
final protected long expirationMilliseconds;
final protected T fact;
// These are only useful for debugging, easier to read than timestamps
diff --git a/model/src/main/java/org/openremote/model/syslog/SyslogConfig.java b/model/src/main/java/org/openremote/model/syslog/SyslogConfig.java
index 10b63baec..ac6a89e14 100644
--- a/model/src/main/java/org/openremote/model/syslog/SyslogConfig.java
+++ b/model/src/main/java/org/openremote/model/syslog/SyslogConfig.java
@@ -35,7 +35,6 @@ public class SyslogConfig {
protected SyslogCategory[] storedCategories;
@NotNull
- @Range(max = 10080) // TODO 1 week max or we run out of space?
protected int storedMaxAgeMinutes;
public SyslogConfig() {
diff --git a/model/src/main/java/org/openremote/model/util/ValueUtil.java b/model/src/main/java/org/openremote/model/util/ValueUtil.java
index 8e72d7fe5..7e5b7e78c 100644
--- a/model/src/main/java/org/openremote/model/util/ValueUtil.java
+++ b/model/src/main/java/org/openremote/model/util/ValueUtil.java
@@ -41,6 +41,7 @@ import org.openremote.model.asset.AssetTypeInfo;
import org.openremote.model.asset.agent.Agent;
import org.openremote.model.asset.agent.AgentDescriptor;
import org.openremote.model.asset.agent.AgentLink;
+import org.openremote.model.asset.impl.UnknownAsset;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.value.*;
@@ -535,13 +536,16 @@ public class ValueUtil {
return assetTypeMap.values().toArray(new Class[0]);
}
- public static Optional<AssetTypeInfo> getAssetInfo(Class<? extends Asset<?>> assetType) {
+ public static Optional<Class<? extends Asset<?>>> getAssetClass(String assetType) {
+ return Optional.ofNullable(assetTypeMap.get(assetType));
+ }
+
+ public static <T extends Asset<?>> Optional<AssetTypeInfo> getAssetInfo(Class<T> assetType) {
return Optional.ofNullable(assetInfoMap.get(assetType));
}
public static Optional<AssetTypeInfo> getAssetInfo(String assetType) {
- Class<? extends Asset<?>> assetClass = assetTypeMap.get(assetType);
- return Optional.ofNullable(assetClass != null ? assetInfoMap.get(assetClass) : null);
+ return getAssetClass(assetType).map(assetClass -> assetInfoMap.get(assetClass));
}
// TODO: Implement ability to restrict which asset types are allowed to be added to a given parent type
@@ -571,7 +575,7 @@ public class ValueUtil {
return metaItemDescriptors.toArray(new MetaItemDescriptor<?>[0]);
}
- public static Optional<MetaItemDescriptor<?>[]> getMetaItemDescriptors(Class<? extends Asset<?>> assetType) {
+ public static <T extends Asset<?>> Optional<MetaItemDescriptor<?>[]> getMetaItemDescriptors(Class<T> assetType) {
return getAssetInfo(assetType).map(AssetTypeInfo::getMetaItemDescriptors);
}
@@ -589,7 +593,7 @@ public class ValueUtil {
return valueDescriptors.toArray(new ValueDescriptor<?>[0]);
}
- public static Optional<ValueDescriptor<?>[]> getValueDescriptors(Class<? extends Asset<?>> assetType) {
+ public static <T extends Asset<?>> Optional<ValueDescriptor<?>[]> getValueDescriptors(Class<T> assetType) {
return getAssetInfo(assetType).map(AssetTypeInfo::getValueDescriptors);
}
@@ -765,16 +769,18 @@ public class ValueUtil {
if (!Modifier.isAbstract(assetClass.getModifiers())) {
AssetTypeInfo assetInfo = buildAssetInfo(assetClass, copy);
- assetInfoMap.put(assetClass, assetInfo);
- assetTypeMap.put(assetInfo.getAssetDescriptor().getName(), assetClass);
-
- if (assetInfo.getAssetDescriptor() instanceof AgentDescriptor) {
- AgentDescriptor<?,?,?> agentDescriptor = (AgentDescriptor<?,?,?>)assetInfo.getAssetDescriptor();
- String agentLinkName = agentDescriptor.getAgentLinkClass().getSimpleName();
- if (agentLinkMap.containsKey(agentLinkName) && agentLinkMap.get(agentLinkName) != agentDescriptor.getAgentLinkClass()) {
- throw new IllegalStateException("AgentLink simple class name must be unique, duplicate found for: " + agentDescriptor.getAgentLinkClass());
+
+ if (assetInfo != null) {
+ assetInfoMap.put(assetClass, assetInfo);
+ assetTypeMap.put(assetInfo.getAssetDescriptor().getName(), assetClass);
+
+ if (assetInfo.getAssetDescriptor() instanceof AgentDescriptor<?, ?, ?> agentDescriptor) {
+ String agentLinkName = agentDescriptor.getAgentLinkClass().getSimpleName();
+ if (agentLinkMap.containsKey(agentLinkName) && agentLinkMap.get(agentLinkName) != agentDescriptor.getAgentLinkClass()) {
+ throw new IllegalStateException("AgentLink simple class name must be unique, duplicate found for: " + agentDescriptor.getAgentLinkClass());
+ }
+ agentLinkMap.put(agentLinkName, agentDescriptor.getAgentLinkClass());
}
- agentLinkMap.put(agentLinkName, agentDescriptor.getAgentLinkClass());
}
}
});
@@ -989,6 +995,10 @@ public class ValueUtil {
protected static AssetTypeInfo buildAssetInfo(Class<? extends Asset<?>> assetClass, Map<Class<? extends Asset<?>>, List<NameHolder>> classDescriptorMap) throws IllegalStateException {
+ if (assetClass == UnknownAsset.class) {
+ return null;
+ }
+
Class<?> currentClass = assetClass;
List<Class<?>> classTree = new ArrayList<>();
diff --git a/model/src/main/java/org/openremote/model/value/MetaItemType.java b/model/src/main/java/org/openremote/model/value/MetaItemType.java
index 9b6bdcdf7..15c881015 100644
--- a/model/src/main/java/org/openremote/model/value/MetaItemType.java
+++ b/model/src/main/java/org/openremote/model/value/MetaItemType.java
@@ -110,8 +110,6 @@ public final class MetaItemType {
* <p>
* The default expiration for asset events can be configured with environment variable
* <code>RULE_EVENT_EXPIRES</code>.
- * <p>
- * Also see {@link TemporaryFact#GUARANTEED_MIN_EXPIRATION_MILLIS}.
*/
@Pattern(regexp = Constants.ISO8601_DURATION_REGEXP)
public static final MetaItemDescriptor<String> RULE_EVENT_EXPIRES = new MetaItemDescriptor<>("ruleEventExpires", ValueType.TEXT);
diff --git a/profile/demo.yml b/profile/demo.yml
index ee74a2aaf..74fbd45c9 100644
--- a/profile/demo.yml
+++ b/profile/demo.yml
@@ -88,6 +88,7 @@ services:
# Set this variable `demo` to auto provision demo assets etc.
OR_SETUP_TYPE: ${OR_SETUP_TYPE:-demo}
OR_ADMIN_PASSWORD: ${OR_ADMIN_PASSWORD?OR_ADMIN_PASSWORD must be set}
+ OR_LOGGING_CONFIG_FILE: ${OR_LOGGING_CONFIG_FILE:-}
OR_SETUP_RUN_ON_RESTART:
OR_EMAIL_HOST:
OR_EMAIL_USER:
diff --git a/profile/deploy.yml b/profile/deploy.yml
index 120cc0954..3f7121b15 100644
--- a/profile/deploy.yml
+++ b/profile/deploy.yml
@@ -51,7 +51,7 @@ services:
# Customize proxy log, defaults to 'notice', requests are logged at 'info'.
# Available levels are: emerg, alert, crit, err, warning, notice, info, debug
- PROXY_LOGLEVEL: ${PROXY_LOGLEVEL:-notice}
+ PROXY_LOGLEVEL: ${PROXY_LOGLEVEL:-info}
# IANA TLD subdomain for which an SSL certificate should be requested (leave blank for private/externally
# generated certificates).
diff --git a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
index 7105d6c3e..a8a789ea1 100644
--- a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
@@ -231,6 +231,7 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {
returnedConsoleRegistration.providers.get("test").disabled = false
returnedConsoleRegistration = anonymousConsoleResource.register(null, returnedConsoleRegistration)
console = assetStorageService.find(consoleId, true)
+ testUser3Console1 = console
consoleTestProvider = console.getConsoleProviders().map{it.get("test")}.orElse(null)
then: "the returned console should contain the updated data and have the same id"
@@ -330,7 +331,27 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {
and: "the console should not have been linked to any users"
assert userAssets.isEmpty()
- and: "each created consoles should have been sent notifications to refresh their geofences"
+ when: "the location of each console is marked as RULE_STATE"
+ testUser3Console1.getAttribute(Asset.LOCATION).ifPresent {it -> it.addMeta(
+ new MetaItem<>(MetaItemType.RULE_STATE),
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_WRITE),
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_READ)
+ )}
+ testUser3Console1 = assetStorageService.merge(testUser3Console1)
+ testUser3Console2.getAttribute(Asset.LOCATION).ifPresent {it -> it.addMeta(
+ new MetaItem<>(MetaItemType.RULE_STATE),
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_WRITE),
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_READ)
+ )}
+ testUser3Console2 = assetStorageService.merge(testUser3Console2)
+ anonymousConsole1.getAttribute(Asset.LOCATION).ifPresent {it -> it.addMeta(
+ new MetaItem<>(MetaItemType.RULE_STATE),
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_WRITE),
+ new MetaItem<>(MetaItemType.ACCESS_RESTRICTED_READ)
+ )}
+ anonymousConsole1 = assetStorageService.merge(anonymousConsole1)
+
+ then: "each created consoles should have been sent notifications to refresh their geofences"
conditions.eventually {
assert notificationIds.size() == 3
assert messages.count { ((PushNotificationMessage) it).data.get("action").asText() == "GEOFENCE_REFRESH" } == 3
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
index 931541a36..9e4a6c0d9 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -49,7 +49,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {
List<Object> receivedValues = []
MQTT_IOClient client = null
MQTT_IOClient newClient = null
- def conditions = new PollingConditions(timeout: 10, initialDelay: 0.1, delay: 0.2)
+ def conditions = new PollingConditions(timeout: 15, initialDelay: 0.1, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
diff --git a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
index a0d1042e3..d90dd088c 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/TcpClientTest.groovy
@@ -26,6 +26,7 @@ import io.netty.util.CharsetUtil
import org.openremote.agent.protocol.io.AbstractNettyIOClient
import org.openremote.agent.protocol.tcp.TCPIOClient
import org.openremote.agent.protocol.tcp.TCPStringServer
+import org.openremote.container.timer.TimerService
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.notification.AbstractNotificationMessage
import org.openremote.model.notification.Notification
@@ -44,7 +45,7 @@ class TcpClientTest extends Specification implements ManagerContainerTrait {
def conditions = new PollingConditions(timeout: 20, delay: 0.2)
and: "the container is started"
- def container = startContainer(defaultConfig(), [])
+ def container = startContainer(defaultConfig(), [new TimerService()])
and: "a simple TCP echo server"
def echoServerPort = findEphemeralPort()
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
index 35b76ba7f..79e8208af 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientTest.groovy
@@ -27,6 +27,7 @@ import io.netty.util.internal.SocketUtils
import org.openremote.agent.protocol.io.AbstractNettyIOClient
import org.openremote.agent.protocol.udp.UDPIOClient
import org.openremote.agent.protocol.udp.UDPStringServer
+import org.openremote.container.timer.TimerService
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
@@ -46,7 +47,7 @@ class UdpClientTest extends Specification implements ManagerContainerTrait {
and: "the container is started"
def clientPort = findEphemeralPort()
- def container = startContainer(defaultConfig(), [])
+ def container = startContainer(defaultConfig(), [new TimerService()])
and: "a simple UDP echo server"
def echoServerPort = findEphemeralPort()
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
index 6b912a735..0e01c7a5e 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
@@ -157,14 +157,14 @@ class BasicRulesDeploymentTest extends Specification implements ManagerContainer
)
ruleset = rulesetStorageService.merge(ruleset)
- then: "the global rules engine should not run and the rule engine status should indicate the issue"
+ then: "the global rules engine should still run and the rule engine status should indicate the issue"
conditions.eventually {
assert rulesService.globalEngine.deployments.size() == 3
- assert !rulesService.globalEngine.running
- assert rulesService.globalEngine.isError()
+ assert rulesService.globalEngine.running
+ assert !rulesService.globalEngine.isError()
assert rulesService.globalEngine.error instanceof RuntimeException
- assert rulesService.globalEngine.deployments.values().any({ it.name == "Some global demo rules" && it.status == READY})
- assert rulesService.globalEngine.deployments.values().any({ it.name == "Some more global rules" && it.status == READY})
+ assert rulesService.globalEngine.deployments.values().any({ it.name == "Some global demo rules" && it.status == DEPLOYED})
+ assert rulesService.globalEngine.deployments.values().any({ it.name == "Some more global rules" && it.status == DEPLOYED})
assert rulesService.globalEngine.deployments.values().any({ it.name == "Some broken global rules" && it.status == COMPILATION_ERROR})
}
@@ -176,6 +176,7 @@ class BasicRulesDeploymentTest extends Specification implements ManagerContainer
assert rulesService.globalEngine.deployments.size() == 2
assert rulesService.globalEngine.running
assert !rulesService.globalEngine.isError()
+ assert rulesService.globalEngine.error == null
assert rulesService.globalEngine.deployments.values().any({ it.name == "Some global demo rules" && it.status == DEPLOYED })
assert rulesService.globalEngine.deployments.values().any({ it.name == "Some more global rules" && it.status == DEPLOYED })
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
index 764356ebf..f5419ed41 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
@@ -6,26 +6,26 @@ import org.openremote.manager.rules.RulesEngine
import org.openremote.manager.rules.RulesService
import org.openremote.manager.rules.RulesetStorageService
import org.openremote.manager.setup.SetupService
-import org.openremote.setup.integration.KeycloakTestSetup
-import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.model.asset.impl.RoomAsset
import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeEvent
import org.openremote.model.attribute.MetaItem
import org.openremote.model.rules.AssetRuleset
-import org.openremote.model.rules.Ruleset
-import org.openremote.model.rules.TemporaryFact
import org.openremote.model.rules.RealmRuleset
+import org.openremote.model.rules.Ruleset
import org.openremote.model.value.MetaItemType
import org.openremote.model.value.ValueType
+import org.openremote.setup.integration.KeycloakTestSetup
+import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
+import spock.lang.Ignore
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
import java.util.concurrent.TimeUnit
-import static org.openremote.setup.integration.ManagerTestSetup.*
import static org.openremote.model.rules.RulesetStatus.*
+import static org.openremote.setup.integration.ManagerTestSetup.*
import static org.openremote.test.rules.BasicRulesImport.assertRulesFired
class BasicRulesProcessingTest extends Specification implements ManagerContainerTrait {
@@ -35,8 +35,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
and: "the container is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
@@ -107,8 +105,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
assertRulesFired(smartHomeEngine, ["Living Room All", "Kitchen All", "Kitchen Number Attributes", "Asset Type Room", "Boolean Attributes", "String attributes", "Number value types"])
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
}
def "Handle attribute event with no meta, asset create, update, delete"() {
@@ -116,8 +112,6 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
def conditions = new PollingConditions(timeout: 15, delay: 0.2)
and: "the container is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
@@ -331,17 +325,15 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
assertRulesFired(rulesImport.apartment3Engine, 0)
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
+ // TODO: Decide if continue on error should be supported
+ @Ignore
def "Stop processing when engine in error state"() {
given: "expected conditions"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
and: "the container is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
@@ -403,7 +395,5 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
assert rulesImport.apartment3Engine.lastFireTimestamp == apartment3LastFireTimestamp
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
}
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesTimedExecutionTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesTimedExecutionTest.groovy
index 51fb09279..d112c00fd 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesTimedExecutionTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesTimedExecutionTest.groovy
@@ -30,8 +30,6 @@ class BasicRulesTimedExecutionTest extends Specification implements ManagerConta
def conditions = new PollingConditions(timeout: 30, delay: 0.2)
and: "the container is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def rulesService = container.getService(RulesService.class)
def rulesetStorageService = container.getService(RulesetStorageService.class)
@@ -65,15 +63,11 @@ class BasicRulesTimedExecutionTest extends Specification implements ManagerConta
assert globalEngineFiredRules.containsAll(expectedFiredRules)
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Check firing of timer rules with pseudo clock"() {
given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def conditions = new PollingConditions(timeout: 30, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def rulesService = container.getService(RulesService.class)
@@ -119,7 +113,5 @@ class BasicRulesTimedExecutionTest extends Specification implements ManagerConta
assert globalEngineFiredRules.containsAll(expectedFiredRules)
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesetResourceTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesetResourceTest.groovy
index d56979a34..e7fd8d198 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesetResourceTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesetResourceTest.groovy
@@ -24,8 +24,6 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
def "Access rules as superuser"() {
given: "the server container is started"
def container = startContainer(defaultConfig(), defaultServices())
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def rulesetStorageService = container.getService(RulesetStorageService.class)
def rulesService = container.getService(RulesService.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
@@ -366,15 +364,11 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
assert deployment.ruleset.version == assetRuleset.version
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Access rules as testuser1"() {
given: "the server container is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def rulesetStorageService = container.getService(RulesetStorageService.class)
def rulesService = container.getService(RulesService.class)
@@ -645,16 +639,12 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
ex = thrown()
ex.response.status == 403
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Access rules as testuser2"() {
given: "the server container is started"
def container = startContainer(defaultConfig(), defaultServices())
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def rulesetStorageService = container.getService(RulesetStorageService.class)
def rulesService = container.getService(RulesService.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
@@ -793,16 +783,12 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
ex = thrown()
ex.response.status == 403
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Access rules as testuser3"() {
given: "the server container is started"
def container = startContainer(defaultConfig(), defaultServices())
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def rulesetStorageService = container.getService(RulesetStorageService.class)
def rulesService = container.getService(RulesService.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
@@ -1028,7 +1014,5 @@ class BasicRulesetResourceTest extends Specification implements ManagerContainer
ex = thrown()
ex.response.status == 403
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/RulesExecutionFailureTest.groovy b/test/src/test/groovy/org/openremote/test/rules/RulesExecutionFailureTest.groovy
deleted file mode 100644
index b9643fd5d..000000000
--- a/test/src/test/groovy/org/openremote/test/rules/RulesExecutionFailureTest.groovy
+++ /dev/null
@@ -1,163 +0,0 @@
-package org.openremote.test.rules
-
-
-import org.openremote.manager.rules.*
-import org.openremote.manager.setup.SetupService
-import org.openremote.setup.integration.ManagerTestSetup
-import org.openremote.model.rules.AssetRuleset
-import org.openremote.model.rules.Ruleset
-import org.openremote.model.rules.RulesetStatus
-import org.openremote.model.rules.TemporaryFact
-import org.openremote.test.ManagerContainerTrait
-import spock.lang.Specification
-import spock.util.concurrent.PollingConditions
-
-class RulesExecutionFailureTest extends Specification implements ManagerContainerTrait {
-
- def "Rule condition invalid return"() {
-
- given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
- def conditions = new PollingConditions(timeout: 20, delay: 0.2)
- def container = startContainer(defaultConfig(), defaultServices())
- def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
- def rulesService = container.getService(RulesService.class)
- def rulesetStorageService = container.getService(RulesetStorageService.class)
- RulesEngine apartment2Engine
-
- and: "some rules"
- Ruleset ruleset = new AssetRuleset(
- managerTestSetup.apartment2Id,
- "Failure Ruleset",
- Ruleset.Lang.GROOVY,
- getClass().getResource("/org/openremote/test/failure/RulesFailureConditionInvalidReturn.groovy").text)
- ruleset = rulesetStorageService.merge(ruleset)
-
- expect: "the rule engine should have an error (first firing after initial asset state insert)"
- conditions.eventually {
- apartment2Engine = rulesService.assetEngines.get(managerTestSetup.apartment2Id)
- assert apartment2Engine != null
- assert apartment2Engine.isError()
- assert apartment2Engine.getError() instanceof RuntimeException
- assert apartment2Engine.getError().message.startsWith("Ruleset deployments have errors, failed compilation: 0, failed execution: 1")
- assert apartment2Engine.deployments[ruleset.id].status == RulesetStatus.EXECUTION_ERROR
- assert apartment2Engine.deployments[ruleset.id].error instanceof IllegalArgumentException
- assert apartment2Engine.deployments[ruleset.id].error.message == "Error evaluating condition of rule 'The when condition is illegal, it's returning an Optional instead of a boolean': result is not boolean but Optional.empty"
- }
-
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
-
- def "Rule condition throws exception"() {
-
- given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
- def conditions = new PollingConditions(timeout: 20, delay: 0.2)
- def container = startContainer(defaultConfig(), defaultServices())
- def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
- def rulesService = container.getService(RulesService.class)
- def rulesetStorageService = container.getService(RulesetStorageService.class)
- RulesEngine apartment2Engine
-
- and: "some rules"
- Ruleset ruleset = new AssetRuleset(
- managerTestSetup.apartment2Id,
- "Failure Ruleset",
- Ruleset.Lang.GROOVY,
- getClass().getResource("/org/openremote/test/failure/RulesFailureConditionThrowsException.groovy").text)
- ruleset = rulesetStorageService.merge(ruleset)
-
- expect: "the rule engine should have an error (first firing after initial asset state insert)"
- conditions.eventually {
- apartment2Engine = rulesService.assetEngines.get(managerTestSetup.apartment2Id)
- assert apartment2Engine != null
- assert apartment2Engine.deployments[ruleset.id].status == RulesetStatus.EXECUTION_ERROR
- assert apartment2Engine.deployments[ruleset.id].error instanceof RuntimeException
- assert apartment2Engine.deployments[ruleset.id].error.message == "Error evaluating condition of rule 'Condition always throws exception': Oops"
- assert apartment2Engine.isError()
- assert apartment2Engine.getError() instanceof RuntimeException
- assert apartment2Engine.getError().message.startsWith("Ruleset deployments have errors, failed compilation: 0, failed execution: 1")
- }
-
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
-
- def "Rule action throws exception"() {
-
- given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
- def conditions = new PollingConditions(timeout: 20, delay: 0.2)
- def container = startContainer(defaultConfig(), defaultServices())
- def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
- def rulesService = container.getService(RulesService.class)
- def rulesetStorageService = container.getService(RulesetStorageService.class)
- RulesEngine apartment2Engine
-
- and: "some rules"
- Ruleset ruleset = new AssetRuleset(
- managerTestSetup.apartment2Id,
- "Failure Ruleset",
- Ruleset.Lang.GROOVY,
- getClass().getResource("/org/openremote/test/failure/RulesFailureActionThrowsException.groovy").text)
- ruleset = rulesetStorageService.merge(ruleset)
-
- expect: "the rule engine should have an error (first firing after initial asset state insert)"
- conditions.eventually {
- apartment2Engine = rulesService.assetEngines.get(managerTestSetup.apartment2Id)
- assert apartment2Engine != null
- assert apartment2Engine.deployments[ruleset.id].status == RulesetStatus.EXECUTION_ERROR
- assert apartment2Engine.deployments[ruleset.id].error instanceof RuntimeException
- assert apartment2Engine.deployments[ruleset.id].error.message == "Error executing action of rule 'Action always throws exception': Oops"
- assert apartment2Engine.isError()
- assert apartment2Engine.getError() instanceof RuntimeException
- assert apartment2Engine.getError().message.startsWith("Ruleset deployments have errors, failed compilation: 0, failed execution: 1")
- }
-
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
-
-// Switched rule engine to DefaultEngine rather than InferenceEngine so loops cannot occur now
-// def "Rule condition loops"() {
-//
-// given: "the container environment is started"
-// def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
-// TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
-// def conditions = new PollingConditions(timeout: 20, delay: 0.2)
-// def container = startContainer(defaultConfig(), defaultServices())
-// def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
-// def rulesService = container.getService(RulesService.class)
-// def rulesetStorageService = container.getService(RulesetStorageService.class)
-// RulesEngine apartment2Engine
-//
-// and: "some rules"
-// Ruleset ruleset = new AssetRuleset(
-// managerTestSetup.apartment2Id,
-// "Failure Ruleset",
-// Ruleset.Lang.GROOVY,
-// getClass().getResource("/org/openremote/test/failure/RulesFailureLoop.groovy").text)
-// ruleset.getMeta().addOrReplace(new MetaItem<>(Ruleset.CONTINUE_ON_ERROR, true))
-// ruleset = rulesetStorageService.merge(ruleset)
-//
-// expect: "the rule engine should have an error (first firing after initial asset state insert)"
-// conditions.eventually {
-// apartment2Engine = rulesService.assetEngines.get(managerTestSetup.apartment2Id)
-// assert apartment2Engine != null
-// assert apartment2Engine.deployments[ruleset.id].status == RulesetStatus.LOOP_ERROR
-// assert apartment2Engine.deployments[ruleset.id].error instanceof RulesLoopException
-// assert apartment2Engine.deployments[ruleset.id].error.message == "Possible rules loop detected, exceeded max trigger count of " + RulesFacts.MAX_RULES_TRIGGERED_PER_EXECUTION + " for rule: Condition loops"
-// assert apartment2Engine.isError()
-// assert apartment2Engine.getError() instanceof RuntimeException
-// assert apartment2Engine.getError().message.startsWith("Ruleset deployments have errors, failed compilation: 0, failed execution: 1")
-// assert apartment2Engine.facts.triggerCount == 0 // Ensure trigger count is reset after execution
-// }
-//
-// cleanup: "the static rules time variable is reset"
-// TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
-// }
-}
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy
index bf74e914c..4be66fa9d 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy
@@ -26,8 +26,6 @@ class FlowRulesTest extends Specification implements ManagerContainerTrait {
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
and: "the container is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def rulesService = container.getService(RulesService.class)
@@ -76,7 +74,5 @@ class FlowRulesTest extends Specification implements ManagerContainerTrait {
assert bedroomTargetTemp.intValue() == (startTemperature.intValue() + 10) : ("it was actually " + bedroomTargetTemp.intValue())//convert to int considering floating point inaccuracy
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
}
}
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.groovy
index 586358f59..16e7bfbee 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
@@ -10,21 +10,15 @@ import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.notification.EmailNotificationHandler
import org.openremote.manager.notification.NotificationService
import org.openremote.manager.notification.PushNotificationHandler
-import org.openremote.manager.rules.JsonRulesBuilder
-import org.openremote.manager.rules.RulesEngine
-import org.openremote.manager.rules.RulesService
-import org.openremote.manager.rules.RulesetDeployment
-import org.openremote.manager.rules.RulesetStorageService
+import org.openremote.manager.rules.*
import org.openremote.manager.rules.geofence.ORConsoleGeofenceAssetAdapter
import org.openremote.manager.setup.SetupService
+import org.openremote.model.asset.Asset
import org.openremote.model.asset.UserAssetLink
import org.openremote.model.asset.impl.ThingAsset
import org.openremote.model.attribute.Attribute
-import org.openremote.model.value.ValueType
-import org.openremote.setup.integration.KeycloakTestSetup
-import org.openremote.setup.integration.ManagerTestSetup
-import org.openremote.model.asset.Asset
import org.openremote.model.attribute.AttributeEvent
+import org.openremote.model.attribute.MetaItem
import org.openremote.model.calendar.CalendarEvent
import org.openremote.model.console.ConsoleProvider
import org.openremote.model.console.ConsoleRegistration
@@ -34,12 +28,15 @@ import org.openremote.model.notification.AbstractNotificationMessage
import org.openremote.model.notification.Notification
import org.openremote.model.notification.NotificationSendResult
import org.openremote.model.notification.PushNotificationMessage
+import org.openremote.model.rules.RealmRuleset
import org.openremote.model.rules.Ruleset
import org.openremote.model.rules.RulesetStatus
-import org.openremote.model.rules.TemporaryFact
-import org.openremote.model.rules.RealmRuleset
import org.openremote.model.rules.json.JsonRulesetDefinition
import org.openremote.model.util.ValueUtil
+import org.openremote.model.value.MetaItemType
+import org.openremote.model.value.ValueType
+import org.openremote.setup.integration.KeycloakTestSetup
+import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
import org.simplejavamail.email.Email
import spock.lang.Specification
@@ -52,9 +49,9 @@ import java.util.concurrent.TimeUnit
import static java.util.concurrent.TimeUnit.HOURS
import static java.util.concurrent.TimeUnit.MILLISECONDS
-import static org.openremote.setup.integration.ManagerTestSetup.DEMO_RULE_STATES_SMART_BUILDING
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
import static org.openremote.model.util.ValueUtil.parse
+import static org.openremote.setup.integration.ManagerTestSetup.DEMO_RULE_STATES_SMART_BUILDING
class JsonRulesTest extends Specification implements ManagerContainerTrait {
@@ -66,13 +63,9 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
List<Notification.Target> emailTargets = []
given: "the geofence notifier debounce is set to a small value for testing"
- def originalDebounceMillis = ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS
+ Integer originalDebounceMillis = ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS
ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS = 100
- and: "the rule firing delay time is set to a small value for testing"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
-
and: "the container environment is started with the mock handler"
def conditions = new PollingConditions(timeout: 15, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
@@ -189,9 +182,13 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
["manager"] as String[])
consoleRegistration = authenticatedConsoleResource.register(null, consoleRegistration)
- then: "the console should have been registered and a geofence refresh notification should have been sent"
+ and: "the console location is marked as RULE_STATE"
+ def asset = assetStorageService.find(consoleRegistration.id)
+ asset.getAttribute(Asset.LOCATION).ifPresent { it.addMeta(new MetaItem<>(MetaItemType.RULE_STATE))}
+ asset = assetStorageService.merge(asset)
+
+ then: "a geofence refresh notification should have been sent to the console"
conditions.eventually {
- assert consoleRegistration.id != null
assert pushMessages.size() == 1
}
@@ -486,10 +483,15 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
}
cleanup: "static variables are reset and the mock is removed"
- RulesEngine.PAUSE_SCHEDULER = originalPause
- RulesEngine.UNPAUSE_SCHEDULER = originalUnpause
- ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS = originalDebounceMillis
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
+ if (originalPause != null) {
+ RulesEngine.PAUSE_SCHEDULER = originalPause
+ }
+ if (originalUnpause != null) {
+ RulesEngine.UNPAUSE_SCHEDULER = originalUnpause
+ }
+ if (originalDebounceMillis != null) {
+ ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS = originalDebounceMillis
+ }
if (notificationService != null) {
notificationService.notificationHandlerMap.put(emailNotificationHandler.getTypeName(), emailNotificationHandler)
notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), pushNotificationHandler)
@@ -498,11 +500,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
def "Trigger actions based on the position of the sun"() {
- given: "the rule firing delay time is set to a small value for testing"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
-
- and: "the container environment is started"
+ given: "the container environment is started"
def conditions = new PollingConditions(timeout: 15, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def pushNotificationHandler = container.getService(PushNotificationHandler.class)
@@ -582,11 +580,24 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
advancePseudoClock(Duration.between(timerService.getNow(), sunTimes.getSet()).getSeconds()+1, TimeUnit.SECONDS, container)
then: "the rule engine should have fired again and the rule should have triggered"
+ def lastUpdateTime = 0
conditions.eventually {
assert realmBuildingEngine.lastFireTimestamp > lastFireTimestamp
assert realmBuildingEngine.lastFireTimestamp == timerService.getNow().toEpochMilli()
thingAsset = assetStorageService.find(thingId) as ThingAsset
assert thingAsset.getAttribute("sunset").get().getValue().orElse(0) == 100
+ lastFireTimestamp = realmBuildingEngine.lastFireTimestamp
+ lastUpdateTime = thingAsset.getAttribute("sunset").get().getTimestamp().orElse(0)
+ }
+
+ when: "time advances slightly"
+ advancePseudoClock(1, TimeUnit.SECONDS, container)
+
+ then: "the rule engine should have fired again and the rule should not have triggered"
+ conditions.eventually {
+ assert realmBuildingEngine.lastFireTimestamp >= lastFireTimestamp + 1000
+ thingAsset = assetStorageService.find(thingId) as ThingAsset
+ assert thingAsset.getAttribute("sunset").get().getTimestamp().orElse(-1) == lastUpdateTime
}
when: "time advances past the sunset"
@@ -716,7 +727,6 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
}
cleanup: "static variables are reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
if (notificationService != null) {
notificationService.notificationHandlerMap.put(emailNotificationHandler.getTypeName(), emailNotificationHandler)
notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), pushNotificationHandler)
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy
index fc55fd396..87f1b6080 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy
@@ -25,8 +25,6 @@ class ResidenceAllLightsOffTest extends Specification implements ManagerContaine
def "Turn all lights off"() {
given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
@@ -96,7 +94,5 @@ class ResidenceAllLightsOffTest extends Specification implements ManagerContaine
assert livingroomAsset.getAttribute("lightSwitch").get().value.get()
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
index 330f68dcd..32768430c 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
@@ -30,8 +30,6 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
def "Auto ventilation with CO2 detection"() {
given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def conditions = new PollingConditions(timeout: 20, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
@@ -225,7 +223,5 @@ class ResidenceAutoVentilationTest extends Specification implements ManagerConta
assert apartment.getAttribute("ventilationLevel").flatMap{it.value}.orElse(0d) == 64d
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
index a484bad0b..c356e2774 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
@@ -59,8 +59,6 @@ class ResidenceNotifyAlarmTriggerTest extends Specification implements ManagerCo
def messages = []
given: "the container environment is started with the mock handler"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def conditions = new PollingConditions(timeout: 20, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
@@ -339,6 +337,5 @@ class ResidenceNotifyAlarmTriggerTest extends Specification implements ManagerCo
cleanup: "the mock is removed"
notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), pushNotificationHandler)
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
}
}
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
index 512e12f01..a7f6c36e1 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
@@ -29,8 +29,6 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
def "Presence detection with motion sensor"() {
given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def conditions = new PollingConditions(timeout: 30, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
@@ -180,15 +178,11 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
assert hallway.getAttribute("lastPresenceDetected").flatMap{it.value}.orElse(0d) == expectedLastPresenceDetected
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Presence detection with motion sensor and confirmation with CO2 level"() {
given: "the container environment is started"
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def conditions = new PollingConditions(timeout: 20, delay: 0.2)
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
@@ -329,9 +323,7 @@ class ResidencePresenceDetectionTest extends Specification implements ManagerCon
assert roomAsset.getAttribute("lastPresenceDetected").flatMap{it.value}.orElse(0d) == expectedLastPresenceDetected
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
/* TODO Migrate to JS, didn't compile even with Drools
def "Presence prediction rules compilation"() {
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy
index 1709dabfc..9fb30c147 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy
@@ -28,8 +28,6 @@ class ResidenceSmartSwitchTest extends Specification implements ManagerContainer
given: "the container environment is started"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def rulesService = container.getService(RulesService.class)
@@ -150,16 +148,12 @@ class ResidenceSmartSwitchTest extends Specification implements ManagerContainer
assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Set mode ON_AT and begin/end cycle time in past"() {
given: "the container environment is started"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def rulesService = container.getService(RulesService.class)
@@ -248,16 +242,12 @@ class ResidenceSmartSwitchTest extends Specification implements ManagerContainer
assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Set mode READY_AT and begin/end cycle time in future"() {
given: "the container environment is started"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def rulesService = container.getService(RulesService.class)
@@ -370,16 +360,12 @@ class ResidenceSmartSwitchTest extends Specification implements ManagerContainer
assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Set mode READY_AT and begin/end cycle time in past"() {
given: "the container environment is started"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def rulesService = container.getService(RulesService.class)
@@ -469,16 +455,12 @@ class ResidenceSmartSwitchTest extends Specification implements ManagerContainer
assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
def "Set mode READY_AT and insufficient begin/end cycle time"() {
given: "the container environment is started"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
- def expirationMillis = TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = 500
def container = startContainer(defaultConfig(), defaultServices())
def managerTestSetup = container.getService(SetupService.class).getTaskOfType(ManagerTestSetup.class)
def rulesService = container.getService(RulesService.class)
@@ -568,8 +550,6 @@ class ResidenceSmartSwitchTest extends Specification implements ManagerContainer
assert !kitchen.getAttribute("smartSwitchEnabledC").get().getValue().isPresent()
}
- cleanup: "the static rules time variable is reset"
- TemporaryFact.GUARANTEED_MIN_EXPIRATION_MILLIS = expirationMillis
- }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy b/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy
index cc4ae1160..316686bb1 100644
--- a/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/users/UserResourceTest.groovy
@@ -19,8 +19,9 @@
*/
package org.openremote.test.users
-import org.openremote.container.security.keycloak.KeycloakIdentityProvider
+
import org.openremote.manager.setup.SetupService
+import org.openremote.model.Constants
import org.openremote.model.query.UserQuery
import org.openremote.model.query.filter.RealmPredicate
import org.openremote.setup.integration.KeycloakTestSetup
@@ -81,7 +82,7 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {
then: "all users should be returned including system users"
users.size() == 3
- users.count {it.isSystemAccount() && it.username == KeycloakIdentityProvider.MANAGER_CLIENT_ID} == 1
+ users.count {it.isSystemAccount() && it.username == Constants.MANAGER_CLIENT_ID} == 1
users.count {!it.isServiceAccount()} == 3
when: "a request is made for all users by a regular user"
@@ -89,7 +90,7 @@ class UserResourceTest extends Specification implements ManagerContainerTrait {
then: "only non system users of the users realm should be returned"
users.size() == 5
- users.count {it.isSystemAccount() && it.username == KeycloakIdentityProvider.MANAGER_CLIENT_ID} == 0
+ users.count {it.isSystemAccount() && it.username == Constants.MANAGER_CLIENT_ID} == 0
users.find {it.id == keycloakTestSetup.testuser2Id} != null
users.find {it.id == keycloakTestSetup.testuser3Id} != null
users.find {it.id == keycloakTestSetup.buildingUserId} != null
diff --git a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy
index 9bd6b7219..d4dbd8a3f 100644
--- a/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy
+++ b/test/src/testFixtures/groovy/org/openremote/test/ManagerContainerTrait.groovy
@@ -18,6 +18,8 @@ import static org.openremote.container.timer.TimerService.Clock.REAL
import static org.openremote.container.timer.TimerService.TIMER_CLOCK_TYPE
import static org.openremote.container.web.WebService.OR_WEBSERVER_LISTEN_PORT
import static org.openremote.manager.mqtt.MQTTBrokerService.MQTT_SERVER_LISTEN_HOST
+import static org.openremote.manager.rules.RulesService.OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS
+import static org.openremote.manager.rules.RulesService.OR_RULES_QUICK_FIRE_MILLIS
trait ManagerContainerTrait extends ContainerTrait {
@@ -29,6 +31,8 @@ trait ManagerContainerTrait extends ContainerTrait {
(OR_WEBSERVER_LISTEN_PORT): Integer.toString(serverPort),
(MQTT_SERVER_LISTEN_HOST) : "127.0.0.1", // Works best for cross platform test running,
(MQTTBrokerService.MQTT_FORCE_USER_DISCONNECT_DEBOUNCE_MILLIS): "10",
+ (OR_RULES_QUICK_FIRE_MILLIS): "500",
+ (OR_RULES_MIN_TEMP_FACT_EXPIRATION_MILLIS): "500",
(TIMER_CLOCK_TYPE) : PSEUDO.name()
]
}
diff --git a/ui/app/manager/src/pages/page-rules.ts b/ui/app/manager/src/pages/page-rules.ts
index bba7bdd79..439a47f4f 100644
--- a/ui/app/manager/src/pages/page-rules.ts
+++ b/ui/app/manager/src/pages/page-rules.ts
@@ -50,8 +50,7 @@ export const PAGE_RULES_CONFIG_DEFAULT: PageRulesConfig = {
excludeAssets: [
WellknownAssets.TRADFRILIGHTASSET,
WellknownAssets.TRADFRIPLUGASSET,
- WellknownAssets.ARTNETLIGHTASSET,
- WellknownAssets.UNKNOWNASSET
+ WellknownAssets.ARTNETLIGHTASSET
]
}
},
diff --git a/ui/component/model/src/util.ts b/ui/component/model/src/util.ts
index 62686935e..e00b59409 100644
--- a/ui/component/model/src/util.ts
+++ b/ui/component/model/src/util.ts
@@ -71,7 +71,7 @@ export class AssetModelUtil {
return;
}
- const assetTypeInfo = this.getAssetTypeInfo(assetTypeOrDescriptor || WellknownAssets.UNKNOWNASSET);
+ const assetTypeInfo = this.getAssetTypeInfo(assetTypeOrDescriptor || WellknownAssets.THINGASSET);
if (!assetTypeInfo || !assetTypeInfo.attributeDescriptors) {
return;
@@ -178,7 +178,7 @@ export class AssetModelUtil {
if (attributeNameOrDescriptor && typeof attributeNameOrDescriptor !== "string") {
attributeDescriptor = attributeNameOrDescriptor as AttributeDescriptor;
} else {
- const assetTypeInfo = this.getAssetTypeInfo(assetType || WellknownAssets.UNKNOWNASSET);
+ const assetTypeInfo = this.getAssetTypeInfo(assetType || WellknownAssets.THINGASSET);
if (!assetTypeInfo) {
return [undefined, undefined];
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index 96cf10c2e..f7bad3da8 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -20,8 +20,7 @@ import {
LogicGroup,
LogicGroupOperator,
SharedEvent,
- StringPredicate,
- WellknownAssets
+ StringPredicate
} from "@openremote/model";
import "@openremote/or-translate";
import {style} from "./style";
@@ -254,10 +253,6 @@ export const getAssetTypes = async () => {
}
}
-export function getDefaultAllowedAddAssetTypes(): AssetDescriptor[] {
- return AssetModelUtil.getAssetDescriptors().filter(ad => ad.name !== WellknownAssets.UNKNOWNASSET);
-}
-
@customElement("or-asset-tree")
export class OrAssetTree extends subscribe(manager)(LitElement) {
@@ -1565,7 +1560,7 @@ export class OrAssetTree extends subscribe(manager)(LitElement) {
}
}
- return getDefaultAllowedAddAssetTypes()
+ return AssetModelUtil.getAssetDescriptors()
.filter((descriptor) => (!includedAssetTypes || includedAssetTypes.some((inc) => Util.stringMatch(inc, descriptor.name!)))
&& (!excludedAssetTypes || !excludedAssetTypes.some((exc) => Util.stringMatch(exc, descriptor.name!))));
}
diff --git a/ui/component/or-icon/src/index.ts b/ui/component/or-icon/src/index.ts
index ecbc1b817..f1a572786 100644
--- a/ui/component/or-icon/src/index.ts
+++ b/ui/component/or-icon/src/index.ts
@@ -124,7 +124,7 @@ export function getAssetDescriptorIconTemplate(descriptor: AssetDescriptor | und
const color = overrideColor ? overrideColor : AssetModelUtil.getAssetDescriptorColour(descriptor, fallbackColor);
let icon = overrideIcon ? overrideIcon : AssetModelUtil.getAssetDescriptorIcon(descriptor, fallbackIcon);
if (!icon) {
- icon = AssetModelUtil.getAssetDescriptorIcon(WellknownAssets.UNKNOWNASSET);
+ icon = AssetModelUtil.getAssetDescriptorIcon(WellknownAssets.THINGASSET);
}
return html`<or-icon style="--or-icon-fill: ${color ? "#" + color : "unset"}" icon="${icon}"></or-icon>`;
}
diff --git a/ui/component/or-rules/src/index.ts b/ui/component/or-rules/src/index.ts
index 302d9b327..2132d7ca9 100644
--- a/ui/component/or-rules/src/index.ts
+++ b/ui/component/or-rules/src/index.ts
@@ -374,7 +374,7 @@ export function getAssetIdsFromQuery(query?: AssetQuery) {
export const getAssetTypes: () => Promise<string[]> = async () => {
// RT: Change to just get all asset types for now as if an instance of a particular asset doesn't exist you
// won't be able to create a rule for it (e.g. if no ConsoleAsset in a realm then cannot create a console rule)
- return AssetModelUtil.getAssetTypeInfos().filter((ati) => ati.assetDescriptor!.name !== WellknownAssets.UNKNOWNASSET).map(ati => ati.assetDescriptor!.name!);
+ return AssetModelUtil.getAssetTypeInfos().map(ati => ati.assetDescriptor!.name!);
// const response = await manager.rest.api.AssetResource.queryAssets({
// select: {
// attributes: []