Skip to content

armgit / core

Public Imported from GitHub
0

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

Gateway functionality implemented along with various minor bug fixes and changes

Rich Turner352a3e5703f5
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java
index 00d9fb95f..0bac36d37 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java
@@ -151,5 +151,5 @@ public interface ProtocolAssetService extends ContainerService {
/**
* Apply the specified set of {@link ValueFilter}s to the specified {@link Value}
*/
- Value applyValueFilters(Value value, ValueFilter... filters);
+ Value applyValueFilters(Value value, ValueFilter<?>... filters);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
index a760c6ab6..4d4ebf5b1 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
@@ -402,7 +402,7 @@ public class HttpClientProtocol extends AbstractProtocol {
null);
/**
- * Ping frequency in seconds (integer default: {@value #DEFAULT_PING_MILLIS})
+ * Ping frequency in milliseconds (integer default: {@value #DEFAULT_PING_MILLIS})
*/
public static final MetaItemDescriptor META_PROTOCOL_PING_MILLIS = metaItemInteger(
PROTOCOL_NAME + ":pingMillis",
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthClientCredentialsGrant.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthClientCredentialsGrant.java
index 124e757a7..f44dbb506 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthClientCredentialsGrant.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthClientCredentialsGrant.java
@@ -42,4 +42,8 @@ public class OAuthClientCredentialsGrant extends OAuthGrant {
String scope) {
super(tokenEndpointUri, grantType, clientId, clientSecret, scope);
}
+
+ public OAuthClientCredentialsGrant setBasicAuthHeader(boolean basicAuthHeader) {
+ return (OAuthClientCredentialsGrant)super.setBasicAuthHeader(basicAuthHeader);
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java
index f64085266..a7d0cd75d 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java
@@ -19,13 +19,11 @@
*/
package org.openremote.agent.protocol.http;
+import org.jboss.resteasy.util.BasicAuthHelper;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
-import javax.ws.rs.client.ClientRequestContext;
-import javax.ws.rs.client.ClientRequestFilter;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.client.*;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
@@ -43,7 +41,7 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
public class OAuthFilter implements ClientRequestFilter {
private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, OAuthFilter.class);
- public static final String AUTH_TYPE = "Bearer";
+ public static final String BEARER_AUTH = "Bearer";
protected OAuthServerResponse authServerResponse;
protected WebTarget authTarget;
protected OAuthGrant oAuthGrant;
@@ -59,7 +57,7 @@ public class OAuthFilter implements ClientRequestFilter {
String accessToken = getAccessToken();
if (!TextUtil.isNullOrEmpty(accessToken)) {
- return AUTH_TYPE + " " + accessToken;
+ return BEARER_AUTH + " " + accessToken;
}
return null;
@@ -121,9 +119,13 @@ public class OAuthFilter implements ClientRequestFilter {
}
protected Response requestToken() {
- return authTarget
- .request(MediaType.APPLICATION_JSON_TYPE)
- .post(Entity.entity(new Form(oAuthGrant.valueMap), MediaType.APPLICATION_FORM_URLENCODED_TYPE));
+ Invocation.Builder builder = authTarget
+ .request(MediaType.APPLICATION_JSON_TYPE);
+
+ if (oAuthGrant.isBasicAuthHeader()) {
+ builder.header(HttpHeaders.AUTHORIZATION, BasicAuthHelper.createHeader(oAuthGrant.getClientId(), oAuthGrant.getClientSecret()));
+ }
+ return builder.post(Entity.entity(new Form(oAuthGrant.valueMap), MediaType.APPLICATION_FORM_URLENCODED_TYPE));
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java
index 137825229..c9eca0764 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthGrant.java
@@ -48,6 +48,8 @@ public abstract class OAuthGrant {
@JsonIgnore
protected MultivaluedMap<String, String> valueMap = new MultivaluedHashMap<>(6);
protected String tokenEndpointUri;
+ @JsonProperty
+ protected boolean basicAuthHeader;
protected OAuthGrant(String tokenEndpointUri, String grantType, String clientId, String clientSecret, String scope) {
requireNonNullAndNonEmpty(tokenEndpointUri);
@@ -93,6 +95,15 @@ public abstract class OAuthGrant {
return valueMap.getFirst(VALUE_KEY_SCOPE);
}
+ public boolean isBasicAuthHeader() {
+ return basicAuthHeader;
+ }
+
+ public OAuthGrant setBasicAuthHeader(boolean basicAuthHeader) {
+ this.basicAuthHeader = basicAuthHeader;
+ return this;
+ }
+
public ObjectValue toObjectValue() {
try {
return Values.<ObjectValue>parse(Container.JSON.writeValueAsString(this)).orElse(null);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthPasswordGrant.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthPasswordGrant.java
index 0193cb1eb..f0c9a3752 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthPasswordGrant.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthPasswordGrant.java
@@ -62,4 +62,8 @@ public class OAuthPasswordGrant extends OAuthClientCredentialsGrant {
public String getPassword() {
return valueMap.getFirst(VALUE_KEY_PASSWORD);
}
+
+ public OAuthPasswordGrant setBasicAuthHeader(boolean basicAuthHeader) {
+ return (OAuthPasswordGrant)super.setBasicAuthHeader(basicAuthHeader);
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthRefreshTokenGrant.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthRefreshTokenGrant.java
index c2b0cf5d6..4f13425b7 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthRefreshTokenGrant.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthRefreshTokenGrant.java
@@ -52,4 +52,8 @@ public class OAuthRefreshTokenGrant extends OAuthClientCredentialsGrant {
public String getRefreshToken() {
return valueMap.getFirst(VALUE_KEY_REFRESH_TOKEN);
}
+
+ public OAuthRefreshTokenGrant setBasicAuthHeader(boolean basicAuthHeader) {
+ return (OAuthRefreshTokenGrant)super.setBasicAuthHeader(basicAuthHeader);
+ }
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java
index 9aa32a04b..4ab23e72d 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java
@@ -175,8 +175,8 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}
@Override
- public void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encodersProvider) throws UnsupportedOperationException {
- this.encoderDecoderProvider = encodersProvider;
+ public void setEncoderDecoderProvider(Supplier<ChannelHandler[]> encoderDecoderProvider) throws UnsupportedOperationException {
+ this.encoderDecoderProvider = encoderDecoderProvider;
}
protected abstract Class<? extends Channel> getChannelClass();
@@ -190,19 +190,21 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}
@Override
- public synchronized void connect() {
- if (permanentError) {
- LOG.fine("Unable to connect as permanent error has been set");
- return;
- }
+ public void connect() {
+ synchronized (this) {
+ if (permanentError) {
+ LOG.fine("Unable to connect as permanent error has been set");
+ return;
+ }
- if (connectionStatus != ConnectionStatus.DISCONNECTED && connectionStatus != ConnectionStatus.WAITING) {
- LOG.finest("Must be disconnected before calling connect: " + getClientUri());
- return;
- }
+ if (connectionStatus == ConnectionStatus.CONNECTED || connectionStatus == ConnectionStatus.CONNECTING) {
+ LOG.finest("Must be disconnected before calling connect: " + getClientUri());
+ return;
+ }
- LOG.fine("Connecting IO Client: " + getClientUri());
- onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+ LOG.fine("Connecting IO Client: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+ }
if (workerGroup == null) {
// TODO: In Netty 5 you can pass in an executor service; can only pass in thread factory for now
@@ -259,14 +261,16 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}
@Override
- public synchronized void disconnect() {
- if ((permanentError && connectionStatus == ConnectionStatus.ERROR) || connectionStatus == ConnectionStatus.DISCONNECTING || connectionStatus == ConnectionStatus.DISCONNECTED) {
- LOG.finest("Already disconnecting or disconnected: " + getClientUri());
- return;
- }
+ public void disconnect() {
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.ERROR || connectionStatus == ConnectionStatus.DISCONNECTING || connectionStatus == ConnectionStatus.DISCONNECTED) {
+ LOG.finest("Already disconnecting or disconnected: " + getClientUri());
+ return;
+ }
- LOG.finest("Disconnecting IO client: " + getClientUri());
- onConnectionStatusChanged(permanentError ? ConnectionStatus.ERROR : ConnectionStatus.DISCONNECTING);
+ LOG.finest("Disconnecting IO client: " + getClientUri());
+ onConnectionStatusChanged(permanentError ? ConnectionStatus.ERROR : ConnectionStatus.DISCONNECTING);
+ }
try {
if (reconnectTask != null) {
@@ -339,20 +343,26 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}
@Override
- public synchronized void addMessageConsumer(Consumer<T> messageConsumer) {
- if (!messageConsumers.contains(messageConsumer)) {
- messageConsumers.add(messageConsumer);
+ public void addMessageConsumer(Consumer<T> messageConsumer) {
+ synchronized (messageConsumers) {
+ if (!messageConsumers.contains(messageConsumer)) {
+ messageConsumers.add(messageConsumer);
+ }
}
}
@Override
- public synchronized void removeMessageConsumer(Consumer<T> messageConsumer) {
- messageConsumers.remove(messageConsumer);
+ public void removeMessageConsumer(Consumer<T> messageConsumer) {
+ synchronized (messageConsumers) {
+ messageConsumers.remove(messageConsumer);
+ }
}
@Override
- public synchronized void removeAllMessageConsumers() {
- messageConsumers.clear();
+ public void removeAllMessageConsumers() {
+ synchronized (messageConsumers) {
+ messageConsumers.clear();
+ }
}
/**
@@ -407,38 +417,41 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
try {
consumer.accept(message);
} catch (Exception e) {
- LOG.log(Level.SEVERE, "Exception occurred in message handler: " + getClientUri(), e);
- onConnectionStatusChanged(ConnectionStatus.ERROR);
+ LOG.log(Level.WARNING, "Exception occurred in message handler: " + getClientUri(), e);
}
});
}
protected void onDecodeException(ChannelHandlerContext ctx, Throwable cause) {
LOG.log(Level.SEVERE, "Exception occurred on in-bound message: " + getClientUri(), cause);
- onConnectionStatusChanged(ConnectionStatus.ERROR);
}
protected void onEncodeException(ChannelHandlerContext ctx, Throwable cause) {
LOG.log(Level.SEVERE, "Exception occurred on out-bound message: " + getClientUri(), cause);
- onConnectionStatusChanged(ConnectionStatus.ERROR);
}
- protected synchronized void onConnectionStatusChanged(ConnectionStatus connectionStatus) {
+ protected void onConnectionStatusChanged(ConnectionStatus connectionStatus) {
this.connectionStatus = connectionStatus;
- synchronized (connectionStatusConsumers) {
+ executorService.submit(() ->
connectionStatusConsumers.forEach(
- consumer -> consumer.accept(connectionStatus)
- );
- }
+ consumer -> {
+ try {
+ consumer.accept(connectionStatus);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Connection status change handler threw an exception: " + getClientUri(), e);
+ }
+ }));
}
- protected synchronized void scheduleReconnect() {
- if (reconnectTask != null) {
- return;
- }
+ protected void scheduleReconnect() {
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.WAITING) {
+ return;
+ }
- onConnectionStatusChanged(ConnectionStatus.WAITING);
+ onConnectionStatusChanged(ConnectionStatus.WAITING);
+ }
if (reconnectDelayMilliseconds < MAX_RECONNECT_DELAY_MILLIS) {
reconnectDelayMilliseconds *= RECONNECT_BACKOFF_MULTIPLIER;
@@ -448,7 +461,7 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
LOG.finest("Scheduling reconnection in '" + reconnectDelayMilliseconds + "' milliseconds: " + getClientUri());
reconnectTask = executorService.schedule(() -> {
- synchronized (AbstractNettyIoClient.this) {
+ synchronized (this) {
reconnectTask = null;
// Attempt to reconnect if not disconnecting
@@ -459,13 +472,16 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen
}, reconnectDelayMilliseconds);
}
- protected synchronized void setPermanentError(String message) {
- if (permanentError) {
- return;
- }
+ protected void setPermanentError(String message) {
- LOG.info("An unrecoverable error has occurred with client '" + getClientUri() + "' is no longer usable: " + message);
- this.permanentError = true;
+ synchronized (this) {
+ if (permanentError) {
+ return;
+ }
+
+ LOG.info("An unrecoverable error has occurred with client '" + getClientUri() + "' is no longer usable: " + message);
+ this.permanentError = true;
+ }
disconnect();
}
diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
index 22f8af7fe..d70f0192a 100644
--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetEditActivity.java
@@ -365,7 +365,7 @@ public class AssetEditActivity
// Is it agent or attribute link?
if ((valueHolder instanceof MetaItem) && AgentLink.isAgentLink((MetaItem) valueHolder)) {
query = new AssetQuery()
- .select(AssetQuery.Select.selectExcludePathAndParentAndRealm())
+ .select(AssetQuery.Select.selectExcludePathAndParentInfo())
// Limit to agents
.types(AssetType.AGENT);
diff --git a/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java b/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java
index d379cba27..5e8aeb94a 100644
--- a/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java
+++ b/client/src/main/java/org/openremote/app/client/assets/asset/AssetViewActivity.java
@@ -186,7 +186,7 @@ public class AssetViewActivity
if (subscribe) {
environment.getEventService().subscribe(
AttributeEvent.class,
- new AssetEvent.AssetIdFilter<>(asset.getId())
+ new AssetFilter<AttributeEvent>().setAssetIds(asset.getId())
);
} else {
environment.getEventService().unsubscribe(
diff --git a/client/src/main/java/org/openremote/app/client/map/MapActivity.java b/client/src/main/java/org/openremote/app/client/map/MapActivity.java
index 7b0f46daa..f93af0bf7 100644
--- a/client/src/main/java/org/openremote/app/client/map/MapActivity.java
+++ b/client/src/main/java/org/openremote/app/client/map/MapActivity.java
@@ -31,10 +31,7 @@ import org.openremote.app.client.assets.browser.TenantTreeNode;
import org.openremote.app.client.interop.value.ObjectValueMapper;
import org.openremote.app.client.mvp.AcceptsView;
import org.openremote.app.client.mvp.AppActivity;
-import org.openremote.model.asset.Asset;
-import org.openremote.model.asset.AssetAttribute;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetResource;
+import org.openremote.model.asset.*;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueType;
@@ -181,7 +178,7 @@ public class MapActivity extends AssetBrowsingActivity<MapPlace> implements MapV
showAssetInfoItems();
environment.getEventService().subscribe(
AttributeEvent.class,
- new AssetEvent.AssetIdFilter<>(asset.getId())
+ new AssetFilter<AttributeEvent>().setAssetIds(asset.getId())
);
}
});
diff --git a/client/src/main/webapp/src/or-app/or-app-events.html b/client/src/main/webapp/src/or-app/or-app-events.html
index 6ae370090..917ca26dd 100644
--- a/client/src/main/webapp/src/or-app/or-app-events.html
+++ b/client/src/main/webapp/src/or-app/or-app-events.html
@@ -41,7 +41,7 @@
this.subscribe({
eventType: "attribute",
filter: {
- filterType: "asset-id",
+ filterType: "asset",
assetIds: assetIds
}
});
diff --git a/container/src/main/java/org/openremote/container/security/AuthContext.java b/container/src/main/java/org/openremote/container/security/AuthContext.java
index 42d31d4b9..906dcba9a 100644
--- a/container/src/main/java/org/openremote/container/security/AuthContext.java
+++ b/container/src/main/java/org/openremote/container/security/AuthContext.java
@@ -32,6 +32,8 @@ public interface AuthContext {
String getUserId();
+ String getClientId();
+
/**
* @return <code>true</code> if the user is authenticated in the "master" realm and has the realm role "admin".
*/
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 62538036b..2df9c0dc7 100644
--- a/container/src/main/java/org/openremote/container/security/IdentityService.java
+++ b/container/src/main/java/org/openremote/container/security/IdentityService.java
@@ -48,7 +48,7 @@ public abstract class IdentityService implements ContainerService {
@Override
public int getPriority() {
- return ContainerService.DEFAULT_PRIORITY;
+ return ContainerService.DEFAULT_PRIORITY - 20;
}
@Override
diff --git a/container/src/main/java/org/openremote/container/security/basic/BasicAuthContext.java b/container/src/main/java/org/openremote/container/security/basic/BasicAuthContext.java
index 66a2cd0b6..2a6343681 100644
--- a/container/src/main/java/org/openremote/container/security/basic/BasicAuthContext.java
+++ b/container/src/main/java/org/openremote/container/security/basic/BasicAuthContext.java
@@ -59,6 +59,11 @@ public class BasicAuthContext implements AuthContext, Principal {
return userId;
}
+ @Override
+ public String getClientId() {
+ return null;
+ }
+
@Override
public boolean hasRealmRole(String role) {
return true;
diff --git a/container/src/main/java/org/openremote/container/security/keycloak/AccessTokenAuthContext.java b/container/src/main/java/org/openremote/container/security/keycloak/AccessTokenAuthContext.java
index 09f6099f9..c6b183733 100644
--- a/container/src/main/java/org/openremote/container/security/keycloak/AccessTokenAuthContext.java
+++ b/container/src/main/java/org/openremote/container/security/keycloak/AccessTokenAuthContext.java
@@ -50,6 +50,11 @@ public class AccessTokenAuthContext implements AuthContext {
return accessToken.getSubject();
}
+ @Override
+ public String getClientId() {
+ return accessToken.getIssuedFor();
+ }
+
@Override
public boolean hasRealmRole(String role) {
return accessToken.getRealmAccess().isUserInRole(role);
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 5fa767585..8160cab04 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
@@ -66,6 +66,12 @@ import static org.openremote.model.Constants.REQUEST_HEADER_REALM;
public abstract class KeycloakIdentityProvider implements IdentityProvider {
+ // We use this client ID to access Keycloak because by default it allows obtaining
+ // 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";
+
+
// TODO: Below is here ready for Resteasy 4.x but their maven packages are a mess at the moment
// protected static class EngineBuilder extends ClientHttpEngineBuilder43 {
//
diff --git a/container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java b/container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java
index 2164ba643..6ca72bb3e 100644
--- a/container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java
+++ b/container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java
@@ -23,12 +23,17 @@ import com.devskiller.friendly_id.Url62;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.NameBasedGenerator;
import com.fasterxml.uuid.impl.RandomBasedGenerator;
+import org.apache.james.mime4j.util.CharsetUtil;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.openremote.model.IdentifiableEntity;
import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.nio.charset.Charset;
+import java.util.UUID;
/**
* Generate a globally unique identifier value.
@@ -38,8 +43,9 @@ import java.io.Serializable;
*/
public class UniqueIdentifierGenerator implements IdentifierGenerator {
+ static final protected String macAddress = generateMacAddressString();
static final protected RandomBasedGenerator randomGenerator = Generators.randomBasedGenerator();
- static final protected NameBasedGenerator nameGenerator = Generators.nameBasedGenerator();
+ static final protected NameBasedGenerator nameGenerator = Generators.nameBasedGenerator(UUID.nameUUIDFromBytes(macAddress.getBytes(CharsetUtil.UTF_8)));
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
@@ -59,7 +65,8 @@ public class UniqueIdentifierGenerator implements IdentifierGenerator {
}
/**
- * Generates the same UUID value encoded as Base62, 22 characters long, if the name is the same.
+ * Generates the same UUID for the current system value encoded as Base62, 22 characters long, if the name is the same.
+ * The UUID is namespaced with the MAC address of the host to provide a level of variability for multiple deployments.
*/
public static String generateId(String name) {
return Url62.encode(nameGenerator.generate(name));
@@ -71,4 +78,24 @@ public class UniqueIdentifierGenerator implements IdentifierGenerator {
public static String generateId(byte[] bytes) {
return Url62.encode(nameGenerator.generate(bytes));
}
+
+ public static String getMacAddress() {
+ return macAddress;
+ }
+
+ protected static String generateMacAddressString() {
+ try {
+ final InetAddress ip = InetAddress.getLocalHost();
+ final NetworkInterface network = NetworkInterface.getByInetAddress(ip);
+
+ byte[] macAddress = network.getHardwareAddress();
+ StringBuilder sb = new StringBuilder();
+ for (byte address : macAddress) {
+ sb.append(String.format("%02X", (address & 0xFF)));
+ }
+ return sb.toString();
+ } catch (Exception e) {
+ return "000000000000";
+ }
+ }
}
diff --git a/container/src/main/java/org/openremote/container/web/WebResource.java b/container/src/main/java/org/openremote/container/web/WebResource.java
index b6186994c..c10403268 100644
--- a/container/src/main/java/org/openremote/container/web/WebResource.java
+++ b/container/src/main/java/org/openremote/container/web/WebResource.java
@@ -113,6 +113,11 @@ public class WebResource implements AuthContext {
return isAuthenticated() ? getAuthContext().getUserId() : null;
}
+ @Override
+ public String getClientId() {
+ return isAuthenticated() ? getAuthContext().getClientId() : null;
+ }
+
@Override
public boolean hasRealmRole(String role) {
return isAuthenticated() && getAuthContext().hasRealmRole(role);
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
index 40df29cb0..502d7d96d 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -39,6 +39,7 @@ import org.openremote.manager.asset.AssetProcessingService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.asset.AssetUpdateProcessor;
import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.asset.Asset;
@@ -73,6 +74,7 @@ import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.concurrent.GlobalLock.withLockReturning;
import static org.openremote.container.persistence.PersistenceEvent.*;
import static org.openremote.manager.asset.AssetProcessingService.ASSET_QUEUE;
+import static org.openremote.manager.gateway.GatewayService.GATEWAY_SERVICE_PRIORITY;
import static org.openremote.model.AbstractValueTimestampHolder.VALUE_TIMESTAMP_FIELD_NAME;
import static org.openremote.model.asset.AssetAttribute.attributesFromJson;
import static org.openremote.model.asset.AssetAttribute.getAddedOrModifiedAttributes;
@@ -80,6 +82,7 @@ import static org.openremote.model.asset.AssetType.AGENT;
import static org.openremote.model.asset.agent.AgentLink.getAgentLink;
import static org.openremote.model.asset.agent.ConnectionStatus.*;
import static org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE;
+import static org.openremote.model.attribute.AttributeEvent.Source.GATEWAY;
import static org.openremote.model.attribute.AttributeEvent.Source.SENSOR;
import static org.openremote.model.util.TextUtil.isNullOrEmpty;
import static org.openremote.model.util.TextUtil.isValidURN;
@@ -92,12 +95,14 @@ import static org.openremote.model.util.TextUtil.isValidURN;
public class AgentService extends RouteBuilder implements ContainerService, AssetUpdateProcessor, ProtocolAssetService {
private static final Logger LOG = Logger.getLogger(AgentService.class.getName());
+ public static final int AGENT_SERVICE_PRIORITY = GATEWAY_SERVICE_PRIORITY + 1; // Start after the gateway service
protected TimerService timerService;
protected ManagerIdentityService identityService;
protected AssetProcessingService assetProcessingService;
protected AssetStorageService assetStorageService;
protected MessageBrokerService messageBrokerService;
protected ClientEventService clientEventService;
+ protected GatewayService gatewayService;
protected final Map<AttributeRef, Pair<AssetAttribute, ConnectionStatus>> protocolConfigurations = new HashMap<>();
protected final Map<String, Protocol> protocols = new HashMap<>();
protected final Map<AttributeRef, List<AttributeRef>> linkedAttributes = new HashMap<>();
@@ -111,7 +116,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
*/
@Override
public int getPriority() {
- return ContainerService.DEFAULT_PRIORITY;
+ return AGENT_SERVICE_PRIORITY;
}
@Override
@@ -122,6 +127,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
assetStorageService = container.getService(AssetStorageService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
clientEventService = container.getService(ClientEventService.class);
+ gatewayService = container.getService(GatewayService.class);
localAgentConnector = new LocalAgentConnector(this);
clientEventService.addSubscriptionAuthorizer((auth, subscription) ->
@@ -282,7 +288,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
LOG.fine("Merging (and overriding existing older version of) with protocol-provided: " + asset);
if (options != null && options.getAssignToUserName() != null) {
- return assetStorageService.merge(updatedAsset, true, options.getAssignToUserName());
+ return assetStorageService.merge(updatedAsset, true, false, options.getAssignToUserName());
} else {
return assetStorageService.merge(updatedAsset, true);
}
@@ -291,7 +297,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
@Override
public boolean deleteAsset(String assetId) {
LOG.fine("Deleting protocol-provided: " + assetId);
- return assetStorageService.delete(Collections.singletonList(assetId));
+ return assetStorageService.delete(Collections.singletonList(assetId), false);
}
@Override
@@ -314,18 +320,24 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
* Looks for new, modified and obsolete protocol configurations and links / unlinks any associated attributes
*/
protected void processAgentChange(Asset agent, PersistenceEvent persistenceEvent) {
+
LOG.finest("Processing agent persistence event: " + persistenceEvent.getCause());
switch (persistenceEvent.getCause()) {
case CREATE:
- addReplaceAgent(agent);
+ if (!addReplaceAgent(agent)) {
+ LOG.finest("Agent is a gateway asset so ignoring");
+ return;
+ }
linkProtocolConfigurations(
agent.getAttributesStream().filter(ProtocolConfiguration::isProtocolConfiguration)
);
-
break;
case UPDATE:
- addReplaceAgent(agent);
+ if (!addReplaceAgent(agent)) {
+ LOG.finest("Agent is a gateway asset so ignoring");
+ return;
+ }
// Check if any protocol config attributes have been added/removed or modified
int attributesIndex = Arrays.asList(persistenceEvent.getPropertyNames()).indexOf("attributes");
if (attributesIndex < 0) {
@@ -371,7 +383,10 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
break;
case DELETE:
- removeAgent(agent);
+ if (!removeAgent(agent)) {
+ LOG.finest("Agent is a gateway asset so ignoring");
+ return;
+ }
// Unlink any attributes that have an agent link to this agent
unlinkProtocolConfigurations(agent.getAttributesStream()
.filter(ProtocolConfiguration::isProtocolConfiguration)
@@ -390,6 +405,13 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
switch (persistenceEvent.getCause()) {
case CREATE:
+ // Check if asset parent is a gateway or a gateway descendant, if so ignore it
+ // Need to look at parent as this asset may not have been acknowledged by the gateway service yet
+ if (gatewayService.getLocallyRegisteredGatewayId(asset.getId(), asset.getParentId()) != null) {
+ LOG.finest("This is a gateway descendant asset so ignoring: " + asset.getId());
+ return;
+ }
+
// Asset insert persistence events can be fired before the agent insert persistence event
// so need to check that all protocol configs exist - any that don't we will exclude here
// and handle in agent insert
@@ -408,6 +430,12 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
break;
case UPDATE:
+
+ if (gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) != null) {
+ LOG.finest("This is a gateway descendant asset so ignoring: " + asset.getId());
+ return;
+ }
+
List<String> propertyNames = Arrays.asList(persistenceEvent.getPropertyNames());
// Check if attributes of the asset have been modified
@@ -416,6 +444,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
return;
}
+
// Attributes have possibly changed so need to compare old and new state to determine any changes to
// AGENT_LINK attributes
List<AssetAttribute> oldAgentLinkedAttributes =
@@ -423,15 +452,24 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
(ObjectValue) persistenceEvent.getPreviousState()[attributesIndex],
asset.getId()
)
- .filter(AgentLink::hasAgentLink)
+ .filter(assetAttribute ->
+ // Exclude attributes without agent link or with agent link to not recognised agents (could be gateway agents)
+ assetAttribute.getMetaItem(MetaItemType.AGENT_LINK)
+ .flatMap(agentLinkMetaItem -> AttributeRef.fromValue(agentLinkMetaItem.getValue().orElse(null)))
+ .map(agentLinkRef -> getAgents().containsKey(agentLinkRef.getEntityId()))
+ .orElse(false))
.collect(Collectors.toList());
List<AssetAttribute> newAgentLinkedAttributes =
attributesFromJson(
(ObjectValue) persistenceEvent.getCurrentState()[attributesIndex],
- asset.getId()
- )
- .filter(AgentLink::hasAgentLink)
+ asset.getId())
+ .filter(assetAttribute ->
+ // Exclude attributes without agent link or with agent link to not recognised agents (could be gateway agents)
+ assetAttribute.getMetaItem(MetaItemType.AGENT_LINK)
+ .flatMap(agentLinkMetaItem -> AttributeRef.fromValue(agentLinkMetaItem.getValue().orElse(null)))
+ .map(agentLinkRef -> getAgents().containsKey(agentLinkRef.getEntityId()))
+ .orElse(false))
.collect(Collectors.toList());
// Unlink thing attributes that are in old but not in new
@@ -450,6 +488,11 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
break;
case DELETE: {
+ if (gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) != null) {
+ LOG.finest("This is a gateway descendant asset so ignoring: " + asset.getId());
+ return;
+ }
+
// Unlink any AGENT_LINK attributes from the referenced protocol
Map<AssetAttribute, List<AssetAttribute>> groupedAgentLinkAndProtocolAttributes =
getGroupedAgentLinkAttributes(asset.getAttributesStream(), attribute -> true);
@@ -672,7 +715,8 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
Asset asset,
AssetAttribute attribute,
Source source) throws AssetProcessingException {
- if (source == SENSOR) {
+
+ if (source == SENSOR || source == GATEWAY) {
return false;
}
@@ -716,7 +760,12 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
Consumer<AssetAttribute> notFoundConsumer) {
Map<AssetAttribute, List<AssetAttribute>> result = new HashMap<>();
attributes
- .filter(AgentLink::hasAgentLink)
+ .filter(assetAttribute ->
+ // Exclude attributes without agent link or with agent link to not recognised agents (could be gateway agents)
+ assetAttribute.getMetaItem(MetaItemType.AGENT_LINK)
+ .flatMap(agentLinkMetaItem -> AttributeRef.fromValue(agentLinkMetaItem.getValue().orElse(null)))
+ .map(agentLinkRef -> getAgents().containsKey(agentLinkRef.getEntityId()))
+ .orElse(false))
.filter(filter)
.map(attribute -> new Pair<>(attribute, getAgentLink(attribute)))
.filter(pair -> pair.value.isPresent())
@@ -755,14 +804,19 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
return !Agent.hasUrl(agent) ? Optional.of(localAgentConnector) : Optional.empty();
}
- protected void addReplaceAgent(Asset agent) {
+ protected boolean addReplaceAgent(Asset agent) {
// Fully load agent asset
final Asset loadedAgent = assetStorageService.find(agent.getId(), true);
+ if (gatewayService.getLocallyRegisteredGatewayId(agent.getId(), agent.getParentId()) != null) {
+ return false;
+ }
withLock(getClass().getSimpleName() + "::addReplaceAgent", () -> getAgents().put(loadedAgent.getId(), loadedAgent));
+ return true;
}
- protected void removeAgent(Asset agent) {
- withLock(getClass().getSimpleName() + "::removeAgent", () -> getAgents().remove(agent.getId()));
+ @SuppressWarnings("ConstantConditions")
+ protected boolean removeAgent(Asset agent) {
+ return withLockReturning(getClass().getSimpleName() + "::removeAgent", () -> getAgents().remove(agent.getId()) != null);
}
public Map<String, Asset> getAgents() {
@@ -771,6 +825,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
agentMap = assetStorageService.findAll(new AssetQuery()
.types(AssetType.AGENT))
.stream()
+ .filter(asset -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) == null)
.collect(Collectors.toMap(Asset::getId, agent -> agent));
}
return agentMap;
@@ -780,7 +835,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
/**
* Apply the specified set of {@link ValueFilter}s to the specified {@link Value}
*/
- public Value applyValueFilters(Value value, ValueFilter... filters) {
+ public Value applyValueFilters(Value value, ValueFilter<?>... filters) {
if (filters == null) {
return value;
@@ -788,7 +843,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
LOG.fine("Applying value filters to value...");
- for (ValueFilter filter : filters) {
+ for (ValueFilter<?> filter : filters) {
boolean filterOk = filter.getValueType() == Value.class || filter.getValueType() == value.getType().getModelType();
if (!filterOk) {
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java
index b40305f5e..de4943f97 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingException.java
@@ -117,7 +117,12 @@ public class AssetProcessingException extends RuntimeException {
/**
* The event value is not the excepted value for the attribute
*/
- INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE
+ INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE,
+
+ /**
+ * The event is for a gateway descendant asset but the gateway is not connected
+ */
+ GATEWAY_DISCONNECTED
}
final protected Reason reason;
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 47493b59e..035dc07cf 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java
@@ -33,6 +33,7 @@ import org.openremote.container.timer.TimerService;
import org.openremote.manager.agent.AgentService;
import org.openremote.manager.datapoint.AssetDatapointService;
import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.rules.RulesService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.model.Constants;
@@ -131,6 +132,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
protected PersistenceService persistenceService;
protected RulesService rulesService;
protected AgentService agentService;
+ protected GatewayService gatewayService;
protected AssetStorageService assetStorageService;
protected AssetDatapointService assetDatapointService;
protected AssetAttributeLinkingService assetAttributeLinkingService;
@@ -153,6 +155,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
persistenceService = container.getService(PersistenceService.class);
rulesService = container.getService(RulesService.class);
agentService = container.getService(AgentService.class);
+ gatewayService = container.getService(GatewayService.class);
assetStorageService = container.getService(AssetStorageService.class);
assetDatapointService = container.getService(AssetDatapointService.class);
assetAttributeLinkingService = container.getService(AssetAttributeLinkingService.class);
@@ -165,7 +168,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
// Only Asset ID filters allowed
- if (subscription.getFilter() != null && !(subscription.getFilter() instanceof AssetEvent.AssetIdFilter)) {
+ if (subscription.getFilter() != null && !(subscription.getFilter() instanceof AssetFilter)) {
return false;
}
@@ -175,7 +178,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
if (subscription.getFilter() != null) {
- AssetEvent.AssetIdFilter filter = (AssetEvent.AssetIdFilter) subscription.getFilter();
+ AssetFilter filter = (AssetFilter) subscription.getFilter();
// Superuser can get events for any asset
if (auth.isSuperUser())
@@ -210,6 +213,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
return false;
});
+ processors.add(gatewayService);
processors.add(agentService);
processors.add(rulesService);
processors.add(assetDatapointService);
@@ -282,6 +286,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
if (asset == null)
throw new AssetProcessingException(ASSET_NOT_FOUND);
+
AssetAttribute oldAttribute = asset.getAttribute(event.getAttributeName()).orElse(null);
if (oldAttribute == null)
throw new AssetProcessingException(ATTRIBUTE_NOT_FOUND);
@@ -359,7 +364,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
}
}
- //Check if attribute is well known and the value is valid
+ // Check if attribute is well known and the value is valid
AssetModelUtil.getAttributeDescriptor(oldAttribute.name).ifPresent(wellKnownAttribute -> {
// Check if the value is valid
wellKnownAttribute.getValueDescriptor()
@@ -551,7 +556,7 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer
attribute.getNameOrThrow(),
attribute.getValue().orElse(null),
timerService.getCurrentTimeMillis()
- )
+ ).setParentId(asset.getParentId()).setRealm(asset.getRealm())
);
}
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
index 774e3b900..194ecf382 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java
@@ -251,7 +251,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
asset,
storageAsset,
isRestrictedUser ? storageAsset.getName() : null, // TODO We could allow restricted users to update names?
- isRestrictedUser ? storageAsset.getRealm() : null, // Restricted users can not change realm
+ storageAsset.getRealm(), // The realm can never change
isRestrictedUser ? storageAsset.getParentId() : null, // Restricted users can not change realm
storageAsset.getType(), // The type can never change
isRestrictedUser ? storageAsset.isAccessPublicRead() : null, // Restricted user can not change access public flag
@@ -527,7 +527,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResour
throw new WebApplicationException(FORBIDDEN);
}
- if (!assetStorageService.delete(assetIds)) {
+ if (!assetStorageService.delete(assetIds, false)) {
throw new WebApplicationException(BAD_REQUEST);
}
} catch (IllegalStateException ex) {
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 e10572609..72550fcef 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -32,6 +32,7 @@ import org.openremote.container.security.AuthContext;
import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.console.ConsoleResourceImpl;
import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.rules.AssetQueryPredicate;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
@@ -124,6 +125,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
protected PersistenceService persistenceService;
protected ManagerIdentityService identityService;
protected ClientEventService clientEventService;
+ protected GatewayService gatewayService;
/**
* Will evaluate each {@link CalendarEventPredicate} and apply it depending on the {@link LogicGroup} type
@@ -138,7 +140,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
return calendarEventPredicateMatches(query.attributes, asset);
}
- private static boolean calendarEventPredicateMatches(LogicGroup<AttributePredicate> group, Asset asset) {
+ protected static boolean calendarEventPredicateMatches(LogicGroup<AttributePredicate> group, Asset asset) {
boolean isOr = group.operator == LogicGroup.Operator.OR;
boolean matches = true;
@@ -208,6 +210,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
persistenceService = container.getService(PersistenceService.class);
identityService = container.getService(ManagerIdentityService.class);
clientEventService = container.getService(ClientEventService.class);
+ gatewayService = container.getService(GatewayService.class);
META_ITEM_RESTRICTED_READ_SQL_FRAGMENT =
" ('" + Arrays.stream(AssetModelUtil.getMetaItemDescriptors()).filter(i -> i.getAccess().restrictedRead).map(MetaItemDescriptor::getUrn).collect(joining("','")) + "')";
@@ -398,7 +401,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
* @throws IllegalArgumentException if the realm or parent is illegal, or other asset constraint is violated.
*/
public Asset merge(Asset asset, boolean overrideVersion) {
- return merge(asset, overrideVersion, null);
+ return merge(asset, overrideVersion, false, null);
}
/**
@@ -407,28 +410,40 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
* @throws IllegalArgumentException if the realm or parent is illegal, or other asset constraint is violated.
*/
public Asset merge(Asset asset, String userName) {
- return merge(asset, false, userName);
+ return merge(asset, false, false, userName);
}
/**
* @param overrideVersion If <code>true</code>, the merge will override the data in the database, independent of
* version.
+ * @param skipGatewayCheck Don't check if asset is a gateway asset and merge asset into local persistence service.
* @param userName the user which this asset needs to be assigned to.
* @return The current stored asset state.
* @throws IllegalArgumentException if the realm or parent is illegal, or other asset constraint is violated.
*/
- public Asset merge(Asset asset, boolean overrideVersion, String userName) {
+ public Asset merge(Asset asset, boolean overrideVersion, boolean skipGatewayCheck, String userName) {
return persistenceService.doReturningTransaction(em -> {
- // Update all empty attribute timestamps with server-time (a caller which doesn't have a
- // reliable time source such as a browser should clear the timestamp when setting an attribute
- // value).
- asset.getAttributesStream().forEach(attribute -> {
- Optional<Long> timestamp = attribute.getValueTimestamp();
- if (!timestamp.isPresent() || timestamp.get() <= 0) {
- attribute.setValueTimestamp(timerService.getCurrentTimeMillis());
+ if (asset.getId() != null) {
+
+ Asset existing = em.find(Asset.class, asset.getId());
+
+ // Verify type has not been changed
+ if (existing != null && !existing.getType().equals(asset.getType())) {
+ throw new IllegalStateException("Asset type cannot be changed");
+ }
+
+ if (existing != null && !existing.getRealm().equals(asset.getRealm())) {
+ throw new IllegalStateException("Asset realm cannot be changed");
}
- });
+
+ // 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
+ // concurrent updates
+ if (existing != null && overrideVersion) {
+ asset.setVersion(existing.getVersion());
+ }
+ }
// Validate parent
if (asset.getParentId() != null) {
@@ -468,15 +483,15 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
throw new IllegalStateException("Storing asset failed, invalid attributes: " + invalid);
}
- // 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
- // concurrent updates
- if (asset.getId() != null && overrideVersion) {
- Asset existing = em.find(Asset.class, asset.getId());
- if (existing != null) {
- asset.setVersion(existing.getVersion());
+ // Update all empty attribute timestamps with server-time (a caller which doesn't have a
+ // reliable time source such as a browser should clear the timestamp when setting an attribute
+ // value).
+ asset.getAttributesStream().forEach(attribute -> {
+ Optional<Long> timestamp = attribute.getValueTimestamp();
+ if (!timestamp.isPresent() || timestamp.get() <= 0) {
+ attribute.setValueTimestamp(timerService.getCurrentTimeMillis());
}
- }
+ });
// If username present
User user = null;
@@ -489,7 +504,15 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
LOG.fine("Storing: " + asset);
- Asset updatedAsset = em.merge(asset);
+ Asset updatedAsset;
+ String gatewayId = gatewayService.getLocallyRegisteredGatewayId(asset.getId(), asset.getParentId());
+
+ if (!skipGatewayCheck && gatewayId != null) {
+ LOG.fine("Sending asset merge request to gateway: Gateway ID=" + gatewayId);
+ updatedAsset = gatewayService.mergeGatewayAsset(gatewayId, asset);
+ } else {
+ updatedAsset = em.merge(asset);
+ }
if (user != null) {
storeUserAsset(em, new UserAsset(user.getRealm(), user.getId(), updatedAsset.getId()));
@@ -503,18 +526,91 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
* @return <code>true</code> if the assets were deleted, false if any of the assets still have children and can't be deleted.
*/
public boolean delete(List<String> assetIds) {
+ return delete(assetIds, false);
+ }
+
+ public boolean delete(List<String> assetIds, boolean skipGatewayCheck) {
+
+ List<String> ids = new ArrayList<>(assetIds);
+ Map<String, List<String>> gatewayIdAssetIdMap = new HashMap<>();
+
+ if (!skipGatewayCheck) {
+ List<String> gatewayIds = ids.stream().filter(id -> gatewayService.isLocallyRegisteredGateway(id)).collect(Collectors.toList());
+
+ if (!gatewayIds.isEmpty()) {
+ // Handle gateway asset deletion in a special way
+ ids.removeAll(gatewayIds);
+ for (String gatewayId : gatewayIds) {
+ try {
+ boolean deleted = gatewayService.deleteGateway(gatewayId);
+ if (!deleted) {
+ return false;
+ }
+ } catch (Exception e) {
+ return false;
+ }
+ }
+ }
+
+ ids.removeIf(id -> {
+ String gatewayId = gatewayService.getLocallyRegisteredGatewayId(id, null);
+ if (gatewayId != null) {
+
+ if (gatewayIds.contains(gatewayId)) {
+ // Gateway is being deleted so no need to try and delete this descendant asset
+ return true;
+ }
+
+ gatewayIdAssetIdMap.compute(gatewayId, (gId, aIds) -> {
+ if (aIds == null) {
+ aIds = new ArrayList<>();
+ }
+ aIds.add(id);
+ return aIds;
+ });
+ return true;
+ }
+ return false;
+ });
+
+ if (gatewayIdAssetIdMap.isEmpty() && ids.isEmpty()) {
+ return true;
+ }
+
+ // This is not atomic across gateways
+ if (!gatewayIdAssetIdMap.isEmpty()) {
+ for (Map.Entry<String, List<String>> gatewayIdAssetIds : gatewayIdAssetIdMap.entrySet()) {
+ String gatewayId = gatewayIdAssetIds.getKey();
+ List<String> gatewayAssetIds = gatewayIdAssetIds.getValue();
+ try {
+ boolean deleted = gatewayService.deleteGatewayAssets(gatewayId, gatewayAssetIds);
+ if (!deleted) {
+ return false;
+ }
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ if (ids.isEmpty()) {
+ return true;
+ }
+ }
+ }
+
try {
persistenceService.doTransaction(em -> {
- LOG.fine("Removing: " + String.join(", ", assetIds));
+ LOG.fine("Removing: " + String.join(", ", ids));
List<Asset> assets = em
- .createQuery("select a from Asset a where not exists(select child.id from Asset child where child.parentId = a.id) and a.id in :ids", Asset.class)
- .setParameter("ids", assetIds)
+ .createQuery("select a from Asset a where not exists(select child.id from Asset child where child.parentId = a.id and not child.id in :ids) and a.id in :ids", Asset.class)
+ .setParameter("ids", ids)
.getResultList();
if (assetIds.size() != assets.size()) {
throw new IllegalArgumentException("Cannot delete one or more requested assets as they either have children or don't exist");
}
+ assets.sort(Comparator.comparingInt((Asset asset) -> asset.getPath() == null ? 0 : asset.getPath().length).reversed());
assets.forEach(em::remove);
});
} catch (Exception e) {
@@ -791,13 +887,15 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
sb.append(", A.REALM AS REALM, A.OBJ_VERSION as OBJ_VERSION");
if (select == null || !select.excludeParentInfo) {
- sb.append(", P.NAME as PARENT_NAME, P.ASSET_TYPE as PARENT_TYPE");
+ if (level == 3) {
+ sb.append(", A.PARENT_NAME as PARENT_NAME, A.PARENT_TYPE as PARENT_TYPE");
+ } else {
+ sb.append(", P.NAME as PARENT_NAME, P.ASSET_TYPE as PARENT_TYPE");
+ }
}
- if (select == null ||!select.excludeRealm) {
- if (!query.recursive || level == 3) {
- sb.append(", R.NAME as TENANT_NAME");
- }
+ if (!query.recursive || level == 3) {
+ sb.append(", A.NAME as TENANT_NAME");
}
if (!query.recursive || level == 3) {
@@ -920,7 +1018,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
// level = 3 is CTE
StringBuilder sb = new StringBuilder();
boolean recursive = query.recursive;
- boolean includeRealmInfo = query.select == null || !query.select.excludeRealm;
if (level == 1) {
sb.append(" from Asset A ");
@@ -931,10 +1028,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
sb.append(" from top_level_assets A ");
}
- if ((!recursive || level == 3) && (includeRealmInfo || query.tenant != null)) {
- sb.append("join PUBLIC.REALM R on R.NAME = A.REALM ");
- }
-
if ((!recursive || level == 3) && query.ids == null && query.userIds != null && query.userIds.length > 0) {
sb.append("cross join USER_ASSET ua ");
}
@@ -1078,6 +1171,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, pred.name));
}
+ } else {
+ sb.append("true");
}
sb.append(")");
@@ -1106,7 +1201,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
if (!recursive || level == 3) {
if (query.tenant != null && !TextUtil.isNullOrEmpty(query.tenant.realm)) {
- sb.append(" and R.NAME = ?");
+ sb.append(" and A.REALM = ?");
final int pos = binders.size() + 1;
binders.add(st -> st.setString(pos, query.tenant.realm));
}
@@ -1595,17 +1690,14 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
asset.setVersion(rs.getLong("OBJ_VERSION"));
asset.setCreatedOn(rs.getTimestamp("CREATED_ON"));
asset.setAccessPublicRead(rs.getBoolean("ACCESS_PUBLIC_READ"));
+ asset.setParentId(rs.getString("PARENT_ID"));
+ asset.setRealm(rs.getString("REALM"));
if (query.select == null || !query.select.excludeParentInfo) {
- asset.setParentId(rs.getString("PARENT_ID"));
asset.setParentName(rs.getString("PARENT_NAME"));
asset.setParentType(rs.getString("PARENT_TYPE"));
}
- if (query.select == null || !query.select.excludeRealm) {
- asset.setRealm(rs.getString("REALM"));
- }
-
if (query.select == null || !query.select.excludeAttributes) {
if (rs.getString("ATTRIBUTES") != null) {
asset.setAttributes(Values.instance().<ObjectValue>parse(rs.getString("ATTRIBUTES")).orElse(null));
@@ -1712,15 +1804,13 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
newAttribute.getNameOrThrow(),
newAttribute.getValue().orElse(null),
newAttribute.getValueTimestamp().orElse(timerService.getCurrentTimeMillis()))
+ .setParentId(asset.getParentId()).setRealm(asset.getRealm())
));
break;
case UPDATE:
// Use simple equality check on each property
String[] updatedProperties = Arrays.stream(persistenceEvent.getPropertyNames()).filter(propertyName -> {
- if ("attributes".equals(propertyName)) {
- return false;
- }
Object oldValue = persistenceEvent.getPreviousState(propertyName);
Object newValue = persistenceEvent.getCurrentState(propertyName);
return !Objects.equals(oldValue, newValue);
@@ -1792,6 +1882,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
newOrModifiedAttribute.getNameOrThrow(),
newOrModifiedAttribute.getValue().orElse(null),
newOrModifiedAttribute.getValueTimestamp().orElse(timerService.getCurrentTimeMillis()))
+ .setParentId(asset.getParentId()).setRealm(asset.getRealm())
));
break;
case DELETE:
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
index b1aa4ff39..b7ef2f1d9 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
@@ -314,7 +314,7 @@ public class AssetDatapointService implements ContainerService, AssetUpdateProce
.attributeMeta(
new MetaPredicate(MetaItemType.DATA_POINTS_MAX_AGE_DAYS),
new MetaPredicate(MetaItemType.STORE_DATA_POINTS))
- .select(AssetQuery.Select.selectExcludePathAndParentAndRealm()));
+ .select(AssetQuery.Select.selectExcludePathAndParentInfo()));
List<AssetAttribute> attributes = assets.stream()
.map(asset -> asset
diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
index f2c80de2b..1497b580b 100644
--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
@@ -29,6 +29,7 @@ import org.openremote.container.security.AuthContext;
import org.openremote.container.timer.TimerService;
import org.openremote.container.web.socket.WebsocketConstants;
import org.openremote.manager.concurrent.ManagerExecutorService;
+import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.model.Constants;
import org.openremote.model.event.shared.*;
@@ -39,6 +40,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Logger;
import static org.apache.camel.builder.PredicateBuilder.or;
+import static org.openremote.manager.gateway.GatewayService.isGatewayClientId;
/**
* Receives and publishes messages, handles the client/server event bus.
@@ -105,6 +107,7 @@ public class ClientEventService implements ContainerService {
protected MessageBrokerService messageBrokerService;
protected ManagerIdentityService identityService;
protected EventSubscriptions eventSubscriptions;
+ protected GatewayService gatewayService;
protected boolean stopped;
@Override
@@ -117,6 +120,7 @@ public class ClientEventService implements ContainerService {
timerService = container.getService(TimerService.class);
messageBrokerService = container.getService(MessageBrokerService.class);
identityService = container.getService(ManagerIdentityService.class);
+ gatewayService = container.getService(GatewayService.class);
eventSubscriptions = new EventSubscriptions(
container.getService(TimerService.class),
@@ -136,68 +140,83 @@ public class ClientEventService implements ContainerService {
from("websocket://" + WEBSOCKET_EVENTS)
.routeId("FromClientWebsocketEvents")
.choice()
- .when(header(WebsocketConstants.SESSION_OPEN))
- .process(exchange -> {
- String sessionKey = getSessionKey(exchange);
- sendToSession(sessionKey, "CONNECTED");
- })
- .stop()
+ .when(header(WebsocketConstants.SESSION_OPEN))
+ .process(exchange -> {
+ String sessionKey = getSessionKey(exchange);
+ sendToSession(sessionKey, "CONNECTED");
+ })
+ .choice()
+ .when(exchange -> isGatewayClientId(getClientId(exchange)))
+ .to(GatewayService.GATEWAY_EVENT_TOPIC)
+ .endChoice()
+ .stop()
.when(or(
header(WebsocketConstants.SESSION_CLOSE),
header(WebsocketConstants.SESSION_CLOSE_ERROR)
))
- .process(exchange -> {
- String sessionKey = getSessionKey(exchange);
- eventSubscriptions.cancelAll(sessionKey);
- })
- .stop()
- .end()
- .choice()
+ .process(exchange -> {
+ String sessionKey = getSessionKey(exchange);
+ eventSubscriptions.cancelAll(sessionKey);
+ })
+ .choice()
+ .when(exchange -> isGatewayClientId(getClientId(exchange)))
+ .to(GatewayService.GATEWAY_EVENT_TOPIC)
+ .endChoice()
+ .stop()
.when(bodyAs(String.class).startsWith(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX))
- .convertBodyTo(EventSubscription.class)
- .process(exchange -> {
- String sessionKey = getSessionKey(exchange);
- EventSubscription subscription = exchange.getIn().getBody(EventSubscription.class);
- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
- if (eventSubscriptionAuthorizers.stream()
- .anyMatch(authorizer -> authorizer.apply(authContext, subscription))) {
- boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId());
- eventSubscriptions.createOrUpdate(sessionKey, restrictedUser, subscription);
- sendToSession(sessionKey, subscription);
- } else {
- LOG.warning("Unauthorized subscription from '"
- + authContext.getUsername() + "' in realm '" + authContext.getAuthenticatedRealm()
- + "': " + subscription
- );
- sendToSession(sessionKey, new UnauthorizedEventSubscription(subscription));
- }
- })
+ .convertBodyTo(EventSubscription.class)
+ .process(exchange -> {
+ String sessionKey = getSessionKey(exchange);
+ EventSubscription subscription = exchange.getIn().getBody(EventSubscription.class);
+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
+ if (eventSubscriptionAuthorizers.stream()
+ .anyMatch(authorizer -> authorizer.apply(authContext, subscription))) {
+ boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId());
+ eventSubscriptions.createOrUpdate(sessionKey, restrictedUser, subscription);
+ subscription.setSubscribed(true);
+ sendToSession(sessionKey, subscription);
+ } else {
+ LOG.warning("Unauthorized subscription from '"
+ + authContext.getUsername() + "' in realm '" + authContext.getAuthenticatedRealm()
+ + "': " + subscription
+ );
+ sendToSession(sessionKey, new UnauthorizedEventSubscription(subscription));
+ }
+ })
+ .stop()
.when(bodyAs(String.class).startsWith(CancelEventSubscription.MESSAGE_PREFIX))
- .convertBodyTo(CancelEventSubscription.class)
- .process(exchange -> {
- String sessionKey = getSessionKey(exchange);
- eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));
- })
+ .convertBodyTo(CancelEventSubscription.class)
+ .process(exchange -> {
+ String sessionKey = getSessionKey(exchange);
+ eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));
+ })
+ .stop()
.when(bodyAs(String.class).startsWith(RenewEventSubscriptions.MESSAGE_PREFIX))
- .convertBodyTo(RenewEventSubscriptions.class)
- .process(exchange -> {
- String sessionKey = getSessionKey(exchange);
- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
- boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId());
- eventSubscriptions.update(sessionKey, restrictedUser,exchange.getIn().getBody(RenewEventSubscriptions.class).getSubscriptionIds());
- })
+ .convertBodyTo(RenewEventSubscriptions.class)
+ .process(exchange -> {
+ String sessionKey = getSessionKey(exchange);
+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
+ boolean restrictedUser = identityService.getIdentityProvider().isRestrictedUser(authContext.getUserId());
+ eventSubscriptions.update(sessionKey, restrictedUser,exchange.getIn().getBody(RenewEventSubscriptions.class).getSubscriptionIds());
+ })
+ .stop()
.when(bodyAs(String.class).startsWith(SharedEvent.MESSAGE_PREFIX))
- .convertBodyTo(SharedEvent.class)
- .process(exchange -> {
- SharedEvent event = exchange.getIn().getBody(SharedEvent.class);
- // If there is no timestamp in event, set to system time
- if (event.getTimestamp() <= 0) {
- event.setTimestamp(timerService.getCurrentTimeMillis());
- }
- })
- .to(ClientEventService.CLIENT_EVENT_TOPIC)
+ .convertBodyTo(SharedEvent.class)
+ .process(exchange -> {
+ SharedEvent event = exchange.getIn().getBody(SharedEvent.class);
+ // If there is no timestamp in event, set to system time
+ if (event.getTimestamp() <= 0) {
+ event.setTimestamp(timerService.getCurrentTimeMillis());
+ }
+ })
+ .choice()
+ .when(exchange -> isGatewayClientId(getClientId(exchange)))
+ .to(GatewayService.GATEWAY_EVENT_TOPIC)
+ .otherwise()
+ .to(ClientEventService.CLIENT_EVENT_TOPIC)
+ .endChoice()
.otherwise()
- .process(exchange -> LOG.fine("Unsupported message body: " + exchange.getIn().getBody()))
+ .process(exchange -> LOG.fine("Unsupported message body: " + exchange.getIn().getBody()))
.end();
from(ClientEventService.CLIENT_EVENT_QUEUE)
@@ -273,4 +292,8 @@ public class ClientEventService implements ContainerService {
'}';
}
+ public static String getClientId(Exchange exchange) {
+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
+ return authContext.getClientId();
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java
index ac99ffdfe..f842f85ed 100644
--- a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java
+++ b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java
@@ -49,6 +49,7 @@ import org.openremote.model.event.shared.*;
import java.util.logging.Logger;
import static org.openremote.model.event.shared.EventSubscription.SUBSCRIBED_MESSAGE_PREFIX;
+import static org.openremote.model.event.shared.EventSubscription.SUBSCRIBE_MESSAGE_PREFIX;
public class EventTypeConverters implements TypeConverters {
@@ -79,7 +80,7 @@ public class EventTypeConverters implements TypeConverters {
@Converter
public String writeEventSubscription(EventSubscription eventSubscription, Exchange exchange) throws Exception {
- return SUBSCRIBED_MESSAGE_PREFIX + Container.JSON.writeValueAsString(eventSubscription);
+ return (eventSubscription.isSubscribed() ? SUBSCRIBED_MESSAGE_PREFIX : SUBSCRIBE_MESSAGE_PREFIX) + Container.JSON.writeValueAsString(eventSubscription);
}
@Converter
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.java
new file mode 100644
index 000000000..589f7a07d
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientResourceImpl.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2020, 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.gateway;
+
+import org.openremote.container.timer.TimerService;
+import org.openremote.manager.security.ManagerIdentityService;
+import org.openremote.manager.web.ManagerWebResource;
+import org.openremote.model.gateway.GatewayConnection;
+import org.openremote.model.gateway.GatewayClientResource;
+import org.openremote.model.http.RequestParams;
+
+import javax.ws.rs.WebApplicationException;
+import java.util.Collections;
+import java.util.List;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.FORBIDDEN;
+
+public class GatewayClientResourceImpl extends ManagerWebResource implements GatewayClientResource {
+
+ protected GatewayClientService gatewayClientService;
+
+ public GatewayClientResourceImpl(TimerService timerService,
+ ManagerIdentityService identityService,
+ GatewayClientService gatewayClientService) {
+ super(timerService, identityService);
+ this.gatewayClientService = gatewayClientService;
+ }
+
+ @Override
+ public GatewayConnection getConnection(RequestParams requestParams, String realm) {
+ if (!realm.equals(getAuthenticatedRealm()) && !isSuperUser()) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ try {
+ return gatewayClientService.getConnections().stream().filter(c -> realm.equals(c.getLocalRealm())).findFirst().orElse(null);
+ } catch (Exception e) {
+ throw new WebApplicationException(e, BAD_REQUEST);
+ }
+ }
+
+ @Override
+ public List<GatewayConnection> getConnections(RequestParams requestParams) {
+ if (!isSuperUser()) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ try {
+ return gatewayClientService.getConnections();
+ } catch (Exception e) {
+ throw new WebApplicationException(e, BAD_REQUEST);
+ }
+ }
+
+ @Override
+ public void setConnection(RequestParams requestParams, String realm, GatewayConnection connection) {
+ connection.setLocalRealm(realm);
+
+ if (!connection.getLocalRealm().equals(getAuthenticatedRealm()) && !isSuperUser()) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ try {
+ gatewayClientService.setConnection(connection);
+ } catch (Exception e) {
+ throw new WebApplicationException(e, BAD_REQUEST);
+ }
+ }
+
+ @Override
+ public void deleteConnection(RequestParams requestParams, String realm) {
+ deleteConnections(requestParams, Collections.singletonList(realm));
+ }
+
+ @Override
+ public void deleteConnections(RequestParams requestParams, List<String> realms) {
+ if (realms.isEmpty()) {
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+
+ if ((realms.size() > 1 || !getAuthenticatedRealm().equals(realms.get(0))) && !isSuperUser()) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
+
+ try {
+ boolean deleted = gatewayClientService.deleteConnections(realms);
+ if (!deleted) {
+ throw new WebApplicationException(BAD_REQUEST);
+ }
+ } catch (Exception e) {
+ throw new WebApplicationException(e, BAD_REQUEST);
+ }
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java
new file mode 100644
index 000000000..4c3abd3c1
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java
@@ -0,0 +1,362 @@
+/*
+ * Copyright 2020, 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.gateway;
+
+import io.netty.channel.ChannelHandler;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.http.client.utils.URIBuilder;
+import org.openremote.agent.protocol.http.OAuthClientCredentialsGrant;
+import org.openremote.agent.protocol.io.AbstractNettyIoClient;
+import org.openremote.agent.protocol.websocket.WebsocketIoClient;
+import org.openremote.container.Container;
+import org.openremote.container.ContainerService;
+import org.openremote.container.message.MessageBrokerSetupService;
+import org.openremote.container.persistence.PersistenceEvent;
+import org.openremote.container.persistence.PersistenceService;
+import org.openremote.container.timer.TimerService;
+import org.openremote.manager.asset.AssetProcessingService;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.concurrent.ManagerExecutorService;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.security.ManagerIdentityService;
+import org.openremote.manager.web.ManagerWebService;
+import org.openremote.model.asset.*;
+import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.event.shared.EventSubscription;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.gateway.GatewayConnection;
+import org.openremote.model.gateway.GatewayDisconnectEvent;
+import org.openremote.model.query.AssetQuery;
+import org.openremote.model.query.filter.TenantPredicate;
+
+import java.util.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;
+import static org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType;
+
+/**
+ * Handles outbound connections to central managers
+ */
+public class GatewayClientService extends RouteBuilder implements ContainerService {
+
+ private static final Logger LOG = Logger.getLogger(GatewayClientService.class.getName());
+ public static final String CLIENT_EVENT_SESSION_PREFIX = GatewayClientService.class.getSimpleName() + ":";
+ protected AssetStorageService assetStorageService;
+ protected AssetProcessingService assetProcessingService;
+ protected PersistenceService persistenceService;
+ protected ClientEventService clientEventService;
+ protected TimerService timerService;
+ protected ManagerExecutorService executorService;
+ protected ManagerIdentityService identityService;
+ protected final Map<String, GatewayConnection> connectionRealmMap = new HashMap<>();
+ protected final Map<String, WebsocketIoClient<String>> clientRealmMap = new HashMap<>();
+
+ @Override
+ public int getPriority() {
+ return DEFAULT_PRIORITY;
+ }
+
+ @Override
+ public void init(Container container) throws Exception {
+ assetStorageService = container.getService(AssetStorageService.class);
+ assetProcessingService = container.getService(AssetProcessingService.class);
+ persistenceService = container.getService(PersistenceService.class);
+ clientEventService = container.getService(ClientEventService.class);
+ timerService = container.getService(TimerService.class);
+ executorService = container.getService(ManagerExecutorService.class);
+ identityService = container.getService(ManagerIdentityService.class);
+
+ container.getService(ManagerWebService.class).getApiSingletons().add(
+ new GatewayClientResourceImpl(timerService, identityService, this)
+ );
+
+ container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);
+ }
+
+ @Override
+ public void start(Container container) throws Exception {
+
+ // Get existing connections
+ connectionRealmMap.putAll(persistenceService.doReturningTransaction(entityManager ->
+ entityManager
+ .createQuery("select gc from GatewayConnection gc", GatewayConnection.class)
+ .getResultList()).stream().collect(Collectors.toMap(GatewayConnection::getLocalRealm, gc -> gc)));
+
+ // Create clients for enabled connections
+ connectionRealmMap.forEach((realm, connection) -> {
+ if (!connection.isDisabled()) {
+ clientRealmMap.put(realm, createGatewayClient(connection));
+ }
+ });
+ }
+
+ @Override
+ public void stop(Container container) throws Exception {
+ clientRealmMap.forEach((realm, client) -> {
+ if (client != null) {
+ destroyGatewayClient(connectionRealmMap.get(realm), client);
+ }
+ });
+ clientRealmMap.clear();
+ connectionRealmMap.clear();
+ }
+
+ @Override
+ public void configure() throws Exception {
+
+ from(PERSISTENCE_TOPIC)
+ .routeId("GatewayServiceConnectionChanges")
+ .filter(isPersistenceEventForEntityType(GatewayConnection.class))
+ .process(exchange -> {
+ @SuppressWarnings("unchecked")
+ PersistenceEvent<GatewayConnection> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
+ GatewayConnection connection = persistenceEvent.getEntity();
+ processConnectionChange(connection, persistenceEvent.getCause());
+ });
+ }
+
+ synchronized protected void processConnectionChange(GatewayConnection connection, PersistenceEvent.Cause cause) {
+
+ LOG.info("Modified gateway client connection '" + cause + "': " + connection);
+
+ synchronized (clientRealmMap) {
+ switch (cause) {
+
+ case UPDATE:
+ WebsocketIoClient<String> client = clientRealmMap.remove(connection.getLocalRealm());
+ if (client != null) {
+ destroyGatewayClient(connection, client);
+ }
+ case CREATE:
+ connectionRealmMap.put(connection.getLocalRealm(), connection);
+ clientRealmMap.put(connection.getLocalRealm(), createGatewayClient(connection));
+ break;
+ case DELETE:
+ connectionRealmMap.remove(connection.getLocalRealm());
+ client = clientRealmMap.remove(connection.getLocalRealm());
+ if (client != null) {
+ destroyGatewayClient(connection, client);
+ }
+ break;
+ }
+ }
+ }
+
+ protected WebsocketIoClient<String> createGatewayClient(GatewayConnection connection) {
+
+ if (connection.isDisabled()) {
+ LOG.info("Disabled gateway client connection so ignoring: " + connection);
+ return null;
+ }
+
+ LOG.info("Creating gateway IO client: " + connection);
+
+ try {
+ WebsocketIoClient<String> client = new WebsocketIoClient<>(
+ new URIBuilder()
+ .setScheme(connection.isSecured() ? "wss" : "ws")
+ .setHost(connection.getHost())
+ .setPort(connection.getPort() == null ? -1 : connection.getPort())
+ .setPath("websocket/events")
+ .setParameter("Auth-Realm", connection.getRealm()).build(),
+ null,
+ new OAuthClientCredentialsGrant(
+ new URIBuilder()
+ .setScheme(connection.isSecured() ? "https" : "http")
+ .setHost(connection.getHost())
+ .setPort(connection.getPort() == null ? -1 : connection.getPort())
+ .setPath("auth/realms/" + connection.getRealm() + "/protocol/openid-connect/token")
+ .build().toString(),
+ connection.getClientId(),
+ connection.getClientSecret(),
+ null).setBasicAuthHeader(true),
+ executorService);
+
+ client.setEncoderDecoderProvider(() ->
+ new ChannelHandler[] {new AbstractNettyIoClient.MessageToMessageDecoder<>(String.class, client)}
+ );
+
+ client.addConnectionStatusConsumer(
+ connectionStatus -> onGatewayClientConnectionStatusChanged(connection, connectionStatus)
+ );
+
+ client.addMessageConsumer(message -> onCentralManagerMessage(connection, message));
+
+ // Subscribe to Asset and attribute events of local realm and pass through to connected manager
+ clientEventService.getEventSubscriptions().createOrUpdate(
+ getClientSessionKey(connection),
+ false,
+ new EventSubscription<>(
+ AssetEvent.class,
+ new AssetFilter<AssetEvent>().setRealm(connection.getLocalRealm()),
+ triggeredEventSubscription ->
+ Arrays.stream(triggeredEventSubscription.getEvents())
+ .forEach(event ->
+ sendCentralManagerMessage(connection.getLocalRealm(), messageFromSharedEvent(event)))));
+
+ clientEventService.getEventSubscriptions().createOrUpdate(
+ getClientSessionKey(connection),
+ false,
+ new EventSubscription<>(
+ AttributeEvent.class,
+ new AssetFilter<AttributeEvent>().setRealm(connection.getLocalRealm()),
+ triggeredEventSubscription ->
+ Arrays.stream(triggeredEventSubscription.getEvents())
+ .forEach(event ->
+ sendCentralManagerMessage(connection.getLocalRealm(), messageFromSharedEvent(event)))));
+
+ client.connect();
+ return client;
+
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Creating gateway IO client failed so marking connection as disabled: " + connection, e);
+ connection.setDisabled(true);
+ setConnection(connection);
+ }
+
+ return null;
+ }
+
+ protected void destroyGatewayClient(GatewayConnection connection, WebsocketIoClient<String> client) {
+ LOG.info("Destroying gateway IO client: " + connection);
+ try {
+ client.disconnect();
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "An exception occurred whilst trying to disconnect the gateway IO client", e);
+ }
+ clientEventService.getEventSubscriptions().cancelAll(getClientSessionKey(connection));
+ }
+
+ protected void onGatewayClientConnectionStatusChanged(GatewayConnection connection, ConnectionStatus connectionStatus) {
+ LOG.info("Connection status change for gateway IO client '" + connectionStatus + "': " + connection);
+ }
+
+ protected void onCentralManagerMessage(GatewayConnection connection, String message) {
+ if (message.startsWith(SharedEvent.MESSAGE_PREFIX)) {
+ SharedEvent event = messageToSharedEvent(message);
+
+ if (event instanceof AttributeEvent) {
+ assetProcessingService.sendAttributeEvent((AttributeEvent)event, AttributeEvent.Source.INTERNAL);
+ } else if (event instanceof AssetEvent) {
+ AssetEvent assetEvent = (AssetEvent)event;
+ if (assetEvent.getCause() == AssetEvent.Cause.CREATE || assetEvent.getCause() == AssetEvent.Cause.UPDATE) {
+ Asset asset = assetEvent.getAsset();
+ asset.setRealm(connection.getLocalRealm());
+ LOG.fine("Request from central manager to create/update an asset: Realm=" + connection.getLocalRealm() + ", Asset ID=" + asset.getId());
+ try {
+ asset = assetStorageService.merge(asset, true);
+ } catch (Exception e) {
+ LOG.log(Level.INFO, "Request from central manager to create/update an asset failed: Realm=" + connection.getLocalRealm() + ", Asset ID=" + asset.getId(), e);
+ }
+ }
+ } else if (event instanceof DeleteAssetsRequestEvent) {
+ DeleteAssetsRequestEvent deleteRequest = (DeleteAssetsRequestEvent)event;
+ LOG.fine("Request from central manager to delete asset(s): Realm=" + connection.getLocalRealm() + ", Asset IDs=" + Arrays.toString(deleteRequest.getAssetIds().toArray()));
+ boolean success = false;
+ try {
+ success = assetStorageService.delete(deleteRequest.getAssetIds());
+ } catch (Exception e) {
+ LOG.log(Level.INFO, "Request from central manager to create/update an asset failed: Realm=" + connection.getLocalRealm() + ", Asset IDs=" + Arrays.toString(deleteRequest.getAssetIds().toArray()), e);
+ } finally {
+ sendCentralManagerMessage(connection.getLocalRealm(), messageFromSharedEvent(new DeleteAssetsResponseEvent(deleteRequest.getName(), success)));
+ }
+ } else if (event instanceof ReadAssetsEvent) {
+ ReadAssetsEvent readAssets = (ReadAssetsEvent)event;
+ AssetQuery query = readAssets.getAssetQuery();
+ // Force realm to be the one that this client is associated with
+ query.tenant(new TenantPredicate(connection.getLocalRealm()));
+ List<Asset> assets = assetStorageService.findAll(readAssets.getAssetQuery());
+ sendCentralManagerMessage(connection.getLocalRealm(), messageFromSharedEvent(new AssetsEvent(readAssets.getName(), assets)));
+ }
+ }
+ }
+
+ protected void sendCentralManagerMessage(String realm, String message) {
+ WebsocketIoClient<String> client;
+
+ synchronized (clientRealmMap) {
+ client = clientRealmMap.get(realm);
+ }
+
+ if (client != null) {
+ client.sendMessage(message);
+ }
+ }
+
+ protected String getClientSessionKey(GatewayConnection connection) {
+ return CLIENT_EVENT_SESSION_PREFIX + connection.getLocalRealm();
+ }
+
+ protected SharedEvent messageToSharedEvent(String message) {
+ try {
+ message = message.substring(SharedEvent.MESSAGE_PREFIX.length());
+ return Container.JSON.readValue(message, SharedEvent.class);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unable to parse SharedEvent message");
+ }
+ }
+
+ protected String messageFromSharedEvent(SharedEvent event) {
+ try {
+ String message = Container.JSON.writeValueAsString(event);
+ return SharedEvent.MESSAGE_PREFIX + message;
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Unable to parse SharedEvent message");
+ }
+ }
+
+ /** GATEWAY RESOURCE METHODS */
+ protected List<GatewayConnection> getConnections() {
+ return new ArrayList<>(connectionRealmMap.values());
+ }
+
+ protected void setConnection(GatewayConnection connection) {
+ LOG.info("Updating/creating gateway connection: " + connection);
+ persistenceService.doTransaction(em -> em.merge(connection));
+ }
+
+ protected boolean deleteConnections(List<String> realms) {
+ LOG.info("Deleting gateway connections for the following realm(s): " + Arrays.toString(realms.toArray()));
+
+ try {
+ persistenceService.doTransaction(em -> {
+
+ List<GatewayConnection> connections = em
+ .createQuery("select gc from GatewayConnection gc where gc.localRealm in :realms", GatewayConnection.class)
+ .setParameter("realms", realms)
+ .getResultList();
+
+ if (connections.size() != realms.size()) {
+ throw new IllegalArgumentException("Cannot delete one or more requested gateway connections as they don't exist");
+ }
+
+ connections.forEach(em::remove);
+ });
+ } catch (Exception e) {
+ return false;
+ }
+
+ return true;
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
new file mode 100644
index 000000000..e768449ed
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
@@ -0,0 +1,610 @@
+/*
+ * Copyright 2020, 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.gateway;
+
+import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.manager.asset.AssetProcessingService;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.concurrent.ManagerExecutorService;
+import org.openremote.model.asset.*;
+import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.event.shared.EventSubscription;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.query.AssetQuery;
+import org.openremote.model.value.Values;
+
+import java.util.*;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Consumer;
+import java.util.function.ToIntFunction;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import static org.openremote.model.query.AssetQuery.Select.selectExcludeAll;
+
+/**
+ * Handles all communication between a gateway representation in the local manager and the actual gateway
+ */
+public class GatewayConnector {
+
+ private static final Logger LOG = Logger.getLogger(GatewayConnector.class.getName());
+ public static long SYNC_TIMEOUT_MILLIS = 10000; // How long to wait for a response before resending request
+ public static long ASSET_CRUD_TIMEOUT_MILLIS = 10000; // How long to wait for a response when merging an asset before throwing an exception
+ public static int MAX_SYNC_RETRIES = 5;
+ public static int SYNC_ASSET_BATCH_SIZE = 20;
+ public static final String ASSET_READ_EVENT_NAME_INITIAL = "INITIAL";
+ public static final String ASSET_READ_EVENT_NAME_BATCH = "BATCH";
+ protected final String realm;
+ protected final String gatewayId;
+ protected final AssetStorageService assetStorageService;
+ protected final ManagerExecutorService executorService;
+ protected final AssetProcessingService assetProcessingService;
+ protected final Map<String, Asset> pendingAssetMerges = new HashMap<>();
+ protected final AtomicReference<DeleteAssetsRequestEvent> pendingAssetDelete = new AtomicReference<>();
+ protected List<AssetEvent> cachedAssetEvents;
+ protected List<AttributeEvent> cachedAttributeEvents;
+ protected Consumer<Object> gatewayMessageConsumer;
+ protected Runnable disconnectRunnable;
+ protected boolean disabled;
+ protected boolean initialSyncInProgress;
+ protected ScheduledFuture<?> syncProcessorFuture;
+ Set<String> syncAssetIds;
+ int syncIndex;
+ int syncErrors;
+ String expectedSyncResponseName;
+
+ public GatewayConnector(
+ AssetStorageService assetStorageService,
+ AssetProcessingService assetProcessingService,
+ ManagerExecutorService executorService,
+ Asset gateway) {
+
+ this.assetStorageService = assetStorageService;
+ this.assetProcessingService = assetProcessingService;
+ this.executorService = executorService;
+ boolean disabled = gateway.getAttribute("disabled").flatMap(AssetAttribute::getValueAsBoolean).orElse(false);
+ this.realm = gateway.getRealm();
+ this.gatewayId = gateway.getId();
+ this.disabled = disabled;
+ }
+
+ public void sendMessageToGateway(Object message) {
+ try {
+ if (gatewayMessageConsumer != null) {
+ gatewayMessageConsumer.accept(message);
+ }
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to send message to gateway: " + gatewayId, e);
+ }
+ }
+
+ /**
+ * Start the connector and initiate synchronisation of assets
+ */
+ synchronized public void connect(Consumer<Object> gatewayMessageConsumer, Runnable disconnectRunnable) {
+ if (this.gatewayMessageConsumer != null) {
+ return;
+ }
+ this.gatewayMessageConsumer = gatewayMessageConsumer;
+ this.disconnectRunnable = disconnectRunnable;
+ initialSyncInProgress = true;
+
+ LOG.info("Gateway connector starting: Gateway ID=" + gatewayId);
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, "status", Values.create(ConnectionStatus.CONNECTING.name())), AttributeEvent.Source.GATEWAY);
+
+ // Reinitialise state
+ syncProcessorFuture = null;
+ cachedAssetEvents = new ArrayList<>();
+ cachedAttributeEvents = new ArrayList<>();
+ syncAssetIds = null;
+ syncIndex = 0;
+ syncErrors = 0;
+
+ startSync();
+ }
+
+ /**
+ * Stop the connector and prevent further communication with the gateway
+ */
+ synchronized public void disconnect() {
+ if (this.gatewayMessageConsumer == null) {
+ return;
+ }
+
+ LOG.info("Gateway connector disconnected: Gateway ID=" + gatewayId);
+
+ this.gatewayMessageConsumer = null;
+ Runnable disconnectRunnable = this.disconnectRunnable;
+ this.disconnectRunnable = null;
+ initialSyncInProgress = false;
+ pendingAssetMerges.clear();
+ pendingAssetDelete.set(null);
+
+ if (syncProcessorFuture != null) {
+ syncProcessorFuture.cancel(true);
+ }
+
+ disconnectRunnable.run();
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, "status", Values.create(ConnectionStatus.DISCONNECTED.name())), AttributeEvent.Source.GATEWAY);
+ }
+
+ public boolean isConnected() {
+ return gatewayMessageConsumer != null;
+ }
+
+ public boolean isInitialSyncInProgress() {
+ return initialSyncInProgress;
+ }
+
+ public String getGatewayId() {
+ return gatewayId;
+ }
+
+ public String getRealm() {
+ return realm;
+ }
+
+ public boolean isDisabled() {
+ return disabled;
+ }
+
+ public void setDisabled(boolean disabled) {
+ if (this.disabled == disabled) {
+ return;
+ }
+
+ this.disabled = disabled;
+
+ if (disabled) {
+ if (isConnected()) {
+ disconnect();
+ }
+ LOG.info("Gateway connector disabled: Gateway ID=" + gatewayId);
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, "status", Values.create(ConnectionStatus.DISABLED.name())), AttributeEvent.Source.GATEWAY);
+ } else {
+ LOG.info("Gateway connector enabled: Gateway ID=" + gatewayId);
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, "status", Values.create(ConnectionStatus.DISCONNECTED.name())), AttributeEvent.Source.GATEWAY);
+ }
+ }
+
+ synchronized protected void onGatewayEvent(SharedEvent e) {
+ if (!isConnected()) {
+ return;
+ }
+
+ if (initialSyncInProgress) {
+ if (e instanceof AssetsEvent) {
+ onSyncAssetsResponse((AssetsEvent) e);
+ } else if (e instanceof AttributeEvent) {
+ cachedAttributeEvents.add((AttributeEvent) e);
+ } else if (e instanceof AssetEvent) {
+ cachedAssetEvents.add((AssetEvent) e);
+ }
+ } else {
+ if (e instanceof AssetEvent) {
+ onAssetEvent((AssetEvent) e);
+ } else if (e instanceof AttributeEvent) {
+ onAttributeEvent((AttributeEvent) e);
+ } else if (e instanceof DeleteAssetsResponseEvent) {
+ onAssetDeleteResponseEvent((DeleteAssetsResponseEvent) e);
+ }
+ }
+ }
+
+ /**
+ * Get list of gateway assets (get basic details and then batch load them to minimise load)
+ */
+ synchronized protected void startSync() {
+
+ if (syncAborted()) {
+ return;
+ }
+
+ expectedSyncResponseName = ASSET_READ_EVENT_NAME_INITIAL;
+ sendMessageToGateway(new ReadAssetsEvent(
+ ASSET_READ_EVENT_NAME_INITIAL, new AssetQuery().select(selectExcludeAll()).recursive(true)
+ ));
+ syncProcessorFuture = executorService.schedule(this::onSyncAssetsTimeout, SYNC_TIMEOUT_MILLIS);
+ }
+
+ /**
+ * Called if a response isn't received from the gateway within {@link #SYNC_TIMEOUT_MILLIS}
+ */
+ synchronized protected void onSyncAssetsTimeout() {
+ if (!isConnected()) {
+ return;
+ }
+
+ LOG.info("Gateway sync timeout occurred: Gateway ID=" + gatewayId);
+ syncErrors++;
+
+ if (syncAborted()) {
+ return;
+ }
+
+ if (syncAssetIds == null) {
+ // Haven't received initial list of assets so retry
+ startSync();
+ } else {
+ requestAssets();
+ }
+ }
+
+ protected boolean syncAborted() {
+ if (syncErrors == MAX_SYNC_RETRIES) {
+ LOG.warning("Gateway sync max retries reached so disabling the gateway: Gateway ID=" + gatewayId);
+ setDisabled(true);
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Request assets in batches of {@link #SYNC_ASSET_BATCH_SIZE} to avoid overloading the gateway
+ */
+ protected void requestAssets() {
+
+ if (syncAborted()) {
+ return;
+ }
+
+ String[] requestAssetIds = syncAssetIds.stream().skip(syncIndex).limit(SYNC_ASSET_BATCH_SIZE).toArray(String[]::new);
+ expectedSyncResponseName = ASSET_READ_EVENT_NAME_BATCH + syncIndex;
+
+ LOG.fine("Synchronising gateway assets " + syncIndex+1 + "-" + syncIndex + requestAssetIds.length + " of " + syncAssetIds.size());
+
+ sendMessageToGateway(
+ new ReadAssetsEvent(
+ expectedSyncResponseName,
+ new AssetQuery()
+ .select(new AssetQuery.Select().excludeParentInfo(true).excludePath(true))
+ .ids(requestAssetIds)
+ )
+ );
+ syncProcessorFuture = executorService.schedule(this::requestAssets, SYNC_TIMEOUT_MILLIS);
+ }
+
+ synchronized protected void onSyncAssetsResponse(AssetsEvent e) {
+ if (!isConnected()) {
+ return;
+ }
+
+ if (!expectedSyncResponseName.equalsIgnoreCase(e.getName())) {
+ LOG.info("Unexpected response from gateway so ignoring (expected=" + expectedSyncResponseName + ", actual =" + e.getName() + "): " + e);
+ return;
+ }
+
+ syncProcessorFuture.cancel(true);
+ syncProcessorFuture = null;
+ boolean isInitialResponse = ASSET_READ_EVENT_NAME_INITIAL.equalsIgnoreCase(e.getName());
+
+ if (isInitialResponse) {
+
+ Map<String, String> gatewayAssetIdParentIdMap = e.getAssets().stream()
+ .collect(HashMap::new, (m,v)->m.put(v.getId(), v.getParentId()), HashMap::putAll);
+
+ ToIntFunction<Asset> assetLevelExtractor = asset -> {
+ int level = 0;
+ String parentId = asset.getParentId();
+ while (parentId != null) {
+ level++;
+ parentId = gatewayAssetIdParentIdMap.get(parentId);
+ }
+ return level;
+ };
+
+ syncAssetIds = e.getAssets()
+ .stream()
+ .sorted(Comparator.comparingInt(assetLevelExtractor))
+ .map(Asset::getId)
+ .collect(Collectors.toCollection(LinkedHashSet::new));
+
+ if (syncAssetIds.isEmpty()) {
+ deleteObsoleteLocalAssets();
+ onInitialSyncComplete();
+ return;
+ }
+
+ requestAssets();
+
+ } else {
+
+ List<String> requestedAssetIds = syncAssetIds.stream().skip(syncIndex).limit(SYNC_ASSET_BATCH_SIZE).collect(Collectors.toList());
+ List<Asset> returnedAssets = e.getAssets();
+
+ // Remove any assets that have been deleted since requested
+ cachedAssetEvents.removeIf(
+ assetEvent -> {
+ boolean remove = requestedAssetIds.stream().anyMatch(id -> id.equals(assetEvent.getEntityId()) && assetEvent.getCause() == AssetEvent.Cause.DELETE);
+ if (remove) {
+ syncAssetIds.remove(assetEvent.getEntityId());
+ requestedAssetIds.remove(assetEvent.getEntityId());
+ }
+ return remove;
+ });
+
+ if (returnedAssets.size() != requestedAssetIds.size() || !returnedAssets.stream().allMatch(asset -> requestedAssetIds.contains(asset.getId()))) {
+ LOG.warning("Retrieved gateway asset batch count or ID mismatch, attempting to re-send the request");
+ syncErrors++;
+ requestAssets();
+ return;
+ }
+
+ // Merge returned assets ensuring the latest version of each is merged
+ returnedAssets.stream()
+ .map(returnedAsset -> {
+ final AtomicReference<Asset> latestAssetVersion = new AtomicReference<>(returnedAsset);
+ cachedAssetEvents.removeIf(
+ assetEvent -> {
+ boolean remove = assetEvent.getEntityId().equals(returnedAsset.getId()) && (assetEvent.getCause() == AssetEvent.Cause.UPDATE || assetEvent.getCause() == AssetEvent.Cause.READ);
+ if (remove && assetEvent.getAsset().getVersion() > latestAssetVersion.get().getVersion()) {
+ latestAssetVersion.set(assetEvent.getAsset());
+ }
+ return remove;
+ });
+ return latestAssetVersion.get();
+ }).forEach(this::saveAssetLocally);
+
+
+ // Request next batch or move on
+ syncIndex += requestedAssetIds.size();
+ if (syncIndex >= syncAssetIds.size()) {
+ LOG.info("All requested gateway assets retrieved");
+
+ Set<String> refreshAssets = new HashSet<>();
+
+ cachedAssetEvents.forEach(
+ assetEvent -> {
+ if (assetEvent.getCause() == AssetEvent.Cause.DELETE) {
+ syncAssetIds.remove(assetEvent.getEntityId());
+ } else if (assetEvent.getCause() == AssetEvent.Cause.CREATE) {
+ syncAssetIds.add(assetEvent.getEntityId());
+ try {
+ saveAssetLocally(assetEvent.getAsset());
+ } catch (Exception ex) {
+ LOG.log(Level.SEVERE, "Failed to add new gateway asset (Gateway ID=" + gatewayId + ", Asset=" + assetEvent.getAsset(), ex);
+ }
+ } else {
+ refreshAssets.add(assetEvent.getEntityId());
+ }
+ }
+ );
+
+ deleteObsoleteLocalAssets();
+ onInitialSyncComplete();
+
+ // Refresh attributes that have changed
+ cachedAttributeEvents.stream().filter(attributeEvent -> syncAssetIds.contains(attributeEvent.getEntityId())).collect(Collectors.groupingBy(AttributeEvent::getEntityId)).forEach(
+ (assetId, attributeEvents) -> {
+ LOG.info("1 or more gateway asset attribute values have changed so requesting latest values (Gateway ID=" + gatewayId + ", Asset ID=" + assetId);
+ sendMessageToGateway(new ReadAssetAttributesEvent(assetId, attributeEvents.stream().map(AttributeEvent::getAttributeName).toArray(String[]::new)));
+ }
+ );
+
+ // Refresh assets that have changed
+ refreshAssets.forEach(id -> sendMessageToGateway(new ReadAssetEvent(id)));
+ } else {
+ requestAssets();
+ }
+ }
+ }
+
+ protected void deleteObsoleteLocalAssets() {
+
+ // Find obsolete local assets
+ List<Asset> localAssets = assetStorageService.findAll(
+ new AssetQuery()
+ .select(selectExcludeAll())
+ .recursive(true)
+ .parents(gatewayId)
+ );
+
+ // Delete obsolete assets
+ List<String> obsoleteLocalAssetIds = localAssets.stream()
+ .filter(localAsset -> !syncAssetIds.contains(localAsset.getId()))
+ .map(Asset::getId).collect(Collectors.toList());
+
+ if (!obsoleteLocalAssetIds.isEmpty()) {
+ boolean deleted = deleteAssetsLocally(obsoleteLocalAssetIds);
+ if (!deleted) {
+ LOG.warning("Failed to delete obsolete local gateway assets; assets are not correctly synced");
+ }
+ }
+ }
+
+ protected void onInitialSyncComplete() {
+ initialSyncInProgress = false;
+ cachedAssetEvents.clear();
+ cachedAttributeEvents.clear();
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, "status", Values.create(ConnectionStatus.CONNECTED.name())), AttributeEvent.Source.GATEWAY);
+ }
+
+ protected Asset mergeGatewayAsset(Asset asset, boolean isUpdate) {
+
+ if (!isConnected() || isInitialSyncInProgress()) {
+ String msg = "Gateway is not connected or initial sync in progress so cannot merge asset: Gateway ID=" + gatewayId + ", Asset ID=" + asset.getId();
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ final String id = asset.getId();
+ final String parentId = asset.getParentId();
+
+ synchronized (pendingAssetMerges) {
+
+ if (id != null && pendingAssetMerges.containsKey(id)) {
+ String msg = "Gateway asset merge already pending for this asset: Gateway ID=" + gatewayId + ", Asset ID=" + id;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+
+ if (id == null) {
+ // Generate an ID to allow tracking the asset when it is returned from the gateway
+ asset.setId(UniqueIdentifierGenerator.generateId());
+ }
+
+ if (gatewayId.equals(parentId)) {
+ asset.setParentId(null);
+ }
+
+ sendMessageToGateway(new AssetEvent(isUpdate ? AssetEvent.Cause.UPDATE : AssetEvent.Cause.CREATE, asset, null));
+ pendingAssetMerges.put(asset.getId(), null);
+ }
+
+ synchronized (asset.getId()) {
+ try {
+ asset.getId().wait(ASSET_CRUD_TIMEOUT_MILLIS);
+
+ Asset mergedAsset;
+ synchronized (pendingAssetMerges) {
+ mergedAsset = pendingAssetMerges.remove(asset.getId());
+ }
+
+ if (mergedAsset == null) {
+ throw new IllegalStateException("Gateway asset merge failed: Gateway ID=" + gatewayId + ", Asset ID=" + asset.getId());
+ }
+
+ return mergedAsset;
+
+ } catch (InterruptedException e) {
+ String msg = "Gateway asset merge interrupted: Gateway ID=" + gatewayId + ", Asset ID=" + id;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ } finally {
+ synchronized (pendingAssetMerges) {
+ pendingAssetMerges.remove(asset.getId());
+ }
+ asset.setId(id);
+ asset.setParentId(parentId);
+ }
+ }
+ }
+
+ protected boolean deleteGatewayAssets(List<String> assetIds) {
+
+ if (!isConnected() || isInitialSyncInProgress()) {
+ String msg = "Gateway is not connected or initial sync in progress so cannot delete asset(s): Gateway ID=" + gatewayId + ", Asset IDs=" + Arrays.toString(assetIds.toArray());
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ synchronized (pendingAssetDelete) {
+
+ if (pendingAssetDelete.get() != null) {
+ String msg = "Gateway asset delete already pending: Gateway ID=" + gatewayId + ", Asset IDs=" + Arrays.toString(assetIds.toArray());
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ pendingAssetDelete.set(new DeleteAssetsRequestEvent(UniqueIdentifierGenerator.generateId(), new ArrayList<>(assetIds)));
+
+ try {
+ sendMessageToGateway(pendingAssetDelete.get());
+ pendingAssetDelete.wait(ASSET_CRUD_TIMEOUT_MILLIS);
+ if (pendingAssetDelete.get() != null) {
+ throw new IllegalStateException("Gateway asset delete failed: Gateway ID=" + gatewayId + ", Asset IDs=" + Arrays.toString(assetIds.toArray()));
+ }
+ return true;
+ } catch (InterruptedException e) {
+ String msg = "Gateway asset delete interrupted: Gateway ID=" + gatewayId + ", Asset IDs=" + Arrays.toString(assetIds.toArray());
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ } finally {
+ pendingAssetDelete.set(null);
+ }
+ }
+ }
+
+ protected void onAssetDeleteResponseEvent(DeleteAssetsResponseEvent e) {
+
+ synchronized (pendingAssetDelete) {
+ if (pendingAssetDelete.get() == null) {
+ return;
+ }
+
+ if (!pendingAssetDelete.get().getName().equals(e.getName())) {
+ LOG.info("Gateway asset delete response name does not match request so ignoring");
+ return;
+ }
+
+ if (e.isDeleted()) {
+ pendingAssetDelete.set(null);
+ }
+
+ pendingAssetDelete.notify();
+ }
+ }
+
+ synchronized protected void onAssetEvent(AssetEvent e) {
+
+ switch (e.getCause()) {
+
+ case CREATE:
+ case READ:
+ case UPDATE:
+ Asset mergedAsset = saveAssetLocally(e.getAsset());
+
+ synchronized (pendingAssetMerges) {
+ if (pendingAssetMerges.containsKey(e.getEntityId())) {
+ @SuppressWarnings("OptionalGetWithoutIsPresent")
+ Map.Entry<String, Asset> pendingAssetMergeEntry = pendingAssetMerges.entrySet().stream().filter(entry -> entry.getKey().equals(e.getEntityId())).findFirst().get();
+ String id = pendingAssetMergeEntry.getKey();
+ pendingAssetMergeEntry.setValue(mergedAsset);
+
+ //noinspection SynchronizationOnLocalVariableOrMethodParameter
+ synchronized (id) {
+ // Notify the waiting merge thread
+ id.notify();
+ }
+ }
+ }
+ break;
+ case DELETE:
+ try {
+ deleteAssetsLocally(Collections.singletonList(e.getEntityId()));
+ } catch (Exception ex) {
+ LOG.log(Level.SEVERE, "Removing obsolete asset failed: " + e.getEntityId(), ex);
+ }
+ break;
+ }
+ }
+
+ protected void onAttributeEvent(AttributeEvent e) {
+ // Just push the event through the processing chain
+ assetProcessingService.sendAttributeEvent(e, AttributeEvent.Source.GATEWAY);
+ }
+
+ protected Asset saveAssetLocally(Asset asset) {
+ asset.setParentId(asset.getParentId() != null ? asset.getParentId() : gatewayId);
+ asset.setRealm(realm);
+ LOG.fine("Creating/updating gateway asset: Gateway ID=" + gatewayId + ", Asset ID=" + asset.getId());
+ return assetStorageService.merge(asset, true, true, null);
+ }
+
+ protected boolean deleteAssetsLocally(List<String> assetIds) {
+ LOG.fine("Removing gateway asset: Gateway ID=" + gatewayId + ", Asset IDs=" + Arrays.toString(assetIds.toArray()));
+ return assetStorageService.delete(assetIds, true);
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
new file mode 100644
index 000000000..f953cb6c2
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
@@ -0,0 +1,546 @@
+/*
+ * Copyright 2020, 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.gateway;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.keycloak.admin.client.resource.ClientsResource;
+import org.keycloak.representations.idm.ClientRepresentation;
+import org.openremote.container.Container;
+import org.openremote.container.ContainerService;
+import org.openremote.container.message.MessageBrokerSetupService;
+import org.openremote.container.persistence.PersistenceEvent;
+import org.openremote.container.web.ClientRequestInfo;
+import org.openremote.container.web.socket.WebsocketConstants;
+import org.openremote.manager.asset.AssetProcessingException;
+import org.openremote.manager.asset.AssetProcessingService;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.asset.AssetUpdateProcessor;
+import org.openremote.manager.concurrent.ManagerExecutorService;
+import org.openremote.manager.event.ClientEventService;
+import org.openremote.manager.security.ManagerIdentityService;
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.asset.Asset;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.AssetType;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.gateway.GatewayDisconnectEvent;
+import org.openremote.model.query.AssetQuery;
+import org.openremote.model.util.TextUtil;
+import org.openremote.model.value.Values;
+
+import javax.persistence.EntityManager;
+import javax.websocket.CloseReason;
+import javax.websocket.Session;
+import javax.ws.rs.core.Response;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+
+import static org.apache.camel.builder.PredicateBuilder.or;
+import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;
+import static org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType;
+import static org.openremote.manager.event.ClientEventService.getClientId;
+import static org.openremote.manager.event.ClientEventService.getSessionKey;
+
+public class GatewayService extends RouteBuilder implements ContainerService, AssetUpdateProcessor {
+
+ public static final int GATEWAY_SERVICE_PRIORITY = DEFAULT_PRIORITY - 10;
+ public static final String GATEWAY_CLIENT_ID_PREFIX = "gateway-";
+ private static final Logger LOG = Logger.getLogger(GatewayService.class.getName());
+ protected AssetStorageService assetStorageService;
+ protected AssetProcessingService assetProcessingService;
+ protected ManagerIdentityService identityService;
+ protected ManagerKeycloakIdentityProvider identityProvider;
+ protected ClientEventService clientEventService;
+ protected ManagerExecutorService executorService;
+ protected final Map<String, GatewayConnector> gatewayConnectorMap = new HashMap<>();
+ protected final Map<String, String> assetIdGatewayIdMap = new HashMap<>();
+ protected boolean active;
+
+ public static final String GATEWAY_EVENT_TOPIC = "seda://GatewayEventTopic?multipleConsumers=false&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&limitConcurrentConsumers=false&size=10000";
+
+ public static boolean isGatewayClientId(String clientId) {
+ return clientId != null && clientId.startsWith(GATEWAY_CLIENT_ID_PREFIX);
+ }
+
+ public static String getGatewayIdFromClientId(String clientId) {
+ return clientId.substring(GATEWAY_CLIENT_ID_PREFIX.length());
+ }
+
+ @Override
+ public int getPriority() {
+ return GATEWAY_SERVICE_PRIORITY;
+ }
+
+ @Override
+ public void init(Container container) throws Exception {
+ assetStorageService = container.getService(AssetStorageService.class);
+ assetProcessingService = container.getService(AssetProcessingService.class);
+ identityService = container.getService(ManagerIdentityService.class);
+ clientEventService = container.getService(ClientEventService.class);
+ executorService = container.getService(ManagerExecutorService.class);
+
+ if (!identityService.isKeycloakEnabled()) {
+ LOG.warning("Gateways are not supported when not using Keycloak identity provider");
+ active = false;
+ } else {
+ active = true;
+ identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();
+ container.getService(MessageBrokerSetupService.class).getContext().addRoutes(this);
+ }
+ }
+
+ @Override
+ public void start(Container container) throws Exception {
+ List<Asset> gateways = assetStorageService.findAll(new AssetQuery().types(AssetType.GATEWAY));
+ List<String> gatewayIds = gateways.stream().map(Asset::getId).collect(Collectors.toList());
+ gateways = gateways.stream()
+ .filter(gateway ->
+ Arrays.stream(gateway.getPath()).noneMatch(p -> !p.equals(gateway.getId()) && gatewayIds.contains(p)))
+ .collect(Collectors.toList());
+
+ if (!gateways.isEmpty()) {
+ LOG.info("Directly registered gateways found = " + gateways.size());
+
+ gateways.forEach(gateway -> {
+
+ // Create connector
+ GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);
+ gatewayConnectorMap.put(gateway.getId(), connector);
+
+ // Get IDs of all assets under this gateway
+ List<Asset> gatewayAssets = assetStorageService
+ .findAll(
+ new AssetQuery()
+ .parents(gateway.getId())
+ .select(AssetQuery.Select.selectExcludeAll())
+ .recursive(true));
+
+ gatewayAssets.forEach(asset -> assetIdGatewayIdMap.put(asset.getId(), gateway.getId()));
+ });
+ }
+ }
+
+ @Override
+ public void stop(Container container) throws Exception {
+ // TODO: Stop all connectors
+ gatewayConnectorMap.values().forEach(GatewayConnector::disconnect);
+ gatewayConnectorMap.clear();
+ assetIdGatewayIdMap.clear();
+ }
+
+ @Override
+ public void configure() throws Exception {
+
+ if (active) {
+ from(PERSISTENCE_TOPIC)
+ .routeId("GatewayServiceAssetChanges")
+ .filter(isPersistenceEventForEntityType(Asset.class))
+ .process(exchange -> {
+ @SuppressWarnings("unchecked")
+ PersistenceEvent<Asset> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
+ Asset eventAsset = persistenceEvent.getEntity();
+
+ if (persistenceEvent.getCause() != PersistenceEvent.Cause.DELETE) {
+ eventAsset = assetStorageService.find(eventAsset.getId(), true);
+ }
+
+ // Only gateways locally registered to this manager are of interest or gateway descendant assets
+ if (eventAsset.getWellKnownType() == AssetType.GATEWAY
+ && (isLocallyRegisteredGateway(eventAsset.getId()) || getLocallyRegisteredGatewayId(eventAsset.getId(), eventAsset.getParentId()) == null)) {
+ processGatewayChange(eventAsset, persistenceEvent);
+ } else {
+
+ String gatewayId = getLocallyRegisteredGatewayId(eventAsset.getId(), eventAsset.getParentId());
+
+ if (gatewayId != null) {
+ processGatewayChildAssetChange(gatewayId, eventAsset, persistenceEvent);
+ }
+ }
+ });
+
+ // From gateway clients
+ from(GATEWAY_EVENT_TOPIC)
+ .routeId("FromGatewayUpdates")
+ .choice()
+ .when(header(WebsocketConstants.SESSION_OPEN))
+ .process(exchange -> {
+ Session session = exchange.getIn().getHeader(WebsocketConstants.SESSION, Session.class);
+ String sessionKey = getSessionKey(exchange);
+ String clientId = getClientId(exchange);
+ if (isGatewayClientId(clientId)) {
+ processGatewayConnected(clientId, sessionKey, session);
+ }
+ })
+ .stop()
+ .when(or(
+ header(WebsocketConstants.SESSION_CLOSE),
+ header(WebsocketConstants.SESSION_CLOSE_ERROR)
+ ))
+ .process(exchange -> {
+ String clientId = getClientId(exchange);
+ if (isGatewayClientId(clientId)) {
+ processGatewayDisconnected(clientId);
+ }
+ })
+ .stop()
+ .when(body().isInstanceOf(SharedEvent.class))
+ .process(exchange -> {
+ String gatewayId = getGatewayIdFromClientId(getClientId(exchange));
+ onGatewayEventReceived(gatewayId, exchange.getIn().getBody(SharedEvent.class));
+ })
+ .end();
+ }
+ }
+
+ @Override
+ public boolean processAssetUpdate(EntityManager em, Asset asset, AssetAttribute attribute, AttributeEvent.Source source) throws AssetProcessingException {
+
+ // If the update was generated by a gateway then we don't want to process it here
+ if (source == AttributeEvent.Source.GATEWAY) {
+ return false;
+ }
+
+ GatewayConnector connector = gatewayConnectorMap.get(asset.getId());
+
+ if (connector != null) {
+ LOG.fine("Attribute event for a locally registered gateway asset (Asset ID=" + asset.getId() + "): " + attribute);
+
+ // This is a change to a locally registered gateway
+ if (attribute.getNameOrThrow().equals("disabled")) {
+ boolean disabled = attribute.getValueAsBoolean().orElse(false);
+ connector.setDisabled(disabled);
+ }
+ } else {
+ String gatewayId = assetIdGatewayIdMap.get(asset.getId());
+
+ if (gatewayId != null) {
+ LOG.fine("Attribute event for a gateway descendant asset (Asset ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);
+ connector = gatewayConnectorMap.get(gatewayId);
+ if (connector == null) {
+ LOG.warning("Gateway not found for descendant asset, this should not happen!!! (Asset ID=" + asset.getId() + ", Gateway ID=" + gatewayId + ")");
+ } else {
+ if (!connector.isConnected()) {
+ LOG.info("Gateway is not connected so attribute event for descendant asset will be dropped (Asset ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);
+ throw new AssetProcessingException(AssetProcessingException.Reason.GATEWAY_DISCONNECTED, "Gateway is not connected: Gateway ID=" + connector.gatewayId);
+ }
+ connector.sendMessageToGateway(
+ new AttributeEvent(
+ asset.getId(),
+ attribute.getNameOrThrow(),
+ attribute.getValue().orElse(null),
+ attribute.getValueTimestamp().orElse(0L))
+ .setParentId(asset.getParentId()).setRealm(asset.getRealm()));
+ }
+
+ // Consume this event as it is for a gateway descendant and we've sent it to that gateway for processing
+ return true;
+ }
+ }
+
+ // Don't consume event for non gateway descendant assets
+ return false;
+ }
+
+ public Asset mergeGatewayAsset(String gatewayId, Asset asset) {
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+
+ if (connector == null) {
+ String msg = "Gateway not found: Gateway ID=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ boolean isUpdate = asset.getId() != null && assetIdGatewayIdMap.containsKey(asset.getId());
+ return connector.mergeGatewayAsset(asset, isUpdate);
+ }
+
+ public boolean deleteGateway(String gatewayId) {
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+
+ if (connector == null) {
+ String msg = "Gateway is not known: Gateway ID=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ if (connector.isConnected()) {
+ connector.setDisabled(true);
+ }
+
+ // Get all child gateway assets
+ List<String> gatewayAssetIds = assetIdGatewayIdMap.entrySet().stream()
+ .filter(entry -> entry.getValue().equals(gatewayId))
+ .map(Map.Entry::getKey)
+ .collect(Collectors.toList());
+
+ gatewayAssetIds.add(gatewayId);
+
+ return assetStorageService.delete(gatewayAssetIds, true);
+ }
+
+ public boolean deleteGatewayAssets(String gatewayId, List<String> assetIds) {
+ if (assetIds == null || assetIds.isEmpty()) {
+ return false;
+ }
+
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+
+ if (connector == null || !connector.isConnected()) {
+ String msg = "Gateway is not connected: Gateway ID=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ return connector.deleteGatewayAssets(assetIds);
+ }
+
+ /**
+ * Check if asset ID is a gateway asset registered locally on this manager
+ */
+ public boolean isLocallyRegisteredGateway(String assetId) {
+ return gatewayConnectorMap.containsKey(assetId);
+ }
+
+ /**
+ * Check if the specified asset ID or parent ID is a known gateway or descendant of a gateway.
+ */
+ public String getLocallyRegisteredGatewayId(String assetId, String parentId) {
+ if (!active) {
+ return null;
+ }
+
+ String gatewayId = assetIdGatewayIdMap.get(assetId);
+
+ if (gatewayId != null) {
+ return gatewayId;
+ }
+
+ if (parentId != null) {
+ GatewayConnector connector = gatewayConnectorMap.get(parentId);
+
+ if (connector != null) {
+ return connector.gatewayId;
+ }
+
+ return getLocallyRegisteredGatewayId(parentId, null);
+ }
+
+ return null;
+ }
+
+ protected void processGatewayConnected(String gatewayClientId, String sessionId, Session session) {
+
+ if (!active) {
+ return;
+ }
+
+ String gatewayId = getGatewayIdFromClientId(gatewayClientId);
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+ Runnable disconnectRunnable = createDisconnectRunnable(gatewayId, session);
+
+ if (connector == null) {
+ LOG.warning("Gateway connected but not recognised which shouldn't happen: Gateway ID=" + gatewayId);
+ clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.UNRECOGNISED));
+ disconnectRunnable.run();
+ return;
+ }
+
+ if (connector.isConnected()) {
+ LOG.warning("Gateway already connected so requesting disconnect on new connection: Gateway ID=" + gatewayId);
+ clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.ALREADY_CONNECTED));
+ disconnectRunnable.run();
+ return;
+ }
+
+ if (connector.isDisabled()) {
+ LOG.warning("Gateway is currently disabled so will be ignored: Gateway ID=" + gatewayId);
+ clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
+ disconnectRunnable.run();
+ return;
+ }
+
+ connector.connect(createConnectorMessageConsumer(sessionId), disconnectRunnable);
+ }
+
+ protected void processGatewayDisconnected(String gatewayClientId) {
+
+ if (!active) {
+ return;
+ }
+
+ String gatewayId = getGatewayIdFromClientId(gatewayClientId);
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+
+ if (connector == null) {
+ return;
+ }
+
+ connector.disconnect();
+ }
+
+ protected void processGatewayChange(Asset gateway, PersistenceEvent<Asset> persistenceEvent) {
+
+ switch (persistenceEvent.getCause()) {
+
+ case CREATE:
+ createGatewayClient(gateway);
+ synchronized (gatewayConnectorMap) {
+ GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);
+ gatewayConnectorMap.put(gateway.getId(), connector);
+ }
+ break;
+ case UPDATE:
+ // Check if this gateway has a connector
+ GatewayConnector connector = gatewayConnectorMap.get(gateway.getId());
+ if (connector == null) {
+ break;
+ }
+
+ // Check if disabled
+ boolean isNowDisabled = gateway.getAttribute("disabled").flatMap(AssetAttribute::getValueAsBoolean).orElse(false);
+ connector.setDisabled(isNowDisabled);
+ break;
+ case DELETE:
+ // Check if this gateway has a connector
+ connector = gatewayConnectorMap.get(gateway.getId());
+ if (connector == null) {
+ break;
+ }
+
+ synchronized (gatewayConnectorMap) {
+ connector = gatewayConnectorMap.remove(gateway.getId());
+
+ if (connector != null) {
+ connector.disconnect();
+ }
+ }
+
+ destroyGatewayClient(gateway);
+ break;
+ }
+ }
+
+ protected void processGatewayChildAssetChange(String gatewayId, Asset childAsset, PersistenceEvent<Asset> persistenceEvent) {
+ // The asset would have been modified by the gateway connector so all we need to do here is update the id map
+ switch (persistenceEvent.getCause()) {
+
+ case CREATE:
+ case UPDATE:
+ synchronized (assetIdGatewayIdMap) {
+ assetIdGatewayIdMap.put(childAsset.getId(), gatewayId);
+ }
+ break;
+ case DELETE:
+ synchronized (assetIdGatewayIdMap) {
+ assetIdGatewayIdMap.remove(childAsset.getId());
+ }
+ break;
+ }
+ }
+
+ protected boolean isGatewayConnected(String gatewayId) {
+ AtomicBoolean connected = new AtomicBoolean(false);
+
+ gatewayConnectorMap.computeIfPresent(gatewayId, (id, gatewayConnector) -> {
+ connected.set(gatewayConnector.isConnected());
+ return gatewayConnector;
+ });
+
+ return connected.get();
+ }
+
+ protected void createGatewayClient(Asset gateway) {
+ String id = UUID.randomUUID().toString();
+ String secret = UUID.randomUUID().toString();
+ String clientId = GATEWAY_CLIENT_ID_PREFIX + gateway.getId();
+ ClientRepresentation clientRepresentation = new ClientRepresentation();
+ clientRepresentation.setId(id);
+ clientRepresentation.setStandardFlowEnabled(false);
+ clientRepresentation.setImplicitFlowEnabled(false);
+ clientRepresentation.setDirectAccessGrantsEnabled(false);
+ clientRepresentation.setServiceAccountsEnabled(true);
+ clientRepresentation.setClientAuthenticatorType("client-secret");
+ clientRepresentation.setClientId(clientId);
+ clientRepresentation.setSecret(secret);
+
+ LOG.info("Creating gateway keycloak client for gateway id: " + gateway.getId());
+
+ ClientsResource clientsResource = identityProvider.getRealms(getClientRequestInfo()).realm(gateway.getRealm()).clients();
+ Response response = clientsResource.create(clientRepresentation);
+
+ if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
+ LOG.warning("Failed to create Keycloak client for gateway '" + gateway.getId() + "' Response=" + response.getStatus());
+ } else {
+ gateway.getAttribute("clientId").ifPresent(assetAttribute -> assetAttribute.setValue(Values.create(clientId)));
+ gateway.getAttribute("clientSecret").ifPresent(assetAttribute -> assetAttribute.setValue(Values.create(secret)));
+ try {
+ assetStorageService.merge(gateway);
+ LOG.info("Created gateway keycloak client for gateway id: " + gateway.getId());
+ } catch (Exception e) {
+ LOG.log(Level.SEVERE, "Failed to merge registered gateway: " + gateway.getId(), e);
+ }
+ }
+ }
+
+ protected void destroyGatewayClient(Asset gateway) {
+ String id = gateway.getAttribute("clientId").flatMap(AssetAttribute::getValueAsString).orElse(null);
+ if (TextUtil.isNullOrEmpty(id)) {
+ LOG.warning("Cannot find gateway keycloak client ID so cannot remove keycloak client for gateway: " + gateway.getId());
+ return;
+ }
+
+ final ClientsResource clientsResource = identityProvider.getRealms(getClientRequestInfo()).realm(gateway.getRealm()).clients();
+ clientsResource.findByClientId(id).stream().findFirst().ifPresent(
+ clientRepresentation -> {
+ clientsResource.get(clientRepresentation.getId()).remove();
+ });
+ }
+
+ protected ClientRequestInfo getClientRequestInfo() {
+ String accessToken = identityProvider.getAdminAccessToken(null);
+ return new ClientRequestInfo(null, accessToken);
+ }
+
+ protected Consumer<Object> createConnectorMessageConsumer(String sessionId) {
+ return msg -> clientEventService.sendToSession(sessionId, msg);
+ }
+
+ protected Runnable createDisconnectRunnable(String gatewayId, Session session) {
+ return () -> {
+ try {
+ session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, ""));
+ } catch (Exception e) {
+ LOG.log(Level.INFO, "Failed to close gateway client connection (GATEWAY ID=" + gatewayId + ")");
+ }
+ };
+ }
+
+ protected void onGatewayEventReceived(String gatewayId, SharedEvent event) {
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+ if (connector != null) {
+ connector.onGatewayEvent(event);
+ }
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
index e389614a8..63fd98373 100644
--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
@@ -191,7 +191,7 @@ public class EmailNotificationHandler implements NotificationHandler {
// Find descendant assets with email attribute
List<Asset> assets = assetStorageService.findAll(
new AssetQuery()
- .select(AssetQuery.Select.selectExcludePathAndParentAndRealm()
+ .select(AssetQuery.Select.selectExcludePathAndParentInfo()
.attributes(AttributeType.EMAIL.getAttributeName()))
.paths(new PathPredicate(targetId))
.attributes(new AttributePredicate(
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 f48ac3780..bea921f6e 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -32,7 +32,6 @@ import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.security.AuthForm;
import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
-import org.openremote.container.security.keycloak.KeycloakResource;
import org.openremote.container.timer.TimerService;
import org.openremote.container.web.ClientRequestInfo;
import org.openremote.container.web.WebService;
@@ -361,7 +360,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
public void updateTenant(ClientRequestInfo clientRequestInfo, String realm, Tenant tenant) {
LOG.fine("Update tenant: " + tenant);
- getRealms(new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getRealmAdminToken(realm, clientRequestInfo))).realm(realm).update(
+ getRealms(new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getAdminAccessToken(clientRequestInfo))).realm(realm).update(
convert(Container.JSON, RealmRepresentation.class, tenant)
);
publishModification(PersistenceEvent.Cause.UPDATE, tenant);
@@ -377,7 +376,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
LOG.fine("Create tenant: " + tenant);
RealmRepresentation realmRepresentation = convert(Container.JSON, RealmRepresentation.class, tenant);
configureRealm(realmRepresentation, emailConfig);
- clientRequestInfo = new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getRealmAdminToken(tenant.getRealm(), clientRequestInfo));
+ clientRequestInfo = new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getAdminAccessToken(clientRequestInfo));
// TODO This is not atomic, write compensation actions
getRealms(clientRequestInfo).create(realmRepresentation);
createClientApplication(clientRequestInfo, realmRepresentation.getRealm());
@@ -390,7 +389,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
Tenant tenant = getTenant(realm);
if (tenant != null) {
LOG.fine("Delete tenant: " + realm);
- getRealms(new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getRealmAdminToken(realm, clientRequestInfo))).realm(realm).remove();
+ getRealms(new ClientRequestInfo(clientRequestInfo.getRemoteAddress(), getAdminAccessToken(clientRequestInfo))).realm(realm).remove();
publishModification(PersistenceEvent.Cause.DELETE, tenant);
}
}
@@ -480,10 +479,14 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
public void createClientApplication(ClientRequestInfo clientRequestInfo, String realm) {
- ClientsResource clientsResource = getRealms(clientRequestInfo).realm(realm).clients();
- ClientRepresentation client = createClientApplication(
+ ClientRepresentation client = createDefaultClientRepresentation(
realm, KEYCLOAK_CLIENT_ID, "OpenRemote", devMode
);
+ createClientApplication(clientRequestInfo, realm, client);
+ }
+
+ public void createClientApplication(ClientRequestInfo clientRequestInfo, String realm, ClientRepresentation client) {
+ ClientsResource clientsResource = getRealms(clientRequestInfo).realm(realm).clients();
clientsResource.create(client);
client = clientsResource.findByClientId(client.getClientId()).get(0);
ClientResource clientResource = clientsResource.get(client.getId());
@@ -494,11 +497,11 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
* Keycloak only allows realm CRUD using the {realm}-realm client or the admin-cli client so we need to ensure we
* have a token for one of these realms; if we are creating a realm then that means using the admin-cli
*/
- protected String getRealmAdminToken(String realm, ClientRequestInfo clientRequestInfo) {
+ public String getAdminAccessToken(ClientRequestInfo clientRequestInfo) {
try {
- AccessToken token = TokenVerifier.create(clientRequestInfo.getAccessToken(), AccessToken.class).getToken();
+ AccessToken token = clientRequestInfo != null ? TokenVerifier.create(clientRequestInfo.getAccessToken(), AccessToken.class).getToken() : null;
- if (!token.getIssuedFor().equals(ADMIN_CLI_CLIENT_ID)) {
+ if (token == null || !token.getIssuedFor().equals(ADMIN_CLI_CLIENT_ID)) {
return getKeycloak().getAccessToken(
MASTER_REALM, new AuthForm(ADMIN_CLI_CLIENT_ID, MASTER_REALM_ADMIN_USER, keycloakAdminPassword)
).getToken();
@@ -510,7 +513,7 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
}
- protected ClientRepresentation createClientApplication(String realm, String clientId, String appName, boolean devMode) {
+ protected ClientRepresentation createDefaultClientRepresentation(String realm, String clientId, String appName, boolean devMode) {
ClientRepresentation client = new ClientRepresentation();
client.setClientId(clientId);
diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
index fdca1c8b6..281095470 100644
--- a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
@@ -25,6 +25,7 @@ import org.keycloak.admin.client.resource.UsersResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.openremote.container.Container;
import org.openremote.container.security.AuthForm;
+import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
import org.openremote.model.security.TenantEmailConfig;
@@ -38,11 +39,6 @@ import static org.openremote.model.Constants.*;
public abstract class AbstractKeycloakSetup implements Setup {
- // We use this client ID to access Keycloak because by default it allows obtaining
- // 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 SETUP_ADMIN_PASSWORD = "SETUP_ADMIN_PASSWORD";
public static final String SETUP_ADMIN_PASSWORD_DEFAULT = "secret";
public static final String SETUP_EMAIL_FROM_KEYCLOAK = "SETUP_EMAIL_FROM_KEYCLOAK";
@@ -93,7 +89,7 @@ public abstract class AbstractKeycloakSetup implements Setup {
// Use direct access grant feature of Keycloak Admin CLI to get superuser access token
String keycloakAdminPassword = container.getConfig().getOrDefault(SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT);
this.accessToken = keycloakProvider.getKeycloak().getAccessToken(
- MASTER_REALM, new AuthForm(ADMIN_CLI_CLIENT_ID, MASTER_REALM_ADMIN_USER, keycloakAdminPassword)
+ MASTER_REALM, new AuthForm(KeycloakIdentityProvider.ADMIN_CLI_CLIENT_ID, MASTER_REALM_ADMIN_USER, keycloakAdminPassword)
).getToken();
masterRealmResource = keycloakProvider.getRealms(accessToken).realm(MASTER_REALM);
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 ddd17f750..aaaca07ea 100644
--- a/manager/src/main/java/org/openremote/manager/setup/SetupService.java
+++ b/manager/src/main/java/org/openremote/manager/setup/SetupService.java
@@ -29,6 +29,8 @@ import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Logger;
+import static org.openremote.manager.agent.AgentService.AGENT_SERVICE_PRIORITY;
+
/**
* Executes setup tasks for a clean installation when the application starts.
* <p>
@@ -51,7 +53,7 @@ public class SetupService implements ContainerService {
@Override
public int getPriority() {
- return ContainerService.DEFAULT_PRIORITY;
+ return AGENT_SERVICE_PRIORITY - 1; // Start before agent service to ensure agents etc. are available
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
index b5f5e6fed..5b77fb8e0 100644
--- a/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
@@ -97,8 +97,8 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
public String realmBuildingTenant;
public String realmCityTenant;
public String smartCityServiceAgentId;
-
-
+ public String area1Id;
+ public String microphone1Id;
public ManagerDemoSetup(Container container, boolean importDemoScenes) {
super(container);
@@ -789,6 +789,7 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
new AssetAttribute(AttributeType.GEO_COUNTRY, Values.create("Netherlands"))
);
assetArea1 = assetStorageService.merge(assetArea1);
+ area1Id = assetArea1.getId();
Asset peopleCounter1Asset = createDemoPeopleCounterAsset("PeopleCounter 1", assetArea1, new GeoJSONPoint(5.477126, 51.439137), () -> new MetaItem[]{
new MetaItem(AGENT_LINK, new AttributeRef(smartCityServiceAgentId, "citySimulator").toArrayValue()),
@@ -796,11 +797,12 @@ public class ManagerDemoSetup extends AbstractManagerSetup {
});
peopleCounter1Asset = assetStorageService.merge(peopleCounter1Asset);
- Asset microPhone1Asset = createDemoMicrophoneAsset("Microphone 1", assetArea1, new GeoJSONPoint(5.478092, 51.438655), () -> new MetaItem[]{
+ Asset microphone1Asset = createDemoMicrophoneAsset("Microphone 1", assetArea1, new GeoJSONPoint(5.478092, 51.438655), () -> new MetaItem[]{
new MetaItem(AGENT_LINK, new AttributeRef(smartCityServiceAgentId, "citySimulator").toArrayValue()),
new MetaItem(SimulatorProtocol.SIMULATOR_ELEMENT, Values.create(NumberSimulatorElement.ELEMENT_NAME))
});
- microPhone1Asset = assetStorageService.merge(microPhone1Asset);
+ microphone1Asset = assetStorageService.merge(microphone1Asset);
+ microphone1Id = microphone1Asset.getId();
Asset enviroment1Asset = createDemoEnvironmentAsset("Environment 1", assetArea1, new GeoJSONPoint(5.478907, 51.438943),() -> new MetaItem[]{
new MetaItem(AGENT_LINK, new AttributeRef(smartCityServiceAgentId, "citySimulator").toArrayValue()),
diff --git a/manager/src/main/resources/META-INF/persistence.xml b/manager/src/main/resources/META-INF/persistence.xml
index 9bfb8f398..3a06b9a2a 100644
--- a/manager/src/main/resources/META-INF/persistence.xml
+++ b/manager/src/main/resources/META-INF/persistence.xml
@@ -11,7 +11,6 @@
<class>org.openremote.container.util</class>
<class>org.openremote.model.asset.Asset</class>
- <class>org.openremote.model.notification.DeviceNotificationToken</class>
<class>org.openremote.model.notification.SentNotification</class>
<class>org.openremote.manager.security.UserConfiguration</class>
<class>org.openremote.model.datapoint.AssetDatapoint</class>
@@ -23,6 +22,7 @@
<class>org.openremote.model.rules.TenantRuleset</class>
<class>org.openremote.model.rules.AssetRuleset</class>
<class>org.openremote.model.syslog.SyslogEvent</class>
+ <class>org.openremote.model.gateway.GatewayConnection</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
diff --git a/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService
index cb2f184ca..c97d1d549 100644
--- a/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService
+++ b/manager/src/main/resources/META-INF/services/org.openremote.container.ContainerService
@@ -25,3 +25,5 @@ org.openremote.container.message.MessageBrokerSetupService
org.openremote.manager.notification.PushNotificationHandler
org.openremote.manager.notification.EmailNotificationHandler
org.openremote.manager.predicted.AssetPredictedDatapointService
+org.openremote.manager.gateway.GatewayService
+org.openremote.manager.gateway.GatewayClientService
diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
index ef8d6f01c..7ef8d7d14 100644
--- a/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20191202_01__Schema.sql
@@ -129,6 +129,18 @@ create table ASSET_PREDICTED_DATAPOINT (
primary key (TIMESTAMP, ENTITY_ID, ATTRIBUTE_NAME)
);
+create table GATEWAY_CONNECTION (
+ LOCAL_REALM varchar(255) not null,
+ REALM varchar(255) not null,
+ HOST varchar(255) not null,
+ PORT int8 null,
+ CLIENT_ID varchar(36) not null,
+ CLIENT_SECRET varchar(36) not null,
+ SECURED boolean null,
+ DISABLED boolean not null default false,
+ primary key (LOCAL_REALM)
+);
+
/*
############################# FUNCTIONS #############################
*/
diff --git a/model/src/main/java/org/openremote/model/asset/AssetEvent.java b/model/src/main/java/org/openremote/model/asset/AssetEvent.java
index 8e80d509f..30173888e 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetEvent.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetEvent.java
@@ -19,8 +19,11 @@
*/
package org.openremote.model.asset;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
import org.openremote.model.event.shared.EventFilter;
-import org.openremote.model.event.shared.SharedEventWithAssetId;
+import org.openremote.model.event.shared.AssetInfo;
+import org.openremote.model.event.shared.SharedEvent;
import java.util.Arrays;
@@ -30,42 +33,7 @@ import java.util.Arrays;
* the {@link org.openremote.model.attribute.AttributeEvent}. When the cause is {@link Cause#READ} then the asset's
* {@link org.openremote.model.attribute.Attribute}s will be included in the asset otherwise they are not.
*/
-public class AssetEvent extends SharedEventWithAssetId {
-
- public static class AssetIdFilter<T extends SharedEventWithAssetId> extends EventFilter<T> {
-
- public static final String FILTER_TYPE = "asset-id";
-
- protected String[] assetIds = new String[0];
-
- protected AssetIdFilter() {
- }
-
- public AssetIdFilter(String... assetIds) {
- this.assetIds = assetIds;
- }
-
- public String[] getAssetIds() {
- return assetIds;
- }
-
- @Override
- public String getFilterType() {
- return FILTER_TYPE;
- }
-
- @Override
- public boolean apply(SharedEventWithAssetId event) {
- return Arrays.asList(assetIds).contains(event.getEntityId());
- }
-
- @Override
- public String toString() {
- return getClass().getSimpleName() + "{" +
- "assetIds='" + Arrays.toString(assetIds) + '\'' +
- '}';
- }
- }
+public class AssetEvent extends SharedEvent implements AssetInfo {
public enum Cause {
CREATE,
@@ -78,8 +46,8 @@ public class AssetEvent extends SharedEventWithAssetId {
protected Asset asset;
protected String[] updatedProperties;
-
- public AssetEvent(Cause cause, Asset asset, String[] updatedProperties) {
+ @JsonCreator
+ public AssetEvent(@JsonProperty("cause") Cause cause, @JsonProperty("asset") Asset asset, @JsonProperty("updatedProperties") String[] updatedProperties) {
this.cause = cause;
this.asset = asset;
this.updatedProperties = updatedProperties;
@@ -89,6 +57,16 @@ public class AssetEvent extends SharedEventWithAssetId {
return asset.id;
}
+ @Override
+ public String getRealm() {
+ return asset.realm;
+ }
+
+ @Override
+ public String getParentId() {
+ return asset.parentId;
+ }
+
public Cause getCause() {
return cause;
}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetFilter.java b/model/src/main/java/org/openremote/model/asset/AssetFilter.java
new file mode 100644
index 000000000..87b55c292
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AssetFilter.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright 2020, 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.model.asset;
+
+import org.openremote.model.event.shared.AssetInfo;
+import org.openremote.model.event.shared.EventFilter;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.util.TextUtil;
+
+import java.util.Arrays;
+
+public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<T> {
+
+ public static final String FILTER_TYPE = "asset";
+
+ protected String[] assetIds;
+ protected String realm;
+ protected String[] parentIds;
+
+ public AssetFilter() {
+ }
+
+ public AssetFilter(String... assetIds) {
+ this.assetIds = assetIds;
+ }
+
+ public String[] getAssetIds() {
+ return assetIds;
+ }
+
+ public AssetFilter<T> setAssetIds(String...assetIds) {
+ this.assetIds = assetIds;
+ return this;
+ }
+
+ public String getRealm() {
+ return realm;
+ }
+
+ public AssetFilter<T> setRealm(String realm) {
+ this.realm = realm;
+ return this;
+ }
+
+ public String[] getParentIds() {
+ return parentIds;
+ }
+
+ public AssetFilter<T> setParentIds(String...parentIds) {
+ this.parentIds = parentIds;
+ return this;
+ }
+
+ @Override
+ public String getFilterType() {
+ return FILTER_TYPE;
+ }
+
+ @Override
+ public boolean apply(T event) {
+
+ if (assetIds != null && assetIds.length > 0) {
+ if (!Arrays.asList(assetIds).contains(event.getEntityId())) {
+ return false;
+ }
+ }
+
+ if (parentIds != null && parentIds.length > 0) {
+ if (!Arrays.asList(parentIds).contains(event.getParentId())) {
+ return false;
+ }
+ }
+
+ if (!TextUtil.isNullOrEmpty(realm)) {
+ if (!realm.equals(event.getRealm())) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "assetIds='" + (assetIds != null ? Arrays.toString(assetIds) : "") + '\'' +
+ ", parentIds='" + (parentIds != null ? Arrays.toString(parentIds) : "") + '\'' +
+ ", realm='" + realm + '\'' +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/AssetType.java b/model/src/main/java/org/openremote/model/asset/AssetType.java
index ee51f4730..e410045af 100644
--- a/model/src/main/java/org/openremote/model/asset/AssetType.java
+++ b/model/src/main/java/org/openremote/model/asset/AssetType.java
@@ -21,10 +21,12 @@ package org.openremote.model.asset;
import org.openremote.model.attribute.AttributeDescriptor;
import org.openremote.model.attribute.AttributeDescriptorImpl;
+import org.openremote.model.attribute.MetaItemDescriptorImpl;
import org.openremote.model.value.Values;
import java.util.Optional;
+import static org.openremote.model.Constants.ASSET_META_NAMESPACE;
import static org.openremote.model.Constants.ASSET_NAMESPACE;
import static org.openremote.model.attribute.AttributeType.*;
import static org.openremote.model.attribute.AttributeValueType.*;
@@ -261,6 +263,26 @@ public enum AssetType implements AssetDescriptor {
)
),
+ GATEWAY(ASSET_NAMESPACE + ":gateway", "router-wireless", null,
+ new AttributeDescriptorImpl("clientId", STRING, null,
+ LABEL.withInitialValue(Values.create("Client ID")),
+ DESCRIPTION.withInitialValue(Values.create("Client ID for gateway to authorise")),
+ READ_ONLY
+ ),
+ new AttributeDescriptorImpl("clientSecret", STRING, null,
+ LABEL.withInitialValue(Values.create("Client Secret")),
+ DESCRIPTION.withInitialValue(Values.create("Client secret for gateway to authorise")),
+ READ_ONLY,
+ SECRET
+ ),
+ new AttributeDescriptorImpl("status", CONNECTION_STATUS, null,
+ LABEL.withInitialValue(Values.create("Connection status")),
+ DESCRIPTION.withInitialValue(Values.create("Connection status of the gateway")),
+ READ_ONLY
+ ),
+ new AttributeDescriptorImpl("disabled", BOOLEAN, Values.create(false))
+ ),
+
THING(ASSET_NAMESPACE + ":thing", "cube-outline", null);
final protected String type;
diff --git a/model/src/main/java/org/openremote/model/asset/AssetsEvent.java b/model/src/main/java/org/openremote/model/asset/AssetsEvent.java
new file mode 100644
index 000000000..18077daf4
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/AssetsEvent.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2019, 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.model.asset;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * This event is used in response to a {@link ReadAssetsEvent}.
+ */
+public class AssetsEvent extends SharedEvent {
+
+ protected String name;
+ protected List<Asset> assets;
+
+ @JsonCreator
+ public AssetsEvent(@JsonProperty("name") String name, @JsonProperty("assets") List<Asset> assets) {
+ this.name = name;
+ this.assets = assets;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public List<Asset> getAssets() {
+ return assets;
+ }
+
+ @Override
+ public String toString() {
+ return AssetsEvent.class.getSimpleName() + "{" +
+ "name='" + name + '\'' +
+ ", assets=" + (assets == null ? "null" : assets.stream().map(Asset::getId).collect(Collectors.joining())) +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/DeleteAssetsRequestEvent.java b/model/src/main/java/org/openremote/model/asset/DeleteAssetsRequestEvent.java
new file mode 100644
index 000000000..d0c0a5b0a
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/DeleteAssetsRequestEvent.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2017, 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.model.asset;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.query.AssetQuery;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A client sends this event to the server to delete one or more assets, expecting
+ * the server to answer "soon" with an {@link DeleteAssetsResponseEvent}.
+ */
+public class DeleteAssetsRequestEvent extends SharedEvent {
+
+ protected String name;
+ protected List<String> assetIds;
+
+ @JsonCreator
+ public DeleteAssetsRequestEvent(@JsonProperty("name") String name, @JsonProperty("assetIds") List<String> assetIds) {
+ this.name = name;
+ this.assetIds = assetIds;
+ }
+
+ public DeleteAssetsRequestEvent(List<String> assetIds) {
+ this.assetIds = assetIds;
+ }
+
+ public List<String> getAssetIds() {
+ return assetIds;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "name='" + name + '\'' +
+ "ids='" + Arrays.toString(assetIds.toArray()) + '\'' +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/DeleteAssetsResponseEvent.java b/model/src/main/java/org/openremote/model/asset/DeleteAssetsResponseEvent.java
new file mode 100644
index 000000000..fe62db890
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/DeleteAssetsResponseEvent.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2017, 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.model.asset;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A client sends this event in response to a {@link DeleteAssetsRequestEvent} indicating if the delete
+ * was successful.
+ */
+public class DeleteAssetsResponseEvent extends SharedEvent {
+
+ protected String name;
+ protected boolean deleted;
+
+ @JsonCreator
+ public DeleteAssetsResponseEvent(@JsonProperty("name") String name, @JsonProperty("deleted") boolean deleted) {
+ this.name = name;
+ this.deleted = deleted;
+ }
+
+ public DeleteAssetsResponseEvent(boolean deleted) {
+ this.deleted = deleted;
+ }
+
+ public boolean isDeleted() {
+ return deleted;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "name='" + name + '\'' +
+ "deleted='" + deleted + '\'' +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java
index 5dd49afa2..15be78137 100644
--- a/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java
+++ b/model/src/main/java/org/openremote/model/asset/ReadAssetEvent.java
@@ -19,20 +19,16 @@
*/
package org.openremote.model.asset;
-import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.shared.SharedEvent;
-import java.util.Arrays;
-
/**
- * A client sends this event to the server to refresh its attribute state, expecting
- * the server to answer "soon" with {@link AttributeEvent}s. If the server
+ * A client sends this event to the server to refresh its asset state, expecting
+ * the server to answer "soon" with an {@link AssetEvent}, the client must have an
+ * {@link org.openremote.model.event.shared.EventSubscription}&lt;{@link AssetEvent}&gt;
+ * in place in order to receive the response. If the server
* decides that the client doesn't have the right permissions, or if anything
* else is not in order (e.g. the asset doesn't exist), the server might not react
* at all.
- * <p>
- * If no attribute names and only an asset identifier are provided, all attributes
- * of the asset, accessible by the client, will be read/returned.
*/
public class ReadAssetEvent extends SharedEvent {
diff --git a/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java b/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java
new file mode 100644
index 000000000..02d189fff
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/asset/ReadAssetsEvent.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2017, 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.model.asset;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+import org.openremote.model.query.AssetQuery;
+
+/**
+ * A client sends this event to the server to query assets, expecting
+ * the server to answer "soon" with an {@link AssetsEvent} with the results.
+ */
+public class ReadAssetsEvent extends SharedEvent {
+
+ protected String name;
+ protected AssetQuery assetQuery;
+
+ @JsonCreator
+ public ReadAssetsEvent(@JsonProperty("name") String name, @JsonProperty("assetQuery") AssetQuery assetQuery) {
+ this.name = name;
+ this.assetQuery = assetQuery;
+ }
+
+ public ReadAssetsEvent(AssetQuery assetQuery) {
+ this.assetQuery = assetQuery;
+ }
+
+ public AssetQuery getAssetQuery() {
+ return assetQuery;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "name='" + name + '\'' +
+ "query='" + assetQuery + '\'' +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java
index 0d999390c..cb017b417 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java
@@ -19,7 +19,8 @@
*/
package org.openremote.model.attribute;
-import org.openremote.model.event.shared.SharedEventWithAssetId;
+import org.openremote.model.event.shared.AssetInfo;
+import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.value.Value;
import java.util.Objects;
@@ -28,7 +29,7 @@ import java.util.Optional;
/**
* A timestamped {@link AttributeState}.
*/
-public class AttributeEvent extends SharedEventWithAssetId {
+public class AttributeEvent extends SharedEvent implements AssetInfo {
public static final String HEADER_SOURCE = AttributeEvent.class.getName() + ".SOURCE";
@@ -58,10 +59,17 @@ public class AttributeEvent extends SharedEventWithAssetId {
/**
* The event is a value change on a sensor, created by a protocol.
*/
- SENSOR
+ SENSOR,
+
+ /**
+ * An attribute event generated by a gateway connector.
+ */
+ GATEWAY
}
protected AttributeState attributeState;
+ protected String realm;
+ protected String parentId;
protected AttributeEvent() {
}
@@ -117,6 +125,25 @@ public class AttributeEvent extends SharedEventWithAssetId {
return getAttributeRef().getEntityId();
}
+ public String getRealm() {
+ return realm;
+ }
+
+ public AttributeEvent setRealm(String realm) {
+ this.realm = realm;
+ return this;
+ }
+
+ @Override
+ public String getParentId() {
+ return parentId;
+ }
+
+ public AttributeEvent setParentId(String parentId) {
+ this.parentId = parentId;
+ return this;
+ }
+
public String getAttributeName() {
return getAttributeRef().getAttributeName();
}
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeType.java b/model/src/main/java/org/openremote/model/attribute/AttributeType.java
index 879f66a6d..572d264b6 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeType.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeType.java
@@ -20,7 +20,6 @@
package org.openremote.model.attribute;
import org.openremote.model.value.Value;
-import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;
import static org.openremote.model.Constants.ASSET_META_NAMESPACE;
diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java b/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java
index b8b147ab8..0e97dd7ea 100644
--- a/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java
+++ b/model/src/main/java/org/openremote/model/attribute/AttributeValueType.java
@@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import org.openremote.model.Constants;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
+import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;
@@ -158,7 +159,9 @@ public enum AttributeValueType implements AttributeValueDescriptor {
ASSET_STATUS("heart-pulse", ValueType.STRING),
- CALENDAR_EVENT("calendar", ValueType.OBJECT);
+ CALENDAR_EVENT("calendar", ValueType.OBJECT),
+
+ CONNECTION_STATUS("lan-connect", ValueType.STRING, new MetaItemDescriptorImpl(ALLOWED_VALUES, Values.createArray(Arrays.stream(ConnectionStatus.values()).map(connectionStatus -> Values.create(connectionStatus.name())).toArray(Value[]::new))));
public enum AttributeValueTypeFailureReason implements ValidationFailure.Reason {
ATTRIBUTE_TYPE_VALUE_DOES_NOT_MATCH
diff --git a/model/src/main/java/org/openremote/model/attribute/MetaItemType.java b/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
index 4e275d99d..ac639998c 100644
--- a/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
+++ b/model/src/main/java/org/openremote/model/attribute/MetaItemType.java
@@ -426,7 +426,20 @@ public enum MetaItemType implements MetaItemDescriptor {
null,
null,
Values.create(true),
- true);
+ true),
+
+ /**
+ * Marks the attribute as secret and indicates that clients should display this in a concealed manner (e.g. password
+ * input with optional show)
+ */
+ SECRET(
+ ASSET_META_NAMESPACE + ":readOnly",
+ new Access(true, false, true),
+ ValueType.BOOLEAN,
+ null,
+ null,
+ Values.create(true),
+ true),;
final protected String urn;
final protected Access access;
diff --git a/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java b/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java
index 7667e704e..525461e81 100644
--- a/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java
+++ b/model/src/main/java/org/openremote/model/event/TriggeredEventSubscription.java
@@ -25,18 +25,18 @@ import org.openremote.model.event.shared.SharedEvent;
import java.util.Arrays;
-public class TriggeredEventSubscription {
+public class TriggeredEventSubscription<T extends SharedEvent> {
- protected SharedEvent[] events;
+ protected T[] events;
protected String subscriptionId;
@JsonCreator
- public TriggeredEventSubscription(@JsonProperty("events") SharedEvent[] events, @JsonProperty("subscriptionId") String subscriptionId) {
+ public TriggeredEventSubscription(@JsonProperty("events") T[] events, @JsonProperty("subscriptionId") String subscriptionId) {
this.events = events;
this.subscriptionId = subscriptionId;
}
- public SharedEvent[] getEvents() {
+ public T[] getEvents() {
return events;
}
diff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEventWithAssetId.java b/model/src/main/java/org/openremote/model/event/shared/AssetInfo.java
similarity index 78%
rename from model/src/main/java/org/openremote/model/event/shared/SharedEventWithAssetId.java
rename to model/src/main/java/org/openremote/model/event/shared/AssetInfo.java
index 3bc0779f8..5ebdb9f9b 100644
--- a/model/src/main/java/org/openremote/model/event/shared/SharedEventWithAssetId.java
+++ b/model/src/main/java/org/openremote/model/event/shared/AssetInfo.java
@@ -19,14 +19,11 @@
*/
package org.openremote.model.event.shared;
-public abstract class SharedEventWithAssetId extends SharedEvent {
+public interface AssetInfo {
- public SharedEventWithAssetId() {
- }
+ String getEntityId();
- public SharedEventWithAssetId(long timestamp) {
- super(timestamp);
- }
+ String getRealm();
- public abstract String getEntityId();
+ String getParentId();
}
diff --git a/model/src/main/java/org/openremote/model/event/shared/EventFilter.java b/model/src/main/java/org/openremote/model/event/shared/EventFilter.java
index 9c2f4c714..8be623228 100644
--- a/model/src/main/java/org/openremote/model/event/shared/EventFilter.java
+++ b/model/src/main/java/org/openremote/model/event/shared/EventFilter.java
@@ -22,7 +22,7 @@ package org.openremote.model.event.shared;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
-import org.openremote.model.asset.AssetEvent;
+import org.openremote.model.asset.AssetFilter;
import org.openremote.model.simulator.SimulatorState;
import org.openremote.model.syslog.SyslogEvent;
@@ -32,7 +32,7 @@ import org.openremote.model.syslog.SyslogEvent;
@JsonSubTypes({
@JsonSubTypes.Type(value = TenantFilter.class, name = TenantFilter.FILTER_TYPE),
@JsonSubTypes.Type(value = SyslogEvent.LevelCategoryFilter.class, name = SyslogEvent.LevelCategoryFilter.FILTER_TYPE),
- @JsonSubTypes.Type(value = AssetEvent.AssetIdFilter.class, name = AssetEvent.AssetIdFilter.FILTER_TYPE),
+ @JsonSubTypes.Type(value = AssetFilter.class, name = AssetFilter.FILTER_TYPE),
@JsonSubTypes.Type(value = SimulatorState.ConfigurationFilter.class, name = SimulatorState.ConfigurationFilter.FILTER_TYPE)
})
@JsonTypeInfo(
diff --git a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java
index dccf7d7c6..1995bf47b 100644
--- a/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java
+++ b/model/src/main/java/org/openremote/model/event/shared/EventSubscription.java
@@ -45,12 +45,14 @@ public class EventSubscription<E extends SharedEvent> {
protected String eventType;
protected EventFilter<E> filter;
protected String subscriptionId;
+ @JsonIgnore
+ protected boolean subscribed;
/**
* Optional only set when an internal subscription is made
*/
@JsonIgnore
- protected Consumer<TriggeredEventSubscription> internalConsumer;
+ protected Consumer<TriggeredEventSubscription<E>> internalConsumer;
protected EventSubscription() {
}
@@ -72,13 +74,13 @@ public class EventSubscription<E extends SharedEvent> {
this.filter = filter;
}
- public EventSubscription(Class<E> eventClass, EventFilter<E> filter, Consumer<TriggeredEventSubscription> internalConsumer) {
+ public EventSubscription(Class<E> eventClass, EventFilter<E> filter, Consumer<TriggeredEventSubscription<E>> internalConsumer) {
this.eventType = Event.getEventType(eventClass);
this.filter = filter;
this.internalConsumer = internalConsumer;
}
- public EventSubscription(Class<E> eventClass, EventFilter<E> filter, String subscriptionId, Consumer<TriggeredEventSubscription> internalConsumer) {
+ public EventSubscription(Class<E> eventClass, EventFilter<E> filter, String subscriptionId, Consumer<TriggeredEventSubscription<E>> internalConsumer) {
this.eventType = Event.getEventType(eventClass);
this.filter = filter;
this.subscriptionId = subscriptionId;
@@ -105,14 +107,26 @@ public class EventSubscription<E extends SharedEvent> {
return Event.getEventType(eventClass).equals(getEventType());
}
- public Consumer<TriggeredEventSubscription> getInternalConsumer() {
+ public Consumer<TriggeredEventSubscription<E>> getInternalConsumer() {
return internalConsumer;
}
+ public void setInternalConsumer(Consumer<TriggeredEventSubscription<E>> internalConsumer) {
+ this.internalConsumer = internalConsumer;
+ }
+
public String getSubscriptionId() {
return subscriptionId;
}
+ public void setSubscribed(boolean subscribed) {
+ this.subscribed = subscribed;
+ }
+
+ public boolean isSubscribed() {
+ return subscribed;
+ }
+
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
diff --git a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
index da04b3129..f72acae76 100644
--- a/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
+++ b/model/src/main/java/org/openremote/model/event/shared/SharedEvent.java
@@ -21,13 +21,11 @@ package org.openremote.model.event.shared;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
-import org.openremote.model.asset.AssetEvent;
-import org.openremote.model.asset.AssetTreeModifiedEvent;
-import org.openremote.model.asset.ReadAssetAttributesEvent;
-import org.openremote.model.asset.ReadAssetEvent;
+import org.openremote.model.asset.*;
import org.openremote.model.asset.agent.AgentStatusEvent;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.Event;
+import org.openremote.model.gateway.GatewayDisconnectEvent;
import org.openremote.model.rules.RulesEngineStatusEvent;
import org.openremote.model.rules.RulesetChangedEvent;
import org.openremote.model.simulator.RequestSimulatorState;
@@ -42,14 +40,19 @@ import org.openremote.model.syslog.SyslogEvent;
@JsonSubTypes.Type(value = SyslogEvent.class, name = "syslog"),
@JsonSubTypes.Type(value = AttributeEvent.class, name = "attribute"),
@JsonSubTypes.Type(value = AssetEvent.class, name = "asset"),
+ @JsonSubTypes.Type(value = AssetsEvent.class, name = "assets"),
@JsonSubTypes.Type(value = ReadAssetAttributesEvent.class, name = "read-asset-attributes"),
@JsonSubTypes.Type(value = ReadAssetEvent.class, name = "read-asset"),
+ @JsonSubTypes.Type(value = ReadAssetsEvent.class, name = "read-assets"),
@JsonSubTypes.Type(value = AssetTreeModifiedEvent.class, name = "asset-tree-modified"),
@JsonSubTypes.Type(value = SimulatorState.class, name = "simulator-state"),
@JsonSubTypes.Type(value = RequestSimulatorState.class, name = "request-simulator-state"),
@JsonSubTypes.Type(value = AgentStatusEvent.class, name = "agent-status"),
@JsonSubTypes.Type(value = RulesEngineStatusEvent.class, name = "rules-engine-status"),
- @JsonSubTypes.Type(value = RulesetChangedEvent.class, name = "ruleset-changed")
+ @JsonSubTypes.Type(value = RulesetChangedEvent.class, name = "ruleset-changed"),
+ @JsonSubTypes.Type(value = GatewayDisconnectEvent.class, name = "gateway-disconnect"),
+ @JsonSubTypes.Type(value = DeleteAssetsRequestEvent.class, name = "delete-assets-request"),
+ @JsonSubTypes.Type(value = DeleteAssetsResponseEvent.class, name = "delete-assets-response")
})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayClientResource.java b/model/src/main/java/org/openremote/model/gateway/GatewayClientResource.java
new file mode 100644
index 000000000..cbca4dc3a
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayClientResource.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2016, 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.model.gateway;
+
+import jsinterop.annotations.JsType;
+import org.openremote.model.Constants;
+import org.openremote.model.http.RequestParams;
+import org.openremote.model.http.SuccessStatusCode;
+
+import javax.annotation.security.RolesAllowed;
+import javax.validation.Valid;
+import javax.ws.rs.*;
+
+import java.util.List;
+
+import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
+
+/**
+ * Resource for managing the connection to a central manager
+ */
+@Path("gateway")
+@JsType(isNative = true)
+public interface GatewayClientResource {
+
+ /**
+ * Get the {@link GatewayConnection} for the specified realm, user must be a realm admin
+ */
+ @GET
+ @Path("connection/{realm}")
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})
+ GatewayConnection getConnection(@BeanParam RequestParams requestParams, @PathParam("realm") String realm);
+
+ /**
+ * Get the {@link GatewayConnection}s for all realms, user must be a super user
+ */
+ @GET
+ @Path("connection")
+ @Produces(APPLICATION_JSON)
+ @SuccessStatusCode(200)
+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})
+ List<GatewayConnection> getConnections(@BeanParam RequestParams requestParams);
+
+ /**
+ * Update a {@link GatewayConnection} for the specified realm
+ */
+ @PUT
+ @Path("connection/{realm}")
+ @Consumes(APPLICATION_JSON)
+ @SuccessStatusCode(204)
+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})
+ @SuppressWarnings("unusable-by-js")
+ void setConnection(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @Valid GatewayConnection connection);
+
+ @DELETE
+ @Path("connection/{realm}")
+ @SuccessStatusCode(204)
+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})
+ void deleteConnection(@BeanParam RequestParams requestParams, @PathParam("realm") String realm);
+
+ @DELETE
+ @Path("connection")
+ @SuccessStatusCode(204)
+ @RolesAllowed({Constants.WRITE_ADMIN_ROLE})
+ void deleteConnections(@BeanParam RequestParams requestParams, @QueryParam("realm") List<String> realms);
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java b/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java
new file mode 100644
index 000000000..7de7c97d8
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright 2020, 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.model.gateway;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.Min;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+@Entity
+@Table(name = "GATEWAY_CONNECTION")
+public class GatewayConnection {
+
+ @Id
+ @Column(name = "LOCAL_REALM", nullable = false)
+ protected String localRealm;
+
+ @NotNull(message = "{GatewayConnection.host.NotNull}")
+ @Size(min = 1, max = 255, message = "{GatewayConnection.host.Size}")
+ @Column(name = "HOST", nullable = false)
+ protected String host;
+
+ @Min(value = 1, message = "{GatewayConnection.port.Range}")
+ @Max(value = 65536, message = "{GatewayConnection.port.Range}")
+ @Column(name = "PORT")
+ protected Integer port;
+
+ @Column(name = "REALM", nullable = false)
+ protected String realm;
+
+ @NotNull(message = "{GatewayConnection.clientId.NotNull}")
+ @Size(min = 1, max = 255, message = "{GatewayConnection.clientId.Size}")
+ @Column(name = "CLIENT_ID", nullable = false, length = 36)
+ protected String clientId;
+
+ @NotNull(message = "{GatewayConnection.clientSecret.NotNull}")
+ @Size(min = 36, max = 36, message = "{GatewayConnection.clientSecret.Size}")
+ @Column(name = "CLIENT_SECRET", nullable = false, length = 36)
+ protected String clientSecret;
+
+ @Column(name = "SECURED")
+ protected Boolean secured;
+
+ @Column(name = "DISABLED")
+ protected boolean disabled;
+
+ /**
+ * For JPA
+ */
+ GatewayConnection() {
+ }
+
+ @JsonCreator
+ public GatewayConnection(
+ @JsonProperty("localRealm") String localRealm,
+ @JsonProperty("host") String host,
+ @JsonProperty("port") Integer port,
+ @JsonProperty("realm") String realm,
+ @JsonProperty("clientId") String clientId,
+ @JsonProperty("clientSecret") String clientSecret,
+ @JsonProperty("secured") Boolean secured,
+ @JsonProperty("disabled") boolean disabled) {
+ this.localRealm = localRealm;
+ this.host = host;
+ this.port = port;
+ this.realm = realm;
+ this.clientId = clientId;
+ this.clientSecret = clientSecret;
+ this.secured = secured;
+ this.disabled = disabled;
+ }
+
+ public GatewayConnection(
+ String host,
+ Integer port,
+ String realm,
+ String clientId,
+ String clientSecret,
+ Boolean secured,
+ boolean disabled) {
+ this.host = host;
+ this.port = port;
+ this.realm = realm;
+ this.clientId = clientId;
+ this.clientSecret = clientSecret;
+ this.secured = secured;
+ this.disabled = disabled;
+ }
+
+ public String getLocalRealm() {
+ return localRealm;
+ }
+
+ public void setLocalRealm(String localRealm) {
+ this.localRealm = localRealm;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public Integer getPort() {
+ return port;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public void setPort(Integer port) {
+ this.port = port;
+ }
+
+ public String getRealm() {
+ return realm;
+ }
+
+ public void setRealm(String realm) {
+ this.realm = realm;
+ }
+
+ public String getClientId() {
+ return clientId;
+ }
+
+ public void setClientId(String clientId) {
+ this.clientId = clientId;
+ }
+
+ public String getClientSecret() {
+ return clientSecret;
+ }
+
+ public void setClientSecret(String clientSecret) {
+ this.clientSecret = clientSecret;
+ }
+
+ public boolean isSecured() {
+ return secured == null ? true : secured;
+ }
+
+ public void setSecured(boolean secured) {
+ this.secured = secured;
+ }
+
+ public boolean isDisabled() {
+ return disabled;
+ }
+
+ public void setDisabled(boolean disabled) {
+ this.disabled = disabled;
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "{" +
+ "localRealm='" + localRealm + '\'' +
+ ", host='" + host + '\'' +
+ ", port=" + port +
+ ", realm='" + realm + '\'' +
+ ", clientId='" + clientId + '\'' +
+ ", clientSecret='" + clientSecret + '\'' +
+ ", secured=" + secured +
+ ", disabled=" + disabled +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java
new file mode 100644
index 000000000..132ed28a6
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2020, 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.model.gateway;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+
+/**
+ * Used to indicate to a connected gateway should should disconnect
+ * with the {@link #reason} indicating why the disconnect has been
+ * requested.
+ */
+public class GatewayDisconnectEvent extends SharedEvent {
+
+ public enum Reason {
+ TERMINATING,
+ DISABLED,
+ ALREADY_CONNECTED,
+ UNRECOGNISED,
+ }
+
+ protected Reason reason;
+
+ @JsonCreator
+ public GatewayDisconnectEvent(@JsonProperty("timestamp") long timestamp, @JsonProperty("reason") Reason reason) {
+ super(timestamp);
+ this.reason = reason;
+ }
+
+ public GatewayDisconnectEvent(Reason reason) {
+ this.reason = reason;
+ }
+
+ public Reason getReason() {
+ return reason;
+ }
+}
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 8ff578f9b..1437112f2 100644
--- a/model/src/main/java/org/openremote/model/query/AssetQuery.java
+++ b/model/src/main/java/org/openremote/model/query/AssetQuery.java
@@ -42,9 +42,8 @@ public class AssetQuery {
public boolean excludeAttributeTimestamp;
public boolean excludeAttributeType;
public boolean excludeParentInfo;
- public boolean excludeRealm;
- public static Select selectExcludePathAndParentAndRealm() {
+ public static Select selectExcludePathAndParentInfo() {
return new Select()
.excludeAttributes(false)
.excludeAttributeMeta(false)
@@ -52,8 +51,7 @@ public class AssetQuery {
.excludeAttributeTimestamp(false)
.excludeAttributeValue(false)
.excludePath(true)
- .excludeParentInfo(true)
- .excludeRealm(true);
+ .excludeParentInfo(true);
}
public static Select selectExcludePathAndAttributes() {
@@ -70,8 +68,7 @@ public class AssetQuery {
.excludeAttributeValue(true)
.excludeAttributeTimestamp(true)
.excludePath(true)
- .excludeParentInfo(true)
- .excludeRealm(true);
+ .excludeParentInfo(true);
}
public Select attributes(String... attributeNames) {
@@ -128,22 +125,16 @@ public class AssetQuery {
return this;
}
- public Select excludeRealm(boolean exclude) {
- this.excludeRealm = exclude;
- return this;
- }
-
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
- ", excludeAttributes=" + excludeAttributes +
+ "excludeAttributes=" + excludeAttributes +
", excludeAttributeMeta=" + excludeAttributeMeta +
", excludeAttributeValue=" + excludeAttributeValue +
", excludeAttributeTimestamp=" + excludeAttributeTimestamp +
", excludeAttributeType=" + excludeAttributeType +
", excludePath=" + excludePath +
", excludeParentInfo=" + excludeParentInfo +
- ", excludeRealm=" + excludeRealm +
", attributeNames=" + Arrays.toString(attributes) +
'}';
}
@@ -437,6 +428,7 @@ public class AssetQuery {
", attribute=" + (attributes != null ? attributes.toString() : "null" ) +
", attributeMeta=" + Arrays.toString(attributeMeta) +
", orderBy=" + orderBy +
+ ", recursive=" + recursive +
'}';
}
}
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index 54446d5ca..f7b865b0a 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -47,6 +47,12 @@ public class Values {
return instance().create(bool);
}
+ public static ArrayValue createArray(Value...values) {
+ ArrayValue arrayValue = createArray();
+ arrayValue.addAll(values);
+ return arrayValue;
+ }
+
public static ArrayValue createArray() {
return instance().createArray();
}
diff --git a/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java b/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java
index f17f64b03..e77416fe2 100644
--- a/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java
+++ b/model/src/main/java/org/openremote/model/value/impl/ValueFactoryImpl.java
@@ -53,7 +53,7 @@ public class ValueFactoryImpl implements ValueFactory {
@Override
public <T extends Value> Optional<T> parse(String jsonString) throws ValueException {
- if (jsonString.isEmpty() || NULL_LITERAL.equals(jsonString)) {
+ if (jsonString == null || jsonString.isEmpty() || NULL_LITERAL.equals(jsonString)) {
return Optional.empty();
}
// TODO: Remove this
diff --git a/model/src/main/resources/ValidationMessages.properties b/model/src/main/resources/ValidationMessages.properties
index 0d682835b..04e9000d8 100644
--- a/model/src/main/resources/ValidationMessages.properties
+++ b/model/src/main/resources/ValidationMessages.properties
@@ -23,3 +23,11 @@ Asset.name.NotNull = Asset name can not be empty.
Asset.name.Size = Asset name must be at least {min} and not more than {max} characters long.
Asset.type.NotNull = Asset type can not be empty.
Asset.type.Size = Asset type must be at least {min} and not more than {max} characters long.
+
+GatewayConnection.host.NotNull = Gateway connection host can not be empty.
+GatewayConnection.host.Size = Gateway connection host must be at least {min} and not more than {max} characters long.
+GatewayConnection.port.Range = Gateway connection port must be between {min} and {max}.
+GatewayConnection.clientId.NotNull = Gateway connection client ID can not be empty.
+GatewayConnection.clientId.Size = Gateway connection client ID must be at least {min} and not more than {max} characters long.
+GatewayConnection.clientSecret.NotNull = Gateway connection client secret can not be empty.
+GatewayConnection.clientSecret.Size = Gateway connection client secret must be at least {min} and not more than {max} characters long.
diff --git a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy
index 7acf6843c..df23a27b9 100644
--- a/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy
+++ b/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy
@@ -32,7 +32,7 @@ trait ManagerContainerTrait extends ContainerTrait {
static Iterable<ContainerService> defaultServices(Iterable<ContainerService> additionalServices) {
[
*stream(ServiceLoader.load(ContainerService.class).spliterator(), false)
- .sorted(Comparator.comparingInt({it.getPriority()}))
+ .sorted(Comparator.comparingInt{it.getPriority()})
.collect(Collectors.toList()),
*additionalServices
] as Iterable<ContainerService>
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
index 8495f54bb..bd58be199 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetAttributeLinkingTest.groovy
@@ -33,6 +33,7 @@ class AssetAttributeLinkingTest extends Specification implements ManagerContaine
then: "the container should be running and initialised"
conditions.eventually {
+ container.isRunning()
assert noEventProcessedIn(assetProcessingService, 500)
}
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy
index 790d9cc6c..c6a1fbdee 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetModelResourceTest.groovy
@@ -21,7 +21,9 @@ package org.openremote.test.assets
import org.openremote.container.Container
import org.openremote.model.asset.AssetModelResource
+import org.openremote.model.asset.AssetType
import org.openremote.model.attribute.AttributeType
+import org.openremote.model.attribute.AttributeValueType
import org.openremote.model.attribute.MetaItemType
import org.openremote.model.value.ValueType
import org.openremote.test.ManagerContainerTrait
@@ -71,31 +73,30 @@ class AssetModelResourceTest extends Specification implements ManagerContainerTr
def assetDescriptors = assetModelResource.getAssetDescriptors(null)
then: "the default asset types should be present"
- assetDescriptors.size() == 15
- assetDescriptors[0].name == BUILDING.name
- assetDescriptors[0].attributeDescriptors.length == 5
- assetDescriptors[0].attributeDescriptors.find {it.attributeName == AttributeType.SURFACE_AREA.attributeName}.valueDescriptor.valueType == ValueType.NUMBER
- assetDescriptors[1].name == CITY.name
- assetDescriptors[2].name == AREA.name
- assetDescriptors[3].name == FLOOR.name
- assetDescriptors[4].name == RESIDENCE.name
- assetDescriptors[5].name == ROOM.name
- assetDescriptors[6].name == AGENT.name
- assetDescriptors[7].name == CONSOLE.name
- assetDescriptors[8].name == MICROPHONE.name
- assetDescriptors[9].name == SOUND_EVENT.name
- assetDescriptors[10].name == ENVIRONMENT_SENSOR.name
- assetDescriptors[11].name == LIGHT.name
- assetDescriptors[12].name == LIGHT_CONTROLLER.name
- assetDescriptors[13].name == PEOPLE_COUNTER.name
- assetDescriptors[14].name == THING.name
+ assetDescriptors.size() == AssetType.values().length
+ assetDescriptors.any{it.name == BUILDING.name && it.attributeDescriptors.length == 5 && it.attributeDescriptors.find {it.attributeName == AttributeType.SURFACE_AREA.attributeName}.valueDescriptor.valueType == ValueType.NUMBER}
+ assetDescriptors.any{it.name == CITY.name}
+ assetDescriptors.any{it.name == AREA.name}
+ assetDescriptors.any{it.name == FLOOR.name}
+ assetDescriptors.any{it.name == RESIDENCE.name}
+ assetDescriptors.any{it.name == ROOM.name}
+ assetDescriptors.any{it.name == AGENT.name}
+ assetDescriptors.any{it.name == CONSOLE.name}
+ assetDescriptors.any{it.name == MICROPHONE.name}
+ assetDescriptors.any{it.name == SOUND_EVENT.name}
+ assetDescriptors.any{it.name == ENVIRONMENT_SENSOR.name}
+ assetDescriptors.any{it.name == LIGHT.name}
+ assetDescriptors.any{it.name == LIGHT_CONTROLLER.name}
+ assetDescriptors.any{it.name == PEOPLE_COUNTER.name}
+ assetDescriptors.any{it.name == THING.name}
+ assetDescriptors.any{it.name == THING.name}
when: "a request for Attribute types is made"
def attributeTypeDescriptors = assetModelResource.getAttributeDescriptors(null)
then: "the default types should be present"
- attributeTypeDescriptors.size() == 13
+ attributeTypeDescriptors.size() == AttributeType.values().length
attributeTypeDescriptors.any {it.attributeName == "consoleName"}
attributeTypeDescriptors.any {it.attributeName == "consoleVersion"}
attributeTypeDescriptors.any {it.attributeName == "consolePlatform"}
@@ -111,14 +112,12 @@ class AssetModelResourceTest extends Specification implements ManagerContainerTr
attributeTypeDescriptors.any {it.attributeName == "assetTags"}
when: "a request for Attribute value types is made"
-
def attributeValueTypeDescriptors = assetModelResource.getAttributeValueDescriptors(null)
then: "the default value types should be present"
- attributeValueTypeDescriptors.size() == 36
+ attributeValueTypeDescriptors.size() == AttributeValueType.values().length
when: "a request for Attribute value types is made"
-
def metaItemDescriptors = assetModelResource.getMetaItemDescriptors(null)
then: "the default MetaItem types should be present"
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
index 946e5c697..dfeaa0716 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetPermissionsTest.groovy
@@ -157,25 +157,14 @@ class AssetPermissionsTest extends Specification implements ManagerContainerTrai
then: "the asset should be updated"
testAsset.parentId == managerDemoSetup.groundFloorId
- when: "an asset is moved to a foreign realm and made a root asset"
- testAsset.setRealm(keycloakDemoSetup.tenantBuilding.realm)
+ when: "an asset is made a root asset"
testAsset.setParentId(null)
assetResource.update(null, testAsset.id, testAsset)
testAsset = assetResource.get(null, testAsset.getId())
then: "the asset should be updated"
- testAsset.realm == keycloakDemoSetup.tenantBuilding.realm
testAsset.parentId == null
- when: "an asset is updated with a new parent in a foreign realm"
- testAsset.setParentId(managerDemoSetup.smartBuildingId)
- assetResource.update(null, testAsset.id, testAsset)
- testAsset = assetResource.get(null, testAsset.getId())
-
- then: "the asset should be updated"
- testAsset.realm == keycloakDemoSetup.tenantBuilding.realm
- testAsset.parentId == managerDemoSetup.smartBuildingId
-
when: "an asset is deleted in the authenticated realm"
assetResource.delete(null, [managerDemoSetup.thingId])
assetResource.get(null, managerDemoSetup.thingId)
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
index ed0aa005a..c140110b2 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
@@ -1,7 +1,10 @@
package org.openremote.test.assets
import org.openremote.agent.protocol.AbstractProtocol
+import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.*
+import org.openremote.manager.datapoint.AssetDatapointService
+import org.openremote.manager.rules.RulesService
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.KeycloakDemoSetup
import org.openremote.manager.setup.builtin.ManagerDemoSetup
@@ -10,6 +13,7 @@ import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetType
import org.openremote.model.asset.agent.ProtocolConfiguration
import org.openremote.model.attribute.*
+import org.openremote.model.datapoint.Datapoint
import org.openremote.model.value.Value
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
@@ -155,10 +159,10 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
then: "register mock asset processors"
assetProcessingService.processors.add(0, firstProcessor)
- assetProcessingService.processors.add(2, afterAgentServiceProcessor)
- assetProcessingService.processors.add(4, afterRulesServiceProcessor)
- assetProcessingService.processors.add(6, afterDatapointServiceProcessor)
- assetProcessingService.processors.add(8, afterAttributeLinkingServiceProcessor)
+ assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AgentService}+1, afterAgentServiceProcessor)
+ assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof RulesService}+1, afterRulesServiceProcessor)
+ assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AssetDatapointService}+1, afterDatapointServiceProcessor)
+ assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AssetAttributeLinkingService}+1, afterAttributeLinkingServiceProcessor)
when: "a mock agent that uses the mock protocol is created"
def mockAgent = new Asset()
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
index 5d05c0671..e13b2def4 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetQueryTest.groovy
@@ -78,7 +78,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "an agent filtering query is executed"
def assets = assetStorageService.findAll(
new AssetQuery()
- .select(Select.selectExcludePathAndParentAndRealm())
+ .select(Select.selectExcludePathAndParentInfo())
.types(AssetType.AGENT)
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.realm))
)
@@ -88,10 +88,10 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets[0].id == managerDemoSetup.agentId
assets[0].name == "Demo Agent"
assets[0].type == AssetType.AGENT.getType()
- assets[0].parentId == null
+ assets[0].parentId == managerDemoSetup.lobbyId
assets[0].parentName == null
assets[0].parentType == null
- assets[0].realm == null
+ assets[0].realm == managerDemoSetup.masterRealm
assets[0].path == null
when: "a user filtering query is executed that returns only IDs, names and attribute names and label meta"
@@ -178,10 +178,10 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
assets[1].name == "Apartment 1"
assets[1].type == AssetType.RESIDENCE.getType()
assets[1].createdOn != null
- assets[1].parentId == null
+ assets[1].parentId == managerDemoSetup.smartBuildingId
assets[1].parentName == null
assets[1].parentType == null
- assets[1].realm == null
+ assets[1].realm == managerDemoSetup.realmBuildingTenant
assets[1].path == null
assets[1].getAttributesList().size() == 7
assets[1].getAttribute("ventilationAuto").isPresent()
@@ -671,7 +671,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed to select an asset with multiple attribute predicates 'ANDED'"
asset = assetStorageService.find(
- new AssetQuery().select(Select.selectExcludePathAndParentAndRealm()).attributes(
+ new AssetQuery().select(Select.selectExcludePathAndParentInfo()).attributes(
new AttributePredicate(
new StringPredicate("windowOpen"), new BooleanPredicate(false)
),
@@ -690,7 +690,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed to select an asset with multiple attribute predicates 'ANDED' where one predicate is false"
asset = assetStorageService.find(
- new AssetQuery().select(Select.selectExcludePathAndParentAndRealm()).attributes(
+ new AssetQuery().select(Select.selectExcludePathAndParentInfo()).attributes(
new AttributePredicate(
new StringPredicate("windowOpen"), new BooleanPredicate(false)
),
@@ -705,7 +705,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed to select an asset with multiple logic groups"
asset = assetStorageService.find(
- new AssetQuery().select(Select.selectExcludePathAndParentAndRealm()).attributes(
+ new AssetQuery().select(Select.selectExcludePathAndParentInfo()).attributes(
new LogicGroup<AttributePredicate>(LogicGroup.Operator.AND, [
new AttributePredicate(
new StringPredicate("windowOpen"), new BooleanPredicate(false)
@@ -732,7 +732,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a query is executed to select an asset with multiple logic groups where one of the groups is false"
asset = assetStorageService.find(
- new AssetQuery().select(Select.selectExcludePathAndParentAndRealm()).attributes(
+ new AssetQuery().select(Select.selectExcludePathAndParentInfo()).attributes(
new LogicGroup<AttributePredicate>(LogicGroup.Operator.AND, [
new AttributePredicate(
new StringPredicate("windowOpen"), new BooleanPredicate(false)
@@ -887,7 +887,7 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
when: "a calendar event filtering query is executed for the correct date and time of the event"
def assets = assetStorageService.findAll(
new AssetQuery()
- .select(new Select().excludePath(true).excludeRealm(true).excludeAttributeMeta(true)) // Need attributes to do calendar filtering
+ .select(new Select().excludePath(true).excludeAttributeMeta(true)) // Need attributes to do calendar filtering
.tenant(new TenantPredicate(keycloakDemoSetup.masterTenant.realm))
.attributes(new AttributePredicate(new StringPredicate("test"), new CalendarEventPredicate(new Date(1517155200000)))) // 30/01/2018 @ 6:00pm (UTC)
.orderBy(new OrderBy(NAME))
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 02a2d5d64..03841bd89 100644
--- a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
@@ -82,8 +82,7 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {
ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_DEBOUNCE_MILLIS = 100
ORConsoleGeofenceAssetAdapter.NOTIFY_ASSETS_BATCH_MILLIS = 200
def services = Lists.newArrayList(defaultServices())
- services.removeIf {it instanceof PushNotificationHandler}
- services.add(mockPushNotificationHandler)
+ services.replaceAll{it instanceof PushNotificationHandler ? mockPushNotificationHandler : it}
def container = startContainer(defaultConfig(serverPort), services)
def assetStorageService = container.getService(AssetStorageService.class)
def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
@@ -484,7 +483,7 @@ class ConsoleTest extends Specification implements ManagerContainerTrait {
assert geofences.size() == 0
when: "a console is deleted"
- assetStorageService.delete([testUser3Console2.id])
+ assetStorageService.delete([testUser3Console2.id],false)
then: "the deleted console should be removed from ORConsoleGeofenceAssetAdapter"
conditions.eventually {
diff --git a/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy b/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
index f91d2c14c..c404b8055 100644
--- a/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/event/ClientEventTest.groovy
@@ -24,7 +24,7 @@ import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.ManagerDemoSetup
-import org.openremote.model.asset.AssetEvent
+import org.openremote.model.asset.AssetFilter
import org.openremote.model.attribute.MetaItemType
import org.openremote.model.asset.agent.AgentStatusEvent
import org.openremote.model.asset.agent.ConnectionStatus
@@ -133,7 +133,7 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
}
when: "a subscription is made to the attribute event"
- clientEventService.subscribe(AttributeEvent.class, new AssetEvent.AssetIdFilter(managerDemoSetup.thingId))
+ clientEventService.subscribe(AttributeEvent.class, new AssetFilter().setAssetIds(managerDemoSetup.thingId))
and: "an assets location is changed"
def asset = assetStorageService.find(managerDemoSetup.thingId, true)
@@ -187,7 +187,7 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
collectedSharedEvents.clear()
and: "a new attribute event subscription is added"
- clientEventService.subscribe(AttributeEvent.class, new AssetEvent.AssetIdFilter(managerDemoSetup.thingId))
+ clientEventService.subscribe(AttributeEvent.class, new AssetFilter().setAssetIds(managerDemoSetup.thingId))
and: "an internal attribute event subscription is made"
List<AttributeEvent> internalReceivedEvents = []
@@ -196,7 +196,7 @@ class ClientEventTest extends Specification implements ManagerContainerTrait, Gw
false,
new EventSubscription<>(
AttributeEvent.class,
- new AssetEvent.AssetIdFilter(managerDemoSetup.thingId),
+ new AssetFilter().setAssetIds(managerDemoSetup.thingId),
{ triggeredEventSubscription ->
internalReceivedEvents.add(triggeredEventSubscription.events[0] as AttributeEvent)
}
diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
new file mode 100644
index 000000000..ab8542f67
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
@@ -0,0 +1,986 @@
+package org.openremote.test.gateway
+
+import com.google.common.collect.Lists
+import io.netty.channel.ChannelHandler
+import org.apache.camel.builder.AdviceWithRouteBuilder
+import org.apache.http.client.utils.URIBuilder
+import org.openremote.agent.protocol.http.HttpClientProtocol
+import org.openremote.agent.protocol.http.OAuthClientCredentialsGrant
+import org.openremote.agent.protocol.io.AbstractNettyIoClient
+import org.openremote.agent.protocol.simulator.SimulatorProtocol
+import org.openremote.agent.protocol.websocket.WebsocketIoClient
+import org.openremote.container.Container
+import org.openremote.container.message.MessageBrokerSetupService
+import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.container.web.ClientRequestInfo
+import org.openremote.manager.asset.AssetProcessingService
+import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.concurrent.ManagerExecutorService
+import org.openremote.manager.event.ClientEventService
+import org.openremote.manager.gateway.GatewayClientService
+import org.openremote.manager.gateway.GatewayConnector
+import org.openremote.manager.gateway.GatewayService
+import org.openremote.manager.security.ManagerIdentityService
+import org.openremote.manager.security.ManagerKeycloakIdentityProvider
+import org.openremote.manager.setup.SetupService
+import org.openremote.manager.setup.builtin.ManagerDemoSetup
+import org.openremote.model.Constants
+import org.openremote.model.asset.*
+import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.attribute.*
+import org.openremote.model.event.shared.SharedEvent
+import org.openremote.model.gateway.GatewayClientResource
+import org.openremote.model.gateway.GatewayConnection
+import org.openremote.model.geo.GeoJSONPoint
+import org.openremote.model.query.AssetQuery
+import org.openremote.model.value.Values
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import java.util.concurrent.ScheduledFuture
+import java.util.concurrent.atomic.AtomicReference
+import java.util.stream.Collectors
+import java.util.stream.IntStream
+
+import static org.openremote.container.util.MapAccess.getString
+import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD
+import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD_DEFAULT
+import static org.openremote.model.Constants.*
+import static org.openremote.model.util.TextUtil.isNullOrEmpty
+
+class GatewayTest extends Specification implements ManagerContainerTrait {
+
+ def "Gateway asset provisioning and local manager logic test"() {
+
+ given: "the container environment is started"
+ def conditions = new PollingConditions(timeout: 10, delay: 1)
+ def serverPort = findEphemeralPort()
+ def container = startContainer(defaultConfig(serverPort), defaultServices())
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def executorService = container.getService(ManagerExecutorService.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def gatewayService = container.getService(GatewayService.class)
+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
+ def identityProvider = container.getService(ManagerIdentityService.class).identityProvider as ManagerKeycloakIdentityProvider
+ def httpClientProtocol = container.getService(HttpClientProtocol.class)
+ def clientsResource = identityProvider.getRealms(new ClientRequestInfo(null, identityProvider.getAdminAccessToken(null))).realm(managerDemoSetup.realmBuildingTenant).clients()
+
+ expect: "the container should be running and initialised"
+ conditions.eventually {
+ assert container.isRunning()
+ assert noEventProcessedIn(assetProcessingService, 300)
+ }
+
+ when: "a gateway is provisioned in this manager"
+ def gateway = assetStorageService.merge(new Asset("Test gateway", AssetType.GATEWAY, null, managerDemoSetup.realmBuildingTenant))
+
+ then: "a keycloak client should have been created for this gateway"
+ conditions.eventually {
+ def client = clientsResource.findByClientId(GatewayService.GATEWAY_CLIENT_ID_PREFIX + gateway.getId()).get(0)
+ assert client != null
+ }
+
+ and: "a set of credentials should have been created for this gateway and be stored against the gateway for easy reference"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId(), true)
+ assert gateway.getAttribute("clientId").isPresent()
+ assert !isNullOrEmpty(gateway.getAttribute("clientId").flatMap{it.getValueAsString()}.orElse(""))
+ assert !isNullOrEmpty(gateway.getAttribute("clientSecret").flatMap{it.getValueAsString()}.orElse(""))
+ }
+
+ and: "a gateway connector should have been created for this gateway"
+ conditions.eventually {
+ assert gatewayService.gatewayConnectorMap.size() == 1
+ assert gatewayService.gatewayConnectorMap.get(gateway.getId()).gatewayId == gateway.getId()
+ }
+
+ when: "the Gateway client is created"
+ def gatewayClient = new WebsocketIoClient<String>(
+ new URIBuilder("ws://localhost:$serverPort/websocket/events?Auth-Realm=$managerDemoSetup.realmBuildingTenant").build(),
+ null,
+ new OAuthClientCredentialsGrant("http://localhost:$serverPort/auth/realms/$managerDemoSetup.realmBuildingTenant/protocol/openid-connect/token",
+ gateway.getAttribute("clientId").flatMap{it.getValueAsString()}.orElse(""),
+ gateway.getAttribute("clientSecret").flatMap{it.getValueAsString()}.orElse(""),
+ null).setBasicAuthHeader(true),
+ executorService)
+ gatewayClient.setEncoderDecoderProvider({
+ [new AbstractNettyIoClient.MessageToMessageDecoder<String>(String.class, gatewayClient)].toArray(new ChannelHandler[0])
+ })
+
+ and: "we add callback consumers to the client"
+ def connectionStatus = gatewayClient.getConnectionStatus()
+ List<String> clientReceivedMessages = []
+ gatewayClient.addMessageConsumer({
+ message -> clientReceivedMessages.add(message)
+ })
+ gatewayClient.addConnectionStatusConsumer({
+ status -> connectionStatus = status
+ })
+
+ and: "the gateway connects to this manager"
+ gatewayClient.connect()
+
+ then: "the gateway netty client status should become CONNECTED"
+ conditions.eventually {
+ assert connectionStatus == ConnectionStatus.CONNECTED
+ assert gatewayClient.connectionStatus == ConnectionStatus.CONNECTED
+ }
+
+ and: "the gateway asset connection status should be CONNECTING"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getAttribute("status").flatMap{it.getValueAsString()}.orElse(null) == ConnectionStatus.CONNECTING.name()
+ }
+
+ and: "the server should have sent a CONNECTED message and an asset read request"
+ conditions.eventually {
+ assert clientReceivedMessages.size() >= 2
+ assert clientReceivedMessages[0] == "CONNECTED"
+ assert clientReceivedMessages[1].startsWith(SharedEvent.MESSAGE_PREFIX)
+ def readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[1].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)
+ assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL
+ assert readAssetsEvent.assetQuery != null
+ assert readAssetsEvent.assetQuery.select.excludeAttributes
+ assert readAssetsEvent.assetQuery.select.excludePath
+ assert readAssetsEvent.assetQuery.select.excludeParentInfo
+ assert readAssetsEvent.assetQuery.recursive
+ }
+
+ when: "the previously received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "the gateway client assets are defined"
+ List<String> agentAssetIds = []
+ List<Asset> agentAssets = []
+ List<String> assetIds = []
+ List<Asset> assets = []
+
+ IntStream.rangeClosed(1, 5).forEach {i ->
+ agentAssetIds.add(UniqueIdentifierGenerator.generateId("Test Agent $i"))
+ agentAssets.add(
+ new Asset(
+ agentAssetIds[i-1],
+ 0L,
+ new Date(),
+ "Test Agent $i",
+ AssetType.AGENT.type,
+ false,
+ (String)null,
+ (String)null,
+ (String)null,
+ "master",
+ (String[])[agentAssetIds[i-1]].toArray(new String[0]),
+ null).addAttributes(
+ new AssetAttribute("protocolConfig", AttributeValueType.STRING, Values.create(HttpClientProtocol.PROTOCOL_NAME))
+ .addMeta(
+ new MetaItem(MetaItemType.PROTOCOL_CONFIGURATION),
+ new MetaItem(HttpClientProtocol.META_PROTOCOL_BASE_URI, Values.create("https://google.co.uk")),
+ new MetaItem(HttpClientProtocol.META_PROTOCOL_PING_PATH, Values.create(""))
+ ),
+ new AssetAttribute(AttributeType.SURFACE_AREA, Values.create(1000), new Date().getTime())
+ )
+ )
+
+ assetIds.add(UniqueIdentifierGenerator.generateId("Test Building $i"))
+
+ // Add assets out of order to test gateway connector re-ordering logic
+ IntStream.rangeClosed(1, 4).forEach{j ->
+
+ assetIds.add(UniqueIdentifierGenerator.generateId("Test Building $i Room $j"))
+ assets.add(
+ new Asset(
+ assetIds[(i-1)*5+j],
+ 0L,
+ new Date(),
+ "Test Building $i Room $j",
+ AssetType.ROOM.type,
+ false,
+ assetIds[(i-1)*5],
+ (String)null,
+ (String)null,
+ "master",
+ (String[])[assetIds[(i-1)*5+j], assetIds[(i-1)*5]].toArray(new String[0]),
+ null).addAttributes(
+ new AssetAttribute(AttributeType.LOCATION, new GeoJSONPoint(10,11).toValue(), new Date().getTime())
+ .addMeta(
+ MetaItemType.ACCESS_PUBLIC_READ
+ ),
+ new AssetAttribute("temp", AttributeValueType.TEMPERATURE, null, new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[i-1], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ ),
+ new AssetAttribute("tempSetpoint", AttributeValueType.TEMPERATURE, null, new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[i-1], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ )
+ )
+ )
+ }
+
+ assets.add(
+ new Asset(
+ assetIds[(i-1)*5],
+ 0L,
+ new Date(),
+ "Test Building $i",
+ AssetType.BUILDING.type,
+ false,
+ (String)null,
+ (String)null,
+ (String)null,
+ "master",
+ (String[])[assetIds[(i-1)*5]].toArray(new String[0]),
+ null).addAttributes(
+ new AssetAttribute(AttributeType.LOCATION, new GeoJSONPoint(10,11).toValue(), new Date().getTime())
+ .addMeta(
+ MetaItemType.ACCESS_PUBLIC_READ
+ ),
+ new AssetAttribute(AttributeType.SURFACE_AREA, Values.create(1000), new Date().getTime())
+ )
+ )
+ }
+
+ and: "the gateway client replies to the central manager with the assets of the gateway"
+ List<Asset> sendAssets = []
+ sendAssets.addAll(agentAssets)
+ sendAssets.addAll(assets)
+ def readAssetsReplyEvent = new AssetsEvent(
+ GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL,
+ sendAssets
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))
+
+ then: "the central manager should have requested the full loading of the first batch of assets"
+ ReadAssetsEvent readAssetsEvent = null
+ conditions.eventually {
+ assert clientReceivedMessages.size() == 1
+ assert clientReceivedMessages.get(0).startsWith(SharedEvent.MESSAGE_PREFIX)
+ assert clientReceivedMessages.get(0).contains("read-assets")
+ readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)
+ assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + "0"
+ assert readAssetsEvent.assetQuery != null
+ assert readAssetsEvent.assetQuery.select.excludePath
+ assert readAssetsEvent.assetQuery.select.excludeParentInfo
+ assert readAssetsEvent.assetQuery.ids != null
+ assert readAssetsEvent.assetQuery.ids.length == GatewayConnector.SYNC_ASSET_BATCH_SIZE
+ assert agentAssetIds.stream().filter{readAssetsEvent.assetQuery.ids.contains(it)}.count() == agentAssetIds.size()
+ assert assetIds.stream().filter{readAssetsEvent.assetQuery.ids.contains(it)}.count() == GatewayConnector.SYNC_ASSET_BATCH_SIZE - agentAssetIds.size()
+ }
+
+ when: "the gateway returns the requested assets"
+ sendAssets = []
+ sendAssets.addAll(agentAssets)
+ sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).filter{!agentAssetIds.contains(it)}.map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))
+ readAssetsReplyEvent = new AssetsEvent(
+ readAssetsEvent.name,
+ sendAssets
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))
+
+ then: "the central manager should have added the first batch of assets under the gateway asset"
+ conditions.eventually {
+ def syncedAssets = assetStorageService.findAll(new AssetQuery().parents(gateway.getId()).recursive(true))
+ assert syncedAssets.size() == sendAssets.size()
+ assert syncedAssets.stream().filter{syncedAsset -> sendAssets.stream().anyMatch{it.id == syncedAsset.id}}.count() == sendAssets.size()
+ assert syncedAssets.find {it.id == agentAssetIds[0]}.getName() == "Test Agent 1"
+ assert syncedAssets.find {it.id == agentAssetIds[0]}.getType() == AssetType.AGENT.type
+ assert syncedAssets.find {it.id == agentAssetIds[0]}.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert syncedAssets.find {it.id == agentAssetIds[0]}.getParentId() == gateway.getId()
+ assert syncedAssets.find {it.id == agentAssetIds[4]}.getName() == "Test Agent 5"
+ assert syncedAssets.find {it.id == agentAssetIds[4]}.getType() == AssetType.AGENT.type
+ assert syncedAssets.find {it.id == agentAssetIds[4]}.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert syncedAssets.find {it.id == agentAssetIds[4]}.getParentId() == gateway.getId()
+ assert syncedAssets.find {it.id == assetIds[0]}.getName() == "Test Building 1"
+ assert syncedAssets.find {it.id == assetIds[0]}.getType() == AssetType.BUILDING.type
+ assert syncedAssets.find {it.id == assetIds[0]}.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert syncedAssets.find {it.id == assetIds[0]}.getParentId() == gateway.getId()
+ assert syncedAssets.find {it.id == assetIds[0]}.getAttribute(AttributeType.LOCATION).flatMap {GeoJSONPoint.fromValue(it.getValue().orElse(null))}.get().x == 10
+ assert syncedAssets.find {it.id == assetIds[0]}.getAttribute(AttributeType.LOCATION).flatMap {GeoJSONPoint.fromValue(it.getValue().orElse(null))}.get().y == 11
+ assert syncedAssets.find {it.id == assetIds[0]}.getAttribute(AttributeType.LOCATION).flatMap{it.getMetaItem(MetaItemType.ACCESS_PUBLIC_READ)}.isPresent()
+ assert syncedAssets.find {it.id == assetIds[0]}.getAttribute(AttributeType.SURFACE_AREA).flatMap {it.getValueAsNumber()}.orElse(0d) == 1000
+ assert syncedAssets.find {it.id == assetIds[1]}.getName() == "Test Building 1 Room 1"
+ assert syncedAssets.find {it.id == assetIds[1]}.getType() == AssetType.ROOM.type
+ assert syncedAssets.find {it.id == assetIds[1]}.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert syncedAssets.find {it.id == assetIds[1]}.getParentId() == assetIds[0]
+ assert syncedAssets.find {it.id == assetIds[1]}.getAttribute("temp").map{it.hasMetaItem(MetaItemType.AGENT_LINK)}.orElse(false)
+ assert syncedAssets.find {it.id == assetIds[1]}.getAttribute("tempSetpoint").map{it.hasMetaItem(MetaItemType.AGENT_LINK)}.orElse(false)
+ assert syncedAssets.find {it.id == assetIds[5]}.getName() == "Test Building 2"
+ assert syncedAssets.find {it.id == assetIds[5]}.getType() == AssetType.BUILDING.type
+ assert syncedAssets.find {it.id == assetIds[5]}.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert syncedAssets.find {it.id == assetIds[5]}.getParentId() == gateway.getId()
+ assert syncedAssets.find {it.id == assetIds[5]}.getAttribute(AttributeType.LOCATION).flatMap {GeoJSONPoint.fromValue(it.getValue().orElse(null))}.get().x == 10
+ assert syncedAssets.find {it.id == assetIds[5]}.getAttribute(AttributeType.LOCATION).flatMap {GeoJSONPoint.fromValue(it.getValue().orElse(null))}.get().y == 11
+ assert syncedAssets.find {it.id == assetIds[5]}.getAttribute(AttributeType.LOCATION).flatMap{it.getMetaItem(MetaItemType.ACCESS_PUBLIC_READ)}.isPresent()
+ assert syncedAssets.find {it.id == assetIds[5]}.getAttribute(AttributeType.SURFACE_AREA).flatMap {it.getValueAsNumber()}.orElse(0d) == 1000
+ assert syncedAssets.find {it.id == assetIds[6]}.getName() == "Test Building 2 Room 1"
+ assert syncedAssets.find {it.id == assetIds[6]}.getType() == AssetType.ROOM.type
+ assert syncedAssets.find {it.id == assetIds[6]}.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert syncedAssets.find {it.id == assetIds[6]}.getParentId() == assetIds[5]
+ assert syncedAssets.find {it.id == assetIds[6]}.getAttribute("temp").map{it.hasMetaItem(MetaItemType.AGENT_LINK)}.orElse(false)
+ assert syncedAssets.find {it.id == assetIds[6]}.getAttribute("tempSetpoint").map{it.hasMetaItem(MetaItemType.AGENT_LINK)}.orElse(false)
+ }
+
+ and: "the central manager should have requested the full loading of the second batch of assets"
+ conditions.eventually {
+ assert clientReceivedMessages.size() == 2
+ assert clientReceivedMessages.get(1).startsWith(SharedEvent.MESSAGE_PREFIX)
+ assert clientReceivedMessages.get(1).contains("read-assets")
+ readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[1].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)
+ assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + GatewayConnector.SYNC_ASSET_BATCH_SIZE
+ assert readAssetsEvent.assetQuery != null
+ assert readAssetsEvent.assetQuery.select.excludePath
+ assert readAssetsEvent.assetQuery.select.excludeParentInfo
+ assert readAssetsEvent.assetQuery.ids != null
+ assert readAssetsEvent.assetQuery.ids.length == agentAssetIds.size() + assetIds.size() - GatewayConnector.SYNC_ASSET_BATCH_SIZE
+ assert assetIds.stream().filter{id -> sendAssets.stream().noneMatch{it.id == id}}.count() == readAssetsEvent.assetQuery.ids.length
+ }
+
+ when: "the gateway returns the requested assets"
+ sendAssets = []
+ sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))
+ readAssetsReplyEvent = new AssetsEvent(
+ readAssetsEvent.name,
+ sendAssets
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))
+
+ then: "the gateway asset status should become connected"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getAttribute("status").flatMap{it.getValueAsString()}.orElse(null) == ConnectionStatus.CONNECTED.name()
+ }
+
+ and: "all the gateway assets should be replicated underneath the gateway"
+ assert assetStorageService.findAll(new AssetQuery().parents(gateway.getId()).recursive(true)).size() == agentAssets.size() + assets.size()
+
+ and: "the http client protocol of the gateway agents should not have been linked to the central manager"
+ Thread.sleep(2000)
+ conditions.eventually {
+ assert !httpClientProtocol.clientMap.containsKey(new AttributeRef(agentAssetIds[0], "protocolConfig"))
+ assert !httpClientProtocol.clientMap.containsKey(new AttributeRef(agentAssetIds[4], "protocolConfig"))
+ }
+
+ when: "the previously received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "an attribute event for a gateway descendant asset (building 1 room 1) is sent to the local manager"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(assetIds[1], "tempSetpoint", Values.create(20d)))
+
+ then: "the event should have been forwarded to the gateway"
+ conditions.eventually {
+ assert clientReceivedMessages.size() == 1
+ assert clientReceivedMessages.get(0).startsWith(SharedEvent.MESSAGE_PREFIX)
+ assert clientReceivedMessages.get(0).contains("attribute")
+ }
+
+ when: "the gateway handles the forwarded attribute event and sends a follow up attribute event to the local manager"
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AttributeEvent(assetIds[1], "tempSetpoint", Values.create(20d))))
+
+ then: "the descendant asset in the local manager should contain the new attribute value"
+ conditions.eventually {
+ def building1Room1Asset = assetStorageService.find(assetIds[1])
+ assert building1Room1Asset.getAttribute("tempSetpoint").flatMap {it.getValueAsNumber()}.orElse(0d) == 20d
+ }
+
+ when: "an asset is added on the gateway and the local manager is notified"
+ def building1Room5AssetId = UniqueIdentifierGenerator.generateId("Test Building 1 Room 5")
+ def building1Room5Asset = new Asset(
+ building1Room5AssetId,
+ 0L,
+ new Date(),
+ "Test Building 1 Room 5",
+ AssetType.ROOM.type,
+ false,
+ assetIds[0],
+ (String)null,
+ (String)null,
+ "master",
+ (String[])[building1Room5AssetId, assetIds[0]].toArray(new String[0]),
+ null).addAttributes(
+ new AssetAttribute(AttributeType.LOCATION, new GeoJSONPoint(10,11).toValue(), new Date().getTime())
+ .addMeta(
+ MetaItemType.ACCESS_PUBLIC_READ
+ ),
+ new AssetAttribute("temp", AttributeValueType.TEMPERATURE, null, new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[0], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ ),
+ new AssetAttribute("tempSetpoint", AttributeValueType.TEMPERATURE, null, new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[0], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ )
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.CREATE, building1Room5Asset, null)))
+
+ then: "the asset should be replicated in the local manager"
+ conditions.eventually {
+ building1Room5Asset = assetStorageService.find(building1Room5AssetId)
+ assert building1Room5Asset != null
+ assert building1Room5Asset.getName() == "Test Building 1 Room 5"
+ assert building1Room5Asset.getType() == AssetType.ROOM.type
+ assert building1Room5Asset.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert building1Room5Asset.getParentId() == assetIds[0]
+ assert building1Room5Asset.getAttributesList().size() == 3
+ }
+
+ when: "an asset is modified on the gateway and the local manager is notified"
+ building1Room5Asset.setName("Test Building 1 Room 5 Updated")
+ building1Room5Asset.setVersion(1)
+ building1Room5Asset.addAttributes(
+ new AssetAttribute("co2Level", AttributeValueType.CO2, Values.create(500), new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[0], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ )
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.UPDATE, building1Room5Asset, (String[]) ["name", "attributes"].toArray(new String[0]))))
+
+ then: "the asset should also be updated in the local manager"
+ conditions.eventually {
+ building1Room5Asset = assetStorageService.find(building1Room5AssetId)
+ assert building1Room5Asset != null
+ assert building1Room5Asset.getVersion() == 1
+ assert building1Room5Asset.getName() == "Test Building 1 Room 5 Updated"
+ assert building1Room5Asset.getType() == AssetType.ROOM.type
+ assert building1Room5Asset.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert building1Room5Asset.getParentId() == assetIds[0]
+ assert building1Room5Asset.getAttributesList().size() == 4
+ assert building1Room5Asset.getAttribute("co2Level").flatMap{it.getValueAsNumber()}.orElse(0d) == 500d
+ }
+
+ when: "an asset is deleted on the gateway and the local manager is notified"
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.DELETE, building1Room5Asset, null)))
+
+ then: "the asset should also be deleted in the local manager"
+ conditions.eventually {
+ def asset = assetStorageService.find(building1Room5AssetId)
+ assert asset == null
+ }
+
+ when: "the client received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "an asset is added under the gateway in the local manager and the gateway responds that it successfully added the asset"
+ def responseFuture = new AtomicReference<ScheduledFuture>()
+ responseFuture.set(executorService.scheduleAtFixedRate({
+ if (!clientReceivedMessages.isEmpty()) {
+ def assetAddEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), AssetEvent.class)
+ if (assetAddEvent.cause == AssetEvent.Cause.CREATE && assetAddEvent.asset.id.equals(building1Room5AssetId)) {
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.CREATE, building1Room5Asset, null)))
+ responseFuture.get().cancel(false)
+ }
+ }
+ }, 100, 100))
+ building1Room5Asset = assetStorageService.merge(building1Room5Asset)
+
+ then: "the asset should have been added to the gateway and eventually replicated in the local manager"
+ assert building1Room5Asset != null
+ assert assetStorageService.find(building1Room5AssetId).version == building1Room5Asset.version
+
+ when: "the client received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "an asset is modified under the gateway in the local manager and the gateway responds that it successfully updated the asset"
+ building1Room5Asset.setName("Test Building 1 Room 5")
+ building1Room5Asset.removeAttribute("co2Level")
+ responseFuture.set(executorService.scheduleAtFixedRate({
+ if (!clientReceivedMessages.isEmpty()) {
+ def assetAddEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), AssetEvent.class)
+ if (assetAddEvent.cause == AssetEvent.Cause.UPDATE && assetAddEvent.asset.id.equals(building1Room5AssetId)) {
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.UPDATE, building1Room5Asset, null)))
+ responseFuture.get().cancel(false)
+ }
+ }
+ }, 100, 100))
+ building1Room5Asset = assetStorageService.merge(building1Room5Asset)
+
+ then: "the asset should also be updated in the local manager"
+ assert building1Room5Asset != null
+ assert building1Room5Asset.getVersion() == 2
+ assert building1Room5Asset.getName() == "Test Building 1 Room 5"
+ assert building1Room5Asset.getType() == AssetType.ROOM.type
+ assert building1Room5Asset.getRealm() == managerDemoSetup.realmBuildingTenant
+ assert building1Room5Asset.getParentId() == assetIds[0]
+ assert building1Room5Asset.getAttributesList().size() == 3
+ assert assetStorageService.find(building1Room5AssetId).version == building1Room5Asset.version
+
+ when: "the client received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "an asset is deleted under the gateway in the local manager and the gateway responds that it successfully deleted the asset"
+ responseFuture.set(executorService.scheduleAtFixedRate({
+ if (!clientReceivedMessages.isEmpty()) {
+ def deleteRequest = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), DeleteAssetsRequestEvent.class)
+ if (deleteRequest.assetIds.size() == 1 && deleteRequest.assetIds.get(0) == building1Room5AssetId) {
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new DeleteAssetsResponseEvent(deleteRequest.name, true)))
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.DELETE, building1Room5Asset, null)))
+ responseFuture.get().cancel(false)
+ }
+ }
+ }, 100, 100))
+ def deleted = assetStorageService.delete([building1Room5AssetId])
+
+ then: "the asset should have been deleted"
+ assert deleted
+ conditions.eventually {
+ assert assetStorageService.find(building1Room5AssetId) == null
+ }
+
+ when: "the gateway asset is marked as disabled"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gateway.getId(), "disabled", Values.create(true)))
+
+ then: "the gateway asset status should become disabled"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId(), true)
+ assert gateway.getAttribute("status").flatMap{it.getValueAsString()}.orElse("") == ConnectionStatus.DISABLED.name()
+ }
+
+ and: "the gateway connector should be marked as disconnected and the gateway client should have been disconnected"
+ conditions.eventually {
+ assert gatewayService.gatewayConnectorMap.get(gateway.getId()).disabled
+ assert !gatewayService.gatewayConnectorMap.get(gateway.getId()).connected
+ assert gatewayClient.connectionStatus == ConnectionStatus.WAITING
+ }
+ gatewayClient.disconnect()
+
+ when: "an attempt is made to add a descendant asset to a gateway that isn't connected"
+ def failedAsset = new Asset(
+ UniqueIdentifierGenerator.generateId("Failed asset"),
+ 0L,
+ new Date(),
+ "Failed Asset",
+ AssetType.BUILDING.type,
+ false,
+ gateway.id,
+ (String)null,
+ (String)null,
+ managerDemoSetup.realmBuildingTenant,
+ (String[])[UniqueIdentifierGenerator.generateId("Failed asset")].toArray(new String[0]),
+ null).addAttributes(
+ new AssetAttribute(AttributeType.LOCATION, new GeoJSONPoint(10,11).toValue(), new Date().getTime())
+ .addMeta(
+ MetaItemType.ACCESS_PUBLIC_READ
+ ),
+ new AssetAttribute(AttributeType.SURFACE_AREA, Values.create(1000), new Date().getTime())
+ )
+ assetStorageService.merge(failedAsset)
+
+ then: "an error should occur"
+ thrown(IllegalStateException)
+
+ when: "gateway assets are modified whilst the gateway is disconnected (building1Room5 re-added, building5 and descendants removed and building1 modified and building1Room1 attribute updated)"
+ assets[4].setName("Test Building 1 Updated")
+ assets[4].setVersion(2L)
+ assets[0].getAttribute("temp").ifPresent{it.setValue(Values.create(10), new Date().getTime())}
+ assets = assets.subList(0, 20)
+
+ and: "the client received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "the gateway is enabled again"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(gateway.getId(), "disabled", Values.create(false)))
+
+ then: "the gateway connector should be enabled"
+ conditions.eventually {
+ assert !gatewayService.gatewayConnectorMap.get(gateway.getId()).disabled
+ }
+
+ and: "the gateway client reconnects"
+ gatewayClient.connect()
+
+ then: "the gateway netty client status should become CONNECTED"
+ conditions.eventually {
+ assert connectionStatus == ConnectionStatus.CONNECTED
+ assert gatewayClient.connectionStatus == ConnectionStatus.CONNECTED
+ }
+
+ and: "the gateway asset connection status should be CONNECTING"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getAttribute("status").flatMap{it.getValueAsString()}.orElse(null) == ConnectionStatus.CONNECTING.name()
+ }
+
+ and: "the local manager should have sent a CONNECTED message and an asset read request"
+ conditions.eventually {
+ assert clientReceivedMessages.size() >= 2
+ assert clientReceivedMessages[0] == "CONNECTED"
+ assert clientReceivedMessages[1].startsWith(SharedEvent.MESSAGE_PREFIX)
+ assert clientReceivedMessages[1].contains("read-assets")
+ }
+
+ when: "the previously received messages are cleared"
+ clientReceivedMessages.clear()
+
+ and: "the gateway client replies to the central manager with the assets of the gateway"
+ sendAssets = [building1Room5Asset]
+ sendAssets.addAll(agentAssets)
+ sendAssets.addAll(assets)
+ readAssetsReplyEvent = new AssetsEvent(
+ GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL,
+ sendAssets
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))
+
+ then: "the central manager should have requested the full loading of the first batch of assets"
+ conditions.eventually {
+ assert clientReceivedMessages.size() == 1
+ assert clientReceivedMessages.get(0).startsWith(SharedEvent.MESSAGE_PREFIX)
+ assert clientReceivedMessages.get(0).contains("read-assets")
+ readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[0].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)
+ assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + "0"
+ assert readAssetsEvent.assetQuery != null
+ assert readAssetsEvent.assetQuery.select.excludePath
+ assert readAssetsEvent.assetQuery.select.excludeParentInfo
+ assert readAssetsEvent.assetQuery.ids != null
+ assert readAssetsEvent.assetQuery.ids.length == GatewayConnector.SYNC_ASSET_BATCH_SIZE
+ assert agentAssetIds.stream().filter{readAssetsEvent.assetQuery.ids.contains(it)}.count() == agentAssetIds.size()
+ assert assetIds.stream().filter{readAssetsEvent.assetQuery.ids.contains(it)}.count() + 1 == GatewayConnector.SYNC_ASSET_BATCH_SIZE - agentAssetIds.size()
+ }
+
+ when: "another asset is added to the gateway during the initial sync process"
+ def building2Room5Asset = new Asset(
+ UniqueIdentifierGenerator.generateId("Test Building 2 Room 5"),
+ 0L,
+ new Date(),
+ "Test Building 2 Room 5",
+ AssetType.ROOM.type,
+ false,
+ assetIds[5],
+ (String)null,
+ (String)null,
+ "master",
+ (String[])[UniqueIdentifierGenerator.generateId("Test Building 2 Room 5"), assetIds[5]].toArray(new String[0]),
+ null).addAttributes(
+ new AssetAttribute(AttributeType.LOCATION, new GeoJSONPoint(10,11).toValue(), new Date().getTime())
+ .addMeta(
+ MetaItemType.ACCESS_PUBLIC_READ
+ ),
+ new AssetAttribute("temp", AttributeValueType.TEMPERATURE, null, new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[1], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ ),
+ new AssetAttribute("tempSetpoint", AttributeValueType.TEMPERATURE, null, new Date().getTime())
+ .addMeta(
+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agentAssetIds[1], "protocolConfig").toArrayValue()),
+ new MetaItem(HttpClientProtocol.META_ATTRIBUTE_PATH, Values.create(""))
+ )
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.CREATE, building2Room5Asset, null)))
+
+ and: "another asset is deleted from the gateway during the initial sync process (Building 3 Room 1)"
+ def removedAsset = assets.remove(10)
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(new AssetEvent(AssetEvent.Cause.DELETE, removedAsset, null)))
+
+ and: "the gateway returns the requested assets (minus the deleted Building 3 Room 1 asset)"
+ sendAssets = [building1Room5Asset]
+ sendAssets.addAll(agentAssets)
+ sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).filter{!agentAssetIds.contains(it) && it != removedAsset.id && it != building1Room5Asset.id}.map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))
+ readAssetsReplyEvent = new AssetsEvent(
+ readAssetsEvent.name,
+ sendAssets
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))
+
+ then: "the central manager should have requested the full loading of the second batch of assets"
+ conditions.eventually {
+ assert clientReceivedMessages.size() == 2
+ assert clientReceivedMessages.get(1).startsWith(SharedEvent.MESSAGE_PREFIX)
+ assert clientReceivedMessages.get(1).contains("read-assets")
+ readAssetsEvent = Container.JSON.readValue(clientReceivedMessages[1].substring(SharedEvent.MESSAGE_PREFIX.length()), ReadAssetsEvent.class)
+ assert readAssetsEvent.name == GatewayConnector.ASSET_READ_EVENT_NAME_BATCH + (GatewayConnector.SYNC_ASSET_BATCH_SIZE - 1)
+ assert readAssetsEvent.assetQuery != null
+ assert readAssetsEvent.assetQuery.select.excludePath
+ assert readAssetsEvent.assetQuery.select.excludeParentInfo
+ assert readAssetsEvent.assetQuery.ids != null
+ assert readAssetsEvent.assetQuery.ids.length == agentAssetIds.size() + assets.size() + 1 - GatewayConnector.SYNC_ASSET_BATCH_SIZE + 1
+ assert assets.stream().filter{asset -> sendAssets.stream().noneMatch{it.id == asset.id}}.count() == readAssetsEvent.assetQuery.ids.length
+ }
+
+ when: "the gateway returns the requested assets"
+ sendAssets = []
+ sendAssets.addAll(Arrays.stream(readAssetsEvent.assetQuery.ids).map{id -> assets.stream().filter{asset -> asset.id == id}.findFirst().orElse(null)}.collect(Collectors.toList()))
+ readAssetsReplyEvent = new AssetsEvent(
+ readAssetsEvent.name,
+ sendAssets
+ )
+ gatewayClient.sendMessage(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(readAssetsReplyEvent))
+
+ then: "the gateway asset status should become connected"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getAttribute("status").flatMap{it.getValueAsString()}.orElse(null) == ConnectionStatus.CONNECTED.name()
+ }
+
+ and: "the gateway should have the correct assets"
+ def gatewayAssets = assetStorageService.findAll(new AssetQuery().parents(gateway.getId()).recursive(true))
+ assert gatewayAssets.size() == 2 + agentAssets.size() + assets.size()
+
+ when: "the gateway asset is deleted"
+ deleted = assetStorageService.delete([gateway.id])
+ clientsResource = identityProvider.getRealms(new ClientRequestInfo(null, identityProvider.getAdminAccessToken(null))).realm(managerDemoSetup.realmBuildingTenant).clients()
+
+ then: "all descendant assets should have been removed"
+ conditions.eventually {
+ assert assetStorageService.find(assets[0].id) == null
+ }
+
+ then: "the keycloak client should also be removed"
+ assert deleted
+ conditions.eventually {
+ assert clientsResource.findByClientId(GatewayService.GATEWAY_CLIENT_ID_PREFIX + gateway.getId()).isEmpty()
+ }
+
+ cleanup: "the server should be stopped"
+ stopContainer(container)
+ }
+
+ def "Verify gateway client service"() {
+
+ given: "a spy gateway client service"
+ // This spy maps asset IDs to fake IDs to allow both the client and central manager to actually be the same
+ // manager instance
+ Map<String, String> realIdToFakeIdMap = new HashMap<>()
+ Map<String, String> fakeIdToRealIdMap = new HashMap<>()
+ GatewayClientService spyGatewayClientService = Spy(GatewayClientService) {
+ messageFromSharedEvent(_) >> {
+ SharedEvent event ->
+ if (event instanceof AssetsEvent) {
+ if (GatewayConnector.ASSET_READ_EVENT_NAME_INITIAL.equals(event.getName())) {
+ // Store real IDs and map to a fake IDs
+ event.getAssets().forEach {
+ String oldId = it.id
+ it.id = UniqueIdentifierGenerator.generateId()
+ realIdToFakeIdMap.put(oldId, it.id)
+ fakeIdToRealIdMap.put(it.id, oldId)
+ }
+ } else {
+ event.getAssets().forEach {
+ it.id = realIdToFakeIdMap.get(it.id)
+ it.parentId = it.parentId == null ? null : realIdToFakeIdMap.get(it.parentId)
+ }
+ }
+ } else if (event instanceof AssetEvent) {
+ if (!realIdToFakeIdMap.containsKey(event.asset.id)) {
+ def fakeId = UniqueIdentifierGenerator.generateId()
+ realIdToFakeIdMap.put(event.asset.id, fakeId)
+ fakeIdToRealIdMap.put(fakeId, event.asset.id)
+ }
+ event.asset.id = realIdToFakeIdMap.get(event.entityId)
+ event.asset.parentId = event.parentId == null ? null : realIdToFakeIdMap.get(event.parentId)
+ } else if (event instanceof AttributeEvent) {
+ event.attributeState = new AttributeState(
+ realIdToFakeIdMap.get(event.entityId),
+ event.attributeState.attributeRef.attributeName,
+ event.attributeState.value.orElse(null)
+ )
+ event.parentId = event.parentId == null ? null : realIdToFakeIdMap.get(event.parentId)
+ }
+
+ return callRealMethodWithArgs(event)
+ }
+
+ messageToSharedEvent(_) >> {
+ String message ->
+ SharedEvent event = callRealMethod()
+ if (event instanceof ReadAssetsEvent) {
+ if (event.assetQuery.ids != null && event.assetQuery.ids.length > 0) {
+ event.assetQuery.ids(Arrays.stream(event.assetQuery.ids).map{fakeIdToRealIdMap.get(it)}.toArray{new String[it]})
+ }
+ }
+ if (event instanceof AssetEvent) {
+ if (fakeIdToRealIdMap.containsKey(event.entityId)) {
+ event.asset.id = fakeIdToRealIdMap.get(event.entityId)
+ } else {
+ // This is a new asset creation originating from the central manager
+ def id = UniqueIdentifierGenerator.generateId()
+ realIdToFakeIdMap.put(id, event.entityId)
+ fakeIdToRealIdMap.put(event.entityId, id)
+ }
+ event.asset.id = fakeIdToRealIdMap.get(event.entityId)
+ event.asset.parentId = event.parentId == null ? null : fakeIdToRealIdMap.get(event.parentId)
+ } else if (event instanceof AttributeEvent) {
+ event.attributeState = new AttributeState(
+ fakeIdToRealIdMap.get(event.entityId),
+ event.attributeState.attributeRef.attributeName,
+ event.attributeState.value.orElse(null)
+ )
+ event.parentId = event.parentId == null ? null : fakeIdToRealIdMap.get(event.parentId)
+ } else if (event instanceof DeleteAssetsRequestEvent) {
+ event.assetIds = event.assetIds.stream().map{fakeIdToRealIdMap.get(it)}.collect(Collectors.toList())
+ }
+ return event
+ }
+ }
+
+ and: "the container environment is started with the spy gateway client service"
+ def conditions = new PollingConditions(timeout: 10, delay: 1)
+ def serverPort = findEphemeralPort()
+ def services = Lists.newArrayList(defaultServices())
+ services.replaceAll{it instanceof GatewayClientService ? spyGatewayClientService : it}
+ def container = startContainer(defaultConfig(serverPort), services)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def gatewayService = container.getService(GatewayService.class)
+ def gatewayClientService = container.getService(GatewayClientService.class)
+ def simulatorProtocol = container.getService(SimulatorProtocol.class)
+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
+
+ and: "an authenticated admin user"
+ def accessToken = authenticate(
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ MASTER_REALM_ADMIN_USER,
+ getString(container.getConfig(), SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)
+ ).token
+
+ and: "the gateway client resource"
+ def gatewayClientResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(GatewayClientResource.class)
+
+ expect: "the container should be running and initialised"
+ conditions.eventually {
+ assert container.isRunning()
+ assert noEventProcessedIn(assetProcessingService, 300)
+ }
+
+ when: "a gateway is provisioned in this manager in the building realm"
+ def gateway = assetStorageService.merge(new Asset("Test gateway", AssetType.GATEWAY, null, managerDemoSetup.realmBuildingTenant))
+
+ then: "a set of credentials should have been created for this gateway and be stored against the gateway for easy reference"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId(), true)
+ assert gateway.getAttribute("clientId").isPresent()
+ assert !isNullOrEmpty(gateway.getAttribute("clientId").flatMap{it.getValueAsString()}.orElse(""))
+ assert !isNullOrEmpty(gateway.getAttribute("clientSecret").flatMap{it.getValueAsString()}.orElse(""))
+ }
+
+ and: "a gateway connector should have been created for this gateway"
+ conditions.eventually {
+ assert gatewayService.gatewayConnectorMap.size() == 1
+ assert gatewayService.gatewayConnectorMap.get(gateway.getId()).gatewayId == gateway.getId()
+ }
+
+ when: "a gateway client connection is created to connect the city realm to the gateway"
+ def gatewayConnection = new GatewayConnection(
+ "localhost",
+ serverPort,
+ managerDemoSetup.realmBuildingTenant,
+ gateway.getAttribute("clientId").flatMap{it.getValueAsString()}.orElse(""),
+ gateway.getAttribute("clientSecret").flatMap{it.getValueAsString()}.orElse(""),
+ false,
+ false
+ )
+ gatewayClientResource.setConnection(null, managerDemoSetup.realmCityTenant, gatewayConnection)
+
+ then: "the gateway client should become connected"
+ conditions.eventually {
+ assert gatewayClientService.clientRealmMap.get(managerDemoSetup.realmCityTenant) != null
+ }
+
+ and: "the gateway asset connection status should become CONNECTED"
+ conditions.eventually {
+ gateway = assetStorageService.find(gateway.getId())
+ assert gateway.getAttribute("status").flatMap{it.getValueAsString()}.orElse(null) == ConnectionStatus.CONNECTED.name()
+ }
+
+ and: "the fake mirrored assets should have been created under the gateway asset"
+ conditions.eventually {
+ def fakeAssets = assetStorageService.findAll(new AssetQuery().parents(gateway.id).recursive(true))
+ fakeAssets.size() == realIdToFakeIdMap.size()
+ }
+
+ when: "a gateway client asset is modified"
+ def microphone1 = assetStorageService.find(managerDemoSetup.microphone1Id)
+ microphone1.setName("Microphone 1 Updated")
+ microphone1.getAttribute("microphoneLevel").ifPresent{it.addMeta(MetaItemType.UNIT_TYPE.withInitialValue(UNITS_SOUND_DECIBELS))}
+ microphone1.addAttributes(
+ new AssetAttribute("test", AttributeValueType.DISTANCE, Values.create(100))
+ )
+ microphone1 = assetStorageService.merge(microphone1)
+
+ then: "the mirrored asset under the gateway should have also been updated"
+ conditions.eventually {
+ def mirroredMicrophone = assetStorageService.find(realIdToFakeIdMap.get(managerDemoSetup.microphone1Id))
+ assert mirroredMicrophone != null
+ assert mirroredMicrophone.getAttribute("microphoneLevel").flatMap{it.getMetaItem(MetaItemType.UNIT_TYPE)}.flatMap{it.getValueAsString()}.orElse("") == UNITS_SOUND_DECIBELS
+ assert mirroredMicrophone.getAttribute("test").isPresent()
+ assert mirroredMicrophone.getAttribute("test").flatMap{it.getValueAsNumber()}.orElse(0) == 100
+ }
+
+ when: "a gateway client asset is added"
+ def microphone2 = new Asset("Microphone 2", AssetType.MICROPHONE, null, managerDemoSetup.realmCityTenant).addAttributes(
+ new AssetAttribute("test", AttributeValueType.STRING, Values.create("testValue"))
+ )
+ microphone2.setParentId(managerDemoSetup.area1Id)
+ microphone2 = assetStorageService.merge(microphone2)
+
+ then: "the new asset should have been created in the gateway and also mirrored under the gateway asset"
+ assert microphone2.id != null
+ conditions.eventually {
+ def mirroredMicrophone2 = assetStorageService.find(realIdToFakeIdMap.get(microphone2.id))
+ assert mirroredMicrophone2 != null
+ assert mirroredMicrophone2.getAttribute("microphoneLevel").isPresent()
+ assert mirroredMicrophone2.getAttribute("test").flatMap{it.getValueAsString()}.orElse("") == "testValue"
+ }
+
+ when: "an asset is added under the gateway client"
+ def microphone3 = new Asset()
+ Asset.map(microphone1, microphone3, "Microphone 3", managerDemoSetup.realmBuildingTenant, realIdToFakeIdMap.get(managerDemoSetup.area1Id), null, null, null)
+ microphone3 = assetStorageService.merge(microphone3)
+
+ then: "the new asset should have been created in the gateway and also mirrored under the gateway asset"
+ assert microphone3.id != null
+ conditions.eventually {
+ def gatewayMicrophone3 = assetStorageService.find(fakeIdToRealIdMap.get(microphone3.id))
+ assert gatewayMicrophone3 != null
+ assert gatewayMicrophone3.getAttribute("microphoneLevel").isPresent()
+ }
+
+ and: "the new asset microphone level should be correctly linked to the simulator protocol only on the gateway"
+ conditions.eventually {
+ assert !simulatorProtocol.elements.containsKey(new AttributeRef(microphone3.id, "microphoneLevel"))
+ assert simulatorProtocol.elements.containsKey(new AttributeRef(fakeIdToRealIdMap.get(microphone3.id), "microphoneLevel"))
+ }
+
+ when: "an attribute is updated on the gateway client"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(microphone2.id, "test", Values.create("newValue")))
+
+ then: "the mirrored asset attribute should also be updated"
+ conditions.eventually {
+ def mirroredMicrophone2 = assetStorageService.find(realIdToFakeIdMap.get(microphone2.id))
+ assert mirroredMicrophone2 != null
+ assert mirroredMicrophone2.getAttribute("test").flatMap{it.getValueAsString()}.orElse("") == "newValue"
+ }
+
+ when: "a mirrored asset attribute is updated"
+ assetProcessingService.sendAttributeEvent(new AttributeEvent(realIdToFakeIdMap.get(microphone2.id), "test", Values.create("newerValue")))
+
+ then: "the attribute should be updated on the gateway client and the mirrored asset"
+ conditions.eventually {
+ microphone2 = assetStorageService.find(microphone2.id)
+ def mirroredMicrophone2 = assetStorageService.find(realIdToFakeIdMap.get(microphone2.id))
+ assert microphone2 != null
+ assert mirroredMicrophone2 != null
+ assert microphone2.getAttribute("test").flatMap{it.getValueAsString()}.orElse("") == "newerValue"
+ assert mirroredMicrophone2.getAttribute("test").flatMap{it.getValueAsString()}.orElse("") == "newerValue"
+ }
+
+ when: "a gateway client asset is deleted"
+ def deleted = assetStorageService.delete(Collections.singletonList(managerDemoSetup.microphone1Id))
+
+ then: "the client asset should have been deleted and also the mirrored asset under the gateway should also be deleted"
+ assert deleted
+ conditions.eventually {
+ assert assetStorageService.find(realIdToFakeIdMap.get(managerDemoSetup.microphone1Id)) == null
+ }
+
+ cleanup: "the server should be stopped"
+ stopContainer(container)
+ }
+}
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
index 64d97ca62..761ef79c8 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
@@ -5,6 +5,7 @@ import com.google.firebase.messaging.Message
import org.openremote.container.web.WebService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.asset.console.ConsoleResourceImpl
+import org.openremote.manager.gateway.GatewayClientService
import org.openremote.manager.notification.EmailNotificationHandler
import org.openremote.manager.notification.NotificationService
import org.openremote.manager.notification.PushNotificationHandler
@@ -69,8 +70,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
and: "the container environment is started with the mock handler"
def serverPort = findEphemeralPort()
def services = Lists.newArrayList(defaultServices())
- services.removeIf {it instanceof PushNotificationHandler}
- services.add(mockPushNotificationHandler)
+ services.replaceAll{it instanceof PushNotificationHandler ? mockPushNotificationHandler : it}
def container = startContainerWithPseudoClock(defaultConfig(serverPort), services)
def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
@@ -562,8 +562,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
and: "the container environment is started with the mock handler"
def serverPort = findEphemeralPort()
def services = Lists.newArrayList(defaultServices())
- services.removeIf {it instanceof EmailNotificationHandler}
- services.add(mockEmailNotificationHandler)
+ services.replaceAll{it instanceof EmailNotificationHandler ? mockEmailNotificationHandler : it}
def conditions = new PollingConditions(timeout: 10)
def container = startContainer(defaultConfig(serverPort), services)
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
diff --git a/test/src/test/groovy/org/openremote/test/protocol/controller/ControllerProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/controller/ControllerProtocolTest.groovy
index 53fc5281f..658c7f53c 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/controller/ControllerProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/controller/ControllerProtocolTest.groovy
@@ -144,7 +144,7 @@ class ControllerProtocolTest extends Specification implements ManagerContainerTr
String bodyStr = (String)requestContext.getEntity()
ObjectValue body = Values.<ObjectValue>parse(bodyStr).orElse(null)
- if (body.get("parameter").isPresent() && body.get("parameter").orElse(null).toString() == "a_parameter") {
+ if (body != null && body.get("parameter").isPresent() && body.get("parameter").orElse(null).toString() == "a_parameter") {
requestContext.abortWith(Response.ok().build())
commandCount++
return
@@ -170,7 +170,7 @@ class ControllerProtocolTest extends Specification implements ManagerContainerTr
def "Check Controller connection"() {
given: "expected conditions"
- def conditions = new PollingConditions(timeout: 10, initialDelay: 1)
+ def conditions = new PollingConditions(timeout: 20, initialDelay: 1)
when: "the container starts"
def serverPort = findEphemeralPort()
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
index 802ef1a72..d2ba25f12 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy
@@ -1,5 +1,5 @@
/*
- * Copyright 2017, OpenRemote Inc.
+ * Copyright 2019, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
@@ -124,7 +124,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
assert container.isRunning()
}
- when: "the web target builder is configured to use the mock server"
+ when: "the web target builder is configured to use the mock HTTP server (to test subscriptions)"
if (!websocketClientProtocol.client.configuration.isRegistered(mockServer)) {
websocketClientProtocol.client.register(mockServer, Integer.MAX_VALUE)
}
@@ -157,7 +157,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai
new WebsocketSubscription().body(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new EventSubscription(
AttributeEvent.class,
- new AssetEvent.AssetIdFilter<AttributeEvent>(managerDemoSetup.apartment1LivingroomId),
+ new AssetFilter<AttributeEvent>().setAssetIds(managerDemoSetup.apartment1LivingroomId),
"1",
null))),
new WebsocketHttpSubscription()
diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
index 83c4e540e..d5e187b71 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy
@@ -29,7 +29,7 @@ import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.concurrent.ManagerExecutorService
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.builtin.ManagerDemoSetup
-import org.openremote.model.asset.AssetEvent
+import org.openremote.model.asset.AssetFilter
import org.openremote.model.asset.agent.ConnectionStatus
import org.openremote.model.attribute.AttributeEvent
import org.openremote.model.event.TriggeredEventSubscription
@@ -107,7 +107,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTrait
client.sendMessage(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX + Container.JSON.writeValueAsString(
new EventSubscription(
AttributeEvent.class,
- new AssetEvent.AssetIdFilter<AttributeEvent>(managerDemoSetup.apartment1LivingroomId),
+ new AssetFilter<AttributeEvent>().setAssetIds(managerDemoSetup.apartment1LivingroomId),
"1",
null)))
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 50ded5f06..ab9d9e687 100644
--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
@@ -313,7 +313,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainer
when: "the Kitchen room asset is deleted"
rulesImport.resetRulesFired()
- assetStorageService.delete([asset.getId()])
+ assetStorageService.delete([asset.getId()], false)
then: "the facts should be removed from the rule engines and rules should have fired"
conditions.eventually {
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 83e20f82c..fc9b54a02 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
@@ -101,10 +101,8 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
def conditions = new PollingConditions(timeout: 15, delay: 1)
def serverPort = findEphemeralPort()
def services = Lists.newArrayList(defaultServices())
- services.removeIf {it instanceof PushNotificationHandler}
- services.removeIf {it instanceof EmailNotificationHandler}
- services.add(mockPushNotificationHandler)
- services.add(mockEmailNotificationHandler)
+ services.replaceAll{it instanceof PushNotificationHandler ? mockPushNotificationHandler : it}
+ services.replaceAll{it instanceof EmailNotificationHandler ? mockEmailNotificationHandler : it}
def container = startContainerWithPseudoClock(defaultConfig(serverPort), services)
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
def keycloakDemoSetup = container.getService(SetupService.class).getTaskOfType(KeycloakDemoSetup.class)
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 3bbcfa74b..476fa6199 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
@@ -68,8 +68,7 @@ class ResidenceNotifyAlarmTriggerTest extends Specification implements ManagerCo
def conditions = new PollingConditions(timeout: 20, delay: 1)
def serverPort = findEphemeralPort()
def services = Lists.newArrayList(defaultServices())
- services.removeIf { it instanceof PushNotificationHandler }
- services.add(mockPushNotificationHandler)
+ services.replaceAll{it instanceof PushNotificationHandler ? mockPushNotificationHandler : it}
def container = startContainerWithPseudoClock(defaultConfig(serverPort), services)
def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)
def rulesService = container.getService(RulesService.class)
diff --git a/ui/app/main/src/actions/map.ts b/ui/app/main/src/actions/map.ts
index 24e28522f..32866fb9c 100644
--- a/ui/app/main/src/actions/map.ts
+++ b/ui/app/main/src/actions/map.ts
@@ -40,7 +40,6 @@ export const subscribeAssets: ActionCreator<ThunkResult> = () => async (dispatch
const result = await manager.rest.api.AssetResource.queryAssets({
select: {
excludeAttributes: true,
- excludeRealm: true,
excludeParentInfo: true,
excludePath: true
}
diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.ts
index c66137c31..6ad44c32d 100644
--- a/ui/component/core/src/event.ts
+++ b/ui/component/core/src/event.ts
@@ -224,7 +224,7 @@ abstract class EventProviderImpl implements EventProvider {
if (assetIds && assetIds.length > 0) {
subscription.filter = {
- filterType: "asset-id",
+ filterType: "asset",
assetIds: ids
};
}
@@ -269,7 +269,7 @@ abstract class EventProviderImpl implements EventProvider {
let subscription: EventSubscription<AttributeEvent> = {
eventType: "attribute",
filter: {
- filterType: "asset-id",
+ filterType: "asset",
assetIds: assetIds
}
};
diff --git a/ui/component/or-asset-tree/src/index.ts b/ui/component/or-asset-tree/src/index.ts
index db2cd33c6..d9bd89268 100644
--- a/ui/component/or-asset-tree/src/index.ts
+++ b/ui/component/or-asset-tree/src/index.ts
@@ -502,7 +502,6 @@ export class OrAssetTree extends LitElement {
},
select: {
excludePath: true,
- excludeRealm: true,
excludeParentInfo: false
}
};
diff --git a/ui/component/or-asset-viewer/src/index.ts b/ui/component/or-asset-viewer/src/index.ts
index d40165821..e297732f0 100644
--- a/ui/component/or-asset-viewer/src/index.ts
+++ b/ui/component/or-asset-viewer/src/index.ts
@@ -658,8 +658,7 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem
const response = await manager.rest.api.AssetResource.queryAssets({
select: {
excludePath: true,
- excludeParentInfo: true,
- excludeRealm: true
+ excludeParentInfo: true
},
ids: ids
});
diff --git a/ui/component/or-attribute-history/src/index.ts b/ui/component/or-attribute-history/src/index.ts
index 7e0bdb1d7..21abeb271 100644
--- a/ui/component/or-attribute-history/src/index.ts
+++ b/ui/component/or-attribute-history/src/index.ts
@@ -680,7 +680,6 @@ export class OrAttributeHistory extends translate(i18next)(LitElement) {
ids: [assetId],
select: {
excludeParentInfo: true,
- excludeRealm: true,
excludePath: true,
excludeAttributeMeta: true,
attributes: [
diff --git a/ui/component/or-attribute-input/src/index.ts b/ui/component/or-attribute-input/src/index.ts
index 32f300fe2..c6e4e6f62 100644
--- a/ui/component/or-attribute-input/src/index.ts
+++ b/ui/component/or-attribute-input/src/index.ts
@@ -220,7 +220,6 @@ export class OrAttributeInput extends subscribe(manager)(translate(i18next)(LitE
select: {
attributes: [this.attributeRef.attributeName!],
excludePath: true,
- excludeRealm: true,
excludeParentInfo: true
}
}).then((response) => {
diff --git a/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts b/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts
index afdf4cc76..8c45b53d9 100644
--- a/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts
+++ b/ui/component/or-rules/src/json-viewer/or-rule-action-attribute.ts
@@ -207,7 +207,6 @@ export class OrRuleActionAttribute extends translate(i18next)(LitElement) {
excludeAttributeTimestamp: true,
excludeAttributeValue: true,
excludeParentInfo: true,
- excludeRealm: true,
excludePath: true
},
orderBy: {
diff --git a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts
index 87a3d2269..d15ae25f7 100644
--- a/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts
+++ b/ui/component/or-rules/src/json-viewer/or-rule-asset-query.ts
@@ -717,7 +717,6 @@ export class OrRuleAssetQuery extends translate(i18next)(LitElement) {
excludeAttributeTimestamp: true,
excludeAttributeValue: true,
excludeParentInfo: true,
- excludeRealm: true,
excludePath: true
},
orderBy: {
diff --git a/ui/component/or-smart-notify/src/index.ts b/ui/component/or-smart-notify/src/index.ts
index 86bcfefcd..21afb215d 100644
--- a/ui/component/or-smart-notify/src/index.ts
+++ b/ui/component/or-smart-notify/src/index.ts
@@ -54,7 +54,6 @@ class OrSmartNotify extends LitElement {
excludePath: true,
excludeAttributes: false,
excludeParentInfo: false,
- excludeRealm: false,
excludeAttributeMeta: false,
excludeAttributeType: false,
excludeAttributeValue: false,
diff --git a/ui/demo/demo-core/src/util.ts b/ui/demo/demo-core/src/util.ts
index 87dcb8206..07e686a3c 100644
--- a/ui/demo/demo-core/src/util.ts
+++ b/ui/demo/demo-core/src/util.ts
@@ -14,7 +14,6 @@ export async function getApartment1Asset(): Promise<Asset | undefined> {
value: "urn:openremote:asset:residence"
}],
select: {
- excludeRealm: true,
excludePath: true,
excludeAttributes: true,
excludeParentInfo: true