diff --git a/application/src/main/resources/thingsboard.yml b/application/src/main/resources/thingsboard.yml
index 1e09461a7f..27d05708d7 100644
--- a/application/src/main/resources/thingsboard.yml
+++ b/application/src/main/resources/thingsboard.yml
@@ -589,6 +589,10 @@ transport:
dtls:
# Enable/disable DTLS 1.2 support
enabled: "${COAP_DTLS_ENABLED:false}"
+ # CoAP DTLS bind address
+ bind_address: "${COAP_DTLS_BIND_ADDRESS:0.0.0.0}"
+ # CoAP DTLS bind port
+ bind_port: "${COAP_DTLS_BIND_PORT:5684}"
# Secure mode. Allowed values: NO_AUTH, X509
mode: "${COAP_DTLS_SECURE_MODE:NO_AUTH}"
# Path to the key store that holds the certificate
diff --git a/common/coap-server/pom.xml b/common/coap-server/pom.xml
new file mode 100644
index 0000000000..8adbe4329a
--- /dev/null
+++ b/common/coap-server/pom.xml
@@ -0,0 +1,73 @@
+
+
+
+ 4.0.0
+
+ org.thingsboard
+ 3.3.0-SNAPSHOT
+ common
+
+ org.thingsboard.common
+ coap-server
+ jar
+
+ Thingsboard CoAP server
+ https://thingsboard.io
+
+
+ UTF-8
+ ${basedir}/../..
+
+
+
+
+ org.thingsboard.common
+ queue
+
+
+ org.thingsboard.common
+ data
+
+
+ org.thingsboard.common.transport
+ transport-api
+
+
+ org.springframework
+ spring-context
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+ provided
+
+
+ org.eclipse.californium
+ californium-core
+
+
+ org.eclipse.californium
+ scandium
+
+
+
+
+
\ No newline at end of file
diff --git a/common/coap-server/src/main/java/org/thingsboard/server/coapserver/CoapServerContext.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/CoapServerContext.java
new file mode 100644
index 0000000000..4129108ba3
--- /dev/null
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/CoapServerContext.java
@@ -0,0 +1,46 @@
+/**
+ * Copyright © 2016-2021 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.coapserver;
+
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
+import org.springframework.stereotype.Component;
+
+@Slf4j
+@ConditionalOnExpression("'${service.type:null}'=='tb-transport' || ('${service.type:null}'=='monolith' && '${transport.api_enabled:true}'=='true' && '${transport.coap.enabled}'=='true')")
+@Component
+public class CoapServerContext {
+
+ @Getter
+ @Value("${transport.coap.bind_address}")
+ private String host;
+
+ @Getter
+ @Value("${transport.coap.bind_port}")
+ private Integer port;
+
+ @Getter
+ @Value("${transport.coap.timeout}")
+ private Long timeout;
+
+ @Getter
+ @Autowired(required = false)
+ private TbCoapDtlsSettings dtlsSettings;
+
+}
diff --git a/common/coap-server/src/main/java/org/thingsboard/server/coapserver/CoapServerService.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/CoapServerService.java
new file mode 100644
index 0000000000..f8b3ffefc2
--- /dev/null
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/CoapServerService.java
@@ -0,0 +1,31 @@
+/**
+ * Copyright © 2016-2021 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.coapserver;
+
+import org.eclipse.californium.core.CoapServer;
+
+import java.net.UnknownHostException;
+import java.util.concurrent.ConcurrentMap;
+
+public interface CoapServerService {
+
+ CoapServer getCoapServer() throws UnknownHostException;
+
+ ConcurrentMap getDtlsSessionsMap();
+
+ long getTimeout();
+
+}
diff --git a/common/coap-server/src/main/java/org/thingsboard/server/coapserver/DefaultCoapServerService.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/DefaultCoapServerService.java
new file mode 100644
index 0000000000..aebac1a86d
--- /dev/null
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/DefaultCoapServerService.java
@@ -0,0 +1,133 @@
+/**
+ * Copyright © 2016-2021 The Thingsboard Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.thingsboard.server.coapserver;
+
+import lombok.extern.slf4j.Slf4j;
+import org.eclipse.californium.core.CoapServer;
+import org.eclipse.californium.core.network.CoapEndpoint;
+import org.eclipse.californium.core.network.config.NetworkConfig;
+import org.eclipse.californium.core.server.resources.Resource;
+import org.eclipse.californium.scandium.DTLSConnector;
+import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
+import java.util.Random;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@Slf4j
+@Component
+@ConditionalOnExpression("'${service.type:null}'=='tb-transport' || ('${service.type:null}'=='monolith' && '${transport.api_enabled:true}'=='true' && '${transport.coap.enabled}'=='true')")
+public class DefaultCoapServerService implements CoapServerService {
+
+ @Autowired
+ private CoapServerContext coapServerContext;
+
+ private CoapServer server;
+
+ private TbCoapDtlsCertificateVerifier tbDtlsCertificateVerifier;
+
+ private ScheduledExecutorService dtlsSessionsExecutor;
+
+ @PostConstruct
+ public void init() throws UnknownHostException {
+ createCoapServer();
+ }
+
+ @PreDestroy
+ public void shutdown() {
+ if (dtlsSessionsExecutor != null) {
+ dtlsSessionsExecutor.shutdownNow();
+ }
+ log.info("Stopping CoAP server!");
+ server.destroy();
+ log.info("CoAP server stopped!");
+ }
+
+ @Override
+ public CoapServer getCoapServer() throws UnknownHostException {
+ if (server != null) {
+ return server;
+ } else {
+ return createCoapServer();
+ }
+ }
+
+ @Override
+ public ConcurrentMap getDtlsSessionsMap() {
+ return tbDtlsCertificateVerifier != null ? tbDtlsCertificateVerifier.getTbCoapDtlsSessionIdsMap() : null;
+ }
+
+ @Override
+ public long getTimeout() {
+ return coapServerContext.getTimeout();
+ }
+
+ private CoapServer createCoapServer() throws UnknownHostException {
+ server = new CoapServer();
+
+ CoapEndpoint.Builder noSecCoapEndpointBuilder = new CoapEndpoint.Builder();
+ InetAddress addr = InetAddress.getByName(coapServerContext.getHost());
+ InetSocketAddress sockAddr = new InetSocketAddress(addr, coapServerContext.getPort());
+ noSecCoapEndpointBuilder.setInetSocketAddress(sockAddr);
+ noSecCoapEndpointBuilder.setNetworkConfig(NetworkConfig.getStandard());
+ CoapEndpoint noSecCoapEndpoint = noSecCoapEndpointBuilder.build();
+ server.addEndpoint(noSecCoapEndpoint);
+
+ if (isDtlsEnabled()) {
+ CoapEndpoint.Builder dtlsCoapEndpointBuilder = new CoapEndpoint.Builder();
+ TbCoapDtlsSettings dtlsSettings = coapServerContext.getDtlsSettings();
+ DtlsConnectorConfig dtlsConnectorConfig = dtlsSettings.dtlsConnectorConfig();
+ DTLSConnector connector = new DTLSConnector(dtlsConnectorConfig);
+ dtlsCoapEndpointBuilder.setConnector(connector);
+ CoapEndpoint dtlsCoapEndpoint = dtlsCoapEndpointBuilder.build();
+ server.addEndpoint(dtlsCoapEndpoint);
+ if (dtlsConnectorConfig.isClientAuthenticationRequired()) {
+ tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier();
+ dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor();
+ dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS);
+ }
+ }
+ Resource root = server.getRoot();
+ TbCoapServerMessageDeliverer messageDeliverer = new TbCoapServerMessageDeliverer(root);
+ server.setMessageDeliverer(messageDeliverer);
+
+ server.start();
+ return server;
+ }
+
+ private boolean isDtlsEnabled() {
+ return coapServerContext.getDtlsSettings() != null;
+ }
+
+ private void evictTimeoutSessions() {
+ tbDtlsCertificateVerifier.evictTimeoutSessions();
+ }
+
+ private long getDtlsSessionReportTimeout() {
+ return tbDtlsCertificateVerifier.getDtlsSessionReportTimeout();
+ }
+
+}
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsCertificateVerifier.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsCertificateVerifier.java
similarity index 99%
rename from common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsCertificateVerifier.java
rename to common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsCertificateVerifier.java
index a94ed6caeb..99a35d4831 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsCertificateVerifier.java
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsCertificateVerifier.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.thingsboard.server.transport.coap;
+package org.thingsboard.server.coapserver;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSessionInMemoryStorage.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSessionInMemoryStorage.java
similarity index 97%
rename from common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSessionInMemoryStorage.java
rename to common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSessionInMemoryStorage.java
index d7dd9c1829..618a10a1eb 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSessionInMemoryStorage.java
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSessionInMemoryStorage.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.thingsboard.server.transport.coap;
+package org.thingsboard.server.coapserver;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSessionInfo.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSessionInfo.java
similarity index 96%
rename from common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSessionInfo.java
rename to common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSessionInfo.java
index 452c5eb792..893ca38a5c 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSessionInfo.java
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSessionInfo.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.thingsboard.server.transport.coap;
+package org.thingsboard.server.coapserver;
import lombok.Data;
import org.thingsboard.server.common.data.DeviceProfile;
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSettings.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSettings.java
similarity index 96%
rename from common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSettings.java
rename to common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSettings.java
index d7cd0b3f39..417a78da12 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapDtlsSettings.java
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapDtlsSettings.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.thingsboard.server.transport.coap;
+package org.thingsboard.server.coapserver;
import com.google.common.io.Resources;
import lombok.extern.slf4j.Slf4j;
@@ -39,15 +39,15 @@ import java.util.Collections;
import java.util.Optional;
@Slf4j
+@ConditionalOnExpression("'${transport.coap.enabled}'=='true'")
@ConditionalOnProperty(prefix = "transport.coap.dtls", value = "enabled", havingValue = "true", matchIfMissing = false)
-@ConditionalOnExpression("'${transport.type:null}'=='null' || ('${transport.type}'=='local' && '${transport.coap.enabled}'=='true')")
@Component
public class TbCoapDtlsSettings {
- @Value("${transport.coap.bind_address}")
+ @Value("${transport.coap.dtls.bind_address}")
private String host;
- @Value("${transport.coap.bind_port}")
+ @Value("${transport.coap.dtls.bind_port}")
private Integer port;
@Value("${transport.coap.dtls.mode}")
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapServerMessageDeliverer.java b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapServerMessageDeliverer.java
similarity index 97%
rename from common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapServerMessageDeliverer.java
rename to common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapServerMessageDeliverer.java
index fa189e35f1..307455a6a5 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/TbCoapServerMessageDeliverer.java
+++ b/common/coap-server/src/main/java/org/thingsboard/server/coapserver/TbCoapServerMessageDeliverer.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.thingsboard.server.transport.coap;
+package org.thingsboard.server.coapserver;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.californium.core.coap.OptionSet;
diff --git a/common/pom.xml b/common/pom.xml
index 7568d18ca6..84e268bbc8 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -43,6 +43,7 @@
dao-api
stats
cache
+ coap-server
diff --git a/common/transport/coap/pom.xml b/common/transport/coap/pom.xml
index bd4ea45d8b..2fefb8f300 100644
--- a/common/transport/coap/pom.xml
+++ b/common/transport/coap/pom.xml
@@ -40,6 +40,10 @@
org.thingsboard.common.transport
transport-api
+
+ org.thingsboard.common
+ coap-server
+
org.eclipse.californium
californium-core
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportContext.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportContext.java
index 73bb318c70..9133809225 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportContext.java
+++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportContext.java
@@ -18,13 +18,12 @@ package org.thingsboard.server.transport.coap;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;
import org.thingsboard.server.common.transport.TransportContext;
-import org.thingsboard.server.transport.coap.efento.adaptor.EfentoCoapAdaptor;
import org.thingsboard.server.transport.coap.adaptors.JsonCoapAdaptor;
import org.thingsboard.server.transport.coap.adaptors.ProtoCoapAdaptor;
+import org.thingsboard.server.transport.coap.efento.adaptor.EfentoCoapAdaptor;
/**
@@ -35,22 +34,6 @@ import org.thingsboard.server.transport.coap.adaptors.ProtoCoapAdaptor;
@Component
public class CoapTransportContext extends TransportContext {
- @Getter
- @Value("${transport.coap.bind_address}")
- private String host;
-
- @Getter
- @Value("${transport.coap.bind_port}")
- private Integer port;
-
- @Getter
- @Value("${transport.coap.timeout}")
- private Long timeout;
-
- @Getter
- @Autowired(required = false)
- private TbCoapDtlsSettings dtlsSettings;
-
@Getter
@Autowired
private JsonCoapAdaptor jsonCoapAdaptor;
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java
index 074be3ae47..8d756631e8 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java
+++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java
@@ -28,6 +28,8 @@ import org.eclipse.californium.core.server.resources.CoapExchange;
import org.eclipse.californium.core.server.resources.Resource;
import org.eclipse.californium.core.server.resources.ResourceObserver;
import org.springframework.util.StringUtils;
+import org.thingsboard.server.coapserver.CoapServerService;
+import org.thingsboard.server.coapserver.TbCoapDtlsSessionInfo;
import org.thingsboard.server.common.data.DataConstants;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.DeviceTransportType;
@@ -74,12 +76,14 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
private final Set attributeSubscriptions = ConcurrentHashMap.newKeySet();
private ConcurrentMap dtlsSessionIdMap;
+ private long timeout;
- public CoapTransportResource(CoapTransportContext coapTransportContext, ConcurrentMap dtlsSessionIdMap, String name) {
+ public CoapTransportResource(CoapTransportContext coapTransportContext, CoapServerService coapServerService, String name) {
super(coapTransportContext, name);
this.setObservable(true); // enable observing
this.addObserver(new CoapResourceObserver());
- this.dtlsSessionIdMap = dtlsSessionIdMap;
+ this.dtlsSessionIdMap = coapServerService.getDtlsSessionsMap();
+ this.timeout = coapServerService.getTimeout();
// this.setObservable(false); // disable observing
// this.setObserveType(CoAP.Type.CON); // configure the notification type to CONs
// this.getAttributes().setObservable(); // mark observable in the Link-Format
@@ -303,13 +307,13 @@ public class CoapTransportResource extends AbstractCoapTransportResource {
new CoapOkCallback(exchange, CoAP.ResponseCode.CREATED, CoAP.ResponseCode.INTERNAL_SERVER_ERROR));
break;
case TO_SERVER_RPC_REQUEST:
- transportService.registerSyncSession(sessionInfo, getCoapSessionListener(exchange, coapTransportAdaptor), transportContext.getTimeout());
+ transportService.registerSyncSession(sessionInfo, getCoapSessionListener(exchange, coapTransportAdaptor), timeout);
transportService.process(sessionInfo,
coapTransportAdaptor.convertToServerRpcRequest(sessionId, request),
new CoapNoOpCallback(exchange));
break;
case GET_ATTRIBUTES_REQUEST:
- transportService.registerSyncSession(sessionInfo, getCoapSessionListener(exchange, coapTransportAdaptor), transportContext.getTimeout());
+ transportService.registerSyncSession(sessionInfo, getCoapSessionListener(exchange, coapTransportAdaptor), timeout);
transportService.process(sessionInfo,
coapTransportAdaptor.convertToGetAttributes(sessionId, request),
new CoapNoOpCallback(exchange));
diff --git a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportService.java b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportService.java
index a28019c6c0..a165cde74b 100644
--- a/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportService.java
+++ b/common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportService.java
@@ -18,26 +18,15 @@ package org.thingsboard.server.transport.coap;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
-import org.eclipse.californium.core.network.CoapEndpoint;
-import org.eclipse.californium.core.network.config.NetworkConfig;
-import org.eclipse.californium.core.server.resources.Resource;
-import org.eclipse.californium.scandium.DTLSConnector;
-import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;
+import org.thingsboard.server.coapserver.CoapServerService;
import org.thingsboard.server.transport.coap.efento.CoapEfentoTransportResource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
import java.net.UnknownHostException;
-import java.util.Random;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
@Service("CoapTransportService")
@ConditionalOnExpression("'${service.type:null}'=='tb-transport' || ('${service.type:null}'=='monolith' && '${transport.api_enabled:true}'=='true' && '${transport.coap.enabled}'=='true')")
@@ -50,87 +39,30 @@ public class CoapTransportService {
private static final String MEASUREMENTS = "m";
@Autowired
- private CoapTransportContext coapTransportContext;
-
- private TbCoapDtlsCertificateVerifier tbDtlsCertificateVerifier;
+ private CoapServerService coapServerService;
- private CoapServer server;
+ @Autowired
+ private CoapTransportContext coapTransportContext;
- private ScheduledExecutorService dtlsSessionsExecutor;
+ private CoapServer coapServer;
@PostConstruct
public void init() throws UnknownHostException {
log.info("Starting CoAP transport...");
- log.info("Starting CoAP transport server");
-
- this.server = new CoapServer();
-
- CoapEndpoint.Builder capEndpointBuilder = new CoapEndpoint.Builder();
-
- if (isDtlsEnabled()) {
- TbCoapDtlsSettings dtlsSettings = coapTransportContext.getDtlsSettings();
- DtlsConnectorConfig dtlsConnectorConfig = dtlsSettings.dtlsConnectorConfig();
- DTLSConnector connector = new DTLSConnector(dtlsConnectorConfig);
- capEndpointBuilder.setConnector(connector);
- if (dtlsConnectorConfig.isClientAuthenticationRequired()) {
- tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier();
- dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor();
- dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS);
- }
- } else {
- InetAddress addr = InetAddress.getByName(coapTransportContext.getHost());
- InetSocketAddress sockAddr = new InetSocketAddress(addr, coapTransportContext.getPort());
- capEndpointBuilder.setInetSocketAddress(sockAddr);
- capEndpointBuilder.setNetworkConfig(NetworkConfig.getStandard());
- }
- CoapEndpoint coapEndpoint = capEndpointBuilder.build();
-
- server.addEndpoint(coapEndpoint);
-
- createResources();
- Resource root = this.server.getRoot();
- TbCoapServerMessageDeliverer messageDeliverer = new TbCoapServerMessageDeliverer(root);
- this.server.setMessageDeliverer(messageDeliverer);
-
- server.start();
- log.info("CoAP transport started!");
- }
-
- private void createResources() {
+ coapServer = coapServerService.getCoapServer();
CoapResource api = new CoapResource(API);
- api.add(new CoapTransportResource(coapTransportContext, getDtlsSessionsMap(), V1));
+ api.add(new CoapTransportResource(coapTransportContext, coapServerService, V1));
CoapResource efento = new CoapResource(EFENTO);
CoapEfentoTransportResource efentoMeasurementsTransportResource = new CoapEfentoTransportResource(coapTransportContext, MEASUREMENTS);
efento.add(efentoMeasurementsTransportResource);
-
- server.add(api);
- server.add(efento);
- }
-
- private boolean isDtlsEnabled() {
- return coapTransportContext.getDtlsSettings() != null;
- }
-
- private ConcurrentMap getDtlsSessionsMap() {
- return tbDtlsCertificateVerifier != null ? tbDtlsCertificateVerifier.getTbCoapDtlsSessionIdsMap() : null;
- }
-
- private void evictTimeoutSessions() {
- tbDtlsCertificateVerifier.evictTimeoutSessions();
- }
-
- private long getDtlsSessionReportTimeout() {
- return tbDtlsCertificateVerifier.getDtlsSessionReportTimeout();
+ coapServer.add(api);
+ coapServer.add(efento);
+ log.info("CoAP transport started!");
}
@PreDestroy
public void shutdown() {
- if (dtlsSessionsExecutor != null) {
- dtlsSessionsExecutor.shutdownNow();
- }
- log.info("Stopping CoAP transport!");
- this.server.destroy();
log.info("CoAP transport stopped!");
}
}
diff --git a/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttSslHandlerProvider.java b/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttSslHandlerProvider.java
index 1c0801c64a..64efb5f036 100644
--- a/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttSslHandlerProvider.java
+++ b/common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttSslHandlerProvider.java
@@ -55,7 +55,7 @@ import java.util.concurrent.TimeUnit;
*/
@Slf4j
@Component("MqttSslHandlerProvider")
-@ConditionalOnExpression("'${transport.type:null}'=='null' || ('${transport.type}'=='local' && '${transport.mqtt.enabled}'=='true')")
+@ConditionalOnExpression("'${transport.mqtt.enabled}'=='true'")
@ConditionalOnProperty(prefix = "transport.mqtt.ssl", value = "enabled", havingValue = "true", matchIfMissing = false)
public class MqttSslHandlerProvider {
diff --git a/pom.xml b/pom.xml
index d635859b8a..5d03107626 100755
--- a/pom.xml
+++ b/pom.xml
@@ -933,6 +933,11 @@
stats
${project.version}
+
+ org.thingsboard.common
+ coap-server
+ ${project.version}
+
org.thingsboard
tools
diff --git a/transport/coap/src/main/java/org/thingsboard/server/coap/ThingsboardCoapTransportApplication.java b/transport/coap/src/main/java/org/thingsboard/server/coap/ThingsboardCoapTransportApplication.java
index 7afafcfb73..010a774e0e 100644
--- a/transport/coap/src/main/java/org/thingsboard/server/coap/ThingsboardCoapTransportApplication.java
+++ b/transport/coap/src/main/java/org/thingsboard/server/coap/ThingsboardCoapTransportApplication.java
@@ -26,7 +26,7 @@ import java.util.Arrays;
@SpringBootConfiguration
@EnableAsync
@EnableScheduling
-@ComponentScan({"org.thingsboard.server.coap", "org.thingsboard.server.common", "org.thingsboard.server.transport.coap", "org.thingsboard.server.queue"})
+@ComponentScan({"org.thingsboard.server.coap", "org.thingsboard.server.common", "org.thingsboard.server.coapserver", "org.thingsboard.server.transport.coap", "org.thingsboard.server.queue"})
public class ThingsboardCoapTransportApplication {
private static final String SPRING_CONFIG_NAME_KEY = "--spring.config.name";
diff --git a/transport/coap/src/main/resources/tb-coap-transport.yml b/transport/coap/src/main/resources/tb-coap-transport.yml
index 0ffcd27f21..4d5bbf2c6f 100644
--- a/transport/coap/src/main/resources/tb-coap-transport.yml
+++ b/transport/coap/src/main/resources/tb-coap-transport.yml
@@ -49,6 +49,10 @@ transport:
dtls:
# Enable/disable DTLS 1.2 support
enabled: "${COAP_DTLS_ENABLED:false}"
+ # CoAP DTLS bind address
+ bind_address: "${COAP_DTLS_BIND_ADDRESS:0.0.0.0}"
+ # CoAP DTLS bind port
+ bind_port: "${COAP_DTLS_BIND_PORT:5684}"
# Secure mode. Allowed values: NO_AUTH, X509
mode: "${COAP_DTLS_SECURE_MODE:NO_AUTH}"
# Path to the key store that holds the certificate