Commit f5f531e716b8f168b1e03617a22c42fa6fc59e1a
Merge branch 'develop/3.2' of github.com:thingsboard/thingsboard into develop/3.2
Showing
17 changed files
with
289 additions
and
119 deletions
... | ... | @@ -15,30 +15,16 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.common.data.device.profile; |
17 | 17 | |
18 | -import com.fasterxml.jackson.annotation.JsonAnyGetter; | |
19 | -import com.fasterxml.jackson.annotation.JsonAnySetter; | |
20 | -import com.fasterxml.jackson.annotation.JsonIgnore; | |
21 | 18 | import lombok.Data; |
22 | 19 | import org.thingsboard.server.common.data.DeviceTransportType; |
23 | 20 | |
24 | -import java.util.HashMap; | |
25 | -import java.util.Map; | |
26 | - | |
27 | 21 | @Data |
28 | 22 | public class MqttDeviceProfileTransportConfiguration implements DeviceProfileTransportConfiguration { |
29 | 23 | |
30 | - @JsonIgnore | |
31 | - private Map<String, Object> properties = new HashMap<>(); | |
32 | - | |
33 | - @JsonAnyGetter | |
34 | - public Map<String, Object> properties() { | |
35 | - return this.properties; | |
36 | - } | |
37 | - | |
38 | - @JsonAnySetter | |
39 | - public void put(String name, Object value) { | |
40 | - this.properties.put(name, value); | |
41 | - } | |
24 | + private String deviceTelemetryTopic = MqttTopics.DEVICE_TELEMETRY_TOPIC; | |
25 | + private String deviceAttributesTopic = MqttTopics.DEVICE_ATTRIBUTES_TOPIC; | |
26 | + private String deviceRpcRequestTopic = MqttTopics.DEVICE_RPC_REQUESTS_TOPIC; | |
27 | + private String deviceRpcResponseTopic = MqttTopics.DEVICE_RPC_RESPONSE_TOPIC; | |
42 | 28 | |
43 | 29 | @Override |
44 | 30 | public DeviceTransportType getType() { | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/device/profile/MqttTopics.java
renamed from
common/transport/mqtt/src/main/java/org/thingsboard/server/transport/mqtt/MqttTopics.java
... | ... | @@ -13,7 +13,7 @@ |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | -package org.thingsboard.server.transport.mqtt; | |
16 | +package org.thingsboard.server.common.data.device.profile; | |
17 | 17 | |
18 | 18 | /** |
19 | 19 | * Created by ashvayka on 19.01.17. | ... | ... |
common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java
... | ... | @@ -32,6 +32,8 @@ import org.thingsboard.server.common.transport.TransportContext; |
32 | 32 | import org.thingsboard.server.common.transport.TransportService; |
33 | 33 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
34 | 34 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
35 | +import org.thingsboard.server.common.transport.auth.SessionInfoCreator; | |
36 | +import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; | |
35 | 37 | import org.thingsboard.server.gen.transport.TransportProtos; |
36 | 38 | |
37 | 39 | import java.lang.reflect.Field; |
... | ... | @@ -295,7 +297,7 @@ public class CoapTransportResource extends CoapResource { |
295 | 297 | return this; |
296 | 298 | } |
297 | 299 | |
298 | - private static class DeviceAuthCallback implements TransportServiceCallback<TransportProtos.ValidateDeviceCredentialsResponseMsg> { | |
300 | + private static class DeviceAuthCallback implements TransportServiceCallback<ValidateDeviceCredentialsResponse> { | |
299 | 301 | private final TransportContext transportContext; |
300 | 302 | private final CoapExchange exchange; |
301 | 303 | private final Consumer<TransportProtos.SessionInfoProto> onSuccess; |
... | ... | @@ -307,22 +309,9 @@ public class CoapTransportResource extends CoapResource { |
307 | 309 | } |
308 | 310 | |
309 | 311 | @Override |
310 | - public void onSuccess(TransportProtos.ValidateDeviceCredentialsResponseMsg msg) { | |
312 | + public void onSuccess(ValidateDeviceCredentialsResponse msg) { | |
311 | 313 | if (msg.hasDeviceInfo()) { |
312 | - UUID sessionId = UUID.randomUUID(); | |
313 | - TransportProtos.DeviceInfoProto deviceInfoProto = msg.getDeviceInfo(); | |
314 | - TransportProtos.SessionInfoProto sessionInfo = TransportProtos.SessionInfoProto.newBuilder() | |
315 | - .setNodeId(transportContext.getNodeId()) | |
316 | - .setTenantIdMSB(deviceInfoProto.getTenantIdMSB()) | |
317 | - .setTenantIdLSB(deviceInfoProto.getTenantIdLSB()) | |
318 | - .setDeviceIdMSB(deviceInfoProto.getDeviceIdMSB()) | |
319 | - .setDeviceIdLSB(deviceInfoProto.getDeviceIdLSB()) | |
320 | - .setSessionIdMSB(sessionId.getMostSignificantBits()) | |
321 | - .setSessionIdLSB(sessionId.getLeastSignificantBits()) | |
322 | - .setDeviceName(msg.getDeviceInfo().getDeviceName()) | |
323 | - .setDeviceType(msg.getDeviceInfo().getDeviceType()) | |
324 | - .build(); | |
325 | - onSuccess.accept(sessionInfo); | |
314 | + onSuccess.accept(SessionInfoCreator.create(msg, transportContext, UUID.randomUUID())); | |
326 | 315 | } else { |
327 | 316 | exchange.respond(ResponseCode.UNAUTHORIZED); |
328 | 317 | } | ... | ... |
... | ... | @@ -36,6 +36,8 @@ import org.thingsboard.server.common.transport.TransportContext; |
36 | 36 | import org.thingsboard.server.common.transport.TransportService; |
37 | 37 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
38 | 38 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; |
39 | +import org.thingsboard.server.common.transport.auth.SessionInfoCreator; | |
40 | +import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; | |
39 | 41 | import org.thingsboard.server.gen.transport.TransportProtos; |
40 | 42 | import org.thingsboard.server.gen.transport.TransportProtos.AttributeUpdateNotificationMsg; |
41 | 43 | import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; |
... | ... | @@ -200,7 +202,7 @@ public class DeviceApiController { |
200 | 202 | return responseWriter; |
201 | 203 | } |
202 | 204 | |
203 | - private static class DeviceAuthCallback implements TransportServiceCallback<ValidateDeviceCredentialsResponseMsg> { | |
205 | + private static class DeviceAuthCallback implements TransportServiceCallback<ValidateDeviceCredentialsResponse> { | |
204 | 206 | private final TransportContext transportContext; |
205 | 207 | private final DeferredResult<ResponseEntity> responseWriter; |
206 | 208 | private final Consumer<SessionInfoProto> onSuccess; |
... | ... | @@ -212,22 +214,9 @@ public class DeviceApiController { |
212 | 214 | } |
213 | 215 | |
214 | 216 | @Override |
215 | - public void onSuccess(ValidateDeviceCredentialsResponseMsg msg) { | |
217 | + public void onSuccess(ValidateDeviceCredentialsResponse msg) { | |
216 | 218 | if (msg.hasDeviceInfo()) { |
217 | - UUID sessionId = UUID.randomUUID(); | |
218 | - DeviceInfoProto deviceInfoProto = msg.getDeviceInfo(); | |
219 | - SessionInfoProto sessionInfo = SessionInfoProto.newBuilder() | |
220 | - .setNodeId(transportContext.getNodeId()) | |
221 | - .setTenantIdMSB(deviceInfoProto.getTenantIdMSB()) | |
222 | - .setTenantIdLSB(deviceInfoProto.getTenantIdLSB()) | |
223 | - .setDeviceIdMSB(deviceInfoProto.getDeviceIdMSB()) | |
224 | - .setDeviceIdLSB(deviceInfoProto.getDeviceIdLSB()) | |
225 | - .setSessionIdMSB(sessionId.getMostSignificantBits()) | |
226 | - .setSessionIdLSB(sessionId.getLeastSignificantBits()) | |
227 | - .setDeviceName(msg.getDeviceInfo().getDeviceName()) | |
228 | - .setDeviceType(msg.getDeviceInfo().getDeviceType()) | |
229 | - .build(); | |
230 | - onSuccess.accept(sessionInfo); | |
219 | + onSuccess.accept(SessionInfoCreator.create(msg, transportContext, UUID.randomUUID())); | |
231 | 220 | } else { |
232 | 221 | responseWriter.setResult(new ResponseEntity<>(HttpStatus.UNAUTHORIZED)); |
233 | 222 | } | ... | ... |
... | ... | @@ -27,6 +27,7 @@ import org.springframework.util.StringUtils; |
27 | 27 | import org.thingsboard.server.common.msg.EncryptionUtil; |
28 | 28 | import org.thingsboard.server.common.transport.TransportService; |
29 | 29 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
30 | +import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; | |
30 | 31 | import org.thingsboard.server.gen.transport.TransportProtos; |
31 | 32 | import org.thingsboard.server.transport.mqtt.util.SslUtil; |
32 | 33 | |
... | ... | @@ -157,11 +158,11 @@ public class MqttSslHandlerProvider { |
157 | 158 | final String[] credentialsBodyHolder = new String[1]; |
158 | 159 | CountDownLatch latch = new CountDownLatch(1); |
159 | 160 | transportService.process(TransportProtos.ValidateDeviceX509CertRequestMsg.newBuilder().setHash(sha3Hash).build(), |
160 | - new TransportServiceCallback<TransportProtos.ValidateDeviceCredentialsResponseMsg>() { | |
161 | + new TransportServiceCallback<ValidateDeviceCredentialsResponse>() { | |
161 | 162 | @Override |
162 | - public void onSuccess(TransportProtos.ValidateDeviceCredentialsResponseMsg msg) { | |
163 | - if (!StringUtils.isEmpty(msg.getCredentialsBody())) { | |
164 | - credentialsBodyHolder[0] = msg.getCredentialsBody(); | |
163 | + public void onSuccess(ValidateDeviceCredentialsResponse msg) { | |
164 | + if (!StringUtils.isEmpty(msg.getCredentials())) { | |
165 | + credentialsBodyHolder[0] = msg.getCredentials(); | |
165 | 166 | } |
166 | 167 | latch.countDown(); |
167 | 168 | } | ... | ... |
... | ... | @@ -39,17 +39,19 @@ import io.netty.util.concurrent.Future; |
39 | 39 | import io.netty.util.concurrent.GenericFutureListener; |
40 | 40 | import lombok.extern.slf4j.Slf4j; |
41 | 41 | import org.springframework.util.StringUtils; |
42 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | |
42 | 43 | import org.thingsboard.server.common.msg.EncryptionUtil; |
43 | 44 | import org.thingsboard.server.common.transport.SessionMsgListener; |
44 | 45 | import org.thingsboard.server.common.transport.TransportService; |
45 | 46 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
46 | 47 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
48 | +import org.thingsboard.server.common.transport.auth.SessionInfoCreator; | |
49 | +import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; | |
50 | +import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; | |
47 | 51 | import org.thingsboard.server.common.transport.service.DefaultTransportService; |
48 | 52 | import org.thingsboard.server.gen.transport.TransportProtos; |
49 | -import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; | |
50 | 53 | import org.thingsboard.server.gen.transport.TransportProtos.SessionEvent; |
51 | 54 | import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto; |
52 | -import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceCredentialsResponseMsg; | |
53 | 55 | import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceTokenRequestMsg; |
54 | 56 | import org.thingsboard.server.gen.transport.TransportProtos.ValidateDeviceX509CertRequestMsg; |
55 | 57 | import org.thingsboard.server.transport.mqtt.adaptors.MqttTransportAdaptor; |
... | ... | @@ -365,9 +367,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
365 | 367 | ctx.close(); |
366 | 368 | } else { |
367 | 369 | transportService.process(ValidateDeviceTokenRequestMsg.newBuilder().setToken(userName).build(), |
368 | - new TransportServiceCallback<ValidateDeviceCredentialsResponseMsg>() { | |
370 | + new TransportServiceCallback<ValidateDeviceCredentialsResponse>() { | |
369 | 371 | @Override |
370 | - public void onSuccess(ValidateDeviceCredentialsResponseMsg msg) { | |
372 | + public void onSuccess(ValidateDeviceCredentialsResponse msg) { | |
371 | 373 | onValidateDeviceResponse(msg, ctx); |
372 | 374 | } |
373 | 375 | |
... | ... | @@ -386,9 +388,9 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
386 | 388 | String strCert = SslUtil.getX509CertificateString(cert); |
387 | 389 | String sha3Hash = EncryptionUtil.getSha3Hash(strCert); |
388 | 390 | transportService.process(ValidateDeviceX509CertRequestMsg.newBuilder().setHash(sha3Hash).build(), |
389 | - new TransportServiceCallback<ValidateDeviceCredentialsResponseMsg>() { | |
391 | + new TransportServiceCallback<ValidateDeviceCredentialsResponse>() { | |
390 | 392 | @Override |
391 | - public void onSuccess(ValidateDeviceCredentialsResponseMsg msg) { | |
393 | + public void onSuccess(ValidateDeviceCredentialsResponse msg) { | |
392 | 394 | onValidateDeviceResponse(msg, ctx); |
393 | 395 | } |
394 | 396 | |
... | ... | @@ -474,7 +476,7 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
474 | 476 | } |
475 | 477 | |
476 | 478 | private void checkGatewaySession() { |
477 | - DeviceInfoProto device = deviceSessionCtx.getDeviceInfo(); | |
479 | + TransportDeviceInfo device = deviceSessionCtx.getDeviceInfo(); | |
478 | 480 | try { |
479 | 481 | JsonNode infoNode = context.getMapper().readTree(device.getAdditionalInfo()); |
480 | 482 | if (infoNode != null) { |
... | ... | @@ -504,25 +506,14 @@ public class MqttTransportHandler extends ChannelInboundHandlerAdapter implement |
504 | 506 | } |
505 | 507 | } |
506 | 508 | |
507 | - private void onValidateDeviceResponse(ValidateDeviceCredentialsResponseMsg msg, ChannelHandlerContext ctx) { | |
509 | + private void onValidateDeviceResponse(ValidateDeviceCredentialsResponse msg, ChannelHandlerContext ctx) { | |
508 | 510 | if (!msg.hasDeviceInfo()) { |
509 | 511 | ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_REFUSED_NOT_AUTHORIZED)); |
510 | 512 | ctx.close(); |
511 | 513 | } else { |
512 | 514 | deviceSessionCtx.setDeviceInfo(msg.getDeviceInfo()); |
513 | - sessionInfo = SessionInfoProto.newBuilder() | |
514 | - .setNodeId(context.getNodeId()) | |
515 | - .setSessionIdMSB(sessionId.getMostSignificantBits()) | |
516 | - .setSessionIdLSB(sessionId.getLeastSignificantBits()) | |
517 | - .setDeviceIdMSB(msg.getDeviceInfo().getDeviceIdMSB()) | |
518 | - .setDeviceIdLSB(msg.getDeviceInfo().getDeviceIdLSB()) | |
519 | - .setTenantIdMSB(msg.getDeviceInfo().getTenantIdMSB()) | |
520 | - .setTenantIdLSB(msg.getDeviceInfo().getTenantIdLSB()) | |
521 | - .setDeviceName(msg.getDeviceInfo().getDeviceName()) | |
522 | - .setDeviceType(msg.getDeviceInfo().getDeviceType()) | |
523 | - .setDeviceProfileIdMSB(msg.getDeviceInfo().getDeviceProfileIdMSB()) | |
524 | - .setDeviceProfileIdLSB(msg.getDeviceInfo().getDeviceProfileIdLSB()) | |
525 | - .build(); | |
515 | +// deviceSessionCtx.setProfile(msg.getDeviceProfile()); | |
516 | + sessionInfo = SessionInfoCreator.create(msg, context, sessionId); | |
526 | 517 | transportService.process(sessionInfo, DefaultTransportService.getSessionEventMsg(SessionEvent.OPEN), new TransportServiceCallback<Void>() { |
527 | 518 | @Override |
528 | 519 | public void onSuccess(Void msg) { | ... | ... |
... | ... | @@ -34,7 +34,7 @@ import org.springframework.util.StringUtils; |
34 | 34 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
35 | 35 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; |
36 | 36 | import org.thingsboard.server.gen.transport.TransportProtos; |
37 | -import org.thingsboard.server.transport.mqtt.MqttTopics; | |
37 | +import org.thingsboard.server.common.data.device.profile.MqttTopics; | |
38 | 38 | import org.thingsboard.server.transport.mqtt.session.MqttDeviceAwareSessionContext; |
39 | 39 | |
40 | 40 | import java.nio.charset.Charset; | ... | ... |
... | ... | @@ -17,6 +17,7 @@ package org.thingsboard.server.transport.mqtt.session; |
17 | 17 | |
18 | 18 | import lombok.extern.slf4j.Slf4j; |
19 | 19 | import org.thingsboard.server.common.transport.SessionMsgListener; |
20 | +import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; | |
20 | 21 | import org.thingsboard.server.gen.transport.TransportProtos; |
21 | 22 | import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; |
22 | 23 | import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto; |
... | ... | @@ -33,21 +34,23 @@ public class GatewayDeviceSessionCtx extends MqttDeviceAwareSessionContext imple |
33 | 34 | private final GatewaySessionHandler parent; |
34 | 35 | private final SessionInfoProto sessionInfo; |
35 | 36 | |
36 | - public GatewayDeviceSessionCtx(GatewaySessionHandler parent, DeviceInfoProto deviceInfo, ConcurrentMap<MqttTopicMatcher, Integer> mqttQoSMap) { | |
37 | + public GatewayDeviceSessionCtx(GatewaySessionHandler parent, TransportDeviceInfo deviceInfo, ConcurrentMap<MqttTopicMatcher, Integer> mqttQoSMap) { | |
37 | 38 | super(UUID.randomUUID(), mqttQoSMap); |
38 | 39 | this.parent = parent; |
39 | 40 | this.sessionInfo = SessionInfoProto.newBuilder() |
40 | 41 | .setNodeId(parent.getNodeId()) |
41 | 42 | .setSessionIdMSB(sessionId.getMostSignificantBits()) |
42 | 43 | .setSessionIdLSB(sessionId.getLeastSignificantBits()) |
43 | - .setDeviceIdMSB(deviceInfo.getDeviceIdMSB()) | |
44 | - .setDeviceIdLSB(deviceInfo.getDeviceIdLSB()) | |
45 | - .setTenantIdMSB(deviceInfo.getTenantIdMSB()) | |
46 | - .setTenantIdLSB(deviceInfo.getTenantIdLSB()) | |
44 | + .setDeviceIdMSB(deviceInfo.getDeviceId().getId().getMostSignificantBits()) | |
45 | + .setDeviceIdLSB(deviceInfo.getDeviceId().getId().getLeastSignificantBits()) | |
46 | + .setTenantIdMSB(deviceInfo.getTenantId().getId().getMostSignificantBits()) | |
47 | + .setTenantIdLSB(deviceInfo.getTenantId().getId().getLeastSignificantBits()) | |
47 | 48 | .setDeviceName(deviceInfo.getDeviceName()) |
48 | 49 | .setDeviceType(deviceInfo.getDeviceType()) |
49 | 50 | .setGwSessionIdMSB(parent.getSessionId().getMostSignificantBits()) |
50 | 51 | .setGwSessionIdLSB(parent.getSessionId().getLeastSignificantBits()) |
52 | + .setDeviceProfileIdMSB(deviceInfo.getDeviceProfileId().getId().getMostSignificantBits()) | |
53 | + .setDeviceProfileIdLSB(deviceInfo.getDeviceProfileId().getId().getLeastSignificantBits()) | |
51 | 54 | .build(); |
52 | 55 | setDeviceInfo(deviceInfo); |
53 | 56 | } | ... | ... |
... | ... | @@ -35,6 +35,8 @@ import org.thingsboard.server.common.transport.TransportService; |
35 | 35 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
36 | 36 | import org.thingsboard.server.common.transport.adaptor.AdaptorException; |
37 | 37 | import org.thingsboard.server.common.transport.adaptor.JsonConverter; |
38 | +import org.thingsboard.server.common.transport.auth.GetOrCreateDeviceFromGatewayResponse; | |
39 | +import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; | |
38 | 40 | import org.thingsboard.server.common.transport.service.DefaultTransportService; |
39 | 41 | import org.thingsboard.server.gen.transport.TransportProtos; |
40 | 42 | import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; |
... | ... | @@ -69,7 +71,7 @@ public class GatewaySessionHandler { |
69 | 71 | |
70 | 72 | private final MqttTransportContext context; |
71 | 73 | private final TransportService transportService; |
72 | - private final DeviceInfoProto gateway; | |
74 | + private final TransportDeviceInfo gateway; | |
73 | 75 | private final UUID sessionId; |
74 | 76 | private final ConcurrentMap<String, Lock> deviceCreationLockMap; |
75 | 77 | private final ConcurrentMap<String, GatewayDeviceSessionCtx> devices; |
... | ... | @@ -140,11 +142,11 @@ public class GatewaySessionHandler { |
140 | 142 | transportService.process(GetOrCreateDeviceFromGatewayRequestMsg.newBuilder() |
141 | 143 | .setDeviceName(deviceName) |
142 | 144 | .setDeviceType(deviceType) |
143 | - .setGatewayIdMSB(gateway.getDeviceIdMSB()) | |
144 | - .setGatewayIdLSB(gateway.getDeviceIdLSB()).build(), | |
145 | - new TransportServiceCallback<GetOrCreateDeviceFromGatewayResponseMsg>() { | |
145 | + .setGatewayIdMSB(gateway.getDeviceId().getId().getMostSignificantBits()) | |
146 | + .setGatewayIdLSB(gateway.getDeviceId().getId().getLeastSignificantBits()).build(), | |
147 | + new TransportServiceCallback<GetOrCreateDeviceFromGatewayResponse>() { | |
146 | 148 | @Override |
147 | - public void onSuccess(GetOrCreateDeviceFromGatewayResponseMsg msg) { | |
149 | + public void onSuccess(GetOrCreateDeviceFromGatewayResponse msg) { | |
148 | 150 | GatewayDeviceSessionCtx deviceSessionCtx = new GatewayDeviceSessionCtx(GatewaySessionHandler.this, msg.getDeviceInfo(), mqttQoSMap); |
149 | 151 | if (devices.putIfAbsent(deviceName, deviceSessionCtx) == null) { |
150 | 152 | log.trace("[{}] First got or created device [{}], type [{}] for the gateway session", sessionId, deviceName, deviceType); |
... | ... | @@ -218,8 +220,7 @@ public class GatewaySessionHandler { |
218 | 220 | TransportProtos.PostTelemetryMsg postTelemetryMsg = JsonConverter.convertToTelemetryProto(deviceEntry.getValue().getAsJsonArray()); |
219 | 221 | transportService.process(deviceCtx.getSessionInfo(), postTelemetryMsg, getPubAckCallback(channel, deviceName, msgId, postTelemetryMsg)); |
220 | 222 | } catch (Throwable e) { |
221 | - UUID gatewayId = new UUID(gateway.getDeviceIdMSB(), gateway.getDeviceIdLSB()); | |
222 | - log.warn("[{}][{}] Failed to convert telemetry: {}", gatewayId, deviceName, deviceEntry.getValue(), e); | |
223 | + log.warn("[{}][{}] Failed to convert telemetry: {}", gateway.getDeviceId(), deviceName, deviceEntry.getValue(), e); | |
223 | 224 | } |
224 | 225 | } |
225 | 226 | |
... | ... | @@ -253,8 +254,7 @@ public class GatewaySessionHandler { |
253 | 254 | TransportProtos.ClaimDeviceMsg claimDeviceMsg = JsonConverter.convertToClaimDeviceProto(deviceId, deviceEntry.getValue()); |
254 | 255 | transportService.process(deviceCtx.getSessionInfo(), claimDeviceMsg, getPubAckCallback(channel, deviceName, msgId, claimDeviceMsg)); |
255 | 256 | } catch (Throwable e) { |
256 | - UUID gatewayId = new UUID(gateway.getDeviceIdMSB(), gateway.getDeviceIdLSB()); | |
257 | - log.warn("[{}][{}] Failed to convert claim message: {}", gatewayId, deviceName, deviceEntry.getValue(), e); | |
257 | + log.warn("[{}][{}] Failed to convert claim message: {}", gateway.getDeviceId(), deviceName, deviceEntry.getValue(), e); | |
258 | 258 | } |
259 | 259 | } |
260 | 260 | ... | ... |
... | ... | @@ -17,6 +17,8 @@ package org.thingsboard.server.common.transport; |
17 | 17 | |
18 | 18 | import org.thingsboard.server.common.data.DeviceProfile; |
19 | 19 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
20 | +import org.thingsboard.server.common.transport.auth.GetOrCreateDeviceFromGatewayResponse; | |
21 | +import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; | |
20 | 22 | import org.thingsboard.server.gen.transport.TransportProtos.ClaimDeviceMsg; |
21 | 23 | import org.thingsboard.server.gen.transport.TransportProtos.GetAttributeRequestMsg; |
22 | 24 | import org.thingsboard.server.gen.transport.TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg; |
... | ... | @@ -44,13 +46,13 @@ public interface TransportService { |
44 | 46 | GetTenantRoutingInfoResponseMsg getRoutingInfo(GetTenantRoutingInfoRequestMsg msg); |
45 | 47 | |
46 | 48 | void process(ValidateDeviceTokenRequestMsg msg, |
47 | - TransportServiceCallback<ValidateDeviceCredentialsResponseMsg> callback); | |
49 | + TransportServiceCallback<ValidateDeviceCredentialsResponse> callback); | |
48 | 50 | |
49 | 51 | void process(ValidateDeviceX509CertRequestMsg msg, |
50 | - TransportServiceCallback<ValidateDeviceCredentialsResponseMsg> callback); | |
52 | + TransportServiceCallback<ValidateDeviceCredentialsResponse> callback); | |
51 | 53 | |
52 | 54 | void process(GetOrCreateDeviceFromGatewayRequestMsg msg, |
53 | - TransportServiceCallback<GetOrCreateDeviceFromGatewayResponseMsg> callback); | |
55 | + TransportServiceCallback<GetOrCreateDeviceFromGatewayResponse> callback); | |
54 | 56 | |
55 | 57 | void getDeviceProfile(DeviceProfileId deviceProfileId, TransportServiceCallback<DeviceProfile> callback); |
56 | 58 | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.transport.auth; | |
17 | + | |
18 | +import org.thingsboard.server.common.data.DeviceProfile; | |
19 | + | |
20 | +public interface DeviceProfileAware { | |
21 | + | |
22 | + DeviceProfile getDeviceProfile(); | |
23 | + | |
24 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.transport.auth; | |
17 | + | |
18 | +import lombok.Builder; | |
19 | +import lombok.Data; | |
20 | +import org.thingsboard.server.common.data.DeviceProfile; | |
21 | + | |
22 | +@Data | |
23 | +@Builder | |
24 | +public class GetOrCreateDeviceFromGatewayResponse implements DeviceProfileAware { | |
25 | + | |
26 | + private TransportDeviceInfo deviceInfo; | |
27 | + private DeviceProfile deviceProfile; | |
28 | + | |
29 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.transport.auth; | |
17 | + | |
18 | +import org.thingsboard.server.common.transport.TransportContext; | |
19 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
20 | + | |
21 | +import java.util.UUID; | |
22 | + | |
23 | +public class SessionInfoCreator { | |
24 | + | |
25 | + public static TransportProtos.SessionInfoProto create(ValidateDeviceCredentialsResponse msg, TransportContext context, UUID sessionId) { | |
26 | + return TransportProtos.SessionInfoProto.newBuilder() | |
27 | + .setNodeId(context.getNodeId()) | |
28 | + .setSessionIdMSB(sessionId.getMostSignificantBits()) | |
29 | + .setSessionIdLSB(sessionId.getLeastSignificantBits()) | |
30 | + .setDeviceIdMSB(msg.getDeviceInfo().getDeviceId().getId().getMostSignificantBits()) | |
31 | + .setDeviceIdLSB(msg.getDeviceInfo().getDeviceId().getId().getLeastSignificantBits()) | |
32 | + .setTenantIdMSB(msg.getDeviceInfo().getTenantId().getId().getMostSignificantBits()) | |
33 | + .setTenantIdLSB(msg.getDeviceInfo().getTenantId().getId().getLeastSignificantBits()) | |
34 | + .setDeviceName(msg.getDeviceInfo().getDeviceName()) | |
35 | + .setDeviceType(msg.getDeviceInfo().getDeviceType()) | |
36 | + .setDeviceProfileIdMSB(msg.getDeviceInfo().getDeviceProfileId().getId().getMostSignificantBits()) | |
37 | + .setDeviceProfileIdLSB(msg.getDeviceInfo().getDeviceProfileId().getId().getLeastSignificantBits()) | |
38 | + .build(); | |
39 | + } | |
40 | + | |
41 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.transport.auth; | |
17 | + | |
18 | +import lombok.Data; | |
19 | +import org.thingsboard.server.common.data.id.DeviceId; | |
20 | +import org.thingsboard.server.common.data.id.DeviceProfileId; | |
21 | +import org.thingsboard.server.common.data.id.TenantId; | |
22 | + | |
23 | +@Data | |
24 | +public class TransportDeviceInfo { | |
25 | + | |
26 | + private TenantId tenantId; | |
27 | + private DeviceProfileId deviceProfileId; | |
28 | + private DeviceId deviceId; | |
29 | + private String deviceName; | |
30 | + private String deviceType; | |
31 | + private String additionalInfo; | |
32 | + | |
33 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 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.common.transport.auth; | |
17 | + | |
18 | +import lombok.Builder; | |
19 | +import lombok.Data; | |
20 | +import org.thingsboard.server.common.data.DeviceProfile; | |
21 | + | |
22 | +@Data | |
23 | +@Builder | |
24 | +public class ValidateDeviceCredentialsResponse implements DeviceProfileAware { | |
25 | + | |
26 | + private final TransportDeviceInfo deviceInfo; | |
27 | + private final DeviceProfile deviceProfile; | |
28 | + private final String credentials; | |
29 | + | |
30 | + public boolean hasDeviceInfo() { | |
31 | + return deviceInfo != null; | |
32 | + } | |
33 | +} | ... | ... |
... | ... | @@ -17,6 +17,7 @@ package org.thingsboard.server.common.transport.service; |
17 | 17 | |
18 | 18 | import com.google.common.util.concurrent.Futures; |
19 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
20 | +import com.google.common.util.concurrent.MoreExecutors; | |
20 | 21 | import com.google.gson.Gson; |
21 | 22 | import com.google.gson.JsonObject; |
22 | 23 | import com.google.protobuf.ByteString; |
... | ... | @@ -43,6 +44,9 @@ import org.thingsboard.server.common.msg.tools.TbRateLimitsException; |
43 | 44 | import org.thingsboard.server.common.transport.SessionMsgListener; |
44 | 45 | import org.thingsboard.server.common.transport.TransportService; |
45 | 46 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
47 | +import org.thingsboard.server.common.transport.auth.GetOrCreateDeviceFromGatewayResponse; | |
48 | +import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; | |
49 | +import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; | |
46 | 50 | import org.thingsboard.server.common.transport.util.DataDecodingEncodingService; |
47 | 51 | import org.thingsboard.server.common.transport.util.JsonUtils; |
48 | 52 | import org.thingsboard.server.gen.transport.TransportProtos; |
... | ... | @@ -248,38 +252,82 @@ public class DefaultTransportService implements TransportService { |
248 | 252 | } |
249 | 253 | |
250 | 254 | @Override |
251 | - public void process(TransportProtos.ValidateDeviceTokenRequestMsg msg, TransportServiceCallback<TransportProtos.ValidateDeviceCredentialsResponseMsg> callback) { | |
255 | + public void process(TransportProtos.ValidateDeviceTokenRequestMsg msg, TransportServiceCallback<ValidateDeviceCredentialsResponse> callback) { | |
252 | 256 | log.trace("Processing msg: {}", msg); |
253 | 257 | TbProtoQueueMsg<TransportApiRequestMsg> protoMsg = new TbProtoQueueMsg<>(UUID.randomUUID(), TransportApiRequestMsg.newBuilder().setValidateTokenRequestMsg(msg).build()); |
254 | - process(callback, protoMsg); | |
258 | + doProcess(protoMsg, callback); | |
255 | 259 | } |
256 | 260 | |
257 | 261 | @Override |
258 | - public void process(TransportProtos.ValidateDeviceX509CertRequestMsg msg, TransportServiceCallback<TransportProtos.ValidateDeviceCredentialsResponseMsg> callback) { | |
262 | + public void process(TransportProtos.ValidateDeviceX509CertRequestMsg msg, TransportServiceCallback<ValidateDeviceCredentialsResponse> callback) { | |
259 | 263 | log.trace("Processing msg: {}", msg); |
260 | 264 | TbProtoQueueMsg<TransportApiRequestMsg> protoMsg = new TbProtoQueueMsg<>(UUID.randomUUID(), TransportApiRequestMsg.newBuilder().setValidateX509CertRequestMsg(msg).build()); |
261 | - process(callback, protoMsg); | |
262 | - } | |
263 | - | |
264 | - private void process(TransportServiceCallback<TransportProtos.ValidateDeviceCredentialsResponseMsg> callback, TbProtoQueueMsg<TransportApiRequestMsg> protoMsg) { | |
265 | - ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> result = extractProfile(transportApiRequestTemplate.send(protoMsg), | |
266 | - response -> response.getValidateTokenResponseMsg().hasDeviceInfo(), | |
267 | - response -> response.getValidateTokenResponseMsg().getDeviceInfo(), | |
268 | - response -> response.getValidateTokenResponseMsg().getProfileBody()); | |
269 | - AsyncCallbackTemplate.withCallback(result, | |
270 | - response -> callback.onSuccess(response.getValue().getValidateTokenResponseMsg()), callback::onError, transportCallbackExecutor); | |
265 | + doProcess(protoMsg, callback); | |
266 | + } | |
267 | + | |
268 | + private void doProcess(TbProtoQueueMsg<TransportApiRequestMsg> protoMsg, TransportServiceCallback<ValidateDeviceCredentialsResponse> callback) { | |
269 | + ListenableFuture<ValidateDeviceCredentialsResponse> response = Futures.transform(transportApiRequestTemplate.send(protoMsg), tmp -> { | |
270 | + TransportProtos.ValidateDeviceCredentialsResponseMsg msg = tmp.getValue().getValidateTokenResponseMsg(); | |
271 | + ValidateDeviceCredentialsResponse.ValidateDeviceCredentialsResponseBuilder result = ValidateDeviceCredentialsResponse.builder(); | |
272 | + if (msg.hasDeviceInfo()) { | |
273 | + result.credentials(msg.getCredentialsBody()); | |
274 | + TransportDeviceInfo tdi = getTransportDeviceInfo(msg.getDeviceInfo()); | |
275 | + result.deviceInfo(tdi); | |
276 | + ByteString profileBody = msg.getProfileBody(); | |
277 | + if (profileBody != null && !profileBody.isEmpty()) { | |
278 | + DeviceProfile profile = deviceProfiles.get(tdi.getDeviceProfileId()); | |
279 | + if (profile == null) { | |
280 | + Optional<DeviceProfile> deviceProfile = dataDecodingEncodingService.decode(profileBody.toByteArray()); | |
281 | + if (deviceProfile.isPresent()) { | |
282 | + profile = deviceProfile.get(); | |
283 | + deviceProfiles.put(tdi.getDeviceProfileId(), profile); | |
284 | + } | |
285 | + } | |
286 | + result.deviceProfile(profile); | |
287 | + } | |
288 | + } | |
289 | + return result.build(); | |
290 | + }, MoreExecutors.directExecutor()); | |
291 | + AsyncCallbackTemplate.withCallback(response, callback::onSuccess, callback::onError, transportCallbackExecutor); | |
271 | 292 | } |
272 | 293 | |
273 | 294 | @Override |
274 | - public void process(TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg msg, TransportServiceCallback<TransportProtos.GetOrCreateDeviceFromGatewayResponseMsg> callback) { | |
275 | - log.trace("Processing msg: {}", msg); | |
276 | - TbProtoQueueMsg<TransportApiRequestMsg> protoMsg = new TbProtoQueueMsg<>(UUID.randomUUID(), TransportApiRequestMsg.newBuilder().setGetOrCreateDeviceRequestMsg(msg).build()); | |
277 | - ListenableFuture<TbProtoQueueMsg<TransportApiResponseMsg>> result = extractProfile(transportApiRequestTemplate.send(protoMsg), | |
278 | - response -> response.getGetOrCreateDeviceResponseMsg().hasDeviceInfo(), | |
279 | - response -> response.getGetOrCreateDeviceResponseMsg().getDeviceInfo(), | |
280 | - response -> response.getGetOrCreateDeviceResponseMsg().getProfileBody()); | |
281 | - AsyncCallbackTemplate.withCallback(result, | |
282 | - response -> callback.onSuccess(response.getValue().getGetOrCreateDeviceResponseMsg()), callback::onError, transportCallbackExecutor); | |
295 | + public void process(TransportProtos.GetOrCreateDeviceFromGatewayRequestMsg requestMsg, TransportServiceCallback<GetOrCreateDeviceFromGatewayResponse> callback) { | |
296 | + TbProtoQueueMsg<TransportApiRequestMsg> protoMsg = new TbProtoQueueMsg<>(UUID.randomUUID(), TransportApiRequestMsg.newBuilder().setGetOrCreateDeviceRequestMsg(requestMsg).build()); | |
297 | + log.trace("Processing msg: {}", requestMsg); | |
298 | + ListenableFuture<GetOrCreateDeviceFromGatewayResponse> response = Futures.transform(transportApiRequestTemplate.send(protoMsg), tmp -> { | |
299 | + TransportProtos.GetOrCreateDeviceFromGatewayResponseMsg msg = tmp.getValue().getGetOrCreateDeviceResponseMsg(); | |
300 | + GetOrCreateDeviceFromGatewayResponse.GetOrCreateDeviceFromGatewayResponseBuilder result = GetOrCreateDeviceFromGatewayResponse.builder(); | |
301 | + if (msg.hasDeviceInfo()) { | |
302 | + TransportDeviceInfo tdi = getTransportDeviceInfo(msg.getDeviceInfo()); | |
303 | + result.deviceInfo(tdi); | |
304 | + ByteString profileBody = msg.getProfileBody(); | |
305 | + if (profileBody != null && !profileBody.isEmpty()) { | |
306 | + DeviceProfile profile = deviceProfiles.get(tdi.getDeviceProfileId()); | |
307 | + if (profile == null) { | |
308 | + Optional<DeviceProfile> deviceProfile = dataDecodingEncodingService.decode(profileBody.toByteArray()); | |
309 | + if (deviceProfile.isPresent()) { | |
310 | + profile = deviceProfile.get(); | |
311 | + deviceProfiles.put(tdi.getDeviceProfileId(), profile); | |
312 | + } | |
313 | + } | |
314 | + result.deviceProfile(profile); | |
315 | + } | |
316 | + } | |
317 | + return result.build(); | |
318 | + }, MoreExecutors.directExecutor()); | |
319 | + AsyncCallbackTemplate.withCallback(response, callback::onSuccess, callback::onError, transportCallbackExecutor); | |
320 | + } | |
321 | + | |
322 | + private TransportDeviceInfo getTransportDeviceInfo(TransportProtos.DeviceInfoProto di) { | |
323 | + TransportDeviceInfo tdi = new TransportDeviceInfo(); | |
324 | + tdi.setTenantId(new TenantId(new UUID(di.getTenantIdMSB(), di.getTenantIdLSB()))); | |
325 | + tdi.setDeviceId(new DeviceId(new UUID(di.getDeviceIdMSB(), di.getDeviceIdLSB()))); | |
326 | + tdi.setDeviceProfileId(new DeviceProfileId(new UUID(di.getDeviceProfileIdMSB(), di.getDeviceProfileIdLSB()))); | |
327 | + tdi.setAdditionalInfo(di.getAdditionalInfo()); | |
328 | + tdi.setDeviceName(di.getDeviceName()); | |
329 | + tdi.setDeviceType(di.getDeviceType()); | |
330 | + return tdi; | |
283 | 331 | } |
284 | 332 | |
285 | 333 | @Override | ... | ... |
... | ... | @@ -19,6 +19,7 @@ import lombok.Data; |
19 | 19 | import lombok.Getter; |
20 | 20 | import org.thingsboard.server.common.data.id.DeviceId; |
21 | 21 | import org.thingsboard.server.common.msg.session.SessionContext; |
22 | +import org.thingsboard.server.common.transport.auth.TransportDeviceInfo; | |
22 | 23 | import org.thingsboard.server.gen.transport.TransportProtos.DeviceInfoProto; |
23 | 24 | |
24 | 25 | import java.util.UUID; |
... | ... | @@ -34,17 +35,17 @@ public abstract class DeviceAwareSessionContext implements SessionContext { |
34 | 35 | @Getter |
35 | 36 | private volatile DeviceId deviceId; |
36 | 37 | @Getter |
37 | - private volatile DeviceInfoProto deviceInfo; | |
38 | + private volatile TransportDeviceInfo deviceInfo; | |
38 | 39 | private volatile boolean connected; |
39 | 40 | |
40 | 41 | public DeviceId getDeviceId() { |
41 | 42 | return deviceId; |
42 | 43 | } |
43 | 44 | |
44 | - public void setDeviceInfo(DeviceInfoProto deviceInfo) { | |
45 | + public void setDeviceInfo(TransportDeviceInfo deviceInfo) { | |
45 | 46 | this.deviceInfo = deviceInfo; |
46 | 47 | this.connected = true; |
47 | - this.deviceId = new DeviceId(new UUID(deviceInfo.getDeviceIdMSB(), deviceInfo.getDeviceIdLSB())); | |
48 | + this.deviceId = deviceInfo.getDeviceId(); | |
48 | 49 | } |
49 | 50 | |
50 | 51 | public boolean isConnected() { | ... | ... |