Commit dd5195e8d741a427c44da49f308f7f48a67625c8

Authored by Andrii Shvaika
1 parent 8a4c0a71

Refactoring of LwM2MTransport

Showing 29 changed files with 225 additions and 384 deletions
... ... @@ -15,15 +15,14 @@
15 15 */
16 16 package org.thingsboard.server.transport.lwm2m.bootstrap;
17 17
  18 +import lombok.RequiredArgsConstructor;
18 19 import lombok.extern.slf4j.Slf4j;
19 20 import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
20 21 import org.eclipse.leshan.core.util.Hex;
21 22 import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager;
22 23 import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServer;
23 24 import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServerBuilder;
24   -import org.springframework.beans.factory.annotation.Autowired;
25 25 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
26   -import org.springframework.context.annotation.Bean;
27 26 import org.springframework.stereotype.Component;
28 27 import org.thingsboard.server.common.data.StringUtils;
29 28 import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapSecurityStore;
... ... @@ -31,8 +30,10 @@ import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MInMemoryBoot
31 30 import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2mDefaultBootstrapSessionManager;
32 31 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig;
33 32 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig;
34   -import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper;
  33 +import org.thingsboard.server.transport.lwm2m.secure.LWM2MGenerationPSkRPkECC;
35 34
  35 +import javax.annotation.PostConstruct;
  36 +import javax.annotation.PreDestroy;
36 37 import java.math.BigInteger;
37 38 import java.security.AlgorithmParameters;
38 39 import java.security.KeyFactory;
... ... @@ -62,38 +63,45 @@ import static org.thingsboard.server.transport.lwm2m.server.LwM2mNetworkConfig.g
62 63
63 64 @Slf4j
64 65 @Component
65   -@ConditionalOnExpression("('${service.type:null}'=='tb-transport' && '${transport.lwm2m.enabled:false}'=='true'&& '${transport.lwm2m.bootstrap.enable:false}'=='true') || ('${service.type:null}'=='monolith' && '${transport.lwm2m.enabled:false}'=='true'&& '${transport.lwm2m.bootstrap.enable:false}'=='true')")
  66 +@ConditionalOnExpression("('${service.type:null}'=='tb-transport' && '${transport.lwm2m.enabled:false}'=='true' && '${transport.lwm2m.bootstrap.enable:false}'=='true') || ('${service.type:null}'=='monolith' && '${transport.lwm2m.enabled:false}'=='true'&& '${transport.lwm2m.bootstrap.enable:false}'=='true')")
  67 +@RequiredArgsConstructor
66 68 public class LwM2MTransportBootstrapServerConfiguration {
67 69 private PublicKey publicKey;
68 70 private PrivateKey privateKey;
69 71 private boolean pskMode = false;
70 72
71   - @Autowired
72   - private LwM2MTransportServerConfig serverConfig;
  73 + private final LwM2MTransportServerConfig serverConfig;
  74 + private final LwM2MTransportBootstrapConfig bootstrapConfig;
  75 + private final LwM2MBootstrapSecurityStore lwM2MBootstrapSecurityStore;
  76 + private final LwM2MInMemoryBootstrapConfigStore lwM2MInMemoryBootstrapConfigStore;
73 77
74   - @Autowired
75   - private LwM2MTransportContextBootstrap contextBs;
76   -
77   - @Autowired
78   - private LwM2MBootstrapSecurityStore lwM2MBootstrapSecurityStore;
79   -
80   - @Autowired
81   - private LwM2MInMemoryBootstrapConfigStore lwM2MInMemoryBootstrapConfigStore;
  78 + private LeshanBootstrapServer server;
82 79
  80 + @PostConstruct
  81 + public void init() {
  82 + if (serverConfig.getEnableGenNewKeyPskRpk()) {
  83 + new LWM2MGenerationPSkRPkECC();
  84 + }
  85 + log.info("Starting LwM2M transport bootstrap server...");
  86 + this.server = getLhBootstrapServer();
  87 + this.server.start();
  88 + log.info("Started LwM2M transport bootstrap server.");
  89 + }
83 90
84   - @Bean
85   - public LeshanBootstrapServer getLeshanBootstrapServer() {
86   - log.info("Prepare and start BootstrapServer... PostConstruct");
87   - return this.getLhBootstrapServer(this.contextBs.getCtxBootStrap().getPort(), this.contextBs.getCtxBootStrap().getSecurePort());
  91 + @PreDestroy
  92 + public void shutdown() {
  93 + log.info("Stopping LwM2M transport bootstrap server!");
  94 + server.destroy();
  95 + log.info("LwM2M transport bootstrap server stopped!");
88 96 }
89 97
90   - public LeshanBootstrapServer getLhBootstrapServer(Integer bootstrapPortNoSec, Integer bootstrapSecurePort) {
  98 + public LeshanBootstrapServer getLhBootstrapServer() {
91 99 LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
92   - builder.setLocalAddress(this.contextBs.getCtxBootStrap().getHost(), bootstrapPortNoSec);
93   - builder.setLocalSecureAddress(this.contextBs.getCtxBootStrap().getSecureHost(), bootstrapSecurePort);
  100 + builder.setLocalAddress(bootstrapConfig.getHost(), bootstrapConfig.getPort());
  101 + builder.setLocalSecureAddress(bootstrapConfig.getSecureHost(), bootstrapConfig.getSecurePort());
94 102
95 103 /** Create CoAP Config */
96   - builder.setCoapConfig(getCoapConfig(bootstrapPortNoSec, bootstrapSecurePort));
  104 + builder.setCoapConfig(getCoapConfig(bootstrapConfig.getPort(), bootstrapConfig.getSecurePort()));
97 105
98 106 /** Define model provider (Create Models )*/
99 107
... ... @@ -169,8 +177,8 @@ public class LwM2MTransportBootstrapServerConfiguration {
169 177 * For idea => KeyStorePathResource == common/transport/lwm2m/src/main/resources/credentials: in LwM2MTransportContextServer: credentials/serverKeyStore.jks
170 178 */
171 179 try {
172   - X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(this.contextBs.getCtxBootStrap().getCertificateAlias());
173   - PrivateKey privateKey = (PrivateKey) serverConfig.getKeyStoreValue().getKey(this.contextBs.getCtxBootStrap().getCertificateAlias(), serverConfig.getKeyStorePassword() == null ? null : serverConfig.getKeyStorePassword().toCharArray());
  180 + X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(this.bootstrapConfig.getCertificateAlias());
  181 + PrivateKey privateKey = (PrivateKey) serverConfig.getKeyStoreValue().getKey(this.bootstrapConfig.getCertificateAlias(), serverConfig.getKeyStorePassword() == null ? null : serverConfig.getKeyStorePassword().toCharArray());
174 182 PublicKey publicKey = serverCertificate.getPublicKey();
175 183 if (privateKey != null && privateKey.getEncoded().length > 0 && publicKey != null && publicKey.getEncoded().length > 0) {
176 184 builder.setPublicKey(serverCertificate.getPublicKey());
... ... @@ -201,10 +209,10 @@ public class LwM2MTransportBootstrapServerConfiguration {
201 209 private void infoPramsUri(String mode) {
202 210 log.info("Bootstrap Server uses [{}]: serverNoSecureURI : [{}:{}], serverSecureURI : [{}:{}]",
203 211 mode,
204   - this.contextBs.getCtxBootStrap().getHost(),
205   - this.contextBs.getCtxBootStrap().getPort(),
206   - this.contextBs.getCtxBootStrap().getSecureHost(),
207   - this.contextBs.getCtxBootStrap().getSecurePort());
  212 + this.bootstrapConfig.getHost(),
  213 + this.bootstrapConfig.getPort(),
  214 + this.bootstrapConfig.getSecureHost(),
  215 + this.bootstrapConfig.getSecurePort());
208 216 }
209 217
210 218
... ... @@ -238,7 +246,7 @@ public class LwM2MTransportBootstrapServerConfiguration {
238 246 AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
239 247 algoParameters.init(new ECGenParameterSpec("secp256r1"));
240 248 ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
241   - LwM2MTransportBootstrapConfig serverConfig = this.contextBs.getCtxBootStrap();
  249 + LwM2MTransportBootstrapConfig serverConfig = this.bootstrapConfig;
242 250 if (StringUtils.isNotEmpty(serverConfig.getPublicX()) && StringUtils.isNotEmpty(serverConfig.getPublicY())) {
243 251 /** Get point values */
244 252 byte[] publicX = Hex.decodeHex(serverConfig.getPublicX().toCharArray());
... ...
1   -/**
2   - * Copyright © 2016-2021 The Thingsboard Authors
3   - *
4   - * Licensed under the Apache License, Version 2.0 (the "License");
5   - * you may not use this file except in compliance with the License.
6   - * You may obtain a copy of the License at
7   - *
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - *
10   - * Unless required by applicable law or agreed to in writing, software
11   - * distributed under the License is distributed on an "AS IS" BASIS,
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   - * See the License for the specific language governing permissions and
14   - * limitations under the License.
15   - */
16   -package org.thingsboard.server.transport.lwm2m.bootstrap;
17   -
18   -import lombok.extern.slf4j.Slf4j;
19   -import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServer;
20   -import org.springframework.beans.factory.annotation.Autowired;
21   -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
22   -import org.springframework.stereotype.Service;
23   -
24   -import javax.annotation.PostConstruct;
25   -import javax.annotation.PreDestroy;
26   -
27   -@Slf4j
28   -@Service
29   -@ConditionalOnExpression("('${service.type:null}'=='tb-transport' && '${transport.lwm2m.enabled:false}'=='true'&& '${transport.lwm2m.bootstrap.enable:false}'=='true') || ('${service.type:null}'=='monolith' && '${transport.lwm2m.enabled:false}'=='true'&& '${transport.lwm2m.bootstrap.enable:false}'=='true')")
30   -public class LwM2MTransportBootstrapServerInitializer {
31   -
32   - @Autowired(required = false)
33   - private LeshanBootstrapServer lhBServer;
34   -
35   - @Autowired
36   - private LwM2MTransportContextBootstrap contextBS;
37   -
38   - @PostConstruct
39   - public void init() {
40   - this.lhBServer.start();
41   - }
42   -
43   - @PreDestroy
44   - public void shutdown() throws InterruptedException {
45   - log.info("Stopping LwM2M transport Bootstrap Server!");
46   - lhBServer.destroy();
47   - log.info("LwM2M transport Bootstrap Server stopped!");
48   - }
49   -}
1   -/**
2   - * Copyright © 2016-2021 The Thingsboard Authors
3   - *
4   - * Licensed under the Apache License, Version 2.0 (the "License");
5   - * you may not use this file except in compliance with the License.
6   - * You may obtain a copy of the License at
7   - *
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - *
10   - * Unless required by applicable law or agreed to in writing, software
11   - * distributed under the License is distributed on an "AS IS" BASIS,
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   - * See the License for the specific language governing permissions and
14   - * limitations under the License.
15   - */
16   -package org.thingsboard.server.transport.lwm2m.bootstrap;
17   -/**
18   - * Copyright © 2016-2020 The Thingsboard Authors
19   - *
20   - * Licensed under the Apache License, Version 2.0 (the "License");
21   - * you may not use this file except in compliance with the License.
22   - * You may obtain a copy of the License at
23   - *
24   - * http://www.apache.org/licenses/LICENSE-2.0
25   - *
26   - * Unless required by applicable law or agreed to in writing, software
27   - * distributed under the License is distributed on an "AS IS" BASIS,
28   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   - * See the License for the specific language governing permissions and
30   - * limitations under the License.
31   - */
32   -
33   -import lombok.extern.slf4j.Slf4j;
34   -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
35   -import org.springframework.stereotype.Component;
36   -import org.thingsboard.server.common.transport.TransportContext;
37   -import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig;
38   -
39   -
40   -@Slf4j
41   -@Component
42   -@ConditionalOnExpression("('${service.type:null}'=='tb-transport' && '${transport.lwm2m.enabled:false}'=='true') || '${service.type:null}'=='monolith'")
43   -public class LwM2MTransportContextBootstrap extends TransportContext {
44   -
45   - private final LwM2MTransportBootstrapConfig lwM2MTransportBootstrapConfig;
46   -
47   - public LwM2MTransportContextBootstrap(LwM2MTransportBootstrapConfig ctxBootStrap) {
48   - this.lwM2MTransportBootstrapConfig = ctxBootStrap;
49   - }
50   -
51   - public LwM2MTransportBootstrapConfig getCtxBootStrap() {
52   - return this.lwM2MTransportBootstrapConfig;
53   - }
54   -}
... ... @@ -25,12 +25,12 @@ import java.nio.charset.StandardCharsets;
25 25
26 26 @Data
27 27 public class LwM2MBootstrapConfig {
28   - /**
29   - * interface BootstrapSecurityConfig
30   - * servers: BootstrapServersSecurityConfig,
31   - * bootstrapServer: ServerSecurityConfig,
32   - * lwm2mServer: ServerSecurityConfig
33   - * }
  28 + /*
  29 + interface BootstrapSecurityConfig
  30 + servers: BootstrapServersSecurityConfig,
  31 + bootstrapServer: ServerSecurityConfig,
  32 + lwm2mServer: ServerSecurityConfig
  33 + }
34 34 */
35 35 /** -servers
36 36 * shortId: number,
... ... @@ -60,10 +60,10 @@ public class LwM2MBootstrapConfig {
60 60
61 61 public BootstrapConfig getLwM2MBootstrapConfig() {
62 62 BootstrapConfig configBs = new BootstrapConfig();
63   - /** Delete old security objects */
  63 + /* Delete old security objects */
64 64 configBs.toDelete.add("/0");
65 65 configBs.toDelete.add("/1");
66   - /** Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */
  66 + /* Server Configuration (object 1) as defined in LWM2M 1.0.x TS. */
67 67 BootstrapConfig.ServerConfig server0 = new BootstrapConfig.ServerConfig();
68 68 server0.shortId = servers.getShortId();
69 69 server0.lifetime = servers.getLifetime();
... ... @@ -71,10 +71,10 @@ public class LwM2MBootstrapConfig {
71 71 server0.notifIfDisabled = servers.isNotifIfDisabled();
72 72 server0.binding = BindingMode.valueOf(servers.getBinding());
73 73 configBs.servers.put(0, server0);
74   - /** Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Bootstrap instance = 0 */
  74 + /* Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Bootstrap instance = 0 */
75 75 this.bootstrapServer.setBootstrapServerIs(true);
76 76 configBs.security.put(0, setServerSecuruty(this.bootstrapServer.getHost(), this.bootstrapServer.getPort(), this.bootstrapServer.isBootstrapServerIs(), this.bootstrapServer.getSecurityMode(), this.bootstrapServer.getClientPublicKeyOrId(), this.bootstrapServer.getServerPublicKey(), this.bootstrapServer.getClientSecretKey(), this.bootstrapServer.getServerId()));
77   - /** Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Server instance = 1 */
  77 + /* Security Configuration (object 0) as defined in LWM2M 1.0.x TS. Server instance = 1 */
78 78 configBs.security.put(1, setServerSecuruty(this.lwm2mServer.getHost(), this.lwm2mServer.getPort(), this.lwm2mServer.isBootstrapServerIs(), this.lwm2mServer.getSecurityMode(), this.lwm2mServer.getClientPublicKeyOrId(), this.lwm2mServer.getServerPublicKey(), this.lwm2mServer.getClientSecretKey(), this.lwm2mServer.getServerId()));
79 79 return configBs;
80 80 }
... ... @@ -92,9 +92,8 @@ public class LwM2MBootstrapConfig {
92 92 }
93 93
94 94 private byte[] setPublicKeyOrId(String publicKeyOrIdStr, String securityMode) {
95   - byte[] publicKey = (publicKeyOrIdStr == null || publicKeyOrIdStr.isEmpty()) ? new byte[]{} :
  95 + return (publicKeyOrIdStr == null || publicKeyOrIdStr.isEmpty()) ? new byte[]{} :
96 96 SecurityMode.valueOf(securityMode).equals(SecurityMode.PSK) ? publicKeyOrIdStr.getBytes(StandardCharsets.UTF_8) :
97 97 Hex.decodeHex(publicKeyOrIdStr.toCharArray());
98   - return publicKey;
99 98 }
100 99 }
... ...
... ... @@ -36,7 +36,7 @@ import org.thingsboard.server.transport.lwm2m.secure.ReadResultSecurityStore;
36 36 import org.thingsboard.server.transport.lwm2m.server.LwM2mSessionMsgListener;
37 37 import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext;
38 38 import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper;
39   -import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil;
  39 +import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil;
40 40
41 41 import java.io.IOException;
42 42 import java.security.GeneralSecurityException;
... ... @@ -44,12 +44,12 @@ import java.util.Collections;
44 44 import java.util.List;
45 45 import java.util.UUID;
46 46
47   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.BOOTSTRAP_SERVER;
48   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_ERROR;
49   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_INFO;
50   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LWM2M_SERVER;
51   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.SERVERS;
52   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.getBootstrapParametersFromThingsboard;
  47 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.BOOTSTRAP_SERVER;
  48 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_ERROR;
  49 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_INFO;
  50 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LWM2M_SERVER;
  51 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.SERVERS;
  52 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.getBootstrapParametersFromThingsboard;
53 53
54 54 @Slf4j
55 55 @Service("LwM2MBootstrapSecurityStore")
... ... @@ -72,9 +72,9 @@ public class LwM2MBootstrapSecurityStore implements BootstrapSecurityStore {
72 72
73 73 @Override
74 74 public List<SecurityInfo> getAllByEndpoint(String endPoint) {
75   - ReadResultSecurityStore store = lwM2MCredentialsSecurityInfoValidator.createAndValidateCredentialsSecurityInfo(endPoint, LwM2mTransportHandlerUtil.LwM2mTypeServer.BOOTSTRAP);
  75 + ReadResultSecurityStore store = lwM2MCredentialsSecurityInfoValidator.createAndValidateCredentialsSecurityInfo(endPoint, LwM2mTransportUtil.LwM2mTypeServer.BOOTSTRAP);
76 76 if (store.getBootstrapJsonCredential() != null && store.getSecurityMode() < LwM2MSecurityMode.DEFAULT_MODE.code) {
77   - /** add value to store from BootstrapJson */
  77 + /* add value to store from BootstrapJson */
78 78 this.setBootstrapConfigScurityInfo(store);
79 79 BootstrapConfig bsConfigNew = store.getBootstrapConfig();
80 80 if (bsConfigNew != null) {
... ... @@ -96,9 +96,9 @@ public class LwM2MBootstrapSecurityStore implements BootstrapSecurityStore {
96 96
97 97 @Override
98 98 public SecurityInfo getByIdentity(String identity) {
99   - ReadResultSecurityStore store = lwM2MCredentialsSecurityInfoValidator.createAndValidateCredentialsSecurityInfo(identity, LwM2mTransportHandlerUtil.LwM2mTypeServer.BOOTSTRAP);
  99 + ReadResultSecurityStore store = lwM2MCredentialsSecurityInfoValidator.createAndValidateCredentialsSecurityInfo(identity, LwM2mTransportUtil.LwM2mTypeServer.BOOTSTRAP);
100 100 if (store.getBootstrapJsonCredential() != null && store.getSecurityMode() < LwM2MSecurityMode.DEFAULT_MODE.code) {
101   - /** add value to store from BootstrapJson */
  101 + /* add value to store from BootstrapJson */
102 102 this.setBootstrapConfigScurityInfo(store);
103 103 BootstrapConfig bsConfig = store.getBootstrapConfig();
104 104 if (bsConfig.security != null) {
... ... @@ -114,12 +114,12 @@ public class LwM2MBootstrapSecurityStore implements BootstrapSecurityStore {
114 114 }
115 115
116 116 private void setBootstrapConfigScurityInfo(ReadResultSecurityStore store) {
117   - /** BootstrapConfig */
  117 + /* BootstrapConfig */
118 118 LwM2MBootstrapConfig lwM2MBootstrapConfig = this.getParametersBootstrap(store);
119 119 if (lwM2MBootstrapConfig != null) {
120   - /** Security info */
  120 + /* Security info */
121 121 switch (SecurityMode.valueOf(lwM2MBootstrapConfig.getBootstrapServer().getSecurityMode())) {
122   - /** Use RPK only */
  122 + /* Use RPK only */
123 123 case PSK:
124 124 store.setSecurityInfo(SecurityInfo.newPreSharedKeyInfo(store.getEndPoint(),
125 125 lwM2MBootstrapConfig.getBootstrapServer().getClientPublicKeyOrId(),
... ...
... ... @@ -21,6 +21,7 @@ import org.eclipse.leshan.server.bootstrap.InMemoryBootstrapConfigStore;
21 21 import org.eclipse.leshan.server.bootstrap.InvalidConfigurationException;
22 22 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
23 23 import org.springframework.stereotype.Component;
  24 +
24 25 import java.util.Map;
25 26 import java.util.concurrent.locks.Lock;
26 27 import java.util.concurrent.locks.ReadWriteLock;
... ... @@ -58,8 +59,7 @@ public class LwM2MInMemoryBootstrapConfigStore extends InMemoryBootstrapConfigSt
58 59 public BootstrapConfig remove(String enpoint) {
59 60 writeLock.lock();
60 61 try {
61   - BootstrapConfig res = super.remove(enpoint);
62   - return res;
  62 + return super.remove(enpoint);
63 63 } finally {
64 64 writeLock.unlock();
65 65 }
... ...
... ... @@ -24,7 +24,6 @@ import org.eclipse.leshan.server.security.BootstrapSecurityStore;
24 24 import org.eclipse.leshan.server.security.SecurityChecker;
25 25 import org.eclipse.leshan.server.security.SecurityInfo;
26 26
27   -import java.util.Arrays;
28 27 import java.util.Collections;
29 28 import java.util.List;
30 29
... ...
... ... @@ -16,15 +16,10 @@
16 16 package org.thingsboard.server.transport.lwm2m.config;
17 17
18 18 import lombok.Getter;
19   -import lombok.Setter;
20 19 import lombok.extern.slf4j.Slf4j;
21 20 import org.springframework.beans.factory.annotation.Value;
22 21 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
23 22 import org.springframework.stereotype.Component;
24   -import org.thingsboard.server.gen.transport.TransportProtos;
25   -
26   -import java.security.PublicKey;
27   -import java.util.Map;
28 23
29 24 @Slf4j
30 25 @Component
... ...
... ... @@ -27,15 +27,9 @@ import org.springframework.stereotype.Component;
27 27 import javax.annotation.PostConstruct;
28 28 import java.io.File;
29 29 import java.io.FileInputStream;
30   -import java.io.IOException;
31 30 import java.io.InputStream;
32 31 import java.net.URI;
33   -import java.nio.file.Path;
34   -import java.nio.file.Paths;
35 32 import java.security.KeyStore;
36   -import java.security.KeyStoreException;
37   -import java.security.NoSuchAlgorithmException;
38   -import java.security.cert.CertificateException;
39 33
40 34 @Slf4j
41 35 @Component
... ...
... ... @@ -17,14 +17,15 @@ package org.thingsboard.server.transport.lwm2m.secure;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.eclipse.leshan.core.util.Hex;
20   -import java.security.SecureRandom;
21   -import java.security.KeyPairGenerator;
  20 +
  21 +import java.security.InvalidAlgorithmParameterException;
22 22 import java.security.KeyPair;
23   -import java.security.PrivateKey;
24   -import java.security.PublicKey;
  23 +import java.security.KeyPairGenerator;
25 24 import java.security.NoSuchAlgorithmException;
26 25 import java.security.NoSuchProviderException;
27   -import java.security.InvalidAlgorithmParameterException;
  26 +import java.security.PrivateKey;
  27 +import java.security.PublicKey;
  28 +import java.security.SecureRandom;
28 29 import java.security.interfaces.ECPublicKey;
29 30 import java.security.spec.ECGenParameterSpec;
30 31 import java.util.Arrays;
... ... @@ -48,33 +49,31 @@ public class LWM2MGenerationPSkRPkECC {
48 49 }
49 50
50 51 private void generationPSkKey() {
51   - /** PSK */
  52 + /* PSK */
52 53 int lenPSkKey = 32;
53   - /** Start PSK
54   - * Clients and Servers MUST support PSK keys of up to 64 bytes in length, as required by [RFC7925]
55   - * SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong, as described in [RFC4086]
56   - * */
  54 + /* Start PSK
  55 + Clients and Servers MUST support PSK keys of up to 64 bytes in length, as required by [RFC7925]
  56 + SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong, as described in [RFC4086]
  57 + */
57 58 SecureRandom randomPSK = new SecureRandom();
58   - byte bytesPSK[] = new byte[lenPSkKey];
  59 + byte[] bytesPSK = new byte[lenPSkKey];
59 60 randomPSK.nextBytes(bytesPSK);
60 61 log.info("\nCreating new PSK: \n for the next start PSK -> security key: [{}]", Hex.encodeHexString(bytesPSK));
61 62 }
62 63
63 64 private void generationRPKECCKey() {
64   - /** RPK */
  65 + /* RPK */
65 66 String algorithm = "EC";
66 67 String provider = "SunEC";
67 68 String nameParameterSpec = "secp256r1";
68 69
69   - /** Start RPK
70   - * Elliptic Curve parameters : [secp256r1 [NIST P-256, X9.62 prime256v1] (1.2.840.10045.3.1.7)]
71   - * */
  70 + /* Start RPK
  71 + Elliptic Curve parameters : [secp256r1 [NIST P-256, X9.62 prime256v1] (1.2.840.10045.3.1.7)]
  72 + */
72 73 KeyPairGenerator kpg = null;
73 74 try {
74 75 kpg = KeyPairGenerator.getInstance(algorithm, provider);
75   - } catch (NoSuchAlgorithmException e) {
76   - log.error("", e);
77   - } catch (NoSuchProviderException e) {
  76 + } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
78 77 log.error("", e);
79 78 }
80 79 ECGenParameterSpec ecsp = new ECGenParameterSpec(nameParameterSpec);
... ... @@ -90,17 +89,17 @@ public class LWM2MGenerationPSkRPkECC {
90 89
91 90 if (pubKey instanceof ECPublicKey) {
92 91 ECPublicKey ecPublicKey = (ECPublicKey) pubKey;
93   - /** Get x coordinate */
  92 + /* Get x coordinate */
94 93 byte[] x = ecPublicKey.getW().getAffineX().toByteArray();
95 94 if (x[0] == 0)
96 95 x = Arrays.copyOfRange(x, 1, x.length);
97 96
98   - /** Get Y coordinate */
  97 + /* Get Y coordinate */
99 98 byte[] y = ecPublicKey.getW().getAffineY().toByteArray();
100 99 if (y[0] == 0)
101 100 y = Arrays.copyOfRange(y, 1, y.length);
102 101
103   - /** Get Curves params */
  102 + /* Get Curves params */
104 103 String privHex = Hex.encodeHexString(privKey.getEncoded());
105 104 log.info("\nCreating new RPK for the next start... \n" +
106 105 " Public Key (Hex): [{}]\n" +
... ...
... ... @@ -29,8 +29,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceLwM2MC
29 29 import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
30 30 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig;
31 31 import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext;
32   -import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportServerHelper;
33   -import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil;
  32 +import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil;
34 33
35 34 import java.io.IOException;
36 35 import java.security.GeneralSecurityException;
... ... @@ -59,7 +58,7 @@ public class LwM2mCredentialsSecurityInfoValidator {
59 58 * @param keyValue -
60 59 * @return ValidateDeviceCredentialsResponseMsg and SecurityInfo
61 60 */
62   - public ReadResultSecurityStore createAndValidateCredentialsSecurityInfo(String endpoint, LwM2mTransportHandlerUtil.LwM2mTypeServer keyValue) {
  61 + public ReadResultSecurityStore createAndValidateCredentialsSecurityInfo(String endpoint, LwM2mTransportUtil.LwM2mTypeServer keyValue) {
63 62 CountDownLatch latch = new CountDownLatch(1);
64 63 final ReadResultSecurityStore[] resultSecurityStore = new ReadResultSecurityStore[1];
65 64 context.getTransportService().process(ValidateDeviceLwM2MCredentialsRequestMsg.newBuilder().setCredentialsId(endpoint).build(),
... ... @@ -69,7 +68,7 @@ public class LwM2mCredentialsSecurityInfoValidator {
69 68 String credentialsBody = msg.getCredentialsBody();
70 69 resultSecurityStore[0] = createSecurityInfo(endpoint, credentialsBody, keyValue);
71 70 resultSecurityStore[0].setMsg(msg);
72   - Optional<DeviceProfile> deviceProfileOpt = LwM2mTransportHandlerUtil.decode(msg.getProfileBody().toByteArray());
  71 + Optional<DeviceProfile> deviceProfileOpt = LwM2mTransportUtil.decode(msg.getProfileBody().toByteArray());
73 72 deviceProfileOpt.ifPresent(profile -> resultSecurityStore[0].setDeviceProfile(profile));
74 73 latch.countDown();
75 74 }
... ... @@ -96,9 +95,9 @@ public class LwM2mCredentialsSecurityInfoValidator {
96 95 * @param keyValue -
97 96 * @return SecurityInfo
98 97 */
99   - private ReadResultSecurityStore createSecurityInfo(String endPoint, String jsonStr, LwM2mTransportHandlerUtil.LwM2mTypeServer keyValue) {
  98 + private ReadResultSecurityStore createSecurityInfo(String endPoint, String jsonStr, LwM2mTransportUtil.LwM2mTypeServer keyValue) {
100 99 ReadResultSecurityStore result = new ReadResultSecurityStore();
101   - JsonObject objectMsg = LwM2mTransportHandlerUtil.validateJson(jsonStr);
  100 + JsonObject objectMsg = LwM2mTransportUtil.validateJson(jsonStr);
102 101 if (objectMsg != null && !objectMsg.isJsonNull()) {
103 102 JsonObject object = (objectMsg.has(keyValue.type) && !objectMsg.get(keyValue.type).isJsonNull()) ? objectMsg.get(keyValue.type).getAsJsonObject() : null;
104 103 /**
... ... @@ -109,7 +108,7 @@ public class LwM2mCredentialsSecurityInfoValidator {
109 108 && objectMsg.get("client").getAsJsonObject().get("endpoint").isJsonPrimitive()) ? objectMsg.get("client").getAsJsonObject().get("endpoint").getAsString() : null;
110 109 endPoint = (endPointPsk == null || endPointPsk.isEmpty()) ? endPoint : endPointPsk;
111 110 if (object != null && !object.isJsonNull()) {
112   - if (keyValue.equals(LwM2mTransportHandlerUtil.LwM2mTypeServer.BOOTSTRAP)) {
  111 + if (keyValue.equals(LwM2mTransportUtil.LwM2mTypeServer.BOOTSTRAP)) {
113 112 result.setBootstrapJsonCredential(object);
114 113 result.setEndPoint(endPoint);
115 114 result.setSecurityMode(LwM2MSecurityMode.fromSecurityMode(object.get("bootstrapServer").getAsJsonObject().get("securityMode").getAsString().toLowerCase()).code);
... ...
... ... @@ -18,20 +18,21 @@ package org.thingsboard.server.transport.lwm2m.secure;
18 18 import lombok.Data;
19 19 import lombok.extern.slf4j.Slf4j;
20 20 import org.eclipse.leshan.core.util.Hex;
  21 +
21 22 import java.math.BigInteger;
22   -import java.security.PrivateKey;
23   -import java.security.PublicKey;
24 23 import java.security.AlgorithmParameters;
25   -import java.security.KeyFactory;
26 24 import java.security.GeneralSecurityException;
  25 +import java.security.KeyFactory;
  26 +import java.security.PrivateKey;
  27 +import java.security.PublicKey;
27 28 import java.security.cert.Certificate;
28 29 import java.security.cert.X509Certificate;
29 30 import java.security.spec.ECGenParameterSpec;
30 31 import java.security.spec.ECParameterSpec;
  32 +import java.security.spec.ECPoint;
  33 +import java.security.spec.ECPrivateKeySpec;
31 34 import java.security.spec.ECPublicKeySpec;
32 35 import java.security.spec.KeySpec;
33   -import java.security.spec.ECPrivateKeySpec;
34   -import java.security.spec.ECPoint;
35 36 import java.util.List;
36 37
37 38 @Slf4j
... ... @@ -54,7 +55,7 @@ public class LwM2mRPkCredentials {
54 55
55 56 private void generatePublicKeyRPK(String publX, String publY, String privS) {
56 57 try {
57   - /**Get Elliptic Curve Parameter spec for secp256r1 */
  58 + /*Get Elliptic Curve Parameter spec for secp256r1 */
58 59 AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC");
59 60 algoParameters.init(new ECGenParameterSpec("secp256r1"));
60 61 ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class);
... ... @@ -62,18 +63,18 @@ public class LwM2mRPkCredentials {
62 63 // Get point values
63 64 byte[] publicX = Hex.decodeHex(publX.toCharArray());
64 65 byte[] publicY = Hex.decodeHex(publY.toCharArray());
65   - /** Create key specs */
  66 + /* Create key specs */
66 67 KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)),
67 68 parameterSpec);
68   - /** Get keys */
  69 + /* Get keys */
69 70 this.serverPublicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec);
70 71 }
71 72 if (privS != null && !privS.isEmpty()) {
72   - /** Get point values */
  73 + /* Get point values */
73 74 byte[] privateS = Hex.decodeHex(privS.toCharArray());
74   - /** Create key specs */
  75 + /* Create key specs */
75 76 KeySpec privateKeySpec = new ECPrivateKeySpec(new BigInteger(privateS), parameterSpec);
76   - /** Get keys */
  77 + /* Get keys */
77 78 this.serverPrivateKey = KeyFactory.getInstance("EC").generatePrivate(privateKeySpec);
78 79 }
79 80 } catch (GeneralSecurityException | IllegalArgumentException e) {
... ...
... ... @@ -31,8 +31,8 @@ public class ReadResultSecurityStore {
31 31 private int securityMode = DEFAULT_MODE.code;
32 32
33 33 /** bootstrap */
34   - DeviceProfile deviceProfile;
35   - JsonObject bootstrapJsonCredential;
36   - String endPoint;
37   - BootstrapConfig bootstrapConfig;
  34 + private DeviceProfile deviceProfile;
  35 + private JsonObject bootstrapJsonCredential;
  36 + private String endPoint;
  37 + private BootstrapConfig bootstrapConfig;
38 38 }
... ...
... ... @@ -33,7 +33,6 @@ import org.eclipse.leshan.core.request.ContentFormat;
33 33 import org.eclipse.leshan.core.request.WriteRequest;
34 34 import org.eclipse.leshan.core.response.ReadResponse;
35 35 import org.eclipse.leshan.core.util.NamedThreadFactory;
36   -import org.eclipse.leshan.server.californium.LeshanServer;
37 36 import org.eclipse.leshan.server.registration.Registration;
38 37 import org.springframework.context.annotation.Lazy;
39 38 import org.springframework.stereotype.Service;
... ... @@ -55,6 +54,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.SessionEvent;
55 54 import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto;
56 55 import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
57 56 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig;
  57 +import org.thingsboard.server.transport.lwm2m.server.adaptors.LwM2MJsonAdaptor;
58 58 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient;
59 59 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext;
60 60 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientProfile;
... ... @@ -85,30 +85,30 @@ import static org.eclipse.californium.core.coap.CoAP.ResponseCode.BAD_REQUEST;
85 85 import static org.eclipse.leshan.core.attributes.Attribute.OBJECT_VERSION;
86 86 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_KEY;
87 87 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH;
88   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.CLIENT_NOT_AUTHORIZED;
89   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.DEVICE_ATTRIBUTES_REQUEST;
90   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.FR_OBJECT_ID;
91   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.FR_PATH_RESOURCE_VER_ID;
92   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_ERROR;
93   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_INFO;
94   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_VALUE;
95   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LWM2M_STRATEGY_2;
96   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper;
97   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.DISCOVER;
98   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.EXECUTE;
99   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.OBSERVE;
100   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.OBSERVE_CANCEL;
101   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.OBSERVE_READ_ALL;
102   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.READ;
103   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.WRITE_ATTRIBUTES;
104   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.WRITE_REPLACE;
105   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.WRITE_UPDATE;
106   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.SERVICE_CHANNEL;
107   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertJsonArrayToSet;
108   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromIdVerToObjectId;
109   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromObjectIdToIdVer;
110   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.getAckCallback;
111   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.validateObjectVerFromKey;
  88 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.CLIENT_NOT_AUTHORIZED;
  89 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.DEVICE_ATTRIBUTES_REQUEST;
  90 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.FR_OBJECT_ID;
  91 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.FR_PATH_RESOURCE_VER_ID;
  92 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_ERROR;
  93 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_INFO;
  94 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_VALUE;
  95 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LWM2M_STRATEGY_2;
  96 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper;
  97 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.DISCOVER;
  98 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.EXECUTE;
  99 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE;
  100 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_CANCEL;
  101 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_READ_ALL;
  102 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.READ;
  103 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.WRITE_ATTRIBUTES;
  104 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.WRITE_REPLACE;
  105 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.WRITE_UPDATE;
  106 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.SERVICE_CHANNEL;
  107 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertJsonArrayToSet;
  108 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromIdVerToObjectId;
  109 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromObjectIdToIdVer;
  110 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.getAckCallback;
  111 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.validateObjectVerFromKey;
112 112
113 113 @Slf4j
114 114 @Service
... ... @@ -125,6 +125,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
125 125 private final LwM2MTransportServerConfig config;
126 126 private final FirmwareDataCache firmwareDataCache;
127 127 private final LwM2mTransportServerHelper helper;
  128 + private final LwM2MJsonAdaptor adaptor;
128 129 private final LwM2mClientContext lwM2mClientContext;
129 130 private final LwM2mTransportRequest lwM2mTransportRequest;
130 131
... ... @@ -132,7 +133,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
132 133 LwM2mClientContext lwM2mClientContext,
133 134 @Lazy LwM2mTransportRequest lwM2mTransportRequest,
134 135 FirmwareDataCache firmwareDataCache,
135   - LwM2mTransportContext context) {
  136 + LwM2mTransportContext context, LwM2MJsonAdaptor adaptor) {
136 137 this.transportService = transportService;
137 138 this.config = config;
138 139 this.helper = helper;
... ... @@ -140,6 +141,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
140 141 this.lwM2mTransportRequest = lwM2mTransportRequest;
141 142 this.firmwareDataCache = firmwareDataCache;
142 143 this.context = context;
  144 + this.adaptor = adaptor;
143 145 }
144 146
145 147 @PostConstruct
... ... @@ -334,7 +336,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
334 336 msg.getSharedUpdatedList().forEach(tsKvProto -> {
335 337 String pathName = tsKvProto.getKv().getKey();
336 338 String pathIdVer = this.getPresentPathIntoProfile(sessionInfo, pathName);
337   - Object valueNew = this.helper.getValueFromKvProto(tsKvProto.getKv());
  339 + Object valueNew = LwM2mTransportServerHelper.getValueFromKvProto(tsKvProto.getKv());
338 340 //TODO: react on change of the firmware name.
339 341 if (FirmwareUtil.getAttributeKey(FirmwareType.FIRMWARE, FirmwareKey.VERSION).equals(pathName) && !valueNew.equals(lwM2MClient.getFrUpdate().getCurrentFwVersion())) {
340 342 this.getInfoFirmwareUpdate(lwM2MClient);
... ... @@ -361,7 +363,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
361 363 } else if (msg.getSharedDeletedCount() > 0) {
362 364 msg.getSharedUpdatedList().forEach(tsKvProto -> {
363 365 String pathName = tsKvProto.getKv().getKey();
364   - Object valueNew = this.helper.getValueFromKvProto(tsKvProto.getKv());
  366 + Object valueNew = LwM2mTransportServerHelper.getValueFromKvProto(tsKvProto.getKv());
365 367 if (FirmwareUtil.getAttributeKey(FirmwareType.FIRMWARE, FirmwareKey.VERSION).equals(pathName) && !valueNew.equals(lwM2MClient.getFrUpdate().getCurrentFwVersion())) {
366 368 lwM2MClient.getFrUpdate().setCurrentFwVersion((String) valueNew);
367 369 }
... ... @@ -458,7 +460,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
458 460 lwm2mClientRpcRequest.setRequestId(toDeviceRequest.getRequestId());
459 461 lwm2mClientRpcRequest.setSessionInfo(sessionInfo);
460 462 lwm2mClientRpcRequest.setValidTypeOper(toDeviceRequest.getMethodName());
461   - JsonObject rpcRequest = LwM2mTransportHandlerUtil.validateJson(toDeviceRequest.getParams());
  463 + JsonObject rpcRequest = LwM2mTransportUtil.validateJson(toDeviceRequest.getParams());
462 464 if (rpcRequest != null) {
463 465 if (rpcRequest.has(lwm2mClientRpcRequest.keyNameKey)) {
464 466 String targetIdVer = this.getPresentPathIntoProfile(sessionInfo,
... ... @@ -639,7 +641,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
639 641 LwM2mClientProfile lwM2MClientProfile = lwM2mClientContext.getProfile(registration);
640 642 Set<String> clientObjects = lwM2mClientContext.getSupportedIdVerInClient(registration);
641 643 if (clientObjects != null && clientObjects.size() > 0) {
642   - if (LWM2M_STRATEGY_2 == LwM2mTransportHandlerUtil.getClientOnlyObserveAfterConnect(lwM2MClientProfile)) {
  644 + if (LWM2M_STRATEGY_2 == LwM2mTransportUtil.getClientOnlyObserveAfterConnect(lwM2MClientProfile)) {
643 645 // #2
644 646 lwM2MClient.getPendingReadRequests().addAll(clientObjects);
645 647 clientObjects.forEach(path -> lwM2mTransportRequest.sendAllRequest(registration, path, READ, ContentFormat.TLV.getName(),
... ... @@ -1361,7 +1363,7 @@ public class DefaultLwM2MTransportMsgHandler implements LwM2mTransportMsgHandler
1361 1363 if (keyNamesMap.values().size() > 0) {
1362 1364 try {
1363 1365 //#1.2
1364   - TransportProtos.GetAttributeRequestMsg getAttributeMsg = helper.getAdaptor().convertToGetAttributes(null, keyNamesMap.values());
  1366 + TransportProtos.GetAttributeRequestMsg getAttributeMsg = adaptor.convertToGetAttributes(null, keyNamesMap.values());
1365 1367 transportService.process(sessionInfo, getAttributeMsg, getAckCallback(lwM2MClient, getAttributeMsg.getRequestId(), DEVICE_ATTRIBUTES_REQUEST));
1366 1368 } catch (AdaptorException e) {
1367 1369 log.warn("Failed to decode get attributes request", e);
... ...
... ... @@ -17,8 +17,6 @@ package org.thingsboard.server.transport.lwm2m.server;
17 17
18 18 import lombok.RequiredArgsConstructor;
19 19 import lombok.extern.slf4j.Slf4j;
20   -import org.eclipse.californium.core.network.config.NetworkConfig;
21   -import org.eclipse.californium.core.network.stack.BlockwiseLayer;
22 20 import org.eclipse.californium.scandium.config.DtlsConnectorConfig;
23 21 import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder;
24 22 import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder;
... ... @@ -91,40 +89,39 @@ public class DefaultLwM2mTransportService implements LwM2MTransportService {
91 89 if (config.getEnableGenNewKeyPskRpk()) {
92 90 new LWM2MGenerationPSkRPkECC();
93 91 }
94   - this.server = getLhServer(config.getPort(), config.getSecurePort());
  92 + this.server = getLhServer();
95 93 this.startLhServer();
96 94 this.context.setServer(server);
97 95 }
98 96
99 97 private void startLhServer() {
100   - log.info("Starting LwM2M transport Server...");
  98 + log.info("Starting LwM2M transport server...");
101 99 this.server.start();
102 100 LwM2mServerListener lhServerCertListener = new LwM2mServerListener(handler);
103 101 this.server.getRegistrationService().addListener(lhServerCertListener.registrationListener);
104 102 this.server.getPresenceService().addListener(lhServerCertListener.presenceListener);
105 103 this.server.getObservationService().addListener(lhServerCertListener.observationListener);
  104 + log.info("Started LwM2M transport server.");
106 105 }
107 106
108 107 @PreDestroy
109 108 public void shutdown() {
110   - log.info("Stopping LwM2M transport Server!");
  109 + log.info("Stopping LwM2M transport server!");
111 110 server.destroy();
112   - log.info("LwM2M transport Server stopped!");
  111 + log.info("LwM2M transport server stopped!");
113 112 }
114 113
115   - private LeshanServer getLhServer(Integer serverPortNoSec, Integer serverSecurePort) {
  114 + private LeshanServer getLhServer() {
116 115 LeshanServerBuilder builder = new LeshanServerBuilder();
117   - builder.setLocalAddress(config.getHost(), serverPortNoSec);
118   - builder.setLocalSecureAddress(config.getSecureHost(), serverSecurePort);
  116 + builder.setLocalAddress(config.getHost(), config.getPort());
  117 + builder.setLocalSecureAddress(config.getSecureHost(), config.getSecurePort());
119 118 builder.setDecoder(new DefaultLwM2mNodeDecoder());
120 119 /* Use a magic converter to support bad type send by the UI. */
121 120 builder.setEncoder(new DefaultLwM2mNodeEncoder(LwM2mValueConverterImpl.getInstance()));
122 121
123 122
124 123 /* Create CoAP Config */
125   - NetworkConfig networkConfig = getCoapConfig(serverPortNoSec, serverSecurePort);
126   - BlockwiseLayer blockwiseLayer = new BlockwiseLayer(networkConfig);
127   - builder.setCoapConfig(getCoapConfig(serverPortNoSec, serverSecurePort));
  124 + builder.setCoapConfig(getCoapConfig(config.getPort(), config.getSecurePort()));
128 125
129 126 /* Define model provider (Create Models )*/
130 127 LwM2mModelProvider modelProvider = new LwM2mVersionedModelProvider(this.lwM2mClientContext, this.helper, this.context);
... ...
... ... @@ -26,8 +26,8 @@ import org.eclipse.leshan.server.registration.RegistrationUpdate;
26 26
27 27 import java.util.Collection;
28 28
29   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_INFO;
30   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromObjectIdToIdVer;
  29 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_INFO;
  30 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromObjectIdToIdVer;
31 31
32 32 @Slf4j
33 33 public class LwM2mServerListener {
... ...
... ... @@ -35,22 +35,22 @@ import java.util.Optional;
35 35
36 36 @Slf4j
37 37 public class LwM2mSessionMsgListener implements GenericFutureListener<Future<? super Void>>, SessionMsgListener {
38   - private DefaultLwM2MTransportMsgHandler service;
  38 + private DefaultLwM2MTransportMsgHandler handler;
39 39 private TransportProtos.SessionInfoProto sessionInfo;
40 40
41   - public LwM2mSessionMsgListener(DefaultLwM2MTransportMsgHandler service, TransportProtos.SessionInfoProto sessionInfo) {
42   - this.service = service;
  41 + public LwM2mSessionMsgListener(DefaultLwM2MTransportMsgHandler handler, TransportProtos.SessionInfoProto sessionInfo) {
  42 + this.handler = handler;
43 43 this.sessionInfo = sessionInfo;
44 44 }
45 45
46 46 @Override
47 47 public void onGetAttributesResponse(GetAttributeResponseMsg getAttributesResponse) {
48   - this.service.onGetAttributesResponse(getAttributesResponse, this.sessionInfo);
  48 + this.handler.onGetAttributesResponse(getAttributesResponse, this.sessionInfo);
49 49 }
50 50
51 51 @Override
52 52 public void onAttributeUpdate(AttributeUpdateNotificationMsg attributeUpdateNotification) {
53   - this.service.onAttributeUpdate(attributeUpdateNotification, this.sessionInfo);
  53 + this.handler.onAttributeUpdate(attributeUpdateNotification, this.sessionInfo);
54 54 }
55 55
56 56 @Override
... ... @@ -60,27 +60,27 @@ public class LwM2mSessionMsgListener implements GenericFutureListener<Future<? s
60 60
61 61 @Override
62 62 public void onToTransportUpdateCredentials(ToTransportUpdateCredentialsProto updateCredentials) {
63   - this.service.onToTransportUpdateCredentials(updateCredentials);
  63 + this.handler.onToTransportUpdateCredentials(updateCredentials);
64 64 }
65 65
66 66 @Override
67 67 public void onDeviceProfileUpdate(TransportProtos.SessionInfoProto sessionInfo, DeviceProfile deviceProfile) {
68   - this.service.onDeviceProfileUpdate(sessionInfo, deviceProfile);
  68 + this.handler.onDeviceProfileUpdate(sessionInfo, deviceProfile);
69 69 }
70 70
71 71 @Override
72 72 public void onDeviceUpdate(TransportProtos.SessionInfoProto sessionInfo, Device device, Optional<DeviceProfile> deviceProfileOpt) {
73   - this.service.onDeviceUpdate(sessionInfo, device, deviceProfileOpt);
  73 + this.handler.onDeviceUpdate(sessionInfo, device, deviceProfileOpt);
74 74 }
75 75
76 76 @Override
77 77 public void onToDeviceRpcRequest(ToDeviceRpcRequestMsg toDeviceRequest) {
78   - this.service.onToDeviceRpcRequest(toDeviceRequest,this.sessionInfo);
  78 + this.handler.onToDeviceRpcRequest(toDeviceRequest,this.sessionInfo);
79 79 }
80 80
81 81 @Override
82 82 public void onToServerRpcResponse(ToServerRpcResponseMsg toServerResponse) {
83   - this.service.onToServerRpcResponse(toServerResponse);
  83 + this.handler.onToServerRpcResponse(toServerResponse);
84 84 }
85 85
86 86 @Override
... ... @@ -91,14 +91,14 @@ public class LwM2mSessionMsgListener implements GenericFutureListener<Future<? s
91 91 @Override
92 92 public void onResourceUpdate(@NotNull Optional<TransportProtos.ResourceUpdateMsg> resourceUpdateMsgOpt) {
93 93 if (ResourceType.LWM2M_MODEL.name().equals(resourceUpdateMsgOpt.get().getResourceType())) {
94   - this.service.onResourceUpdate(resourceUpdateMsgOpt);
  94 + this.handler.onResourceUpdate(resourceUpdateMsgOpt);
95 95 }
96 96 }
97 97
98 98 @Override
99 99 public void onResourceDelete(@NotNull Optional<TransportProtos.ResourceDeleteMsg> resourceDeleteMsgOpt) {
100 100 if (ResourceType.LWM2M_MODEL.name().equals(resourceDeleteMsgOpt.get().getResourceType())) {
101   - this.service.onResourceDelete(resourceDeleteMsgOpt);
  101 + this.handler.onResourceDelete(resourceDeleteMsgOpt);
102 102 }
103 103 }
104 104 }
... ...
... ... @@ -20,7 +20,6 @@ import org.eclipse.leshan.core.response.ReadResponse;
20 20 import org.eclipse.leshan.server.registration.Registration;
21 21 import org.thingsboard.server.common.data.Device;
22 22 import org.thingsboard.server.common.data.DeviceProfile;
23   -import org.thingsboard.server.common.data.TbTransportService;
24 23 import org.thingsboard.server.gen.transport.TransportProtos;
25 24 import org.thingsboard.server.transport.lwm2m.server.client.Lwm2mClientRpcRequest;
26 25
... ...
... ... @@ -46,10 +46,8 @@ import org.eclipse.leshan.core.response.WriteAttributesResponse;
46 46 import org.eclipse.leshan.core.response.WriteResponse;
47 47 import org.eclipse.leshan.core.util.Hex;
48 48 import org.eclipse.leshan.core.util.NamedThreadFactory;
49   -import org.eclipse.leshan.server.californium.LeshanServer;
50 49 import org.eclipse.leshan.server.registration.Registration;
51 50 import org.springframework.stereotype.Service;
52   -import org.thingsboard.server.common.transport.TransportService;
53 51 import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
54 52 import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig;
55 53 import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient;
... ... @@ -68,18 +66,18 @@ import java.util.stream.Collectors;
68 66 import static org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT;
69 67 import static org.eclipse.leshan.core.ResponseCode.BAD_REQUEST;
70 68 import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND;
71   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.DEFAULT_TIMEOUT;
72   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.FR_PATH_RESOURCE_VER_ID;
73   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_ERROR;
74   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_INFO;
75   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_VALUE;
76   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper;
77   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.OBSERVE_CANCEL;
78   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper.OBSERVE_READ_ALL;
79   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.RESPONSE_CHANNEL;
80   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromIdVerToObjectId;
81   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromObjectIdToIdVer;
82   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.createWriteAttributeRequest;
  69 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.DEFAULT_TIMEOUT;
  70 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.FR_PATH_RESOURCE_VER_ID;
  71 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_ERROR;
  72 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_INFO;
  73 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_VALUE;
  74 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper;
  75 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_CANCEL;
  76 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper.OBSERVE_READ_ALL;
  77 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.RESPONSE_CHANNEL;
  78 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromIdVerToObjectId;
  79 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromObjectIdToIdVer;
  80 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.createWriteAttributeRequest;
83 81
84 82 @Slf4j
85 83 @Service
... ... @@ -92,7 +90,6 @@ public class LwM2mTransportRequest {
92 90
93 91 private final LwM2mTransportContext context;
94 92 private final LwM2MTransportServerConfig config;
95   - private final LwM2mTransportServerHelper lwM2MTransportServerHelper;
96 93 private final LwM2mClientContext lwM2mClientContext;
97 94 private final DefaultLwM2MTransportMsgHandler serviceImpl;
98 95
... ... @@ -120,8 +117,7 @@ public class LwM2mTransportRequest {
120 117 ContentFormat contentFormat = contentFormatName != null ? ContentFormat.fromName(contentFormatName.toUpperCase()) : ContentFormat.DEFAULT;
121 118 LwM2mClient lwM2MClient = this.lwM2mClientContext.getLwM2mClientWithReg(registration, null);
122 119 LwM2mPath resultIds = target != null ? new LwM2mPath(target) : null;
123   - if (!OBSERVE_READ_ALL.name().equals(typeOper.name()) && resultIds != null && registration != null && resultIds.getObjectId() >= 0 &&
124   - lwM2MClient != null) {
  120 + if (!OBSERVE_READ_ALL.name().equals(typeOper.name()) && resultIds != null && registration != null && resultIds.getObjectId() >= 0 && lwM2MClient != null) {
125 121 if (lwM2MClient.isValidObjectVersion(targetIdVer)) {
126 122 timeoutInMs = timeoutInMs > 0 ? timeoutInMs : DEFAULT_TIMEOUT;
127 123 ResourceModel resourceModel = null;
... ... @@ -142,10 +138,10 @@ public class LwM2mTransportRequest {
142 138 }
143 139 break;
144 140 case OBSERVE_CANCEL:
145   - /**
146   - * lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, POST_TYPE_OPER_OBSERVE_CANCEL, null, null, null, null, context.getTimeout());
147   - * At server side this will not remove the observation from the observation store, to do it you need to use
148   - * {@code ObservationService#cancelObservation()}
  141 + /*
  142 + lwM2MTransportRequest.sendAllRequest(lwServer, registration, path, POST_TYPE_OPER_OBSERVE_CANCEL, null, null, null, null, context.getTimeout());
  143 + At server side this will not remove the observation from the observation store, to do it you need to use
  144 + {@code ObservationService#cancelObservation()}
149 145 */
150 146 context.getServer().getObservationService().cancelObservations(registration, target);
151 147 break;
... ... @@ -266,9 +262,9 @@ public class LwM2mTransportRequest {
266 262 if (rpcRequest != null) {
267 263 serviceImpl.sentRpcRequest(rpcRequest, response.getCode().getName(), response.getErrorMessage(), LOG_LW2M_ERROR);
268 264 }
269   - /** Not Found
270   - * set setClient_fw_version = empty
271   - **/
  265 + /* Not Found
  266 + set setClient_fw_version = empty
  267 + */
272 268 if (FR_PATH_RESOURCE_VER_ID.equals(request.getPath().toString()) && lwM2MClient.isUpdateFw()) {
273 269 lwM2MClient.setUpdateFw(false);
274 270 lwM2MClient.getFrUpdate().setClientFwVersion("");
... ... @@ -277,9 +273,9 @@ public class LwM2mTransportRequest {
277 273 }
278 274 }
279 275 }, e -> {
280   - /** version == null
281   - * set setClient_fw_version = empty
282   - **/
  276 + /* version == null
  277 + set setClient_fw_version = empty
  278 + */
283 279 if (FR_PATH_RESOURCE_VER_ID.equals(request.getPath().toString()) && lwM2MClient.isUpdateFw()) {
284 280 lwM2MClient.setUpdateFw(false);
285 281 lwM2MClient.getFrUpdate().setClientFwVersion("");
... ...
... ... @@ -30,7 +30,6 @@ package org.thingsboard.server.transport.lwm2m.server;
30 30 * limitations under the License.
31 31 */
32 32
33   -import lombok.Getter;
34 33 import lombok.RequiredArgsConstructor;
35 34 import lombok.extern.slf4j.Slf4j;
36 35 import org.eclipse.leshan.core.model.DDFFileParser;
... ... @@ -40,7 +39,6 @@ import org.eclipse.leshan.core.model.ObjectModel;
40 39 import org.eclipse.leshan.core.model.ResourceModel;
41 40 import org.eclipse.leshan.core.node.codec.CodecException;
42 41 import org.springframework.stereotype.Component;
43   -import org.thingsboard.server.common.transport.TransportService;
44 42 import org.thingsboard.server.common.transport.TransportServiceCallback;
45 43 import org.thingsboard.server.gen.transport.TransportProtos;
46 44 import org.thingsboard.server.gen.transport.TransportProtos.PostAttributeMsg;
... ... @@ -55,7 +53,7 @@ import java.util.ArrayList;
55 53 import java.util.List;
56 54
57 55 import static org.thingsboard.server.gen.transport.TransportProtos.KeyValueType.BOOLEAN_V;
58   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LOG_LW2M_TELEMETRY;
  56 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LOG_LW2M_TELEMETRY;
59 57
60 58 @Slf4j
61 59 @Component
... ... @@ -64,10 +62,6 @@ import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandle
64 62 public class LwM2mTransportServerHelper {
65 63
66 64 private final LwM2mTransportContext context;
67   -
68   - private final TransportService transportService;
69   -
70   - @Getter
71 65 private final LwM2MJsonAdaptor adaptor;
72 66
73 67 /**
... ... @@ -95,7 +89,7 @@ public class LwM2mTransportServerHelper {
95 89 request.addAllKv(result);
96 90 PostAttributeMsg postAttributeMsg = request.build();
97 91 TransportServiceCallback call = this.getPubAckCallbackSendAttrTelemetry(postAttributeMsg);
98   - transportService.process(sessionInfo, postAttributeMsg, this.getPubAckCallbackSendAttrTelemetry(call));
  92 + context.getTransportService().process(sessionInfo, postAttributeMsg, this.getPubAckCallbackSendAttrTelemetry(call));
99 93 }
100 94
101 95 public void sendParametersOnThingsboardTelemetry(List<TransportProtos.KeyValueProto> result, SessionInfoProto sessionInfo) {
... ... @@ -106,7 +100,7 @@ public class LwM2mTransportServerHelper {
106 100 request.addTsKvList(builder.build());
107 101 PostTelemetryMsg postTelemetryMsg = request.build();
108 102 TransportServiceCallback call = this.getPubAckCallbackSendAttrTelemetry(postTelemetryMsg);
109   - transportService.process(sessionInfo, postTelemetryMsg, this.getPubAckCallbackSendAttrTelemetry(call));
  103 + context.getTransportService().process(sessionInfo, postTelemetryMsg, this.getPubAckCallbackSendAttrTelemetry(call));
110 104 }
111 105
112 106 /**
... ... @@ -191,7 +185,7 @@ public class LwM2mTransportServerHelper {
191 185 * @param resourcePath -
192 186 * @return
193 187 */
194   - public ResourceModel.Type getResourceModelTypeEqualsKvProtoValueType(ResourceModel.Type currentType, String resourcePath) {
  188 + public static ResourceModel.Type getResourceModelTypeEqualsKvProtoValueType(ResourceModel.Type currentType, String resourcePath) {
195 189 switch (currentType) {
196 190 case BOOLEAN:
197 191 return ResourceModel.Type.BOOLEAN;
... ... @@ -209,7 +203,7 @@ public class LwM2mTransportServerHelper {
209 203 throw new CodecException("Invalid ResourceModel_Type for resource %s, got %s", resourcePath, currentType);
210 204 }
211 205
212   - public Object getValueFromKvProto(TransportProtos.KeyValueProto kv) {
  206 + public static Object getValueFromKvProto(TransportProtos.KeyValueProto kv) {
213 207 switch (kv.getType()) {
214 208 case BOOLEAN_V:
215 209 return kv.getBoolV();
... ...
1   -/**
2   - * Copyright © 2016-2021 The Thingsboard Authors
3   - *
4   - * Licensed under the Apache License, Version 2.0 (the "License");
5   - * you may not use this file except in compliance with the License.
6   - * You may obtain a copy of the License at
7   - *
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - *
10   - * Unless required by applicable law or agreed to in writing, software
11   - * distributed under the License is distributed on an "AS IS" BASIS,
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   - * See the License for the specific language governing permissions and
14   - * limitations under the License.
15   - */
16   -package org.thingsboard.server.transport.lwm2m.server;
17   -
18   -import lombok.extern.slf4j.Slf4j;
19   -import org.eclipse.leshan.server.californium.LeshanServer;
20   -import org.springframework.beans.factory.annotation.Autowired;
21   -import org.springframework.stereotype.Component;
22   -import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
23   -import org.thingsboard.server.transport.lwm2m.secure.LWM2MGenerationPSkRPkECC;
24   -
25   -import javax.annotation.PostConstruct;
26   -import javax.annotation.PreDestroy;
27   -
28   -@Slf4j
29   -@Component("LwM2MTransportServerInitializer")
30   -@TbLwM2mTransportComponent
31   -public class LwM2mTransportServerInitializer {
32   -
33   -}
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2mTransportUtil.java renamed from common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/LwM2mTransportHandlerUtil.java
... ... @@ -67,7 +67,7 @@ import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPA
67 67 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH;
68 68
69 69 @Slf4j
70   -public class LwM2mTransportHandlerUtil {
  70 +public class LwM2mTransportUtil {
71 71
72 72 public static final String TRANSPORT_DEFAULT_LWM2M_VERSION = "1.0";
73 73 public static final String CLIENT_LWM2M_SETTINGS = "clientLwM2mSettings";
... ... @@ -150,8 +150,8 @@ public class LwM2mTransportHandlerUtil {
150 150 * if all resources are to be replaced
151 151 */
152 152 WRITE_REPLACE(6, "WriteReplace"),
153   - /**
154   - * PUT
  153 + /*
  154 + PUT
155 155 */
156 156 /**
157 157 * Adds or updates Resources provided in the new value and leaves other existing Resources unchanged. (see section
... ... @@ -178,7 +178,7 @@ public class LwM2mTransportHandlerUtil {
178 178 return to;
179 179 }
180 180 }
181   - throw new IllegalArgumentException(String.format("Unsupported typeOper type : %d", type));
  181 + throw new IllegalArgumentException(String.format("Unsupported typeOper type : %s", type));
182 182 }
183 183 }
184 184
... ... @@ -198,7 +198,7 @@ public class LwM2mTransportHandlerUtil {
198 198 case OBJLNK:
199 199 return valueOld.equals(valueNew);
200 200 case OPAQUE:
201   - return Hex.decodeHex(((String) valueOld).toCharArray()).equals(Hex.decodeHex(((String) valueNew).toCharArray()));
  201 + return Arrays.equals(Hex.decodeHex(((String) valueOld).toCharArray()), Hex.decodeHex(((String) valueNew).toCharArray()));
202 202 default:
203 203 throw new CodecException("Invalid value type for resource %s, type %s", resourcePath, type);
204 204 }
... ... @@ -256,7 +256,7 @@ public class LwM2mTransportHandlerUtil {
256 256 ObjectMapper mapper = new ObjectMapper();
257 257 String profileStr = mapper.writeValueAsString(profile);
258 258 JsonObject profileJson = (profileStr != null) ? validateJson(profileStr) : null;
259   - return getValidateCredentialsBodyFromThingsboard(profileJson) ? LwM2mTransportHandlerUtil.getNewProfileParameters(profileJson, deviceProfile.getTenantId()) : null;
  259 + return getValidateCredentialsBodyFromThingsboard(profileJson) ? LwM2mTransportUtil.getNewProfileParameters(profileJson, deviceProfile.getTenantId()) : null;
260 260 } catch (IOException e) {
261 261 log.error("", e);
262 262 }
... ... @@ -415,7 +415,7 @@ public class LwM2mTransportHandlerUtil {
415 415 }
416 416
417 417 public static String validPathIdVer(String pathIdVer, Registration registration) throws IllegalArgumentException {
418   - if (pathIdVer.indexOf(LWM2M_SEPARATOR_PATH) < 0) {
  418 + if (!pathIdVer.contains(LWM2M_SEPARATOR_PATH)) {
419 419 throw new IllegalArgumentException(String.format("Error:"));
420 420 } else {
421 421 String[] keyArray = pathIdVer.split(LWM2M_SEPARATOR_PATH);
... ... @@ -488,7 +488,7 @@ public class LwM2mTransportHandlerUtil {
488 488 }
489 489
490 490 private static Attribute[] createWriteAttributes(Object params) {
491   - List attributeLists = new ArrayList<Attribute>();
  491 + List<Attribute> attributeLists = new ArrayList<>();
492 492 ObjectMapper oMapper = new ObjectMapper();
493 493 Map<String, Object> map = oMapper.convertValue(params, ConcurrentHashMap.class);
494 494 map.forEach((k, v) -> {
... ... @@ -498,7 +498,7 @@ public class LwM2mTransportHandlerUtil {
498 498 ((Double) v).longValue() : v));
499 499 }
500 500 });
501   - return (Attribute[]) attributeLists.toArray(Attribute[]::new);
  501 + return attributeLists.toArray(Attribute[]::new);
502 502 }
503 503
504 504
... ...
... ... @@ -30,7 +30,6 @@ import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext;
30 30 import java.util.ArrayList;
31 31 import java.util.Base64;
32 32 import java.util.Collection;
33   -import java.util.Iterator;
34 33 import java.util.Map;
35 34 import java.util.Optional;
36 35
... ... @@ -51,7 +50,7 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
51 50 private final LwM2mTransportServerHelper helper;
52 51 private final LwM2mTransportContext context;
53 52
54   - private String getKeyIdVer(Integer objectId, String version) {
  53 + private String getKeyIdVer(Integer objectId, String version) {
55 54 return objectId != null ? objectId + LWM2M_SEPARATOR_KEY + ((version == null || version.isEmpty()) ? ObjectModel.DEFAULT_VERSION : version) : null;
56 55 }
57 56
... ... @@ -84,7 +83,7 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
84 83 return objectModel.resources.get(resourceId);
85 84 else
86 85 log.warn("TbResources (Object model) with id [{}/0/{}] not found on the server", objectId, resourceId);
87   - return null;
  86 + return null;
88 87 } catch (Exception e) {
89 88 log.error("", e);
90 89 return null;
... ... @@ -104,9 +103,7 @@ public class LwM2mVersionedModelProvider implements LwM2mModelProvider {
104 103 public Collection<ObjectModel> getObjectModels() {
105 104 Map<Integer, String> supportedObjects = this.registration.getSupportedObject();
106 105 Collection<ObjectModel> result = new ArrayList<>(supportedObjects.size());
107   - Iterator<Map.Entry<Integer, String>> i$ = supportedObjects.entrySet().iterator();
108   - while (i$.hasNext()) {
109   - Map.Entry<Integer, String> supportedObject = i$.next();
  106 + for (Map.Entry<Integer, String> supportedObject : supportedObjects.entrySet()) {
110 107 ObjectModel objectModel = this.getObjectModelDynamic(supportedObject.getKey(), supportedObject.getValue());
111 108 if (objectModel != null) {
112 109 result.add(objectModel);
... ...
... ... @@ -26,8 +26,8 @@ import org.eclipse.leshan.server.registration.Registration;
26 26 import org.eclipse.leshan.server.security.SecurityInfo;
27 27 import org.thingsboard.server.gen.transport.TransportProtos;
28 28 import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg;
29   -import org.thingsboard.server.transport.lwm2m.server.LwM2mQueuedRequest;
30 29 import org.thingsboard.server.transport.lwm2m.server.DefaultLwM2MTransportMsgHandler;
  30 +import org.thingsboard.server.transport.lwm2m.server.LwM2mQueuedRequest;
31 31 import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl;
32 32
33 33 import java.util.Collection;
... ... @@ -42,9 +42,9 @@ import java.util.concurrent.CopyOnWriteArrayList;
42 42 import java.util.stream.Collectors;
43 43
44 44 import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH;
45   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.TRANSPORT_DEFAULT_LWM2M_VERSION;
46   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromIdVerToObjectId;
47   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.getVerFromPathIdVerOrId;
  45 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.TRANSPORT_DEFAULT_LWM2M_VERSION;
  46 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromIdVerToObjectId;
  47 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.getVerFromPathIdVerOrId;
48 48
49 49 @Slf4j
50 50 @Data
... ...
... ... @@ -25,7 +25,7 @@ import org.thingsboard.server.queue.util.TbLwM2mTransportComponent;
25 25 import org.thingsboard.server.transport.lwm2m.secure.LwM2MSecurityMode;
26 26 import org.thingsboard.server.transport.lwm2m.secure.LwM2mCredentialsSecurityInfoValidator;
27 27 import org.thingsboard.server.transport.lwm2m.secure.ReadResultSecurityStore;
28   -import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil;
  28 +import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil;
29 29
30 30 import java.util.Arrays;
31 31 import java.util.Map;
... ... @@ -34,7 +34,7 @@ import java.util.UUID;
34 34 import java.util.concurrent.ConcurrentHashMap;
35 35
36 36 import static org.thingsboard.server.transport.lwm2m.secure.LwM2MSecurityMode.NO_SEC;
37   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.convertPathFromObjectIdToIdVer;
  37 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertPathFromObjectIdToIdVer;
38 38
39 39 @Service
40 40 @TbLwM2mTransportComponent
... ... @@ -118,7 +118,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
118 118 */
119 119 @Override
120 120 public LwM2mClient addLwM2mClientToSession(String identity) {
121   - ReadResultSecurityStore store = lwM2MCredentialsSecurityInfoValidator.createAndValidateCredentialsSecurityInfo(identity, LwM2mTransportHandlerUtil.LwM2mTypeServer.CLIENT);
  121 + ReadResultSecurityStore store = lwM2MCredentialsSecurityInfoValidator.createAndValidateCredentialsSecurityInfo(identity, LwM2mTransportUtil.LwM2mTypeServer.CLIENT);
122 122 if (store.getSecurityMode() < LwM2MSecurityMode.DEFAULT_MODE.code) {
123 123 UUID profileUuid = (store.getDeviceProfile() != null && addUpdateProfileParameters(store.getDeviceProfile())) ? store.getDeviceProfile().getUuidId() : null;
124 124 LwM2mClient client;
... ... @@ -165,7 +165,7 @@ public class LwM2mClientContextImpl implements LwM2mClientContext {
165 165
166 166 @Override
167 167 public boolean addUpdateProfileParameters(DeviceProfile deviceProfile) {
168   - LwM2mClientProfile lwM2MClientProfile = LwM2mTransportHandlerUtil.getLwM2MClientProfileFromThingsboard(deviceProfile);
  168 + LwM2mClientProfile lwM2MClientProfile = LwM2mTransportUtil.getLwM2MClientProfileFromThingsboard(deviceProfile);
169 169 if (lwM2MClientProfile != null) {
170 170 profiles.put(deviceProfile.getUuidId(), lwM2MClientProfile);
171 171 return true;
... ...
... ... @@ -21,11 +21,11 @@ import org.eclipse.leshan.core.request.ContentFormat;
21 21 import org.eclipse.leshan.server.registration.Registration;
22 22 import org.thingsboard.server.gen.transport.TransportProtos;
23 23 import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto;
24   -import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.LwM2mTypeOper;
  24 +import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LwM2mTypeOper;
25 25
26 26 import java.util.concurrent.ConcurrentHashMap;
27 27
28   -import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportHandlerUtil.validPathIdVer;
  28 +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.validPathIdVer;
29 29
30 30 @Data
31 31 public class Lwm2mClientRpcRequest {
... ... @@ -43,7 +43,7 @@ public class Lwm2mClientRpcRequest {
43 43
44 44 private LwM2mTypeOper typeOper;
45 45 private String targetIdVer;
46   - private String contentFormatName;
  46 + private String contentFormatName;
47 47 private long timeoutInMs;
48 48 private Object value;
49 49 private ConcurrentHashMap<String, Object> params;
... ... @@ -54,19 +54,19 @@ public class Lwm2mClientRpcRequest {
54 54 private String infoMsg;
55 55 private String responseCode;
56 56
57   - public void setValidTypeOper (String typeOper){
  57 + public void setValidTypeOper(String typeOper) {
58 58 try {
59 59 this.typeOper = LwM2mTypeOper.fromLwLwM2mTypeOper(typeOper);
60 60 } catch (Exception e) {
61 61 this.errorMsg = this.methodKey + " - " + typeOper + " is not valid.";
62 62 }
63 63 }
64   - public void setValidContentFormatName (JsonObject rpcRequest){
  64 +
  65 + public void setValidContentFormatName(JsonObject rpcRequest) {
65 66 try {
66 67 if (ContentFormat.fromName(rpcRequest.get(this.contentFormatNameKey).getAsString()) != null) {
67 68 this.contentFormatName = rpcRequest.get(this.contentFormatNameKey).getAsString();
68   - }
69   - else {
  69 + } else {
70 70 this.errorMsg = this.contentFormatNameKey + " - " + rpcRequest.get(this.contentFormatNameKey).getAsString() + " is not valid.";
71 71 }
72 72 } catch (Exception e) {
... ... @@ -74,14 +74,14 @@ public class Lwm2mClientRpcRequest {
74 74 }
75 75 }
76 76
77   - public void setValidTargetIdVerKey (JsonObject rpcRequest, Registration registration){
  77 + public void setValidTargetIdVerKey(JsonObject rpcRequest, Registration registration) {
78 78 if (rpcRequest.has(this.targetIdVerKey)) {
79 79 String targetIdVerStr = rpcRequest.get(targetIdVerKey).getAsString();
80 80 // targetIdVer without ver - ok
81 81 try {
82 82 // targetIdVer with/without ver - ok
83 83 this.targetIdVer = validPathIdVer(targetIdVerStr, registration);
84   - if (this.targetIdVer != null){
  84 + if (this.targetIdVer != null) {
85 85 this.infoMsg = String.format("Changed by: pathIdVer - %s", this.targetIdVer);
86 86 }
87 87 } catch (Exception e) {
... ... @@ -97,11 +97,9 @@ public class Lwm2mClientRpcRequest {
97 97 payloadResp.addProperty(this.resultKey, this.responseCode);
98 98 if (this.errorMsg != null) {
99 99 payloadResp.addProperty(this.errorKey, this.errorMsg);
100   - }
101   - else if (this.valueMsg != null) {
  100 + } else if (this.valueMsg != null) {
102 101 payloadResp.addProperty(this.valueKey, this.valueMsg);
103   - }
104   - else if (this.infoMsg != null) {
  102 + } else if (this.infoMsg != null) {
105 103 payloadResp.addProperty(this.infoKey, this.infoMsg);
106 104 }
107 105 return TransportProtos.ToDeviceRpcResponseMsg.newBuilder()
... ...
... ... @@ -18,6 +18,7 @@ package org.thingsboard.server.transport.lwm2m.server.client;
18 18 import lombok.Data;
19 19 import org.eclipse.leshan.core.model.ObjectModel;
20 20 import org.eclipse.leshan.core.node.LwM2mObjectInstance;
  21 +
21 22 import java.util.Map;
22 23
23 24 @Data
... ...
... ... @@ -18,12 +18,12 @@ package org.thingsboard.server.transport.lwm2m.server.store;
18 18 import org.eclipse.californium.core.coap.Token;
19 19 import org.eclipse.californium.core.observe.ObservationStoreException;
20 20 import org.eclipse.californium.elements.EndpointContext;
21   -import org.eclipse.leshan.core.observation.Observation;
22   -import org.eclipse.leshan.core.util.NamedThreadFactory;
23   -import org.eclipse.leshan.core.util.Validate;
24 21 import org.eclipse.leshan.core.Destroyable;
25 22 import org.eclipse.leshan.core.Startable;
26 23 import org.eclipse.leshan.core.Stoppable;
  24 +import org.eclipse.leshan.core.observation.Observation;
  25 +import org.eclipse.leshan.core.util.NamedThreadFactory;
  26 +import org.eclipse.leshan.core.util.Validate;
27 27 import org.eclipse.leshan.server.californium.observation.ObserveUtil;
28 28 import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore;
29 29 import org.eclipse.leshan.server.redis.JedisLock;
... ...
... ... @@ -21,7 +21,6 @@ import org.eclipse.leshan.server.security.NonUniqueSecurityInfoException;
21 21 import org.eclipse.leshan.server.security.SecurityInfo;
22 22 import org.eclipse.leshan.server.security.SecurityStoreListener;
23 23 import org.springframework.data.redis.connection.RedisConnectionFactory;
24   -import org.springframework.stereotype.Service;
25 24 import redis.clients.jedis.Jedis;
26 25 import redis.clients.jedis.ScanParams;
27 26 import redis.clients.jedis.ScanResult;
... ...