Skip to content

armgit / core

Public Imported from GitHub
0

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

Initial implementation of gateway tunnelling (#1297)

Martin Peeters61cc6a152bb8
diff --git a/.ci_cd/aws/cloudformation-create-ec2.yml b/.ci_cd/aws/cloudformation-create-ec2.yml
index 083d79527..b45535973 100644
--- a/.ci_cd/aws/cloudformation-create-ec2.yml
+++ b/.ci_cd/aws/cloudformation-create-ec2.yml
@@ -709,6 +709,12 @@ Resources:
echo "EFS Filesystem DNS: ${EFSDNS}"
echo "EFS_DNS=${EFSDNS}" >> /etc/environment
fi
+ 03_route53_role:
+ command: !Sub |
+ # Output Route53 role to /etc/environment for use by proxy container
+ if [ -n "${DNSHostedZoneRoleArn}" ]; then
+ echo "AWS_ROUTE53_ROLE=${DNSHostedZoneRoleArn}" >> /etc/environment
+ fi
docker_setup:
packages:
diff --git a/.ci_cd/deploy.sh b/.ci_cd/deploy.sh
index 146e607da..3a96ba1c7 100644
--- a/.ci_cd/deploy.sh
+++ b/.ci_cd/deploy.sh
@@ -235,7 +235,7 @@ fi
CONTAINER_IDS=\$(docker ps -q)
if [ -n "\$CONTAINER_IDS" ]; then
echo "Stopping existing stack"
- docker-compose -f temp/docker-compose.yml -p or down 2> /dev/null
+ docker-compose -f temp/docker-compose.yml -p or down --remove-orphans 2> /dev/null
if [ \$? -ne 0 ]; then
echo "Deployment failed to stop the existing stack"
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml
index 7f952a997..713f615b8 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -841,17 +841,24 @@ jobs:
os.system(f"echo '{value}'\"'\"'' >> {envFile}")
else:
os.system(f"echo '{key}='\"'\"'{value}'\"'\"'' >> {envFile}")
-
+
+ # Output new line
+ os.system(f"echo '\n' >> {envFile}")
+
# Output env file if exists
if os.path.exists(".ci_cd/env/.env"):
os.system(f"echo 'Outputting .ci_cd/env/.env to temp/env'")
os.system("cat .ci_cd/env/.env >> temp/env")
+ # Output new line
+ os.system(f"echo '\n' >> {envFile}")
# Output environment specific env file if exists
if env is not None and env != '' and os.path.exists(f".ci_cd/env/{env}.env"):
os.system(f"echo 'Outputting .ci_cd/env/{env}.env to temp/env'")
- os.system(f"cat .ci_cd/env/{env}.env >> temp/env")
-
+ os.system(f"cat .ci_cd/env/{env}.env >> temp/env")
+ # Output new line
+ os.system(f"echo '\n' >> {envFile}")
+
# Set CIDR environment variable
if ipv4 is not None and ipv4 != '':
os.putenv("CIDR", ipv4 + '/32')
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 8e36955f5..a55089e4f 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
@@ -37,6 +37,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
@@ -165,13 +166,12 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
protected final List<Consumer<T>> messageConsumers = new ArrayList<>();
protected final List<Consumer<ConnectionStatus>> connectionStatusConsumers = new CopyOnWriteArrayList<>();
protected ConnectionStatus connectionStatus = ConnectionStatus.DISCONNECTED;
- protected ChannelFuture channelStartFuture;
protected Channel channel;
protected Bootstrap bootstrap;
protected EventLoopGroup workerGroup;
protected ScheduledExecutorService executorService;
protected CompletableFuture<Void> connectRetry;
- protected boolean permanentError;
+ protected int connectTimeout = 5000;
protected Supplier<ChannelHandler[]> encoderDecoderProvider;
protected AbstractNettyIOClient() {
@@ -183,6 +183,14 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
this.encoderDecoderProvider = encoderDecoderProvider;
}
+ public int getConnectTimeoutMillis() {
+ return this.connectTimeout;
+ }
+
+ public void setConnectTimeoutMillis(int connectTimeout) {
+ this.connectTimeout = connectTimeout;
+ }
+
protected abstract Class<? extends Channel> getChannelClass();
protected abstract EventLoopGroup getWorkerGroup();
@@ -190,68 +198,78 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
protected abstract ChannelFuture startChannel();
protected void configureChannel() {
- bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
+ bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeoutMillis());
}
@Override
- public void connect() {
- synchronized (this) {
- if (permanentError) {
- LOG.info("Unable to connect as permanent error has been set");
- return;
- }
-
- if (connectionStatus != ConnectionStatus.DISCONNECTED) {
- LOG.finest("Must be disconnected before calling connect: " + getClientUri());
- return;
- }
-
- LOG.fine("Connecting IO Client: " + getClientUri());
- onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+ public synchronized void connect() {
+ if (connectionStatus != ConnectionStatus.DISCONNECTED) {
+ LOG.finest("Must be disconnected before calling connect: " + getClientUri());
+ return;
}
- scheduleDoConnect(false);
+ LOG.fine("Connecting IO Client: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+
+ scheduleDoConnect(100);
}
- protected void scheduleDoConnect(boolean immediate) {
- long delay = immediate ? 50 : Math.max(250, RECONNECT_DELAY_INITIAL_MILLIS);
- long maxDelay = immediate ? 51 : Math.max(delay+1, Math.max(250, RECONNECT_DELAY_MAX_MILLIS));
+ protected void scheduleDoConnect(long initialDelay) {
+ long delay = Math.max(initialDelay, RECONNECT_DELAY_INITIAL_MILLIS);
+ long maxDelay = Math.max(delay+1, RECONNECT_DELAY_MAX_MILLIS);
RetryPolicy<Object> retryPolicy = RetryPolicy.builder()
.withJitter(Duration.ofMillis(delay))
.withBackoff(Duration.ofMillis(delay), Duration.ofMillis(maxDelay))
- .handle(RuntimeException.class)
+ .handle(ExecutionException.class)
.onRetryScheduled((execution) ->
- LOG.fine("Re-connection scheduled in '" + execution.getDelay() + "' for: " + getClientUri()))
+ LOG.info("Re-connection scheduled in '" + execution.getDelay() + "' for: " + getClientUri()))
.onFailedAttempt((execution) ->
- LOG.fine("Re-connection attempt failed '" + execution.getAttemptCount() + "' for: " + getClientUri()))
+ LOG.info("Connection attempt failed '" + execution.getAttemptCount() + "' for: " + getClientUri()))
.withMaxRetries(Integer.MAX_VALUE)
.build();
+ AtomicReference<Future<Void>> connectFutureRef = new AtomicReference<>();
connectRetry = Failsafe.with(retryPolicy).with(executorService).runAsyncExecution((execution) -> {
LOG.fine("Connection attempt '" + (execution.getAttemptCount()+1) + "' for: " + getClientUri());
+ connectFutureRef.set(doConnect());
+ connectFutureRef.get().get();
+
+ // Add closed callback
+ channel.closeFuture().addListener(closedFuture -> {
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.CONNECTED) {
+ LOG.info("Connection closed un-expectedly: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+ doDisconnect();
+ scheduleDoConnect(5000);
+ }
+ }
+ });
- boolean success = doConnect().get();
-
- if (connectRetry.isCancelled()) {
- execution.recordResult(null);
- return;
+ execution.recordResult(null);
+ }).whenComplete((result, ex) -> {
+ if (ex instanceof InterruptedException) {
+ connectFutureRef.get().cancel(true);
}
-
- if (success) {
- connectRetry = null;
- onConnectionStatusChanged(ConnectionStatus.CONNECTED);
- execution.recordResult(null);
- } else {
- // Cleanup resources ready for next connection attempt
+ if (ex != null) {
+ LOG.log(Level.INFO, "Connection attempt failed: " + getClientUri(), ex);
+ // Cleanup resources
doDisconnect();
- execution.recordFailure(new RuntimeException("Connection attempt failed"));
+ } else {
+ synchronized (this) {
+ if (connectionStatus == ConnectionStatus.CONNECTING) {
+ onConnectionStatusChanged(ConnectionStatus.CONNECTED);
+ } else {
+ doDisconnect();
+ }
+ }
}
});
}
- protected Future<Boolean> doConnect() {
+ protected Future<Void> doConnect() {
LOG.info("Establishing connection: " + getClientUri());
@@ -267,70 +285,61 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
- public void initChannel(Channel channel) {
+ public void initChannel(Channel channel) throws Exception {
AbstractNettyIOClient.this.initChannel(channel);
}
});
// Start and store the channel
- channelStartFuture = startChannel();
+ ChannelFuture channelStartFuture = startChannel();
channel = channelStartFuture.channel();
- // Create connected future
- CompletableFuture<Boolean> connectedFuture = createConnectedFuture();
-
- // Add closed callback
- channel.closeFuture().addListener(future -> {
- synchronized (this) {
- if (connectionStatus == ConnectionStatus.CONNECTED) {
- LOG.info("Connection closed un-expectedly: " + getClientUri());
- onConnectionStatusChanged(ConnectionStatus.CONNECTING);
+ return createConnectedFuture(channelStartFuture)
+ .whenComplete((result, ex) -> {
+ if (ex instanceof InterruptedException) {
+ // Cancel the connection
+ channelStartFuture.cancel(true);
+ }
+ if (ex != null) {
+ // Cleanup resources
doDisconnect();
- scheduleDoConnect(false);
}
- }
- });
-
- return connectedFuture;
- }
-
- protected CompletableFuture<Boolean> createConnectedFuture() {
- CompletableFuture<Boolean> connectedFuture = new CompletableFuture<>();
- channelStartFuture.addListener(future -> onConnectedFutureComplete(future, connectedFuture));
- return connectedFuture;
+ });
}
- protected void onConnectedFutureComplete(io.netty.util.concurrent.Future<? super Void> future, CompletableFuture<Boolean> connectedFuture) {
- synchronized (this) {
-
- if (connectionStatus != ConnectionStatus.CONNECTING) {
+ /**
+ * A wrapper to provide a java concurrent future; can be overridden in subclasses to add additional connection logic
+ * on top of simply establishing the channel.
+ */
+ protected CompletableFuture<Void> createConnectedFuture(final ChannelFuture channelStartFuture) {
+ CompletableFuture<Void> connectedFuture = new CompletableFuture<>();
+ channelStartFuture.addListener(future -> {
+ if (future.isCancelled()) {
return;
}
-
- if (future.isSuccess()) {
- LOG.log(Level.INFO, "Connected: " + getClientUri());
- } else if (future.cause() != null) {
- LOG.log(Level.WARNING, "Connection error: " + getClientUri(), future.cause());
+ if (future.cause() != null) {
+ connectedFuture.completeExceptionally(future.cause());
+ } else if (!future.isSuccess()) {
+ connectedFuture.completeExceptionally(new RuntimeException("Unknown connection failure occurred"));
+ } else {
+ connectedFuture.complete(null);
}
-
- connectedFuture.complete(future.isSuccess());
- }
+ });
+ return connectedFuture;
}
@Override
- public void disconnect() {
- synchronized (this) {
- if (connectionStatus != ConnectionStatus.CONNECTED && connectionStatus != ConnectionStatus.CONNECTING) {
- LOG.finest("Already disconnected: " + getClientUri());
- return;
- }
-
- LOG.fine("Disconnecting IO client: " + getClientUri());
- onConnectionStatusChanged(ConnectionStatus.DISCONNECTING);
+ public synchronized void disconnect() {
+ if (connectionStatus == ConnectionStatus.DISCONNECTED || connectionStatus == ConnectionStatus.DISCONNECTING) {
+ LOG.finest("Already disconnected or disconnecting: " + getClientUri());
+ return;
}
+ LOG.fine("Disconnecting IO client: " + getClientUri());
+ onConnectionStatusChanged(ConnectionStatus.DISCONNECTING);
+
if (connectRetry != null) {
- connectRetry.cancel(false);
+ connectRetry.cancel(true);
try {
connectRetry.get();
} catch (CancellationException | InterruptedException | ExecutionException ignored) {
@@ -346,17 +355,14 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
protected void doDisconnect() {
LOG.finest("Performing disconnect: " + getClientUri());
try {
- if (channelStartFuture != null) {
- channelStartFuture.cancel(true);
- channelStartFuture = null;
- }
-
// Close the channel
if (channel != null) {
channel.disconnect();
channel.close();
channel = null;
}
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Failed to disconnect gracefully: " + getClientUri(), e);
} finally {
if (workerGroup != null) {
workerGroup.shutdownGracefully();
@@ -368,7 +374,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
@Override
public void sendMessage(T message) {
- if (connectionStatus != ConnectionStatus.CONNECTED) {
+ if (channel == null) {
return;
}
@@ -377,7 +383,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
channel.writeAndFlush(message);
LOG.finest("Message sent to server: " + getClientUri());
} catch (Exception e) {
- LOG.log(Level.WARNING, "Message send failed: " + getClientUri(), e);
+ LOG.log(Level.INFO, "Message send failed: " + getClientUri(), e);
}
}
@@ -429,7 +435,7 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
/**
* Inserts the decoders and encoders into the channel pipeline
*/
- protected void initChannel(Channel channel) {
+ protected void initChannel(Channel channel) throws Exception {
// Below is un-necessary as channel listener handles this
addEncodersDecoders(channel);
}
@@ -446,10 +452,6 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
}
protected void onMessageReceived(T message) {
- if (connectionStatus != ConnectionStatus.CONNECTED) {
- return;
- }
-
LOG.finest("Message received notifying consumers: " + getClientUri());
messageConsumers.forEach(consumer -> {
try {
@@ -461,16 +463,19 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
}
protected void onDecodeException(ChannelHandlerContext ctx, Throwable cause) {
- LOG.log(Level.SEVERE, "Exception occurred on in-bound message: " + getClientUri(), cause);
+ LOG.log(Level.FINE, "Exception occurred on in-bound message '" + cause.getMessage() +"': " + getClientUri());
}
protected void onEncodeException(ChannelHandlerContext ctx, Throwable cause) {
- LOG.log(Level.SEVERE, "Exception occurred on out-bound message: " + getClientUri(), cause);
+ LOG.log(Level.FINE, "Exception occurred on out-bound message '" + cause.getMessage() +"': " + getClientUri());
}
protected void onConnectionStatusChanged(ConnectionStatus connectionStatus) {
+ if (this.connectionStatus == connectionStatus) {
+ return;
+ }
this.connectionStatus = connectionStatus;
- if (connectionStatusConsumers.size() > 0) {
+ if (!connectionStatusConsumers.isEmpty()) {
LOG.finest("Notifying connection status consumers: count=" + connectionStatusConsumers.size());
}
connectionStatusConsumers.forEach(
@@ -483,19 +488,6 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemen
});
}
- protected void setPermanentError(String message) {
-
- synchronized (this) {
- if (permanentError) {
- return;
- }
-
- LOG.info("An unrecoverable error has occurred with client '" + getClientUri() + "' is no longer usable: " + message);
- this.permanentError = true;
- }
- disconnect();
- }
-
@Override
public String toString() {
return getClientUri();
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
index 076fe7f24..8434619fd 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/AbstractMQTT_IOClient.java
@@ -35,7 +35,7 @@ import com.hivemq.client.mqtt.mqtt3.message.connect.connack.Mqtt3ConnAck;
import com.hivemq.client.mqtt.mqtt3.message.subscribe.suback.Mqtt3SubAck;
import org.openremote.agent.protocol.io.IOClient;
import org.openremote.container.Container;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.auth.UsernamePassword;
import org.openremote.model.syslog.SyslogCategory;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java
index 685971305..563d4526d 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java
@@ -20,7 +20,7 @@
package org.openremote.agent.protocol.mqtt;
import org.apache.http.client.utils.URIBuilder;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.attribute.AttributeRef;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java
index 6e83628e8..ade08923b 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java
@@ -5,7 +5,7 @@ import org.openremote.agent.protocol.tradfri.device.Device;
import org.openremote.agent.protocol.tradfri.device.Gateway;
import org.openremote.agent.protocol.tradfri.device.event.EventHandler;
import org.openremote.agent.protocol.tradfri.device.event.GatewayEvent;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.model.Container;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.agent.Agent;
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgent.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgent.java
index 4441f5958..f284f21f0 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgent.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgent.java
@@ -49,6 +49,11 @@ public class WebsocketAgent extends IOAgent<WebsocketAgent, WebsocketAgentProtoc
*/
public static final AttributeDescriptor<WebsocketSubscription[]> CONNECT_SUBSCRIPTIONS = new AttributeDescriptor<>("connectSubscriptions", WEBSOCKET_SUBSCRIPTION_VALUE_DESCRIPTOR.asArray());
+ /**
+ * Ability to disable the Websocket PING/PONG requests which ensure the connection is not broken
+ */
+ public static final AttributeDescriptor<Boolean> PING_DISABLED = new AttributeDescriptor<>("pingDisabled", ValueType.BOOLEAN);
+
public static final AgentDescriptor<WebsocketAgent, WebsocketAgentProtocol, WebsocketAgentLink> DESCRIPTOR = new AgentDescriptor<>(
WebsocketAgent.class, WebsocketAgentProtocol.class, WebsocketAgentLink.class, null
);
@@ -90,6 +95,15 @@ public class WebsocketAgent extends IOAgent<WebsocketAgent, WebsocketAgentProtoc
return this;
}
+ public Optional<Boolean> getPingDisabled() {
+ return getAttributes().getValue(PING_DISABLED);
+ }
+
+ public WebsocketAgent setPingDisabled(Boolean value) {
+ getAttributes().getOrCreate(PING_DISABLED).setValue(value);
+ return this;
+ }
+
@Override
public WebsocketAgentProtocol getProtocolInstance() {
return new WebsocketAgentProtocol(this);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java
index a383d89aa..f78d7de49 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketIOClient.java
@@ -31,6 +31,7 @@ import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.util.CharsetUtil;
+import jakarta.ws.rs.ProcessingException;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.openremote.agent.protocol.io.AbstractNettyIOClient;
import org.openremote.agent.protocol.io.IOClient;
@@ -39,7 +40,6 @@ import org.openremote.model.auth.OAuthGrant;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.TextUtil;
-import jakarta.ws.rs.ProcessingException;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.net.URI;
@@ -47,7 +47,9 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
-import java.util.logging.Level;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;
import static org.openremote.container.web.WebTargetBuilder.CONNECTION_TIMEOUT_MILLISECONDS;
@@ -94,14 +96,13 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
} catch (WebSocketHandshakeException e) {
handshakeFuture.setFailure(e);
}
- WebsocketIOClient.this.onHandshakeComplete(handshakeFuture);
return;
}
if (msg instanceof FullHttpResponse) {
FullHttpResponse response = (FullHttpResponse) msg;
LOG.severe("Websocket client unexpected FullHttpResponse (getStatus=" + response.status() +
- ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
+ ", content=" + response.content().toString(CharsetUtil.UTF_8) + "):" + getClientUri());
}
WebSocketFrame frame = (WebSocketFrame) msg;
@@ -110,7 +111,10 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
String str = textFrame.text();
ctx.fireChannelRead(str);
} else if (frame instanceof PongWebSocketFrame) {
- LOG.finest("Websocket client pong received");
+ LOG.finest("Received PONG: " + getClientUri());
+ if (pingCounter != null) {
+ pingCounter.set(0);
+ }
} else if (frame instanceof CloseWebSocketFrame) {
ch.close();
}
@@ -118,16 +122,19 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- LOG.log(Level.SEVERE, "Websocket client exception caught", cause);
if (!handshakeFuture.isDone()) {
handshakeFuture.setFailure(cause);
+ ctx.close();
}
- ctx.close();
WebsocketIOClient.this.onDecodeException(ctx, cause);
}
}
private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketIOClient.class);
+ protected static ResteasyClient client;
+ public static final long PING_MILLIS = 60000;
+ protected ScheduledFuture<?> pingFuture;
+ protected AtomicInteger pingCounter;
protected boolean useSsl;
protected URI uri;
protected SslContext sslCtx;
@@ -137,13 +144,17 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
protected String authHeaderValue;
protected String host;
protected int port;
- protected CompletableFuture<Boolean> connectedFuture;
+ protected boolean pingDisabled;
public WebsocketIOClient(URI uri, Map<String, List<String>> headers, OAuthGrant oAuthGrant) {
+ this(uri, headers, oAuthGrant, false);
+ }
+
+ public WebsocketIOClient(URI uri, Map<String, List<String>> headers, OAuthGrant oAuthGrant, boolean pingDisabled) {
this.uri = uri;
this.headers = headers;
this.oAuthGrant = oAuthGrant;
-
+ this.pingDisabled = pingDisabled;
String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
@@ -169,6 +180,13 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
useSsl = "wss".equalsIgnoreCase(scheme);
}
+ protected synchronized ResteasyClient getClient() {
+ if (client == null) {
+ client = createClient(executorService, 1, CONNECTION_TIMEOUT_MILLISECONDS, null);
+ }
+ return client;
+ }
+
@Override
protected Class<? extends Channel> getChannelClass() {
return NioSocketChannel.class;
@@ -190,44 +208,56 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
}
@Override
- protected void initChannel(Channel channel) {
- try {
- if (useSsl) {
- sslCtx = SslContextBuilder.forClient()
- .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
- } else {
- sslCtx = null;
- }
-
- HttpHeaders hdrs = new DefaultHttpHeaders();
+ protected void initChannel(Channel channel) throws Exception {
+ if (useSsl) {
+ sslCtx = SslContextBuilder.forClient()
+ .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
+ } else {
+ sslCtx = null;
+ }
- if (this.headers != null) {
- this.headers.forEach(hdrs::add);
- }
+ HttpHeaders hdrs = new DefaultHttpHeaders();
- if (authHeaderValue != null) {
- hdrs.set(HttpHeaderNames.AUTHORIZATION, authHeaderValue);
- }
+ if (this.headers != null) {
+ this.headers.forEach(hdrs::add);
+ }
- // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
- // If you change it to V00, ping is not supported and remember to change
- // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
- handler =
- new WebSocketClientHandler(
- WebSocketClientHandshakerFactory.newHandshaker(
- uri, WebSocketVersion.V13, null, true, hdrs));
-
- } catch (Exception e) {
- LOG.log(Level.SEVERE, "Failed to initialise channel: " + getClientUri(), e);
- setPermanentError(e.getMessage());
- return;
+ if (authHeaderValue != null) {
+ hdrs.set(HttpHeaderNames.AUTHORIZATION, authHeaderValue);
}
+ // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
+ // If you change it to V00, ping is not supported and remember to change
+ // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
+ handler =
+ new WebSocketClientHandler(
+ WebSocketClientHandshakerFactory.newHandshaker(
+ uri, WebSocketVersion.V13, null, true, hdrs));
+
+
super.initChannel(channel);
}
- protected void onHandshakeComplete(ChannelFuture handshakeFuture) {
- super.onConnectedFutureComplete(handshakeFuture, connectedFuture);
+ protected void onHandshakeComplete() {
+ // Start ping task
+ if (!pingDisabled) {
+ LOG.fine("Starting PING task: " + getClientUri());
+ pingCounter = new AtomicInteger();
+ pingFuture = executorService.scheduleWithFixedDelay(() -> {
+ try {
+ if (pingCounter.get() > 2) {
+ LOG.info("No PING response so reconnecting: " + this);
+ doDisconnect();
+ scheduleDoConnect(1000);
+ return;
+ }
+ LOG.finest("Sending PING: " + getClientUri());
+ channel.writeAndFlush(new PingWebSocketFrame());
+ pingCounter.incrementAndGet();
+ } catch (Exception ignored) {
+ }
+ }, PING_MILLIS, PING_MILLIS, TimeUnit.MILLISECONDS);
+ }
}
@Override
@@ -262,15 +292,13 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
}
@Override
- protected Future<Boolean> doConnect() {
+ protected Future<Void> doConnect() {
if (oAuthGrant != null) {
LOG.fine("Retrieving OAuth access token: " + getClientUri());
- ResteasyClient client = createClient(executorService, 1, CONNECTION_TIMEOUT_MILLISECONDS, null);
-
try {
- OAuthFilter oAuthFilter = new OAuthFilter(client, oAuthGrant);
+ OAuthFilter oAuthFilter = new OAuthFilter(getClient(), oAuthGrant);
authHeaderValue = oAuthFilter.getAuthHeader();
if (TextUtil.isNullOrEmpty(authHeaderValue)) {
throw new RuntimeException("Returned access token is null");
@@ -278,15 +306,9 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
LOG.fine("Retrieved access token via OAuth: " + getClientUri());
} catch (SocketException | ProcessingException e) {
- LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token for '" + getClientUri() + "': Connection error");
- return CompletableFuture.completedFuture(false);
+ return CompletableFuture.failedFuture(new RuntimeException("Failed to retrieve OAuth access token for '" + getClientUri() + "': Connection error"));
} catch (Exception e) {
- LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token: " + getClientUri());
- return CompletableFuture.completedFuture(false);
- } finally {
- if (client != null) {
- client.close();
- }
+ return CompletableFuture.failedFuture(new RuntimeException("Failed to retrieve OAuth access token '" + getClientUri() + "': " + e.getMessage()));
}
}
@@ -294,8 +316,25 @@ public class WebsocketIOClient<T> extends AbstractNettyIOClient<T, InetSocketAdd
}
@Override
- protected CompletableFuture<Boolean> createConnectedFuture() {
- connectedFuture = new CompletableFuture<>();
- return connectedFuture;
+ protected void doDisconnect() {
+ // Cancel ping task
+ if (pingFuture != null) {
+ pingFuture.cancel(false);
+ }
+
+ super.doDisconnect();
+ }
+
+ @Override
+ protected CompletableFuture<Void> createConnectedFuture(final ChannelFuture channelStartFuture) {
+ CompletableFuture<Void> connectedFuture = super.createConnectedFuture(channelStartFuture);
+ return connectedFuture.thenRunAsync(() -> {
+ ChannelPromise handshakeFuture = handler.handshakeFuture;
+ handshakeFuture.awaitUninterruptibly();
+
+ if (handshakeFuture.isSuccess()) {
+ onHandshakeComplete();
+ }
+ });
}
}
diff --git a/container/build.gradle b/container/build.gradle
index 47363a11c..a1e627b77 100644
--- a/container/build.gradle
+++ b/container/build.gradle
@@ -15,8 +15,6 @@ dependencies {
// Log4J redirect to SLF4J
api "org.slf4j:log4j-over-slf4j:$slf4jVersion"
- api "com.devskiller.friendly-id:friendly-id:$friendlyIdVersion"
-
api "io.undertow:undertow-servlet:$undertowVersion"
api "io.undertow:undertow-websockets-jsr:$undertowVersion"
api ("org.jboss.resteasy:resteasy-undertow:$resteasyVersion") {
@@ -47,7 +45,6 @@ dependencies {
api "org.apache.camel:camel-snmp:$camelVersion"
api "org.apache.camel:camel-health:$camelVersion"
api "org.apache.camel:camel-management:$camelVersion"
- api "com.fasterxml.uuid:java-uuid-generator:$uuidGeneratorVersion"
api "org.apache.camel:camel-micrometer:$camelVersion"
api "io.micrometer:micrometer-registry-prometheus:$micrometerVersion"
}
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 5b4defb44..3c7bb2333 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
@@ -345,6 +345,9 @@ public abstract class KeycloakIdentityProvider implements IdentityProvider {
}
public synchronized KeycloakDeployment getKeycloakDeployment(String realm, String clientId) {
+ if (realm == null || clientId == null) {
+ return null;
+ }
try {
return keycloakDeploymentCache.get(new KeycloakRealmClient(realm, clientId));
} catch (Exception ex) {
diff --git a/container/src/main/java/org/openremote/container/util/HibernateUniqueIdentifierGenerator.java b/container/src/main/java/org/openremote/container/util/HibernateUniqueIdentifierGenerator.java
new file mode 100644
index 000000000..88e81736a
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/util/HibernateUniqueIdentifierGenerator.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2024, 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.container.util;
+
+import org.hibernate.HibernateException;
+import org.hibernate.engine.spi.SharedSessionContractImplementor;
+import org.hibernate.id.IdentifierGenerator;
+import org.openremote.model.IdentifiableEntity;
+import org.openremote.model.util.UniqueIdentifierGenerator;
+
+import java.io.Serializable;
+
+/**
+ * DO NOT REMOVE; String reference in package-info.java
+ */
+public class HibernateUniqueIdentifierGenerator implements IdentifierGenerator {
+
+ @Override
+ public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
+ if (object instanceof IdentifiableEntity) {
+ IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity<?>) object;
+ if (identifiableEntity.getId() != null)
+ return identifiableEntity.getId();
+ }
+ return UniqueIdentifierGenerator.generateId();
+ }
+}
diff --git a/container/src/main/java/org/openremote/container/util/package-info.java b/container/src/main/java/org/openremote/container/util/package-info.java
index c3e7ce5f6..36105d892 100644
--- a/container/src/main/java/org/openremote/container/util/package-info.java
+++ b/container/src/main/java/org/openremote/container/util/package-info.java
@@ -1,7 +1,7 @@
@org.hibernate.annotations.GenericGenerators({
@org.hibernate.annotations.GenericGenerator(
name = Constants.PERSISTENCE_UNIQUE_ID_GENERATOR,
- strategy = "org.openremote.container.util.UniqueIdentifierGenerator"
+ strategy = "org.openremote.container.util.HibernateUniqueIdentifierGenerator"
)
})
diff --git a/container/src/main/java/org/openremote/container/web/OAuthFilter.java b/container/src/main/java/org/openremote/container/web/OAuthFilter.java
index cc9edfe8d..56cb6bde4 100644
--- a/container/src/main/java/org/openremote/container/web/OAuthFilter.java
+++ b/container/src/main/java/org/openremote/container/web/OAuthFilter.java
@@ -86,24 +86,24 @@ public class OAuthFilter implements ClientRequestFilter {
try {
if (authServerResponse != null && authServerResponse.refreshToken != null) {
// Do a refresh
- LOG.fine("Using Refresh grant");
+ LOG.finest("Using Refresh grant");
response = requestTokenUsingRefresh();
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
// Maybe the refresh token is not valid so do full auth
- LOG.fine("OAuth token refresh failed, trying a full authentication");
+ LOG.finest("OAuth token refresh failed, trying a full authentication");
authServerResponse = null;
updateToken();
return;
}
} else {
// Do full auth
- LOG.fine("Doing full authentication");
+ LOG.finest("Doing full authentication");
response = requestToken();
}
if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {
authServerResponse = null;
- LOG.warning("OAuth server response error: " + response.getStatus());
+ LOG.finest("OAuth server response error: " + response.getStatus());
throw new RuntimeException("OAuth server response error: " + response.getStatus());
} else {
authServerResponse = response.readEntity(OAuthServerResponse.class);
diff --git a/gradle.properties b/gradle.properties
index 83efbe55b..34a69a238 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -61,3 +61,4 @@ greenmailVersion=2.0.0
prometheusVersion=0.16.0
micrometerVersion=1.11.0
jdomVersion = 2.0.6
+jSchVersion = 0.2.17
diff --git a/manager/Dockerfile b/manager/Dockerfile
index a2563227e..d78455056 100644
--- a/manager/Dockerfile
+++ b/manager/Dockerfile
@@ -57,6 +57,7 @@ ENV OR_IDENTITY_SESSION_MAX_MINUTES ${OR_IDENTITY_SESSION_MAX_MINUTES:-1440}
ENV OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES ${OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES:-2628000}
ENV OR_ATTRIBUTE_EVENT_THREADS ${OR_ATTRIBUTE_EVENT_THREADS}
ENV OR_STORAGE_DIR ${OR_STORAGE_DIR:-/storage}
+ENV OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE ${OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE:-172.17.0.1}
ENV OR_JAVA_OPTS ${OR_JAVA_OPTS:--Xms500m -Xmx2g \
-XX:NativeMemoryTracking=summary \
-Xlog:all=warning:stdout:uptime,level,tags \
diff --git a/manager/build.gradle b/manager/build.gradle
index 0867656e3..acae8ae20 100644
--- a/manager/build.gradle
+++ b/manager/build.gradle
@@ -57,6 +57,8 @@ dependencies {
implementation "org.shredzone.commons:commons-suncalc:$suncalcVersion"
implementation "org.slf4j:slf4j-api:$slf4jVersion"
implementation "io.prometheus:simpleclient_httpserver:$prometheusVersion"
+
+ implementation "com.github.mwiede:jsch:$jSchVersion"
}
jar {
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 f6d4b0462..8991b865b 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java
@@ -2,9 +2,8 @@ package org.openremote.manager.datapoint;
import org.openremote.agent.protocol.ProtocolDatapointService;
import org.openremote.container.timer.TimerService;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.manager.asset.AssetProcessingException;
-import org.openremote.manager.asset.AssetProcessingService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerIdentityService;
@@ -23,7 +22,6 @@ import org.openremote.model.util.Pair;
import org.openremote.model.value.MetaHolder;
import org.openremote.model.value.MetaItemType;
-import jakarta.persistence.EntityManager;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java
index 8cf0c86d7..952d6f8e7 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java
@@ -26,7 +26,6 @@ import org.openremote.model.Constants;
import org.openremote.model.Container;
import org.openremote.model.auth.OAuthClientCredentialsGrant;
import org.openremote.agent.protocol.io.AbstractNettyIOClient;
-import org.openremote.agent.protocol.websocket.WebsocketIOClient;
import org.openremote.model.ContainerService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.model.PersistenceEvent;
@@ -44,14 +43,14 @@ import org.openremote.model.event.shared.EventRequestResponseWrapper;
import org.openremote.model.event.shared.EventSubscription;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.event.shared.RealmFilter;
-import org.openremote.model.gateway.GatewayConnection;
-import org.openremote.model.gateway.GatewayConnectionStatusEvent;
-import org.openremote.model.gateway.GatewayDisconnectEvent;
+import org.openremote.model.gateway.*;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.query.filter.RealmPredicate;
import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.util.TextUtil;
import org.openremote.model.util.ValueUtil;
+import java.io.File;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
@@ -60,6 +59,7 @@ import java.util.stream.Collectors;
import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;
import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;
+import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
/**
@@ -70,6 +70,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
public static final int PRIORITY = ManagerWebService.PRIORITY - 300;
private static final Logger LOG = SyslogCategory.getLogger(GATEWAY, GatewayClientService.class.getName());
public static final String CLIENT_EVENT_SESSION_PREFIX = GatewayClientService.class.getSimpleName() + ":";
+ public static final String OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE = "OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE";
protected AssetStorageService assetStorageService;
protected AssetProcessingService assetProcessingService;
protected PersistenceService persistenceService;
@@ -78,7 +79,8 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
protected ScheduledExecutorService executorService;
protected ManagerIdentityService identityService;
protected final Map<String, GatewayConnection> connectionRealmMap = new HashMap<>();
- protected final Map<String, WebsocketIOClient<String>> clientRealmMap = new HashMap<>();
+ protected final Map<String, GatewayIOClient> clientRealmMap = new HashMap<>();
+ protected GatewayTunnelFactory gatewayTunnelFactory;
@Override
public void init(Container container) throws Exception {
@@ -90,6 +92,22 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
timerService = container.getService(TimerService.class);
identityService = container.getService(ManagerIdentityService.class);
+ String tunnelKeyFile = getString(container.getConfig(), GatewayService.OR_GATEWAY_TUNNEL_SSH_KEY_FILE, null);
+ String localhostRewrite = getString(container.getConfig(), OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE, null);
+
+ if (!TextUtil.isNullOrEmpty(tunnelKeyFile)) {
+ File f = new File(tunnelKeyFile);
+ if (f.exists()) {
+ LOG.info("Gateway tunnelling SSH key file found at: " + f.getAbsolutePath());
+ if (!TextUtil.isNullOrEmpty(localhostRewrite)) {
+ LOG.info("Gateway tunnelling localhostRewrite set to: " + localhostRewrite);
+ }
+ gatewayTunnelFactory = new JSchGatewayTunnelFactory(f, localhostRewrite);
+ } else {
+ LOG.warning("Gateway tunnelling SSH key file does not exist, tunnelling support disabled: " + f.getAbsolutePath());
+ }
+ }
+
container.getService(ManagerWebService.class).addApiSingleton(
new GatewayClientResourceImpl(timerService, identityService, this)
);
@@ -166,7 +184,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
switch (cause) {
case UPDATE:
- WebsocketIOClient<String> client = clientRealmMap.remove(connection.getLocalRealm());
+ GatewayIOClient client = clientRealmMap.remove(connection.getLocalRealm());
if (client != null) {
destroyGatewayClient(connection, client);
}
@@ -187,7 +205,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
}
}
- protected WebsocketIOClient<String> createGatewayClient(GatewayConnection connection) {
+ protected GatewayIOClient createGatewayClient(GatewayConnection connection) {
if (connection.isDisabled()) {
LOG.info("Disabled gateway client connection so ignoring: " + connection);
@@ -197,7 +215,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
LOG.info("Creating gateway IO client: " + connection);
try {
- WebsocketIOClient<String> client = new WebsocketIOClient<>(
+ GatewayIOClient client = new GatewayIOClient(
new URIBuilder()
.setScheme(connection.isSecured() ? "wss" : "ws")
.setHost(connection.getHost())
@@ -254,7 +272,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
return null;
}
- protected void destroyGatewayClient(GatewayConnection connection, WebsocketIOClient<String> client) {
+ protected void destroyGatewayClient(GatewayConnection connection, GatewayIOClient client) {
if (client == null) {
return;
}
@@ -289,9 +307,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
EventRequestResponseWrapper.class);
messageId = wrapper.getMessageId();
event = wrapper.getEvent();
- }
-
- if (message.startsWith(SharedEvent.MESSAGE_PREFIX)) {
+ } else if (message.startsWith(SharedEvent.MESSAGE_PREFIX)) {
event = messageFromString(message, SharedEvent.MESSAGE_PREFIX, SharedEvent.class);
}
@@ -302,6 +318,60 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
destroyGatewayClient(connection, clientRealmMap.get(connection.getLocalRealm()));
clientRealmMap.put(connection.getLocalRealm(), null);
}
+ } else if (event instanceof GatewayCapabilitiesRequestEvent) {
+ LOG.fine("Central manager requested specifications / capabilities of the gateway.");
+ sendCentralManagerMessage(
+ connection.getLocalRealm(),
+ messageToString(
+ EventRequestResponseWrapper.MESSAGE_PREFIX,
+ new EventRequestResponseWrapper<>(
+ messageId,
+ new GatewayCapabilitiesResponseEvent(gatewayTunnelFactory != null)
+ )
+ )
+ );
+ } else if (event instanceof GatewayTunnelStartRequestEvent gatewayTunnelStartRequestEvent) {
+ LOG.info("Start tunnel request received: " + gatewayTunnelStartRequestEvent);
+ String error = null;
+
+ try {
+ gatewayTunnelFactory.startTunnel(gatewayTunnelStartRequestEvent);
+ } catch (Exception e) {
+ error = e.getMessage();
+ }
+
+ sendCentralManagerMessage(
+ connection.getLocalRealm(),
+ messageToString(
+ EventRequestResponseWrapper.MESSAGE_PREFIX,
+ new EventRequestResponseWrapper<>(
+ messageId,
+ new GatewayTunnelStartResponseEvent(error)
+ )
+ )
+ );
+
+ } else if (event instanceof GatewayTunnelStopRequestEvent stopRequestEvent) {
+ LOG.info("Stop tunnel request received: " + stopRequestEvent);
+ String error = null;
+
+ try {
+ gatewayTunnelFactory.stopTunnel(stopRequestEvent.getInfo());
+ } catch (Exception e) {
+ error = e.getMessage();
+ }
+
+ sendCentralManagerMessage(
+ connection.getLocalRealm(),
+ messageToString(
+ EventRequestResponseWrapper.MESSAGE_PREFIX,
+ new EventRequestResponseWrapper<>(
+ messageId,
+ new GatewayTunnelStopResponseEvent(error)
+ )
+ )
+ );
+
} else if (event instanceof AttributeEvent) {
assetProcessingService.sendAttributeEvent((AttributeEvent)event, getClass().getName());
} else if (event instanceof AssetEvent) {
@@ -355,7 +425,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
}
protected void sendCentralManagerMessage(String realm, String message) {
- WebsocketIOClient<String> client;
+ GatewayIOClient client;
synchronized (clientRealmMap) {
client = clientRealmMap.get(realm);
@@ -425,7 +495,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServi
return ConnectionStatus.DISABLED;
}
- WebsocketIOClient<String> client = clientRealmMap.get(realm);
+ GatewayIOClient client = clientRealmMap.get(realm);
return client != null ? client.getConnectionStatus() : null;
}
}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
index 958610e28..cf4edf355 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
@@ -19,7 +19,6 @@
*/
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.model.asset.*;
@@ -28,20 +27,16 @@ import org.openremote.model.asset.impl.GatewayAsset;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.shared.EventRequestResponseWrapper;
import org.openremote.model.event.shared.SharedEvent;
-import org.openremote.model.gateway.GatewayDisconnectEvent;
+import org.openremote.model.gateway.*;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.Pair;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import java.util.*;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
-import java.util.function.BiFunction;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.ToIntFunction;
+import java.util.function.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@@ -51,37 +46,39 @@ import java.util.stream.Stream;
import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
/**
- * Handles all communication between a gateway and the local manager
+ * Handles all communication between a gateway and this manager instance
*/
public class GatewayConnector {
private static final Logger LOG = SyslogCategory.getLogger(GATEWAY, 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";
+ public static final long RESPONSE_TIMEOUT_MILLIS = 10000;
protected static final Map<String, Pair<Function<String, String>, Function<String, String>>> ASSET_ID_MAPPERS = new HashMap<>();
protected final String realm;
protected final String gatewayId;
protected final AssetStorageService assetStorageService;
protected final ScheduledExecutorService executorService;
protected final AssetProcessingService assetProcessingService;
+ protected final GatewayService gatewayService;
protected final Map<String, Asset<?>> pendingAssetMerges = new HashMap<>();
protected final AtomicReference<EventRequestResponseWrapper<DeleteAssetsRequestEvent>> pendingAssetDelete = new AtomicReference<>();
protected List<AssetEvent> cachedAssetEvents;
protected List<AttributeEvent> cachedAttributeEvents;
protected Consumer<Object> gatewayMessageConsumer;
- protected Runnable disconnectRunnable;
+ protected Runnable requestDisconnect;
+ protected final AtomicReference<String> sessionId = new AtomicReference<>();
protected boolean disabled;
protected boolean initialSyncInProgress;
protected ScheduledFuture<?> syncProcessorFuture;
List<String> syncAssetIds;
int syncIndex;
int syncErrors;
- GatewayAsset gateway;
String expectedSyncResponseName;
+ protected boolean tunnellingSupported;
+ protected final Map<Class<? extends SharedEvent>, BiConsumer<String, SharedEvent>> eventConsumerMap = new HashMap<>();
protected static List<Integer> ALPHA_NUMERIC_CHARACTERS = new ArrayList<>(62);
@@ -97,75 +94,58 @@ public class GatewayConnector {
);
}
- /**
- * An easily reversible mathematical way of ensuring gateway asset IDs are unique by incrementing the first two
- * characters by adding the first two characters of the gateway ID for inbound IDs and the reverse for outbound.
- */
- public static String mapAssetId(String gatewayId, String assetId, boolean outbound) {
- Pair<Function<String, String>, Function<String, String>> gatewayIdMappers = ASSET_ID_MAPPERS.computeIfAbsent(gatewayId, gwId -> {
- int g1 = gatewayId.charAt(0) % ALPHA_NUMERIC_CHARACTERS.size();
- int g2 = gatewayId.charAt(1) % ALPHA_NUMERIC_CHARACTERS.size();
-
- BiFunction<Integer, String, String> mapper = (sign, id) -> {
- int a1 = (ALPHA_NUMERIC_CHARACTERS.indexOf((int)id.charAt(0)) + (sign * g1) + ALPHA_NUMERIC_CHARACTERS.size()) % ALPHA_NUMERIC_CHARACTERS.size();
- int a2 = (ALPHA_NUMERIC_CHARACTERS.indexOf((int)id.charAt(1)) + (sign * g2) + ALPHA_NUMERIC_CHARACTERS.size()) % ALPHA_NUMERIC_CHARACTERS.size();
- return String.valueOf((char)ALPHA_NUMERIC_CHARACTERS.get(a1).intValue()) + ((char)ALPHA_NUMERIC_CHARACTERS.get(a2).intValue()) + id.substring(2);
- };
-
- return new Pair<>(
- id -> mapper.apply(1, id), // Inbound
- id -> mapper.apply(-1, id) // Outbound
- );
- });
-
- return outbound ? gatewayIdMappers.value.apply(assetId) : gatewayIdMappers.key.apply(assetId);
- }
-
- public GatewayConnector(
+ protected GatewayConnector(
AssetStorageService assetStorageService,
AssetProcessingService assetProcessingService,
ScheduledExecutorService executorService,
+ GatewayService gatewayService,
GatewayAsset gateway) {
this.assetStorageService = assetStorageService;
this.assetProcessingService = assetProcessingService;
this.executorService = executorService;
- boolean disabled = gateway.getDisabled().orElse(false);
+ this.gatewayService = gatewayService;
+ this.disabled = gateway.getDisabled().orElse(false);
this.realm = gateway.getRealm();
this.gatewayId = gateway.getId();
- this.disabled = disabled;
- this.gateway = gateway;
+
+ // Setup static inbound event handling
+ synchronized(eventConsumerMap) {
+ eventConsumerMap.put(AssetEvent.class, (msgId, e) -> onAssetEvent((AssetEvent) e));
+ eventConsumerMap.put(AttributeEvent.class, (msgId, e) -> onAttributeEvent((AttributeEvent) e));
+ eventConsumerMap.put(DeleteAssetsResponseEvent.class, (msgId, e) -> onAssetDeleteResponseEvent(msgId, (DeleteAssetsResponseEvent) e));
+ }
+ publishAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED));
}
- public void sendMessageToGateway(Object message) {
+ protected 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);
+ LOG.log(Level.SEVERE, "Failed to send message to gateway: " + this, e);
}
}
/**
- * Start the connector and initiate synchronisation of assets
+ * Connection for this gateway has started so initiate synchronisation of assets
*/
- public void connect(Consumer<Object> gatewayMessageConsumer, Runnable disconnectRunnable) {
- synchronized (this) {
- if (this.gatewayMessageConsumer != null) {
- return;
- }
- this.gatewayMessageConsumer = gatewayMessageConsumer;
- }
-
- this.disconnectRunnable = disconnectRunnable;
- initialSyncInProgress = true;
+ protected void connected(String sessionId, Consumer<Object> gatewayMessageConsumer, Runnable requestDisconnect) {
+ LOG.fine("Gateway connector connected: " + this);
- LOG.fine("Gateway connector starting: Gateway ID=" + gatewayId);
- sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTING));
+ synchronized (this.sessionId) {
+ if (getSessionId() != null) {
+ disconnect();
+ }
+ this.sessionId.set(sessionId);
+ }
+ this.gatewayMessageConsumer = gatewayMessageConsumer;
+ this.requestDisconnect = requestDisconnect;
// Reinitialise state
+ initialSyncInProgress = true;
syncProcessorFuture = null;
cachedAssetEvents = new ArrayList<>();
cachedAttributeEvents = new ArrayList<>();
@@ -173,85 +153,240 @@ public class GatewayConnector {
syncIndex = 0;
syncErrors = 0;
+ publishAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTED));
+
startSync();
}
/**
- * Stop the connector and prevent further communication with the gateway
+ * Connection to the edge gateway instance has been disconnected so stop any synchronisation
*/
- public void disconnect() {
- synchronized (this) {
- if (this.gatewayMessageConsumer == null) {
+ protected void disconnected(String sessionId) {
+ synchronized (this.sessionId) {
+ if (!sessionId.equals(this.sessionId.get())) {
return;
}
- this.gatewayMessageConsumer = null;
+ this.sessionId.set(null);
}
- Runnable disconnectRunnable = this.disconnectRunnable;
- this.disconnectRunnable = null;
- initialSyncInProgress = false;
- pendingAssetMerges.clear();
- pendingAssetDelete.set(null);
-
+ LOG.fine("Gateway connector disconnected: " + this);
if (syncProcessorFuture != null) {
+ LOG.finest("Aborting active sync process: " + this);
syncProcessorFuture.cancel(true);
}
- // Wait a short while to disconnect to allow disconnect message to be delivered
- LOG.fine("Gateway connector disconnected: Gateway ID=" + gatewayId);
- executorService.schedule(disconnectRunnable, 1, TimeUnit.SECONDS);
- if (!isDisabled()) {
- sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED));
+ initialSyncInProgress = false;
+ pendingAssetMerges.clear();
+ pendingAssetDelete.set(null);
+ publishAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED));
+ }
+
+ protected void disconnect() {
+ synchronized (this.sessionId) {
+ if (isConnected()) {
+ requestDisconnect.run();
+ disconnected(getSessionId());
+ }
}
}
- public boolean isConnected() {
- return gatewayMessageConsumer != null;
+ protected boolean isConnected() {
+ return sessionId.get() != null;
}
- public boolean isInitialSyncInProgress() {
+ protected boolean isInitialSyncInProgress() {
return initialSyncInProgress;
}
- public String getGatewayId() {
- return gatewayId;
+ protected boolean isTunnellingSupported() {
+ return tunnellingSupported;
}
- public String getRealm() {
- return realm;
- }
+ /**
+ * Request for gateway capabilities such as tunneling support
+ */
+ protected CompletableFuture<GatewayCapabilitiesResponseEvent> getCapabilities() {
- public boolean isDisabled() {
- return disabled;
+ final AtomicReference<GatewayCapabilitiesResponseEvent> responseRef = new AtomicReference<>();
+
+ synchronized (eventConsumerMap) {
+ if (eventConsumerMap.containsKey(GatewayCapabilitiesRequestEvent.class)) {
+ return CompletableFuture.failedFuture(new IllegalArgumentException("A capabilities request is already pending"));
+ }
+
+ eventConsumerMap.put(GatewayCapabilitiesResponseEvent.class, (msgID, e) -> {
+ GatewayCapabilitiesResponseEvent response = (GatewayCapabilitiesResponseEvent) e;
+
+ synchronized (responseRef) {
+ responseRef.set(response);
+ responseRef.notify();
+ }
+ });
+ }
+
+ return CompletableFuture.supplyAsync(() -> {
+ sendMessageToGateway(new EventRequestResponseWrapper<>(
+ UniqueIdentifierGenerator.generateId(),
+ new GatewayCapabilitiesRequestEvent()
+ ));
+
+ // Wait for response indefinitely as timeout handled on CompletableFuture
+ try {
+ synchronized (responseRef) {
+ responseRef.wait();
+ }
+ } catch (InterruptedException ignored) {
+ } finally {
+ synchronized (eventConsumerMap) {
+ eventConsumerMap.remove(GatewayCapabilitiesResponseEvent.class);
+ }
+ }
+ return responseRef.get();
+ }, executorService)
+ .orTimeout(RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+ .whenComplete((result, ex) -> {
+ if (ex instanceof TimeoutException) {
+ synchronized (responseRef) {
+ responseRef.notify();
+ }
+ }
+ });
}
- public void setDisabled(boolean disabled) {
- if (this.disabled == disabled) {
- return;
+ protected CompletableFuture<Void> startTunnel(GatewayTunnelInfo tunnelInfo) {
+
+ if (!isConnected() || isInitialSyncInProgress()) {
+ String msg = "Gateway is not connected or initial sync in progress so cannot start tunnel: " + this;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
}
- this.disabled = disabled;
+ final AtomicReference<GatewayTunnelStartResponseEvent> responseRef = new AtomicReference<>();
- if (disabled) {
- if (isConnected()) {
- disconnect();
+ synchronized (eventConsumerMap) {
+ if (eventConsumerMap.containsKey(GatewayTunnelStartResponseEvent.class)) {
+ return CompletableFuture.failedFuture(new IllegalArgumentException("A start tunnel request is already pending"));
}
- LOG.fine("Gateway connector disabled: Gateway ID=" + gatewayId);
- sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISABLED));
- } else {
- LOG.info("Gateway connector enabled: Gateway ID=" + gatewayId);
- sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED));
+
+ eventConsumerMap.put(GatewayTunnelStartResponseEvent.class, (msgID, e) -> {
+ GatewayTunnelStartResponseEvent response = (GatewayTunnelStartResponseEvent) e;
+
+ synchronized (responseRef) {
+ responseRef.set(response);
+ responseRef.notify();
+ }
+ });
}
+
+ return CompletableFuture.runAsync(() -> {
+ sendMessageToGateway(new EventRequestResponseWrapper<>(
+ tunnelInfo.getId(),
+ new GatewayTunnelStartRequestEvent(gatewayService.getTunnelSSHHostname(), gatewayService.getTunnelSSHPort(), tunnelInfo)
+ ));
+
+ // Wait for response indefinitely as timeout handled on CompletableFuture
+ try {
+ synchronized (responseRef) {
+ responseRef.wait();
+ }
+
+ GatewayTunnelStartResponseEvent responseEvent = responseRef.get();
+
+ if (responseEvent != null && responseEvent.getError() != null) {
+ throw new RuntimeException("Failed to start tunnel: error=" + responseEvent.getError() + ", " + tunnelInfo);
+ }
+ } catch (InterruptedException ignored) {
+ } finally {
+ synchronized (eventConsumerMap) {
+ eventConsumerMap.remove(GatewayTunnelStartResponseEvent.class);
+ }
+ }
+ }, executorService)
+ .orTimeout(RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+ .whenComplete((result, ex) -> {
+ if (ex instanceof TimeoutException) {
+ synchronized (responseRef) {
+ responseRef.notify();
+ }
+ }
+ });
+ }
+
+ protected CompletableFuture<Void> stopTunnel(GatewayTunnelInfo tunnelInfo) {
+
+ final AtomicReference<GatewayTunnelStopResponseEvent> responseRef = new AtomicReference<>();
+
+ synchronized (eventConsumerMap) {
+ if (eventConsumerMap.containsKey(GatewayTunnelStopResponseEvent.class)) {
+ return CompletableFuture.failedFuture(new IllegalArgumentException("A stop tunnel request is already pending"));
+ }
+
+ eventConsumerMap.put(GatewayTunnelStopResponseEvent.class, (msgID, e) -> {
+ GatewayTunnelStopResponseEvent response = (GatewayTunnelStopResponseEvent) e;
+
+ synchronized (responseRef) {
+ responseRef.set(response);
+ responseRef.notify();
+ }
+ });
+ }
+
+ return CompletableFuture.runAsync(() -> {
+ sendMessageToGateway(new EventRequestResponseWrapper<>(
+ tunnelInfo.getId(),
+ new GatewayTunnelStopRequestEvent(tunnelInfo)
+ ));
+
+ // Wait for response indefinitely as timeout handled on CompletableFuture
+ try {
+ synchronized (responseRef) {
+ responseRef.wait();
+ }
+
+ GatewayTunnelStopResponseEvent responseEvent = responseRef.get();
+
+ if (responseEvent != null && responseEvent.getError() != null) {
+ throw new RuntimeException("Failed to stop tunnel: error=" + responseEvent.getError() + ", " + tunnelInfo);
+ }
+ } catch (InterruptedException ignored) {
+ } finally {
+ synchronized (eventConsumerMap) {
+ eventConsumerMap.remove(GatewayTunnelStopResponseEvent.class);
+ }
+ }
+ }, executorService)
+ .orTimeout(RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+ .whenComplete((result, ex) -> {
+ if (ex instanceof TimeoutException) {
+ synchronized (responseRef) {
+ responseRef.notify();
+ }
+ }
+ });
+ }
+
+ protected String getRealm() {
+ return realm;
}
- protected void sendAttributeEvent(AttributeEvent event) {
+ protected boolean isDisabled() {
+ return disabled;
+ }
+
+ protected void setDisabled(boolean disabled) {
+ this.disabled = disabled;
+ this.disconnect();
+ }
+
+ protected String getSessionId() {
+ return sessionId.get();
+ }
+
+ protected void publishAttributeEvent(AttributeEvent event) {
assetProcessingService.sendAttributeEvent(event, GatewayService.class.getName());
}
synchronized protected void onGatewayEvent(String messageId, SharedEvent e) {
- if (!isConnected()) {
- return;
- }
if (initialSyncInProgress) {
if (e instanceof AssetsEvent) {
@@ -262,12 +397,11 @@ public class GatewayConnector {
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(messageId, (DeleteAssetsResponseEvent) e);
+ synchronized (eventConsumerMap) {
+ BiConsumer<String, SharedEvent> consumer = eventConsumerMap.get(e.getClass());
+ if (consumer != null) {
+ consumer.accept(messageId, e);
+ }
}
}
}
@@ -285,18 +419,18 @@ public class GatewayConnector {
sendMessageToGateway(new EventRequestResponseWrapper<>(
ASSET_READ_EVENT_NAME_INITIAL,
new ReadAssetsEvent(new AssetQuery().select(new AssetQuery.Select().excludeAttributes()).recursive(true))));
- syncProcessorFuture = executorService.schedule(this::onSyncAssetsTimeout, SYNC_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+ syncProcessorFuture = executorService.schedule(this::onSyncAssetsTimeout, RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
}
/**
- * Called if a response isn't received from the gateway within {@link #SYNC_TIMEOUT_MILLIS}
+ * Called if a response isn't received from the gateway within {@link #RESPONSE_TIMEOUT_MILLIS}
*/
synchronized protected void onSyncAssetsTimeout() {
if (!isConnected()) {
return;
}
- LOG.info("Gateway sync timeout occurred: Gateway ID=" + gatewayId);
+ LOG.info("Gateway sync timeout occurred: " + this);
syncErrors++;
if (syncAborted()) {
@@ -313,9 +447,8 @@ public class GatewayConnector {
protected boolean syncAborted() {
if (syncErrors == MAX_SYNC_RETRIES) {
- LOG.warning("Gateway sync max retries reached so disconnecting the gateway: Gateway ID=" + gatewayId);
- sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.PERMANENT_ERROR));
- disconnect();
+ LOG.warning("Gateway sync max retries reached so disconnecting the gateway: " + this);
+ requestDisconnect.run();
return true;
}
@@ -334,8 +467,7 @@ public class GatewayConnector {
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());
-
+ LOG.fine("Synchronising gateway assets " + syncIndex+1 + "-" + syncIndex + requestAssetIds.length + " of " + syncAssetIds.size() + ": " + this);
sendMessageToGateway(
new EventRequestResponseWrapper<>(
expectedSyncResponseName,
@@ -345,7 +477,7 @@ public class GatewayConnector {
)
)
);
- syncProcessorFuture = executorService.schedule(this::requestAssets, SYNC_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+ syncProcessorFuture = executorService.schedule(this::requestAssets, RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
}
synchronized protected void onSyncAssetsResponse(String messageId, AssetsEvent e) {
@@ -354,7 +486,7 @@ public class GatewayConnector {
}
if (!expectedSyncResponseName.equalsIgnoreCase(messageId)) {
- LOG.info("Unexpected response from gateway so ignoring (expected=" + expectedSyncResponseName + ", actual =" + messageId + "): " + e);
+ LOG.info("Unexpected response from gateway so ignoring (expected=" + expectedSyncResponseName + ", actual =" + messageId + "): " + this);
return;
}
@@ -365,7 +497,7 @@ public class GatewayConnector {
if (isInitialResponse) {
// Put assets in hierarchical order
- Map<String, String> gatewayAssetIdParentIdMap = e.getAssets().stream()
+ Map<String, String> gatewayAssetIdParentIdMap = e.getAssets() == null ? Collections.emptyMap() : e.getAssets().stream()
.collect(HashMap::new, (m, v) -> m.put(v.getId(), v.getParentId()), HashMap::putAll);
ToIntFunction<Asset<?>> assetLevelExtractor = asset -> {
@@ -378,7 +510,7 @@ public class GatewayConnector {
return level;
};
- syncAssetIds = e.getAssets()
+ syncAssetIds = e.getAssets() == null ? Collections.emptyList() : e.getAssets()
.stream()
.sorted(Comparator.comparingInt(assetLevelExtractor))
.map(Asset::getId)
@@ -395,7 +527,7 @@ public class GatewayConnector {
} else {
List<String> requestedAssetIds = syncAssetIds.stream().skip(syncIndex).limit(SYNC_ASSET_BATCH_SIZE).collect(Collectors.toList());
- List<Asset<?>> returnedAssets = e.getAssets();
+ List<Asset<?>> returnedAssets = e.getAssets() == null ? Collections.emptyList() : e.getAssets();
// Remove any assets that have been deleted since requested
cachedAssetEvents.removeIf(
@@ -409,7 +541,7 @@ public class GatewayConnector {
});
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");
+ LOG.warning("Retrieved gateway asset batch count or ID mismatch, attempting to re-send the request: " + this);
syncErrors++;
requestAssets();
return;
@@ -439,7 +571,7 @@ public class GatewayConnector {
// Request next batch or move on
syncIndex += requestedAssetIds.size();
if (syncIndex >= syncAssetIds.size()) {
- LOG.info("All requested gateway assets retrieved");
+ LOG.info("All requested gateway assets retrieved: " + this);
Set<String> refreshAssets = new HashSet<>();
@@ -452,7 +584,7 @@ public class GatewayConnector {
try {
saveAssetLocally(assetEvent.getAsset());
} catch (Exception ex) {
- LOG.log(Level.SEVERE, "Failed to add new gateway asset (Gateway ID=" + gatewayId + ", Asset=" + assetEvent.getAsset(), ex);
+ LOG.log(Level.SEVERE, "Failed to add new gateway asset (Asset=" + assetEvent.getAsset() + "): " + this, ex);
}
} else {
refreshAssets.add(assetEvent.getId());
@@ -467,7 +599,7 @@ public class GatewayConnector {
cachedAttributeEvents.forEach(attributeEvent -> {
String assetId = attributeEvent.getId();
if (!refreshAssets.contains(assetId)) {
- LOG.info("1 or more gateway asset attribute values have changed so requesting the asset again (Gateway ID=" + gatewayId + ", Asset<?> ID=" + assetId);
+ LOG.info("1 or more gateway asset attribute values have changed so requesting the asset again (Asset<?> ID=" + assetId + ": " + this);
refreshAssets.add(assetId);
}
});
@@ -499,7 +631,7 @@ public class GatewayConnector {
if (!obsoleteLocalAssetIds.isEmpty()) {
boolean deleted = deleteAssetsLocally(obsoleteLocalAssetIds);
if (!deleted) {
- LOG.warning("Failed to delete obsolete local gateway assets; assets are not correctly synced");
+ LOG.warning("Failed to delete obsolete local gateway assets; assets are not correctly synced: " + this);
}
}
}
@@ -508,14 +640,21 @@ public class GatewayConnector {
initialSyncInProgress = false;
cachedAssetEvents.clear();
cachedAttributeEvents.clear();
- sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTED));
+
+ getCapabilities().whenComplete ((response, error) -> {
+ if (error != null) {
+ LOG.warning("An error occurred whilst getting the gateway capabilities, assuming no support: " + this);
+ }
+ tunnellingSupported = response != null && response.isTunnelingSupported();
+ publishAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.TUNNELING_SUPPORTED, tunnellingSupported));
+ });
}
@SuppressWarnings("unchecked")
protected <T extends Asset<?>> T mergeGatewayAsset(T 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();
+ String msg = "Gateway is not connected or initial sync in progress so cannot merge asset: Asset<?> ID=" + asset.getId() + ": " + this;
LOG.info(msg);
throw new IllegalStateException(msg);
}
@@ -526,7 +665,7 @@ public class GatewayConnector {
synchronized (pendingAssetMerges) {
if (id != null && pendingAssetMerges.containsKey(id)) {
- String msg = "Gateway asset merge already pending for this asset: Gateway ID=" + gatewayId + ", Asset<?> ID=" + id;
+ String msg = "Gateway asset merge already pending for this asset: Asset<?> ID=" + id + ": " + this;
LOG.info(msg);
throw new IllegalStateException(msg);
}
@@ -552,7 +691,7 @@ public class GatewayConnector {
synchronized (asset.getId()) {
try {
- asset.getId().wait(ASSET_CRUD_TIMEOUT_MILLIS);
+ asset.getId().wait(RESPONSE_TIMEOUT_MILLIS);
T mergedAsset;
synchronized (pendingAssetMerges) {
@@ -560,13 +699,13 @@ public class GatewayConnector {
}
if (mergedAsset == null) {
- throw new IllegalStateException("Gateway asset merge failed: Gateway ID=" + gatewayId + ", Asset ID=" + asset.getId() + ", Asset ID Mapped=" + id);
+ throw new IllegalStateException("Gateway asset merge failed: Asset ID=" + asset.getId() + ", Asset ID Mapped=" + id + ": " + this);
}
return mergedAsset;
} catch (InterruptedException e) {
- String msg = "Gateway asset merge interrupted: Gateway ID=" + gatewayId + ", Asset ID=" + asset.getId() + ", Asset ID Mapped=" + id;
+ String msg = "Gateway asset merge interrupted: Asset ID=" + asset.getId() + ", Asset ID Mapped=" + id + ": " + this;
LOG.info(msg);
throw new IllegalStateException(msg);
} finally {
@@ -582,7 +721,7 @@ public class GatewayConnector {
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());
+ String msg = "Gateway is not connected or initial sync in progress so cannot delete asset(s): Asset<?> IDs=" + Arrays.toString(assetIds.toArray()) + ": " + this;
LOG.info(msg);
throw new IllegalStateException(msg);
}
@@ -590,7 +729,7 @@ public class GatewayConnector {
synchronized (pendingAssetDelete) {
if (pendingAssetDelete.get() != null) {
- String msg = "Gateway asset delete already pending: Gateway ID=" + gatewayId + ", Asset<?> IDs Mapped=" + Arrays.toString(pendingAssetDelete.get().getEvent().getAssetIds().toArray());
+ String msg = "Gateway asset delete already pending: Asset<?> IDs Mapped=" + Arrays.toString(pendingAssetDelete.get().getEvent().getAssetIds().toArray()) + ": " + this;
LOG.info(msg);
throw new IllegalStateException(msg);
}
@@ -604,13 +743,13 @@ public class GatewayConnector {
try {
sendMessageToGateway(pendingAssetDelete.get());
- pendingAssetDelete.wait(ASSET_CRUD_TIMEOUT_MILLIS);
+ pendingAssetDelete.wait(RESPONSE_TIMEOUT_MILLIS);
if (pendingAssetDelete.get() != null) {
- throw new IllegalStateException("Gateway asset delete failed: Gateway ID=" + gatewayId + ", Asset<?> IDs=" + originalIds + ", Asset<?> IDs Mapped=" + Arrays.toString(assetIds.toArray()));
+ throw new IllegalStateException("Gateway asset delete failed: Asset<?> IDs=" + originalIds + ", Asset<?> IDs Mapped=" + Arrays.toString(assetIds.toArray()) + ": " + this);
}
return true;
} catch (InterruptedException e) {
- String msg = "Gateway asset delete interrupted: Gateway ID=" + gatewayId + ", Asset<?> IDs=" + originalIds + ", Asset<?> IDs Mapped=" + Arrays.toString(assetIds.toArray());
+ String msg = "Gateway asset delete interrupted: Asset<?> IDs=" + originalIds + ", Asset<?> IDs Mapped=" + Arrays.toString(assetIds.toArray()) + ": " + this;
LOG.info(msg);
throw new IllegalStateException(msg);
} finally {
@@ -627,7 +766,7 @@ public class GatewayConnector {
}
if (!pendingAssetDelete.get().getMessageId().equals(messageId)) {
- LOG.info("Gateway asset delete response name does not match request so ignoring");
+ LOG.info("Gateway asset delete response name does not match request so ignoring: " + this);
return;
}
@@ -639,6 +778,7 @@ public class GatewayConnector {
}
}
+ @SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
synchronized protected void onAssetEvent(AssetEvent e) {
switch (e.getCause()) {
@@ -652,7 +792,6 @@ public class GatewayConnector {
String id = pendingAssetMergeEntry.getKey();
pendingAssetMergeEntry.setValue(mergedAsset);
- //noinspection
synchronized (id) {
// Notify the waiting merge thread
id.notify();
@@ -664,7 +803,7 @@ public class GatewayConnector {
try {
deleteAssetsLocally(Collections.singletonList(mapAssetId(gatewayId, e.getId(), false)));
} catch (Exception ex) {
- LOG.log(Level.SEVERE, "Removing obsolete asset failed: " + e.getId(), ex);
+ LOG.log(Level.SEVERE, "Removing obsolete asset failed: " + e.getId() + ": " + this, ex);
}
}
}
@@ -672,7 +811,7 @@ public class GatewayConnector {
protected void onAttributeEvent(AttributeEvent e) {
// Just push the event through the processing chain
- sendAttributeEvent(new AttributeEvent(mapAssetId(gatewayId, e.getId(), false), e.getName(), e.getValue().orElse(null), e.getTimestamp()));
+ publishAttributeEvent(new AttributeEvent(mapAssetId(gatewayId, e.getId(), false), e.getName(), e.getValue().orElse(null), e.getTimestamp()));
}
protected <T extends Asset<?>> T saveAssetLocally(T asset) {
@@ -680,12 +819,43 @@ public class GatewayConnector {
asset.setId(mapAssetId(gatewayId, assetId, false));
asset.setParentId(asset.getParentId() != null ? mapAssetId(gatewayId, asset.getParentId(), false) : gatewayId);
asset.setRealm(realm);
- LOG.fine("Creating/updating gateway asset: Gateway ID=" + gatewayId + ", Asset ID=" + assetId + ", Asset ID Mapped=" + asset.getId());
+ LOG.fine("Creating/updating gateway asset: Asset ID=" + assetId + ", Asset ID Mapped=" + asset.getId() + ": " + this);
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()));
+ LOG.fine("Removing gateway asset: Asset IDs=" + Arrays.toString(assetIds.toArray()) + ": " + this);
return assetStorageService.delete(assetIds, true);
}
+
+ @Override
+ public String toString() {
+ return GatewayConnector.class.getSimpleName() + "{" +
+ "gatewayId='" + gatewayId + '\'' +
+ '}';
+ }
+
+ /**
+ * An easily reversible mathematical way of ensuring gateway asset IDs are unique by incrementing the first two
+ * characters by adding the first two characters of the gateway ID for inbound IDs and the reverse for outbound.
+ */
+ public static String mapAssetId(String gatewayId, String assetId, boolean outbound) {
+ Pair<Function<String, String>, Function<String, String>> gatewayIdMappers = ASSET_ID_MAPPERS.computeIfAbsent(gatewayId, gwId -> {
+ int g1 = gatewayId.charAt(0) % ALPHA_NUMERIC_CHARACTERS.size();
+ int g2 = gatewayId.charAt(1) % ALPHA_NUMERIC_CHARACTERS.size();
+
+ BiFunction<Integer, String, String> mapper = (sign, id) -> {
+ int a1 = (ALPHA_NUMERIC_CHARACTERS.indexOf((int)id.charAt(0)) + (sign * g1) + ALPHA_NUMERIC_CHARACTERS.size()) % ALPHA_NUMERIC_CHARACTERS.size();
+ int a2 = (ALPHA_NUMERIC_CHARACTERS.indexOf((int)id.charAt(1)) + (sign * g2) + ALPHA_NUMERIC_CHARACTERS.size()) % ALPHA_NUMERIC_CHARACTERS.size();
+ return String.valueOf((char)ALPHA_NUMERIC_CHARACTERS.get(a1).intValue()) + ((char)ALPHA_NUMERIC_CHARACTERS.get(a2).intValue()) + id.substring(2);
+ };
+
+ return new Pair<>(
+ id -> mapper.apply(1, id), // Inbound
+ id -> mapper.apply(-1, id) // Outbound
+ );
+ });
+
+ return outbound ? gatewayIdMappers.value.apply(assetId) : gatewayIdMappers.key.apply(assetId);
+ }
}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.java
new file mode 100644
index 000000000..8b074c12a
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayIOClient.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2024, 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.ChannelFuture;
+import org.openremote.agent.protocol.websocket.WebsocketIOClient;
+import org.openremote.model.auth.OAuthGrant;
+import org.openremote.model.event.shared.EventRequestResponseWrapper;
+import org.openremote.model.gateway.GatewayCapabilitiesRequestEvent;
+import org.openremote.model.syslog.SyslogCategory;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.logging.Logger;
+
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+/**
+ * This is a special version of {@link WebsocketIOClient} that waits for a
+ * {@link org.openremote.model.gateway.GatewayCapabilitiesRequestEvent} from the central manager before completing the
+ * connected future; this means that synchronisation failures will be handled with exponential backoff. If no event
+ * is received within {@link #TIMEOUT_MILLIS} then it is assumed the connection is ready as we could be talking to
+ * an old central manager that doesn't emit the event.
+ */
+public class GatewayIOClient extends WebsocketIOClient<String> {
+
+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, GatewayIOClient.class);
+ protected static final int TIMEOUT_MILLIS = 30000;
+ protected CompletableFuture<Void> syncFuture;
+
+ public GatewayIOClient(URI uri, Map<String, List<String>> headers, OAuthGrant oAuthGrant) {
+ super(uri, headers, oAuthGrant);
+ }
+
+ @Override
+ protected CompletableFuture<Void> createConnectedFuture(ChannelFuture channelStartFuture) {
+ CompletableFuture<Void> connectedFuture = super.createConnectedFuture(channelStartFuture);
+ return connectedFuture.thenCompose(__ ->
+ getFuture()
+ .orTimeout(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)
+ .handle((result, ex) -> {
+ syncFuture = null;
+ if (ex instanceof TimeoutException) {
+ LOG.finest("Timeout reached whilst waiting for sync complete event");
+ } else if (ex != null) {
+ throw new RuntimeException(ex.getMessage());
+ }
+ return null;
+ })
+ );
+ }
+
+ protected CompletableFuture<Void> getFuture() {
+ syncFuture = new CompletableFuture<>();
+ return syncFuture;
+ }
+
+ @Override
+ protected void onMessageReceived(String message) {
+ if (syncFuture != null && message.startsWith(EventRequestResponseWrapper.MESSAGE_PREFIX) && message.contains(GatewayCapabilitiesRequestEvent.TYPE)) {
+ LOG.finest("Gateway connection is now ready");
+ syncFuture.complete(null);
+ }
+
+ super.onMessageReceived(message);
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
index 1847764a1..06b801d42 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
@@ -24,6 +24,7 @@ import org.apache.camel.Exchange;
import org.apache.camel.Predicate;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.message.MessageBrokerService;
+import org.openremote.container.timer.TimerService;
import org.openremote.manager.asset.AssetProcessingException;
import org.openremote.manager.asset.AssetProcessingService;
import org.openremote.manager.asset.AssetStorageService;
@@ -33,6 +34,7 @@ import org.openremote.manager.rules.RulesService;
import org.openremote.manager.rules.RulesetStorageService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.manager.web.ManagerWebService;
import org.openremote.model.Constants;
import org.openremote.model.Container;
import org.openremote.model.ContainerService;
@@ -44,6 +46,7 @@ import org.openremote.model.attribute.AttributeMap;
import org.openremote.model.attribute.AttributeWriteFailure;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.gateway.GatewayDisconnectEvent;
+import org.openremote.model.gateway.GatewayTunnelInfo;
import org.openremote.model.query.AssetQuery;
import org.openremote.model.rules.Ruleset;
import org.openremote.model.security.ClientRole;
@@ -54,8 +57,9 @@ import org.openremote.model.util.TextUtil;
import org.openremote.model.value.MetaItemType;
import java.util.*;
-import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -64,6 +68,8 @@ import java.util.stream.Collectors;
import static org.apache.camel.builder.PredicateBuilder.or;
import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;
import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;
+import static org.openremote.container.util.MapAccess.getInteger;
+import static org.openremote.container.util.MapAccess.getString;
import static org.openremote.manager.gateway.GatewayConnector.mapAssetId;
import static org.openremote.model.Constants.*;
import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
@@ -82,7 +88,12 @@ public class GatewayService extends RouteBuilder implements ContainerService {
// Need a high priority so that event authorizer can get the events before other authorizers
public static final int PRIORITY = HIGH_PRIORITY + 100;
public static final String GATEWAY_CLIENT_ID_PREFIX = "gateway-";
+ public static final String OR_GATEWAY_TUNNEL_SSH_KEY_FILE = "OR_GATEWAY_TUNNEL_SSH_KEY_FILE";
private static final Logger LOG = SyslogCategory.getLogger(GATEWAY, GatewayService.class.getName());
+ public static final String OR_GATEWAY_TUNNEL_SSH_HOSTNAME = "OR_GATEWAY_TUNNEL_SSH_HOSTNAME";
+ public static final String OR_GATEWAY_TUNNEL_SSH_PORT = "OR_GATEWAY_TUNNEL_SSH_PORT";
+ public static final String OR_GATEWAY_TUNNEL_TCP_START = "OR_GATEWAY_TUNNEL_TCP_START";
+ public static final int OR_GATEWAY_TUNNEL_TCP_START_DEFAULT = 9000;
protected AssetStorageService assetStorageService;
protected AssetProcessingService assetProcessingService;
protected ManagerIdentityService identityService;
@@ -91,15 +102,22 @@ public class GatewayService extends RouteBuilder implements ContainerService {
protected RulesetStorageService rulesetStorageService;
protected RulesService rulesService;
protected ScheduledExecutorService executorService;
+ protected TimerService timerService;
+ protected String tunnelSSHHostname;
+ protected int tunnelSSHPort;
+ protected int tunnelTCPStart;
+
/**
* Maps gateway asset IDs to connections; note that gateway asset IDs are stored lower case so that they can be
* matched up to the service user client ID (which needs to be all lower case); this could technically cause an
* ID collision but for now the odds of that are low enough to not be a concern.
*/
- protected final Map<String, GatewayConnector> gatewayConnectorMap = new HashMap<>();
+ protected final Map<String, GatewayConnector> gatewayConnectorMap = new ConcurrentHashMap<>();
protected final Map<String, String> assetIdGatewayIdMap = new HashMap<>();
protected boolean active;
protected List<String> realmIds = new ArrayList<>();
+ protected Map<String, GatewayTunnelInfo> tunnelInfos = new ConcurrentHashMap<>();
+ protected AtomicInteger pendingTunnelCounter = new AtomicInteger();
@SuppressWarnings("unchecked")
public static Predicate isNotForGateway(GatewayService gatewayService) {
@@ -163,6 +181,11 @@ public class GatewayService extends RouteBuilder implements ContainerService {
executorService = container.getExecutorService();
rulesetStorageService = container.getService(RulesetStorageService.class);
rulesService = container.getService(RulesService.class);
+ timerService = container.getService(TimerService.class);
+
+ container.getService(ManagerWebService.class).addApiSingleton(
+ new GatewayServiceResourceImpl(timerService, identityService, this, assetStorageService)
+ );
if (!identityService.isKeycloakEnabled()) {
LOG.warning("Incoming edge gateway connections disabled: Not supported when not using Keycloak identity provider");
@@ -199,6 +222,11 @@ public class GatewayService extends RouteBuilder implements ContainerService {
}
});
}
+
+ // Check tunnelling support
+ tunnelSSHHostname = getString(container.getConfig(), OR_GATEWAY_TUNNEL_SSH_HOSTNAME, null);
+ tunnelSSHPort = getInteger(container.getConfig(), OR_GATEWAY_TUNNEL_SSH_PORT, 0);
+ tunnelTCPStart = getInteger(container.getConfig(), OR_GATEWAY_TUNNEL_TCP_START, OR_GATEWAY_TUNNEL_TCP_START_DEFAULT);
}
@Override
@@ -232,7 +260,7 @@ public class GatewayService extends RouteBuilder implements ContainerService {
}
// Create connector
- GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);
+ GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, this, gateway);
gatewayConnectorMap.put(gateway.getId().toLowerCase(Locale.ROOT), connector);
// Get IDs of all assets under this gateway
@@ -250,10 +278,10 @@ public class GatewayService extends RouteBuilder implements ContainerService {
@Override
public void stop(Container container) throws Exception {
- // TODO: Stop all connectors
gatewayConnectorMap.values().forEach(GatewayConnector::disconnect);
gatewayConnectorMap.clear();
assetIdGatewayIdMap.clear();
+ tunnelInfos.clear();
}
@Override
@@ -319,15 +347,18 @@ public class GatewayService extends RouteBuilder implements ContainerService {
}
if (or(header(SESSION_CLOSE), header(SESSION_CLOSE_ERROR)).matches(exchange)) {
- processGatewayDisconnected(clientId);
+ String sessionKey = ClientEventService.getSessionKey(exchange);
+ processGatewayDisconnected(clientId, sessionKey);
return;
}
// Inbound shared events
if (body().isInstanceOf(SharedEvent.class).matches(exchange)) {
exchange.setRouteStop(true);
+ String sessionKey = ClientEventService.getSessionKey(exchange);
String gatewayId = getGatewayIdFromClientId(clientId);
- onGatewayClientEventReceived(gatewayId, exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class), exchange.getIn().getBody(SharedEvent.class));
+ String messageId = exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class);
+ processGatewayMessage(gatewayId, sessionKey, messageId, exchange.getIn().getBody(SharedEvent.class));
}
}
@@ -335,7 +366,6 @@ public class GatewayService extends RouteBuilder implements ContainerService {
// If the event came from a gateway then we don't want to process it here
if (getClass().getName().equals(event.getSource())) {
-
// Clear out agentlink meta so agent interceptor doesn't try intercepting the event
event.getMeta().remove(MetaItemType.AGENT_LINK);
return false;
@@ -362,9 +392,6 @@ public class GatewayService extends RouteBuilder implements ContainerService {
// Push value into asset for service user method
gatewayAsset.setDisabled(disabled);
createUpdateGatewayServiceUser(gatewayAsset);
- if (disabled) {
- connector.sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
- }
connector.setDisabled(disabled);
}
} else if (GatewayAsset.CLIENT_SECRET.getName().equals(event.getName())) {
@@ -388,6 +415,9 @@ public class GatewayService extends RouteBuilder implements ContainerService {
}
newSecret = identityProvider.resetSecret(event.getRealm(), gatewayServiceUser.getId(), newSecret);
+ // Disconnect current session
+ connector.disconnect();
+
// Update the event value with the potentially newly generated secret
event.setValue(newSecret);
}
@@ -482,6 +512,128 @@ public class GatewayService extends RouteBuilder implements ContainerService {
return connector.deleteGatewayAssets(assetIds);
}
+ public Collection<GatewayTunnelInfo> getTunnelInfos() {
+ return this.tunnelInfos.values();
+ }
+
+ protected boolean tunnellingSupported() {
+ return !TextUtil.isNullOrEmpty(tunnelSSHHostname) && tunnelSSHPort > 0;
+ }
+
+ public GatewayTunnelInfo startTunnel(GatewayTunnelInfo tunnelInfo) throws IllegalArgumentException, IllegalStateException {
+ if (!tunnellingSupported()) {
+ String msg = "Failed to start tunnel: reason=tunnelling is not supported";
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ if (TextUtil.isNullOrEmpty(tunnelInfo.getGatewayId())) {
+ String msg = "Failed to start tunnel: reason=gateway ID cannot be null or empty";
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ String gatewayId = tunnelInfo.getGatewayId().toLowerCase(Locale.ROOT);
+ String realm = tunnelInfo.getRealm();
+ final GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+
+ if (connector == null || !realm.equals(connector.getRealm())) {
+ String msg = "Failed to start tunnel: reason=Gateway disconnected or doesn't exist, id=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ if (!connector.isTunnellingSupported()) {
+ String msg = "Failed to start tunnel: reason=Not supported by gateway, id=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ if (!connector.isConnected()) {
+ String msg = "Failed to start tunnel: reason=Not connected, id=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ if (tunnelInfo.getType() == GatewayTunnelInfo.Type.TCP) {
+ // This is pretty crude but should be robust enough
+ int assignedPort = tunnelTCPStart + Math.toIntExact(pendingTunnelCounter.get() + tunnelInfos.values().stream().filter(ti -> ti.getType() == GatewayTunnelInfo.Type.TCP).count());
+ tunnelInfo.setAssignedPort(assignedPort);
+ }
+ CompletableFuture<Void> startFuture = connector.startTunnel(tunnelInfo);
+ try {
+ pendingTunnelCounter.incrementAndGet();
+ startFuture.get();
+ tunnelInfos.put(tunnelInfo.getId(), tunnelInfo);
+ return tunnelInfo;
+ } catch (ExecutionException e) {
+ if (e.getCause() instanceof TimeoutException) {
+ String msg = "Failed to start tunnel: A timeout occurred whilst waiting for the tunnel to be started: id=" + gatewayId;
+ LOG.log(Level.WARNING, msg);
+ } else {
+ String msg = "Failed to start tunnel: An error occurred whilst waiting for the tunnel to be started: id=" + gatewayId;
+ LOG.log(Level.WARNING, msg, e.getCause());
+ }
+ throw new RuntimeException(e);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ } finally {
+ pendingTunnelCounter.decrementAndGet();
+ }
+ }
+
+ public void stopTunnel(GatewayTunnelInfo tunnelInfo) throws IllegalArgumentException, IllegalStateException {
+ if (!tunnellingSupported()) {
+ String msg = "Failed to stop tunnel: reason=tunnelling is not supported";
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ if (TextUtil.isNullOrEmpty(tunnelInfo.getGatewayId())) {
+ String msg = "Failed to stop tunnel: reason=gateway ID cannot be null or empty";
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ String gatewayId = tunnelInfo.getGatewayId().toLowerCase(Locale.ROOT);
+ String realm = tunnelInfo.getRealm();
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
+
+ if (connector == null || !realm.equals(connector.getRealm())) {
+ String msg = "Failed to stop tunnel: reason=Gateway disconnected or doesn't exist, id=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalStateException(msg);
+ }
+
+ if (!connector.isTunnellingSupported()) {
+ String msg = "Failed to stop tunnel: reason=Not supported by gateway, id=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ if (!connector.isConnected()) {
+ String msg = "Failed to stop tunnel: reason=Not connected, id=" + gatewayId;
+ LOG.info(msg);
+ throw new IllegalArgumentException(msg);
+ }
+
+ // Wait for up to 20 seconds for the tunnel to stop
+ CompletableFuture<Void> stopFuture = connector.stopTunnel(tunnelInfo);
+ try {
+ stopFuture.get(20, TimeUnit.SECONDS);
+ } catch (ExecutionException e) {
+ String msg = "Failed to start tunnel: An error occurred whilst waiting for the tunnel to be started: id=" + gatewayId;
+ LOG.log(Level.WARNING, msg, e.getCause());
+ throw new RuntimeException(msg, e.getCause());
+ } catch (InterruptedException | TimeoutException e) {
+ String msg = "Failed to start tunnel: An error occurred whilst waiting for the tunnel to be started: id=" + gatewayId;
+ LOG.warning(msg);
+ throw new RuntimeException(msg);
+ } finally {
+ tunnelInfos.remove(tunnelInfo.getId(), tunnelInfo);
+ }
+ }
+
/**
* Check if asset ID is a gateway asset registered locally on this manager
*/
@@ -493,10 +645,6 @@ public class GatewayService extends RouteBuilder implements ContainerService {
* 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) {
@@ -517,52 +665,48 @@ public class GatewayService extends RouteBuilder implements ContainerService {
}
protected void processGatewayConnected(String gatewayClientId, String sessionId) {
-
- if (!active) {
- return;
- }
-
String gatewayId = getGatewayIdFromClientId(gatewayClientId);
GatewayConnector connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));
if (connector == null) {
- LOG.warning("Gateway connected but not recognised which shouldn't happen: Gateway ID=" + gatewayId);
+ LOG.warning("Gateway connected but not recognised which shouldn't happen: GatewayID=" + gatewayId);
clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.UNRECOGNISED));
clientEventService.closeSession(sessionId);
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));
- clientEventService.closeSession(sessionId);
- return;
- }
-
if (connector.isDisabled()) {
- LOG.warning("Gateway is currently disabled so will be ignored: Gateway ID=" + gatewayId);
+ LOG.warning("Gateway is currently disabled so will be ignored: " + this);
clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
clientEventService.closeSession(sessionId);
return;
}
- connector.connect(createConnectorMessageConsumer(sessionId), () -> clientEventService.closeSession(sessionId));
+ connector.connected(sessionId, createConnectorMessageConsumer(sessionId), () -> {
+ clientEventService.closeSession(sessionId);
+ tunnelInfos.values().removeIf(tunnelInfo -> tunnelInfo.getGatewayId().equals(gatewayId));
+ });
}
- protected void processGatewayDisconnected(String gatewayClientId) {
+ protected void processGatewayDisconnected(String gatewayClientId, String sessionId) {
+ String gatewayId = getGatewayIdFromClientId(gatewayClientId);
+ GatewayConnector connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));
- if (!active) {
- return;
+ if (connector != null) {
+ connector.disconnected(sessionId);
}
+ }
- String gatewayId = getGatewayIdFromClientId(gatewayClientId);
+ protected void processGatewayMessage(String gatewayId, String sessionId, String messageId, SharedEvent event) {
GatewayConnector connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));
-
if (connector == null) {
return;
}
-
- connector.disconnect();
+ if (!connector.isConnected() || !sessionId.equals(connector.getSessionId())) {
+ LOG.finest("Gateway event received for an obsolete session so ignoring: " + this);
+ return;
+ }
+ connector.onGatewayEvent(messageId, event);
}
protected void processGatewayChange(GatewayAsset gateway, PersistenceEvent<Asset<?>> persistenceEvent) {
@@ -571,10 +715,8 @@ public class GatewayService extends RouteBuilder implements ContainerService {
case CREATE -> {
createUpdateGatewayServiceUser(gateway);
- synchronized (gatewayConnectorMap) {
- GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);
- gatewayConnectorMap.put(gateway.getId().toLowerCase(Locale.ROOT), connector);
- }
+ GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, this, gateway);
+ gatewayConnectorMap.put(gateway.getId().toLowerCase(Locale.ROOT), connector);
}
case UPDATE -> {
// Check if this gateway has a connector
@@ -583,8 +725,6 @@ public class GatewayService extends RouteBuilder implements ContainerService {
break;
}
- connector.gateway = gateway;
-
// Check if disabled
boolean isNowDisabled = gateway.getDisabled().orElse(false);
if (isNowDisabled) {
@@ -609,12 +749,10 @@ public class GatewayService extends RouteBuilder implements ContainerService {
break;
}
- synchronized (gatewayConnectorMap) {
- connector = gatewayConnectorMap.remove(gateway.getId().toLowerCase(Locale.ROOT));
+ connector = gatewayConnectorMap.remove(gateway.getId().toLowerCase(Locale.ROOT));
- if (connector != null) {
- connector.disconnect();
- }
+ if (connector != null) {
+ connector.disconnect();
}
removeGatewayServiceUser(gateway);
@@ -710,11 +848,16 @@ public class GatewayService extends RouteBuilder implements ContainerService {
return msg -> clientEventService.sendToSession(sessionId, msg);
}
- protected void onGatewayClientEventReceived(String gatewayId, String messageId, SharedEvent event) {
- GatewayConnector connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));
- if (connector != null) {
- connector.onGatewayEvent(messageId, event);
- }
+ String getTunnelSSHHostname() {
+ return tunnelSSHHostname;
+ }
+
+ int getTunnelSSHPort() {
+ return tunnelSSHPort;
+ }
+
+ public int getTunnelTCPStart() {
+ return tunnelTCPStart;
}
@Override
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java
new file mode 100644
index 000000000..fa06a8f11
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayServiceResourceImpl.java
@@ -0,0 +1,154 @@
+package org.openremote.manager.gateway;
+
+import jakarta.ws.rs.ForbiddenException;
+import jakarta.ws.rs.WebApplicationException;
+import jakarta.ws.rs.core.Response;
+import org.openremote.container.timer.TimerService;
+import org.openremote.manager.asset.AssetStorageService;
+import org.openremote.manager.security.ManagerIdentityService;
+import org.openremote.manager.web.ManagerWebResource;
+import org.openremote.model.gateway.GatewayServiceResource;
+import org.openremote.model.gateway.GatewayTunnelInfo;
+import org.openremote.model.http.RequestParams;
+import org.openremote.model.util.TextUtil;
+import org.w3c.dom.Text;
+
+public class GatewayServiceResourceImpl extends ManagerWebResource implements GatewayServiceResource {
+
+ protected GatewayService gatewayService;
+ protected AssetStorageService assetStorageService;
+
+ public GatewayServiceResourceImpl(TimerService timerService, ManagerIdentityService identityService, GatewayService gatewayService, AssetStorageService assetStorageService) {
+ super(timerService, identityService);
+ this.gatewayService = gatewayService;
+ this.assetStorageService = assetStorageService;
+ }
+
+ @Override
+ public GatewayTunnelInfo[] getAllActiveTunnelInfos(RequestParams requestParams, String realm) {
+ if (TextUtil.isNullOrEmpty(realm) || !isAuthenticated()) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ if (!isSuperUser() && !getAuthenticatedRealmName().equals(realm)) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ if (isRestrictedUser()) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ return this.gatewayService.getTunnelInfos().stream()
+ .filter(tunnel -> tunnel.getRealm().equals(realm))
+ .toArray(GatewayTunnelInfo[]::new);
+ }
+
+ @Override
+ public GatewayTunnelInfo[] getGatewayActiveTunnelInfos(RequestParams requestParams, String realm, String gatewayId) {
+ if (TextUtil.isNullOrEmpty(realm) || !isAuthenticated()) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ if (!isSuperUser() && !getAuthenticatedRealmName().equals(realm)) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ if (TextUtil.isNullOrEmpty(gatewayId)) {
+ throw new WebApplicationException(Response.Status.BAD_REQUEST);
+ }
+
+ if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), gatewayId)) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ return this.gatewayService.getTunnelInfos().stream()
+ .filter(tunnel -> tunnel.getRealm().equals(realm) && tunnel.getGatewayId().equals(gatewayId))
+ .toArray(GatewayTunnelInfo[]::new);
+ }
+
+ @Override
+ public GatewayTunnelInfo getActiveTunnelInfo(RequestParams requestParams, String realm, String gatewayId, String target, int targetPort) {
+ if (TextUtil.isNullOrEmpty(realm) || !isAuthenticated()) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ if (!isSuperUser() && !getAuthenticatedRealmName().equals(realm)) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ if (TextUtil.isNullOrEmpty(gatewayId)) {
+ throw new WebApplicationException(Response.Status.BAD_REQUEST);
+ }
+
+ if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), gatewayId)) {
+ throw new WebApplicationException(Response.Status.FORBIDDEN);
+ }
+
+ return this.gatewayService.getTunnelInfos().stream()
+ .filter(tunnel -> tunnel.getRealm().equals(realm) && tunnel.getGatewayId().equals(gatewayId) && tunnel.getTarget().equals(target) && tunnel.getTargetPort() == targetPort)
+ .findFirst()
+ .orElse(null);
+ }
+
+ @Override
+ public GatewayTunnelInfo startTunnel(GatewayTunnelInfo tunnelInfo) {
+ if (!isAuthenticated()) {
+ throw new ForbiddenException();
+ }
+
+ if (tunnelInfo == null || TextUtil.isNullOrEmpty(tunnelInfo.getGatewayId())) {
+ throw new WebApplicationException(Response.Status.BAD_REQUEST);
+ }
+
+ if (TextUtil.isNullOrEmpty(tunnelInfo.getRealm())) {
+ tunnelInfo.setRealm(getAuthenticatedRealmName());
+ } else if (!isSuperUser() && !tunnelInfo.getRealm().equals(getAuthenticatedRealmName())) {
+ throw new ForbiddenException();
+ }
+
+ if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), tunnelInfo.getGatewayId())) {
+ throw new ForbiddenException();
+ }
+
+ try {
+ return this.gatewayService.startTunnel(tunnelInfo);
+ } catch (IllegalArgumentException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.BAD_REQUEST);
+ } catch (IllegalStateException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.NOT_FOUND);
+ } catch (RuntimeException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
+ public void stopTunnel(GatewayTunnelInfo tunnelInfo) {
+ if (!isAuthenticated()) {
+ throw new ForbiddenException();
+ }
+
+ if (tunnelInfo == null || TextUtil.isNullOrEmpty(tunnelInfo.getGatewayId())) {
+ throw new WebApplicationException(Response.Status.BAD_REQUEST);
+ }
+
+ if (TextUtil.isNullOrEmpty(tunnelInfo.getRealm())) {
+ tunnelInfo.setRealm(getAuthenticatedRealmName());
+ } else if (!isSuperUser() && !tunnelInfo.getRealm().equals(getAuthenticatedRealmName())) {
+ throw new ForbiddenException();
+ }
+
+ if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), tunnelInfo.getGatewayId())) {
+ throw new ForbiddenException();
+ }
+
+ try {
+ this.gatewayService.stopTunnel(tunnelInfo);
+ } catch (IllegalArgumentException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.BAD_REQUEST);
+ } catch (IllegalStateException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.NOT_FOUND);
+ } catch (RuntimeException e) {
+ throw new WebApplicationException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR);
+ }
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.java
new file mode 100644
index 000000000..3fe3646d7
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayTunnelFactory.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2024, 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.model.gateway.GatewayTunnelInfo;
+import org.openremote.model.gateway.GatewayTunnelStartRequestEvent;
+
+/**
+ * This interface is an abstraction for starting/stopping gateway tunnels and is used by edge gateway instances.
+ */
+public interface GatewayTunnelFactory {
+
+ /**
+ * Start a tunnel for the requested {@link GatewayTunnelInfo}
+ *
+ * @return Tunnel instance counter (should be tracked by implementations as this will be used to stop the tunnel)
+ * @throws jakarta.ws.rs.BadRequestException If {@link GatewayTunnelInfo} is invalid
+ * @throws jakarta.ws.rs.ServerErrorException If the {@link GatewayTunnelInfo} is valid but the tunnel could not be created
+ */
+ void startTunnel(GatewayTunnelStartRequestEvent startRequestEvent) throws Exception;
+
+ void stopTunnel(GatewayTunnelInfo tunnelInfo) throws Exception;
+}
diff --git a/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.java b/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.java
new file mode 100644
index 000000000..23d94b81b
--- /dev/null
+++ b/manager/src/main/java/org/openremote/manager/gateway/JSchGatewayTunnelFactory.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2024, 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 com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JulLogger;
+import com.jcraft.jsch.Session;
+import org.openremote.model.gateway.GatewayTunnelInfo;
+import org.openremote.model.gateway.GatewayTunnelStartRequestEvent;
+
+import java.io.File;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class JSchGatewayTunnelFactory implements GatewayTunnelFactory {
+
+ protected String localhostRewrite;
+ protected File sshKeyFile;
+ protected JSch jSch;
+ protected final Map<GatewayTunnelInfo, Session> sessionMap = new ConcurrentHashMap<>(2);
+
+ public JSchGatewayTunnelFactory(File sshKeyFile, String localhostRewrite) {
+ this.sshKeyFile = sshKeyFile;
+ this.localhostRewrite = localhostRewrite;
+ }
+
+ @Override
+ public void startTunnel(GatewayTunnelStartRequestEvent startRequestEvent) throws Exception {
+ if (sessionMap.containsKey(startRequestEvent.getInfo())) {
+ // Tunnel already exists so nothing to do here
+ return;
+ }
+
+ synchronized (this) {
+ if (jSch == null) {
+ jSch = new JSch();
+ JSch.setLogger(new JulLogger());
+ jSch.addIdentity(sshKeyFile.getAbsolutePath());
+ }
+ }
+
+ GatewayTunnelInfo tunnelInfo = startRequestEvent.getInfo();
+ Session session = jSch.getSession(null, startRequestEvent.getSshHostname(), startRequestEvent.getSshPort());
+ session.setTimeout(10000);
+ session.setConfig("StrictHostKeyChecking", "no");
+ String bindAddress = tunnelInfo.getType() == GatewayTunnelInfo.Type.TCP ? null : tunnelInfo.getId();
+ int rPort = tunnelInfo.getType() == GatewayTunnelInfo.Type.HTTPS ? 443 : tunnelInfo.getType() == GatewayTunnelInfo.Type.HTTP ? 80 : tunnelInfo.getAssignedPort();
+ String target = localhostRewrite != null && "localhost".equals(tunnelInfo.getTarget()) ? localhostRewrite : tunnelInfo.getTarget();
+ session.connect();
+ session.setPortForwardingR(bindAddress, rPort, target, tunnelInfo.getTargetPort());
+ sessionMap.put(tunnelInfo, session);
+ }
+
+ @Override
+ public void stopTunnel(GatewayTunnelInfo tunnelInfo) throws Exception {
+ Session session = sessionMap.remove(tunnelInfo);
+
+ if (session != null) {
+ session.disconnect();
+ }
+ }
+}
diff --git a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
index 784f3a0f6..015a394c0 100644
--- a/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/map/MapResourceImpl.java
@@ -50,7 +50,7 @@ public class MapResourceImpl extends WebResource implements MapResource {
public ObjectNode getSettings(RequestParams requestParams) {
return mapService.getMapSettings(
getRequestRealmName(),
- requestParams.getExternalRequestBaseUri()
+ requestParams.getExternalSchemeHostAndPort()
);
}
@@ -58,7 +58,7 @@ public class MapResourceImpl extends WebResource implements MapResource {
public ObjectNode getSettingsJs(RequestParams requestParams) {
return mapService.getMapSettingsJs(
getAuthenticatedRealmName(),
- requestParams.getExternalRequestBaseUri()
+ requestParams.getExternalSchemeHostAndPort()
);
}
diff --git a/manager/src/main/java/org/openremote/manager/map/MapService.java b/manager/src/main/java/org/openremote/manager/map/MapService.java
index 99e4597e3..d3bc47ee4 100644
--- a/manager/src/main/java/org/openremote/manager/map/MapService.java
+++ b/manager/src/main/java/org/openremote/manager/map/MapService.java
@@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.ResponseCodeHandler;
import io.undertow.server.handlers.proxy.ProxyHandler;
+import jakarta.ws.rs.core.UriInfo;
import org.openremote.container.web.WebService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.web.ManagerWebService;
@@ -36,6 +37,7 @@ import org.openremote.model.util.ValueUtil;
import jakarta.ws.rs.core.UriBuilder;
import java.io.*;
+import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -303,17 +305,17 @@ public class MapService implements ContainerService {
/**
* Dynamically build Mapbox GL settings based on mapsettings.json
*/
- public ObjectNode getMapSettings(String realm, UriBuilder baseUriBuilder) {
-
- if (mapSettings.containsKey(realm)) {
- return mapSettings.get(realm);
+ public ObjectNode getMapSettings(String realm, URI host) {
+ String realmUriKey = realm + host.toString();
+ if (mapSettings.containsKey(realmUriKey)) {
+ return mapSettings.get(realmUriKey);
}
if (mapConfig == null) {
return null;
}
- final ObjectNode settings = mapSettings.computeIfAbsent(realm, r -> {
+ final ObjectNode settings = mapSettings.computeIfAbsent(realmUriKey, r -> {
if (metadata.isValid() && !mapConfig.isEmpty()) {
// Use config as a settings base and convert URLs
return mapConfig.deepCopy();
@@ -346,7 +348,7 @@ public class MapService implements ContainerService {
}));
ArrayNode tilesArray = mapConfig.arrayNode();
- String tileUrl = baseUriBuilder.clone().replacePath(API_PATH).path(realm).path("map/tile").build().toString() + "/{z}/{x}/{y}";
+ String tileUrl = UriBuilder.fromUri(host).replacePath(API_PATH).path(realm).path("map/tile").build().toString() + "/{z}/{x}/{y}";
tilesArray.insert(0, tileUrl);
vectorTilesObj.replace("tiles", tilesArray);
@@ -363,7 +365,7 @@ public class MapService implements ContainerService {
// Set sprite URL to shared folder
Optional.ofNullable(settings.has("sprite") && settings.get("sprite").isTextual() ? settings.get("sprite").asText() : null).ifPresent(sprite -> {
String spriteUri =
- baseUriBuilder.clone()
+ UriBuilder.fromUri(host)
.replacePath(MAP_SHARED_DATA_BASE_URI)
.path(sprite)
.build().toString();
@@ -373,7 +375,7 @@ public class MapService implements ContainerService {
// Set glyphs URL to shared folder (tileserver-gl glyphs url cannot contain a path segment so add /fonts here
Optional.ofNullable(settings.has("glyphs") && settings.get("glyphs").isTextual() ? settings.get("glyphs").asText() : null).ifPresent(glyphs -> {
String glyphsUri =
- baseUriBuilder.clone()
+ UriBuilder.fromUri(host)
.replacePath(MAP_SHARED_DATA_BASE_URI)
.build().toString() + "/fonts/" + glyphs;
settings.put("glyphs", glyphsUri);
@@ -385,20 +387,20 @@ public class MapService implements ContainerService {
/**
* Dynamically build Mapbox JS settings based on mapsettings.json
*/
- public ObjectNode getMapSettingsJs(String realm, UriBuilder baseUriBuilder) {
-
- if (mapSettingsJs.containsKey(realm)) {
- return mapSettingsJs.get(realm);
+ public ObjectNode getMapSettingsJs(String realm, URI host) {
+ String realmUriKey = realm + host.toString();
+ if (mapSettingsJs.containsKey(realmUriKey)) {
+ return mapSettingsJs.get(realmUriKey);
}
- final ObjectNode settings = mapSettingsJs.computeIfAbsent(realm, r -> ValueUtil.JSON.createObjectNode());
+ final ObjectNode settings = mapSettingsJs.computeIfAbsent(realmUriKey, r -> ValueUtil.JSON.createObjectNode());
if (!metadata.isValid() || mapConfig.isEmpty()) {
return settings;
}
ArrayNode tilesArray = ValueUtil.JSON.createArrayNode();
- String tileUrl = baseUriBuilder.clone().replacePath(RASTER_MAP_TILE_PATH).build().toString() + "/{z}/{x}/{y}.png";
+ String tileUrl = UriBuilder.fromUri(host).replacePath(RASTER_MAP_TILE_PATH).build().toString() + "/{z}/{x}/{y}.png";
tilesArray.insert(0, tileUrl);
settings.replace("options", mapConfig.has("options") && mapConfig.get("options").isObject() ? (ObjectNode)mapConfig.get("options") : null);
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
index a3ac8e029..1da10f77e 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java
@@ -55,7 +55,7 @@ import org.keycloak.adapters.jaas.AbstractKeycloakLoginModule;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
import org.openremote.container.timer.TimerService;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.manager.asset.AssetProcessingService;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.event.ClientEventService;
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java
index 5ac7e9986..650f3e58d 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java
@@ -26,7 +26,7 @@ import org.apache.camel.builder.RouteBuilder;
import org.keycloak.KeycloakSecurityContext;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.timer.TimerService;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.provisioning.ProvisioningService;
import org.openremote.manager.security.ManagerIdentityService;
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 804c126df..96223be48 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -37,7 +37,7 @@ import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.security.keycloak.KeycloakIdentityProvider;
import org.openremote.container.timer.TimerService;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.container.web.WebService;
import org.openremote.manager.apps.ConsoleAppService;
import org.openremote.manager.asset.AssetStorageService;
diff --git a/manager/src/main/java/org/openremote/manager/syslog/SyslogResourceImpl.java b/manager/src/main/java/org/openremote/manager/syslog/SyslogResourceImpl.java
index bc5c6a7e4..3dceab64f 100644
--- a/manager/src/main/java/org/openremote/manager/syslog/SyslogResourceImpl.java
+++ b/manager/src/main/java/org/openremote/manager/syslog/SyslogResourceImpl.java
@@ -27,7 +27,7 @@ import org.openremote.model.util.Pair;
import jakarta.ws.rs.BeanParam;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
-import java.net.URI;
+
import java.time.Instant;
import java.util.Collections;
import java.util.List;
@@ -65,16 +65,13 @@ public class SyslogResourceImpl extends WebResource implements SyslogResource {
long lastPage = (result.key / perPage) + 1L;
Response.ResponseBuilder rb = Response.ok(result.value.toArray(new SyslogEvent[0]));
- URI requestBaseUri = requestParams.getExternalRequestBaseUri().build(); // This gives request base from in front of proxy
- UriBuilder requestUriBuilder = requestParams.uriInfo.getRequestUriBuilder();
- requestUriBuilder.scheme(requestBaseUri.getScheme()).host(requestBaseUri.getHost()).port(requestBaseUri.getPort());
+ UriBuilder requestUriBuilder = requestParams.getExternalBaseUriBuilder();
if (page != lastPage) {
rb.link(requestUriBuilder.replaceQueryParam("page", page + 1).build(), "next");
}
rb.link(requestUriBuilder.replaceQueryParam("page", lastPage).build(), "last");
-
return rb.build();
}
diff --git a/manager/src/main/resources/org/openremote/manager/setup/database/V20240508_01__Gateways.sql b/manager/src/main/resources/org/openremote/manager/setup/database/V20240508_01__Gateways.sql
new file mode 100644
index 000000000..5eb0e7244
--- /dev/null
+++ b/manager/src/main/resources/org/openremote/manager/setup/database/V20240508_01__Gateways.sql
@@ -0,0 +1,2 @@
+-- Add tunnelSupported attribute for GatewayAsset
+SELECT a.id, ADD_ATTRIBUTE(a, 'tunnelingSupported', 'boolean', null, now(), null) from asset a where a.type = 'GatewayAsset';
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index 82022cb17..a966d4d94 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -322,6 +322,29 @@
},
"gatewayConnection": "Manager interconnect",
"gateway": "Manager interconnect",
+ "gatewayTunnel": "Gateway tunnel",
+ "gatewayTunnels": {
+ "": "Gateway tunnels",
+ "settings": "Gateway settings",
+ "start": "Start",
+ "open": "Open",
+ "stop": "Stop",
+ "stopWarning": "This action closes the tunnel, and terminates all active connections.",
+ "copyAddress": "Copy address",
+ "copySuccess": "Copied the tunnel address to your clipboard",
+ "id": "ID",
+ "gatewayId": "Gateway ID",
+ "type": "Type",
+ "target": "Target",
+ "targetPort": "Target port",
+ "assignedPort": "Assigned port",
+ "protocol": "Protocol",
+ "createNew": "Add new Gateway tunnel",
+ "selectAsset": "Select gateway asset",
+ "closeSuccessful": "Closed tunnel successfully"
+ },
+ "tunnels": "tunnels",
+ "tunnel": "tunnel",
"connectionDetails": "CONNECTION DETAILS",
"secured": "Secured",
"clientSecret": "Client secret",
diff --git a/manager/src/web/shared/locales/nl/or.json b/manager/src/web/shared/locales/nl/or.json
index fad167f29..f6c71b155 100644
--- a/manager/src/web/shared/locales/nl/or.json
+++ b/manager/src/web/shared/locales/nl/or.json
@@ -322,6 +322,28 @@
},
"gatewayConnection": "Manager interconnect",
"gateway": "Manager interconnect",
+ "gatewayTunnel": "Gateway tunnel",
+ "gatewayTunnels": {
+ "": "Gateway tunnels",
+ "start": "Start",
+ "open": "Open",
+ "stop": "Stop",
+ "stopWarning": "Deze actie sluit de tunnel, en beëindigt alle actieve verbindingen.",
+ "copyAddress": "Kopieer adres",
+ "copySuccess": "Adres gekopieerd naar je klipbord",
+ "id": "ID",
+ "gatewayId": "Gateway ID",
+ "type": "Type",
+ "target": "Target",
+ "targetPort": "Target poort",
+ "assignedPort": "Toegewezen poort",
+ "protocol": "Protocol",
+ "createNew": "Maak een nieuwe Gateway tunnel",
+ "selectAsset": "Selecteer gateway asset",
+ "closeSuccessful": "Tunnel gesloten"
+ },
+ "tunnels": "tunnels",
+ "tunnel": "tunnel",
"connectionDetails": "CONNECTIE DETAILS",
"secured": "Beveiligd",
"clientSecret": "Client wachtwoord",
diff --git a/model/build.gradle b/model/build.gradle
index cb37f8c31..0dd36d6f7 100644
--- a/model/build.gradle
+++ b/model/build.gradle
@@ -33,6 +33,8 @@ dependencies {
api "org.scala-lang:scala-library:2.12.10"
api "io.github.classgraph:classgraph:$classgraphVersion"
api "io.micrometer:micrometer-core:$micrometerVersion"
+ api "com.devskiller.friendly-id:friendly-id:$friendlyIdVersion"
+ api "com.fasterxml.uuid:java-uuid-generator:$uuidGeneratorVersion"
}
jar {
diff --git a/model/src/main/java/org/openremote/model/asset/impl/GatewayAsset.java b/model/src/main/java/org/openremote/model/asset/impl/GatewayAsset.java
index 9c660abda..ec7e03948 100644
--- a/model/src/main/java/org/openremote/model/asset/impl/GatewayAsset.java
+++ b/model/src/main/java/org/openremote/model/asset/impl/GatewayAsset.java
@@ -37,7 +37,7 @@ public class GatewayAsset extends Asset<GatewayAsset> {
public static final AttributeDescriptor<String> CLIENT_SECRET = new AttributeDescriptor<>("clientSecret", ValueType.UUID);
public static final AttributeDescriptor<ConnectionStatus> STATUS = new AttributeDescriptor<>("gatewayStatus", ValueType.CONNECTION_STATUS, new MetaItem<>(MetaItemType.READ_ONLY));
public static final AttributeDescriptor<Boolean> DISABLED = new AttributeDescriptor<>("disabled", ValueType.BOOLEAN);
-
+ public static final AttributeDescriptor<Boolean> TUNNELING_SUPPORTED = new AttributeDescriptor<>("tunnelingSupported", ValueType.BOOLEAN, new MetaItem<>(MetaItemType.READ_ONLY));
public static final AssetDescriptor<GatewayAsset> DESCRIPTOR = new AssetDescriptor<>("router-wireless", null, GatewayAsset.class);
/**
@@ -85,4 +85,11 @@ public class GatewayAsset extends Asset<GatewayAsset> {
getAttributes().getOrCreate(DISABLED).setValue(disabled);
return this;
}
+
+ public Optional<Boolean> getTunnelingSupported() { return getAttributes().getValue(TUNNELING_SUPPORTED); }
+
+ public GatewayAsset setTunnelingSupported(Boolean supported) {
+ getAttributes().getOrCreate(TUNNELING_SUPPORTED).setValue(supported);
+ return this;
+ }
}
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 1bc3bd5cf..b81d7027e 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
@@ -24,8 +24,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.openremote.model.asset.*;
import org.openremote.model.attribute.AttributeEvent;
import org.openremote.model.event.Event;
-import org.openremote.model.gateway.GatewayConnectionStatusEvent;
-import org.openremote.model.gateway.GatewayDisconnectEvent;
+import org.openremote.model.gateway.*;
import org.openremote.model.rules.RulesEngineStatusEvent;
import org.openremote.model.rules.RulesetChangedEvent;
import org.openremote.model.simulator.RequestSimulatorState;
@@ -50,6 +49,12 @@ import org.openremote.model.syslog.SyslogEvent;
@JsonSubTypes.Type(value = RulesetChangedEvent.class, name = "ruleset-changed"),
@JsonSubTypes.Type(value = GatewayDisconnectEvent.class, name = "gateway-disconnect"),
@JsonSubTypes.Type(value = GatewayConnectionStatusEvent.class, name = "gateway-connection-status"),
+ @JsonSubTypes.Type(value = GatewayCapabilitiesRequestEvent.class, name = GatewayCapabilitiesRequestEvent.TYPE),
+ @JsonSubTypes.Type(value = GatewayCapabilitiesResponseEvent.class, name = GatewayCapabilitiesResponseEvent.TYPE),
+ @JsonSubTypes.Type(value = GatewayTunnelStartRequestEvent.class, name = "gateway-tunnel-start-request"),
+ @JsonSubTypes.Type(value = GatewayTunnelStartResponseEvent.class, name = "gateway-tunnel-start-response"),
+ @JsonSubTypes.Type(value = GatewayTunnelStopRequestEvent.class, name = "gateway-tunnel-stop-request"),
+ @JsonSubTypes.Type(value = GatewayTunnelStopResponseEvent.class, name = "gateway-tunnel-stop-response"),
@JsonSubTypes.Type(value = DeleteAssetsRequestEvent.class, name = "delete-assets-request"),
@JsonSubTypes.Type(value = DeleteAssetsResponseEvent.class, name = "delete-assets-response")
})
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayCapabilitiesRequestEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayCapabilitiesRequestEvent.java
new file mode 100644
index 000000000..67d9e9f32
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayCapabilitiesRequestEvent.java
@@ -0,0 +1,25 @@
+package org.openremote.model.gateway;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+
+import java.util.Date;
+
+/**
+ * Request the gateway to return a {@link GatewayCapabilitiesResponseEvent}
+ */
+public class GatewayCapabilitiesRequestEvent extends SharedEvent {
+
+ public static final String TYPE = "gateway-capabilities-request";
+
+ @JsonCreator
+ public GatewayCapabilitiesRequestEvent(@JsonProperty("timestamp") Date timestamp) {
+ super(timestamp != null ? timestamp.getTime() : new Date().getTime());
+ }
+
+ public GatewayCapabilitiesRequestEvent() {
+
+ }
+
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayCapabilitiesResponseEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayCapabilitiesResponseEvent.java
new file mode 100644
index 000000000..77f44bfc9
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayCapabilitiesResponseEvent.java
@@ -0,0 +1,27 @@
+package org.openremote.model.gateway;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.event.shared.SharedEvent;
+
+import java.util.Date;
+
+public class GatewayCapabilitiesResponseEvent extends SharedEvent {
+
+ public static final String TYPE = "gateway-capabilities-response";
+ protected final boolean tunnelingSupported;
+
+ @JsonCreator
+ public GatewayCapabilitiesResponseEvent(@JsonProperty("timestamp") Date timestamp, @JsonProperty("tunnelingSupported") boolean tunnelingSupported) {
+ super(timestamp != null ? timestamp.getTime() : new Date().getTime());
+ this.tunnelingSupported = tunnelingSupported;
+ }
+
+ public GatewayCapabilitiesResponseEvent(final boolean tunnelingSupported) {
+ this.tunnelingSupported = tunnelingSupported;
+ }
+
+ public boolean isTunnelingSupported() {
+ return tunnelingSupported;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java b/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java
index a594d5bbe..6953debf0 100644
--- a/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayConnection.java
@@ -186,8 +186,6 @@ public class GatewayConnection {
", 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
index 95a1cf44c..30129fc44 100644
--- a/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayDisconnectEvent.java
@@ -26,7 +26,7 @@ import org.openremote.model.event.shared.SharedEvent;
import java.util.Date;
/**
- * Used to indicate to a connected gateway should should disconnect
+ * Used to indicate to a connected gateway should disconnect
* with the {@link #reason} indicating why the disconnect has been
* requested.
*/
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayServiceResource.java b/model/src/main/java/org/openremote/model/gateway/GatewayServiceResource.java
new file mode 100644
index 000000000..5ecef0150
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayServiceResource.java
@@ -0,0 +1,54 @@
+package org.openremote.model.gateway;
+
+import io.swagger.v3.oas.annotations.tags.Tag;
+import jakarta.annotation.security.RolesAllowed;
+import jakarta.ws.rs.*;
+import org.openremote.model.Constants;
+import org.openremote.model.http.RequestParams;
+
+import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
+
+@Tag(name = "Gateway")
+@Path("gateway")
+public interface GatewayServiceResource {
+
+ /**
+ * TODO: write docs
+ */
+ @GET
+ @Path("tunnel/{realm}")
+ @Produces(APPLICATION_JSON)
+ @RolesAllowed({Constants.READ_ADMIN_ROLE})
+ GatewayTunnelInfo[] getAllActiveTunnelInfos(@BeanParam RequestParams requestParams, @PathParam("realm") String realm);
+
+ /**
+ * TODO: write docs
+ */
+ @GET
+ @Path("tunnel/{realm}/{id}")
+ @Produces(APPLICATION_JSON)
+ GatewayTunnelInfo[] getGatewayActiveTunnelInfos(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("id") String gatewayId);
+
+ @GET
+ @Path("tunnel/{realm}/{id}/{target}/{targetPort}")
+ @Produces(APPLICATION_JSON)
+ GatewayTunnelInfo getActiveTunnelInfo(@BeanParam RequestParams requestParams, @PathParam("realm") String realm, @PathParam("id") String gatewayId, @PathParam("target") String target, @PathParam("targetPort") int targetPort);
+
+ /**
+ * TODO: write docs
+ */
+ @POST
+ @Path("tunnel")
+ @Consumes(APPLICATION_JSON)
+ @Produces(APPLICATION_JSON)
+ GatewayTunnelInfo startTunnel(GatewayTunnelInfo tunnelInfo);
+
+ /**
+ * TODO: write docs
+ */
+ @DELETE
+ @Path("tunnel")
+ @Consumes(APPLICATION_JSON)
+ @Produces(APPLICATION_JSON)
+ void stopTunnel(GatewayTunnelInfo tunnelInfo);
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayTunnelInfo.java b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelInfo.java
new file mode 100644
index 000000000..a949bbbb0
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelInfo.java
@@ -0,0 +1,124 @@
+package org.openremote.model.gateway;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.openremote.model.util.UniqueIdentifierGenerator;
+
+import java.util.Locale;
+import java.util.Objects;
+
+public class GatewayTunnelInfo {
+
+ public enum Type {
+ HTTPS,
+ HTTP,
+ TCP
+ }
+
+ protected String gatewayId;
+ protected String realm;
+ protected int targetPort = 443;
+ protected String target = "localhost";
+ protected Integer assignedPort = null;
+ protected Type type = Type.HTTPS;
+
+ @JsonCreator
+ public GatewayTunnelInfo(String realm, String gatewayId) {
+ this.realm = realm;
+ this.gatewayId = gatewayId;
+ }
+
+ public GatewayTunnelInfo(String realm, String gatewayId, Type type, String target, int targetPort) {
+ this.gatewayId = gatewayId;
+ this.realm = realm;
+ this.targetPort = targetPort;
+ this.target = target;
+ this.type = type;
+ }
+
+ public String getGatewayId() {
+ return gatewayId;
+ }
+
+ public String getRealm() {
+ return realm;
+ }
+
+ public int getTargetPort() {
+ return targetPort;
+ }
+
+ public String getTarget() {
+ return target;
+ }
+
+ public Type getType() {
+ return type;
+ }
+
+ public Integer getAssignedPort() {
+ return assignedPort;
+ }
+
+ public GatewayTunnelInfo setGatewayId(String gatewayId) {
+ this.gatewayId = gatewayId;
+ return this;
+ }
+
+ public GatewayTunnelInfo setRealm(String realm) {
+ this.realm = realm;
+ return this;
+ }
+
+ public GatewayTunnelInfo setTargetPort(int targetPort) {
+ this.targetPort = targetPort;
+ return this;
+ }
+
+ public GatewayTunnelInfo setTarget(String target) {
+ this.target = target;
+ return this;
+ }
+
+ public GatewayTunnelInfo setType(Type type) {
+ this.type = type;
+ return this;
+ }
+
+ public GatewayTunnelInfo setAssignedPort(Integer assignedPort) {
+ this.assignedPort = assignedPort;
+ return this;
+ }
+
+ @JsonProperty
+ public String getId() {
+ String seed = gatewayId + target + targetPort;
+ return "gw-" + UniqueIdentifierGenerator.generateId(seed).toLowerCase(Locale.ROOT);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ GatewayTunnelInfo that = (GatewayTunnelInfo) o;
+ return targetPort == that.targetPort && Objects.equals(gatewayId, that.gatewayId) && Objects.equals(target, that.target);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(gatewayId, targetPort, target);
+ }
+
+ @Override
+ public String toString() {
+ return GatewayTunnelInfo.class.getSimpleName() + "{" +
+ "id='" + getId() + '\'' +
+ ", gatewayId='" + gatewayId + '\'' +
+ ", realm='" + realm + '\'' +
+ ", targetPort=" + targetPort +
+ ", target='" + target + '\'' +
+ ", type='" + type + '\'' +
+ ", assignedPort=" + assignedPort +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStartRequestEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStartRequestEvent.java
new file mode 100644
index 000000000..8939e9bf5
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStartRequestEvent.java
@@ -0,0 +1,39 @@
+package org.openremote.model.gateway;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import org.openremote.model.event.shared.SharedEvent;
+
+public class GatewayTunnelStartRequestEvent extends SharedEvent {
+
+ protected String sshHostname;
+ protected int sshPort;
+ protected GatewayTunnelInfo info;
+
+ @JsonCreator
+ public GatewayTunnelStartRequestEvent(String sshHostname, int sshPort, GatewayTunnelInfo info) {
+ this.sshHostname = sshHostname;
+ this.sshPort = sshPort;
+ this.info = info;
+ }
+
+ public GatewayTunnelInfo getInfo() {
+ return info;
+ }
+
+ public String getSshHostname() {
+ return sshHostname;
+ }
+
+ public int getSshPort() {
+ return sshPort;
+ }
+
+ @Override
+ public String toString() {
+ return GatewayTunnelStartRequestEvent.class.getSimpleName() + "{" +
+ "sshHostname='" + sshHostname + '\'' +
+ ", sshPort=" + sshPort +
+ ", info=" + info +
+ '}';
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStartResponseEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStartResponseEvent.java
new file mode 100644
index 000000000..b34778361
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStartResponseEvent.java
@@ -0,0 +1,19 @@
+package org.openremote.model.gateway;
+
+import org.openremote.model.event.shared.SharedEvent;
+
+public class GatewayTunnelStartResponseEvent extends SharedEvent {
+
+ protected String error;
+
+ public GatewayTunnelStartResponseEvent() {
+ }
+
+ public GatewayTunnelStartResponseEvent(String error) {
+ this.error = error;
+ }
+
+ public String getError() {
+ return error;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStopRequestEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStopRequestEvent.java
new file mode 100644
index 000000000..9881e6ef3
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStopRequestEvent.java
@@ -0,0 +1,20 @@
+package org.openremote.model.gateway;
+
+import org.openremote.model.event.shared.SharedEvent;
+
+public class GatewayTunnelStopRequestEvent extends SharedEvent {
+
+ protected GatewayTunnelInfo info;
+
+ protected GatewayTunnelStopRequestEvent() {
+
+ }
+
+ public GatewayTunnelStopRequestEvent(GatewayTunnelInfo info) {
+ this.info = info;
+ }
+
+ public GatewayTunnelInfo getInfo() {
+ return info;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStopResponseEvent.java b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStopResponseEvent.java
new file mode 100644
index 000000000..54e66a915
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/gateway/GatewayTunnelStopResponseEvent.java
@@ -0,0 +1,19 @@
+package org.openremote.model.gateway;
+
+import org.openremote.model.event.shared.SharedEvent;
+
+public class GatewayTunnelStopResponseEvent extends SharedEvent {
+
+ protected String error;
+
+ public GatewayTunnelStopResponseEvent() {
+ }
+
+ public GatewayTunnelStopResponseEvent(String error) {
+ this.error = error;
+ }
+
+ public String getError() {
+ return error;
+ }
+}
diff --git a/model/src/main/java/org/openremote/model/http/RequestParams.java b/model/src/main/java/org/openremote/model/http/RequestParams.java
index 3a677277a..b7f9e3080 100644
--- a/model/src/main/java/org/openremote/model/http/RequestParams.java
+++ b/model/src/main/java/org/openremote/model/http/RequestParams.java
@@ -48,11 +48,15 @@ public class RequestParams {
return authorization.split(" ")[1];
}
+ public URI getExternalSchemeHostAndPort() {
+ return getExternalBaseUriBuilder().replacePath("").build();
+ }
+
/**
* Handles reverse proxying and returns the request base URI
*/
- public UriBuilder getExternalRequestBaseUri() {
- URI uri = this.uriInfo.getRequestUri();
+ public UriBuilder getExternalBaseUriBuilder() {
+ URI uri = this.uriInfo.getBaseUri();
String scheme = TextUtil.isNullOrEmpty(this.forwardedProtoHeader) ? uri.getScheme() : this.forwardedProtoHeader;
int port = uri.getPort();
String host = uri.getHost();
diff --git a/container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java b/model/src/main/java/org/openremote/model/util/UniqueIdentifierGenerator.java
similarity index 74%
rename from container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java
rename to model/src/main/java/org/openremote/model/util/UniqueIdentifierGenerator.java
index 010196c26..923e7de4a 100644
--- a/container/src/main/java/org/openremote/container/util/UniqueIdentifierGenerator.java
+++ b/model/src/main/java/org/openremote/model/util/UniqueIdentifierGenerator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016, OpenRemote Inc.
+ * Copyright 2024, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
@@ -17,40 +17,25 @@
* 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.container.util;
+package org.openremote.model.util;
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.hibernate.HibernateException;
-import org.hibernate.engine.spi.SharedSessionContractImplementor;
-import org.hibernate.id.IdentifierGenerator;
import org.openremote.model.IdentifiableEntity;
-import java.io.Serializable;
-
/**
* Generate a globally unique identifier value.
* <p>
* This is a Hibernate identifier generator as well: Assigns a random UUID if the persisted entity instance is of
* type {@link IdentifiableEntity} and if its {@link IdentifiableEntity#getId()} method returns <code>null</code>.
*/
-public class UniqueIdentifierGenerator implements IdentifierGenerator {
+public class UniqueIdentifierGenerator {
static final protected RandomBasedGenerator randomGenerator = Generators.randomBasedGenerator();
static final protected NameBasedGenerator nameGenerator = Generators.nameBasedGenerator();
- @Override
- public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
- if (object instanceof IdentifiableEntity) {
- IdentifiableEntity<?> identifiableEntity = (IdentifiableEntity<?>) object;
- if (identifiableEntity.getId() != null)
- return identifiableEntity.getId();
- }
- return generateId();
- }
-
/**
* Generates a random UUID value encoded as Base62, 22 characters long.
*/
diff --git a/profile/deploy.yml b/profile/deploy.yml
index 43b1ecfcc..bf7a353b5 100644
--- a/profile/deploy.yml
+++ b/profile/deploy.yml
@@ -71,6 +71,14 @@ services:
# e.g. '--staging'
LE_EXTRA_ARGS: ${PROXY_EXTRA_ARGS:-}
+ # Provide this value if requesting wildcard certs with Route53 DNS zone; this should be the role to assume to
+ # perform route53 operations if calling across AWS accounts
+ AWS_ROUTE53_ROLE:
+
+ # Enable SISH HTTP tunnel forwarding
+ #SISH_HOST: sish
+ #SISH_PORT: 8090
+
# The proxy supports up to 10 custom redirects from hostname to any path on the
# manager service.
# e.g. PROXY_HOST_REDIRECT_1_NAME: www.my-console-master.tld
@@ -177,6 +185,14 @@ services:
# Console applications can register for push notifications through FCM.
OR_FIREBASE_CONFIG_FILE: ${OR_FIREBASE_CONFIG_FILE:-/deployment/manager/fcm.json}
+ # Configure the central instance for Gateway SSH tunnelling (if these are set then gateway tunnelling support is enabled on the central instance)
+ OR_GATEWAY_TUNNEL_SSH_HOSTNAME:
+ OR_GATEWAY_TUNNEL_SSH_PORT:
+ OR_GATEWAY_TUNNEL_TCP_START: # First TCP port that can be allocated for raw TCP connections (should be start of an assigned contiguous block e.g. 9000-10000 = 9000)
+ # Configure the edge instance for Gateway SSH tunnelling (if SSH_KEY_FILE is set ten gateway tunnelling support is enabled on te edge instance)
+ OR_GATEWAY_TUNNEL_SSH_KEY_FILE: # SSH key file in RSA (not openSSH) format (use ssh-keygen -p -f ~/.ssh/id_rsa -m pem) to convert from openSSH
+ OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE: # Override default rewriting of localhost tunnelling to work within Docker
+
# Developer mode (see code for usages of OR_DEV_MODE flag)
OR_DEV_MODE: ${OR_DEV_MODE:-false}
@@ -408,3 +424,33 @@ services:
restart: always
#ports:
# - "8082:80" # Web server port
+
+
+ sish:
+ image: antoniomika/sish:latest
+ depends_on:
+ - proxy
+ healthcheck:
+ test: ["CMD", "/app/app", "-v"]
+ ports:
+ - "2222:2222"
+ - "9000-10000:9000-10000"
+ volumes:
+ - proxy-data:/etc/letsencrypt
+ - deployment-data:/deployment
+ #--https-request-port-override=8443
+ #--https-certificate-directory=
+ command: |
+ --ssh-address=:2222
+ --http-address=:8090
+ --https=false
+ --verify-ssl=false
+ --idle-connection=false
+ --authentication-keys-directory=/deployment/sish/pubkeys
+ --private-keys-directory=/deployment/sish/keys
+ --port-bind-range="9000-10000"
+ --bind-random-ports=false
+ --bind-random-subdomains=false
+ --domain=${OR_HOSTNAME}
+ --force-requested-subdomains=true
+ restart: always
diff --git a/profile/dev-proxy.yml b/profile/dev-proxy.yml
index 68d634bd8..249942df0 100644
--- a/profile/dev-proxy.yml
+++ b/profile/dev-proxy.yml
@@ -34,6 +34,10 @@ services:
# Access directly if needed on localhost
ports:
- "8081:8080"
+ # Following options are useful for tunnelling functionality do not use on public instance
+ environment:
+ KEYCLOAK_ISSUER_BASE_URI: https://localhost/auth
+ KC_HOSTNAME_STRICT: false
depends_on:
postgresql:
condition: service_healthy
diff --git a/project.gradle b/project.gradle
index bd72d5680..4ba8f011a 100644
--- a/project.gradle
+++ b/project.gradle
@@ -126,34 +126,12 @@ if (project == rootProject) {
OR_SETUP_TYPE: "demo"
]
}
- "Demo Setup with Proxy"(Application) {
- moduleName = getProject().idea.module.name + (isCustomProject() ? ".openremote.setup.demo" : ".setup.demo")
- envs = [
- OR_SETUP_TYPE: "demo",
- OR_WEBSERVER_LISTEN_HOST: "0.0.0.0",
- OR_SSL_PORT: "-1"
- ]
- }
"Test Setup"(Application) {
moduleName = getProject().idea.module.name + (isCustomProject() ? ".openremote.setup.integration" : ".setup.integration")
}
- "Test Setup with Proxy"(Application) {
- moduleName = getProject().idea.module.name + (isCustomProject() ? ".openremote.setup.integration" : ".setup.integration")
- envs = [
- OR_WEBSERVER_LISTEN_HOST: "0.0.0.0",
- OR_SSL_PORT: "-1"
- ]
- }
"Empty"(Application) {
moduleName = getProject().idea.module.name + (isCustomProject() ? ".openremote.manager.main" : ".manager.main")
}
- "Empty with Proxy"(Application) {
- moduleName = getProject().idea.module.name + (isCustomProject() ? ".openremote.manager.main" : ".manager.main")
- envs = [
- OR_WEBSERVER_LISTEN_HOST: "0.0.0.0",
- OR_SSL_PORT: "-1"
- ]
- }
}
}
diff --git a/setup/src/demo/java/org/openremote/setup/demo/ManagerDemoSetup.java b/setup/src/demo/java/org/openremote/setup/demo/ManagerDemoSetup.java
index 5b941e66d..5989d536a 100644
--- a/setup/src/demo/java/org/openremote/setup/demo/ManagerDemoSetup.java
+++ b/setup/src/demo/java/org/openremote/setup/demo/ManagerDemoSetup.java
@@ -23,7 +23,7 @@ import org.openremote.agent.protocol.http.HTTPAgent;
import org.openremote.agent.protocol.http.HTTPAgentLink;
import org.openremote.agent.protocol.simulator.SimulatorAgent;
import org.openremote.agent.protocol.simulator.SimulatorAgentLink;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.manager.security.ManagerIdentityProvider;
import org.openremote.manager.setup.ManagerSetup;
import org.openremote.model.Constants;
diff --git a/setup/src/integration/java/org/openremote/setup/integration/KeycloakTestSetup.java b/setup/src/integration/java/org/openremote/setup/integration/KeycloakTestSetup.java
index 3e7a83f72..55c27254e 100644
--- a/setup/src/integration/java/org/openremote/setup/integration/KeycloakTestSetup.java
+++ b/setup/src/integration/java/org/openremote/setup/integration/KeycloakTestSetup.java
@@ -21,7 +21,7 @@ package org.openremote.setup.integration;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.RoleRepresentation;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.manager.security.ManagerIdentityProvider;
import org.openremote.manager.setup.AbstractKeycloakSetup;
import org.openremote.model.Constants;
diff --git a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
index 73158fbf8..3fc1b01b8 100644
--- a/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
+++ b/setup/src/integration/java/org/openremote/setup/integration/ManagerTestSetup.java
@@ -21,7 +21,7 @@ package org.openremote.setup.integration;
import org.openremote.agent.protocol.simulator.SimulatorAgent;
import org.openremote.agent.protocol.simulator.SimulatorAgentLink;
-import org.openremote.container.util.UniqueIdentifierGenerator;
+import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.manager.setup.ManagerSetup;
import org.openremote.model.Constants;
import org.openremote.model.Container;
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy
index a6e9c9514..b869c33b6 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointQueryTest.groovy
@@ -1,6 +1,6 @@
package org.openremote.test.assets
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.datapoint.AssetDatapointService
import org.openremote.manager.setup.SetupService
@@ -16,7 +16,6 @@ import org.openremote.model.datapoint.query.AssetDatapointLTTBQuery
import org.openremote.model.geo.GeoJSONPoint
import org.openremote.model.query.AssetQuery
import org.openremote.model.query.filter.RealmPredicate
-import org.openremote.model.util.Pair
import org.openremote.model.value.MetaItemType
import org.openremote.model.value.ValueType
import org.openremote.setup.integration.KeycloakTestSetup
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
index 595dca23e..27902980e 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetIntegrityTest.groovy
@@ -3,7 +3,7 @@ package org.openremote.test.assets
import jakarta.ws.rs.WebApplicationException
import org.jboss.resteasy.api.validation.ViolationReport
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.setup.SetupService
import org.openremote.model.asset.AssetResource
import org.openremote.model.asset.impl.RoomAsset
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 0068dc9a0..31b940eea 100644
--- a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
@@ -4,7 +4,7 @@ package org.openremote.test.console
import com.google.firebase.messaging.Message
import jakarta.ws.rs.WebApplicationException
import org.openremote.container.timer.TimerService
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.notification.NotificationService
import org.openremote.manager.notification.PushNotificationHandler
diff --git a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationTest.groovy b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationTest.groovy
index c8b918331..e061e57a2 100644
--- a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationTest.groovy
@@ -1,16 +1,11 @@
package org.openremote.test.energy
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.energy.EnergyOptimiser
-import org.openremote.model.asset.impl.ElectricitySupplierAsset
-import org.openremote.model.attribute.AttributeRef
import org.openremote.model.util.Pair
import spock.lang.Specification
-import java.time.Instant
import java.time.LocalDateTime
-import java.time.ZoneId
-import java.time.ZoneOffset
import java.time.temporal.ChronoUnit
import java.util.function.Function
import java.util.stream.IntStream
diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
index fef5ee602..d93f80c98 100644
--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy
@@ -1,6 +1,5 @@
package org.openremote.test.gateway
-import com.google.common.collect.Lists
import io.netty.channel.ChannelHandler
import org.apache.http.client.utils.URIBuilder
import org.openremote.agent.protocol.http.HTTPAgent
@@ -9,7 +8,7 @@ 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.timer.TimerService
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.container.web.WebTargetBuilder
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
@@ -17,18 +16,13 @@ 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.gateway.JSchGatewayTunnelFactory
import org.openremote.manager.security.ManagerIdentityService
import org.openremote.manager.security.ManagerKeycloakIdentityProvider
import org.openremote.manager.setup.SetupService
-import org.openremote.model.asset.impl.LightAsset
-import org.openremote.model.security.User
-import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.model.asset.*
import org.openremote.model.asset.agent.ConnectionStatus
-import org.openremote.model.asset.impl.BuildingAsset
-import org.openremote.model.asset.impl.GatewayAsset
-import org.openremote.model.asset.impl.MicrophoneAsset
-import org.openremote.model.asset.impl.RoomAsset
+import org.openremote.model.asset.impl.*
import org.openremote.model.attribute.Attribute
import org.openremote.model.attribute.AttributeEvent
import org.openremote.model.attribute.AttributeRef
@@ -38,14 +32,21 @@ import org.openremote.model.event.shared.EventRequestResponseWrapper
import org.openremote.model.event.shared.SharedEvent
import org.openremote.model.gateway.GatewayClientResource
import org.openremote.model.gateway.GatewayConnection
+import org.openremote.model.gateway.GatewayTunnelInfo
+import org.openremote.model.gateway.GatewayTunnelStartRequestEvent
import org.openremote.model.geo.GeoJSONPoint
import org.openremote.model.query.AssetQuery
import org.openremote.model.query.filter.RealmPredicate
+import org.openremote.model.security.User
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.model.util.ValueUtil
+import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
+import spock.lang.Ignore
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
+import java.nio.file.Paths
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference
@@ -95,10 +96,10 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
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)
+ gateway = assetStorageService.find(gateway.getId(), true) as GatewayAsset
assert gateway.getAttribute("clientId").isPresent()
- assert !isNullOrEmpty(gateway.getAttribute("clientId").flatMap{it.getValue()}.orElse(""))
- assert !isNullOrEmpty(gateway.getAttribute("clientSecret").flatMap{it.getValue()}.orElse(""))
+ assert !isNullOrEmpty(gateway.getClientId().orElse(""))
+ assert !isNullOrEmpty(gateway.getClientSecret().orElse(""))
}
and: "a gateway connector should have been created for this gateway"
@@ -139,13 +140,13 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
assert gatewayClient.connectionStatus == ConnectionStatus.CONNECTED
}
- and: "the gateway asset connection status should be CONNECTING"
+ and: "the gateway asset connection status should be CONNECTED"
conditions.eventually {
gateway = assetStorageService.find(gateway.getId()) as GatewayAsset
- assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTING
+ assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTED
}
- and: "the server should have sent a CONNECTED message and an asset read request"
+ and: "the server should have sent an asset read request"
conditions.eventually {
assert clientReceivedMessages.size() >= 1
assert clientReceivedMessages[0].startsWith(EventRequestResponseWrapper.MESSAGE_PREFIX)
@@ -327,14 +328,17 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
new AssetsEvent(sendAssets))
gatewayClient.sendMessage(EventRequestResponseWrapper.MESSAGE_PREFIX + ValueUtil.asJSON(readAssetsReplyEvent).get())
- then: "the gateway asset status should become connected"
+ then: "the gateway connector sync should be completed"
conditions.eventually {
- gateway = assetStorageService.find(gateway.getId())
- assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTED
+ def gatewayConnector = gatewayService.gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT))
+ assert gatewayConnector.isConnected()
+ assert !gatewayConnector.isInitialSyncInProgress()
}
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()
+ conditions.eventually {
+ 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(500)
@@ -524,26 +528,20 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
and: "the gateway asset is marked as disabled"
assetProcessingService.sendAttributeEvent(new AttributeEvent(gateway.getId(), GatewayAsset.DISABLED, true))
- then: "the gateway asset status should become disabled"
+ then: "the gateway asset status should become DISCONNECTED"
conditions.eventually {
gateway = assetStorageService.find(gateway.getId(), true)
- assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.DISABLED
+ assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.DISCONNECTED
}
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().toLowerCase(Locale.ROOT)).disabled
assert !gatewayService.gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT)).connected
- assert gatewayClient.connectionStatus == ConnectionStatus.CONNECTING
- }
-
- and: "the central manager should have sent a disconnect event to the client"
- conditions.eventually {
- assert clientReceivedMessages.last().contains("gateway-disconnect")
}
- gatewayClient.disconnect()
when: "an attempt is made to add a descendant asset to a gateway that isn't connected"
+ gatewayClient.disconnect()
def failedAsset = new BuildingAsset("Failed Asset")
.setId(UniqueIdentifierGenerator.generateId("Failed asset"))
.setCreatedOn(Date.from(timerService.getNow()))
@@ -618,10 +616,10 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
assert gatewayClient.connectionStatus == ConnectionStatus.CONNECTED
}
- and: "the gateway asset connection status should be CONNECTING"
+ and: "the gateway asset connection status should be CONNECTED"
conditions.eventually {
gateway = assetStorageService.find(gateway.getId())
- assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTING
+ assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTED
}
and: "the local manager should have sent an asset read request"
@@ -717,10 +715,11 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
readAssetsReplyEvent = new EventRequestResponseWrapper(messageId, new AssetsEvent(sendAssets))
gatewayClient.sendMessage(EventRequestResponseWrapper.MESSAGE_PREFIX + ValueUtil.asJSON(readAssetsReplyEvent).get())
- then: "the gateway asset status should become connected"
+ then: "the gateway connector sync should be completed"
conditions.eventually {
- gateway = assetStorageService.find(gateway.getId())
- assert gateway.getGatewayStatus().orElse(null) == ConnectionStatus.CONNECTED
+ def gatewayConnector = gatewayService.gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT))
+ assert gatewayConnector.isConnected()
+ assert !gatewayConnector.isInitialSyncInProgress()
}
and: "the gateway should have the correct assets"
@@ -750,10 +749,9 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
def "Verify gateway client service"() {
- given: "the container environment is started with the spy gateway client service"
+ given: "the container environment is started"
def conditions = new PollingConditions(timeout: 15, delay: 0.2)
- def services = Lists.newArrayList(defaultServices())
- def container = startContainer(defaultConfig(), services)
+ def container = startContainer(defaultConfig(), defaultServices())
def assetProcessingService = container.getService(AssetProcessingService.class)
def assetStorageService = container.getService(AssetStorageService.class)
def gatewayService = container.getService(GatewayService.class)
@@ -801,8 +799,8 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
"127.0.0.1",
serverPort,
managerTestSetup.realmBuildingName,
- gateway.getAttribute("clientId").flatMap{it.getValue()}.orElse(""),
- gateway.getAttribute("clientSecret").flatMap{it.getValue()}.orElse(""),
+ gateway.getClientId().orElse(""),
+ gateway.getClientSecret().orElse(""),
false,
false
)
@@ -937,4 +935,63 @@ class GatewayTest extends Specification implements ManagerContainerTrait {
assert assetStorageService.find(mapAssetId(gateway.id, managerTestSetup.microphone1Id, false)) == null
}
}
+
+ /**
+ * This test requires a manager instance with tunnelling configured, so is manual for now unfortunately.
+ * Change the test url and key path to match the instance to connect to.
+ * Recommended to run profile/dev-proxy.yml profile.
+ */
+ @Ignore
+ def "Verify gateway tunnel factory"() {
+ given: "an ssh private key and the URL of a manager instance with tunnelling configured"
+ def keyPath = Paths.get(System.getProperty("user.home"), ".ssh", "test_key")
+ def tunnelSSHHost = "test.openremote.app"
+ def tunnelSSHPort = 2222
+
+ and: "the container environment is started"
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+ def container = startContainer(defaultConfig() << [(GatewayService.OR_GATEWAY_TUNNEL_SSH_KEY_FILE): keyPath.toAbsolutePath().toString()], defaultServices())
+ def gatewayClientService = container.getService(GatewayClientService)
+ def tunnelFactory = gatewayClientService.gatewayTunnelFactory as JSchGatewayTunnelFactory
+ def client = WebTargetBuilder.createClient(container.getExecutorService())
+ def tunnelInfo = new GatewayTunnelInfo(
+ "",
+ UniqueIdentifierGenerator.generateId(),
+ GatewayTunnelInfo.Type.HTTPS,
+ "localhost",
+ 443)
+ def target = client.target("https://${tunnelInfo.getId()}.${tunnelSSHHost}/auth/")
+
+ expect: "the tunnel factory to be created"
+ tunnelFactory != null
+
+ when: "a tunnel is requested to start"
+ def startEvent = new GatewayTunnelStartRequestEvent(
+ tunnelSSHHost,
+ tunnelSSHPort,
+ null,
+ null,
+ tunnelInfo)
+ tunnelFactory.startTunnel(startEvent)
+
+ then: "the tunnel should be established and be usable"
+ tunnelFactory.sessionMap.containsKey(tunnelInfo)
+ def response = target.request().get()
+ response.status == 200
+
+ when: "the tunnel is stopped"
+ tunnelFactory.stopTunnel(tunnelInfo)
+
+ then: "the tunnel should be destroyed"
+ !tunnelFactory.sessionMap.containsKey(tunnelInfo)
+
+ and: "requests should fail"
+ def response2 = target.request().get()
+ response2.status != 200
+
+ cleanup: "cleanup"
+ if (client != null) {
+ client.close()
+ }
+ }
}
diff --git a/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy b/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy
index 3fa154981..4ff22eb70 100644
--- a/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy
@@ -8,7 +8,7 @@ import org.openremote.agent.protocol.simulator.SimulatorAgent
import org.openremote.agent.protocol.velbus.VelbusTCPAgent
import org.openremote.container.persistence.PersistenceService
import org.openremote.container.timer.TimerService
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetModelService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.setup.SetupService
diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
index 5bd8f9bbb..5a355305b 100644
--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy
@@ -9,7 +9,7 @@ import org.openremote.agent.protocol.mqtt.MQTTLastWill
import org.openremote.agent.protocol.mqtt.MQTTMessage
import org.openremote.agent.protocol.mqtt.MQTT_IOClient
import org.openremote.agent.protocol.simulator.SimulatorProtocol
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.event.ClientEventService
diff --git a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy
index a7a48c965..bf8a74ccc 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/http/HttpServerProtocolTest.groovy
@@ -25,7 +25,7 @@ import io.undertow.servlet.api.DeploymentManager
import org.jboss.resteasy.specimpl.ResteasyUriInfo
import org.jboss.resteasy.util.BasicAuthHelper
import org.openremote.agent.protocol.http.AbstractHTTPServerProtocol
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.container.web.OAuthServerResponse
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
diff --git a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
index 915a151e2..71832040c 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy
@@ -23,7 +23,7 @@ import org.openremote.agent.protocol.mqtt.MQTTAgent
import org.openremote.agent.protocol.mqtt.MQTTAgentLink
import org.openremote.agent.protocol.mqtt.MQTTProtocol
import org.openremote.agent.protocol.simulator.SimulatorProtocol
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
index fa613444f..65b29b173 100644
--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy
@@ -30,7 +30,7 @@ import org.keycloak.representations.idm.RoleRepresentation
import org.openremote.agent.protocol.mqtt.MQTTMessage
import org.openremote.agent.protocol.mqtt.MQTT_IOClient
import org.openremote.container.security.keycloak.KeycloakIdentityProvider
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.event.ClientEventService
diff --git a/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy
index 80a039a5b..e02c2fe65 100644
--- a/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy
@@ -1,6 +1,6 @@
package org.openremote.test.rules
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.rules.RulesEngine
diff --git a/test/src/test/groovy/org/openremote/test/rules/GroupSummationRuleTest.groovy b/test/src/test/groovy/org/openremote/test/rules/GroupSummationRuleTest.groovy
index ad4ab135c..ce07e21cb 100644
--- a/test/src/test/groovy/org/openremote/test/rules/GroupSummationRuleTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/GroupSummationRuleTest.groovy
@@ -1,6 +1,6 @@
package org.openremote.test.rules
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.rules.RulesEngine
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 fe75e7ec2..c30d7846b 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -10,7 +10,7 @@ import jakarta.ws.rs.core.Response
import net.fortuna.ical4j.model.Recur
import org.openremote.container.timer.TimerService
import org.openremote.container.util.MailUtil
-import org.openremote.container.util.UniqueIdentifierGenerator
+import org.openremote.model.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.notification.EmailNotificationHandler
diff --git a/ui/app/manager/src/headers.ts b/ui/app/manager/src/headers.ts
index ab1d764fc..0114ea09b 100644
--- a/ui/app/manager/src/headers.ts
+++ b/ui/app/manager/src/headers.ts
@@ -47,6 +47,16 @@ export function headerItemGatewayConnection<S extends AppStateKeyed, A extends A
};
}
+export function headerItemGatewayTunnel<S extends AppStateKeyed, A extends AnyAction>(orApp: OrApp<S>): HeaderItem {
+ return {
+ icon: "lan-connect",
+ value: "gateway-tunnel",
+ href: "gateway-tunnel",
+ text: "gatewayTunnels.",
+ roles: ["write:admin", "read:admin"]
+ }
+}
+
export function headerItemLanguage<S extends AppStateKeyed, A extends AnyAction>(orApp: OrApp<S>): HeaderItem {
return {
icon: "web",
diff --git a/ui/app/manager/src/index.ts b/ui/app/manager/src/index.ts
index 832b0242d..9ed9c4fa3 100644
--- a/ui/app/manager/src/index.ts
+++ b/ui/app/manager/src/index.ts
@@ -9,6 +9,7 @@ import {
headerItemConfiguration,
headerItemExport,
headerItemGatewayConnection,
+ headerItemGatewayTunnel,
headerItemInsights,
headerItemLanguage,
headerItemLogout,
@@ -43,6 +44,7 @@ import {pageRealmsProvider} from "./pages/page-realms";
import {pageExportProvider} from "./pages/page-export";
import { pageConfigurationProvider } from "./pages/page-configuration";
import { ManagerAppConfig } from "@openremote/model";
+import {pageGatewayTunnelProvider} from "./pages/page-gateway-tunnel";
declare var CONFIG_URL_PREFIX: string;
@@ -64,6 +66,7 @@ export const DefaultPagesConfig: PageProvider<any>[] = [
pageMapProvider(store),
pageAssetsProvider(store),
pageGatewayProvider(store),
+ pageGatewayTunnelProvider(store),
pageLogsProvider(store),
pageInsightsProvider(store),
pageRulesProvider(store),
@@ -85,6 +88,7 @@ export const DefaultHeaderMainMenu: {[name: string]: HeaderItem} = {
export const DefaultHeaderSecondaryMenu: {[name: string]: HeaderItem} = {
gateway: headerItemGatewayConnection(orApp),
+ gatewayTunnel: headerItemGatewayTunnel(orApp),
language: headerItemLanguage(orApp),
logs: headerItemLogs(orApp),
account: headerItemAccount(orApp),
diff --git a/ui/app/manager/src/pages/page-gateway-tunnel.ts b/ui/app/manager/src/pages/page-gateway-tunnel.ts
new file mode 100644
index 000000000..bda40fce6
--- /dev/null
+++ b/ui/app/manager/src/pages/page-gateway-tunnel.ts
@@ -0,0 +1,466 @@
+import {AppStateKeyed, Page, PageProvider, router} from "@openremote/or-app";
+import {css, html, TemplateResult, unsafeCSS} from "lit";
+import {customElement, state} from "lit/decorators.js";
+import {until} from "lit/directives/until.js";
+import {when} from "lit/directives/when.js";
+import {Task} from "@lit/task";
+import {Store} from "@reduxjs/toolkit";
+import {i18next} from "@openremote/or-translate";
+import {DefaultColor3, manager} from "@openremote/core";
+import {InputType, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";
+import {Asset, AssetQuery, GatewayTunnelInfo, GatewayTunnelInfoType} from "@openremote/model";
+import {TableColumn, TableRow} from "@openremote/or-mwc-components/or-mwc-table";
+import {getAssetsRoute} from "../routes";
+import {OrMwcDialog, showDialog, showOkCancelDialog} from "@openremote/or-mwc-components/or-mwc-dialog";
+import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";
+
+export function pageGatewayTunnelProvider(store: Store<AppStateKeyed>): PageProvider<AppStateKeyed> {
+ return {
+ name: "gateway-tunnel",
+ routes: [
+ "gateway-tunnel"
+ ],
+ pageCreator: () => {
+ return new PageGatewayTunnel(store);
+ }
+ };
+}
+
+const styling = css`
+ #wrapper {
+ height: 100%;
+ width: 100%;
+ display: flex;
+ flex-direction: column;
+ }
+
+ #title {
+ padding: 0 20px;
+ font-size: 18px;
+ font-weight: bold;
+ width: calc(100% - 40px);
+ max-width: 1360px;
+ margin: 20px auto;
+ align-items: center;
+ display: flex;
+ color: var(--or-app-color3, ${unsafeCSS(DefaultColor3)});
+ }
+
+ #title > or-icon {
+ margin-right: 10px;
+ margin-left: 14px;
+ }
+
+ .panel {
+ flex: 0;
+ width: 100%;
+ box-sizing: border-box;
+ max-width: 1360px;
+ background-color: white;
+ border: 1px solid #e5e5e5;
+ border-radius: 5px;
+ position: relative;
+ margin: 0 auto 10px;
+ padding: 12px 24px 24px;
+ display: flex;
+ flex-direction: column;
+ }
+
+ .panel-title {
+ display: flex;
+ text-transform: uppercase;
+ font-weight: bolder;
+ color: var(--or-app-color3, ${unsafeCSS(DefaultColor3)});
+ line-height: 1em;
+ margin-bottom: 10px;
+ margin-top: 0;
+ flex: 0 0 auto;
+ letter-spacing: 0.025em;
+ align-items: center;
+ min-height: 36px;
+ }
+
+`
+
+@customElement("page-gateway-tunnel")
+export class PageGatewayTunnel extends Page<AppStateKeyed> {
+
+ // Static value with the asset types to be used
+ protected GATEWAY_TUNNEL_TYPES = ["GatewayAsset"];
+
+ // Static value with possible protocol types
+ protected GATEWAY_TUNNEL_PROTOCOL_TYPES: GatewayTunnelInfoType[] = [GatewayTunnelInfoType.HTTPS, GatewayTunnelInfoType.HTTP, GatewayTunnelInfoType.TCP];
+
+ @state()
+ protected _realm = manager.displayRealm;
+
+ @state()
+ protected _loading = false;
+
+ static get styles() {
+ return [styling];
+ }
+
+ get name(): string {
+ return "gateway-tunnel";
+ }
+
+ stateChanged(state: AppStateKeyed): void {
+ this._realm = state.app.realm || manager.displayRealm;
+ }
+
+ protected render(): TemplateResult {
+ return html`
+ <div id="wrapper">
+ <div id="title">
+ <or-icon icon="lan-connect"></or-icon>
+ ${i18next.t("gatewayTunnels.")}
+ </div>
+ <div class="panel">
+ <div class="panel-title" style="justify-content: space-between;">
+ <or-translate value="tunnels"></or-translate>
+ <or-mwc-input style="margin: 0;" type="${InputType.BUTTON}" icon="plus"
+ label="${i18next.t('add')} ${i18next.t("tunnel")}"
+ @or-mwc-input-changed="${(ev) => this._onAddTunnelClick(ev)}"
+ ></or-mwc-input>
+ </div>
+ ${this._fetchTunnelsTask.render({
+ pending: () => html`${i18next.t('loading')}`,
+ complete: (tunnels) => until(this.getTunnelsTable(tunnels), html`${i18next.t('loading')}`),
+ error: () => html`${i18next.t('errorOccurred')}`
+ })}
+ </div>
+ </div>
+ `;
+ }
+
+ /**
+ * Function that returns an HTML {@link TemplateResult} with a table.
+ * Displays the given {@link tunnels} parameter in a nice looking format.
+ * Uses the {@link OrMwcTable} component for displaying
+ */
+ protected async getTunnelsTable(tunnels?: GatewayTunnelInfo[]): Promise<TemplateResult> {
+ const columns: TableColumn[] = [
+ {title: i18next.t("gatewayTunnels.id"), isSortable: true},
+ {title: i18next.t("gatewayTunnels.gatewayId")},
+ {title: i18next.t("gatewayTunnels.type"), isSortable: true},
+ {title: i18next.t("gatewayTunnels.target"), isSortable: true, hideMobile: true},
+ {title: i18next.t("gatewayTunnels.targetPort"), isSortable: true, hideMobile: true},
+ {title: i18next.t("gatewayTunnels.assignedPort"), isSortable: true, hideMobile: true},
+ {title: ""}
+ ];
+ const rows: TableRow[] = tunnels?.map(tunnel => ({
+ content: [
+ tunnel.id,
+ until(this._getTunnelGatewayIdTemplate(tunnel), html`${i18next.t("loading")}`),
+ tunnel.type,
+ tunnel.target,
+ tunnel.targetPort + "",
+ !tunnel.assignedPort ? "" : tunnel.assignedPort + "",
+ until(this._getTunnelActionsTemplate(tunnel), html`${i18next.t("loading")}`)
+ ],
+ clickable: false,
+ } as TableRow)) || [];
+ return html`
+ <or-mwc-table .columns="${columns}" .rows="${rows}">
+
+ </or-mwc-table>
+ `
+ }
+
+ /**
+ * Function that returns an HTML {@link TemplateResult} containing the gateway asset that is linked to the {@link tunnel}.
+ */
+ protected async _getTunnelGatewayIdTemplate(tunnel: GatewayTunnelInfo): Promise<TemplateResult | string> {
+ const onClick = () => {
+ router.navigate(getAssetsRoute(false, tunnel.gatewayId));
+ }
+ return html`
+ <div style="height: 100%; display: flex; align-items: center;">
+ <span style="cursor: pointer;" @click="${() => onClick()}">${tunnel.gatewayId}</span>
+ </div>
+ `;
+ }
+
+ /**
+ * Function that returns an HTML {@link TemplateResult} containing the actions for the given {@link tunnel}.
+ */
+ protected async _getTunnelActionsTemplate(tunnel: GatewayTunnelInfo): Promise<TemplateResult | string> {
+ return html`
+ <div style="display: flex; justify-content: end; align-items: center; gap: 12px;">
+ <or-mwc-input .type="${InputType.BUTTON}" icon="stop" @or-mwc-input-changed="${(ev) => this._onStopTunnelClick(ev, tunnel)}"></or-mwc-input>
+ ${when(tunnel.type === GatewayTunnelInfoType.TCP, () => html`
+ <or-mwc-input .type="${InputType.BUTTON}" outlined label="${i18next.t('gatewayTunnels.copyAddress')}" @or-mwc-input-changed="${(ev) => this._onCopyTunnelAddressClick(ev, tunnel)}"></or-mwc-input>
+ `, () => html`
+ <or-mwc-input .type="${InputType.BUTTON}" outlined label="${i18next.t('gatewayTunnels.open')}" @or-mwc-input-changed="${(ev) => this._onOpenTunnelClick(ev, tunnel)}"></or-mwc-input>
+ `)}
+ </div>
+ `
+ }
+
+ /**
+ * HTML callback event of the 'stop' button in the tunnels table,
+ * meant to stop and delete the constructed tunnel.
+ */
+ protected _onStopTunnelClick(ev: OrInputChangedEvent, tunnel: GatewayTunnelInfo) {
+ ev.stopPropagation();
+ showOkCancelDialog(
+ i18next.t("areYouSure"),
+ i18next.t("gatewayTunnels.stopWarning"),
+ i18next.t("gatewayTunnels.stop")
+ ).then((ok: boolean) => {
+ if (ok) {
+ this._stopTunnel(tunnel).then(success => {
+ if (success) {
+ showSnackbar(undefined, "gatewayTunnels.closeSuccessful");
+ } else {
+ showSnackbar(undefined, "errorOccurred");
+ }
+ }).catch(_error => {
+ showSnackbar(undefined, "errorOccurred");
+ }).finally(() => {
+ this._fetchTunnelsTask.run();
+ });
+ }
+ });
+ }
+
+ /**
+ * Function that requests the manager to stop the {@link tunnel}.
+ */
+ protected async _stopTunnel(tunnel: GatewayTunnelInfo): Promise<boolean> {
+ const response = await manager.rest.api.GatewayServiceResource.stopTunnel(tunnel);
+ return response.status === 204;
+ }
+
+ /**
+ * HTML callback event for the 'copy address' button in the tunnels table,
+ * meant for TCP addresses to be copied to the browsers' clipboard.
+ */
+ protected _onCopyTunnelAddressClick(ev: OrInputChangedEvent, tunnel: GatewayTunnelInfo): void {
+ const address = this._getTunnelAddress(tunnel);
+ if(address) {
+ navigator.clipboard.writeText(address).finally(() => {
+ showSnackbar(undefined, i18next.t('gatewayTunnels.copySuccess'));
+ });
+ } else {
+ console.warn("Could not copy tunnel address as it could not be found.");
+ showSnackbar(undefined, i18next.t('errorOccurred'));
+ }
+ }
+
+ /**
+ * HTML callback event of the 'open' button in the tunnels table,
+ * meant to start tunneling towards that instance
+ */
+ protected _onOpenTunnelClick(ev: OrInputChangedEvent, tunnel: GatewayTunnelInfo): void {
+ this._navigateToTunnel(tunnel);
+ }
+
+ /**
+ * HTML callback event of the 'add' button above the tunnels table,
+ * meant to start a tunnel towards that instance.
+ */
+ protected _onAddTunnelClick(ev: OrInputChangedEvent) {
+ const tunnel = this._getDefaultTunnelToAdd();
+ let dialog: OrMwcDialog | undefined;
+
+ const onAddClick = () => {
+ this._tryStartTunnel(tunnel);
+ }
+ const updateActions = () => {
+ dialog.setActions([
+ {actionName: "cancel", content: "cancel"},
+ {actionName: "add", disabled: (!tunnel.gatewayId || !tunnel.target || !tunnel.targetPort), content: "add", action: () => onAddClick()},
+ ])
+ }
+ const gatewayListTemplate = async (): Promise<TemplateResult> => {
+ const gatewayAssets = await this._fetchGatewayAssets();
+ const items = gatewayAssets.map((g) => [g.id, g.name]);
+ return html`
+ <div style="display: flex; flex-direction: column; gap: 20px; width: 360px;">
+ <or-mwc-input .type="${InputType.SELECT}" .options="${items}" .value="${tunnel.gatewayId}" label="${i18next.t('gatewayTunnels.selectAsset')}" style="width: 100%;"
+ @or-mwc-input-changed="${(ev) => {
+ tunnel.gatewayId = ev.detail.value;
+ updateActions();
+ }}"
+ ></or-mwc-input>
+ <or-mwc-input .type="${InputType.SELECT}" label="${i18next.t('gatewayTunnels.protocol')}" .value="${tunnel.type}" .options="${this.GATEWAY_TUNNEL_PROTOCOL_TYPES}"
+ style="width: 100%"
+ @or-mwc-input-changed="${(ev) => {
+ tunnel.type = ev.detail.value;
+ updateActions();
+ }}"
+ ></or-mwc-input>
+ <or-mwc-input .type="${InputType.TEXT}" label="${i18next.t('host')}" .value="${tunnel.target}" style="width: 100%;"
+ @or-mwc-input-changed="${(ev) => {
+ tunnel.target = ev.detail.value;
+ updateActions();
+ }}"
+ ></or-mwc-input>
+ <or-mwc-input .type="${InputType.NUMBER}" label="${i18next.t('port')}" .value="${tunnel.targetPort}" style="width: 100%"
+ @or-mwc-input-changed="${(ev) => {
+ tunnel.targetPort = ev.detail.value;
+ updateActions();
+ }}"
+ ></or-mwc-input>
+ </div>
+ `
+ }
+ dialog = new OrMwcDialog()
+ .setHeading(`${i18next.t("add")} ${i18next.t("tunnel")}`)
+ .setContent(html`
+ ${until(gatewayListTemplate(), html`${i18next.t("loading")}`)}
+ `);
+
+ updateActions();
+ showDialog(dialog);
+ }
+
+ /**
+ * Function that attempts to start a new {@link tunnel}, but requires checking of its fields beforehand,
+ * making sure the correct details are sent towards the {@link _startTunnel()} method.
+ */
+ protected _tryStartTunnel(tunnel: GatewayTunnelInfo) {
+ if (!tunnel) {
+ console.warn("Tried to add tunnel, but failed.")
+ return;
+ }
+ if (!tunnel.gatewayId || !tunnel.target || !tunnel.targetPort) {
+ console.warn("Tried to add tunnel, but not all fields were filled in!");
+ return;
+ }
+ if (!tunnel.realm) {
+ tunnel.realm = manager.displayRealm;
+ }
+ this._startTunnel(tunnel)
+ .catch(e => {
+ console.error(e)
+ showSnackbar(undefined, i18next.t("errorOccurred"));
+ })
+ .finally(() => {
+ this._fetchTunnelsTask.run();
+ });
+ }
+
+ /**
+ * Function that requests the manager to start a new {@link tunnel}.
+ */
+ protected async _startTunnel(tunnel: GatewayTunnelInfo): Promise<boolean> {
+ const response = await manager.rest.api.GatewayServiceResource.startTunnel(tunnel);
+ return response?.status === 200;
+ }
+
+ /**
+ * Function that opens a web page through the tunnel (in a new browser tab)
+ */
+ protected _navigateToTunnel(info: GatewayTunnelInfo): void {
+ if (!info.realm || !info.gatewayId || !info.target || !info.targetPort) {
+ console.warn("Could not navigate to tunnel, as some provided information was not set.");
+ }
+ const address = this._getTunnelAddress(info);
+ switch (info.type) {
+ case GatewayTunnelInfoType.HTTPS:
+ case GatewayTunnelInfoType.HTTP:
+ window.open(address)?.focus();
+ break;
+ default:
+ console.error("Unknown error when navigating to tunnel.");
+ break;
+ }
+ }
+
+ /**
+ * Internal function to get the tunnel address based on {@link GatewayTunnelInfo}
+ */
+ protected _getTunnelAddress(info: GatewayTunnelInfo): string | undefined {
+ switch (info.type) {
+ case GatewayTunnelInfoType.HTTPS:
+ case GatewayTunnelInfoType.HTTP:
+ return "//" + info.id + "." + window.location.host;
+ case GatewayTunnelInfoType.TCP:
+ return info.id + "." + window.location.hostname + ":" + info.assignedPort;
+ }
+ }
+
+
+ /* ------------------------------------- */
+
+ /**
+ * Internal Lit {@link Task} meant for fetching the list of tunnels.
+ * It acts as a reactive controller, and keeps its value cached.
+ */
+ protected _fetchTunnelsTask = new Task(this, {
+ task: async ([realm], {signal}) => {
+ return await this._fetchTunnels(realm, signal);
+ },
+ args: () => [this._realm]
+ });
+
+ /**
+ * Internal asynchronous function to fetch the list of tunnels.
+ */
+ protected async _fetchTunnels(realm: string = manager.displayRealm, _signal?: AbortSignal): Promise<GatewayTunnelInfo[]> {
+ const response = await manager.rest.api.GatewayServiceResource.getAllActiveTunnelInfos(realm);
+ if (response.status !== 200) {
+ throw new Error(response.statusText);
+ }
+
+ if (response.data.length > 0) {
+ return response.data;
+ } else {
+ console.warn("No tunnels were received from the manager.")
+ }
+ }
+
+
+ /* ------------------------------ */
+
+ /**
+ * Utility function to fetch the available gateway assets to select from
+ */
+ protected async _fetchGatewayAssets(requireTunnelSupport = true): Promise<Asset[]> {
+ const query = {
+ realm: {name: manager.displayRealm},
+ select: {attributes: ["tunnelingSupported"]},
+ types: this.GATEWAY_TUNNEL_TYPES,
+ attributes: requireTunnelSupport ? {
+ items: [
+ {
+ name: {
+ predicateType: "string",
+ value: "tunnelingSupported"
+ },
+ value: {
+ predicateType: "boolean",
+ value: true
+ },
+ },
+ {
+ name: {
+ predicateType: "string",
+ value: "gatewayStatus"
+ },
+ value: {
+ predicateType: "string",
+ value: "CONNECTED"
+ }
+ }
+ ]
+ } : undefined
+ } as AssetQuery;
+ return (await manager.rest.api.AssetResource.queryAssets(query)).data;
+ }
+
+ /**
+ * Utility function that returns the default values of a new tunnel.
+ */
+ protected _getDefaultTunnelToAdd(): GatewayTunnelInfo {
+ return {
+ target: "localhost",
+ targetPort: 443,
+ type: this.GATEWAY_TUNNEL_PROTOCOL_TYPES?.[0]
+ }
+ }
+
+}
diff --git a/ui/component/or-app/package.json b/ui/component/or-app/package.json
index 326ba5644..68313dfe0 100644
--- a/ui/component/or-app/package.json
+++ b/ui/component/or-app/package.json
@@ -19,6 +19,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
+ "@lit/task": "^1.0.0",
"@openremote/core": "workspace:*",
"@openremote/or-asset-tree": "^1.1.0",
"@openremote/or-asset-viewer": "^1.1.0",
diff --git a/ui/component/or-dashboard-builder/src/index.ts b/ui/component/or-dashboard-builder/src/index.ts
index 6fe47288f..4f99f0116 100644
--- a/ui/component/or-dashboard-builder/src/index.ts
+++ b/ui/component/or-dashboard-builder/src/index.ts
@@ -30,6 +30,7 @@ import {KpiWidget} from "./widgets/kpi-widget";
import {MapWidget} from "./widgets/map-widget";
import {AttributeInputWidget} from "./widgets/attribute-input-widget";
import {TableWidget} from "./widgets/table-widget";
+import {GatewayWidget} from "./widgets/gateway-widget";
// language=CSS
const styling = css`
@@ -216,6 +217,7 @@ export function registerWidgetTypes() {
widgetTypes.set("map", MapWidget.getManifest());
widgetTypes.set("attributeinput", AttributeInputWidget.getManifest());
widgetTypes.set("table", TableWidget.getManifest());
+ widgetTypes.set("gateway", GatewayWidget.getManifest());
}
@customElement("or-dashboard-builder")
diff --git a/ui/component/or-dashboard-builder/src/settings/gateway-settings.ts b/ui/component/or-dashboard-builder/src/settings/gateway-settings.ts
new file mode 100644
index 000000000..373b6f618
--- /dev/null
+++ b/ui/component/or-dashboard-builder/src/settings/gateway-settings.ts
@@ -0,0 +1,144 @@
+import { customElement } from "lit/decorators.js";
+import {WidgetSettings} from "../util/widget-settings";
+import {TemplateResult, html} from "lit";
+import { InputType, OrInputChangedEvent } from "@openremote/or-mwc-components/or-mwc-input";
+import {i18next} from "@openremote/or-translate";
+import {Asset, AssetQuery, GatewayTunnelInfoType} from "@openremote/model";
+import {GatewayWidgetConfig} from "../widgets/gateway-widget";
+import {Task} from "@lit/task";
+import manager from "@openremote/core";
+
+@customElement("gateway-settings")
+export class GatewaySettings extends WidgetSettings {
+
+ protected GATEWAY_ASSET_TYPES: string[] = ["GatewayAsset"];
+ protected GATEWAY_TUNNEL_TYPES: GatewayTunnelInfoType[] = [GatewayTunnelInfoType.HTTPS, GatewayTunnelInfoType.HTTP, GatewayTunnelInfoType.TCP];
+
+ // Override of widgetConfig with extended type
+ protected readonly widgetConfig!: GatewayWidgetConfig;
+
+ connectedCallback() {
+ super.connectedCallback();
+ }
+
+ protected render(): TemplateResult {
+ const disabled = !this.widgetConfig.gatewayId;
+ return html`
+ <div>
+ <settings-panel displayName="gatewayTunnels.settings" expanded="${true}">
+ <div style="display: flex; flex-direction: column; gap: 8px;">
+ <div>
+ ${this._fetchAssetsTask.render({
+ pending: () => html`
+ <or-mwc-input .type="${InputType.SELECT}" compact style="width: 100%;" disabled
+ label="${i18next.t('gatewayTunnels.selectAsset')}"
+ ></or-mwc-input>
+ `,
+ complete: (gatewayAssets) => {
+ const options: [string, string][] = gatewayAssets.map(a => [a.id, a.name]);
+ const selected = gatewayAssets.find(a => a.id === this.widgetConfig.gatewayId);
+ const value = selected ? [selected.id, selected.name] as [string, string] : undefined;
+ return html`
+ <or-mwc-input .type="${InputType.SELECT}" compact style="width: 100%;"
+ .options="${options}" .value="${value}"
+ label="${i18next.t('gatewayTunnels.selectAsset')}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onAssetSelect(ev)}"
+ ></or-mwc-input>
+ `;
+ },
+ error: () => html`
+ <or-mwc-input .type="${InputType.TEXT}" compact style="width: 100%;" disabled
+ label="${i18next.t('gatewayTunnels.selectAsset')}" .value="${i18next.t('errorOccurred')}"
+ ></or-mwc-input>
+ `
+ })}
+ </div>
+ <div>
+ <or-mwc-input .type="${InputType.SELECT}" compact style="width: 100%;"
+ .disabled="${disabled}" .options="${this.GATEWAY_TUNNEL_TYPES}" .value="${this.widgetConfig.type}"
+ label="${i18next.t('gatewayTunnels.protocol')}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onTunnelTypeSelect(ev)}"
+ ></or-mwc-input>
+ </div>
+ <div>
+ <or-mwc-input .type="${InputType.TEXT}" style="width: 100%;"
+ .disabled="${disabled}" .value="${this.widgetConfig.target}"
+ label="${i18next.t('gatewayTunnels.target')}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onTunnelTargetSelect(ev)}"
+ ></or-mwc-input>
+ </div>
+ <div>
+ <or-mwc-input .type="${InputType.NUMBER}" style="width: 100%;"
+ .disabled="${disabled}" .value="${this.widgetConfig.targetPort}"
+ label="${i18next.t('gatewayTunnels.targetPort')}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onTunnelTargetPortSelect(ev)}"
+ ></or-mwc-input>
+ </div>
+ </div>
+ </settings-panel>
+ </div>
+ `;
+ }
+
+ protected _onAssetSelect(ev: OrInputChangedEvent): void {
+ this.widgetConfig.gatewayId = ev.detail.value;
+ this.notifyConfigUpdate();
+ }
+
+ protected _onTunnelTypeSelect(ev: OrInputChangedEvent) {
+ this.widgetConfig.type = ev.detail.value;
+ this.notifyConfigUpdate();
+ }
+
+ protected _onTunnelTargetSelect(ev: OrInputChangedEvent) {
+ this.widgetConfig.target = ev.detail.value;
+ this.notifyConfigUpdate();
+ }
+
+ protected _onTunnelTargetPortSelect(ev: OrInputChangedEvent) {
+ this.widgetConfig.targetPort = ev.detail.value;
+ this.notifyConfigUpdate();
+ }
+
+
+ protected _fetchAssetsTask = new Task(this, {
+ task: async ([], {signal}) => {
+ return await this._fetchAssets(true, signal);
+ },
+ args: () => []
+ });
+
+ protected async _fetchAssets(requireTunnelSupport = true, signal?: AbortSignal): Promise<Asset[]> {
+ const query = {
+ realm: {name: manager.displayRealm},
+ select: {attributes: ["tunnelingSupported"]},
+ types: this.GATEWAY_ASSET_TYPES,
+ attributes: requireTunnelSupport ? {
+ items: [
+ {
+ name: {
+ predicateType: "string",
+ value: "tunnelingSupported"
+ },
+ value: {
+ predicateType: "boolean",
+ value: true
+ },
+ },
+ {
+ name: {
+ predicateType: "string",
+ value: "gatewayStatus"
+ },
+ value: {
+ predicateType: "string",
+ value: "CONNECTED"
+ },
+ }
+ ]
+ } : undefined
+ } as AssetQuery;
+ return (await manager.rest.api.AssetResource.queryAssets(query)).data;
+ }
+
+}
diff --git a/ui/component/or-dashboard-builder/src/settings/image-settings.ts b/ui/component/or-dashboard-builder/src/settings/image-settings.ts
index ef5ae0b8b..8be12c3ac 100644
--- a/ui/component/or-dashboard-builder/src/settings/image-settings.ts
+++ b/ui/component/or-dashboard-builder/src/settings/image-settings.ts
@@ -53,7 +53,7 @@ export class ImageSettings extends AssetWidgetSettings {
return html`
<div>
<!-- Attributes selector -->
- <settings-panel displayName="'attributes" expanded="${true}">
+ <settings-panel displayName="attributes" expanded="${true}">
<attributes-panel .attributeRefs="${this.widgetConfig.attributeRefs}" onlyDataAttrs="${false}" multi="${true}"
@attribute-select="${(ev: AttributesSelectEvent) => this.onAttributesSelect(ev)}"
></attributes-panel>
diff --git a/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts b/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts
new file mode 100644
index 000000000..ee909b3f6
--- /dev/null
+++ b/ui/component/or-dashboard-builder/src/widgets/gateway-widget.ts
@@ -0,0 +1,350 @@
+import {customElement, state} from "lit/decorators.js";
+import {OrWidget, WidgetManifest} from "../util/or-widget";
+import {css, html, PropertyValues, TemplateResult} from "lit";
+import {WidgetSettings} from "../util/widget-settings";
+import {WidgetConfig} from "../util/widget-config";
+import {GatewaySettings} from "../settings/gateway-settings";
+import {InputType, OrInputChangedEvent} from "@openremote/or-mwc-components/or-mwc-input";
+import {GatewayTunnelInfo, GatewayTunnelInfoType} from "@openremote/model";
+import manager from "@openremote/core";
+import {when} from "lit/directives/when.js";
+import {i18next} from "@openremote/or-translate";
+import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";
+
+const styling = css`
+ #gateway-widget-wrapper {
+ height: 100%;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ overflow: hidden;
+ }
+
+ #gateway-widget-container {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ gap: 8px;
+ max-height: 48px;
+ flex-wrap: wrap;
+ position: relative;
+ }
+`
+
+export interface GatewayWidgetConfig extends WidgetConfig {
+ gatewayId?: string;
+ type: GatewayTunnelInfoType;
+ target: string;
+ targetPort: number;
+}
+
+function getDefaultWidgetConfig(): GatewayWidgetConfig {
+ return {
+ type: GatewayTunnelInfoType.HTTPS,
+ target: "localhost",
+ targetPort: 443,
+ };
+}
+
+@customElement("gateway-widget")
+export class GatewayWidget extends OrWidget {
+
+ protected widgetConfig!: GatewayWidgetConfig;
+
+ @state()
+ protected _loading = false;
+
+ @state()
+ protected _activeTunnel?: GatewayTunnelInfo;
+
+ protected _startedByUser = false;
+
+ static getManifest(): WidgetManifest {
+ return {
+ displayName: "Gateway",
+ displayIcon: "lan-connect",
+ minColumnWidth: 1,
+ minColumnHeight: 1,
+ getContentHtml(config: GatewayWidgetConfig): OrWidget {
+ return new GatewayWidget(config);
+ },
+ getSettingsHtml(config: GatewayWidgetConfig): WidgetSettings {
+ return new GatewaySettings(config);
+ },
+ getDefaultConfig(): GatewayWidgetConfig {
+ return getDefaultWidgetConfig();
+ }
+ }
+ }
+
+ refreshContent(force: boolean): void {
+ this.widgetConfig = JSON.parse(JSON.stringify(this.widgetConfig)) as GatewayWidgetConfig;
+ }
+
+ static get styles() {
+ return [...super.styles, styling];
+ }
+
+ disconnectedCallback() {
+ if(this._activeTunnel) {
+ if(this._startedByUser) {
+ this._stopTunnel(this._activeTunnel);
+ } else {
+ console.warn("Keeping the active tunnel open, as it is not started through the widget.")
+ }
+ }
+ super.disconnectedCallback();
+ }
+
+ protected willUpdate(changedProps: PropertyValues) {
+ if (changedProps.has("_activeTunnel") && !!this._activeTunnel) {
+ this._navigateToTunnel(this._activeTunnel);
+ }
+ }
+
+ protected render(): TemplateResult {
+ const disabled = this.getEditMode?.() || !this._isConfigComplete(this.widgetConfig);
+ return html`
+ <div id="gateway-widget-wrapper">
+ <div id="gateway-widget-container">
+ ${when(this._loading, () => html`
+ <or-loading-indicator></or-loading-indicator>
+ `, () => {
+ if(this._activeTunnel) {
+ return html`
+
+ <or-mwc-input .type="${InputType.BUTTON}" icon="stop" label="${i18next.t('gatewayTunnels.stop')}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onStopTunnelClick(ev)}"
+ ></or-mwc-input>
+
+ ${when(this.widgetConfig.type === GatewayTunnelInfoType.TCP, () => html`
+ <or-mwc-input .type="${InputType.BUTTON}" icon="content-copy" label="${i18next.t('gatewayTunnels.copyAddress')}" outlined .disabled="${disabled}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onCopyTunnelAddressClick(ev)}"
+ ></or-mwc-input>
+ `, () => html`
+ <or-mwc-input .type="${InputType.BUTTON}" icon="open-in-new" label="${i18next.t('gatewayTunnels.open')}" outlined .disabled="${disabled}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onTunnelNavigateClick(ev)}"
+ ></or-mwc-input>
+ `)}
+
+ `;
+ } else {
+ return html`
+ <or-mwc-input .type="${InputType.BUTTON}" label="${i18next.t('gatewayTunnels.start')}" outlined .disabled="${disabled}"
+ @or-mwc-input-changed="${(ev: OrInputChangedEvent) => this._onStartTunnelClick(ev)}"
+ ></or-mwc-input>
+ `;
+ }
+ })}
+ </div>
+ </div>
+ `;
+ }
+
+ /**
+ * HTML callback function when 'copy address' button is pressed for a TCP tunnel.
+ */
+ protected _onCopyTunnelAddressClick(ev: OrInputChangedEvent) {
+ if (this._isConfigComplete(this.widgetConfig)) {
+ const tunnelInfo = this._getTunnelInfoByConfig(this.widgetConfig);
+ const address = this._getTunnelAddress(tunnelInfo);
+ if(address) {
+ navigator.clipboard.writeText(address).finally(() => {
+ showSnackbar(undefined, i18next.t('gatewayTunnels.copySuccess'));
+ });
+ } else {
+ console.warn("Could not copy tunnel address as it could not be found.");
+ showSnackbar(undefined, i18next.t('errorOccurred'));
+ }
+ } else {
+ console.warn("Could not copy tunnel address as configuration is not complete.");
+ showSnackbar(undefined, i18next.t('errorOccurred'));
+ }
+ }
+
+ /**
+ * HTML callback function when 'start' button is pressed, meant to create / start a new tunnel.
+ */
+ protected _onStartTunnelClick(ev: OrInputChangedEvent) {
+ this._tryStartTunnel(this.widgetConfig);
+ }
+
+ /**
+ * HTML callback function when 'stop' button is pressed, meant to destroy the active tunnel.
+ */
+ protected _onStopTunnelClick(ev: OrInputChangedEvent) {
+ this._tryStopTunnel(this.widgetConfig);
+ }
+
+ /**
+ * HTML callback function when 'open' button is pressed, meant to start using the tunnel.
+ */
+ protected _onTunnelNavigateClick(ev: OrInputChangedEvent) {
+ if (this._isConfigComplete(this.widgetConfig)) {
+ this._navigateToTunnel(this._getTunnelInfoByConfig(this.widgetConfig));
+ } else {
+ console.warn("Could not navigate to tunnel as configuration is not complete.")
+ }
+ }
+
+ /**
+ * Function that tries to start the tunnel. It checks the configuration beforehand,
+ * and acts as a controller to call the correct functions throughout the starting process.
+ */
+ protected _tryStartTunnel(widgetConfig: GatewayWidgetConfig): void {
+ if (this._isConfigComplete(widgetConfig)) {
+
+ const tunnelInfo = this._getTunnelInfoByConfig(widgetConfig);
+ this._loading = true;
+
+ this._getActiveTunnel(tunnelInfo).then(activeTunnel => {
+
+ if (activeTunnel) {
+ console.log("Found an active tunnel!", activeTunnel);
+ this._activeTunnel = activeTunnel;
+ this._loading = false;
+
+ } else {
+ this._startTunnel(tunnelInfo).then(newTunnel => {
+
+ if (newTunnel) {
+ console.log("Started a new tunnel!", newTunnel);
+ this._activeTunnel = newTunnel;
+ this._startedByUser = true;
+
+ } else {
+ console.warn("No new tunnel!");
+ }
+ }).catch(e => {
+ console.error(e);
+ showSnackbar(undefined, i18next.t("errorOccurred"));
+ }).finally(() => {
+ this._loading = false;
+ });
+ }
+
+ }).catch(e => {
+ console.error(e);
+ showSnackbar(undefined, i18next.t("errorOccurred"));
+ this._loading = false;
+ })
+ }
+ }
+
+ /**
+ * Internal function that starts the tunnel by communicating with the Manager API.
+ */
+ protected async _startTunnel(info: GatewayTunnelInfo): Promise<GatewayTunnelInfo | undefined> {
+ if (!info.realm || !info.gatewayId || !info.target || !info.targetPort) {
+ throw new Error("Could not start tunnel, as some provided information was not set.");
+ }
+ return (await manager.rest.api.GatewayServiceResource.startTunnel(info)).data;
+ }
+
+ /**
+ * Internal function that requests the Manager API for the active tunnel based on the {@link GatewayTunnelInfo} parameter.
+ */
+ protected async _getActiveTunnel(info: GatewayTunnelInfo): Promise<GatewayTunnelInfo | undefined> {
+ if (!info.realm || !info.gatewayId || !info.target || !info.targetPort) {
+ throw new Error("Could not get active tunnel, as some provided information was not set.")
+ }
+ return (await manager.rest.api.GatewayServiceResource.getActiveTunnelInfo(info.realm, info.gatewayId, info.target, info.targetPort)).data;
+ }
+
+ /**
+ * Function that tries to destroy the currently active tunnel.
+ */
+ protected _tryStopTunnel(config: GatewayWidgetConfig): void {
+ const tunnelInfo = this._getTunnelInfoByConfig(config);
+ this._loading = true;
+ this._stopTunnel(tunnelInfo)
+ .then(() => {
+ this._activeTunnel = undefined;
+ this._startedByUser = false;
+ })
+ .catch(e => {
+ console.warn(e);
+ })
+ .finally(() => {
+ this._loading = false;
+ });
+ }
+
+ /**
+ * Internal function that requests the Manager API to destroy a tunnel that is in line with the {@link GatewayTunnelInfo} parameter.
+ */
+ protected async _stopTunnel(info: GatewayTunnelInfo): Promise<void> {
+ return (await manager.rest.api.GatewayServiceResource.stopTunnel(info)).data;
+ }
+
+ /**
+ * Function that navigates the user to an HTTP web page that interacts with a service through the tunnel.
+ * It will open in a new browser tab automatically.
+ */
+ protected _navigateToTunnel(info: GatewayTunnelInfo): void {
+ if (!info.realm || !info.gatewayId || !info.target || !info.targetPort) {
+ console.warn("Could not navigate to tunnel, as some provided information was not set.");
+ }
+ const address = this._getTunnelAddress(info);
+ switch (info.type) {
+ case GatewayTunnelInfoType.HTTPS:
+ case GatewayTunnelInfoType.HTTP:
+ window.open(address)?.focus();
+ break;
+ default:
+ console.error("Unknown error when navigating to tunnel.");
+ break;
+ }
+ }
+
+ /**
+ * Internal function to get the tunnel address based on {@link GatewayTunnelInfo}
+ */
+ protected _getTunnelAddress(info: GatewayTunnelInfo): string | undefined {
+ switch (info.type) {
+ case GatewayTunnelInfoType.HTTPS:
+ case GatewayTunnelInfoType.HTTP:
+ return "//" + info.id + "." + window.location.host;
+ case GatewayTunnelInfoType.TCP:
+ return info.id + "." + window.location.hostname + ":" + info.assignedPort;
+ }
+ }
+
+ /**
+ * Internal function to check whether the {@link GatewayWidgetConfig} includes the necessary information to control the tunnel.
+ * Uses several `undefined` or empty checks. Useful for checking the object before interacting with Manager APIs.
+ */
+ protected _isConfigComplete(widgetConfig: GatewayWidgetConfig): boolean {
+ if (!widgetConfig.gatewayId) {
+ console.error("Could not start tunnel, since no gateway asset was configured.");
+ return false;
+ }
+ if (!widgetConfig.type) {
+ console.error("Could not start tunnel, since no gateway type / protocol was configured.");
+ return false;
+ }
+ if (!widgetConfig.target) {
+ console.error("Could not start tunnel, since no gateway target was configured.");
+ return false;
+ }
+ if (!widgetConfig.targetPort) {
+ console.error("Could not start tunnel, since no gateway target port was configured.");
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Internal function that parses a {@link GatewayWidgetConfig} into a new {@link GatewayTunnelInfo}.
+ */
+ protected _getTunnelInfoByConfig(config: GatewayWidgetConfig): GatewayTunnelInfo {
+ return {
+ realm: manager.displayRealm,
+ gatewayId: config.gatewayId,
+ type: config.type,
+ target: config.target,
+ targetPort: config.targetPort
+ } as GatewayTunnelInfo;
+ }
+
+}
diff --git a/yarn.lock b/yarn.lock
index 0d6ca3381..1f0b1b6f5 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1542,13 +1542,22 @@ __metadata:
languageName: node
linkType: hard
-"@lit-labs/ssr-dom-shim@npm:^1.0.0, @lit-labs/ssr-dom-shim@npm:^1.1.0":
+"@lit-labs/ssr-dom-shim@npm:^1.0.0, @lit-labs/ssr-dom-shim@npm:^1.1.0, @lit-labs/ssr-dom-shim@npm:^1.2.0":
version: 1.2.0
resolution: "@lit-labs/ssr-dom-shim@npm:1.2.0"
checksum: 10c0/016168cf6901ab343462c13fb168dda6d549f8b42680aa394e6b7cd0af7cce51271e00dbfa5bbbe388912bf89cbb8f941a21cc3ec9bf95d6a84b6241aa9e5a72
languageName: node
linkType: hard
+"@lit/reactive-element@npm:^1.0.0 || ^2.0.0":
+ version: 2.0.4
+ resolution: "@lit/reactive-element@npm:2.0.4"
+ dependencies:
+ "@lit-labs/ssr-dom-shim": "npm:^1.2.0"
+ checksum: 10c0/359cc19ea9ee8b65e1417eb9c12f40dddba8f0a5ab32f0e5facaecee6060629e44eb4ca27d9af945fe6eda8c033aa636abaa5f0c4e6a529b224d78674acf47ba
+ languageName: node
+ linkType: hard
+
"@lit/reactive-element@npm:^1.3.0, @lit/reactive-element@npm:^1.6.0":
version: 1.6.3
resolution: "@lit/reactive-element@npm:1.6.3"
@@ -1558,6 +1567,15 @@ __metadata:
languageName: node
linkType: hard
+"@lit/task@npm:^1.0.0":
+ version: 1.0.0
+ resolution: "@lit/task@npm:1.0.0"
+ dependencies:
+ "@lit/reactive-element": "npm:^1.0.0 || ^2.0.0"
+ checksum: 10c0/6d776e17620c4bb53a07a17c21f25237480f1d2bc461e54fa9eb387074900d4d04e77aa7ed8a519d2af4ae11c31b8f8edf991d372b5ea45c4934422967fb9f11
+ languageName: node
+ linkType: hard
+
"@mapbox/corslite@npm:0.0.7":
version: 0.0.7
resolution: "@mapbox/corslite@npm:0.0.7"
@@ -2410,6 +2428,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@openremote/or-app@workspace:ui/component/or-app"
dependencies:
+ "@lit/task": "npm:^1.0.0"
"@openremote/core": "workspace:*"
"@openremote/or-asset-tree": "npm:^1.1.0"
"@openremote/or-asset-viewer": "npm:^1.1.0"