Skip to content

armgit / core

Public Imported from GitHub
0

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

Added interceptors to ClientEventService and added ClientEventProtocol

Rich Turnere05d5ff18b83
diff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
index 7659586df..0e578cb94 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java
@@ -22,13 +22,13 @@ package org.openremote.agent.protocol;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.openremote.container.Container;
-import org.openremote.container.ContainerService;
import org.openremote.container.concurrent.GlobalLock;
import org.openremote.container.message.MessageBrokerContext;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.timer.TimerService;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.AgentLink;
import org.openremote.model.asset.agent.ConnectionStatus;
@@ -41,7 +41,6 @@ import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
-import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@@ -119,6 +118,7 @@ public abstract class AbstractProtocol implements Protocol {
protected ProtocolExecutorService executorService;
protected ProtocolAssetService assetService;
protected ProtocolPredictedAssetService predictedAssetService;
+ protected ProtocolClientEventService protocolClientEventService;
@Override
public int getPriority() {
@@ -132,6 +132,7 @@ public abstract class AbstractProtocol implements Protocol {
executorService = container.getService(ProtocolExecutorService.class);
assetService = container.getService(ProtocolAssetService.class);
predictedAssetService = container.getService(ProtocolPredictedAssetService.class);
+ protocolClientEventService = container.getService(ProtocolClientEventService.class);
messageBrokerContext = container.getService(MessageBrokerService.class).getContext();
withLock(getProtocolName() + "::start", () -> {
@@ -181,7 +182,7 @@ public abstract class AbstractProtocol implements Protocol {
}
@Override
- final public void linkProtocolConfiguration(AssetAttribute protocolConfiguration, Consumer<ConnectionStatus> statusConsumer) {
+ final public void linkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration, Consumer<ConnectionStatus> statusConsumer) {
withLock(getProtocolName() + "::linkProtocolConfiguration", () -> {
LOG.finer("Linking protocol configuration to protocol '" + getProtocolName() + "': " + protocolConfiguration);
@@ -193,7 +194,7 @@ public abstract class AbstractProtocol implements Protocol {
);
statusConsumer.accept(currentStatus);
if (currentStatus == ConnectionStatus.CONNECTING) {
- doLinkProtocolConfiguration(protocolConfiguration);
+ doLinkProtocolConfiguration(agent, protocolConfiguration);
} else {
LOG.info("Protocol configuration is disabled so not linking '" + getProtocolName() + "': " + protocolConfiguration);
}
@@ -201,13 +202,13 @@ public abstract class AbstractProtocol implements Protocol {
}
@Override
- final public void unlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ final public void unlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
withLock(getProtocolName() + "::unlinkProtocolConfiguration", () -> {
LinkedProtocolInfo protocolInfo = linkedProtocolConfigurations.get(protocolConfiguration.getReferenceOrThrow());
if (protocolInfo != null && protocolInfo.currentConnectionStatus != ConnectionStatus.DISABLED) {
LOG.finer("Unlinking protocol configuration from protocol '" + getProtocolName() + "': " + protocolConfiguration);
- doUnlinkProtocolConfiguration(protocolConfiguration);
+ doUnlinkProtocolConfiguration(agent, protocolConfiguration);
updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISCONNECTED);
}
linkedProtocolConfigurations.remove(protocolConfiguration.getReferenceOrThrow());
@@ -499,12 +500,12 @@ public abstract class AbstractProtocol implements Protocol {
/**
* Link the protocol configuration.
*/
- abstract protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration);
+ abstract protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration);
/**
* Unlink the protocol configuration.
*/
- abstract protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration);
+ abstract protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration);
/**
* Link an attribute to its linked protocol configuration.
diff --git a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
index 94087198c..dbd3fbf5c 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/Protocol.java
@@ -450,14 +450,14 @@ public interface Protocol extends ContainerService {
* {@link ConnectionStatus#ERROR} or {@link ConnectionStatus#ERROR_CONFIGURATION} or this method throws an exception
* then attribute linking will be skipped for this {@link ProtocolConfiguration}.
*/
- void linkProtocolConfiguration(AssetAttribute protocolConfiguration, Consumer<ConnectionStatus> statusConsumer)
+ void linkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration, Consumer<ConnectionStatus> statusConsumer)
throws Exception;
/**
* Un-links the protocol configuration from the protocol; called whenever a protocolConfiguration is modified
* or removed.
*/
- void unlinkProtocolConfiguration(AssetAttribute protocolConfiguration);
+ void unlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration);
/**
* Get a {@link ProtocolDescriptor} for this protocol.
diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolClientEventService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolClientEventService.java
new file mode 100644
index 000000000..6fa226ab5
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/ProtocolClientEventService.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright 2020, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.processor.StopProcessor;
+import org.openremote.container.ContainerService;
+import org.openremote.container.security.AuthContext;
+import org.openremote.container.web.ConnectionConstants;
+import org.openremote.model.Constants;
+import org.openremote.model.security.ClientRole;
+
+import java.util.Arrays;
+import java.util.function.Consumer;
+
+import static org.apache.camel.builder.Builder.header;
+
+/**
+ * Interface for {@link Protocol}s to add/remove Camel {@link Exchange} interceptors for specific clients and to
+ * also add/remove clients.
+ *
+ */
+public interface ProtocolClientEventService extends ContainerService {
+
+ String HEADER_ACCESS_RESTRICTED = ProtocolClientEventService.class.getName() + ".HEADER_ACCESS_RESTRICTED";
+ String HEADER_CONNECTION_TYPE = ProtocolClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE";
+ String HEADER_CONNECTION_TYPE_WEBSOCKET = ProtocolClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_WEBSOCKET";
+ String HEADER_CONNECTION_TYPE_MQTT = ProtocolClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_MQTT";
+ String HEADER_REQUEST_RESPONSE_MESSAGE_ID = ProtocolClientEventService.class.getName() + ".HEADER_REQUEST_RESPONSE_MESSAGE_ID";
+
+ class ClientCredentials {
+ String realm;
+ ClientRole[] roles;
+ String clientId;
+ String secret;
+
+ public ClientCredentials(String realm, ClientRole[] roles, String clientId, String secret) {
+ this.realm = realm;
+ this.roles = roles;
+ this.clientId = clientId;
+ this.secret = secret;
+ }
+
+ public String getRealm() {
+ return realm;
+ }
+
+ public void setRealm(String realm) {
+ this.realm = realm;
+ }
+
+ public ClientRole[] getRoles() {
+ return roles;
+ }
+
+ public void setRoles(ClientRole[] roles) {
+ this.roles = roles;
+ }
+
+ public String getClientId() {
+ return clientId;
+ }
+
+ public void setClientId(String clientId) {
+ this.clientId = clientId;
+ }
+
+ public String getSecret() {
+ return secret;
+ }
+
+ public void setSecret(String secret) {
+ this.secret = secret;
+ }
+
+ @Override
+ public String toString() {
+ return ClientCredentials.class.getSimpleName() + "{" +
+ "realm='" + realm + '\'' +
+ ", roles=" + Arrays.toString(roles) +
+ ", clientId='" + clientId + '\'' +
+ ", secret=" + secret +
+ '}';
+ }
+ }
+
+ void addExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) throws RuntimeException;
+
+ void removeExchangeInterceptor(Consumer<Exchange> exchangeInterceptor);
+
+ void closeSession(String sessionKey);
+
+ void sendToSession(String sessionKey, Object data);
+
+ void addClientCredentials(ClientCredentials clientCredentials) throws RuntimeException;
+
+ void removeClientCredentials(String realm, String clientId);
+
+ /**
+ * Method to stop further processing of the exchange
+ */
+ static void stopMessage(Exchange exchange) {
+ exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);
+ }
+
+ static boolean isInbound(Exchange exchange) {
+ return header(HEADER_CONNECTION_TYPE).isNotNull().matches(exchange);
+ }
+
+ static String getSessionKey(Exchange exchange) {
+ return exchange.getIn().getHeader(ConnectionConstants.SESSION_KEY, String.class);
+ }
+
+ static String getClientId(Exchange exchange) {
+ AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
+ if(authContext != null) {
+ return authContext.getClientId();
+ }
+ return null;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetClientProtocol.java
index 93d896478..2f698b483 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetClientProtocol.java
@@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
-import org.openremote.agent.protocol.Protocol;
import org.openremote.agent.protocol.ProtocolLinkedAttributeImport;
import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
import org.openremote.agent.protocol.io.AbstractNettyIoClient;
@@ -123,11 +122,11 @@ public class ArtnetClientProtocol extends AbstractIoClientProtocol<ArtnetPacket,
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
synchronized (protocolMessageConsumers) {
protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
- super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ super.doUnlinkProtocolConfiguration(agent, protocolConfiguration);
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
index 85aa23629..133d8d83a 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/controller/ControllerProtocol.java
@@ -30,6 +30,7 @@ import org.openremote.agent.protocol.http.HttpClientProtocol;
import org.openremote.container.web.WebTargetBuilder;
import org.openremote.container.Container;
import org.openremote.model.AbstractValueHolder;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
@@ -203,7 +204,7 @@ public class ControllerProtocol extends AbstractProtocol {
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
LOG.fine("### Adding new protocol " + protocolConfiguration.getReferenceOrThrow() + " (" + protocolConfiguration.getNameOrThrow() + ")");
String baseURL = protocolConfiguration.getMetaItem(META_PROTOCOL_BASE_URI).flatMap(AbstractValueHolder::getValueAsString)
@@ -235,7 +236,7 @@ public class ControllerProtocol extends AbstractProtocol {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
controllersMap.remove(protocolConfiguration.getReferenceOrThrow());
controllersTargetMap.remove(protocolConfiguration.getReferenceOrThrow());
controllerHeartbeat.remove(protocolConfiguration.getReferenceOrThrow());
diff --git a/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
new file mode 100644
index 000000000..b77fa5d12
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/event/ClientEventProtocol.java
@@ -0,0 +1,208 @@
+/*
+ * Copyright 2020, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.agent.protocol.event;
+
+import org.apache.camel.Exchange;
+import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.agent.protocol.ProtocolClientEventService;
+import org.openremote.container.Container;
+import org.openremote.container.web.ConnectionConstants;
+import org.openremote.model.AbstractValueHolder;
+import org.openremote.model.asset.Asset;
+import org.openremote.model.asset.AssetAttribute;
+import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.AttributeState;
+import org.openremote.model.attribute.MetaItemDescriptor;
+import org.openremote.model.syslog.SyslogCategory;
+import org.openremote.model.value.Value;
+
+import java.util.*;
+import java.util.logging.Logger;
+
+import static org.apache.camel.builder.Builder.body;
+import static org.apache.camel.builder.Builder.header;
+import static org.apache.camel.builder.PredicateBuilder.or;
+import static org.openremote.agent.protocol.ProtocolClientEventService.*;
+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;
+import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;
+import static org.openremote.model.attribute.MetaItemDescriptorImpl.metaItemString;
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY;
+
+/**
+ * This protocol is for pub-sub devices to connect to the manager client event broker (MQTT or websocket);
+ * attributes then linked to this protocol can be written to/read by the devices.
+ */
+public class ClientEventProtocol extends AbstractProtocol {
+
+ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":clientEvent";
+ public static final String PROTOCOL_DISPLAY_NAME = "Client Event";
+ public static final String PROTOCOL_VERSION = "1.0";
+ public static final String CLIENT_ID_PREFIX = "ClientEvent-";
+ protected static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, ClientEventProtocol.class);
+
+ public static final MetaItemDescriptor META_PROTOCOL_CLIENT_SECRET = metaItemString(
+ PROTOCOL_NAME + ":clientSecret",
+ ACCESS_PRIVATE,
+ true,
+ REGEXP_PATTERN_STRING_NON_EMPTY,
+ MetaItemDescriptor.PatternFailure.STRING_EMPTY);
+
+ protected Map<String, AssetAttribute> clientIdProtocolConfigMap = new HashMap<>();
+
+ @Override
+ public void init(Container container) throws Exception {
+ super.init(container);
+ protocolClientEventService.addExchangeInterceptor(this::onMessageIntercept);
+ }
+
+ @Override
+ protected void doStop(Container container) throws Exception {
+ super.doStop(container);
+ protocolClientEventService.removeExchangeInterceptor(this::onMessageIntercept);
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {
+ return Collections.singletonList(
+ META_PROTOCOL_CLIENT_SECRET
+ );
+ }
+
+ @Override
+ protected List<MetaItemDescriptor> getLinkedAttributeMetaItemDescriptors() {
+ return Collections.emptyList();
+ }
+
+ @Override
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
+
+ LOG.info("Creating client credentials for: " + protocolConfiguration);
+ AttributeRef attributeRef = protocolConfiguration.getReferenceOrThrow();
+ String clientId = CLIENT_ID_PREFIX + attributeRef.getEntityId() + ":" + attributeRef.getAttributeName();
+
+ String clientSecret = protocolConfiguration.getMetaItem(META_PROTOCOL_CLIENT_SECRET.getUrn())
+ .flatMap(AbstractValueHolder::getValueAsString)
+ .orElse(UUID.randomUUID().toString());
+
+ ProtocolClientEventService.ClientCredentials clientCredentials =
+ new ProtocolClientEventService.ClientCredentials(
+ agent.getRealm(),
+ null,
+ clientId,
+ clientSecret
+ );
+
+ protocolClientEventService.addClientCredentials(clientCredentials);
+ clientIdProtocolConfigMap.put(clientId, protocolConfiguration);
+ }
+
+ @Override
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
+ LOG.info("Removing client credentials for: " + protocolConfiguration);
+ clientIdProtocolConfigMap.values().remove(protocolConfiguration);
+ AttributeRef attributeRef = protocolConfiguration.getReferenceOrThrow();
+ protocolClientEventService.removeClientCredentials(agent.getRealm(), CLIENT_ID_PREFIX + attributeRef.getEntityId() + ":" + attributeRef.getAttributeName());
+ }
+
+ @Override
+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) throws Exception {
+ // Nothing to do here
+ }
+
+ @Override
+ protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {
+ // Nothing to do here
+ }
+
+ @Override
+ protected void processLinkedAttributeWrite(AttributeEvent event, Value processedValue, AssetAttribute protocolConfiguration) {
+ // We'll get here when an attribute event is pushed through the processing chain as with all protocols it won't
+ // make it to the end of the processing chain, and won't reach the client so we write it through immediately
+ // so it will be sent to the client
+ updateLinkedAttribute(new AttributeState(event.getAttributeRef(), processedValue));
+ }
+
+ @Override
+ public String getProtocolName() {
+ return PROTOCOL_NAME;
+ }
+
+ @Override
+ public String getProtocolDisplayName() {
+ return PROTOCOL_DISPLAY_NAME;
+ }
+
+ @Override
+ public String getVersion() {
+ return PROTOCOL_VERSION;
+ }
+
+ protected void onMessageIntercept(Exchange exchange) {
+ String clientId = getClientId(exchange);
+
+ if (!isThisClient(clientId)) {
+ return;
+ }
+
+ AssetAttribute protocolConfiguration = clientIdProtocolConfigMap.get(clientId);
+
+ if (protocolConfiguration == null) {
+ LOG.info("Message received from a client for an unlinked protocol configuration, requesting disconnect");
+ protocolClientEventService.closeSession(getSessionKey(exchange));
+ stopMessage(exchange);
+ return;
+ }
+
+ if (header(ConnectionConstants.SESSION_OPEN).matches(exchange)) {
+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.CONNECTED);
+ return;
+ }
+
+ if (or(header(ConnectionConstants.SESSION_CLOSE), header(ConnectionConstants.SESSION_CLOSE_ERROR)).matches(exchange)) {
+ updateStatus(protocolConfiguration.getReferenceOrThrow(), ConnectionStatus.DISCONNECTED);
+ return;
+ }
+
+ if (isInbound(exchange)) {
+ if (body().isInstanceOf(AttributeEvent.class).matches(exchange)) {
+ // Inbound attribute event is essentially a protocol sensor update
+
+ AttributeEvent attributeEvent = exchange.getIn().getBody(AttributeEvent.class);
+ AssetAttribute linkedAttribute = getLinkedAttribute(attributeEvent.getAttributeRef());
+
+ if (linkedAttribute == null) {
+ LOG.info("Message received from a client for an unlinked attribute, so ignoring");
+ stopMessage(exchange);
+ return;
+ }
+
+ // TODO: Can do some processing of the incoming value here
+ updateLinkedAttribute(new AttributeState(attributeEvent.getAttributeRef(), attributeEvent.getValue().orElse(null)));
+ }
+ }
+ }
+
+ protected boolean isThisClient(String clientId) {
+ return clientId != null && clientId.startsWith(CLIENT_ID_PREFIX);
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java
index 53091a057..5e9cc1c32 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/AbstractHttpServerProtocol.java
@@ -165,7 +165,7 @@ public abstract class AbstractHttpServerProtocol extends AbstractProtocol {
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
Application application = createApplication(protocolConfiguration);
ResteasyDeployment deployment = createDeployment(application, protocolConfiguration);
DeploymentInfo deploymentInfo = createDeploymentInfo(deployment, protocolConfiguration);
@@ -174,7 +174,7 @@ public abstract class AbstractHttpServerProtocol extends AbstractProtocol {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
undeploy(protocolConfiguration);
}
@@ -402,4 +402,4 @@ public abstract class AbstractHttpServerProtocol extends AbstractProtocol {
}
}
-}
\ No newline at end of file
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
index d7afe199a..749f5759b 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HttpClientProtocol.java
@@ -35,6 +35,7 @@ import org.openremote.container.web.WebTargetBuilder;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
@@ -658,7 +659,7 @@ public class HttpClientProtocol extends AbstractProtocol {
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
String baseUri = protocolConfiguration.getMetaItem(META_PROTOCOL_BASE_URI)
@@ -827,7 +828,7 @@ public class HttpClientProtocol extends AbstractProtocol {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef protocolConfigurationRef = protocolConfiguration.getReferenceOrThrow();
clientMap.remove(protocolConfigurationRef);
requestMap.remove(protocolConfigurationRef);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java
index 49fc84ed4..6f5ac2cdc 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIoClientProtocol.java
@@ -30,6 +30,7 @@ import io.netty.util.CharsetUtil;
import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.agent.protocol.Protocol;
import org.openremote.container.Container;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.AttributeEvent;
@@ -222,7 +223,7 @@ public abstract class AbstractIoClientProtocol<T, U extends IoClient<T>> extends
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
ProtocolIoClient<T, U> protocolIoClient = null;
@@ -242,7 +243,7 @@ public abstract class AbstractIoClientProtocol<T, U extends IoClient<T>> extends
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
ProtocolIoClient<T, U> protocolIoClient = protocolIoClientMap.remove(protocolRef);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
index e800ac078..df7be8194 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
@@ -35,7 +35,6 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
-import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Consumer;
@@ -206,7 +205,7 @@ public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttri
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
String connectionType = protocolConfiguration.getMetaItem(META_KNX_IP_CONNECTION_TYPE).flatMap(AbstractValueHolder::getValueAsString).orElse("TUNNELLING");
if (!connectionType.equals("TUNNELLING") && !connectionType.equals("ROUTING")) {
LOG.severe("KNX connectionType can either be 'TUNNELLING' or 'ROUTING' for protocol configuration: " + protocolConfiguration);
@@ -246,7 +245,7 @@ public class KNXProtocol extends AbstractProtocol implements ProtocolLinkedAttri
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
Consumer<ConnectionStatus> statusConsumer;
synchronized (statusConsumerMap) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
index 09fcb6b71..eb3ccfdd1 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/macro/MacroProtocol.java
@@ -20,11 +20,11 @@
package org.openremote.agent.protocol.macro;
import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
import org.openremote.model.syslog.SyslogCategory;
-import org.openremote.model.util.Pair;
import org.openremote.model.value.Value;
import org.openremote.model.value.ValueType;
import org.openremote.model.value.Values;
@@ -179,7 +179,7 @@ public class MacroProtocol extends AbstractProtocol {
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
// Protocol configuration is actually a Macro Configuration
AttributeRef macroRef = protocolConfiguration.getReferenceOrThrow();
@@ -197,7 +197,7 @@ public class MacroProtocol extends AbstractProtocol {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef macroRef = protocolConfiguration.getReferenceOrThrow();
macroMap.remove(macroRef);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java
index 175d27bb0..83aa39308 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialClientProtocol.java
@@ -23,6 +23,7 @@ import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.Protocol;
import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
import org.openremote.agent.protocol.tcp.TcpIoClient;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.*;
@@ -116,11 +117,11 @@ public class SerialClientProtocol extends AbstractSerialClientProtocol<String> {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
synchronized (protocolMessageConsumers) {
protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
- super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ super.doUnlinkProtocolConfiguration(agent, protocolConfiguration);
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java
index d218719de..724155cad 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java
@@ -23,6 +23,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValidationFailure;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.attribute.*;
@@ -223,7 +224,7 @@ public class SimulatorProtocol extends AbstractProtocol {
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
instances.computeIfAbsent(
@@ -256,7 +257,7 @@ public class SimulatorProtocol extends AbstractProtocol {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef configRef = protocolConfiguration.getReferenceOrThrow();
instances.remove(configRef);
}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java
index 20fe251aa..e6b4c1006 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/AbstractTcpServerProtocol.java
@@ -21,6 +21,7 @@ package org.openremote.agent.protocol.tcp;
import org.openremote.agent.protocol.AbstractProtocol;
import org.openremote.model.AbstractValueHolder;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
@@ -68,7 +69,7 @@ public abstract class AbstractTcpServerProtocol<T extends AbstractTcpServer<U>,
protected final Map<AttributeRef, T> tcpServerMap = new HashMap<>();
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
int port = protocolConfiguration.getMetaItem(META_PROTOCOL_BIND_PORT)
@@ -98,7 +99,7 @@ public abstract class AbstractTcpServerProtocol<T extends AbstractTcpServer<U>,
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
T tcpServer = tcpServerMap.remove(protocolRef);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java
index eb421a8eb..646b08d35 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/TcpClientProtocol.java
@@ -22,6 +22,7 @@ package org.openremote.agent.protocol.tcp;
import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.Protocol;
import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.*;
@@ -124,11 +125,11 @@ public class TcpClientProtocol extends AbstractTcpClientProtocol<String> {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
synchronized (protocolMessageConsumers) {
protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
- super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ super.doUnlinkProtocolConfiguration(agent, protocolConfiguration);
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java
index f419b7c17..717b98a61 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/timer/TimerProtocol.java
@@ -20,6 +20,7 @@
package org.openremote.agent.protocol.timer;
import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.attribute.MetaItemType;
import org.openremote.model.asset.agent.ConnectionStatus;
@@ -162,7 +163,7 @@ public class TimerProtocol extends AbstractProtocol {
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
// Verify that this is a valid Timer Configuration
@@ -207,7 +208,7 @@ public class TimerProtocol extends AbstractProtocol {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef protocolConfigRef = protocolConfiguration.getReferenceOrThrow();
if (cronExpressionMap.remove(protocolConfigRef) != null) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
index 090908d08..ae3e2c789 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java
@@ -22,6 +22,7 @@ package org.openremote.agent.protocol.udp;
import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.Protocol;
import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.*;
@@ -125,11 +126,11 @@ public class UdpClientProtocol extends AbstractUdpClientProtocol<String> {
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
synchronized (protocolMessageConsumers) {
protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
- super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ super.doUnlinkProtocolConfiguration(agent, protocolConfiguration);
}
@Override
diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
index 0c532cab8..fa937ec0d 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java
@@ -123,7 +123,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
// Look for existing network
@@ -159,7 +159,7 @@ public abstract class AbstractVelbusProtocol extends AbstractProtocol implements
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
Pair<VelbusNetwork, Consumer<ConnectionStatus>> velbusNetworkAndConsumer = networkConfigurationMap.remove(protocolRef);
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java
index cadad71b9..04da83d79 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/AbstractWebsocketClientProtocol.java
@@ -31,6 +31,7 @@ import org.openremote.container.web.WebTargetBuilder;
import org.openremote.agent.protocol.io.AbstractIoClientProtocol;
import org.openremote.container.Container;
import org.openremote.model.AbstractValueHolder;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ConnectionStatus;
import org.openremote.model.asset.agent.ProtocolConfiguration;
@@ -144,8 +145,8 @@ public abstract class AbstractWebsocketClientProtocol<T> extends AbstractIoClien
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
- super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
+ super.doUnlinkProtocolConfiguration(agent, protocolConfiguration);
AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();
clientHeaders.remove(protocolRef);
synchronized (protocolConnectedTasks) {
diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java
index d62cc8d4c..6cc7a0af7 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java
@@ -23,6 +23,7 @@ import io.netty.channel.ChannelHandler;
import org.openremote.agent.protocol.Protocol;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
+import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.agent.ProtocolConfiguration;
import org.openremote.model.attribute.*;
@@ -118,11 +119,11 @@ public class WebsocketClientProtocol extends AbstractWebsocketClientProtocol<Str
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
synchronized (protocolMessageConsumers) {
protocolMessageConsumers.remove(protocolConfiguration.getReferenceOrThrow());
}
- super.doUnlinkProtocolConfiguration(protocolConfiguration);
+ super.doUnlinkProtocolConfiguration(agent, protocolConfiguration);
}
@Override
diff --git a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
index 7dc45ea60..e71e4baf9 100644
--- a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
+++ b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService
@@ -10,3 +10,4 @@ org.openremote.agent.protocol.websocket.WebsocketClientProtocol
org.openremote.agent.protocol.artnet.ArtnetClientProtocol
org.openremote.agent.protocol.tcp.TcpClientProtocol
org.openremote.agent.protocol.serial.SerialClientProtocol
+org.openremote.agent.protocol.event.ClientEventProtocol
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
index 415a11f2f..57a8edbe6 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -184,7 +184,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
/// For all agents, go through their protocol configurations and find
// assets that are linked to them, to create the binding on startup
for (Asset agent : agents) {
- linkProtocolConfigurations(agent.getAttributesStream()
+ linkProtocolConfigurations(agent, agent.getAttributesStream()
.filter(ProtocolConfiguration::isProtocolConfiguration)
.collect(Collectors.toList())
);
@@ -352,6 +352,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
return;
}
linkProtocolConfigurations(
+ agent,
agent.getAttributesStream()
.filter(ProtocolConfiguration::isProtocolConfiguration)
.collect(Collectors.toList())
@@ -388,7 +389,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
// Compare protocol configurations by JSON value
// Unlink protocols that are in oldConfigs but not in newConfigs
- unlinkProtocolConfigurations(oldProtocolConfigurations
+ unlinkProtocolConfigurations(agent, oldProtocolConfigurations
.stream()
.filter(oldProtocolAttribute -> newProtocolConfigurations
.stream()
@@ -398,7 +399,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
);
// Link protocols that are in newConfigs but not in oldConfigs
- linkProtocolConfigurations(newProtocolConfigurations
+ linkProtocolConfigurations(agent, newProtocolConfigurations
.stream()
.filter(newProtocolAttribute -> oldProtocolConfigurations
.stream()
@@ -414,7 +415,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
return;
}
// Unlink any attributes that have an agent link to this agent
- unlinkProtocolConfigurations(agent.getAttributesStream()
+ unlinkProtocolConfigurations(agent, agent.getAttributesStream()
.filter(ProtocolConfiguration::isProtocolConfiguration)
.collect(Collectors.toList())
);
@@ -561,7 +562,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
.orElse(null);
}
- protected void linkProtocolConfigurations(List<AssetAttribute> configurations) {
+ protected void linkProtocolConfigurations(Asset agent, List<AssetAttribute> configurations) {
withLock(getClass().getSimpleName() + "::linkProtocolConfigurations", () -> configurations.forEach(configuration -> {
AttributeRef protocolAttributeRef = configuration.getReferenceOrThrow();
Protocol protocol = getProtocol(configuration);
@@ -583,7 +584,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
// Link the protocol configuration to the protocol
try {
- protocol.linkProtocolConfiguration(configuration, deploymentStatusConsumer);
+ protocol.linkProtocolConfiguration(agent, configuration, deploymentStatusConsumer);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Protocol threw an exception during protocol configuration linking", e);
// Set status to error
@@ -622,7 +623,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
}));
}
- protected void unlinkProtocolConfigurations(List<AssetAttribute> configurations) {
+ protected void unlinkProtocolConfigurations(Asset agent, List<AssetAttribute> configurations) {
withLock(getClass().getSimpleName() + "::unlinkProtocolConfigurations", () -> configurations.forEach(configuration -> {
AttributeRef protocolAttributeRef = configuration.getReferenceOrThrow();
@@ -652,7 +653,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
// Unlink the protocol configuration from the protocol
try {
- protocol.unlinkProtocolConfiguration(configuration);
+ protocol.unlinkProtocolConfiguration(agent, configuration);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Protocol threw an exception during protocol configuration unlinking", e);
}
diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
index 83dc52d15..6066a6b34 100644
--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java
@@ -22,6 +22,7 @@ package org.openremote.manager.asset;
import org.apache.camel.builder.RouteBuilder;
import org.hibernate.Session;
import org.hibernate.jdbc.AbstractReturningWork;
+import org.openremote.agent.protocol.ProtocolClientEventService;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
@@ -376,7 +377,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
}
boolean isAttributeRead = !TextUtil.isNullOrEmpty(attribute);
- String sessionKey = getSessionKey(exchange);
+ String sessionKey = ProtocolClientEventService.getSessionKey(exchange);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
// Superuser can get all, User must have role
@@ -418,7 +419,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic
ReadAssetsEvent readAssets = exchange.getIn().getBody(ReadAssetsEvent.class);
AssetQuery query = readAssets.getAssetQuery();
- String sessionKey = getSessionKey(exchange);
+ String sessionKey = ProtocolClientEventService.getSessionKey(exchange);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
// Superuser can get all, User must have role
diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
index 891978621..49c63c54a 100644
--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java
@@ -21,31 +21,38 @@ package org.openremote.manager.event;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
+import org.keycloak.representations.idm.ClientRepresentation;
+import org.openremote.agent.protocol.ProtocolClientEventService;
import org.openremote.container.Container;
-import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.security.AuthContext;
import org.openremote.container.timer.TimerService;
import org.openremote.container.web.ConnectionConstants;
-import org.openremote.manager.asset.AssetStorageService;
import org.openremote.manager.concurrent.ManagerExecutorService;
import org.openremote.manager.gateway.GatewayService;
import org.openremote.manager.mqtt.MqttBrokerService;
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.event.shared.*;
import org.openremote.model.syslog.SyslogEvent;
+import org.openremote.model.util.TextUtil;
+import javax.websocket.CloseReason;
+import javax.websocket.Session;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.function.Consumer;
+import java.util.logging.Level;
import java.util.logging.Logger;
-import static org.apache.camel.builder.PredicateBuilder.*;
-import static org.openremote.manager.gateway.GatewayService.isGatewayClientId;
-
+import static org.apache.camel.builder.PredicateBuilder.or;
+import static org.openremote.container.web.ConnectionConstants.SESSION;
+import static org.openremote.agent.protocol.ProtocolClientEventService.getSessionKey;
/**
* Receives and publishes messages, handles the client/server event bus.
* <p>
@@ -93,7 +100,17 @@ import static org.openremote.manager.gateway.GatewayService.isGatewayClientId;
* </p></dd>
* </dl>
*/
-public class ClientEventService implements ContainerService {
+public class ClientEventService implements ProtocolClientEventService {
+
+ protected static class SessionInfo {
+ String connectionType;
+ Runnable closeRunnable;
+
+ public SessionInfo(String connectionType, Runnable closeRunnable) {
+ this.connectionType = connectionType;
+ this.closeRunnable = closeRunnable;
+ }
+ }
public static final int PRIORITY = ManagerWebService.PRIORITY - 200;
private static final Logger LOG = Logger.getLogger(ClientEventService.class.getName());
@@ -104,14 +121,9 @@ public class ClientEventService implements ContainerService {
public static final String CLIENT_EVENT_QUEUE = "seda://ClientEventQueue?multipleConsumers=false&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&size=25000";
- public static final String HEADER_ACCESS_RESTRICTED = ClientEventService.class.getName() + ".HEADER_ACCESS_RESTRICTED";
- public static final String HEADER_CONNECTION_TYPE = ClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE";
- public static final String HEADER_CONNECTION_TYPE_WEBSOCKET = ClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_WEBSOCKET";
- public static final String HEADER_CONNECTION_TYPE_MQTT = ClientEventService.class.getName() + ".HEADER_CONNECTION_TYPE_MQTT";
- public static final String HEADER_REQUEST_RESPONSE_MESSAGE_ID = ClientEventService.class.getName() + ".HEADER_REQUEST_RESPONSE_MESSAGE_ID";
-
final protected Collection<EventSubscriptionAuthorizer> eventSubscriptionAuthorizers = new CopyOnWriteArraySet<>();
- protected Map<String, String> sessionKeyConnectionTypeMap;
+ final protected Collection<Consumer<Exchange>> exchangeInterceptors = new CopyOnWriteArraySet<>();
+ protected Map<String, SessionInfo> sessionKeyInfoMap = new HashMap<>();
protected TimerService timerService;
protected MessageBrokerService messageBrokerService;
protected ManagerIdentityService identityService;
@@ -131,8 +143,6 @@ public class ClientEventService implements ContainerService {
identityService = container.getService(ManagerIdentityService.class);
gatewayService = container.getService(GatewayService.class);
- sessionKeyConnectionTypeMap = new HashMap<>();
-
eventSubscriptions = new EventSubscriptions(
container.getService(TimerService.class),
container.getService(ManagerExecutorService.class)
@@ -159,12 +169,9 @@ public class ClientEventService implements ContainerService {
.when(header(ConnectionConstants.SESSION_OPEN))
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
- sessionKeyConnectionTypeMap.put(sessionKey, (String) exchange.getIn().getHeader(HEADER_CONNECTION_TYPE));
+ sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));
+ passToInterceptors(exchange);
})
- .choice()
- .when(exchange -> isGatewayClientId(getClientId(exchange)))
- .to(GatewayService.GATEWAY_EVENT_TOPIC)
- .endChoice()
.stop()
.when(or(
header(ConnectionConstants.SESSION_CLOSE),
@@ -172,16 +179,15 @@ public class ClientEventService implements ContainerService {
))
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
- sessionKeyConnectionTypeMap.remove(sessionKey);
+ sessionKeyInfoMap.remove(sessionKey);
eventSubscriptions.cancelAll(sessionKey);
+ passToInterceptors(exchange);
})
- .choice()
- .when(exchange -> isGatewayClientId(getClientId(exchange)))
- .to(GatewayService.GATEWAY_EVENT_TOPIC)
- .endChoice()
.stop()
.end()
.process(exchange -> {
+
+ // Do basic formatting of exchange
EventRequestResponseWrapper<?> requestResponse = null;
if (exchange.getIn().getBody() instanceof EventRequestResponseWrapper) {
requestResponse = exchange.getIn().getBody(EventRequestResponseWrapper.class);
@@ -193,10 +199,31 @@ public class ClientEventService implements ContainerService {
exchange.getIn().setHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, requestResponse.getMessageId());
exchange.getIn().setBody(event);
}
+
+ if (exchange.getIn().getBody() instanceof String) {
+ String bodyStr = exchange.getIn().getBody(String.class);
+ if (bodyStr.startsWith(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX)) {
+ exchange.getIn().setBody(exchange.getIn().getBody(EventSubscription.class));
+ } else if (bodyStr.startsWith(CancelEventSubscription.MESSAGE_PREFIX)) {
+ exchange.getIn().setBody(exchange.getIn().getBody(CancelEventSubscription.class));
+ } else if (bodyStr.startsWith(RenewEventSubscriptions.MESSAGE_PREFIX)) {
+ exchange.getIn().setBody(exchange.getIn().getBody(RenewEventSubscriptions.class));
+ } else if (bodyStr.startsWith(SharedEvent.MESSAGE_PREFIX)) {
+ exchange.getIn().setBody(exchange.getIn().getBody(SharedEvent.class));
+ }
+ }
+
+ if (exchange.getIn().getBody() instanceof SharedEvent) {
+ SharedEvent event = exchange.getIn().getBody(SharedEvent.class);
+ // If there is no timestamp in event, set to system time
+ if (event.getTimestamp() <= 0) {
+ event.setTimestamp(timerService.getCurrentTimeMillis());
+ }
+ }
})
+ .process(exchange -> passToInterceptors(exchange))
.choice()
- .when(bodyAs(String.class).startsWith(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX))
- .convertBodyTo(EventSubscription.class)
+ .when(body().isInstanceOf(EventSubscription.class))
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
EventSubscription<?> subscription = exchange.getIn().getBody(EventSubscription.class);
@@ -215,27 +242,13 @@ public class ClientEventService implements ContainerService {
}
})
.stop()
- .when(or(
- body().isInstanceOf(CancelEventSubscription.class),
- bodyAs(String.class).startsWith(CancelEventSubscription.MESSAGE_PREFIX)
- ))
- .choice()
- .when(bodyAs(String.class).startsWith(CancelEventSubscription.MESSAGE_PREFIX))
- .convertBodyTo(CancelEventSubscription.class)
- .endChoice()
+ .when(body().isInstanceOf(CancelEventSubscription.class))
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
eventSubscriptions.cancel(sessionKey, exchange.getIn().getBody(CancelEventSubscription.class));
})
.stop()
- .when(or(
- body().isInstanceOf(RenewEventSubscriptions.class),
- bodyAs(String.class).startsWith(RenewEventSubscriptions.MESSAGE_PREFIX)
- ))
- .choice()
- .when(bodyAs(String.class).startsWith(RenewEventSubscriptions.MESSAGE_PREFIX))
- .convertBodyTo(RenewEventSubscriptions.class)
- .endChoice()
+ .when(body().isInstanceOf(RenewEventSubscriptions.class))
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
@@ -243,37 +256,19 @@ public class ClientEventService implements ContainerService {
eventSubscriptions.update(sessionKey, restrictedUser,exchange.getIn().getBody(RenewEventSubscriptions.class).getSubscriptionIds());
})
.stop()
- .when(or(
- body().isInstanceOf(SharedEvent.class),
- bodyAs(String.class).startsWith(SharedEvent.MESSAGE_PREFIX)
- ))
- .choice()
- .when(bodyAs(String.class).startsWith(SharedEvent.MESSAGE_PREFIX))
- .convertBodyTo(SharedEvent.class)
- .endChoice()
- .process(exchange -> {
- SharedEvent event = exchange.getIn().getBody(SharedEvent.class);
- // If there is no timestamp in event, set to system time
- if (event.getTimestamp() <= 0) {
- event.setTimestamp(timerService.getCurrentTimeMillis());
- }
- })
+ .when(body().isInstanceOf(SharedEvent.class))
.choice()
- .when(header(HEADER_CONNECTION_TYPE).isNotNull())
- .choice()
- .when(exchange -> isGatewayClientId(getClientId(exchange)))
- .to(GatewayService.GATEWAY_EVENT_TOPIC)
- .otherwise()
- .to(ClientEventService.CLIENT_EVENT_TOPIC)
- .endChoice()
+ .when(header(HEADER_CONNECTION_TYPE).isNotNull()) // Inbound messages from clients
+ .to(ClientEventService.CLIENT_EVENT_TOPIC)
+ .stop()
.when(header(HEADER_CONNECTION_TYPE).isNull()) // Outbound message to clients
.split(method(eventSubscriptions, "splitForSubscribers"))
.process(exchange -> {
String sessionKey = getSessionKey(exchange);
sendToSession(sessionKey, exchange.getIn().getBody());
})
+ .stop()
.endChoice()
- .stop()
.otherwise()
.process(exchange -> LOG.fine("Unsupported message body: " + exchange.getIn().getBody()))
.end();
@@ -291,11 +286,70 @@ public class ClientEventService implements ContainerService {
stopped = true;
}
+ @Override
+ public void addExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) throws RuntimeException {
+ exchangeInterceptors.add(exchangeInterceptor);
+ }
+
+ @Override
+ public void removeExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) {
+ exchangeInterceptors.remove(exchangeInterceptor);
+ }
+
+ @Override
+ public void addClientCredentials(ClientCredentials clientCredentials) throws RuntimeException {
+ if (!(identityService.getIdentityProvider() instanceof ManagerKeycloakIdentityProvider)) {
+ throw new IllegalStateException("Cannot add client credentials if not using Keycloak identity provider");
+ }
+
+ if (TextUtil.isNullOrEmpty(clientCredentials.getRealm())
+ || TextUtil.isNullOrEmpty(clientCredentials.getClientId())
+ || TextUtil.isNullOrEmpty(clientCredentials.getSecret())) {
+ throw new IllegalArgumentException("Invalid client credentials realm, client ID and secret must be specified");
+ }
+
+ ManagerKeycloakIdentityProvider keycloakIdentityProvider = (ManagerKeycloakIdentityProvider)identityService.getIdentityProvider();
+
+ // Check if exists already
+ ClientRepresentation clientRepresentation = keycloakIdentityProvider.getClient(clientCredentials.getRealm(), clientCredentials.getClientId());
+
+ if (clientRepresentation == null) {
+ clientRepresentation = new ClientRepresentation();
+ clientRepresentation.setId(UUID.nameUUIDFromBytes(clientCredentials.getClientId().getBytes()).toString());
+ clientRepresentation.setStandardFlowEnabled(false);
+ clientRepresentation.setImplicitFlowEnabled(false);
+ clientRepresentation.setDirectAccessGrantsEnabled(false);
+ clientRepresentation.setServiceAccountsEnabled(true);
+ clientRepresentation.setClientAuthenticatorType("client-secret");
+ clientRepresentation.setClientId(clientCredentials.getClientId());
+ }
+
+ clientRepresentation.setSecret(clientCredentials.getSecret());
+ LOG.info("Creating/updating client event service client credentials: " + clientCredentials);
+ keycloakIdentityProvider.createClient(clientCredentials.getRealm(), clientRepresentation);
+ }
+
+ @Override
+ public void removeClientCredentials(String realm, String clientId) throws RuntimeException {
+ if (!(identityService.getIdentityProvider() instanceof ManagerKeycloakIdentityProvider)) {
+ throw new IllegalStateException("Cannot remove client credentials if not using Keycloak identity provider");
+ }
+
+ if (TextUtil.isNullOrEmpty(realm)
+ || TextUtil.isNullOrEmpty(clientId)) {
+ throw new IllegalArgumentException("Invalid client credentials realm and client ID must be specified");
+ }
+
+ ManagerKeycloakIdentityProvider keycloakIdentityProvider = (ManagerKeycloakIdentityProvider)identityService.getIdentityProvider();
+ LOG.info("Deleting client event service client: realm=" + realm + ", client ID=" + clientId);
+ keycloakIdentityProvider.deleteClient(realm, clientId);
+ }
+
public void addSubscriptionAuthorizer(EventSubscriptionAuthorizer authorizer) {
this.eventSubscriptionAuthorizers.add(authorizer);
}
- public boolean authorizeEventSubscription(AuthContext authContext, EventSubscription subscription) {
+ public boolean authorizeEventSubscription(AuthContext authContext, EventSubscription<?> subscription) {
return eventSubscriptionAuthorizers.stream()
.anyMatch(authorizer -> authorizer.apply(authContext, subscription));
}
@@ -326,14 +380,14 @@ public class ClientEventService implements ContainerService {
public void sendToSession(String sessionKey, Object data) {
if (messageBrokerService != null && messageBrokerService.getProducerTemplate() != null) {
LOG.fine("Sending to session '" + sessionKey + "': " + data);
- String sessionConnectionType = sessionKeyConnectionTypeMap.get(sessionKey);
- if (sessionConnectionType.equals(HEADER_CONNECTION_TYPE_WEBSOCKET)) {
+ SessionInfo sessionInfo = sessionKeyInfoMap.get(sessionKey);
+ if (sessionInfo.connectionType.equals(HEADER_CONNECTION_TYPE_WEBSOCKET)) {
messageBrokerService.getProducerTemplate().sendBodyAndHeader(
"websocket://" + WEBSOCKET_EVENTS,
data,
ConnectionConstants.SESSION_KEY, sessionKey
);
- } else if (sessionConnectionType.equals(HEADER_CONNECTION_TYPE_MQTT)) {
+ } else if (sessionInfo.connectionType.equals(HEADER_CONNECTION_TYPE_MQTT)) {
messageBrokerService.getProducerTemplate().sendBodyAndHeader(
MqttBrokerService.MQTT_CLIENT_QUEUE,
data,
@@ -343,25 +397,56 @@ public class ClientEventService implements ContainerService {
}
}
- public static String getSessionKey(Exchange exchange) {
- return exchange.getIn().getHeader(ConnectionConstants.SESSION_KEY, String.class);
+ public void closeSession(String sessionKey) {
+ SessionInfo sessionInfo = sessionKeyInfoMap.get(sessionKey);
+
+ if (sessionInfo == null || sessionInfo.closeRunnable == null) {
+ return;
+ }
+
+ LOG.fine("Closing session: " + sessionKey);
+ sessionInfo.closeRunnable.run();
}
public EventSubscriptions getEventSubscriptions() {
return eventSubscriptions;
}
+ protected void passToInterceptors(Exchange exchange) {
+ // Pass to each interceptor and stop if any interceptor marks the exchange as stop routing
+ if (exchangeInterceptors.stream().anyMatch(interceptor -> {
+ interceptor.accept(exchange);
+ boolean stop = exchange.getProperty(Exchange.ROUTE_STOP, false, Boolean.class);
+ if (stop) {
+ LOG.fine("Client event interceptor marked exchange as `stop routing`");
+ }
+ return stop;
+ }));
+ }
+
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
'}';
}
- public static String getClientId(Exchange exchange) {
- AuthContext authContext = exchange.getIn().getHeader(Constants.AUTH_CONTEXT, AuthContext.class);
- if(authContext != null) {
- return authContext.getClientId();
+ // TODO: Implement MQTT close once supported by Moquette (https://github.com/moquette-io/moquette/issues/469)
+ protected static SessionInfo createSessionInfo(String sessionKey, Exchange exchange) {
+
+ String connectionType = (String) exchange.getIn().getHeader(HEADER_CONNECTION_TYPE);
+ Runnable closeRunnable = null;
+
+ if (HEADER_CONNECTION_TYPE_WEBSOCKET.equals(connectionType)) {
+ Session session = exchange.getIn().getHeader(SESSION, Session.class);
+ closeRunnable = () -> {
+ try {
+ session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, ""));
+ } catch (Exception e) {
+ LOG.log(Level.INFO, "Failed to close client session: " + sessionKey);
+ }
+ };
}
- return null;
+
+ return new SessionInfo(connectionType, closeRunnable);
}
}
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 d1b8d6fa1..136b62400 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java
@@ -74,6 +74,7 @@ public class GatewayConnector {
List<String> syncAssetIds;
int syncIndex;
int syncErrors;
+ Asset gateway;
String expectedSyncResponseName;
public GatewayConnector(
@@ -89,6 +90,7 @@ public class GatewayConnector {
this.realm = gateway.getRealm();
this.gatewayId = gateway.getId();
this.disabled = disabled;
+ this.gateway = gateway;
}
public void sendMessageToGateway(Object 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 a2c97a3d1..d13d2e2b8 100644
--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java
@@ -19,14 +19,13 @@
*/
package org.openremote.manager.gateway;
+import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
-import org.keycloak.admin.client.resource.ClientsResource;
-import org.keycloak.representations.idm.ClientRepresentation;
+import org.openremote.agent.protocol.ProtocolClientEventService;
import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.message.MessageBrokerService;
import org.openremote.container.persistence.PersistenceEvent;
-import org.openremote.container.web.ClientRequestInfo;
import org.openremote.container.web.ConnectionConstants;
import org.openremote.manager.asset.AssetProcessingException;
import org.openremote.manager.asset.AssetProcessingService;
@@ -36,13 +35,12 @@ import org.openremote.manager.concurrent.ManagerExecutorService;
import org.openremote.manager.event.ClientEventService;
import org.openremote.manager.security.ManagerIdentityService;
import org.openremote.manager.security.ManagerKeycloakIdentityProvider;
+import org.openremote.model.AbstractValueHolder;
import org.openremote.model.ValueHolder;
import org.openremote.model.asset.Asset;
import org.openremote.model.asset.AssetAttribute;
import org.openremote.model.asset.AssetType;
import org.openremote.model.attribute.AttributeEvent;
-import org.openremote.model.event.shared.EventRequestResponseWrapper;
-import org.openremote.model.event.shared.RenewEventSubscriptions;
import org.openremote.model.event.shared.SharedEvent;
import org.openremote.model.gateway.GatewayDisconnectEvent;
import org.openremote.model.query.AssetQuery;
@@ -51,20 +49,21 @@ import org.openremote.model.util.TextUtil;
import org.openremote.model.value.Values;
import javax.persistence.EntityManager;
-import javax.websocket.CloseReason;
import javax.websocket.Session;
-import javax.ws.rs.core.Response;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import static org.apache.camel.builder.PredicateBuilder.and;
import static org.apache.camel.builder.PredicateBuilder.or;
import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;
import static org.openremote.container.persistence.PersistenceEvent.isPersistenceEventForEntityType;
import static org.openremote.manager.event.ClientEventService.*;
+import static org.openremote.model.asset.AssetAttribute.attributeFromJson;
import static org.openremote.model.syslog.SyslogCategory.GATEWAY;
public class GatewayService extends RouteBuilder implements ContainerService, AssetUpdateProcessor {
@@ -82,9 +81,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
protected final Map<String, String> assetIdGatewayIdMap = new HashMap<>();
protected boolean active;
- public static final String GATEWAY_EVENT_TOPIC = "seda://GatewayEventTopic?multipleConsumers=false&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&limitConcurrentConsumers=false&size=10000";
-
- public static boolean isGatewayClientId(String clientId) {
+ protected static boolean isGatewayClientId(String clientId) {
return clientId != null && clientId.startsWith(GATEWAY_CLIENT_ID_PREFIX);
}
@@ -112,6 +109,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
active = true;
identityProvider = (ManagerKeycloakIdentityProvider) identityService.getIdentityProvider();
container.getService(MessageBrokerService.class).getContext().addRoutes(this);
+ clientEventService.addExchangeInterceptor(this::onMessageIntercept);
}
}
@@ -196,46 +194,33 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
}
}
});
+ }
+ }
- // From gateway clients
- from(GATEWAY_EVENT_TOPIC)
- .routeId("FromGatewayUpdates")
- .choice()
- .when(header(ConnectionConstants.SESSION_OPEN))
- .process(exchange -> {
- Session session = exchange.getIn().getHeader(ConnectionConstants.SESSION, Session.class);
- String sessionKey = getSessionKey(exchange);
- String clientId = getClientId(exchange);
- if (isGatewayClientId(clientId)) {
- processGatewayConnected(clientId, sessionKey, session);
- }
- })
- .stop()
- .when(or(
- header(ConnectionConstants.SESSION_CLOSE),
- header(ConnectionConstants.SESSION_CLOSE_ERROR)
- ))
- .process(exchange -> {
- String clientId = getClientId(exchange);
- if (isGatewayClientId(clientId)) {
- processGatewayDisconnected(clientId);
- }
- })
- .stop()
- .when(body().isInstanceOf(SharedEvent.class))
- .process(exchange -> {
- String clientId = getClientId(exchange);
- if (clientId != null) {
- String gatewayId = getGatewayIdFromClientId(clientId);
- onGatewayEventReceived(gatewayId, exchange.getIn().getHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class), exchange.getIn().getBody(SharedEvent.class));
- }
- })
- .stop()
- .otherwise()
- .process(exchange -> {
- LOG.warning("UNKOWN EVENT: " + exchange.getIn().getBody());
- })
- .end();
+ protected void onMessageIntercept(Exchange exchange) {
+ String clientId = ProtocolClientEventService.getClientId(exchange);
+
+ if (!isGatewayClientId(clientId)) {
+ return;
+ }
+
+ if (header(ConnectionConstants.SESSION_OPEN).matches(exchange)) {
+ Session session = exchange.getIn().getHeader(ConnectionConstants.SESSION, Session.class);
+ String sessionKey = ProtocolClientEventService.getSessionKey(exchange);
+ processGatewayConnected(clientId, sessionKey, session);
+ return;
+ }
+
+ if (or(header(ConnectionConstants.SESSION_CLOSE), header(ConnectionConstants.SESSION_CLOSE_ERROR)).matches(exchange)) {
+ processGatewayDisconnected(clientId);
+ return;
+ }
+
+ // Inbound shared events
+ if (and(ProtocolClientEventService::isInbound, body().isInstanceOf(SharedEvent.class)).matches(exchange)) {
+ ProtocolClientEventService.stopMessage(exchange);
+ String gatewayId = getGatewayIdFromClientId(clientId);
+ onGatewayClientEventReceived(gatewayId, exchange.getIn().getHeader(HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class), exchange.getIn().getBody(SharedEvent.class));
}
}
@@ -259,9 +244,11 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
if (disabled != isAlreadyDisabled) {
if (disabled) {
connector.sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
+ destroyGatewayClient(asset);
+ } else {
+ createGatewayClient(asset);
}
connector.setDisabled(disabled);
- updateGatewayClient(connector, !disabled);
}
}
} else {
@@ -391,32 +378,31 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
String gatewayId = getGatewayIdFromClientId(gatewayClientId);
GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
- Runnable disconnectRunnable = createDisconnectRunnable(gatewayId, session);
if (connector == null) {
LOG.warning("Gateway connected but not recognised which shouldn't happen: Gateway ID=" + gatewayId);
clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.UNRECOGNISED));
- disconnectRunnable.run();
+ 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));
- disconnectRunnable.run();
+ clientEventService.closeSession(sessionId);
return;
}
if (connector.isDisabled()) {
LOG.warning("Gateway is currently disabled so will be ignored: Gateway ID=" + gatewayId);
- // Should not have got passed keycloak ensure keycloak client is disabled
- updateGatewayClient(connector, false);
+ // Should not have got passed keycloak ensure keycloak client is removed
+ destroyGatewayClient(connector.gateway);
clientEventService.sendToSession(sessionId, new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
- disconnectRunnable.run();
+ clientEventService.closeSession(sessionId);
return;
}
- connector.connect(createConnectorMessageConsumer(sessionId), disconnectRunnable);
+ connector.connect(createConnectorMessageConsumer(sessionId), () -> clientEventService.closeSession(sessionId));
}
protected void processGatewayDisconnected(String gatewayClientId) {
@@ -453,13 +439,35 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
break;
}
+ connector.gateway = gateway;
+
// Check if disabled
boolean isNowDisabled = gateway.getAttribute("disabled").flatMap(AssetAttribute::getValueAsBoolean).orElse(false);
if (isNowDisabled) {
connector.sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
}
connector.setDisabled(isNowDisabled);
- updateGatewayClient(connector, !isNowDisabled);
+
+ int attributeIndex = persistenceEvent.getPropertyNames() != null
+ ? IntStream.range(0, persistenceEvent.getPropertyNames().length)
+ .filter(i -> "attributes".equals(persistenceEvent.getPropertyNames()[i]))
+ .findFirst()
+ .orElse(-1)
+ : -1;
+ if (attributeIndex >= 0) {
+ // Check if disabled attribute has changed
+ boolean wasDisabled = attributeFromJson(persistenceEvent.getPreviousState("attributes"),
+ gateway.getId(), "disabled").flatMap(AbstractValueHolder::getValueAsBoolean).orElse(false);
+ boolean isDisabled = attributeFromJson(persistenceEvent.getCurrentState("attributes"),
+ gateway.getId(), "disabled").flatMap(AbstractValueHolder::getValueAsBoolean).orElse(false);
+ if (wasDisabled != isDisabled) {
+ if (isNowDisabled) {
+ destroyGatewayClient(gateway);
+ } else {
+ createGatewayClient(gateway);
+ }
+ }
+ }
break;
case DELETE:
// Check if this gateway has a connector
@@ -511,25 +519,14 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
}
protected void createGatewayClient(Asset gateway) {
- String id = UUID.randomUUID().toString();
- String secret = gateway.getAttribute("clientSecret").flatMap(ValueHolder::getValueAsString).map(UUID::fromString).orElseGet(UUID::randomUUID).toString();
- String clientId = GATEWAY_CLIENT_ID_PREFIX + gateway.getId();
- ClientRepresentation clientRepresentation = new ClientRepresentation();
- clientRepresentation.setId(id);
- clientRepresentation.setStandardFlowEnabled(false);
- clientRepresentation.setImplicitFlowEnabled(false);
- clientRepresentation.setDirectAccessGrantsEnabled(false);
- clientRepresentation.setServiceAccountsEnabled(true);
- clientRepresentation.setClientAuthenticatorType("client-secret");
- clientRepresentation.setClientId(clientId);
- clientRepresentation.setSecret(secret);
LOG.info("Creating gateway keycloak client for gateway id: " + gateway.getId());
- clientRepresentation = identityProvider.createClient(gateway.getRealm(), clientRepresentation);
- if (clientRepresentation == null) {
- LOG.warning("Failed to create Keycloak client for gateway '" + gateway.getId());
- } else {
+ String clientId = GATEWAY_CLIENT_ID_PREFIX + gateway.getId();
+ String secret = gateway.getAttribute("clientSecret").flatMap(ValueHolder::getValueAsString).orElseGet(() -> UUID.randomUUID().toString());
+
+ try {
+ clientEventService.addClientCredentials(new ClientCredentials(gateway.getRealm(), null, clientId, secret));
gateway.getAttribute("clientId").ifPresent(assetAttribute -> assetAttribute.setValue(Values.create(clientId)));
gateway.getAttribute("clientSecret").ifPresent(assetAttribute -> assetAttribute.setValue(Values.create(secret)));
try {
@@ -538,15 +535,11 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
} catch (Exception e) {
LOG.log(Level.SEVERE, "Failed to merge registered gateway: " + gateway.getId(), e);
}
+ } catch (Exception e) {
+ LOG.warning("Failed to create client for gateway '" + gateway.getId());
}
}
- protected void updateGatewayClient(GatewayConnector connector, boolean enabled) {
- ClientRepresentation client = identityProvider.getClient(connector.getRealm(), GATEWAY_CLIENT_ID_PREFIX + connector.gatewayId);
- client.setEnabled(enabled);
- identityProvider.updateClient(connector.getRealm(), client);
- }
-
protected void destroyGatewayClient(Asset gateway) {
String id = gateway.getAttribute("clientId").flatMap(AssetAttribute::getValueAsString).orElse(null);
if (TextUtil.isNullOrEmpty(id)) {
@@ -554,24 +547,18 @@ public class GatewayService extends RouteBuilder implements ContainerService, As
return;
}
- identityProvider.deleteClient(gateway.getRealm(), id);
+ try {
+ clientEventService.removeClientCredentials(gateway.getRealm(), id);
+ } catch (Exception e) {
+ LOG.warning("Failed to delete client for gateway '" + gateway.getId());
+ }
}
protected Consumer<Object> createConnectorMessageConsumer(String sessionId) {
return msg -> clientEventService.sendToSession(sessionId, msg);
}
- protected Runnable createDisconnectRunnable(String gatewayId, Session session) {
- return () -> {
- try {
- session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, ""));
- } catch (Exception e) {
- LOG.log(Level.INFO, "Failed to close gateway client connection (GATEWAY ID=" + gatewayId + ")");
- }
- };
- }
-
- protected void onGatewayEventReceived(String gatewayId, String messageId, SharedEvent event) {
+ protected void onGatewayClientEventReceived(String gatewayId, String messageId, SharedEvent event) {
GatewayConnector connector = gatewayConnectorMap.get(gatewayId);
if (connector != null) {
connector.onGatewayEvent(messageId, event);
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 3e446d93f..0e3e636af 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -47,7 +47,7 @@ import java.util.logging.Logger;
import static org.openremote.container.util.MapAccess.getInteger;
import static org.openremote.container.util.MapAccess.getString;
-import static org.openremote.manager.event.ClientEventService.getSessionKey;
+import static org.openremote.agent.protocol.ProtocolClientEventService.getSessionKey;
public class MqttBrokerService implements ContainerService {
diff --git a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java
index e746b28d6..61c588d0c 100644
--- a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java
+++ b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java
@@ -37,7 +37,7 @@ import java.util.List;
import java.util.logging.Logger;
import static org.openremote.manager.event.ClientEventService.CLIENT_EVENT_TOPIC;
-import static org.openremote.manager.event.ClientEventService.getSessionKey;
+import static org.openremote.agent.protocol.ProtocolClientEventService.getSessionKey;
/**
* Connects the client/UI to the {@link SimulatorProtocol}.
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
index c140110b2..e8641daf1 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
@@ -13,7 +13,6 @@ import org.openremote.model.asset.AssetAttribute
import org.openremote.model.asset.AssetType
import org.openremote.model.asset.agent.ProtocolConfiguration
import org.openremote.model.attribute.*
-import org.openremote.model.datapoint.Datapoint
import org.openremote.model.value.Value
import org.openremote.model.value.Values
import org.openremote.test.ManagerContainerTrait
@@ -55,12 +54,12 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
LOG.info("Mock Protocol: linkProtocol")
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
LOG.info("Mock Protocol: unlinkProtocol")
}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
index 669af3474..53e77cf21 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy
@@ -90,7 +90,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
}
@Override
- protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doLinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
protocolMethodCalls.add("LINK_PROTOCOL")
protocolLinkedConfigurations.add(protocolConfiguration)
if (!protocolConfiguration.getMetaItem("MOCK_REQUIRED_META").isPresent()) {
@@ -103,7 +103,7 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
}
@Override
- protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {
+ protected void doUnlinkProtocolConfiguration(Asset agent, AssetAttribute protocolConfiguration) {
protocolMethodCalls.add("UNLINK_PROTOCOL")
protocolLinkedConfigurations.removeAll { (it.getReferenceOrThrow() == protocolConfiguration.getReferenceOrThrow())
}
@@ -526,4 +526,4 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait,
cleanup: "the server should be stopped"
stopContainer(container)
}
-}
\ No newline at end of file
+}