Commit dd9257f6a19d6e68aadb75c82f1ac0c229de91bd
Merge branch 'master' into events-cleanup-performance-improvement-for-big-data
# Conflicts: # application/src/main/data/upgrade/3.2.2/schema_update_event.sql
Showing
77 changed files
with
2165 additions
and
922 deletions
Too many changes to show.
To preserve performance only 77 of 151 files are displayed.
... | ... | @@ -33,15 +33,15 @@ |
33 | 33 | "enableSearch": true, |
34 | 34 | "displayPagination": true, |
35 | 35 | "defaultPageSize": 10, |
36 | - "defaultSortOrder": "entityLabel", | |
37 | - "displayEntityName": false, | |
36 | + "defaultSortOrder": "entityName", | |
37 | + "displayEntityName": true, | |
38 | 38 | "displayEntityType": false, |
39 | 39 | "enableSelectColumnDisplay": false, |
40 | 40 | "enableStickyHeader": true, |
41 | 41 | "enableStickyAction": false, |
42 | 42 | "entitiesTitle": "Devices", |
43 | - "displayEntityLabel": true, | |
44 | - "entityLabelColumnTitle": "Device" | |
43 | + "displayEntityLabel": false, | |
44 | + "entityNameColumnTitle": "Device" | |
45 | 45 | }, |
46 | 46 | "title": "New Entities table", |
47 | 47 | "dropShadow": true, |
... | ... | @@ -828,15 +828,15 @@ |
828 | 828 | "enableSearch": true, |
829 | 829 | "displayPagination": true, |
830 | 830 | "defaultPageSize": 10, |
831 | - "defaultSortOrder": "entityLabel", | |
832 | - "displayEntityName": false, | |
831 | + "defaultSortOrder": "entityName", | |
832 | + "displayEntityName": true, | |
833 | 833 | "displayEntityType": false, |
834 | 834 | "enableSelectColumnDisplay": false, |
835 | 835 | "enableStickyHeader": true, |
836 | 836 | "enableStickyAction": true, |
837 | 837 | "entitiesTitle": "Devices", |
838 | - "displayEntityLabel": true, | |
839 | - "entityLabelColumnTitle": "Device" | |
838 | + "displayEntityLabel": false, | |
839 | + "entityNameColumnTitle": "Device" | |
840 | 840 | }, |
841 | 841 | "title": "New Entities table", |
842 | 842 | "dropShadow": true, |
... | ... | @@ -1125,15 +1125,15 @@ |
1125 | 1125 | "enableSearch": true, |
1126 | 1126 | "displayPagination": true, |
1127 | 1127 | "defaultPageSize": 10, |
1128 | - "defaultSortOrder": "entityLabel", | |
1129 | - "displayEntityName": false, | |
1128 | + "defaultSortOrder": "entityName", | |
1129 | + "displayEntityName": true, | |
1130 | 1130 | "displayEntityType": false, |
1131 | 1131 | "enableSelectColumnDisplay": false, |
1132 | 1132 | "enableStickyHeader": true, |
1133 | 1133 | "enableStickyAction": true, |
1134 | 1134 | "entitiesTitle": "Devices", |
1135 | - "displayEntityLabel": true, | |
1136 | - "entityLabelColumnTitle": "Device" | |
1135 | + "displayEntityLabel": false, | |
1136 | + "entityNameColumnTitle": "Device" | |
1137 | 1137 | }, |
1138 | 1138 | "title": "New Entities table", |
1139 | 1139 | "dropShadow": true, |
... | ... | @@ -1422,15 +1422,15 @@ |
1422 | 1422 | "enableSearch": true, |
1423 | 1423 | "displayPagination": true, |
1424 | 1424 | "defaultPageSize": 10, |
1425 | - "defaultSortOrder": "entityLabel", | |
1426 | - "displayEntityName": false, | |
1425 | + "defaultSortOrder": "entityName", | |
1426 | + "displayEntityName": true, | |
1427 | 1427 | "displayEntityType": false, |
1428 | 1428 | "enableSelectColumnDisplay": false, |
1429 | 1429 | "enableStickyHeader": true, |
1430 | 1430 | "enableStickyAction": true, |
1431 | 1431 | "entitiesTitle": "Devices", |
1432 | - "displayEntityLabel": true, | |
1433 | - "entityLabelColumnTitle": "Device" | |
1432 | + "displayEntityLabel": false, | |
1433 | + "entityNameColumnTitle": "Device" | |
1434 | 1434 | }, |
1435 | 1435 | "title": "New Entities table", |
1436 | 1436 | "dropShadow": true, |
... | ... | @@ -1719,15 +1719,15 @@ |
1719 | 1719 | "enableSearch": true, |
1720 | 1720 | "displayPagination": true, |
1721 | 1721 | "defaultPageSize": 10, |
1722 | - "defaultSortOrder": "entityLabel", | |
1723 | - "displayEntityName": false, | |
1722 | + "defaultSortOrder": "entityName", | |
1723 | + "displayEntityName": true, | |
1724 | 1724 | "displayEntityType": false, |
1725 | 1725 | "enableSelectColumnDisplay": false, |
1726 | 1726 | "enableStickyHeader": true, |
1727 | 1727 | "enableStickyAction": true, |
1728 | 1728 | "entitiesTitle": "Devices", |
1729 | - "displayEntityLabel": true, | |
1730 | - "entityLabelColumnTitle": "Device" | |
1729 | + "displayEntityLabel": false, | |
1730 | + "entityNameColumnTitle": "Device" | |
1731 | 1731 | }, |
1732 | 1732 | "title": "New Entities table", |
1733 | 1733 | "dropShadow": true, | ... | ... |
... | ... | @@ -33,15 +33,15 @@ |
33 | 33 | "enableSearch": true, |
34 | 34 | "displayPagination": true, |
35 | 35 | "defaultPageSize": 10, |
36 | - "defaultSortOrder": "entityLabel", | |
37 | - "displayEntityName": false, | |
36 | + "defaultSortOrder": "entityName", | |
37 | + "displayEntityName": true, | |
38 | 38 | "displayEntityType": false, |
39 | 39 | "enableSelectColumnDisplay": false, |
40 | 40 | "enableStickyHeader": true, |
41 | 41 | "enableStickyAction": false, |
42 | 42 | "entitiesTitle": "Devices", |
43 | - "displayEntityLabel": true, | |
44 | - "entityLabelColumnTitle": "Device" | |
43 | + "displayEntityLabel": false, | |
44 | + "entityNameColumnTitle": "Device" | |
45 | 45 | }, |
46 | 46 | "title": "New Entities table", |
47 | 47 | "dropShadow": true, |
... | ... | @@ -828,15 +828,15 @@ |
828 | 828 | "enableSearch": true, |
829 | 829 | "displayPagination": true, |
830 | 830 | "defaultPageSize": 10, |
831 | - "defaultSortOrder": "entityLabel", | |
832 | - "displayEntityName": false, | |
831 | + "defaultSortOrder": "entityName", | |
832 | + "displayEntityName": true, | |
833 | 833 | "displayEntityType": false, |
834 | 834 | "enableSelectColumnDisplay": false, |
835 | 835 | "enableStickyHeader": true, |
836 | 836 | "enableStickyAction": true, |
837 | 837 | "entitiesTitle": "Devices", |
838 | - "displayEntityLabel": true, | |
839 | - "entityLabelColumnTitle": "Device" | |
838 | + "displayEntityLabel": false, | |
839 | + "entityNameColumnTitle": "Device" | |
840 | 840 | }, |
841 | 841 | "title": "New Entities table", |
842 | 842 | "dropShadow": true, |
... | ... | @@ -1125,15 +1125,15 @@ |
1125 | 1125 | "enableSearch": true, |
1126 | 1126 | "displayPagination": true, |
1127 | 1127 | "defaultPageSize": 10, |
1128 | - "defaultSortOrder": "entityLabel", | |
1129 | - "displayEntityName": false, | |
1128 | + "defaultSortOrder": "entityName", | |
1129 | + "displayEntityName": true, | |
1130 | 1130 | "displayEntityType": false, |
1131 | 1131 | "enableSelectColumnDisplay": false, |
1132 | 1132 | "enableStickyHeader": true, |
1133 | 1133 | "enableStickyAction": true, |
1134 | 1134 | "entitiesTitle": "Devices", |
1135 | - "displayEntityLabel": true, | |
1136 | - "entityLabelColumnTitle": "Device" | |
1135 | + "displayEntityLabel": false, | |
1136 | + "entityNameColumnTitle": "Device" | |
1137 | 1137 | }, |
1138 | 1138 | "title": "New Entities table", |
1139 | 1139 | "dropShadow": true, |
... | ... | @@ -1422,15 +1422,15 @@ |
1422 | 1422 | "enableSearch": true, |
1423 | 1423 | "displayPagination": true, |
1424 | 1424 | "defaultPageSize": 10, |
1425 | - "defaultSortOrder": "entityLabel", | |
1426 | - "displayEntityName": false, | |
1425 | + "defaultSortOrder": "entityName", | |
1426 | + "displayEntityName": true, | |
1427 | 1427 | "displayEntityType": false, |
1428 | 1428 | "enableSelectColumnDisplay": false, |
1429 | 1429 | "enableStickyHeader": true, |
1430 | 1430 | "enableStickyAction": true, |
1431 | 1431 | "entitiesTitle": "Devices", |
1432 | - "displayEntityLabel": true, | |
1433 | - "entityLabelColumnTitle": "Device" | |
1432 | + "displayEntityLabel": false, | |
1433 | + "entityNameColumnTitle": "Device" | |
1434 | 1434 | }, |
1435 | 1435 | "title": "New Entities table", |
1436 | 1436 | "dropShadow": true, |
... | ... | @@ -1719,15 +1719,15 @@ |
1719 | 1719 | "enableSearch": true, |
1720 | 1720 | "displayPagination": true, |
1721 | 1721 | "defaultPageSize": 10, |
1722 | - "defaultSortOrder": "entityLabel", | |
1723 | - "displayEntityName": false, | |
1722 | + "defaultSortOrder": "entityName", | |
1723 | + "displayEntityName": true, | |
1724 | 1724 | "displayEntityType": false, |
1725 | 1725 | "enableSelectColumnDisplay": false, |
1726 | 1726 | "enableStickyHeader": true, |
1727 | 1727 | "enableStickyAction": true, |
1728 | 1728 | "entitiesTitle": "Devices", |
1729 | - "displayEntityLabel": true, | |
1730 | - "entityLabelColumnTitle": "Device" | |
1729 | + "displayEntityLabel": false, | |
1730 | + "entityNameColumnTitle": "Device" | |
1731 | 1731 | }, |
1732 | 1732 | "title": "New Entities table", |
1733 | 1733 | "dropShadow": true, | ... | ... |
... | ... | @@ -24,6 +24,7 @@ import com.google.protobuf.InvalidProtocolBufferException; |
24 | 24 | import lombok.extern.slf4j.Slf4j; |
25 | 25 | import org.apache.commons.collections.CollectionUtils; |
26 | 26 | import org.thingsboard.common.util.JacksonUtil; |
27 | +import org.thingsboard.common.util.LinkedHashMapRemoveEldest; | |
27 | 28 | import org.thingsboard.rule.engine.api.RpcError; |
28 | 29 | import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg; |
29 | 30 | import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; |
... | ... | @@ -96,6 +97,8 @@ import java.util.HashSet; |
96 | 97 | import java.util.LinkedHashMap; |
97 | 98 | import java.util.List; |
98 | 99 | import java.util.Map; |
100 | +import java.util.Objects; | |
101 | +import java.util.Optional; | |
99 | 102 | import java.util.Set; |
100 | 103 | import java.util.UUID; |
101 | 104 | import java.util.function.Consumer; |
... | ... | @@ -110,7 +113,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
110 | 113 | |
111 | 114 | final TenantId tenantId; |
112 | 115 | final DeviceId deviceId; |
113 | - private final Map<UUID, SessionInfoMetaData> sessions; | |
116 | + final LinkedHashMapRemoveEldest<UUID, SessionInfoMetaData> sessions; | |
114 | 117 | private final Map<UUID, SessionInfo> attributeSubscriptions; |
115 | 118 | private final Map<UUID, SessionInfo> rpcSubscriptions; |
116 | 119 | private final Map<Integer, ToDeviceRpcRequestMetadata> toDeviceRpcPendingMap; |
... | ... | @@ -125,16 +128,16 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
125 | 128 | super(systemContext); |
126 | 129 | this.tenantId = tenantId; |
127 | 130 | this.deviceId = deviceId; |
128 | - this.sessions = new LinkedHashMap<>(); | |
129 | 131 | this.attributeSubscriptions = new HashMap<>(); |
130 | 132 | this.rpcSubscriptions = new HashMap<>(); |
131 | 133 | this.toDeviceRpcPendingMap = new HashMap<>(); |
134 | + this.sessions = new LinkedHashMapRemoveEldest<>(systemContext.getMaxConcurrentSessionsPerDevice(), this::notifyTransportAboutClosedSessionMaxSessionsLimit); | |
132 | 135 | if (initAttributes()) { |
133 | 136 | restoreSessions(); |
134 | 137 | } |
135 | 138 | } |
136 | 139 | |
137 | - private boolean initAttributes() { | |
140 | + boolean initAttributes() { | |
138 | 141 | Device device = systemContext.getDeviceService().findDeviceById(tenantId, deviceId); |
139 | 142 | if (device != null) { |
140 | 143 | this.deviceName = device.getName(); |
... | ... | @@ -552,18 +555,14 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
552 | 555 | |
553 | 556 | private void processSessionStateMsgs(SessionInfoProto sessionInfo, SessionEventMsg msg) { |
554 | 557 | UUID sessionId = getSessionId(sessionInfo); |
558 | + Objects.requireNonNull(sessionId); | |
555 | 559 | if (msg.getEvent() == SessionEvent.OPEN) { |
556 | 560 | if (sessions.containsKey(sessionId)) { |
557 | 561 | log.debug("[{}] Received duplicate session open event [{}]", deviceId, sessionId); |
558 | 562 | return; |
559 | 563 | } |
560 | - log.debug("[{}] Processing new session [{}]", deviceId, sessionId); | |
561 | - if (sessions.size() >= systemContext.getMaxConcurrentSessionsPerDevice()) { | |
562 | - UUID sessionIdToRemove = sessions.keySet().stream().findFirst().orElse(null); | |
563 | - if (sessionIdToRemove != null) { | |
564 | - notifyTransportAboutClosedSession(sessionIdToRemove, sessions.remove(sessionIdToRemove), "max concurrent sessions limit reached per device!"); | |
565 | - } | |
566 | - } | |
564 | + log.info("[{}] Processing new session [{}]. Current sessions size {}", deviceId, sessionId, sessions.size()); | |
565 | + | |
567 | 566 | sessions.put(sessionId, new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfo.getNodeId()))); |
568 | 567 | if (sessions.size() == 1) { |
569 | 568 | reportSessionOpen(); |
... | ... | @@ -584,8 +583,10 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
584 | 583 | |
585 | 584 | private void handleSessionActivity(TbActorCtx context, SessionInfoProto sessionInfoProto, SubscriptionInfoProto subscriptionInfo) { |
586 | 585 | UUID sessionId = getSessionId(sessionInfoProto); |
586 | + Objects.requireNonNull(sessionId); | |
587 | + | |
587 | 588 | SessionInfoMetaData sessionMD = sessions.computeIfAbsent(sessionId, |
588 | - id -> new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfoProto.getNodeId()), 0L)); | |
589 | + id -> new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfoProto.getNodeId()), subscriptionInfo.getLastActivityTime())); | |
589 | 590 | |
590 | 591 | sessionMD.setLastActivityTime(subscriptionInfo.getLastActivityTime()); |
591 | 592 | sessionMD.setSubscribedToAttributes(subscriptionInfo.getAttributeSubscription()); |
... | ... | @@ -615,6 +616,11 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
615 | 616 | } |
616 | 617 | } |
617 | 618 | |
619 | + private void notifyTransportAboutClosedSessionMaxSessionsLimit(UUID sessionId, SessionInfoMetaData sessionMd) { | |
620 | + notifyTransportAboutClosedSession(sessionId, sessionMd, "max concurrent sessions limit reached per device!"); | |
621 | + } | |
622 | + | |
623 | + | |
618 | 624 | private void notifyTransportAboutClosedSession(UUID sessionId, SessionInfoMetaData sessionMd, String message) { |
619 | 625 | SessionCloseNotificationProto sessionCloseNotificationProto = SessionCloseNotificationProto |
620 | 626 | .newBuilder() |
... | ... | @@ -761,7 +767,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
761 | 767 | return builder.build(); |
762 | 768 | } |
763 | 769 | |
764 | - private void restoreSessions() { | |
770 | + void restoreSessions() { | |
765 | 771 | log.debug("[{}] Restoring sessions from cache", deviceId); |
766 | 772 | DeviceSessionsCacheEntry sessionsDump = null; |
767 | 773 | try { |
... | ... | @@ -774,6 +780,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
774 | 780 | log.debug("[{}] No session information found", deviceId); |
775 | 781 | return; |
776 | 782 | } |
783 | + // TODO: Take latest max allowed sessions size from cache | |
777 | 784 | for (SessionSubscriptionInfoProto sessionSubscriptionInfoProto : sessionsDump.getSessionsList()) { |
778 | 785 | SessionInfoProto sessionInfoProto = sessionSubscriptionInfoProto.getSessionInfo(); |
779 | 786 | UUID sessionId = getSessionId(sessionInfoProto); |
... | ... | @@ -843,6 +850,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
843 | 850 | } |
844 | 851 | |
845 | 852 | void checkSessionsTimeout() { |
853 | + log.debug("[{}] checkSessionsTimeout started. Size before check {}", deviceId, sessions.size()); | |
846 | 854 | long expTime = System.currentTimeMillis() - systemContext.getSessionInactivityTimeout(); |
847 | 855 | Map<UUID, SessionInfoMetaData> sessionsToRemove = sessions.entrySet().stream().filter(kv -> kv.getValue().getLastActivityTime() < expTime).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
848 | 856 | sessionsToRemove.forEach((sessionId, sessionMD) -> { |
... | ... | @@ -854,6 +862,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
854 | 862 | if (!sessionsToRemove.isEmpty()) { |
855 | 863 | dumpSessions(); |
856 | 864 | } |
865 | + log.debug("[{}] checkSessionsTimeout finished. Size after check {}", deviceId, sessions.size()); | |
857 | 866 | } |
858 | 867 | |
859 | 868 | } | ... | ... |
... | ... | @@ -135,7 +135,7 @@ public class AuthController extends BaseController { |
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
138 | - @RequestMapping(value = "/noauth/activate", params = { "activateToken" }, method = RequestMethod.GET) | |
138 | + @RequestMapping(value = "/noauth/activate", params = {"activateToken"}, method = RequestMethod.GET) | |
139 | 139 | public ResponseEntity<String> checkActivateToken( |
140 | 140 | @RequestParam(value = "activateToken") String activateToken) { |
141 | 141 | HttpHeaders headers = new HttpHeaders(); |
... | ... | @@ -159,7 +159,7 @@ public class AuthController extends BaseController { |
159 | 159 | |
160 | 160 | @RequestMapping(value = "/noauth/resetPasswordByEmail", method = RequestMethod.POST) |
161 | 161 | @ResponseStatus(value = HttpStatus.OK) |
162 | - public void requestResetPasswordByEmail ( | |
162 | + public void requestResetPasswordByEmail( | |
163 | 163 | @RequestBody JsonNode resetPasswordByEmailRequest, |
164 | 164 | HttpServletRequest request) throws ThingsboardException { |
165 | 165 | try { |
... | ... | @@ -170,13 +170,13 @@ public class AuthController extends BaseController { |
170 | 170 | String resetUrl = String.format("%s/api/noauth/resetPassword?resetToken=%s", baseUrl, |
171 | 171 | userCredentials.getResetToken()); |
172 | 172 | |
173 | - mailService.sendResetPasswordEmail(resetUrl, email); | |
173 | + mailService.sendResetPasswordEmailAsync(resetUrl, email); | |
174 | 174 | } catch (Exception e) { |
175 | - throw handleException(e); | |
175 | + log.warn("Error occurred: {}", e.getMessage()); | |
176 | 176 | } |
177 | 177 | } |
178 | 178 | |
179 | - @RequestMapping(value = "/noauth/resetPassword", params = { "resetToken" }, method = RequestMethod.GET) | |
179 | + @RequestMapping(value = "/noauth/resetPassword", params = {"resetToken"}, method = RequestMethod.GET) | |
180 | 180 | public ResponseEntity<String> checkResetToken( |
181 | 181 | @RequestParam(value = "resetToken") String resetToken) { |
182 | 182 | HttpHeaders headers = new HttpHeaders(); | ... | ... |
... | ... | @@ -25,9 +25,12 @@ import org.springframework.security.authentication.BadCredentialsException; |
25 | 25 | import org.springframework.security.authentication.DisabledException; |
26 | 26 | import org.springframework.security.authentication.LockedException; |
27 | 27 | import org.springframework.security.core.AuthenticationException; |
28 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
28 | 29 | import org.springframework.security.web.access.AccessDeniedHandler; |
29 | -import org.springframework.stereotype.Component; | |
30 | +import org.springframework.web.bind.annotation.ExceptionHandler; | |
31 | +import org.springframework.web.bind.annotation.RestControllerAdvice; | |
30 | 32 | import org.springframework.web.client.HttpClientErrorException; |
33 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | |
31 | 34 | import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
32 | 35 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
33 | 36 | import org.thingsboard.server.common.msg.tools.TbRateLimitsException; |
... | ... | @@ -40,14 +43,15 @@ import javax.servlet.http.HttpServletRequest; |
40 | 43 | import javax.servlet.http.HttpServletResponse; |
41 | 44 | import java.io.IOException; |
42 | 45 | |
43 | -@Component | |
44 | 46 | @Slf4j |
45 | -public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { | |
47 | +@RestControllerAdvice | |
48 | +public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler { | |
46 | 49 | |
47 | 50 | @Autowired |
48 | 51 | private ObjectMapper mapper; |
49 | 52 | |
50 | 53 | @Override |
54 | + @ExceptionHandler(AccessDeniedException.class) | |
51 | 55 | public void handle(HttpServletRequest request, HttpServletResponse response, |
52 | 56 | AccessDeniedException accessDeniedException) throws IOException, |
53 | 57 | ServletException { |
... | ... | @@ -60,6 +64,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { |
60 | 64 | } |
61 | 65 | } |
62 | 66 | |
67 | + @ExceptionHandler(Exception.class) | |
63 | 68 | public void handle(Exception exception, HttpServletResponse response) { |
64 | 69 | log.debug("Processing exception {}", exception.getMessage(), exception); |
65 | 70 | if (!response.isCommitted()) { |
... | ... | @@ -148,7 +153,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { |
148 | 153 | |
149 | 154 | private void handleAuthenticationException(AuthenticationException authenticationException, HttpServletResponse response) throws IOException { |
150 | 155 | response.setStatus(HttpStatus.UNAUTHORIZED.value()); |
151 | - if (authenticationException instanceof BadCredentialsException) { | |
156 | + if (authenticationException instanceof BadCredentialsException || authenticationException instanceof UsernameNotFoundException) { | |
152 | 157 | mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("Invalid username or password", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); |
153 | 158 | } else if (authenticationException instanceof DisabledException) { |
154 | 159 | mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of("User account is not active", ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); |
... | ... | @@ -159,7 +164,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { |
159 | 164 | } else if (authenticationException instanceof AuthMethodNotSupportedException) { |
160 | 165 | mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); |
161 | 166 | } else if (authenticationException instanceof UserPasswordExpiredException) { |
162 | - UserPasswordExpiredException expiredException = (UserPasswordExpiredException)authenticationException; | |
167 | + UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException; | |
163 | 168 | String resetToken = expiredException.getResetToken(); |
164 | 169 | mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); |
165 | 170 | } else { | ... | ... |
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.service.install.update; |
17 | 17 | |
18 | +import com.fasterxml.jackson.databind.JsonNode; | |
18 | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
19 | 20 | import com.google.common.util.concurrent.Futures; |
20 | 21 | import com.google.common.util.concurrent.ListenableFuture; |
... | ... | @@ -31,6 +32,7 @@ import org.thingsboard.server.common.data.Tenant; |
31 | 32 | import org.thingsboard.server.common.data.alarm.Alarm; |
32 | 33 | import org.thingsboard.server.common.data.alarm.AlarmInfo; |
33 | 34 | import org.thingsboard.server.common.data.alarm.AlarmQuery; |
35 | +import org.thingsboard.server.common.data.alarm.AlarmSeverity; | |
34 | 36 | import org.thingsboard.server.common.data.id.EntityViewId; |
35 | 37 | import org.thingsboard.server.common.data.id.TenantId; |
36 | 38 | import org.thingsboard.server.common.data.kv.BaseReadTsKvQuery; |
... | ... | @@ -41,16 +43,21 @@ import org.thingsboard.server.common.data.oauth2.deprecated.OAuth2ClientsParams; |
41 | 43 | import org.thingsboard.server.common.data.page.PageData; |
42 | 44 | import org.thingsboard.server.common.data.page.PageLink; |
43 | 45 | import org.thingsboard.server.common.data.page.TimePageLink; |
46 | +import org.thingsboard.server.common.data.query.DynamicValue; | |
47 | +import org.thingsboard.server.common.data.query.FilterPredicateValue; | |
44 | 48 | import org.thingsboard.server.common.data.rule.RuleChain; |
45 | 49 | import org.thingsboard.server.common.data.rule.RuleChainMetaData; |
46 | 50 | import org.thingsboard.server.common.data.rule.RuleNode; |
51 | +import org.thingsboard.server.dao.DaoUtil; | |
47 | 52 | import org.thingsboard.server.dao.alarm.AlarmDao; |
48 | 53 | import org.thingsboard.server.dao.alarm.AlarmService; |
49 | 54 | import org.thingsboard.server.dao.entity.EntityService; |
50 | 55 | import org.thingsboard.server.dao.entityview.EntityViewService; |
56 | +import org.thingsboard.server.dao.model.sql.DeviceProfileEntity; | |
51 | 57 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
52 | 58 | import org.thingsboard.server.dao.oauth2.OAuth2Utils; |
53 | 59 | import org.thingsboard.server.dao.rule.RuleChainService; |
60 | +import org.thingsboard.server.dao.sql.device.DeviceProfileRepository; | |
54 | 61 | import org.thingsboard.server.dao.tenant.TenantService; |
55 | 62 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
56 | 63 | import org.thingsboard.server.service.install.InstallScripts; |
... | ... | @@ -93,6 +100,9 @@ public class DefaultDataUpdateService implements DataUpdateService { |
93 | 100 | private AlarmDao alarmDao; |
94 | 101 | |
95 | 102 | @Autowired |
103 | + private DeviceProfileRepository deviceProfileRepository; | |
104 | + | |
105 | + @Autowired | |
96 | 106 | private OAuth2Service oAuth2Service; |
97 | 107 | |
98 | 108 | @Override |
... | ... | @@ -114,6 +124,7 @@ public class DefaultDataUpdateService implements DataUpdateService { |
114 | 124 | log.info("Updating data from version 3.2.2 to 3.3.0 ..."); |
115 | 125 | tenantsDefaultEdgeRuleChainUpdater.updateEntities(null); |
116 | 126 | tenantsAlarmsCustomerUpdater.updateEntities(null); |
127 | + deviceProfileEntityDynamicConditionsUpdater.updateEntities(null); | |
117 | 128 | updateOAuth2Params(); |
118 | 129 | break; |
119 | 130 | default: |
... | ... | @@ -121,6 +132,45 @@ public class DefaultDataUpdateService implements DataUpdateService { |
121 | 132 | } |
122 | 133 | } |
123 | 134 | |
135 | + private final PaginatedUpdater<String, DeviceProfileEntity> deviceProfileEntityDynamicConditionsUpdater = | |
136 | + new PaginatedUpdater<>() { | |
137 | + | |
138 | + @Override | |
139 | + protected String getName() { | |
140 | + return "Device Profile Entity Dynamic Conditions Updater"; | |
141 | + } | |
142 | + | |
143 | + @Override | |
144 | + protected PageData<DeviceProfileEntity> findEntities(String id, PageLink pageLink) { | |
145 | + return DaoUtil.pageToPageData(deviceProfileRepository.findAll(DaoUtil.toPageable(pageLink))); | |
146 | + } | |
147 | + | |
148 | + @Override | |
149 | + protected void updateEntity(DeviceProfileEntity deviceProfile) { | |
150 | + if (deviceProfile.getProfileData().has("alarms") && | |
151 | + !deviceProfile.getProfileData().get("alarms").isNull()) { | |
152 | + boolean isUpdated = false; | |
153 | + JsonNode array = deviceProfile.getProfileData().get("alarms"); | |
154 | + for (JsonNode node : array) { | |
155 | + if (node.has("createRules")) { | |
156 | + JsonNode createRules = node.get("createRules"); | |
157 | + for (AlarmSeverity severity : AlarmSeverity.values()) { | |
158 | + if (createRules.has(severity.name())) { | |
159 | + isUpdated = isUpdated || convertDeviceProfileAlarmRulesForVersion330(createRules.get(severity.name()).get("condition").get("spec")); | |
160 | + } | |
161 | + } | |
162 | + } | |
163 | + if (node.has("clearRule") && !node.get("clearRule").isNull()) { | |
164 | + isUpdated = isUpdated || convertDeviceProfileAlarmRulesForVersion330(node.get("clearRule").get("condition").get("spec")); | |
165 | + } | |
166 | + } | |
167 | + if (isUpdated) { | |
168 | + deviceProfileRepository.save(deviceProfile); | |
169 | + } | |
170 | + } | |
171 | + } | |
172 | + }; | |
173 | + | |
124 | 174 | private final PaginatedUpdater<String, Tenant> tenantsDefaultRuleChainUpdater = |
125 | 175 | new PaginatedUpdater<>() { |
126 | 176 | |
... | ... | @@ -370,6 +420,33 @@ public class DefaultDataUpdateService implements DataUpdateService { |
370 | 420 | } |
371 | 421 | } |
372 | 422 | |
423 | + private boolean convertDeviceProfileAlarmRulesForVersion330(JsonNode spec) { | |
424 | + if (spec != null) { | |
425 | + if (spec.has("type") && spec.get("type").asText().equals("DURATION")) { | |
426 | + if (spec.has("value")) { | |
427 | + long value = spec.get("value").asLong(); | |
428 | + var predicate = new FilterPredicateValue<>( | |
429 | + value, null, new DynamicValue<>(null, null, false) | |
430 | + ); | |
431 | + ((ObjectNode) spec).remove("value"); | |
432 | + ((ObjectNode) spec).putPOJO("predicate", predicate); | |
433 | + return true; | |
434 | + } | |
435 | + } else if (spec.has("type") && spec.get("type").asText().equals("REPEATING")) { | |
436 | + if (spec.has("count")) { | |
437 | + int count = spec.get("count").asInt(); | |
438 | + var predicate = new FilterPredicateValue<>( | |
439 | + count, null, new DynamicValue<>(null, null, false) | |
440 | + ); | |
441 | + ((ObjectNode) spec).remove("count"); | |
442 | + ((ObjectNode) spec).putPOJO("predicate", predicate); | |
443 | + return true; | |
444 | + } | |
445 | + } | |
446 | + } | |
447 | + return false; | |
448 | + } | |
449 | + | |
373 | 450 | private void updateOAuth2Params() { |
374 | 451 | try { |
375 | 452 | OAuth2ClientsParams oauth2ClientsParams = oAuth2Service.findOAuth2Params(); |
... | ... | @@ -380,9 +457,8 @@ public class DefaultDataUpdateService implements DataUpdateService { |
380 | 457 | oAuth2Service.saveOAuth2Params(new OAuth2ClientsParams(false, Collections.emptyList())); |
381 | 458 | log.info("Successfully updated OAuth2 parameters!"); |
382 | 459 | } |
383 | - } | |
384 | - catch (Exception e) { | |
385 | - log.error("Failed to update OAuth2 parameters", e); | |
460 | + } catch (Exception e) { | |
461 | + log.error("Failed to update OAuth2 parameters", e); | |
386 | 462 | } |
387 | 463 | } |
388 | 464 | ... | ... |
... | ... | @@ -22,7 +22,7 @@ import org.thingsboard.server.common.data.page.PageData; |
22 | 22 | import org.thingsboard.server.common.data.page.PageLink; |
23 | 23 | |
24 | 24 | @Slf4j |
25 | -public abstract class PaginatedUpdater<I, D extends SearchTextBased<? extends UUIDBased>> { | |
25 | +public abstract class PaginatedUpdater<I, D> { | |
26 | 26 | |
27 | 27 | private static final int DEFAULT_LIMIT = 100; |
28 | 28 | private int updated = 0; | ... | ... |
... | ... | @@ -30,8 +30,12 @@ import java.math.BigInteger; |
30 | 30 | import java.security.AlgorithmParameters; |
31 | 31 | import java.security.GeneralSecurityException; |
32 | 32 | import java.security.KeyFactory; |
33 | +import java.security.KeyStore; | |
33 | 34 | import java.security.KeyStoreException; |
35 | +import java.security.NoSuchAlgorithmException; | |
36 | +import java.security.PrivateKey; | |
34 | 37 | import java.security.PublicKey; |
38 | +import java.security.UnrecoverableKeyException; | |
35 | 39 | import java.security.cert.CertificateEncodingException; |
36 | 40 | import java.security.cert.X509Certificate; |
37 | 41 | import java.security.spec.ECGenParameterSpec; |
... | ... | @@ -62,49 +66,23 @@ public class LwM2MServerSecurityInfoRepository { |
62 | 66 | bsServ.setPort(serverConfig.getPort()); |
63 | 67 | bsServ.setSecurityHost(serverConfig.getSecureHost()); |
64 | 68 | bsServ.setSecurityPort(serverConfig.getSecurePort()); |
65 | - bsServ.setServerPublicKey(getPublicKey(serverConfig.getCertificateAlias(), this.serverConfig.getPublicX(), this.serverConfig.getPublicY())); | |
69 | + bsServ.setServerPublicKey(getPublicKey(serverConfig)); | |
66 | 70 | return bsServ; |
67 | 71 | } |
68 | 72 | |
69 | - private String getPublicKey(String alias, String publicServerX, String publicServerY) { | |
70 | - String publicKey = getServerPublicKeyX509(alias); | |
71 | - return publicKey != null ? publicKey : getRPKPublicKey(publicServerX, publicServerY); | |
72 | - } | |
73 | - | |
74 | - private String getServerPublicKeyX509(String alias) { | |
75 | - try { | |
76 | - X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(alias); | |
77 | - return Hex.encodeHexString(serverCertificate.getEncoded()); | |
78 | - } catch (CertificateEncodingException | KeyStoreException e) { | |
79 | - e.printStackTrace(); | |
80 | - } | |
81 | - return null; | |
82 | - } | |
83 | - | |
84 | - private String getRPKPublicKey(String publicServerX, String publicServerY) { | |
73 | + private String getPublicKey(LwM2MSecureServerConfig config) { | |
85 | 74 | try { |
86 | - /** Get Elliptic Curve Parameter spec for secp256r1 */ | |
87 | - AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); | |
88 | - algoParameters.init(new ECGenParameterSpec("secp256r1")); | |
89 | - ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); | |
90 | - if (publicServerX != null && !publicServerX.isEmpty() && publicServerY != null && !publicServerY.isEmpty()) { | |
91 | - /** Get point values */ | |
92 | - byte[] publicX = Hex.decodeHex(publicServerX.toCharArray()); | |
93 | - byte[] publicY = Hex.decodeHex(publicServerY.toCharArray()); | |
94 | - /** Create key specs */ | |
95 | - KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), | |
96 | - parameterSpec); | |
97 | - /** Get keys */ | |
98 | - PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec); | |
99 | - if (publicKey != null && publicKey.getEncoded().length > 0) { | |
100 | - return Hex.encodeHexString(publicKey.getEncoded()); | |
101 | - } | |
75 | + KeyStore keyStore = serverConfig.getKeyStoreValue(); | |
76 | + if (keyStore != null) { | |
77 | + X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(config.getCertificateAlias()); | |
78 | + return Hex.encodeHexString(serverCertificate.getPublicKey().getEncoded()); | |
102 | 79 | } |
103 | - } catch (GeneralSecurityException | IllegalArgumentException e) { | |
104 | - log.error("[{}] Failed generate Server RPK for profile", e.getMessage()); | |
105 | - throw new RuntimeException(e); | |
80 | + } catch (Exception e) { | |
81 | + log.trace("Failed to fetch public key from key store!", e); | |
82 | + | |
106 | 83 | } |
107 | - return null; | |
84 | + return ""; | |
108 | 85 | } |
86 | + | |
109 | 87 | } |
110 | 88 | ... | ... |
... | ... | @@ -24,6 +24,8 @@ import org.springframework.beans.factory.annotation.Autowired; |
24 | 24 | import org.springframework.context.MessageSource; |
25 | 25 | import org.springframework.context.annotation.Lazy; |
26 | 26 | import org.springframework.core.NestedRuntimeException; |
27 | +import org.springframework.core.io.InputStreamSource; | |
28 | +import org.springframework.mail.javamail.JavaMailSender; | |
27 | 29 | import org.springframework.mail.javamail.JavaMailSenderImpl; |
28 | 30 | import org.springframework.mail.javamail.MimeMessageHelper; |
29 | 31 | import org.springframework.stereotype.Service; |
... | ... | @@ -45,8 +47,8 @@ import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
45 | 47 | import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
46 | 48 | |
47 | 49 | import javax.annotation.PostConstruct; |
48 | -import javax.mail.MessagingException; | |
49 | 50 | import javax.mail.internet.MimeMessage; |
51 | +import java.io.ByteArrayInputStream; | |
50 | 52 | import java.util.HashMap; |
51 | 53 | import java.util.Locale; |
52 | 54 | import java.util.Map; |
... | ... | @@ -71,6 +73,9 @@ public class DefaultMailService implements MailService { |
71 | 73 | @Autowired |
72 | 74 | private TbApiUsageStateService apiUsageStateService; |
73 | 75 | |
76 | + @Autowired | |
77 | + private MailExecutorService mailExecutorService; | |
78 | + | |
74 | 79 | private JavaMailSenderImpl mailSender; |
75 | 80 | |
76 | 81 | private String mailFrom; |
... | ... | @@ -220,6 +225,17 @@ public class DefaultMailService implements MailService { |
220 | 225 | } |
221 | 226 | |
222 | 227 | @Override |
228 | + public void sendResetPasswordEmailAsync(String passwordResetLink, String email) { | |
229 | + mailExecutorService.execute(() -> { | |
230 | + try { | |
231 | + this.sendResetPasswordEmail(passwordResetLink, email); | |
232 | + } catch (ThingsboardException e) { | |
233 | + log.error("Error occurred: {} ", e.getMessage()); | |
234 | + } | |
235 | + }); | |
236 | + } | |
237 | + | |
238 | + @Override | |
223 | 239 | public void sendPasswordWasResetEmail(String loginLink, String email) throws ThingsboardException { |
224 | 240 | |
225 | 241 | String subject = messages.getMessage("password.was.reset.subject", null, Locale.US); |
... | ... | @@ -234,22 +250,47 @@ public class DefaultMailService implements MailService { |
234 | 250 | } |
235 | 251 | |
236 | 252 | @Override |
237 | - public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body) throws MessagingException { | |
253 | + public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images) throws ThingsboardException { | |
254 | + sendMail(tenantId, customerId, from, to, cc, bcc, subject, body, isHtml, images, this.mailSender); | |
255 | + } | |
256 | + | |
257 | + @Override | |
258 | + public void send(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException { | |
259 | + sendMail(tenantId, customerId, from, to, cc, bcc, subject, body, isHtml, images, javaMailSender); | |
260 | + } | |
261 | + | |
262 | + private void sendMail(TenantId tenantId, CustomerId customerId, String from, String to, String cc, String bcc, String subject, String body, boolean isHtml, Map<String, String> images, JavaMailSender javaMailSender) throws ThingsboardException { | |
238 | 263 | if (apiUsageStateService.getApiUsageState(tenantId).isEmailSendEnabled()) { |
239 | - MimeMessage mailMsg = mailSender.createMimeMessage(); | |
240 | - MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8"); | |
241 | - helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from); | |
242 | - helper.setTo(to.split("\\s*,\\s*")); | |
243 | - if (!StringUtils.isBlank(cc)) { | |
244 | - helper.setCc(cc.split("\\s*,\\s*")); | |
264 | + try { | |
265 | + MimeMessage mailMsg = javaMailSender.createMimeMessage(); | |
266 | + boolean multipart = (images != null && !images.isEmpty()); | |
267 | + MimeMessageHelper helper = new MimeMessageHelper(mailMsg, multipart, "UTF-8"); | |
268 | + helper.setFrom(StringUtils.isBlank(from) ? mailFrom : from); | |
269 | + helper.setTo(to.split("\\s*,\\s*")); | |
270 | + if (!StringUtils.isBlank(cc)) { | |
271 | + helper.setCc(cc.split("\\s*,\\s*")); | |
272 | + } | |
273 | + if (!StringUtils.isBlank(bcc)) { | |
274 | + helper.setBcc(bcc.split("\\s*,\\s*")); | |
275 | + } | |
276 | + helper.setSubject(subject); | |
277 | + helper.setText(body, isHtml); | |
278 | + | |
279 | + if (multipart) { | |
280 | + for (String imgId : images.keySet()) { | |
281 | + String imgValue = images.get(imgId); | |
282 | + String value = imgValue.replaceFirst("^data:image/[^;]*;base64,?", ""); | |
283 | + byte[] bytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(value); | |
284 | + String contentType = helper.getFileTypeMap().getContentType(imgId); | |
285 | + InputStreamSource iss = () -> new ByteArrayInputStream(bytes); | |
286 | + helper.addInline(imgId, iss, contentType); | |
287 | + } | |
288 | + } | |
289 | + javaMailSender.send(helper.getMimeMessage()); | |
290 | + apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.EMAIL_EXEC_COUNT, 1); | |
291 | + } catch (Exception e) { | |
292 | + throw handleException(e); | |
245 | 293 | } |
246 | - if (!StringUtils.isBlank(bcc)) { | |
247 | - helper.setBcc(bcc.split("\\s*,\\s*")); | |
248 | - } | |
249 | - helper.setSubject(subject); | |
250 | - helper.setText(body); | |
251 | - mailSender.send(helper.getMimeMessage()); | |
252 | - apiUsageClient.report(tenantId, customerId, ApiUsageRecordKey.EMAIL_EXEC_COUNT, 1); | |
253 | 294 | } else { |
254 | 295 | throw new RuntimeException("Email sending is disabled due to API limits!"); |
255 | 296 | } | ... | ... |
... | ... | @@ -32,6 +32,7 @@ import org.thingsboard.server.common.msg.queue.ServiceQueue; |
32 | 32 | import org.thingsboard.server.common.msg.queue.ServiceType; |
33 | 33 | import org.thingsboard.server.common.msg.queue.TbCallback; |
34 | 34 | import org.thingsboard.server.common.msg.queue.TbMsgCallback; |
35 | +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; | |
35 | 36 | import org.thingsboard.server.common.stats.StatsFactory; |
36 | 37 | import org.thingsboard.server.common.transport.util.DataDecodingEncodingService; |
37 | 38 | import org.thingsboard.server.gen.transport.TransportProtos; |
... | ... | @@ -64,12 +65,14 @@ import java.util.Collections; |
64 | 65 | import java.util.HashSet; |
65 | 66 | import java.util.List; |
66 | 67 | import java.util.Map; |
68 | +import java.util.Queue; | |
67 | 69 | import java.util.Set; |
68 | 70 | import java.util.UUID; |
69 | 71 | import java.util.concurrent.ConcurrentHashMap; |
70 | 72 | import java.util.concurrent.ConcurrentMap; |
71 | 73 | import java.util.concurrent.ExecutorService; |
72 | 74 | import java.util.concurrent.Executors; |
75 | +import java.util.concurrent.ScheduledExecutorService; | |
73 | 76 | import java.util.concurrent.TimeUnit; |
74 | 77 | |
75 | 78 | @Service |
... | ... | @@ -79,12 +82,15 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
79 | 82 | |
80 | 83 | public static final String SUCCESSFUL_STATUS = "successful"; |
81 | 84 | public static final String FAILED_STATUS = "failed"; |
85 | + public static final String THREAD_TOPIC_SEPARATOR = " | "; | |
82 | 86 | @Value("${queue.rule-engine.poll-interval}") |
83 | 87 | private long pollDuration; |
84 | 88 | @Value("${queue.rule-engine.pack-processing-timeout}") |
85 | 89 | private long packProcessingTimeout; |
86 | 90 | @Value("${queue.rule-engine.stats.enabled:true}") |
87 | 91 | private boolean statsEnabled; |
92 | + @Value("${queue.rule-engine.prometheus-stats.enabled:false}") | |
93 | + boolean prometheusStatsEnabled; | |
88 | 94 | |
89 | 95 | private final StatsFactory statsFactory; |
90 | 96 | private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory; |
... | ... | @@ -96,7 +102,9 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
96 | 102 | private final ConcurrentMap<String, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>(); |
97 | 103 | private final ConcurrentMap<String, TbRuleEngineQueueConfiguration> consumerConfigurations = new ConcurrentHashMap<>(); |
98 | 104 | private final ConcurrentMap<String, TbRuleEngineConsumerStats> consumerStats = new ConcurrentHashMap<>(); |
99 | - private ExecutorService submitExecutor; | |
105 | + private final ConcurrentMap<String, TbTopicWithConsumerPerPartition> topicsConsumerPerPartition = new ConcurrentHashMap<>(); | |
106 | + final ExecutorService submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-service-submit-executor")); | |
107 | + final ScheduledExecutorService repartitionExecutor = Executors.newScheduledThreadPool(1, ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-repartition-executor")); | |
100 | 108 | |
101 | 109 | public DefaultTbRuleEngineConsumerService(TbRuleEngineProcessingStrategyFactory processingStrategyFactory, |
102 | 110 | TbRuleEngineSubmitStrategyFactory submitStrategyFactory, |
... | ... | @@ -125,18 +133,19 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
125 | 133 | super.init("tb-rule-engine-consumer", "tb-rule-engine-notifications-consumer"); |
126 | 134 | for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) { |
127 | 135 | consumerConfigurations.putIfAbsent(configuration.getName(), configuration); |
128 | - consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration)); | |
129 | 136 | consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), statsFactory)); |
137 | + if (!configuration.isConsumerPerPartition()) { | |
138 | + consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration)); | |
139 | + } else { | |
140 | + topicsConsumerPerPartition.computeIfAbsent(configuration.getName(), TbTopicWithConsumerPerPartition::new); | |
141 | + } | |
130 | 142 | } |
131 | - submitExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("tb-rule-engine-consumer-service-submit-executor")); | |
132 | 143 | } |
133 | 144 | |
134 | 145 | @PreDestroy |
135 | 146 | public void stop() { |
136 | 147 | super.destroy(); |
137 | - if (submitExecutor != null) { | |
138 | - submitExecutor.shutdownNow(); | |
139 | - } | |
148 | + submitExecutor.shutdownNow(); | |
140 | 149 | ruleEngineSettings.getQueues().forEach(config -> consumerConfigurations.put(config.getName(), config)); |
141 | 150 | } |
142 | 151 | |
... | ... | @@ -145,96 +154,185 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService< |
145 | 154 | if (event.getServiceType().equals(getServiceType())) { |
146 | 155 | ServiceQueue serviceQueue = event.getServiceQueueKey().getServiceQueue(); |
147 | 156 | log.info("[{}] Subscribing to partitions: {}", serviceQueue.getQueue(), event.getPartitions()); |
148 | - consumers.get(serviceQueue.getQueue()).subscribe(event.getPartitions()); | |
157 | + if (!consumerConfigurations.get(serviceQueue.getQueue()).isConsumerPerPartition()) { | |
158 | + consumers.get(serviceQueue.getQueue()).subscribe(event.getPartitions()); | |
159 | + } else { | |
160 | + log.info("[{}] Subscribing consumer per partition: {}", serviceQueue.getQueue(), event.getPartitions()); | |
161 | + subscribeConsumerPerPartition(serviceQueue.getQueue(), event.getPartitions()); | |
162 | + } | |
163 | + } | |
164 | + } | |
165 | + | |
166 | + void subscribeConsumerPerPartition(String queue, Set<TopicPartitionInfo> partitions) { | |
167 | + topicsConsumerPerPartition.get(queue).getSubscribeQueue().add(partitions); | |
168 | + scheduleTopicRepartition(queue); | |
169 | + } | |
170 | + | |
171 | + private void scheduleTopicRepartition(String queue) { | |
172 | + repartitionExecutor.schedule(() -> repartitionTopicWithConsumerPerPartition(queue), 1, TimeUnit.SECONDS); | |
173 | + } | |
174 | + | |
175 | + void repartitionTopicWithConsumerPerPartition(final String queueName) { | |
176 | + if (stopped) { | |
177 | + return; | |
178 | + } | |
179 | + TbTopicWithConsumerPerPartition tbTopicWithConsumerPerPartition = topicsConsumerPerPartition.get(queueName); | |
180 | + Queue<Set<TopicPartitionInfo>> subscribeQueue = tbTopicWithConsumerPerPartition.getSubscribeQueue(); | |
181 | + if (subscribeQueue.isEmpty()) { | |
182 | + return; | |
149 | 183 | } |
184 | + if (tbTopicWithConsumerPerPartition.getLock().tryLock()) { | |
185 | + try { | |
186 | + Set<TopicPartitionInfo> partitions = null; | |
187 | + while (!subscribeQueue.isEmpty()) { | |
188 | + partitions = subscribeQueue.poll(); | |
189 | + } | |
190 | + if (partitions == null) { | |
191 | + return; | |
192 | + } | |
193 | + | |
194 | + Set<TopicPartitionInfo> addedPartitions = new HashSet<>(partitions); | |
195 | + ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers = tbTopicWithConsumerPerPartition.getConsumers(); | |
196 | + addedPartitions.removeAll(consumers.keySet()); | |
197 | + log.info("calculated addedPartitions {}", addedPartitions); | |
198 | + | |
199 | + Set<TopicPartitionInfo> removedPartitions = new HashSet<>(consumers.keySet()); | |
200 | + removedPartitions.removeAll(partitions); | |
201 | + log.info("calculated removedPartitions {}", removedPartitions); | |
202 | + | |
203 | + removedPartitions.forEach((tpi) -> { | |
204 | + removeConsumerForTopicByTpi(queueName, consumers, tpi); | |
205 | + }); | |
206 | + | |
207 | + addedPartitions.forEach((tpi) -> { | |
208 | + log.info("[{}] Adding consumer for topic: {}", queueName, tpi); | |
209 | + TbRuleEngineQueueConfiguration configuration = consumerConfigurations.get(queueName); | |
210 | + TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer = tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration); | |
211 | + consumers.put(tpi, consumer); | |
212 | + launchConsumer(consumer, consumerConfigurations.get(queueName), consumerStats.get(queueName), "" + queueName + "-" + tpi.getPartition().orElse(-999999)); | |
213 | + consumer.subscribe(Collections.singleton(tpi)); | |
214 | + }); | |
215 | + | |
216 | + } finally { | |
217 | + tbTopicWithConsumerPerPartition.getLock().unlock(); | |
218 | + } | |
219 | + } else { | |
220 | + scheduleTopicRepartition(queueName); //reschedule later | |
221 | + } | |
222 | + | |
223 | + } | |
224 | + | |
225 | + void removeConsumerForTopicByTpi(String queue, ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>>> consumers, TopicPartitionInfo tpi) { | |
226 | + log.info("[{}] Removing consumer for topic: {}", queue, tpi); | |
227 | + consumers.get(tpi).unsubscribe(); | |
228 | + consumers.remove(tpi); | |
150 | 229 | } |
151 | 230 | |
152 | 231 | @Override |
153 | 232 | protected void launchMainConsumers() { |
154 | - consumers.forEach((queue, consumer) -> launchConsumer(consumer, consumerConfigurations.get(queue), consumerStats.get(queue))); | |
233 | + consumers.forEach((queue, consumer) -> launchConsumer(consumer, consumerConfigurations.get(queue), consumerStats.get(queue), queue)); | |
155 | 234 | } |
156 | 235 | |
157 | 236 | @Override |
158 | 237 | protected void stopMainConsumers() { |
159 | 238 | consumers.values().forEach(TbQueueConsumer::unsubscribe); |
239 | + topicsConsumerPerPartition.values().forEach(tbTopicWithConsumerPerPartition -> tbTopicWithConsumerPerPartition.getConsumers().keySet() | |
240 | + .forEach((tpi) -> removeConsumerForTopicByTpi(tbTopicWithConsumerPerPartition.getTopic(), tbTopicWithConsumerPerPartition.getConsumers(), tpi))); | |
241 | + } | |
242 | + | |
243 | + void launchConsumer(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) { | |
244 | + consumersExecutor.execute(() -> consumerLoop(consumer, configuration, stats, threadSuffix)); | |
160 | 245 | } |
161 | 246 | |
162 | - private void launchConsumer(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats) { | |
163 | - consumersExecutor.execute(() -> { | |
164 | - Thread.currentThread().setName("" + Thread.currentThread().getName() + "-" + configuration.getName()); | |
165 | - while (!stopped) { | |
166 | - try { | |
167 | - List<TbProtoQueueMsg<ToRuleEngineMsg>> msgs = consumer.poll(pollDuration); | |
168 | - if (msgs.isEmpty()) { | |
169 | - continue; | |
247 | + void consumerLoop(TbQueueConsumer<TbProtoQueueMsg<ToRuleEngineMsg>> consumer, TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, String threadSuffix) { | |
248 | + updateCurrentThreadName(threadSuffix); | |
249 | + while (!stopped && !consumer.isStopped()) { | |
250 | + try { | |
251 | + List<TbProtoQueueMsg<ToRuleEngineMsg>> msgs = consumer.poll(pollDuration); | |
252 | + if (msgs.isEmpty()) { | |
253 | + continue; | |
254 | + } | |
255 | + final TbRuleEngineSubmitStrategy submitStrategy = getSubmitStrategy(configuration); | |
256 | + final TbRuleEngineProcessingStrategy ackStrategy = getAckStrategy(configuration); | |
257 | + submitStrategy.init(msgs); | |
258 | + while (!stopped) { | |
259 | + TbMsgPackProcessingContext ctx = new TbMsgPackProcessingContext(configuration.getName(), submitStrategy); | |
260 | + submitStrategy.submitAttempt((id, msg) -> submitExecutor.submit(() -> submitMessage(configuration, stats, ctx, id, msg))); | |
261 | + | |
262 | + final boolean timeout = !ctx.await(configuration.getPackProcessingTimeout(), TimeUnit.MILLISECONDS); | |
263 | + | |
264 | + TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx); | |
265 | + if (timeout) { | |
266 | + printFirstOrAll(configuration, ctx, ctx.getPendingMap(), "Timeout"); | |
170 | 267 | } |
171 | - TbRuleEngineSubmitStrategy submitStrategy = submitStrategyFactory.newInstance(configuration.getName(), configuration.getSubmitStrategy()); | |
172 | - TbRuleEngineProcessingStrategy ackStrategy = processingStrategyFactory.newInstance(configuration.getName(), configuration.getProcessingStrategy()); | |
173 | - | |
174 | - submitStrategy.init(msgs); | |
175 | - | |
176 | - while (!stopped) { | |
177 | - TbMsgPackProcessingContext ctx = new TbMsgPackProcessingContext(configuration.getName(), submitStrategy); | |
178 | - submitStrategy.submitAttempt((id, msg) -> submitExecutor.submit(() -> { | |
179 | - log.trace("[{}] Creating callback for message: {}", id, msg.getValue()); | |
180 | - ToRuleEngineMsg toRuleEngineMsg = msg.getValue(); | |
181 | - TenantId tenantId = new TenantId(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB())); | |
182 | - TbMsgCallback callback = statsEnabled ? | |
183 | - new TbMsgPackCallback(id, tenantId, ctx, stats.getTimer(tenantId, SUCCESSFUL_STATUS), stats.getTimer(tenantId, FAILED_STATUS)) : | |
184 | - new TbMsgPackCallback(id, tenantId, ctx); | |
185 | - try { | |
186 | - if (!toRuleEngineMsg.getTbMsg().isEmpty()) { | |
187 | - forwardToRuleEngineActor(configuration.getName(), tenantId, toRuleEngineMsg, callback); | |
188 | - } else { | |
189 | - callback.onSuccess(); | |
190 | - } | |
191 | - } catch (Exception e) { | |
192 | - callback.onFailure(new RuleEngineException(e.getMessage())); | |
193 | - } | |
194 | - })); | |
195 | - | |
196 | - boolean timeout = false; | |
197 | - if (!ctx.await(configuration.getPackProcessingTimeout(), TimeUnit.MILLISECONDS)) { | |
198 | - timeout = true; | |
199 | - } | |
200 | - | |
201 | - TbRuleEngineProcessingResult result = new TbRuleEngineProcessingResult(configuration.getName(), timeout, ctx); | |
202 | - if (timeout) { | |
203 | - printFirstOrAll(configuration, ctx, ctx.getPendingMap(), "Timeout"); | |
204 | - } | |
205 | - if (!ctx.getFailedMap().isEmpty()) { | |
206 | - printFirstOrAll(configuration, ctx, ctx.getFailedMap(), "Failed"); | |
207 | - } | |
208 | - ctx.printProfilerStats(); | |
209 | - | |
210 | - TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result); | |
211 | - if (statsEnabled) { | |
212 | - stats.log(result, decision.isCommit()); | |
213 | - } | |
214 | - | |
215 | - ctx.cleanup(); | |
216 | - | |
217 | - if (decision.isCommit()) { | |
218 | - submitStrategy.stop(); | |
219 | - break; | |
220 | - } else { | |
221 | - submitStrategy.update(decision.getReprocessMap()); | |
222 | - } | |
268 | + if (!ctx.getFailedMap().isEmpty()) { | |
269 | + printFirstOrAll(configuration, ctx, ctx.getFailedMap(), "Failed"); | |
223 | 270 | } |
224 | - consumer.commit(); | |
225 | - } catch (Exception e) { | |
226 | - if (!stopped) { | |
227 | - log.warn("Failed to process messages from queue.", e); | |
228 | - try { | |
229 | - Thread.sleep(pollDuration); | |
230 | - } catch (InterruptedException e2) { | |
231 | - log.trace("Failed to wait until the server has capacity to handle new requests", e2); | |
232 | - } | |
271 | + ctx.printProfilerStats(); | |
272 | + | |
273 | + TbRuleEngineProcessingDecision decision = ackStrategy.analyze(result); | |
274 | + if (statsEnabled) { | |
275 | + stats.log(result, decision.isCommit()); | |
276 | + } | |
277 | + | |
278 | + ctx.cleanup(); | |
279 | + | |
280 | + if (decision.isCommit()) { | |
281 | + submitStrategy.stop(); | |
282 | + break; | |
283 | + } else { | |
284 | + submitStrategy.update(decision.getReprocessMap()); | |
285 | + } | |
286 | + } | |
287 | + consumer.commit(); | |
288 | + } catch (Exception e) { | |
289 | + if (!stopped) { | |
290 | + log.warn("Failed to process messages from queue.", e); | |
291 | + try { | |
292 | + Thread.sleep(pollDuration); | |
293 | + } catch (InterruptedException e2) { | |
294 | + log.trace("Failed to wait until the server has capacity to handle new requests", e2); | |
233 | 295 | } |
234 | 296 | } |
235 | 297 | } |
236 | - log.info("TB Rule Engine Consumer stopped."); | |
237 | - }); | |
298 | + } | |
299 | + log.info("TB Rule Engine Consumer stopped."); | |
300 | + } | |
301 | + | |
302 | + void updateCurrentThreadName(String threadSuffix) { | |
303 | + String name = Thread.currentThread().getName(); | |
304 | + int spliteratorIndex = name.indexOf(THREAD_TOPIC_SEPARATOR); | |
305 | + if (spliteratorIndex > 0) { | |
306 | + name = name.substring(0, spliteratorIndex); | |
307 | + } | |
308 | + name = name + THREAD_TOPIC_SEPARATOR + threadSuffix; | |
309 | + Thread.currentThread().setName(name); | |
310 | + } | |
311 | + | |
312 | + TbRuleEngineProcessingStrategy getAckStrategy(TbRuleEngineQueueConfiguration configuration) { | |
313 | + return processingStrategyFactory.newInstance(configuration.getName(), configuration.getProcessingStrategy()); | |
314 | + } | |
315 | + | |
316 | + TbRuleEngineSubmitStrategy getSubmitStrategy(TbRuleEngineQueueConfiguration configuration) { | |
317 | + return submitStrategyFactory.newInstance(configuration.getName(), configuration.getSubmitStrategy()); | |
318 | + } | |
319 | + | |
320 | + void submitMessage(TbRuleEngineQueueConfiguration configuration, TbRuleEngineConsumerStats stats, TbMsgPackProcessingContext ctx, UUID id, TbProtoQueueMsg<ToRuleEngineMsg> msg) { | |
321 | + log.trace("[{}] Creating callback for topic {} message: {}", id, configuration.getName(), msg.getValue()); | |
322 | + ToRuleEngineMsg toRuleEngineMsg = msg.getValue(); | |
323 | + TenantId tenantId = new TenantId(new UUID(toRuleEngineMsg.getTenantIdMSB(), toRuleEngineMsg.getTenantIdLSB())); | |
324 | + TbMsgCallback callback = prometheusStatsEnabled ? | |
325 | + new TbMsgPackCallback(id, tenantId, ctx, stats.getTimer(tenantId, SUCCESSFUL_STATUS), stats.getTimer(tenantId, FAILED_STATUS)) : | |
326 | + new TbMsgPackCallback(id, tenantId, ctx); | |
327 | + try { | |
328 | + if (toRuleEngineMsg.getTbMsg() != null && !toRuleEngineMsg.getTbMsg().isEmpty()) { | |
329 | + forwardToRuleEngineActor(configuration.getName(), tenantId, toRuleEngineMsg, callback); | |
330 | + } else { | |
331 | + callback.onSuccess(); | |
332 | + } | |
333 | + } catch (Exception e) { | |
334 | + callback.onFailure(new RuleEngineException(e.getMessage())); | |
335 | + } | |
238 | 336 | } |
239 | 337 | |
240 | 338 | private void printFirstOrAll(TbRuleEngineQueueConfiguration configuration, TbMsgPackProcessingContext ctx, Map<UUID, TbProtoQueueMsg<ToRuleEngineMsg>> map, String prefix) { | ... | ... |
application/src/main/java/org/thingsboard/server/service/queue/TbTopicWithConsumerPerPartition.java
0 → 100644
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.service.queue; | |
17 | + | |
18 | +import lombok.Builder; | |
19 | +import lombok.Data; | |
20 | +import lombok.Getter; | |
21 | +import lombok.RequiredArgsConstructor; | |
22 | +import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; | |
23 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
24 | +import org.thingsboard.server.queue.TbQueueConsumer; | |
25 | +import org.thingsboard.server.queue.common.TbProtoQueueMsg; | |
26 | + | |
27 | +import java.util.Collections; | |
28 | +import java.util.Map; | |
29 | +import java.util.Queue; | |
30 | +import java.util.Set; | |
31 | +import java.util.concurrent.ConcurrentHashMap; | |
32 | +import java.util.concurrent.ConcurrentLinkedQueue; | |
33 | +import java.util.concurrent.ConcurrentMap; | |
34 | +import java.util.concurrent.locks.ReentrantLock; | |
35 | + | |
36 | +@RequiredArgsConstructor | |
37 | +@Data | |
38 | +public class TbTopicWithConsumerPerPartition { | |
39 | + private final String topic; | |
40 | + @Getter | |
41 | + private final ReentrantLock lock = new ReentrantLock(); //NonfairSync | |
42 | + private volatile Set<TopicPartitionInfo> partitions = Collections.emptySet(); | |
43 | + private final ConcurrentMap<TopicPartitionInfo, TbQueueConsumer<TbProtoQueueMsg<TransportProtos.ToRuleEngineMsg>>> consumers = new ConcurrentHashMap<>(); | |
44 | + private final Queue<Set<TopicPartitionInfo>> subscribeQueue = new ConcurrentLinkedQueue<>(); | |
45 | +} | ... | ... |
... | ... | @@ -206,11 +206,7 @@ public abstract class AbstractOAuth2ClientMapper { |
206 | 206 | } |
207 | 207 | |
208 | 208 | private Optional<DashboardId> getDashboardId(TenantId tenantId, String dashboardName) { |
209 | - PageLink searchTextLink = new PageLink(1, 0, dashboardName); | |
210 | - PageData<DashboardInfo> dashboardsPage = dashboardService.findDashboardsByTenantId(tenantId, searchTextLink); | |
211 | - return dashboardsPage.getData().stream() | |
212 | - .findAny() | |
213 | - .map(IdBased::getId); | |
209 | + return Optional.ofNullable(dashboardService.findFirstDashboardInfoByTenantIdAndName(tenantId, dashboardName)).map(IdBased::getId); | |
214 | 210 | } |
215 | 211 | |
216 | 212 | private Optional<DashboardId> getDashboardId(TenantId tenantId, CustomerId customerId, String dashboardName) { | ... | ... |
... | ... | @@ -518,7 +518,7 @@ audit-log: |
518 | 518 | index_pattern: "${AUDIT_LOG_SINK_INDEX_PATTERN:@{TENANT}_AUDIT_LOG_@{DATE}}" |
519 | 519 | # Date format. Details of the pattern could be found here: |
520 | 520 | # https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html |
521 | - date_format: "${AUDIT_LOG_SINK_DATE_FORMAT:YYYY.MM.DD}" | |
521 | + date_format: "${AUDIT_LOG_SINK_DATE_FORMAT:YYYY.MM.dd}" | |
522 | 522 | scheme_name: "${AUDIT_LOG_SINK_SCHEME_NAME:http}" # http or https |
523 | 523 | host: "${AUDIT_LOG_SINK_HOST:localhost}" |
524 | 524 | port: "${AUDIT_LOG_SINK_PORT:9200}" |
... | ... | @@ -595,6 +595,10 @@ transport: |
595 | 595 | ssl: |
596 | 596 | # Enable/disable SSL support |
597 | 597 | enabled: "${MQTT_SSL_ENABLED:false}" |
598 | + # MQTT SSL bind address | |
599 | + bind_address: "${MQTT_SSL_BIND_ADDRESS:0.0.0.0}" | |
600 | + # MQTT SSL bind port | |
601 | + bind_port: "${MQTT_SSL_BIND_PORT:8883}" | |
598 | 602 | # SSL protocol: See http://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#SSLContext |
599 | 603 | protocol: "${MQTT_SSL_PROTOCOL:TLSv1.2}" |
600 | 604 | # Path to the key store that holds the SSL certificate |
... | ... | @@ -621,8 +625,6 @@ transport: |
621 | 625 | bind_address: "${COAP_DTLS_BIND_ADDRESS:0.0.0.0}" |
622 | 626 | # CoAP DTLS bind port |
623 | 627 | bind_port: "${COAP_DTLS_BIND_PORT:5684}" |
624 | - # Secure mode. Allowed values: NO_AUTH, X509 | |
625 | - mode: "${COAP_DTLS_SECURE_MODE:NO_AUTH}" | |
626 | 628 | # Path to the key store that holds the certificate |
627 | 629 | key_store: "${COAP_DTLS_KEY_STORE:coapserver.jks}" |
628 | 630 | # Password used to access the key store |
... | ... | @@ -645,48 +647,40 @@ transport: |
645 | 647 | bind_address: "${LWM2M_BIND_ADDRESS:0.0.0.0}" |
646 | 648 | bind_port: "${LWM2M_BIND_PORT:5685}" |
647 | 649 | security: |
648 | - bind_address: "${LWM2M_BIND_ADDRESS_SECURITY:0.0.0.0}" | |
649 | - bind_port: "${LWM2M_BIND_PORT_SECURITY:5686}" | |
650 | - # Only for RPK: Public & Private Key. If the keystore file is missing or not working | |
651 | - public_x: "${LWM2M_SERVER_PUBLIC_X:05064b9e6762dd8d8b8a52355d7b4d8b9a3d64e6d2ee277d76c248861353f358}" | |
652 | - public_y: "${LWM2M_SERVER_PUBLIC_Y:5eeb1838e4f9e37b31fa347aef5ce3431eb54e0a2506910c5e0298817445721b}" | |
653 | - private_encoded: "${LWM2M_SERVER_PRIVATE_ENCODED:308193020100301306072a8648ce3d020106082a8648ce3d030107047930770201010420dc774b309e547ceb48fee547e104ce201a9c48c449dc5414cd04e7f5cf05f67ba00a06082a8648ce3d030107a1440342000405064b9e6762dd8d8b8a52355d7b4d8b9a3d64e6d2ee277d76c248861353f3585eeb1838e4f9e37b31fa347aef5ce3431eb54e0a2506910c5e0298817445721b}" | |
650 | + bind_address: "${LWM2M_SECURITY_BIND_ADDRESS:0.0.0.0}" | |
651 | + bind_port: "${LWM2M_SECURITY_BIND_PORT:5686}" | |
654 | 652 | # Only Certificate_x509: |
655 | - alias: "${LWM2M_KEYSTORE_ALIAS_SERVER:server}" | |
653 | + key_alias: "${LWM2M_SERVER_KEY_ALIAS:server}" | |
654 | + key_password: "${LWM2M_SERVER_KEY_PASSWORD:server_ks_password}" | |
656 | 655 | skip_validity_check_for_client_cert: "${TB_LWM2M_SERVER_SECURITY_SKIP_VALIDITY_CHECK_FOR_CLIENT_CERT:false}" |
657 | 656 | bootstrap: |
658 | 657 | enable: "${LWM2M_ENABLED_BS:true}" |
659 | 658 | id: "${LWM2M_SERVER_ID_BS:111}" |
660 | - bind_address: "${LWM2M_BIND_ADDRESS_BS:0.0.0.0}" | |
661 | - bind_port: "${LWM2M_BIND_PORT_BS:5687}" | |
659 | + bind_address: "${LWM2M_BS_BIND_ADDRESS:0.0.0.0}" | |
660 | + bind_port: "${LWM2M_BS_BIND_PORT:5687}" | |
662 | 661 | security: |
663 | - bind_address: "${LWM2M_BIND_ADDRESS_SECURITY_BS:0.0.0.0}" | |
664 | - bind_port: "${LWM2M_BIND_PORT_SECURITY_BS:5688}" | |
665 | - # Only for RPK: Public & Private Key. If the keystore file is missing or not working | |
666 | - public_x: "${LWM2M_SERVER_PUBLIC_X_BS:5017c87a1c1768264656b3b355434b0def6edb8b9bf166a4762d9930cd730f91}" | |
667 | - public_y: "${LWM2M_SERVER_PUBLIC_Y_BS:3fc4e61bcd8901ec27c424114c3e887ed372497f0c2cf85839b8443e76988b34}" | |
668 | - private_encoded: "${LWM2M_SERVER_PRIVATE_ENCODED_BS:308193020100301306072a8648ce3d020106082a8648ce3d0301070479307702010104205ecafd90caa7be45c42e1f3f32571632b8409e6e6249d7124f4ba56fab3c8083a00a06082a8648ce3d030107a144034200045017c87a1c1768264656b3b355434b0def6edb8b9bf166a4762d9930cd730f913fc4e61bcd8901ec27c424114c3e887ed372497f0c2cf85839b8443e76988b34}" | |
662 | + bind_address: "${LWM2M_BS_SECURITY_BIND_ADDRESS:0.0.0.0}" | |
663 | + bind_port: "${LWM2M_BS_SECURITY_BIND_PORT:5688}" | |
669 | 664 | # Only Certificate_x509: |
670 | - alias: "${LWM2M_KEYSTORE_ALIAS_BS:bootstrap}" | |
665 | + key_alias: "${LWM2M_BS_KEY_ALIAS:bootstrap}" | |
666 | + key_password: "${LWM2M_BS_KEY_PASSWORD:server_ks_password}" | |
671 | 667 | security: |
672 | 668 | # Certificate_x509: |
673 | 669 | # To get helps about files format and how to generate it, see: https://github.com/eclipse/leshan/wiki/Credential-files-format |
674 | 670 | # Create new X509 Certificates: common/transport/lwm2m/src/main/resources/credentials/shell/lwM2M_credentials.sh |
675 | 671 | key_store_type: "${LWM2M_KEYSTORE_TYPE:JKS}" |
676 | 672 | # key_store_path_file: "${KEY_STORE_PATH_FILE:/common/transport/lwm2m/src/main/resources/credentials/serverKeyStore.jks" |
677 | - key_store: "${LWM2M_KEY_STORE:lwm2mserver.jks}" | |
678 | - key_store_password: "${LWM2M_KEY_STORE_PASSWORD:server_ks_password}" | |
679 | - root_alias: "${LWM2M_SERVER_ROOT_CA:rootca}" | |
680 | - enable_gen_new_key_psk_rpk: "${ENABLE_GEN_NEW_KEY_PSK_RPK:false}" | |
673 | + key_store: "${LWM2M_KEYSTORE:lwm2mserver.jks}" | |
674 | + key_store_password: "${LWM2M_KEYSTORE_PASSWORD:server_ks_password}" | |
675 | + root_alias: "${LWM2M_SERVER_ROOT_CA_ALIAS:rootca}" | |
676 | + recommended_ciphers: "${LWM2M_RECOMMENDED_CIPHERS:false}" | |
677 | + recommended_supported_groups: "${LWM2M_RECOMMENDED_SUPPORTED_GROUPS:true}" | |
681 | 678 | timeout: "${LWM2M_TIMEOUT:120000}" |
682 | - recommended_ciphers: "${LWM2M_RECOMMENDED_CIPHERS:false}" | |
683 | - recommended_supported_groups: "${LWM2M_RECOMMENDED_SUPPORTED_GROUPS:true}" | |
684 | 679 | uplink_pool_size: "${LWM2M_UPLINK_POOL_SIZE:10}" |
685 | 680 | downlink_pool_size: "${LWM2M_DOWNLINK_POOL_SIZE:10}" |
686 | 681 | ota_pool_size: "${LWM2M_OTA_POOL_SIZE:10}" |
687 | - registration_store_pool_size: "${LWM2M_REGISTRATION_STORE_POOL_SIZE:100}" | |
688 | 682 | clean_period_in_sec: "${LWM2M_CLEAN_PERIOD_IN_SEC:2}" |
689 | - log_max_length: "${LWM2M_LOG_MAX_LENGTH:100}" | |
683 | + log_max_length: "${LWM2M_LOG_MAX_LENGTH:1024}" | |
690 | 684 | # Use redis for Security and Registration stores |
691 | 685 | redis.enabled: "${LWM2M_REDIS_ENABLED:false}" |
692 | 686 | snmp: |
... | ... | @@ -872,6 +866,7 @@ queue: |
872 | 866 | topic: "${TB_QUEUE_RE_MAIN_TOPIC:tb_rule_engine.main}" |
873 | 867 | poll-interval: "${TB_QUEUE_RE_MAIN_POLL_INTERVAL_MS:25}" |
874 | 868 | partitions: "${TB_QUEUE_RE_MAIN_PARTITIONS:10}" |
869 | + consumer-per-partition: "${TB_QUEUE_RE_MAIN_CONSUMER_PER_PARTITION:true}" | |
875 | 870 | pack-processing-timeout: "${TB_QUEUE_RE_MAIN_PACK_PROCESSING_TIMEOUT_MS:2000}" |
876 | 871 | submit-strategy: |
877 | 872 | type: "${TB_QUEUE_RE_MAIN_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL |
... | ... | @@ -888,6 +883,7 @@ queue: |
888 | 883 | topic: "${TB_QUEUE_RE_HP_TOPIC:tb_rule_engine.hp}" |
889 | 884 | poll-interval: "${TB_QUEUE_RE_HP_POLL_INTERVAL_MS:25}" |
890 | 885 | partitions: "${TB_QUEUE_RE_HP_PARTITIONS:10}" |
886 | + consumer-per-partition: "${TB_QUEUE_RE_HP_CONSUMER_PER_PARTITION:true}" | |
891 | 887 | pack-processing-timeout: "${TB_QUEUE_RE_HP_PACK_PROCESSING_TIMEOUT_MS:2000}" |
892 | 888 | submit-strategy: |
893 | 889 | type: "${TB_QUEUE_RE_HP_SUBMIT_STRATEGY_TYPE:BURST}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL |
... | ... | @@ -904,6 +900,7 @@ queue: |
904 | 900 | topic: "${TB_QUEUE_RE_SQ_TOPIC:tb_rule_engine.sq}" |
905 | 901 | poll-interval: "${TB_QUEUE_RE_SQ_POLL_INTERVAL_MS:25}" |
906 | 902 | partitions: "${TB_QUEUE_RE_SQ_PARTITIONS:10}" |
903 | + consumer-per-partition: "${TB_QUEUE_RE_SQ_CONSUMER_PER_PARTITION:true}" | |
907 | 904 | pack-processing-timeout: "${TB_QUEUE_RE_SQ_PACK_PROCESSING_TIMEOUT_MS:2000}" |
908 | 905 | submit-strategy: |
909 | 906 | type: "${TB_QUEUE_RE_SQ_SUBMIT_STRATEGY_TYPE:SEQUENTIAL_BY_ORIGINATOR}" # BURST, BATCH, SEQUENTIAL_BY_ORIGINATOR, SEQUENTIAL_BY_TENANT, SEQUENTIAL | ... | ... |
application/src/test/java/org/thingsboard/server/actors/device/DeviceActorMessageProcessorTest.java
0 → 100644
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.actors.device; | |
17 | + | |
18 | +import org.junit.Before; | |
19 | +import org.junit.Test; | |
20 | +import org.thingsboard.common.util.LinkedHashMapRemoveEldest; | |
21 | +import org.thingsboard.server.actors.ActorSystemContext; | |
22 | +import org.thingsboard.server.common.data.id.DeviceId; | |
23 | +import org.thingsboard.server.common.data.id.TenantId; | |
24 | +import org.thingsboard.server.dao.device.DeviceService; | |
25 | + | |
26 | +import static org.hamcrest.CoreMatchers.instanceOf; | |
27 | +import static org.hamcrest.CoreMatchers.is; | |
28 | +import static org.hamcrest.CoreMatchers.notNullValue; | |
29 | +import static org.hamcrest.MatcherAssert.assertThat; | |
30 | +import static org.mockito.BDDMockito.willReturn; | |
31 | +import static org.mockito.Mockito.mock; | |
32 | + | |
33 | +public class DeviceActorMessageProcessorTest { | |
34 | + | |
35 | + public static final long MAX_CONCURRENT_SESSIONS_PER_DEVICE = 10L; | |
36 | + ActorSystemContext systemContext; | |
37 | + DeviceService deviceService; | |
38 | + TenantId tenantId = TenantId.SYS_TENANT_ID; | |
39 | + DeviceId deviceId = DeviceId.fromString("78bf9b26-74ef-4af2-9cfb-ad6cf24ad2ec"); | |
40 | + | |
41 | + DeviceActorMessageProcessor processor; | |
42 | + | |
43 | + @Before | |
44 | + public void setUp() { | |
45 | + systemContext = mock(ActorSystemContext.class); | |
46 | + deviceService = mock(DeviceService.class); | |
47 | + willReturn(MAX_CONCURRENT_SESSIONS_PER_DEVICE).given(systemContext).getMaxConcurrentSessionsPerDevice(); | |
48 | + willReturn(deviceService).given(systemContext).getDeviceService(); | |
49 | + processor = new DeviceActorMessageProcessor(systemContext, tenantId, deviceId); | |
50 | + } | |
51 | + | |
52 | + @Test | |
53 | + public void givenSystemContext_whenNewInstance_thenVerifySessionMapMaxSize() { | |
54 | + assertThat(processor.sessions, instanceOf(LinkedHashMapRemoveEldest.class)); | |
55 | + assertThat(processor.sessions.getMaxEntries(), is(MAX_CONCURRENT_SESSIONS_PER_DEVICE)); | |
56 | + assertThat(processor.sessions.getRemovalConsumer(), notNullValue()); | |
57 | + } | |
58 | +} | |
\ No newline at end of file | ... | ... |
... | ... | @@ -155,6 +155,7 @@ public abstract class BaseUserControllerTest extends AbstractControllerTest { |
155 | 155 | |
156 | 156 | doPost("/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest) |
157 | 157 | .andExpect(status().isOk()); |
158 | + Thread.sleep(1000); | |
158 | 159 | doGet("/api/noauth/resetPassword?resetToken={resetToken}", TestMailService.currentResetPasswordToken) |
159 | 160 | .andExpect(status().isSeeOther()) |
160 | 161 | .andExpect(header().string(HttpHeaders.LOCATION, "/login/resetPassword?resetToken=" + TestMailService.currentResetPasswordToken)); | ... | ... |
... | ... | @@ -51,7 +51,7 @@ public class TestMailService { |
51 | 51 | currentResetPasswordToken = passwordResetLink.split("=")[1]; |
52 | 52 | return null; |
53 | 53 | } |
54 | - }).when(mailService).sendResetPasswordEmail(Mockito.anyString(), Mockito.anyString()); | |
54 | + }).when(mailService).sendResetPasswordEmailAsync(Mockito.anyString(), Mockito.anyString()); | |
55 | 55 | return mailService; |
56 | 56 | } |
57 | 57 | ... | ... |
... | ... | @@ -20,7 +20,6 @@ import org.apache.commons.io.IOUtils; |
20 | 20 | import org.eclipse.californium.core.network.config.NetworkConfig; |
21 | 21 | import org.eclipse.leshan.client.object.Security; |
22 | 22 | import org.eclipse.leshan.core.util.Hex; |
23 | -import org.jetbrains.annotations.NotNull; | |
24 | 23 | import org.junit.After; |
25 | 24 | import org.junit.Assert; |
26 | 25 | import org.junit.Before; |
... | ... | @@ -85,6 +84,7 @@ import java.util.concurrent.ScheduledExecutorService; |
85 | 84 | import static org.eclipse.leshan.client.object.Security.noSec; |
86 | 85 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
87 | 86 | import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE; |
87 | +import static org.thingsboard.server.common.data.ota.OtaPackageType.SOFTWARE; | |
88 | 88 | |
89 | 89 | @DaoSqlTest |
90 | 90 | public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { |
... | ... | @@ -264,7 +264,7 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { |
264 | 264 | executor = Executors.newScheduledThreadPool(10); |
265 | 265 | loginTenantAdmin(); |
266 | 266 | |
267 | - String[] resources = new String[]{"1.xml", "2.xml", "3.xml"}; | |
267 | + String[] resources = new String[]{"1.xml", "2.xml", "3.xml", "5.xml", "9.xml"}; | |
268 | 268 | for (String resourceName : resources) { |
269 | 269 | TbResource lwModel = new TbResource(); |
270 | 270 | lwModel.setResourceType(ResourceType.LWM2M_MODEL); |
... | ... | @@ -300,7 +300,6 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { |
300 | 300 | Assert.assertNotNull(deviceProfile); |
301 | 301 | } |
302 | 302 | |
303 | - @NotNull | |
304 | 303 | protected Device createDevice(LwM2MClientCredentials clientCredentials) throws Exception { |
305 | 304 | Device device = new Device(); |
306 | 305 | device.setName("Device A"); |
... | ... | @@ -340,6 +339,22 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { |
340 | 339 | return savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, "SHA256"); |
341 | 340 | } |
342 | 341 | |
342 | + protected OtaPackageInfo createSoftware() throws Exception { | |
343 | + String CHECKSUM = "4bf5122f344554c53bde2ebb8cd2b7e3d1600ad631c385a5d7cce23c7785459a"; | |
344 | + | |
345 | + OtaPackageInfo swInfo = new OtaPackageInfo(); | |
346 | + swInfo.setDeviceProfileId(deviceProfile.getId()); | |
347 | + swInfo.setType(SOFTWARE); | |
348 | + swInfo.setTitle("My sw"); | |
349 | + swInfo.setVersion("v1.0"); | |
350 | + | |
351 | + OtaPackageInfo savedFirmwareInfo = doPost("/api/otaPackage", swInfo, OtaPackageInfo.class); | |
352 | + | |
353 | + MockMultipartFile testData = new MockMultipartFile("file", "filename.txt", "text/plain", new byte[]{1}); | |
354 | + | |
355 | + return savaData("/api/otaPackage/" + savedFirmwareInfo.getId().getId().toString() + "?checksum={checksum}&checksumAlgorithm={checksumAlgorithm}", testData, CHECKSUM, "SHA256"); | |
356 | + } | |
357 | + | |
343 | 358 | protected OtaPackageInfo savaData(String urlTemplate, MockMultipartFile content, String... params) throws Exception { |
344 | 359 | MockMultipartHttpServletRequestBuilder postRequest = MockMvcRequestBuilders.multipart(urlTemplate, params); |
345 | 360 | postRequest.file(content); | ... | ... |
... | ... | @@ -15,11 +15,15 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.transport.lwm2m; |
17 | 17 | |
18 | +import com.fasterxml.jackson.core.type.TypeReference; | |
18 | 19 | import org.junit.Assert; |
19 | 20 | import org.junit.Test; |
20 | 21 | import org.thingsboard.server.common.data.Device; |
21 | 22 | import org.thingsboard.server.common.data.OtaPackageInfo; |
22 | 23 | import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials; |
24 | +import org.thingsboard.server.common.data.kv.KvEntry; | |
25 | +import org.thingsboard.server.common.data.kv.TsKvEntry; | |
26 | +import org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus; | |
23 | 27 | import org.thingsboard.server.common.data.query.EntityData; |
24 | 28 | import org.thingsboard.server.common.data.query.EntityDataPageLink; |
25 | 29 | import org.thingsboard.server.common.data.query.EntityDataQuery; |
... | ... | @@ -32,11 +36,99 @@ import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate; |
32 | 36 | import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd; |
33 | 37 | import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient; |
34 | 38 | |
39 | +import java.util.Arrays; | |
35 | 40 | import java.util.Collections; |
36 | 41 | import java.util.List; |
42 | +import java.util.stream.Collectors; | |
43 | + | |
44 | +import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries; | |
45 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED; | |
46 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING; | |
47 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.INITIATED; | |
48 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.QUEUED; | |
49 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.UPDATED; | |
50 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.UPDATING; | |
51 | +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.VERIFIED; | |
37 | 52 | |
38 | 53 | public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { |
39 | 54 | |
55 | + private final String OTA_TRANSPORT_CONFIGURATION = "{\n" + | |
56 | + " \"observeAttr\": {\n" + | |
57 | + " \"keyName\": {\n" + | |
58 | + " \"/5_1.0/0/3\": \"state\",\n" + | |
59 | + " \"/5_1.0/0/5\": \"updateResult\",\n" + | |
60 | + " \"/5_1.0/0/6\": \"pkgname\",\n" + | |
61 | + " \"/5_1.0/0/7\": \"pkgversion\",\n" + | |
62 | + " \"/5_1.0/0/9\": \"firmwareUpdateDeliveryMethod\",\n" + | |
63 | + " \"/9_1.0/0/0\": \"pkgname\",\n" + | |
64 | + " \"/9_1.0/0/1\": \"pkgversion\",\n" + | |
65 | + " \"/9_1.0/0/7\": \"updateState\",\n" + | |
66 | + " \"/9_1.0/0/9\": \"updateResult\"\n" + | |
67 | + " },\n" + | |
68 | + " \"observe\": [\n" + | |
69 | + " \"/5_1.0/0/3\",\n" + | |
70 | + " \"/5_1.0/0/5\",\n" + | |
71 | + " \"/5_1.0/0/6\",\n" + | |
72 | + " \"/5_1.0/0/7\",\n" + | |
73 | + " \"/5_1.0/0/9\",\n" + | |
74 | + " \"/9_1.0/0/0\",\n" + | |
75 | + " \"/9_1.0/0/1\",\n" + | |
76 | + " \"/9_1.0/0/7\",\n" + | |
77 | + " \"/9_1.0/0/9\"\n" + | |
78 | + " ],\n" + | |
79 | + " \"attribute\": [],\n" + | |
80 | + " \"telemetry\": [\n" + | |
81 | + " \"/5_1.0/0/3\",\n" + | |
82 | + " \"/5_1.0/0/5\",\n" + | |
83 | + " \"/5_1.0/0/6\",\n" + | |
84 | + " \"/5_1.0/0/7\",\n" + | |
85 | + " \"/5_1.0/0/9\",\n" + | |
86 | + " \"/9_1.0/0/0\",\n" + | |
87 | + " \"/9_1.0/0/1\",\n" + | |
88 | + " \"/9_1.0/0/7\",\n" + | |
89 | + " \"/9_1.0/0/9\"\n" + | |
90 | + " ],\n" + | |
91 | + " \"attributeLwm2m\": {}\n" + | |
92 | + " },\n" + | |
93 | + " \"bootstrap\": {\n" + | |
94 | + " \"servers\": {\n" + | |
95 | + " \"binding\": \"UQ\",\n" + | |
96 | + " \"shortId\": 123,\n" + | |
97 | + " \"lifetime\": 300,\n" + | |
98 | + " \"notifIfDisabled\": true,\n" + | |
99 | + " \"defaultMinPeriod\": 1\n" + | |
100 | + " },\n" + | |
101 | + " \"lwm2mServer\": {\n" + | |
102 | + " \"host\": \"localhost\",\n" + | |
103 | + " \"port\": 5685,\n" + | |
104 | + " \"serverId\": 123,\n" + | |
105 | + " \"securityMode\": \"NO_SEC\",\n" + | |
106 | + " \"serverPublicKey\": \"\",\n" + | |
107 | + " \"clientHoldOffTime\": 1,\n" + | |
108 | + " \"bootstrapServerAccountTimeout\": 0\n" + | |
109 | + " },\n" + | |
110 | + " \"bootstrapServer\": {\n" + | |
111 | + " \"host\": \"localhost\",\n" + | |
112 | + " \"port\": 5687,\n" + | |
113 | + " \"serverId\": 111,\n" + | |
114 | + " \"securityMode\": \"NO_SEC\",\n" + | |
115 | + " \"serverPublicKey\": \"\",\n" + | |
116 | + " \"clientHoldOffTime\": 1,\n" + | |
117 | + " \"bootstrapServerAccountTimeout\": 0\n" + | |
118 | + " }\n" + | |
119 | + " },\n" + | |
120 | + " \"clientLwM2mSettings\": {\n" + | |
121 | + " \"fwUpdateStrategy\": 1,\n" + | |
122 | + " \"swUpdateStrategy\": 1,\n" + | |
123 | + " \"clientOnlyObserveAfterConnect\": 1,\n" + | |
124 | + " \"powerMode\": \"PSM\",\n" + | |
125 | + " \"fwUpdateResource\": \"\",\n" + | |
126 | + " \"swUpdateResource\": \"\",\n" + | |
127 | + " \"compositeOperationsSupport\": false\n" + | |
128 | + " },\n" + | |
129 | + " \"type\": \"LWM2M\"\n" + | |
130 | + "}"; | |
131 | + | |
40 | 132 | @Test |
41 | 133 | public void testConnectAndObserveTelemetry() throws Exception { |
42 | 134 | NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); |
... | ... | @@ -94,4 +186,68 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { |
94 | 186 | client.destroy(); |
95 | 187 | } |
96 | 188 | |
189 | + @Test | |
190 | + public void testFirmwareUpdateByObject5() throws Exception { | |
191 | + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); | |
192 | + NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); | |
193 | + clientCredentials.setEndpoint("OTA_" + ENDPOINT); | |
194 | + Device device = createDevice(clientCredentials); | |
195 | + | |
196 | + OtaPackageInfo firmware = createFirmware(); | |
197 | + | |
198 | + LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT); | |
199 | + client.init(SECURITY, COAP_CONFIG); | |
200 | + | |
201 | + Thread.sleep(1000); | |
202 | + | |
203 | + device.setFirmwareId(firmware.getId()); | |
204 | + | |
205 | + device = doPost("/api/device", device, Device.class); | |
206 | + | |
207 | + Thread.sleep(4000); | |
208 | + | |
209 | + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=fw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() { | |
210 | + })); | |
211 | + | |
212 | + List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); | |
213 | + | |
214 | + List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADED, UPDATING, UPDATED); | |
215 | + | |
216 | + Assert.assertEquals(expectedStatuses, statuses); | |
217 | + | |
218 | + client.destroy(); | |
219 | + } | |
220 | + | |
221 | + @Test | |
222 | + public void testSoftwareUpdateByObject9() throws Exception { | |
223 | + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); | |
224 | + NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); | |
225 | + clientCredentials.setEndpoint("OTA_" + ENDPOINT); | |
226 | + Device device = createDevice(clientCredentials); | |
227 | + | |
228 | + OtaPackageInfo software = createSoftware(); | |
229 | + | |
230 | + LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT); | |
231 | + client.init(SECURITY, COAP_CONFIG); | |
232 | + | |
233 | + Thread.sleep(1000); | |
234 | + | |
235 | + device.setSoftwareId(software.getId()); | |
236 | + | |
237 | + device = doPost("/api/device", device, Device.class); | |
238 | + | |
239 | + Thread.sleep(4000); | |
240 | + | |
241 | + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=sw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() { | |
242 | + })); | |
243 | + | |
244 | + List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); | |
245 | + | |
246 | + List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED); | |
247 | + | |
248 | + Assert.assertEquals(expectedStatuses, statuses); | |
249 | + | |
250 | + client.destroy(); | |
251 | + } | |
252 | + | |
97 | 253 | } | ... | ... |
... | ... | @@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.device.credentials.lwm2m.RPKClientCred |
23 | 23 | import static org.eclipse.leshan.client.object.Security.rpk; |
24 | 24 | |
25 | 25 | public class RpkLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { |
26 | + | |
26 | 27 | @Test |
27 | 28 | public void testConnectWithRPKAndObserveTelemetry() throws Exception { |
28 | 29 | RPKClientCredentials rpkClientCredentials = new RPKClientCredentials(); | ... | ... |
application/src/test/java/org/thingsboard/server/transport/lwm2m/client/FwLwM2MDevice.java
0 → 100644
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.client; | |
17 | + | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | +import org.eclipse.leshan.client.resource.BaseInstanceEnabler; | |
20 | +import org.eclipse.leshan.client.servers.ServerIdentity; | |
21 | +import org.eclipse.leshan.core.model.ObjectModel; | |
22 | +import org.eclipse.leshan.core.node.LwM2mResource; | |
23 | +import org.eclipse.leshan.core.response.ExecuteResponse; | |
24 | +import org.eclipse.leshan.core.response.ReadResponse; | |
25 | +import org.eclipse.leshan.core.response.WriteResponse; | |
26 | + | |
27 | +import javax.security.auth.Destroyable; | |
28 | +import java.util.Arrays; | |
29 | +import java.util.List; | |
30 | +import java.util.concurrent.Executors; | |
31 | +import java.util.concurrent.ScheduledExecutorService; | |
32 | +import java.util.concurrent.TimeUnit; | |
33 | +import java.util.concurrent.atomic.AtomicInteger; | |
34 | + | |
35 | +@Slf4j | |
36 | +public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable { | |
37 | + | |
38 | + private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 5, 6, 7, 9); | |
39 | + | |
40 | + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | |
41 | + | |
42 | + private final AtomicInteger state = new AtomicInteger(0); | |
43 | + | |
44 | + private final AtomicInteger updateResult = new AtomicInteger(0); | |
45 | + | |
46 | + @Override | |
47 | + public ReadResponse read(ServerIdentity identity, int resourceId) { | |
48 | + if (!identity.isSystem()) | |
49 | + log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceId); | |
50 | + switch (resourceId) { | |
51 | + case 3: | |
52 | + return ReadResponse.success(resourceId, getState()); | |
53 | + case 5: | |
54 | + return ReadResponse.success(resourceId, getUpdateResult()); | |
55 | + case 6: | |
56 | + return ReadResponse.success(resourceId, getPkgName()); | |
57 | + case 7: | |
58 | + return ReadResponse.success(resourceId, getPkgVersion()); | |
59 | + case 9: | |
60 | + return ReadResponse.success(resourceId, getFirmwareUpdateDeliveryMethod()); | |
61 | + default: | |
62 | + return super.read(identity, resourceId); | |
63 | + } | |
64 | + } | |
65 | + | |
66 | + @Override | |
67 | + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) { | |
68 | + String withParams = null; | |
69 | + if (params != null && params.length() != 0) { | |
70 | + withParams = " with params " + params; | |
71 | + } | |
72 | + log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceId, withParams != null ? withParams : ""); | |
73 | + | |
74 | + switch (resourceId) { | |
75 | + case 2: | |
76 | + startUpdating(); | |
77 | + return ExecuteResponse.success(); | |
78 | + default: | |
79 | + return super.execute(identity, resourceId, params); | |
80 | + } | |
81 | + } | |
82 | + | |
83 | + @Override | |
84 | + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { | |
85 | + log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceId); | |
86 | + | |
87 | + switch (resourceId) { | |
88 | + case 0: | |
89 | + startDownloading(); | |
90 | + return WriteResponse.success(); | |
91 | + case 1: | |
92 | + startDownloading(); | |
93 | + return WriteResponse.success(); | |
94 | + default: | |
95 | + return super.write(identity, replace, resourceId, value); | |
96 | + } | |
97 | + } | |
98 | + | |
99 | + private int getState() { | |
100 | + return state.get(); | |
101 | + } | |
102 | + | |
103 | + private int getUpdateResult() { | |
104 | + return updateResult.get(); | |
105 | + } | |
106 | + | |
107 | + private String getPkgName() { | |
108 | + return "firmware"; | |
109 | + } | |
110 | + | |
111 | + private String getPkgVersion() { | |
112 | + return "1.0.0"; | |
113 | + } | |
114 | + | |
115 | + private int getFirmwareUpdateDeliveryMethod() { | |
116 | + return 1; | |
117 | + } | |
118 | + | |
119 | + @Override | |
120 | + public List<Integer> getAvailableResourceIds(ObjectModel model) { | |
121 | + return supportedResources; | |
122 | + } | |
123 | + | |
124 | + @Override | |
125 | + public void destroy() { | |
126 | + scheduler.shutdownNow(); | |
127 | + } | |
128 | + | |
129 | + private void startDownloading() { | |
130 | + scheduler.schedule(() -> { | |
131 | + state.set(1); | |
132 | + fireResourcesChange(3); | |
133 | + state.set(2); | |
134 | + fireResourcesChange(3); | |
135 | + }, 100, TimeUnit.MILLISECONDS); | |
136 | + } | |
137 | + | |
138 | + private void startUpdating() { | |
139 | + scheduler.schedule(() -> { | |
140 | + try { | |
141 | + state.set(3); | |
142 | + fireResourcesChange(3); | |
143 | + Thread.sleep(100); | |
144 | + updateResult.set(1); | |
145 | + fireResourcesChange(5); | |
146 | + } catch (Exception e) { | |
147 | + | |
148 | + } | |
149 | + }, 100, TimeUnit.MILLISECONDS); | |
150 | + } | |
151 | + | |
152 | +} | ... | ... |
... | ... | @@ -53,8 +53,10 @@ import java.util.List; |
53 | 53 | import java.util.concurrent.ScheduledExecutorService; |
54 | 54 | |
55 | 55 | import static org.eclipse.leshan.core.LwM2mId.DEVICE; |
56 | +import static org.eclipse.leshan.core.LwM2mId.FIRMWARE; | |
56 | 57 | import static org.eclipse.leshan.core.LwM2mId.SECURITY; |
57 | 58 | import static org.eclipse.leshan.core.LwM2mId.SERVER; |
59 | +import static org.eclipse.leshan.core.LwM2mId.SOFTWARE_MANAGEMENT; | |
58 | 60 | |
59 | 61 | @Slf4j |
60 | 62 | @Data |
... | ... | @@ -65,7 +67,7 @@ public class LwM2MTestClient { |
65 | 67 | private LeshanClient client; |
66 | 68 | |
67 | 69 | public void init(Security security, NetworkConfig coapConfig) throws InvalidDDFFileException, IOException { |
68 | - String[] resources = new String[]{"0.xml", "1.xml", "2.xml", "3.xml"}; | |
70 | + String[] resources = new String[]{"0.xml", "1.xml", "2.xml", "3.xml", "5.xml", "9.xml"}; | |
69 | 71 | List<ObjectModel> models = new ArrayList<>(); |
70 | 72 | for (String resourceName : resources) { |
71 | 73 | models.addAll(ObjectLoader.loadDdfFile(LwM2MTestClient.class.getClassLoader().getResourceAsStream("lwm2m/" + resourceName), resourceName)); |
... | ... | @@ -75,6 +77,8 @@ public class LwM2MTestClient { |
75 | 77 | initializer.setInstancesForObject(SECURITY, security); |
76 | 78 | initializer.setInstancesForObject(SERVER, new Server(123, 300)); |
77 | 79 | initializer.setInstancesForObject(DEVICE, new SimpleLwM2MDevice()); |
80 | + initializer.setInstancesForObject(FIRMWARE, new FwLwM2MDevice()); | |
81 | + initializer.setInstancesForObject(SOFTWARE_MANAGEMENT, new SwLwM2MDevice()); | |
78 | 82 | initializer.setClassForObject(LwM2mId.ACCESS_CONTROL, DummyInstanceEnabler.class); |
79 | 83 | |
80 | 84 | DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(); | ... | ... |
... | ... | @@ -45,74 +45,74 @@ public class SimpleLwM2MDevice extends BaseInstanceEnabler implements Destroyabl |
45 | 45 | ); |
46 | 46 | |
47 | 47 | @Override |
48 | - public ReadResponse read(ServerIdentity identity, int resourceid) { | |
48 | + public ReadResponse read(ServerIdentity identity, int resourceId) { | |
49 | 49 | if (!identity.isSystem()) |
50 | - log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceid); | |
51 | - switch (resourceid) { | |
50 | + log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceId); | |
51 | + switch (resourceId) { | |
52 | 52 | case 0: |
53 | - return ReadResponse.success(resourceid, getManufacturer()); | |
53 | + return ReadResponse.success(resourceId, getManufacturer()); | |
54 | 54 | case 1: |
55 | - return ReadResponse.success(resourceid, getModelNumber()); | |
55 | + return ReadResponse.success(resourceId, getModelNumber()); | |
56 | 56 | case 2: |
57 | - return ReadResponse.success(resourceid, getSerialNumber()); | |
57 | + return ReadResponse.success(resourceId, getSerialNumber()); | |
58 | 58 | case 3: |
59 | - return ReadResponse.success(resourceid, getFirmwareVersion()); | |
59 | + return ReadResponse.success(resourceId, getFirmwareVersion()); | |
60 | 60 | case 9: |
61 | - return ReadResponse.success(resourceid, getBatteryLevel()); | |
61 | + return ReadResponse.success(resourceId, getBatteryLevel()); | |
62 | 62 | case 10: |
63 | - return ReadResponse.success(resourceid, getMemoryFree()); | |
63 | + return ReadResponse.success(resourceId, getMemoryFree()); | |
64 | 64 | case 11: |
65 | 65 | Map<Integer, Long> errorCodes = new HashMap<>(); |
66 | 66 | errorCodes.put(0, getErrorCode()); |
67 | - return ReadResponse.success(resourceid, errorCodes, ResourceModel.Type.INTEGER); | |
67 | + return ReadResponse.success(resourceId, errorCodes, ResourceModel.Type.INTEGER); | |
68 | 68 | case 14: |
69 | - return ReadResponse.success(resourceid, getUtcOffset()); | |
69 | + return ReadResponse.success(resourceId, getUtcOffset()); | |
70 | 70 | case 15: |
71 | - return ReadResponse.success(resourceid, getTimezone()); | |
71 | + return ReadResponse.success(resourceId, getTimezone()); | |
72 | 72 | case 16: |
73 | - return ReadResponse.success(resourceid, getSupportedBinding()); | |
73 | + return ReadResponse.success(resourceId, getSupportedBinding()); | |
74 | 74 | case 17: |
75 | - return ReadResponse.success(resourceid, getDeviceType()); | |
75 | + return ReadResponse.success(resourceId, getDeviceType()); | |
76 | 76 | case 18: |
77 | - return ReadResponse.success(resourceid, getHardwareVersion()); | |
77 | + return ReadResponse.success(resourceId, getHardwareVersion()); | |
78 | 78 | case 19: |
79 | - return ReadResponse.success(resourceid, getSoftwareVersion()); | |
79 | + return ReadResponse.success(resourceId, getSoftwareVersion()); | |
80 | 80 | case 20: |
81 | - return ReadResponse.success(resourceid, getBatteryStatus()); | |
81 | + return ReadResponse.success(resourceId, getBatteryStatus()); | |
82 | 82 | case 21: |
83 | - return ReadResponse.success(resourceid, getMemoryTotal()); | |
83 | + return ReadResponse.success(resourceId, getMemoryTotal()); | |
84 | 84 | default: |
85 | - return super.read(identity, resourceid); | |
85 | + return super.read(identity, resourceId); | |
86 | 86 | } |
87 | 87 | } |
88 | 88 | |
89 | 89 | @Override |
90 | - public ExecuteResponse execute(ServerIdentity identity, int resourceid, String params) { | |
90 | + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) { | |
91 | 91 | String withParams = null; |
92 | 92 | if (params != null && params.length() != 0) { |
93 | 93 | withParams = " with params " + params; |
94 | 94 | } |
95 | - log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceid, withParams != null ? withParams : ""); | |
95 | + log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceId, withParams != null ? withParams : ""); | |
96 | 96 | return ExecuteResponse.success(); |
97 | 97 | } |
98 | 98 | |
99 | 99 | @Override |
100 | - public WriteResponse write(ServerIdentity identity, boolean replace, int resourceid, LwM2mResource value) { | |
101 | - log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceid); | |
100 | + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { | |
101 | + log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceId); | |
102 | 102 | |
103 | - switch (resourceid) { | |
103 | + switch (resourceId) { | |
104 | 104 | case 13: |
105 | 105 | return WriteResponse.notFound(); |
106 | 106 | case 14: |
107 | 107 | setUtcOffset((String) value.getValue()); |
108 | - fireResourcesChange(resourceid); | |
108 | + fireResourcesChange(resourceId); | |
109 | 109 | return WriteResponse.success(); |
110 | 110 | case 15: |
111 | 111 | setTimezone((String) value.getValue()); |
112 | - fireResourcesChange(resourceid); | |
112 | + fireResourcesChange(resourceId); | |
113 | 113 | return WriteResponse.success(); |
114 | 114 | default: |
115 | - return super.write(identity, replace, resourceid, value); | |
115 | + return super.write(identity, replace, resourceId, value); | |
116 | 116 | } |
117 | 117 | } |
118 | 118 | ... | ... |
application/src/test/java/org/thingsboard/server/transport/lwm2m/client/SwLwM2MDevice.java
0 → 100644
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.client; | |
17 | + | |
18 | +import lombok.SneakyThrows; | |
19 | +import lombok.extern.slf4j.Slf4j; | |
20 | +import org.eclipse.leshan.client.resource.BaseInstanceEnabler; | |
21 | +import org.eclipse.leshan.client.servers.ServerIdentity; | |
22 | +import org.eclipse.leshan.core.model.ObjectModel; | |
23 | +import org.eclipse.leshan.core.node.LwM2mResource; | |
24 | +import org.eclipse.leshan.core.response.ExecuteResponse; | |
25 | +import org.eclipse.leshan.core.response.ReadResponse; | |
26 | +import org.eclipse.leshan.core.response.WriteResponse; | |
27 | + | |
28 | +import javax.security.auth.Destroyable; | |
29 | +import java.util.Arrays; | |
30 | +import java.util.List; | |
31 | +import java.util.concurrent.Executors; | |
32 | +import java.util.concurrent.ScheduledExecutorService; | |
33 | +import java.util.concurrent.TimeUnit; | |
34 | +import java.util.concurrent.atomic.AtomicInteger; | |
35 | + | |
36 | +@Slf4j | |
37 | +public class SwLwM2MDevice extends BaseInstanceEnabler implements Destroyable { | |
38 | + | |
39 | + private static final List<Integer> supportedResources = Arrays.asList(0, 1, 2, 3, 4, 6, 7, 9); | |
40 | + | |
41 | + private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); | |
42 | + | |
43 | + private final AtomicInteger state = new AtomicInteger(0); | |
44 | + | |
45 | + private final AtomicInteger updateResult = new AtomicInteger(0); | |
46 | + | |
47 | + @Override | |
48 | + public ReadResponse read(ServerIdentity identity, int resourceId) { | |
49 | + if (!identity.isSystem()) | |
50 | + log.info("Read on Device resource /{}/{}/{}", getModel().id, getId(), resourceId); | |
51 | + switch (resourceId) { | |
52 | + case 0: | |
53 | + return ReadResponse.success(resourceId, getPkgName()); | |
54 | + case 1: | |
55 | + return ReadResponse.success(resourceId, getPkgVersion()); | |
56 | + case 7: | |
57 | + return ReadResponse.success(resourceId, getUpdateState()); | |
58 | + case 9: | |
59 | + return ReadResponse.success(resourceId, getUpdateResult()); | |
60 | + default: | |
61 | + return super.read(identity, resourceId); | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + @Override | |
66 | + public ExecuteResponse execute(ServerIdentity identity, int resourceId, String params) { | |
67 | + String withParams = null; | |
68 | + if (params != null && params.length() != 0) { | |
69 | + withParams = " with params " + params; | |
70 | + } | |
71 | + log.info("Execute on Device resource /{}/{}/{} {}", getModel().id, getId(), resourceId, withParams != null ? withParams : ""); | |
72 | + | |
73 | + switch (resourceId) { | |
74 | + case 4: | |
75 | + startUpdating(); | |
76 | + return ExecuteResponse.success(); | |
77 | + case 6: | |
78 | + return ExecuteResponse.success(); | |
79 | + default: | |
80 | + return super.execute(identity, resourceId, params); | |
81 | + } | |
82 | + } | |
83 | + | |
84 | + @Override | |
85 | + public WriteResponse write(ServerIdentity identity, boolean replace, int resourceId, LwM2mResource value) { | |
86 | + log.info("Write on Device resource /{}/{}/{}", getModel().id, getId(), resourceId); | |
87 | + | |
88 | + switch (resourceId) { | |
89 | + case 2: | |
90 | + startDownloading(); | |
91 | + return WriteResponse.success(); | |
92 | + case 3: | |
93 | + startDownloading(); | |
94 | + return WriteResponse.success(); | |
95 | + default: | |
96 | + return super.write(identity, replace, resourceId, value); | |
97 | + } | |
98 | + } | |
99 | + | |
100 | + private int getUpdateState() { | |
101 | + return state.get(); | |
102 | + } | |
103 | + | |
104 | + private int getUpdateResult() { | |
105 | + return updateResult.get(); | |
106 | + } | |
107 | + | |
108 | + private String getPkgName() { | |
109 | + return "software"; | |
110 | + } | |
111 | + | |
112 | + private String getPkgVersion() { | |
113 | + return "1.0.0"; | |
114 | + } | |
115 | + | |
116 | + @Override | |
117 | + public List<Integer> getAvailableResourceIds(ObjectModel model) { | |
118 | + return supportedResources; | |
119 | + } | |
120 | + | |
121 | + @Override | |
122 | + public void destroy() { | |
123 | + scheduler.shutdownNow(); | |
124 | + } | |
125 | + | |
126 | + private void startDownloading() { | |
127 | + scheduler.schedule(() -> { | |
128 | + try { | |
129 | + state.set(1); | |
130 | + updateResult.set(1); | |
131 | + fireResourcesChange(7, 9); | |
132 | + Thread.sleep(100); | |
133 | + state.set(2); | |
134 | + fireResourcesChange(7); | |
135 | + Thread.sleep(100); | |
136 | + state.set(3); | |
137 | + fireResourcesChange(7); | |
138 | + Thread.sleep(100); | |
139 | + updateResult.set(3); | |
140 | + fireResourcesChange(9); | |
141 | + } catch (Exception e) { | |
142 | + | |
143 | + } | |
144 | + }, 100, TimeUnit.MILLISECONDS); | |
145 | + } | |
146 | + | |
147 | + private void startUpdating() { | |
148 | + scheduler.schedule(() -> { | |
149 | + state.set(4); | |
150 | + updateResult.set(2); | |
151 | + fireResourcesChange(7, 9); | |
152 | + }, 100, TimeUnit.MILLISECONDS); | |
153 | + } | |
154 | + | |
155 | +} | ... | ... |
1 | 1 | transport.lwm2m.security.key_store=lwm2m/credentials/serverKeyStore.jks |
2 | 2 | transport.lwm2m.security.key_store_password=server |
3 | 3 | edges.enabled=true |
4 | -transport.lwm2m.bootstrap.security.alias=server | |
\ No newline at end of file | ||
4 | +transport.lwm2m.server.security.key_alias=server | |
5 | +transport.lwm2m.server.security.key_password=server | |
6 | +transport.lwm2m.bootstrap.security.key_alias=server | |
7 | +transport.lwm2m.bootstrap.security.key_password=server | |
\ No newline at end of file | ... | ... |
application/src/test/resources/lwm2m/5.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | + | |
3 | +<!-- | |
4 | +FILE INFORMATION | |
5 | + | |
6 | +OMA Permanent Document | |
7 | + File: OMA-SUP-XML_LWM2M_Firmware_Update-V1_0_3-20190617-A | |
8 | + Type: xml | |
9 | + Date: 2019-Jun-17 | |
10 | + | |
11 | +Public Reachable Information | |
12 | + Path: http://www.openmobilealliance.org/tech/profiles | |
13 | + Name: LWM2M_Firmware_Update-v1_0_3.xml | |
14 | + | |
15 | +NORMATIVE INFORMATION | |
16 | + | |
17 | + Information about this file can be found in the latest revision of | |
18 | + | |
19 | + OMA-TS-LightweightM2M-V1_0_2 | |
20 | + OMA-TS-LightweightM2M_Core-V1_1_1 | |
21 | + | |
22 | + This is available at http://www.openmobilealliance.org/ | |
23 | + | |
24 | + Send comments to https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues | |
25 | + | |
26 | +LEGAL DISCLAIMER | |
27 | + | |
28 | + Copyright 2019 Open Mobile Alliance. | |
29 | + | |
30 | + Redistribution and use in source and binary forms, with or without | |
31 | + modification, are permitted provided that the following conditions | |
32 | + are met: | |
33 | + | |
34 | + 1. Redistributions of source code must retain the above copyright | |
35 | + notice, this list of conditions and the following disclaimer. | |
36 | + 2. Redistributions in binary form must reproduce the above copyright | |
37 | + notice, this list of conditions and the following disclaimer in the | |
38 | + documentation and/or other materials provided with the distribution. | |
39 | + 3. Neither the name of the copyright holder nor the names of its | |
40 | + contributors may be used to endorse or promote products derived | |
41 | + from this software without specific prior written permission. | |
42 | + | |
43 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
44 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
45 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
46 | + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
47 | + COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
48 | + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
49 | + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
50 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
51 | + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
52 | + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
53 | + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
54 | + POSSIBILITY OF SUCH DAMAGE. | |
55 | + | |
56 | + The above license is used as a license under copyright only. Please | |
57 | + reference the OMA IPR Policy for patent licensing terms: | |
58 | + https://www.omaspecworks.org/about/intellectual-property-rights/ | |
59 | + | |
60 | +--> | |
61 | + | |
62 | +<LWM2M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.openmobilealliance.org/tech/profiles/LWM2M.xsd"> | |
63 | + <Object ObjectType="MODefinition"> | |
64 | + <Name>Firmware Update V_1_0</Name> | |
65 | + <Description1><![CDATA[This LwM2M Object enables management of firmware which is to be updated. This Object includes installing a firmware package, updating firmware, and performing actions after updating firmware. The firmware update MAY require to reboot the device; it will depend on a number of factors, such as the operating system architecture and the extent of the updated software. | |
66 | +The envisioned functionality is to allow a LwM2M Client to connect to any LwM2M Server to obtain a firmware image using the object and resource structure defined in this section experiencing communication security protection using TLS/DTLS. There are, however, other design decisions that need to be taken into account to allow a manufacturer of a device to securely install firmware on a device. Examples for such design decisions are how to manage the firmware update repository at the server side (which may include user interface considerations), the techniques to provide additional application layer security protection of the firmware image, how many versions of firmware images to store on the device, and how to execute the firmware update process considering the hardware specific details of a given IoT hardware product. These aspects are considered to be outside the scope of this version of the specification. | |
67 | +A LwM2M Server may also instruct a LwM2M Client to fetch a firmware image from a dedicated server (instead of pushing firmware images to the LwM2M Client). The Package URI resource is contained in the Firmware object and can be used for this purpose. | |
68 | +A LwM2M Client MUST support block-wise transfer [CoAP_Blockwise] if it implements the Firmware Update object. | |
69 | +A LwM2M Server MUST support block-wise transfer. Other protocols, such as HTTP/HTTPs, MAY also be used for downloading firmware updates (via the Package URI resource). For constrained devices it is, however, RECOMMENDED to use CoAP for firmware downloads to avoid the need for additional protocol implementations.]]></Description1> | |
70 | + <ObjectID>5</ObjectID> | |
71 | + <ObjectURN>urn:oma:lwm2m:oma:5</ObjectURN> | |
72 | + <LWM2MVersion>1.0</LWM2MVersion> | |
73 | + <ObjectVersion>1.0</ObjectVersion> | |
74 | + <MultipleInstances>Single</MultipleInstances> | |
75 | + <Mandatory>Optional</Mandatory> | |
76 | + <Resources> | |
77 | + <Item ID="0"> | |
78 | + <Name>Package</Name> | |
79 | + <Operations>W</Operations> | |
80 | + <MultipleInstances>Single</MultipleInstances> | |
81 | + <Mandatory>Mandatory</Mandatory> | |
82 | + <Type>Opaque</Type> | |
83 | + <RangeEnumeration></RangeEnumeration> | |
84 | + <Units></Units> | |
85 | + <Description><![CDATA[Firmware package]]></Description> | |
86 | + </Item> | |
87 | + <Item ID="1"> | |
88 | + <Name>Package URI</Name> | |
89 | + <Operations>RW</Operations> | |
90 | + <MultipleInstances>Single</MultipleInstances> | |
91 | + <Mandatory>Mandatory</Mandatory> | |
92 | + <Type>String</Type> | |
93 | + <RangeEnumeration>0..255</RangeEnumeration> | |
94 | + <Units></Units> | |
95 | + <Description><![CDATA[URI from where the device can download the firmware package by an alternative mechanism. As soon the device has received the Package URI it performs the download at the next practical opportunity. | |
96 | +The URI format is defined in RFC 3986. For example, coaps://example.org/firmware is a syntactically valid URI. The URI scheme determines the protocol to be used. For CoAP this endpoint MAY be a LwM2M Server but does not necessarily need to be. A CoAP server implementing block-wise transfer is sufficient as a server hosting a firmware repository and the expectation is that this server merely serves as a separate file server making firmware images available to LwM2M Clients.]]></Description> | |
97 | + </Item> | |
98 | + <Item ID="2"> | |
99 | + <Name>Update</Name> | |
100 | + <Operations>E</Operations> | |
101 | + <MultipleInstances>Single</MultipleInstances> | |
102 | + <Mandatory>Mandatory</Mandatory> | |
103 | + <Type></Type> | |
104 | + <RangeEnumeration></RangeEnumeration> | |
105 | + <Units></Units> | |
106 | + <Description><![CDATA[Updates firmware by using the firmware package stored in Package, or, by using the firmware downloaded from the Package URI. | |
107 | +This Resource is only executable when the value of the State Resource is Downloaded.]]></Description> | |
108 | + </Item> | |
109 | + <Item ID="3"> | |
110 | + <Name>State</Name> | |
111 | + <Operations>R</Operations> | |
112 | + <MultipleInstances>Single</MultipleInstances> | |
113 | + <Mandatory>Mandatory</Mandatory> | |
114 | + <Type>Integer</Type> | |
115 | + <RangeEnumeration>0..3</RangeEnumeration> | |
116 | + <Units></Units> | |
117 | + <Description><![CDATA[Indicates current state with respect to this firmware update. This value is set by the LwM2M Client. | |
118 | +0: Idle (before downloading or after successful updating) | |
119 | +1: Downloading (The data sequence is on the way) | |
120 | +2: Downloaded | |
121 | +3: Updating | |
122 | +If writing the firmware package to Package Resource has completed, or, if the device has downloaded the firmware package from the Package URI the state changes to Downloaded. | |
123 | +Writing an empty string to Package URI Resource or setting the Package Resource to NULL (‘\0’), resets the Firmware Update State Machine: the State Resource value is set to Idle and the Update Result Resource value is set to 0. | |
124 | +When in Downloaded state, and the executable Resource Update is triggered, the state changes to Updating. | |
125 | +If the Update Resource failed, the state returns at Downloaded. | |
126 | +If performing the Update Resource was successful, the state changes from Updating to Idle. | |
127 | +The firmware update state machine is illustrated in Figure 29 of the LwM2M version 1.0 specification (and also in Figure E.6.1-1 of this specification).]]></Description> | |
128 | + </Item> | |
129 | + <Item ID="5"> | |
130 | + <Name>Update Result</Name> | |
131 | + <Operations>R</Operations> | |
132 | + <MultipleInstances>Single</MultipleInstances> | |
133 | + <Mandatory>Mandatory</Mandatory> | |
134 | + <Type>Integer</Type> | |
135 | + <RangeEnumeration>0..9</RangeEnumeration> | |
136 | + <Units></Units> | |
137 | + <Description><![CDATA[Contains the result of downloading or updating the firmware | |
138 | +0: Initial value. Once the updating process is initiated (Download /Update), this Resource MUST be reset to Initial value. | |
139 | +1: Firmware updated successfully. | |
140 | +2: Not enough flash memory for the new firmware package. | |
141 | +3: Out of RAM during downloading process. | |
142 | +4: Connection lost during downloading process. | |
143 | +5: Integrity check failure for new downloaded package. | |
144 | +6: Unsupported package type. | |
145 | +7: Invalid URI. | |
146 | +8: Firmware update failed. | |
147 | +9: Unsupported protocol. A LwM2M client indicates the failure to retrieve the firmware image using the URI provided in the Package URI resource by writing the value 9 to the /5/0/5 (Update Result resource) when the URI contained a URI scheme unsupported by the client. Consequently, the LwM2M Client is unable to retrieve the firmware image using the URI provided by the LwM2M Server in the Package URI when it refers to an unsupported protocol.]]></Description> | |
148 | + </Item> | |
149 | + <Item ID="6"> | |
150 | + <Name>PkgName</Name> | |
151 | + <Operations>R</Operations> | |
152 | + <MultipleInstances>Single</MultipleInstances> | |
153 | + <Mandatory>Optional</Mandatory> | |
154 | + <Type>String</Type> | |
155 | + <RangeEnumeration>0..255</RangeEnumeration> | |
156 | + <Units></Units> | |
157 | + <Description><![CDATA[Name of the Firmware Package]]></Description> | |
158 | + </Item> | |
159 | + <Item ID="7"> | |
160 | + <Name>PkgVersion</Name> | |
161 | + <Operations>R</Operations> | |
162 | + <MultipleInstances>Single</MultipleInstances> | |
163 | + <Mandatory>Optional</Mandatory> | |
164 | + <Type>String</Type> | |
165 | + <RangeEnumeration>0..255</RangeEnumeration> | |
166 | + <Units></Units> | |
167 | + <Description><![CDATA[Version of the Firmware package]]></Description> | |
168 | + </Item> | |
169 | + <Item ID="8"> | |
170 | + <Name>Firmware Update Protocol Support</Name> | |
171 | + <Operations>R</Operations> | |
172 | + <MultipleInstances>Multiple</MultipleInstances> | |
173 | + <Mandatory>Optional</Mandatory> | |
174 | + <Type>Integer</Type> | |
175 | + <RangeEnumeration>0..5</RangeEnumeration> | |
176 | + <Units></Units> | |
177 | + <Description><![CDATA[This resource indicates what protocols the LwM2M Client implements to retrieve firmware images. The LwM2M server uses this information to decide what URI to include in the Package URI. A LwM2M Server MUST NOT include a URI in the Package URI object that uses a protocol that is unsupported by the LwM2M client. | |
178 | +For example, if a LwM2M client indicates that it supports CoAP and CoAPS then a LwM2M Server must not provide an HTTP URI in the Packet URI. | |
179 | +The following values are defined by this version of the specification: | |
180 | +0: CoAP (as defined in RFC 7252) with the additional support for block-wise transfer. CoAP is the default setting. | |
181 | +1: CoAPS (as defined in RFC 7252) with the additional support for block-wise transfer | |
182 | +2: HTTP 1.1 (as defined in RFC 7230) | |
183 | +3: HTTPS 1.1 (as defined in RFC 7230) | |
184 | +4: CoAP over TCP (as defined in RFC 8323) | |
185 | +5: CoAP over TLS (as defined in RFC 8323) | |
186 | +Additional values MAY be defined in the future. Any value not understood by the LwM2M Server MUST be ignored.]]></Description> | |
187 | + </Item> | |
188 | + <Item ID="9"> | |
189 | + <Name>Firmware Update Delivery Method</Name> | |
190 | + <Operations>R</Operations> | |
191 | + <MultipleInstances>Single</MultipleInstances> | |
192 | + <Mandatory>Mandatory</Mandatory> | |
193 | + <Type>Integer</Type> | |
194 | + <RangeEnumeration>0..2</RangeEnumeration> | |
195 | + <Units></Units> | |
196 | + <Description><![CDATA[The LwM2M Client uses this resource to indicate its support for transferring firmware images to the client either via the Package Resource (=push) or via the Package URI Resource (=pull) mechanism. | |
197 | +0: Pull only | |
198 | +1: Push only | |
199 | +2: Both. In this case the LwM2M Server MAY choose the preferred mechanism for conveying the firmware image to the LwM2M Client.]]></Description> | |
200 | + </Item> | |
201 | + </Resources> | |
202 | + <Description2><![CDATA[]]></Description2> | |
203 | + </Object> | |
204 | +</LWM2M> | ... | ... |
application/src/test/resources/lwm2m/9.xml
0 → 100644
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | + | |
3 | +<!-- | |
4 | +FILE INFORMATION | |
5 | + | |
6 | +OMA Permanent Document | |
7 | + File: OMA-SUP-XML_9-V1_0_2-20210119-A.xml | |
8 | + Path: http://www.openmobilealliance.org/release/LWM2M_SWMGMT | |
9 | + | |
10 | +OMNA LwM2M Registry | |
11 | + Path: https://github.com/OpenMobileAlliance/lwm2m-registry | |
12 | + Name: 9.xml | |
13 | + | |
14 | +NORMATIVE INFORMATION | |
15 | + | |
16 | + Information about this file can be found in the latest revision of | |
17 | + | |
18 | + OMA-TS-LightweightM2M-V1_0 | |
19 | + | |
20 | + This is available at http://www.openmobilealliance.org/release/LightweightM2M/ | |
21 | + | |
22 | + Send comments to https://github.com/OpenMobileAlliance/OMA_LwM2M_for_Developers/issues | |
23 | + | |
24 | +LEGAL DISCLAIMER | |
25 | + | |
26 | + Copyright 2021 Open Mobile Alliance. | |
27 | + | |
28 | + Redistribution and use in source and binary forms, with or without | |
29 | + modification, are permitted provided that the following conditions | |
30 | + are met: | |
31 | + | |
32 | + 1. Redistributions of source code must retain the above copyright | |
33 | + notice, this list of conditions and the following disclaimer. | |
34 | + 2. Redistributions in binary form must reproduce the above copyright | |
35 | + notice, this list of conditions and the following disclaimer in the | |
36 | + documentation and/or other materials provided with the distribution. | |
37 | + 3. Neither the name of the copyright holder nor the names of its | |
38 | + contributors may be used to endorse or promote products derived | |
39 | + from this software without specific prior written permission. | |
40 | + | |
41 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
42 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
43 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
44 | + FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
45 | + COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
46 | + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
47 | + BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
48 | + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
49 | + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
51 | + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
52 | + POSSIBILITY OF SUCH DAMAGE. | |
53 | + | |
54 | + The above license is used as a license under copyright only. Please | |
55 | + reference the OMA IPR Policy for patent licensing terms: | |
56 | + https://www.omaspecworks.org/about/intellectual-property-rights/ | |
57 | + | |
58 | +--> | |
59 | + | |
60 | +<LWM2M xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://openmobilealliance.org/tech/profiles/LWM2M.xsd"> | |
61 | + <Object ObjectType="MODefinition"> | |
62 | + <Name>LWM2M Software Management</Name> | |
63 | + <Description1><![CDATA[This LwM2M objects provides the resources needed to perform software management on the device. Each software component is managed via a dedicated Software Management Object instance.]]></Description1> | |
64 | + <ObjectID>9</ObjectID> | |
65 | + <ObjectURN>urn:oma:lwm2m:oma:9</ObjectURN> | |
66 | + <LWM2MVersion>1.0</LWM2MVersion> | |
67 | + <ObjectVersion>1.0</ObjectVersion> | |
68 | + <MultipleInstances>Multiple</MultipleInstances> | |
69 | + <Mandatory>Optional</Mandatory> | |
70 | + <Resources> | |
71 | + <Item ID="0"> | |
72 | + <Name>PkgName</Name> | |
73 | + <Operations>R</Operations> | |
74 | + <MultipleInstances>Single</MultipleInstances> | |
75 | + <Mandatory>Mandatory</Mandatory> | |
76 | + <Type>String</Type> | |
77 | + <RangeEnumeration>0..255 bytes</RangeEnumeration> | |
78 | + <Units></Units> | |
79 | + <Description><![CDATA[Name of the software package]]></Description> | |
80 | + </Item> | |
81 | + <Item ID="1"> | |
82 | + <Name>PkgVersion</Name> | |
83 | + <Operations>R</Operations> | |
84 | + <MultipleInstances>Single</MultipleInstances> | |
85 | + <Mandatory>Mandatory</Mandatory> | |
86 | + <Type>String</Type> | |
87 | + <RangeEnumeration>0..255 bytes</RangeEnumeration> | |
88 | + <Units></Units> | |
89 | + <Description><![CDATA[Version of the software package]]></Description> | |
90 | + </Item> | |
91 | + <Item ID="2"> | |
92 | + <Name>Package</Name> | |
93 | + <Operations>W</Operations> | |
94 | + <MultipleInstances>Single</MultipleInstances> | |
95 | + <Mandatory>Optional</Mandatory> | |
96 | + <Type>Opaque</Type> | |
97 | + <RangeEnumeration></RangeEnumeration> | |
98 | + <Units></Units> | |
99 | + <Description><![CDATA[Software package | |
100 | +The package can be in one single software component, or any delivery material used by the Device to determine the software component(s) and proceed to their installation. | |
101 | +Can be archive file, executable, manifest. This resource to be used when it is single block of delivery. | |
102 | +]]> </Description> | |
103 | + </Item> | |
104 | + <Item ID="3"> | |
105 | + <Name>Package URI</Name> | |
106 | + <Operations>W</Operations> | |
107 | + <MultipleInstances>Single</MultipleInstances> | |
108 | + <Mandatory>Optional</Mandatory> | |
109 | + <Type>String</Type> | |
110 | + <RangeEnumeration>0..255 bytes</RangeEnumeration> | |
111 | + <Units></Units> | |
112 | + <Description><![CDATA[URI from where the device can download the software package by an alternative mechanism. As soon the device has received the Package URI it performs the download at the next practical opportunity. | |
113 | +Can be direct link to a single software component or link to archive file, executable, or manifest, used by the Device to determine, then access to the software component(s). This resource to be used when it is single block of delivery. | |
114 | +]]> </Description> | |
115 | + </Item> | |
116 | + | |
117 | + <Item ID="4"> | |
118 | + <Name>Install</Name> | |
119 | + <Operations>E</Operations> | |
120 | + <MultipleInstances>Single</MultipleInstances> | |
121 | + <Mandatory>Mandatory</Mandatory> | |
122 | + <Type></Type> | |
123 | + <RangeEnumeration></RangeEnumeration> | |
124 | + <Units></Units> | |
125 | + <Description><![CDATA[Installs software from the package either stored in Package resource, or, downloaded from the Package URI. This Resource is only executable when the value of the State Resource is DELIVERED.]]></Description> | |
126 | + </Item> | |
127 | + <Item ID="5"> | |
128 | + <Name>Checkpoint</Name> | |
129 | + <Operations>R</Operations> | |
130 | + <MultipleInstances>Single</MultipleInstances> | |
131 | + <Mandatory>Optional</Mandatory> | |
132 | + <Type>Objlnk</Type> | |
133 | + <RangeEnumeration></RangeEnumeration> | |
134 | + <Units></Units> | |
135 | + <Description><![CDATA[Link to a "Checkpoint" object which allows to specify conditions/dependencies for a software update. E.g. power connected, sufficient memory, target system.]]></Description> | |
136 | + </Item> | |
137 | + <Item ID="6"> | |
138 | + <Name>Uninstall</Name> | |
139 | + <Operations>E</Operations> | |
140 | + <MultipleInstances>Single</MultipleInstances> | |
141 | + <Mandatory>Mandatory</Mandatory> | |
142 | + <Type></Type> | |
143 | + <RangeEnumeration></RangeEnumeration> | |
144 | + <Units></Units> | |
145 | + <Description><![CDATA[Uninstalls the software package. | |
146 | +This executable resource may have one argument. | |
147 | +If used with no argument or argument is 0, the Package is removed i from the Device. If the argument is 1 ("ForUpdate"), the Client MUST prepare itself for receiving a Package used to upgrade the Software already in place. Update State is set back to INITIAL state. | |
148 | +]]> </Description> | |
149 | + </Item> | |
150 | + <Item ID="7"> | |
151 | + <Name>Update State</Name> | |
152 | + <Operations>R</Operations> | |
153 | + <MultipleInstances>Single</MultipleInstances> | |
154 | + <Mandatory>Mandatory</Mandatory> | |
155 | + <Type>Integer</Type> | |
156 | + <RangeEnumeration>0..4</RangeEnumeration> | |
157 | + <Units></Units> | |
158 | + <Description><![CDATA[Indicates current state with respect to this software update. This value is set by the LwM2M Client. | |
159 | +0: INITIAL | |
160 | +Before downloading. | |
161 | +(see 5.1.2.1) | |
162 | +1: DOWNLOAD STARTED | |
163 | +The downloading process has started and is on-going. | |
164 | +(see 5.1.2.2) | |
165 | +2: DOWNLOADED | |
166 | +The package has been completely downloaded | |
167 | +(see 5.1.2.3) | |
168 | +3: DELIVERED | |
169 | +In that state, the package has been correctly downloaded and is ready to be installed. | |
170 | +(see 5.1.2.4) | |
171 | +If executing the Install Resource failed, the state remains at DELIVERED. | |
172 | +If executing the Install Resource was successful, the state changes from DELIVERED to INSTALLED. | |
173 | +After executing the UnInstall Resource, the state changes to INITIAL. | |
174 | +4: INSTALLED | |
175 | +In that state the software is correctly installed and can be activated or deactivated according to the Activation State Machine. | |
176 | +(see 5.1.2.5) | |
177 | +]]> </Description> | |
178 | + </Item> | |
179 | + <Item ID="8"> | |
180 | + <Name>Update Supported Objects</Name> | |
181 | + <Operations>RW</Operations> | |
182 | + <MultipleInstances>Single</MultipleInstances> | |
183 | + <Mandatory>Optional</Mandatory> | |
184 | + <Type>Boolean</Type> | |
185 | + <RangeEnumeration></RangeEnumeration> | |
186 | + <Units></Units> | |
187 | + <Description><![CDATA[If this value is true, the LwM2M Client MUST inform the registered LwM2M Servers of Objects and Object Instances parameter by sending an Update or Registration message after the software update operation at the next practical opportunity if supported Objects in the LwM2M Client have changed, in order for the LwM2M Servers to promptly manage newly installed Objects. | |
188 | +If false, Objects and Object Instances parameter MUST be reported at the next periodic Update message. | |
189 | +The default value is false. | |
190 | +]]> </Description> | |
191 | + </Item> | |
192 | + <Item ID="9"> | |
193 | + <Name>Update Result</Name> | |
194 | + <Operations>R</Operations> | |
195 | + <MultipleInstances>Single</MultipleInstances> | |
196 | + <Mandatory>Mandatory</Mandatory> | |
197 | + <Type>Integer</Type> | |
198 | + <RangeEnumeration>0..200</RangeEnumeration> | |
199 | + <Units></Units> | |
200 | + <Description><![CDATA[Contains the result of downloading or installing/uninstalling the software | |
201 | +0: Initial value. Prior to download any new package in the Device, Update Result MUST be reset to this initial value. One side effect of executing the Uninstall resource is to reset Update Result to this initial value "0". | |
202 | +1: Downloading. The package downloading process is on-going. | |
203 | +2: Software successfully installed. | |
204 | +3: Successfully Downloaded and package integrity verified | |
205 | +(( 4-49, for expansion, of other scenarios)) | |
206 | +50: Not enough storage for the new software package. | |
207 | +51: Out of memory during downloading process. | |
208 | +52: Connection lost during downloading process. | |
209 | +53: Package integrity check failure. | |
210 | +54: Unsupported package type. | |
211 | +56: Invalid URI | |
212 | +57: Device defined update error | |
213 | +58: Software installation failure | |
214 | +59: Uninstallation Failure during forUpdate(arg=0) | |
215 | +60-200 : (for expansion, selection to be in blocks depending on new introduction of features) | |
216 | +This Resource MAY be reported by sending Observe operation. | |
217 | +]]> </Description> | |
218 | + </Item> | |
219 | + <Item ID="10"> | |
220 | + <Name>Activate</Name> | |
221 | + <Operations>E</Operations> | |
222 | + <MultipleInstances>Single</MultipleInstances> | |
223 | + <Mandatory>Mandatory</Mandatory> | |
224 | + <Type></Type> | |
225 | + <RangeEnumeration></RangeEnumeration> | |
226 | + <Units></Units> | |
227 | + <Description><![CDATA[This action activates the software previously successfully installed (the Package Installation State Machine is currently in the INSTALLED state)]]></Description> | |
228 | + </Item> | |
229 | + <Item ID="11"> | |
230 | + <Name>Deactivate</Name> | |
231 | + <Operations>E</Operations> | |
232 | + <MultipleInstances>Single</MultipleInstances> | |
233 | + <Mandatory>Mandatory</Mandatory> | |
234 | + <Type></Type> | |
235 | + <RangeEnumeration></RangeEnumeration> | |
236 | + <Units></Units> | |
237 | + <Description><![CDATA[This action deactivates softwareif the Package Installation State Machine is currently in the INSTALLED state.]]></Description> | |
238 | + </Item> | |
239 | + <Item ID="12"> | |
240 | + <Name>Activation State</Name> | |
241 | + <Operations>R</Operations> | |
242 | + <MultipleInstances>Single</MultipleInstances> | |
243 | + <Mandatory>Mandatory</Mandatory> | |
244 | + <Type>Boolean</Type> | |
245 | + <RangeEnumeration></RangeEnumeration> | |
246 | + <Units></Units> | |
247 | + <Description><![CDATA[Indicates the current activation state of this software: | |
248 | +0: DISABLED | |
249 | +Activation State is DISABLED if the Software Activation State Machine is in the INACTIVE state or not alive. | |
250 | +1: ENABLED | |
251 | +Activation State is ENABLED only if the Software Activation State Machine is in the ACTIVE state | |
252 | +]]> </Description> | |
253 | + </Item> | |
254 | + <Item ID="13"> | |
255 | + <Name>Package Settings</Name> | |
256 | + <Operations>RW</Operations> | |
257 | + <MultipleInstances>Single</MultipleInstances> | |
258 | + <Mandatory>Optional</Mandatory> | |
259 | + <Type>Objlnk</Type> | |
260 | + <RangeEnumeration></RangeEnumeration> | |
261 | + <Units></Units> | |
262 | + <Description><![CDATA[Link to "Package Settings" object which allows to modify at any time software configuration settings. This is an application specific object. | |
263 | +Note: OMA might provide a template for a Package Settings object in a future release of this specification. | |
264 | +]]> </Description> | |
265 | + </Item> | |
266 | + <Item ID="14"> | |
267 | + <Name>User Name</Name> | |
268 | + <Operations>W</Operations> | |
269 | + <MultipleInstances>Single</MultipleInstances> | |
270 | + <Mandatory>Optional</Mandatory> | |
271 | + <Type>String</Type> | |
272 | + <RangeEnumeration>0..255 bytes</RangeEnumeration> | |
273 | + <Units></Units> | |
274 | + <Description><![CDATA[User Name for access to SW Update Package in pull mode. | |
275 | +Key based mechanism can alternatively use for talking to the component server instead of user name and password combination. | |
276 | +]]> </Description> | |
277 | + </Item> | |
278 | + <Item ID="15"> | |
279 | + <Name>Password</Name> | |
280 | + <Operations>W</Operations> | |
281 | + <MultipleInstances>Single</MultipleInstances> | |
282 | + <Mandatory>Optional</Mandatory> | |
283 | + <Type>String</Type> | |
284 | + <RangeEnumeration>0..255 bytes</RangeEnumeration> | |
285 | + <Units></Units> | |
286 | + <Description><![CDATA[Password for access to SW Update Package in pull mode.]]></Description> | |
287 | + </Item> | |
288 | + <Item ID="16"> | |
289 | + <Name>Status Reason</Name> | |
290 | + <Operations>R</Operations> | |
291 | + <MultipleInstances>Single</MultipleInstances> | |
292 | + <Mandatory>Optional</Mandatory> | |
293 | + <Type>String</Type> | |
294 | + <RangeEnumeration></RangeEnumeration> | |
295 | + <Units></Units> | |
296 | + <Description><![CDATA[Contains the status of the actions done by the client on the SW Component(s) referred by the present SW Update Package. The status is defined in Appendix B.]]></Description> | |
297 | + </Item> | |
298 | + <Item ID="17"> | |
299 | + <Name>Software Component Link</Name> | |
300 | + <Operations>R</Operations> | |
301 | + <MultipleInstances>Multiple</MultipleInstances> | |
302 | + <Mandatory>Optional</Mandatory> | |
303 | + <Type>Objlnk</Type> | |
304 | + <RangeEnumeration></RangeEnumeration> | |
305 | + <Units></Units> | |
306 | + <Description><![CDATA[Reference to SW Components downloaded and installed in scope of the present SW Update Package Note: When resource 17 objlink exist, resources 2, 3 and 12 in this table are ignored.]]></Description> | |
307 | + </Item> | |
308 | + <Item ID="18"> | |
309 | + <Name>Software Component tree length</Name> | |
310 | + <Operations>R</Operations> | |
311 | + <MultipleInstances>Single</MultipleInstances> | |
312 | + <Mandatory>Optional</Mandatory> | |
313 | + <Type>Integer</Type> | |
314 | + <RangeEnumeration>0..255</RangeEnumeration> | |
315 | + <Units></Units> | |
316 | + <Description><![CDATA[Software Component tree length indicates the number of instances existing for this software package in the Software Component Object.]]></Description> | |
317 | + </Item> | |
318 | + </Resources> | |
319 | + <Description2><![CDATA[]]></Description2> | |
320 | + </Object> | |
321 | +</LWM2M> | ... | ... |
... | ... | @@ -117,11 +117,9 @@ public class DefaultCoapServerService implements CoapServerService { |
117 | 117 | dtlsCoapEndpointBuilder.setConnector(connector); |
118 | 118 | CoapEndpoint dtlsCoapEndpoint = dtlsCoapEndpointBuilder.build(); |
119 | 119 | server.addEndpoint(dtlsCoapEndpoint); |
120 | - if (dtlsConnectorConfig.isClientAuthenticationRequired()) { | |
121 | - tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier(); | |
122 | - dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor(); | |
123 | - dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS); | |
124 | - } | |
120 | + tbDtlsCertificateVerifier = (TbCoapDtlsCertificateVerifier) dtlsConnectorConfig.getAdvancedCertificateVerifier(); | |
121 | + dtlsSessionsExecutor = Executors.newSingleThreadScheduledExecutor(); | |
122 | + dtlsSessionsExecutor.scheduleAtFixedRate(this::evictTimeoutSessions, new Random().nextInt((int) getDtlsSessionReportTimeout()), getDtlsSessionReportTimeout(), TimeUnit.MILLISECONDS); | |
125 | 123 | } |
126 | 124 | Resource root = server.getRoot(); |
127 | 125 | TbCoapServerMessageDeliverer messageDeliverer = new TbCoapServerMessageDeliverer(root); | ... | ... |
... | ... | @@ -78,7 +78,6 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri |
78 | 78 | @Override |
79 | 79 | public CertificateVerificationResult verifyCertificate(ConnectionId cid, ServerNames serverName, Boolean clientUsage, boolean truncateCertificatePath, CertificateMessage message, DTLSSession session) { |
80 | 80 | try { |
81 | - String credentialsBody = null; | |
82 | 81 | CertPath certpath = message.getCertificateChain(); |
83 | 82 | X509Certificate[] chain = certpath.getCertificates().toArray(new X509Certificate[0]); |
84 | 83 | for (X509Certificate cert : chain) { |
... | ... | @@ -110,7 +109,6 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri |
110 | 109 | latch.await(10, TimeUnit.SECONDS); |
111 | 110 | ValidateDeviceCredentialsResponse msg = deviceCredentialsResponse[0]; |
112 | 111 | if (msg != null && strCert.equals(msg.getCredentials())) { |
113 | - credentialsBody = msg.getCredentials(); | |
114 | 112 | DeviceProfile deviceProfile = msg.getDeviceProfile(); |
115 | 113 | if (msg.hasDeviceInfo() && deviceProfile != null) { |
116 | 114 | TransportProtos.SessionInfoProto sessionInfoProto = SessionInfoCreator.create(msg, serviceInfoProvider.getServiceId(), UUID.randomUUID()); |
... | ... | @@ -123,15 +121,12 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri |
123 | 121 | CertificateExpiredException | |
124 | 122 | CertificateNotYetValidException e) { |
125 | 123 | log.error(e.getMessage(), e); |
124 | + AlertMessage alert = new AlertMessage(AlertMessage.AlertLevel.FATAL, AlertMessage.AlertDescription.BAD_CERTIFICATE, | |
125 | + session.getPeer()); | |
126 | + throw new HandshakeException("Certificate chain could not be validated", alert); | |
126 | 127 | } |
127 | 128 | } |
128 | - if (credentialsBody == null) { | |
129 | - AlertMessage alert = new AlertMessage(AlertMessage.AlertLevel.FATAL, AlertMessage.AlertDescription.BAD_CERTIFICATE, | |
130 | - session.getPeer()); | |
131 | - throw new HandshakeException("Certificate chain could not be validated", alert); | |
132 | - } else { | |
133 | - return new CertificateVerificationResult(cid, certpath, null); | |
134 | - } | |
129 | + return new CertificateVerificationResult(cid, certpath, null); | |
135 | 130 | } catch (HandshakeException e) { |
136 | 131 | log.trace("Certificate validation failed!", e); |
137 | 132 | return new CertificateVerificationResult(cid, e, null); |
... | ... | @@ -158,4 +153,4 @@ public class TbCoapDtlsCertificateVerifier implements NewAdvancedCertificateVeri |
158 | 153 | public long getDtlsSessionReportTimeout() { |
159 | 154 | return tbCoapDtlsSessionInMemoryStorage.getDtlsSessionReportTimeout(); |
160 | 155 | } |
161 | -} | |
\ No newline at end of file | ||
156 | +} | ... | ... |
... | ... | @@ -15,15 +15,12 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.coapserver; |
17 | 17 | |
18 | -import com.google.common.io.Resources; | |
19 | 18 | import lombok.extern.slf4j.Slf4j; |
20 | 19 | import org.eclipse.californium.elements.util.SslContextUtil; |
21 | 20 | import org.eclipse.californium.scandium.config.DtlsConnectorConfig; |
22 | 21 | import org.eclipse.californium.scandium.dtls.CertificateType; |
23 | -import org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier; | |
24 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 23 | import org.springframework.beans.factory.annotation.Value; |
26 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | |
27 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
28 | 25 | import org.springframework.stereotype.Component; |
29 | 26 | import org.thingsboard.server.common.data.ResourceUtils; |
... | ... | @@ -35,9 +32,7 @@ import java.net.InetAddress; |
35 | 32 | import java.net.InetSocketAddress; |
36 | 33 | import java.net.UnknownHostException; |
37 | 34 | import java.security.GeneralSecurityException; |
38 | -import java.security.cert.Certificate; | |
39 | 35 | import java.util.Collections; |
40 | -import java.util.Optional; | |
41 | 36 | |
42 | 37 | @Slf4j |
43 | 38 | @ConditionalOnProperty(prefix = "transport.coap.dtls", value = "enabled", havingValue = "true", matchIfMissing = false) |
... | ... | @@ -50,9 +45,6 @@ public class TbCoapDtlsSettings { |
50 | 45 | @Value("${transport.coap.dtls.bind_port}") |
51 | 46 | private Integer port; |
52 | 47 | |
53 | - @Value("${transport.coap.dtls.mode:NO_AUTH}") | |
54 | - private String mode; | |
55 | - | |
56 | 48 | @Value("${transport.coap.dtls.key_store}") |
57 | 49 | private String keyStoreFile; |
58 | 50 | |
... | ... | @@ -81,34 +73,25 @@ public class TbCoapDtlsSettings { |
81 | 73 | private TbServiceInfoProvider serviceInfoProvider; |
82 | 74 | |
83 | 75 | public DtlsConnectorConfig dtlsConnectorConfig() throws UnknownHostException { |
84 | - Optional<SecurityMode> securityModeOpt = SecurityMode.parse(mode); | |
85 | - if (securityModeOpt.isEmpty()) { | |
86 | - log.warn("Incorrect configuration of securityMode {}", mode); | |
87 | - throw new RuntimeException("Failed to parse mode property: " + mode + "!"); | |
88 | - } else { | |
89 | - DtlsConnectorConfig.Builder configBuilder = new DtlsConnectorConfig.Builder(); | |
90 | - configBuilder.setAddress(getInetSocketAddress()); | |
91 | - String keyStoreFilePath = ResourceUtils.getUri(this, keyStoreFile); | |
92 | - SslContextUtil.Credentials serverCredentials = loadServerCredentials(keyStoreFilePath); | |
93 | - SecurityMode securityMode = securityModeOpt.get(); | |
94 | - if (securityMode.equals(SecurityMode.NO_AUTH)) { | |
95 | - configBuilder.setClientAuthenticationRequired(false); | |
96 | - configBuilder.setServerOnly(true); | |
97 | - } else { | |
98 | - configBuilder.setAdvancedCertificateVerifier( | |
99 | - new TbCoapDtlsCertificateVerifier( | |
100 | - transportService, | |
101 | - serviceInfoProvider, | |
102 | - dtlsSessionInactivityTimeout, | |
103 | - dtlsSessionReportTimeout, | |
104 | - skipValidityCheckForClientCert | |
105 | - ) | |
106 | - ); | |
107 | - } | |
108 | - configBuilder.setIdentity(serverCredentials.getPrivateKey(), serverCredentials.getCertificateChain(), | |
109 | - Collections.singletonList(CertificateType.X_509)); | |
110 | - return configBuilder.build(); | |
111 | - } | |
76 | + DtlsConnectorConfig.Builder configBuilder = new DtlsConnectorConfig.Builder(); | |
77 | + configBuilder.setAddress(getInetSocketAddress()); | |
78 | + String keyStoreFilePath = ResourceUtils.getUri(this, keyStoreFile); | |
79 | + SslContextUtil.Credentials serverCredentials = loadServerCredentials(keyStoreFilePath); | |
80 | + configBuilder.setServerOnly(true); | |
81 | + configBuilder.setClientAuthenticationRequired(false); | |
82 | + configBuilder.setClientAuthenticationWanted(true); | |
83 | + configBuilder.setAdvancedCertificateVerifier( | |
84 | + new TbCoapDtlsCertificateVerifier( | |
85 | + transportService, | |
86 | + serviceInfoProvider, | |
87 | + dtlsSessionInactivityTimeout, | |
88 | + dtlsSessionReportTimeout, | |
89 | + skipValidityCheckForClientCert | |
90 | + ) | |
91 | + ); | |
92 | + configBuilder.setIdentity(serverCredentials.getPrivateKey(), serverCredentials.getCertificateChain(), | |
93 | + Collections.singletonList(CertificateType.X_509)); | |
94 | + return configBuilder.build(); | |
112 | 95 | } |
113 | 96 | |
114 | 97 | private SslContextUtil.Credentials loadServerCredentials(String keyStoreFilePath) { |
... | ... | @@ -120,43 +103,9 @@ public class TbCoapDtlsSettings { |
120 | 103 | } |
121 | 104 | } |
122 | 105 | |
123 | - private void loadTrustedCertificates(DtlsConnectorConfig.Builder config, String keyStoreFilePath) { | |
124 | - StaticNewAdvancedCertificateVerifier.Builder trustBuilder = StaticNewAdvancedCertificateVerifier.builder(); | |
125 | - try { | |
126 | - Certificate[] trustedCertificates = SslContextUtil.loadTrustedCertificates( | |
127 | - keyStoreFilePath, keyAlias, | |
128 | - keyStorePassword.toCharArray()); | |
129 | - trustBuilder.setTrustedCertificates(trustedCertificates); | |
130 | - if (trustBuilder.hasTrusts()) { | |
131 | - config.setAdvancedCertificateVerifier(trustBuilder.build()); | |
132 | - } | |
133 | - } catch (GeneralSecurityException | IOException e) { | |
134 | - throw new RuntimeException("Failed to load trusted certificates due to: ", e); | |
135 | - } | |
136 | - } | |
137 | - | |
138 | 106 | private InetSocketAddress getInetSocketAddress() throws UnknownHostException { |
139 | 107 | InetAddress addr = InetAddress.getByName(host); |
140 | 108 | return new InetSocketAddress(addr, port); |
141 | 109 | } |
142 | 110 | |
143 | - private enum SecurityMode { | |
144 | - X509, | |
145 | - NO_AUTH; | |
146 | - | |
147 | - static Optional<SecurityMode> parse(String name) { | |
148 | - SecurityMode mode = null; | |
149 | - if (name != null) { | |
150 | - for (SecurityMode securityMode : SecurityMode.values()) { | |
151 | - if (securityMode.name().equalsIgnoreCase(name)) { | |
152 | - mode = securityMode; | |
153 | - break; | |
154 | - } | |
155 | - } | |
156 | - } | |
157 | - return Optional.ofNullable(mode); | |
158 | - } | |
159 | - | |
160 | - } | |
161 | - | |
162 | -} | |
\ No newline at end of file | ||
111 | +} | ... | ... |
... | ... | @@ -58,4 +58,6 @@ public interface DashboardService { |
58 | 58 | Dashboard unassignDashboardFromEdge(TenantId tenantId, DashboardId dashboardId, EdgeId edgeId); |
59 | 59 | |
60 | 60 | PageData<DashboardInfo> findDashboardsByTenantIdAndEdgeId(TenantId tenantId, EdgeId edgeId, PageLink pageLink); |
61 | + | |
62 | + DashboardInfo findFirstDashboardInfoByTenantIdAndName(TenantId tenantId, String name); | |
61 | 63 | } | ... | ... |
... | ... | @@ -15,10 +15,12 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.common.data.device.data.lwm2m; |
17 | 17 | |
18 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | |
18 | 19 | import lombok.Data; |
19 | 20 | import org.thingsboard.server.common.data.device.data.PowerMode; |
20 | 21 | |
21 | 22 | @Data |
23 | +@JsonIgnoreProperties(ignoreUnknown = true) | |
22 | 24 | public class OtherConfiguration { |
23 | 25 | |
24 | 26 | private Integer fwUpdateStrategy; | ... | ... |
... | ... | @@ -17,6 +17,7 @@ package org.thingsboard.server.common.data.device.profile; |
17 | 17 | |
18 | 18 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
19 | 19 | import lombok.Data; |
20 | +import org.thingsboard.server.common.data.query.FilterPredicateValue; | |
20 | 21 | |
21 | 22 | import java.util.concurrent.TimeUnit; |
22 | 23 | |
... | ... | @@ -25,7 +26,7 @@ import java.util.concurrent.TimeUnit; |
25 | 26 | public class DurationAlarmConditionSpec implements AlarmConditionSpec { |
26 | 27 | |
27 | 28 | private TimeUnit unit; |
28 | - private long value; | |
29 | + private FilterPredicateValue<Long> predicate; | |
29 | 30 | |
30 | 31 | @Override |
31 | 32 | public AlarmConditionSpecType getType() { | ... | ... |
... | ... | @@ -17,14 +17,13 @@ package org.thingsboard.server.common.data.device.profile; |
17 | 17 | |
18 | 18 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
19 | 19 | import lombok.Data; |
20 | - | |
21 | -import java.util.concurrent.TimeUnit; | |
20 | +import org.thingsboard.server.common.data.query.FilterPredicateValue; | |
22 | 21 | |
23 | 22 | @Data |
24 | 23 | @JsonIgnoreProperties(ignoreUnknown = true) |
25 | 24 | public class RepeatingAlarmConditionSpec implements AlarmConditionSpec { |
26 | 25 | |
27 | - private int count; | |
26 | + private FilterPredicateValue<Integer> predicate; | |
28 | 27 | |
29 | 28 | @Override |
30 | 29 | public AlarmConditionSpecType getType() { | ... | ... |
... | ... | @@ -172,6 +172,11 @@ public abstract class AbstractTbQueueConsumerTemplate<R, T extends TbQueueMsg> i |
172 | 172 | } |
173 | 173 | } |
174 | 174 | |
175 | + @Override | |
176 | + public boolean isStopped() { | |
177 | + return stopped; | |
178 | + } | |
179 | + | |
175 | 180 | abstract protected List<R> doPoll(long durationInMillis); |
176 | 181 | |
177 | 182 | abstract protected T decode(R record) throws IOException; | ... | ... |
... | ... | @@ -54,6 +54,7 @@ import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
54 | 54 | |
55 | 55 | import javax.annotation.PreDestroy; |
56 | 56 | import java.nio.charset.StandardCharsets; |
57 | +import java.util.concurrent.atomic.AtomicLong; | |
57 | 58 | |
58 | 59 | @Component |
59 | 60 | @ConditionalOnExpression("'${queue.type:null}'=='kafka' && '${service.type:null}'=='monolith'") |
... | ... | @@ -75,6 +76,7 @@ public class KafkaMonolithQueueFactory implements TbCoreQueueFactory, TbRuleEngi |
75 | 76 | private final TbQueueAdmin transportApiAdmin; |
76 | 77 | private final TbQueueAdmin notificationAdmin; |
77 | 78 | private final TbQueueAdmin fwUpdatesAdmin; |
79 | + private final AtomicLong consumerCount = new AtomicLong(); | |
78 | 80 | |
79 | 81 | public KafkaMonolithQueueFactory(PartitionService partitionService, TbKafkaSettings kafkaSettings, |
80 | 82 | TbServiceInfoProvider serviceInfoProvider, |
... | ... | @@ -159,7 +161,7 @@ public class KafkaMonolithQueueFactory implements TbCoreQueueFactory, TbRuleEngi |
159 | 161 | TbKafkaConsumerTemplate.TbKafkaConsumerTemplateBuilder<TbProtoQueueMsg<ToRuleEngineMsg>> consumerBuilder = TbKafkaConsumerTemplate.builder(); |
160 | 162 | consumerBuilder.settings(kafkaSettings); |
161 | 163 | consumerBuilder.topic(ruleEngineSettings.getTopic()); |
162 | - consumerBuilder.clientId("re-" + queueName + "-consumer-" + serviceInfoProvider.getServiceId()); | |
164 | + consumerBuilder.clientId("re-" + queueName + "-consumer-" + serviceInfoProvider.getServiceId() + "-" + consumerCount.incrementAndGet()); | |
163 | 165 | consumerBuilder.groupId("re-" + queueName + "-consumer"); |
164 | 166 | consumerBuilder.decoder(msg -> new TbProtoQueueMsg<>(msg.getKey(), ToRuleEngineMsg.parseFrom(msg.getData()), msg.getHeaders())); |
165 | 167 | consumerBuilder.admin(ruleEngineAdmin); | ... | ... |
... | ... | @@ -49,6 +49,7 @@ import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration; |
49 | 49 | |
50 | 50 | import javax.annotation.PreDestroy; |
51 | 51 | import java.nio.charset.StandardCharsets; |
52 | +import java.util.concurrent.atomic.AtomicLong; | |
52 | 53 | |
53 | 54 | @Component |
54 | 55 | @ConditionalOnExpression("'${queue.type:null}'=='kafka' && '${service.type:null}'=='tb-rule-engine'") |
... | ... | @@ -66,6 +67,7 @@ public class KafkaTbRuleEngineQueueFactory implements TbRuleEngineQueueFactory { |
66 | 67 | private final TbQueueAdmin ruleEngineAdmin; |
67 | 68 | private final TbQueueAdmin jsExecutorAdmin; |
68 | 69 | private final TbQueueAdmin notificationAdmin; |
70 | + private final AtomicLong consumerCount = new AtomicLong(); | |
69 | 71 | |
70 | 72 | public KafkaTbRuleEngineQueueFactory(PartitionService partitionService, TbKafkaSettings kafkaSettings, |
71 | 73 | TbServiceInfoProvider serviceInfoProvider, |
... | ... | @@ -145,7 +147,7 @@ public class KafkaTbRuleEngineQueueFactory implements TbRuleEngineQueueFactory { |
145 | 147 | TbKafkaConsumerTemplate.TbKafkaConsumerTemplateBuilder<TbProtoQueueMsg<ToRuleEngineMsg>> consumerBuilder = TbKafkaConsumerTemplate.builder(); |
146 | 148 | consumerBuilder.settings(kafkaSettings); |
147 | 149 | consumerBuilder.topic(ruleEngineSettings.getTopic()); |
148 | - consumerBuilder.clientId("re-" + queueName + "-consumer-" + serviceInfoProvider.getServiceId()); | |
150 | + consumerBuilder.clientId("re-" + queueName + "-consumer-" + serviceInfoProvider.getServiceId() + "-" + consumerCount.incrementAndGet()); | |
149 | 151 | consumerBuilder.groupId("re-" + queueName + "-consumer"); |
150 | 152 | consumerBuilder.decoder(msg -> new TbProtoQueueMsg<>(msg.getKey(), ToRuleEngineMsg.parseFrom(msg.getData()), msg.getHeaders())); |
151 | 153 | consumerBuilder.admin(ruleEngineAdmin); | ... | ... |
common/queue/src/main/java/org/thingsboard/server/queue/settings/TbRuleEngineQueueConfiguration.java
... | ... | @@ -24,6 +24,7 @@ public class TbRuleEngineQueueConfiguration { |
24 | 24 | private String topic; |
25 | 25 | private int pollInterval; |
26 | 26 | private int partitions; |
27 | + private boolean consumerPerPartition; | |
27 | 28 | private long packProcessingTimeout; |
28 | 29 | private TbRuleEngineQueueSubmitStrategyConfiguration submitStrategy; |
29 | 30 | private TbRuleEngineQueueAckStrategyConfiguration processingStrategy; | ... | ... |
common/transport/coap/src/main/java/org/thingsboard/server/transport/coap/CoapTransportResource.java
... | ... | @@ -223,18 +223,14 @@ public class CoapTransportResource extends AbstractCoapTransportResource { |
223 | 223 | Request request = advanced.getRequest(); |
224 | 224 | |
225 | 225 | String dtlsSessionIdStr = request.getSourceContext().get(DTLS_SESSION_ID_KEY); |
226 | - if (StringUtils.isNotEmpty(dtlsSessionIdStr)) { | |
227 | - if (dtlsSessionIdMap != null) { | |
228 | - TbCoapDtlsSessionInfo tbCoapDtlsSessionInfo = dtlsSessionIdMap | |
229 | - .computeIfPresent(dtlsSessionIdStr, (dtlsSessionId, dtlsSessionInfo) -> { | |
230 | - dtlsSessionInfo.setLastActivityTime(System.currentTimeMillis()); | |
231 | - return dtlsSessionInfo; | |
232 | - }); | |
233 | - if (tbCoapDtlsSessionInfo != null) { | |
234 | - processRequest(exchange, type, request, tbCoapDtlsSessionInfo.getSessionInfoProto(), tbCoapDtlsSessionInfo.getDeviceProfile()); | |
235 | - } else { | |
236 | - exchange.respond(CoAP.ResponseCode.UNAUTHORIZED); | |
237 | - } | |
226 | + if (dtlsSessionIdMap != null && StringUtils.isNotEmpty(dtlsSessionIdStr)) { | |
227 | + TbCoapDtlsSessionInfo tbCoapDtlsSessionInfo = dtlsSessionIdMap | |
228 | + .computeIfPresent(dtlsSessionIdStr, (dtlsSessionId, dtlsSessionInfo) -> { | |
229 | + dtlsSessionInfo.setLastActivityTime(System.currentTimeMillis()); | |
230 | + return dtlsSessionInfo; | |
231 | + }); | |
232 | + if (tbCoapDtlsSessionInfo != null) { | |
233 | + processRequest(exchange, type, request, tbCoapDtlsSessionInfo.getSessionInfoProto(), tbCoapDtlsSessionInfo.getDeviceProfile()); | |
238 | 234 | } else { |
239 | 235 | processAccessTokenRequest(exchange, type, request); |
240 | 236 | } | ... | ... |
... | ... | @@ -17,58 +17,35 @@ package org.thingsboard.server.transport.lwm2m.bootstrap; |
17 | 17 | |
18 | 18 | import lombok.RequiredArgsConstructor; |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | +import org.eclipse.californium.elements.util.SslContextUtil; | |
20 | 21 | import org.eclipse.californium.scandium.config.DtlsConnectorConfig; |
21 | -import org.eclipse.leshan.core.util.Hex; | |
22 | 22 | import org.eclipse.leshan.server.bootstrap.BootstrapSessionManager; |
23 | 23 | import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServer; |
24 | 24 | import org.eclipse.leshan.server.californium.bootstrap.LeshanBootstrapServerBuilder; |
25 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
26 | 26 | import org.springframework.stereotype.Component; |
27 | -import org.thingsboard.server.common.data.StringUtils; | |
28 | 27 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MBootstrapSecurityStore; |
29 | 28 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2MInMemoryBootstrapConfigStore; |
30 | 29 | import org.thingsboard.server.transport.lwm2m.bootstrap.secure.LwM2mDefaultBootstrapSessionManager; |
31 | 30 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportBootstrapConfig; |
32 | 31 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; |
33 | -import org.thingsboard.server.transport.lwm2m.secure.LWM2MGenerationPSkRPkECC; | |
32 | +import org.thingsboard.server.transport.lwm2m.server.DefaultLwM2mTransportService; | |
34 | 33 | |
35 | 34 | import javax.annotation.PostConstruct; |
36 | 35 | import javax.annotation.PreDestroy; |
37 | -import java.math.BigInteger; | |
38 | -import java.security.AlgorithmParameters; | |
39 | -import java.security.KeyFactory; | |
40 | 36 | import java.security.KeyStore; |
41 | 37 | import java.security.KeyStoreException; |
42 | -import java.security.NoSuchAlgorithmException; | |
43 | 38 | import java.security.PrivateKey; |
44 | 39 | import java.security.PublicKey; |
45 | -import java.security.cert.CertificateEncodingException; | |
46 | 40 | import java.security.cert.X509Certificate; |
47 | -import java.security.interfaces.ECPublicKey; | |
48 | -import java.security.spec.ECGenParameterSpec; | |
49 | -import java.security.spec.ECParameterSpec; | |
50 | -import java.security.spec.ECPoint; | |
51 | -import java.security.spec.ECPublicKeySpec; | |
52 | -import java.security.spec.InvalidKeySpecException; | |
53 | -import java.security.spec.InvalidParameterSpecException; | |
54 | -import java.security.spec.KeySpec; | |
55 | -import java.security.spec.PKCS8EncodedKeySpec; | |
56 | -import java.util.Arrays; | |
57 | 41 | |
58 | -import static org.eclipse.californium.scandium.dtls.cipher.CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256; | |
59 | -import static org.eclipse.californium.scandium.dtls.cipher.CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8; | |
60 | -import static org.eclipse.californium.scandium.dtls.cipher.CipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256; | |
61 | -import static org.eclipse.californium.scandium.dtls.cipher.CipherSuite.TLS_PSK_WITH_AES_128_CCM_8; | |
62 | 42 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mNetworkConfig.getCoapConfig; |
63 | 43 | |
64 | 44 | @Slf4j |
65 | 45 | @Component |
66 | 46 | @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 | 47 | @RequiredArgsConstructor |
68 | -//TODO: @ybondarenko please refactor this to be similar to DefaultLwM2mTransportService | |
69 | 48 | public class LwM2MTransportBootstrapService { |
70 | - private PublicKey publicKey; | |
71 | - private PrivateKey privateKey; | |
72 | 49 | private boolean pskMode = false; |
73 | 50 | |
74 | 51 | private final LwM2MTransportServerConfig serverConfig; |
... | ... | @@ -80,9 +57,6 @@ public class LwM2MTransportBootstrapService { |
80 | 57 | |
81 | 58 | @PostConstruct |
82 | 59 | public void init() { |
83 | - if (serverConfig.getEnableGenNewKeyPskRpk()) { | |
84 | - new LWM2MGenerationPSkRPkECC(); | |
85 | - } | |
86 | 60 | log.info("Starting LwM2M transport bootstrap server..."); |
87 | 61 | this.server = getLhBootstrapServer(); |
88 | 62 | this.server.start(); |
... | ... | @@ -101,44 +75,34 @@ public class LwM2MTransportBootstrapService { |
101 | 75 | builder.setLocalAddress(bootstrapConfig.getHost(), bootstrapConfig.getPort()); |
102 | 76 | builder.setLocalSecureAddress(bootstrapConfig.getSecureHost(), bootstrapConfig.getSecurePort()); |
103 | 77 | |
104 | - /** Create CoAP Config */ | |
78 | + /* Create CoAP Config */ | |
105 | 79 | builder.setCoapConfig(getCoapConfig(bootstrapConfig.getPort(), bootstrapConfig.getSecurePort(), serverConfig)); |
106 | 80 | |
107 | - /** Define model provider (Create Models )*/ | |
81 | + /* Define model provider (Create Models )*/ | |
108 | 82 | |
109 | - /** Create credentials */ | |
83 | + /* Create credentials */ | |
110 | 84 | this.setServerWithCredentials(builder); |
111 | 85 | |
112 | 86 | // /** Set securityStore with new ConfigStore */ |
113 | 87 | // builder.setConfigStore(lwM2MInMemoryBootstrapConfigStore); |
114 | 88 | |
115 | - /** SecurityStore */ | |
89 | + /* SecurityStore */ | |
116 | 90 | builder.setSecurityStore(lwM2MBootstrapSecurityStore); |
117 | 91 | |
118 | 92 | |
119 | - /** Create and Set DTLS Config */ | |
93 | + /* Create and Set DTLS Config */ | |
120 | 94 | DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder(); |
121 | 95 | dtlsConfig.setRecommendedSupportedGroupsOnly(serverConfig.isRecommendedSupportedGroups()); |
122 | 96 | dtlsConfig.setRecommendedCipherSuitesOnly(serverConfig.isRecommendedCiphers()); |
123 | - if (this.pskMode) { | |
124 | - dtlsConfig.setSupportedCipherSuites( | |
125 | - TLS_PSK_WITH_AES_128_CCM_8, | |
126 | - TLS_PSK_WITH_AES_128_CBC_SHA256); | |
127 | - } else { | |
128 | - dtlsConfig.setSupportedCipherSuites( | |
129 | - TLS_PSK_WITH_AES_128_CCM_8, | |
130 | - TLS_PSK_WITH_AES_128_CBC_SHA256, | |
131 | - TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, | |
132 | - TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256); | |
133 | - } | |
97 | + dtlsConfig.setSupportedCipherSuites(this.pskMode ? DefaultLwM2mTransportService.PSK_CIPHER_SUITES : DefaultLwM2mTransportService.RPK_OR_X509_CIPHER_SUITES); | |
134 | 98 | |
135 | - /** Set DTLS Config */ | |
99 | + /* Set DTLS Config */ | |
136 | 100 | builder.setDtlsConfig(dtlsConfig); |
137 | 101 | |
138 | 102 | BootstrapSessionManager sessionManager = new LwM2mDefaultBootstrapSessionManager(lwM2MBootstrapSecurityStore); |
139 | 103 | builder.setSessionManager(sessionManager); |
140 | 104 | |
141 | - /** Create BootstrapServer */ | |
105 | + /* Create BootstrapServer */ | |
142 | 106 | return builder.build(); |
143 | 107 | } |
144 | 108 | |
... | ... | @@ -153,19 +117,15 @@ public class LwM2MTransportBootstrapService { |
153 | 117 | trustedCertificates[0] = rootCAX509Cert; |
154 | 118 | builder.setTrustedCertificates(trustedCertificates); |
155 | 119 | } else { |
156 | - /** by default trust all */ | |
120 | + /* by default trust all */ | |
157 | 121 | builder.setTrustedCertificates(new X509Certificate[0]); |
158 | 122 | } |
159 | 123 | } |
160 | - } else if (this.setServerRPK(builder)) { | |
161 | - this.infoPramsUri("RPK"); | |
162 | - this.infoParamsBootstrapServerKey(this.publicKey, this.privateKey); | |
163 | 124 | } else { |
164 | - /** by default trust all */ | |
125 | + /* by default trust all */ | |
165 | 126 | builder.setTrustedCertificates(new X509Certificate[0]); |
166 | 127 | log.info("Unable to load X509 files for BootStrapServer"); |
167 | 128 | this.pskMode = true; |
168 | - this.infoPramsUri("PSK"); | |
169 | 129 | } |
170 | 130 | } catch (KeyStoreException ex) { |
171 | 131 | log.error("[{}] Unable to load X509 files server", ex.getMessage()); |
... | ... | @@ -173,19 +133,15 @@ public class LwM2MTransportBootstrapService { |
173 | 133 | } |
174 | 134 | |
175 | 135 | private boolean setBuilderX509(LeshanBootstrapServerBuilder builder) { |
176 | - /** | |
177 | - * For deb => KeyStorePathFile == yml or commandline: KEY_STORE_PATH_FILE | |
178 | - * For idea => KeyStorePathResource == common/transport/lwm2m/src/main/resources/credentials: in LwM2MTransportContextServer: credentials/serverKeyStore.jks | |
179 | - */ | |
180 | 136 | try { |
181 | - X509Certificate serverCertificate = (X509Certificate) serverConfig.getKeyStoreValue().getCertificate(this.bootstrapConfig.getCertificateAlias()); | |
182 | - PrivateKey privateKey = (PrivateKey) serverConfig.getKeyStoreValue().getKey(this.bootstrapConfig.getCertificateAlias(), serverConfig.getKeyStorePassword() == null ? null : serverConfig.getKeyStorePassword().toCharArray()); | |
137 | + X509Certificate[] certificateChain = SslContextUtil.asX509Certificates(serverConfig.getKeyStoreValue().getCertificateChain(this.bootstrapConfig.getCertificateAlias())); | |
138 | + X509Certificate serverCertificate = certificateChain[0]; | |
139 | + PrivateKey privateKey = (PrivateKey) serverConfig.getKeyStoreValue().getKey(this.bootstrapConfig.getCertificateAlias(), serverConfig.getCertificatePassword() == null ? null : serverConfig.getCertificatePassword().toCharArray()); | |
183 | 140 | PublicKey publicKey = serverCertificate.getPublicKey(); |
184 | 141 | if (privateKey != null && privateKey.getEncoded().length > 0 && publicKey != null && publicKey.getEncoded().length > 0) { |
185 | 142 | builder.setPublicKey(serverCertificate.getPublicKey()); |
186 | 143 | builder.setPrivateKey(privateKey); |
187 | - builder.setCertificateChain(new X509Certificate[]{serverCertificate}); | |
188 | - this.infoParamsServerX509(serverCertificate, publicKey, privateKey); | |
144 | + builder.setCertificateChain(certificateChain); | |
189 | 145 | return true; |
190 | 146 | } else { |
191 | 147 | return false; |
... | ... | @@ -196,105 +152,4 @@ public class LwM2MTransportBootstrapService { |
196 | 152 | } |
197 | 153 | } |
198 | 154 | |
199 | - private void infoParamsServerX509(X509Certificate certificate, PublicKey publicKey, PrivateKey privateKey) { | |
200 | - try { | |
201 | - this.infoPramsUri("X509"); | |
202 | - log.info("\n- X509 Certificate (Hex): [{}]", | |
203 | - Hex.encodeHexString(certificate.getEncoded())); | |
204 | - this.infoParamsBootstrapServerKey(publicKey, privateKey); | |
205 | - } catch (CertificateEncodingException e) { | |
206 | - log.error("", e); | |
207 | - } | |
208 | - } | |
209 | - | |
210 | - private void infoPramsUri(String mode) { | |
211 | - log.info("Bootstrap Server uses [{}]: serverNoSecureURI : [{}:{}], serverSecureURI : [{}:{}]", | |
212 | - mode, | |
213 | - this.bootstrapConfig.getHost(), | |
214 | - this.bootstrapConfig.getPort(), | |
215 | - this.bootstrapConfig.getSecureHost(), | |
216 | - this.bootstrapConfig.getSecurePort()); | |
217 | - } | |
218 | - | |
219 | - | |
220 | - private boolean setServerRPK(LeshanBootstrapServerBuilder builder) { | |
221 | - try { | |
222 | - this.generateKeyForBootstrapRPK(); | |
223 | - if (this.publicKey != null && this.publicKey.getEncoded().length > 0 && | |
224 | - this.privateKey != null && this.privateKey.getEncoded().length > 0) { | |
225 | - builder.setPublicKey(this.publicKey); | |
226 | -// builder.setCertificateChain(new X509Certificate[] { serverCertificate }); | |
227 | - /// Trust all certificates. | |
228 | - builder.setTrustedCertificates(new X509Certificate[0]); | |
229 | - builder.setPrivateKey(this.privateKey); | |
230 | - return true; | |
231 | - } | |
232 | - } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) { | |
233 | - log.error("Fail create Bootstrap Server with RPK", e); | |
234 | - } | |
235 | - return false; | |
236 | - } | |
237 | - | |
238 | - | |
239 | - /** | |
240 | - * From yml: bootstrap | |
241 | - * public_x: "${LWM2M_SERVER_PUBLIC_X_BS:993ef2b698c6a9c0c1d8be78b13a9383c0854c7c7c7a504d289b403794648183}" | |
242 | - * public_y: "${LWM2M_SERVER_PUBLIC_Y_BS:267412d5fc4e5ceb2257cb7fd7f76ebdac2fa9aa100afb162e990074cc0bfaa2}" | |
243 | - * private_encoded: "${LWM2M_SERVER_PRIVATE_ENCODED_BS:9dbdbb073fc63570693a9aaf1013414e261c571f27e27fc6a8c1c2ad9347875a}" | |
244 | - */ | |
245 | - private void generateKeyForBootstrapRPK() throws NoSuchAlgorithmException, InvalidParameterSpecException, InvalidKeySpecException { | |
246 | - /** Get Elliptic Curve Parameter spec for secp256r1 */ | |
247 | - AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); | |
248 | - algoParameters.init(new ECGenParameterSpec("secp256r1")); | |
249 | - ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); | |
250 | - LwM2MTransportBootstrapConfig serverConfig = this.bootstrapConfig; | |
251 | - if (StringUtils.isNotEmpty(serverConfig.getPublicX()) && StringUtils.isNotEmpty(serverConfig.getPublicY())) { | |
252 | - /** Get point values */ | |
253 | - byte[] publicX = Hex.decodeHex(serverConfig.getPublicX().toCharArray()); | |
254 | - byte[] publicY = Hex.decodeHex(serverConfig.getPublicY().toCharArray()); | |
255 | - /** Create key specs */ | |
256 | - KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), | |
257 | - parameterSpec); | |
258 | - /** Get public key */ | |
259 | - this.publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec); | |
260 | - } | |
261 | - String privateEncodedKey = serverConfig.getPrivateEncoded(); | |
262 | - if (StringUtils.isNotEmpty(privateEncodedKey)) { | |
263 | - /** Get private key */ | |
264 | - byte[] privateS = Hex.decodeHex(privateEncodedKey.toCharArray()); | |
265 | - try { | |
266 | - this.privateKey = KeyFactory.getInstance("EC").generatePrivate(new PKCS8EncodedKeySpec(privateS)); | |
267 | - } catch (InvalidKeySpecException ignore2) { | |
268 | - log.error("Invalid Bootstrap Server rpk.PrivateKey.getEncoded () [{}}]. PrivateKey has no EC algorithm", privateEncodedKey); | |
269 | - } | |
270 | - } | |
271 | - } | |
272 | - | |
273 | - private void infoParamsBootstrapServerKey(PublicKey publicKey, PrivateKey privateKey) { | |
274 | - /** Get x coordinate */ | |
275 | - byte[] x = ((ECPublicKey) publicKey).getW().getAffineX().toByteArray(); | |
276 | - if (x[0] == 0) | |
277 | - x = Arrays.copyOfRange(x, 1, x.length); | |
278 | - | |
279 | - /** Get Y coordinate */ | |
280 | - byte[] y = ((ECPublicKey) publicKey).getW().getAffineY().toByteArray(); | |
281 | - if (y[0] == 0) | |
282 | - y = Arrays.copyOfRange(y, 1, y.length); | |
283 | - | |
284 | - /** Get Curves params */ | |
285 | - String params = ((ECPublicKey) publicKey).getParams().toString(); | |
286 | - String privHex = Hex.encodeHexString(privateKey.getEncoded()); | |
287 | - log.info("\n- Public Key (Hex): [{}] \n" + | |
288 | - "- Private Key (Hex): [{}], \n" + | |
289 | - "public_x: \"${LWM2M_SERVER_PUBLIC_X_BS:{}}\" \n" + | |
290 | - "public_y: \"${LWM2M_SERVER_PUBLIC_Y_BS:{}}\" \n" + | |
291 | - "private_encoded: \"${LWM2M_SERVER_PRIVATE_ENCODED_BS:{}}\" \n" + | |
292 | - "- Elliptic Curve parameters : [{}]", | |
293 | - Hex.encodeHexString(publicKey.getEncoded()), | |
294 | - privHex, | |
295 | - Hex.encodeHexString(x), | |
296 | - Hex.encodeHexString(y), | |
297 | - privHex, | |
298 | - params); | |
299 | - } | |
300 | 155 | } | ... | ... |
... | ... | @@ -27,12 +27,8 @@ public interface LwM2MSecureServerConfig { |
27 | 27 | |
28 | 28 | Integer getSecurePort(); |
29 | 29 | |
30 | - String getPublicX(); | |
31 | - | |
32 | - String getPublicY(); | |
33 | - | |
34 | - String getPrivateEncoded(); | |
35 | - | |
36 | 30 | String getCertificateAlias(); |
37 | 31 | |
32 | + String getCertificatePassword(); | |
33 | + | |
38 | 34 | } | ... | ... |
... | ... | @@ -47,19 +47,11 @@ public class LwM2MTransportBootstrapConfig implements LwM2MSecureServerConfig { |
47 | 47 | private Integer securePort; |
48 | 48 | |
49 | 49 | @Getter |
50 | - @Value("${transport.lwm2m.bootstrap.security.public_x:}") | |
51 | - private String publicX; | |
52 | - | |
53 | - @Getter | |
54 | - @Value("${transport.lwm2m.bootstrap.security.public_y:}") | |
55 | - private String publicY; | |
56 | - | |
57 | - @Getter | |
58 | - @Value("${transport.lwm2m.bootstrap.security.private_encoded:}") | |
59 | - private String privateEncoded; | |
50 | + @Value("${transport.lwm2m.bootstrap.security.key_alias:}") | |
51 | + private String certificateAlias; | |
60 | 52 | |
61 | 53 | @Getter |
62 | - @Value("${transport.lwm2m.bootstrap.security.alias:}") | |
63 | - private String certificateAlias; | |
54 | + @Value("${transport.lwm2m.bootstrap.security.key_password:}") | |
55 | + private String certificatePassword; | |
64 | 56 | |
65 | 57 | } | ... | ... |
... | ... | @@ -53,11 +53,11 @@ public class LwM2MTransportServerConfig implements LwM2MSecureServerConfig { |
53 | 53 | private long sessionReportTimeout; |
54 | 54 | |
55 | 55 | @Getter |
56 | - @Value("${transport.lwm2m.recommended_ciphers:}") | |
56 | + @Value("${transport.lwm2m.security.recommended_ciphers:}") | |
57 | 57 | private boolean recommendedCiphers; |
58 | 58 | |
59 | 59 | @Getter |
60 | - @Value("${transport.lwm2m.recommended_supported_groups:}") | |
60 | + @Value("${transport.lwm2m.security.recommended_supported_groups:}") | |
61 | 61 | private boolean recommendedSupportedGroups; |
62 | 62 | |
63 | 63 | @Getter |
... | ... | @@ -97,10 +97,6 @@ public class LwM2MTransportServerConfig implements LwM2MSecureServerConfig { |
97 | 97 | private String rootCertificateAlias; |
98 | 98 | |
99 | 99 | @Getter |
100 | - @Value("${transport.lwm2m.security.enable_gen_new_key_psk_rpk:}") | |
101 | - private Boolean enableGenNewKeyPskRpk; | |
102 | - | |
103 | - @Getter | |
104 | 100 | @Value("${transport.lwm2m.server.id:}") |
105 | 101 | private Integer id; |
106 | 102 | |
... | ... | @@ -121,20 +117,12 @@ public class LwM2MTransportServerConfig implements LwM2MSecureServerConfig { |
121 | 117 | private Integer securePort; |
122 | 118 | |
123 | 119 | @Getter |
124 | - @Value("${transport.lwm2m.server.security.public_x:}") | |
125 | - private String publicX; | |
126 | - | |
127 | - @Getter | |
128 | - @Value("${transport.lwm2m.server.security.public_y:}") | |
129 | - private String publicY; | |
130 | - | |
131 | - @Getter | |
132 | - @Value("${transport.lwm2m.server.security.private_encoded:}") | |
133 | - private String privateEncoded; | |
120 | + @Value("${transport.lwm2m.server.security.key_alias:}") | |
121 | + private String certificateAlias; | |
134 | 122 | |
135 | 123 | @Getter |
136 | - @Value("${transport.lwm2m.server.security.alias:}") | |
137 | - private String certificateAlias; | |
124 | + @Value("${transport.lwm2m.server.security.key_password:}") | |
125 | + private String certificatePassword; | |
138 | 126 | |
139 | 127 | @Getter |
140 | 128 | @Value("${transport.lwm2m.log_max_length:}") | ... | ... |
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/config/LwM2mVersion.java
... | ... | @@ -20,8 +20,8 @@ import org.eclipse.leshan.core.LwM2m.Version; |
20 | 20 | import org.eclipse.leshan.core.request.ContentFormat; |
21 | 21 | |
22 | 22 | public enum LwM2mVersion { |
23 | - VERSION_1_0(0, Version.V1_0, ContentFormat.TLV), | |
24 | - VERSION_1_1(1, Version.V1_1, ContentFormat.TEXT); | |
23 | + VERSION_1_0(0, Version.V1_0, ContentFormat.TLV, false), | |
24 | + VERSION_1_1(1, Version.V1_1, ContentFormat.TEXT, true); | |
25 | 25 | |
26 | 26 | @Getter |
27 | 27 | private final int code; |
... | ... | @@ -29,11 +29,14 @@ public enum LwM2mVersion { |
29 | 29 | private final Version version; |
30 | 30 | @Getter |
31 | 31 | private final ContentFormat contentFormat; |
32 | + @Getter | |
33 | + private final boolean composite; | |
32 | 34 | |
33 | - LwM2mVersion(int code, Version version, ContentFormat contentFormat) { | |
35 | + LwM2mVersion(int code, Version version, ContentFormat contentFormat, boolean composite) { | |
34 | 36 | this.code = code; |
35 | 37 | this.version = version; |
36 | 38 | this.contentFormat = contentFormat; |
39 | + this.composite = composite; | |
37 | 40 | } |
38 | 41 | |
39 | 42 | public static LwM2mVersion fromVersion(Version version) { | ... | ... |
common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/secure/LWM2MGenerationPSkRPkECC.java
deleted
100644 → 0
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.secure; | |
17 | - | |
18 | -import lombok.extern.slf4j.Slf4j; | |
19 | -import org.eclipse.leshan.core.util.Hex; | |
20 | - | |
21 | -import java.security.InvalidAlgorithmParameterException; | |
22 | -import java.security.KeyPair; | |
23 | -import java.security.KeyPairGenerator; | |
24 | -import java.security.NoSuchAlgorithmException; | |
25 | -import java.security.NoSuchProviderException; | |
26 | -import java.security.PrivateKey; | |
27 | -import java.security.PublicKey; | |
28 | -import java.security.SecureRandom; | |
29 | -import java.security.interfaces.ECPublicKey; | |
30 | -import java.security.spec.ECGenParameterSpec; | |
31 | -import java.util.Arrays; | |
32 | - | |
33 | -@Slf4j | |
34 | -public class LWM2MGenerationPSkRPkECC { | |
35 | - | |
36 | - public LWM2MGenerationPSkRPkECC() { | |
37 | - generationPSkKey(); | |
38 | - generationRPKECCKey(); | |
39 | - } | |
40 | - | |
41 | - private void generationPSkKey() { | |
42 | - /* PSK */ | |
43 | - int lenPSkKey = 32; | |
44 | - /* Start PSK | |
45 | - Clients and Servers MUST support PSK keys of up to 64 bytes in length, as required by [RFC7925] | |
46 | - SecureRandom object must be unpredictable, and all SecureRandom output sequences must be cryptographically strong, as described in [RFC4086] | |
47 | - */ | |
48 | - SecureRandom randomPSK = new SecureRandom(); | |
49 | - byte[] bytesPSK = new byte[lenPSkKey]; | |
50 | - randomPSK.nextBytes(bytesPSK); | |
51 | - log.info("\nCreating new PSK: \n for the next start PSK -> security key: [{}]", Hex.encodeHexString(bytesPSK)); | |
52 | - } | |
53 | - | |
54 | - private void generationRPKECCKey() { | |
55 | - /* RPK */ | |
56 | - String algorithm = "EC"; | |
57 | - String provider = "SunEC"; | |
58 | - String nameParameterSpec = "secp256r1"; | |
59 | - | |
60 | - /* Start RPK | |
61 | - Elliptic Curve parameters : [secp256r1 [NIST P-256, X9.62 prime256v1] (1.2.840.10045.3.1.7)] | |
62 | - */ | |
63 | - KeyPairGenerator kpg = null; | |
64 | - try { | |
65 | - kpg = KeyPairGenerator.getInstance(algorithm, provider); | |
66 | - } catch (NoSuchAlgorithmException | NoSuchProviderException e) { | |
67 | - log.error("", e); | |
68 | - } | |
69 | - ECGenParameterSpec ecsp = new ECGenParameterSpec(nameParameterSpec); | |
70 | - try { | |
71 | - kpg.initialize(ecsp); | |
72 | - } catch (InvalidAlgorithmParameterException e) { | |
73 | - log.error("", e); | |
74 | - } | |
75 | - | |
76 | - KeyPair kp = kpg.genKeyPair(); | |
77 | - PrivateKey privKey = kp.getPrivate(); | |
78 | - PublicKey pubKey = kp.getPublic(); | |
79 | - | |
80 | - if (pubKey instanceof ECPublicKey) { | |
81 | - ECPublicKey ecPublicKey = (ECPublicKey) pubKey; | |
82 | - /* Get x coordinate */ | |
83 | - byte[] x = ecPublicKey.getW().getAffineX().toByteArray(); | |
84 | - if (x[0] == 0) | |
85 | - x = Arrays.copyOfRange(x, 1, x.length); | |
86 | - | |
87 | - /* Get Y coordinate */ | |
88 | - byte[] y = ecPublicKey.getW().getAffineY().toByteArray(); | |
89 | - if (y[0] == 0) | |
90 | - y = Arrays.copyOfRange(y, 1, y.length); | |
91 | - | |
92 | - /* Get Curves params */ | |
93 | - String privHex = Hex.encodeHexString(privKey.getEncoded()); | |
94 | - log.info("\nCreating new RPK for the next start... \n" + | |
95 | - " Public Key (Hex): [{}]\n" + | |
96 | - " Private Key (Hex): [{}]" + | |
97 | - " public_x : [{}] \n" + | |
98 | - " public_y : [{}] \n" + | |
99 | - " private_encode : [{}] \n" + | |
100 | - " Elliptic Curve parameters : [{}] \n", | |
101 | - Hex.encodeHexString(pubKey.getEncoded()), | |
102 | - privHex, | |
103 | - Hex.encodeHexString(x), | |
104 | - Hex.encodeHexString(y), | |
105 | - privHex, | |
106 | - ecPublicKey.getParams().toString()); | |
107 | - } | |
108 | - } | |
109 | -} | |
110 | - |
... | ... | @@ -17,21 +17,19 @@ 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.elements.util.SslContextUtil; | |
20 | 21 | import org.eclipse.californium.scandium.config.DtlsConnectorConfig; |
21 | 22 | import org.eclipse.californium.scandium.dtls.cipher.CipherSuite; |
22 | 23 | import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeDecoder; |
23 | 24 | import org.eclipse.leshan.core.node.codec.DefaultLwM2mNodeEncoder; |
24 | -import org.eclipse.leshan.core.util.Hex; | |
25 | 25 | import org.eclipse.leshan.server.californium.LeshanServer; |
26 | 26 | import org.eclipse.leshan.server.californium.LeshanServerBuilder; |
27 | 27 | import org.eclipse.leshan.server.californium.registration.CaliforniumRegistrationStore; |
28 | 28 | import org.eclipse.leshan.server.model.LwM2mModelProvider; |
29 | 29 | import org.springframework.stereotype.Component; |
30 | 30 | import org.thingsboard.server.cache.ota.OtaPackageDataCache; |
31 | -import org.thingsboard.server.common.data.StringUtils; | |
32 | 31 | import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
33 | 32 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; |
34 | -import org.thingsboard.server.transport.lwm2m.secure.LWM2MGenerationPSkRPkECC; | |
35 | 33 | import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MAuthorizer; |
36 | 34 | import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MDtlsCertificateVerifier; |
37 | 35 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClientContext; |
... | ... | @@ -41,24 +39,9 @@ import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; |
41 | 39 | |
42 | 40 | import javax.annotation.PostConstruct; |
43 | 41 | import javax.annotation.PreDestroy; |
44 | -import java.math.BigInteger; | |
45 | -import java.security.AlgorithmParameters; | |
46 | -import java.security.KeyFactory; | |
47 | -import java.security.NoSuchAlgorithmException; | |
48 | 42 | import java.security.PrivateKey; |
49 | 43 | import java.security.PublicKey; |
50 | -import java.security.cert.CertificateEncodingException; | |
51 | 44 | import java.security.cert.X509Certificate; |
52 | -import java.security.interfaces.ECPublicKey; | |
53 | -import java.security.spec.ECGenParameterSpec; | |
54 | -import java.security.spec.ECParameterSpec; | |
55 | -import java.security.spec.ECPoint; | |
56 | -import java.security.spec.ECPublicKeySpec; | |
57 | -import java.security.spec.InvalidKeySpecException; | |
58 | -import java.security.spec.InvalidParameterSpecException; | |
59 | -import java.security.spec.KeySpec; | |
60 | -import java.security.spec.PKCS8EncodedKeySpec; | |
61 | -import java.util.Arrays; | |
62 | 45 | |
63 | 46 | import static org.eclipse.californium.scandium.dtls.cipher.CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256; |
64 | 47 | import static org.eclipse.californium.scandium.dtls.cipher.CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8; |
... | ... | @@ -75,8 +58,6 @@ public class DefaultLwM2mTransportService implements LwM2MTransportService { |
75 | 58 | |
76 | 59 | public static final CipherSuite[] RPK_OR_X509_CIPHER_SUITES = {TLS_PSK_WITH_AES_128_CCM_8, TLS_PSK_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256}; |
77 | 60 | public static final CipherSuite[] PSK_CIPHER_SUITES = {TLS_PSK_WITH_AES_128_CCM_8, TLS_PSK_WITH_AES_128_CBC_SHA256}; |
78 | - private PublicKey publicKey; | |
79 | - private PrivateKey privateKey; | |
80 | 61 | |
81 | 62 | private final LwM2mTransportContext context; |
82 | 63 | private final LwM2MTransportServerConfig config; |
... | ... | @@ -93,18 +74,14 @@ public class DefaultLwM2mTransportService implements LwM2MTransportService { |
93 | 74 | |
94 | 75 | @PostConstruct |
95 | 76 | public void init() { |
96 | - if (config.getEnableGenNewKeyPskRpk()) { | |
97 | - new LWM2MGenerationPSkRPkECC(); | |
98 | - } | |
99 | 77 | this.server = getLhServer(); |
100 | - /** | |
78 | + /* | |
101 | 79 | * Add a resource to the server. |
102 | 80 | * CoapResource -> |
103 | 81 | * path = FW_PACKAGE or SW_PACKAGE |
104 | 82 | * nameFile = "BC68JAR01A09_TO_BC68JAR01A10.bin" |
105 | 83 | * "coap://host:port/{path}/{token}/{nameFile}" |
106 | 84 | */ |
107 | - | |
108 | 85 | LwM2mTransportCoapResource otaCoapResource = new LwM2mTransportCoapResource(otaPackageDataCache, FIRMWARE_UPDATE_COAP_RESOURCE); |
109 | 86 | this.server.coap().getServer().add(otaCoapResource); |
110 | 87 | this.startLhServer(); |
... | ... | @@ -170,29 +147,24 @@ public class DefaultLwM2mTransportService implements LwM2MTransportService { |
170 | 147 | dtlsConfig.setAdvancedCertificateVerifier(certificateVerifier); |
171 | 148 | builder.setAuthorizer(authorizer); |
172 | 149 | dtlsConfig.setSupportedCipherSuites(RPK_OR_X509_CIPHER_SUITES); |
173 | - } else if (this.setServerRPK(builder)) { | |
174 | - this.infoPramsUri("RPK"); | |
175 | - this.infoParamsServerKey(this.publicKey, this.privateKey); | |
176 | - dtlsConfig.setSupportedCipherSuites(RPK_OR_X509_CIPHER_SUITES); | |
177 | 150 | } else { |
178 | 151 | /* by default trust all */ |
179 | 152 | builder.setTrustedCertificates(new X509Certificate[0]); |
180 | 153 | log.info("Unable to load X509 files for LWM2MServer"); |
181 | 154 | dtlsConfig.setSupportedCipherSuites(PSK_CIPHER_SUITES); |
182 | - this.infoPramsUri("PSK"); | |
183 | 155 | } |
184 | 156 | } |
185 | 157 | |
186 | 158 | private boolean setBuilderX509(LeshanServerBuilder builder) { |
187 | 159 | try { |
188 | - X509Certificate serverCertificate = (X509Certificate) config.getKeyStoreValue().getCertificate(config.getCertificateAlias()); | |
189 | - PrivateKey privateKey = (PrivateKey) config.getKeyStoreValue().getKey(config.getCertificateAlias(), config.getKeyStorePassword() == null ? null : config.getKeyStorePassword().toCharArray()); | |
160 | + X509Certificate[] certificateChain = SslContextUtil.asX509Certificates(config.getKeyStoreValue().getCertificateChain(config.getCertificateAlias())); | |
161 | + X509Certificate serverCertificate = certificateChain[0]; | |
162 | + PrivateKey privateKey = (PrivateKey) config.getKeyStoreValue().getKey(config.getCertificateAlias(), config.getCertificatePassword() == null ? null : config.getCertificatePassword().toCharArray()); | |
190 | 163 | PublicKey publicKey = serverCertificate.getPublicKey(); |
191 | 164 | if (privateKey != null && privateKey.getEncoded().length > 0 && publicKey != null && publicKey.getEncoded().length > 0) { |
192 | 165 | builder.setPublicKey(serverCertificate.getPublicKey()); |
193 | 166 | builder.setPrivateKey(privateKey); |
194 | - builder.setCertificateChain(new X509Certificate[]{serverCertificate}); | |
195 | - this.infoParamsServerX509(serverCertificate, publicKey, privateKey); | |
167 | + builder.setCertificateChain(certificateChain); | |
196 | 168 | return true; |
197 | 169 | } else { |
198 | 170 | return false; |
... | ... | @@ -203,93 +175,6 @@ public class DefaultLwM2mTransportService implements LwM2MTransportService { |
203 | 175 | } |
204 | 176 | } |
205 | 177 | |
206 | - private void infoParamsServerX509(X509Certificate certificate, PublicKey publicKey, PrivateKey privateKey) { | |
207 | - try { | |
208 | - infoPramsUri("X509"); | |
209 | - log.info("\n- X509 Certificate (Hex): [{}]", | |
210 | - Hex.encodeHexString(certificate.getEncoded())); | |
211 | - this.infoParamsServerKey(publicKey, privateKey); | |
212 | - } catch (CertificateEncodingException e) { | |
213 | - log.error("", e); | |
214 | - } | |
215 | - } | |
216 | - | |
217 | - private void infoPramsUri(String mode) { | |
218 | - LwM2MTransportServerConfig lwM2MTransportServerConfig = config; | |
219 | - log.info("Server uses [{}]: serverNoSecureURI : [{}:{}], serverSecureURI : [{}:{}]", mode, | |
220 | - lwM2MTransportServerConfig.getHost(), | |
221 | - lwM2MTransportServerConfig.getPort(), | |
222 | - lwM2MTransportServerConfig.getSecureHost(), | |
223 | - lwM2MTransportServerConfig.getSecurePort()); | |
224 | - } | |
225 | - | |
226 | - private boolean setServerRPK(LeshanServerBuilder builder) { | |
227 | - try { | |
228 | - this.loadOrGenerateRPKKeys(); | |
229 | - if (this.publicKey != null && this.publicKey.getEncoded().length > 0 && | |
230 | - this.privateKey != null && this.privateKey.getEncoded().length > 0) { | |
231 | - builder.setPublicKey(this.publicKey); | |
232 | - builder.setPrivateKey(this.privateKey); | |
233 | - return true; | |
234 | - } | |
235 | - } catch (NoSuchAlgorithmException | InvalidParameterSpecException | InvalidKeySpecException e) { | |
236 | - log.error("Fail create Server with RPK", e); | |
237 | - } | |
238 | - return false; | |
239 | - } | |
240 | - | |
241 | - private void loadOrGenerateRPKKeys() throws NoSuchAlgorithmException, InvalidParameterSpecException, InvalidKeySpecException { | |
242 | - /* Get Elliptic Curve Parameter spec for secp256r1 */ | |
243 | - AlgorithmParameters algoParameters = AlgorithmParameters.getInstance("EC"); | |
244 | - algoParameters.init(new ECGenParameterSpec("secp256r1")); | |
245 | - ECParameterSpec parameterSpec = algoParameters.getParameterSpec(ECParameterSpec.class); | |
246 | - LwM2MTransportServerConfig serverConfig = config; | |
247 | - if (StringUtils.isNotEmpty(serverConfig.getPublicX()) && StringUtils.isNotEmpty(serverConfig.getPublicY())) { | |
248 | - byte[] publicX = Hex.decodeHex(serverConfig.getPublicX().toCharArray()); | |
249 | - byte[] publicY = Hex.decodeHex(serverConfig.getPublicY().toCharArray()); | |
250 | - KeySpec publicKeySpec = new ECPublicKeySpec(new ECPoint(new BigInteger(publicX), new BigInteger(publicY)), | |
251 | - parameterSpec); | |
252 | - this.publicKey = KeyFactory.getInstance("EC").generatePublic(publicKeySpec); | |
253 | - } | |
254 | - String privateEncodedKey = serverConfig.getPrivateEncoded(); | |
255 | - if (StringUtils.isNotEmpty(privateEncodedKey)) { | |
256 | - byte[] privateS = Hex.decodeHex(privateEncodedKey.toCharArray()); | |
257 | - try { | |
258 | - this.privateKey = KeyFactory.getInstance("EC").generatePrivate(new PKCS8EncodedKeySpec(privateS)); | |
259 | - } catch (InvalidKeySpecException ignore2) { | |
260 | - log.error("Invalid Server rpk.PrivateKey.getEncoded () [{}}]. PrivateKey has no EC algorithm", privateEncodedKey); | |
261 | - } | |
262 | - } | |
263 | - } | |
264 | - | |
265 | - private void infoParamsServerKey(PublicKey publicKey, PrivateKey privateKey) { | |
266 | - /* Get x coordinate */ | |
267 | - byte[] x = ((ECPublicKey) publicKey).getW().getAffineX().toByteArray(); | |
268 | - if (x[0] == 0) | |
269 | - x = Arrays.copyOfRange(x, 1, x.length); | |
270 | - | |
271 | - /* Get Y coordinate */ | |
272 | - byte[] y = ((ECPublicKey) publicKey).getW().getAffineY().toByteArray(); | |
273 | - if (y[0] == 0) | |
274 | - y = Arrays.copyOfRange(y, 1, y.length); | |
275 | - | |
276 | - /* Get Curves params */ | |
277 | - String params = ((ECPublicKey) publicKey).getParams().toString(); | |
278 | - String privHex = Hex.encodeHexString(privateKey.getEncoded()); | |
279 | - log.info(" \n- Public Key (Hex): [{}] \n" + | |
280 | - "- Private Key (Hex): [{}], \n" + | |
281 | - "public_x: \"${LWM2M_SERVER_PUBLIC_X:{}}\" \n" + | |
282 | - "public_y: \"${LWM2M_SERVER_PUBLIC_Y:{}}\" \n" + | |
283 | - "private_encoded: \"${LWM2M_SERVER_PRIVATE_ENCODED:{}}\" \n" + | |
284 | - "- Elliptic Curve parameters : [{}]", | |
285 | - Hex.encodeHexString(publicKey.getEncoded()), | |
286 | - privHex, | |
287 | - Hex.encodeHexString(x), | |
288 | - Hex.encodeHexString(y), | |
289 | - privHex, | |
290 | - params); | |
291 | - } | |
292 | - | |
293 | 178 | @Override |
294 | 179 | public String getName() { |
295 | 180 | return "LWM2M"; | ... | ... |
... | ... | @@ -22,8 +22,10 @@ import org.eclipse.leshan.core.attributes.Attribute; |
22 | 22 | import org.eclipse.leshan.core.attributes.AttributeSet; |
23 | 23 | import org.eclipse.leshan.core.model.ObjectModel; |
24 | 24 | import org.eclipse.leshan.core.model.ResourceModel; |
25 | +import org.eclipse.leshan.core.node.LwM2mMultipleResource; | |
25 | 26 | import org.eclipse.leshan.core.node.LwM2mPath; |
26 | 27 | import org.eclipse.leshan.core.node.LwM2mResource; |
28 | +import org.eclipse.leshan.core.node.LwM2mSingleResource; | |
27 | 29 | import org.eclipse.leshan.core.node.codec.CodecException; |
28 | 30 | import org.eclipse.leshan.core.request.SimpleDownlinkRequest; |
29 | 31 | import org.eclipse.leshan.core.request.WriteAttributesRequest; |
... | ... | @@ -48,6 +50,7 @@ import java.util.Arrays; |
48 | 50 | import java.util.Date; |
49 | 51 | import java.util.List; |
50 | 52 | import java.util.Map; |
53 | +import java.util.Optional; | |
51 | 54 | import java.util.concurrent.ConcurrentHashMap; |
52 | 55 | |
53 | 56 | import static org.eclipse.leshan.core.attributes.Attribute.DIMENSION; |
... | ... | @@ -371,4 +374,80 @@ public class LwM2mTransportUtil { |
371 | 374 | return lwm2mResourceValue; |
372 | 375 | } |
373 | 376 | |
377 | + public static Optional<String> contentToString(Object content) { | |
378 | + try { | |
379 | + String value = null; | |
380 | + LwM2mResource resource = null; | |
381 | + String key = null; | |
382 | + if (content instanceof Map) { | |
383 | + Map<Object, Object> contentAsMap = (Map<Object, Object>) content; | |
384 | + if (contentAsMap.size() == 1) { | |
385 | + for (Map.Entry<Object, Object> kv : contentAsMap.entrySet()) { | |
386 | + if (kv.getValue() instanceof LwM2mResource) { | |
387 | + key = kv.getKey().toString(); | |
388 | + resource = (LwM2mResource) kv.getValue(); | |
389 | + } | |
390 | + } | |
391 | + } | |
392 | + } else if (content instanceof LwM2mResource) { | |
393 | + resource = (LwM2mResource) content; | |
394 | + } | |
395 | + if (resource != null && resource.getType() == OPAQUE) { | |
396 | + value = opaqueResourceToString(resource, key); | |
397 | + } | |
398 | + value = value == null ? content.toString() : value; | |
399 | + return Optional.of(value); | |
400 | + } catch (Exception e) { | |
401 | + log.debug("Failed to convert content " + content + " to string", e); | |
402 | + return Optional.ofNullable(content != null ? content.toString() : null); | |
403 | + } | |
404 | + } | |
405 | + | |
406 | + private static String opaqueResourceToString(LwM2mResource resource, String key) { | |
407 | + String value = null; | |
408 | + StringBuilder builder = new StringBuilder(); | |
409 | + if (resource instanceof LwM2mSingleResource) { | |
410 | + builder.append("LwM2mSingleResource"); | |
411 | + if (key == null) { | |
412 | + builder.append(" id=").append(String.valueOf(resource.getId())); | |
413 | + } else { | |
414 | + builder.append(" key=").append(key); | |
415 | + } | |
416 | + builder.append(" value=").append(opaqueToString((byte[]) resource.getValue())); | |
417 | + builder.append(" type=").append(OPAQUE.toString()); | |
418 | + value = builder.toString(); | |
419 | + } else if (resource instanceof LwM2mMultipleResource) { | |
420 | + builder.append("LwM2mMultipleResource"); | |
421 | + if (key == null) { | |
422 | + builder.append(" id=").append(String.valueOf(resource.getId())); | |
423 | + } else { | |
424 | + builder.append(" key=").append(key); | |
425 | + } | |
426 | + builder.append(" values={"); | |
427 | + if (resource.getInstances().size() > 0) { | |
428 | + builder.append(multiInstanceOpaqueToString((LwM2mMultipleResource) resource)); | |
429 | + } | |
430 | + builder.append("}"); | |
431 | + builder.append(" type=").append(OPAQUE.toString()); | |
432 | + value = builder.toString(); | |
433 | + } | |
434 | + return value; | |
435 | + } | |
436 | + | |
437 | + private static String multiInstanceOpaqueToString(LwM2mMultipleResource resource) { | |
438 | + StringBuilder builder = new StringBuilder(); | |
439 | + resource.getInstances().values() | |
440 | + .forEach(v -> builder.append(" id=").append(v.getId()).append(" value=").append(Hex.encodeHexString((byte[]) v.getValue())).append(", ")); | |
441 | + int startInd = builder.lastIndexOf(", "); | |
442 | + if (startInd > 0) { | |
443 | + builder.delete(startInd, startInd + 2); | |
444 | + } | |
445 | + return builder.toString(); | |
446 | + } | |
447 | + | |
448 | + private static String opaqueToString(byte[] value) { | |
449 | + String opaque = Hex.encodeHexString(value); | |
450 | + return opaque.length() > 1024 ? opaque.substring(0, 1024) : opaque; | |
451 | + } | |
452 | + | |
374 | 453 | } | ... | ... |
... | ... | @@ -21,13 +21,13 @@ import lombok.Setter; |
21 | 21 | import lombok.extern.slf4j.Slf4j; |
22 | 22 | import org.eclipse.leshan.core.model.ObjectModel; |
23 | 23 | import org.eclipse.leshan.core.model.ResourceModel; |
24 | -import org.eclipse.leshan.core.node.LwM2mObject; | |
25 | -import org.eclipse.leshan.core.node.LwM2mObjectInstance; | |
24 | +import org.eclipse.leshan.core.node.LwM2mMultipleResource; | |
26 | 25 | import org.eclipse.leshan.core.node.LwM2mPath; |
27 | 26 | import org.eclipse.leshan.core.node.LwM2mResource; |
28 | 27 | import org.eclipse.leshan.core.node.LwM2mSingleResource; |
29 | 28 | import org.eclipse.leshan.core.node.codec.LwM2mValueConverter; |
30 | 29 | import org.eclipse.leshan.core.request.ContentFormat; |
30 | +import org.eclipse.leshan.core.util.Hex; | |
31 | 31 | import org.eclipse.leshan.server.model.LwM2mModelProvider; |
32 | 32 | import org.eclipse.leshan.server.registration.Registration; |
33 | 33 | import org.thingsboard.server.common.data.Device; |
... | ... | @@ -44,6 +44,7 @@ import java.io.IOException; |
44 | 44 | import java.io.ObjectInputStream; |
45 | 45 | import java.io.Serializable; |
46 | 46 | import java.util.Collection; |
47 | +import java.util.HashMap; | |
47 | 48 | import java.util.Map; |
48 | 49 | import java.util.Optional; |
49 | 50 | import java.util.Set; |
... | ... | @@ -53,6 +54,7 @@ import java.util.concurrent.locks.Lock; |
53 | 54 | import java.util.concurrent.locks.ReentrantLock; |
54 | 55 | import java.util.stream.Collectors; |
55 | 56 | |
57 | +import static org.eclipse.leshan.core.model.ResourceModel.Type.OPAQUE; | |
56 | 58 | import static org.thingsboard.server.common.data.lwm2m.LwM2mConstants.LWM2M_SEPARATOR_PATH; |
57 | 59 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.LWM2M_OBJECT_VERSION_DEFAULT; |
58 | 60 | import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.convertObjectIdToVersionedId; |
... | ... | @@ -156,7 +158,6 @@ public class LwM2mClient implements Serializable { |
156 | 158 | this.session = builder.build(); |
157 | 159 | } |
158 | 160 | |
159 | - | |
160 | 161 | private SessionInfoProto createSession(String nodeId, UUID sessionId, ValidateDeviceCredentialsResponse msg) { |
161 | 162 | return SessionInfoProto.newBuilder() |
162 | 163 | .setNodeId(nodeId) |
... | ... | @@ -241,37 +242,7 @@ public class LwM2mClient implements Serializable { |
241 | 242 | .getObjectModel(pathIds.getObjectId()) : null; |
242 | 243 | } |
243 | 244 | |
244 | - public String objectToString(LwM2mObject lwM2mObject) { | |
245 | - StringBuilder builder = new StringBuilder(); | |
246 | - builder.append("LwM2mObject [id=").append(lwM2mObject.getId()).append(", instances={"); | |
247 | - lwM2mObject.getInstances().forEach((instId, inst) -> { | |
248 | - builder.append(instId).append("=").append(this.instanceToString(inst)).append(", "); | |
249 | - }); | |
250 | - int startInd = builder.lastIndexOf(", "); | |
251 | - if (startInd > 0) { | |
252 | - builder.delete(startInd, startInd + 2); | |
253 | - } | |
254 | - builder.append("}]"); | |
255 | - return builder.toString(); | |
256 | - } | |
257 | 245 | |
258 | - public String instanceToString(LwM2mObjectInstance objectInstance) { | |
259 | - StringBuilder builder = new StringBuilder(); | |
260 | - builder.append("LwM2mObjectInstance [id=").append(objectInstance.getId()).append(", resources={"); | |
261 | - objectInstance.getResources().forEach((resId, res) -> { | |
262 | - builder.append(resId).append("=").append(this.resourceToString(res)).append(", "); | |
263 | - }); | |
264 | - int startInd = builder.lastIndexOf(", "); | |
265 | - if (startInd > 0) { | |
266 | - builder.delete(startInd, startInd + 2); | |
267 | - } | |
268 | - builder.append("}]"); | |
269 | - return builder.toString(); | |
270 | - } | |
271 | - | |
272 | - public String resourceToString(LwM2mResource lwM2mResource) { | |
273 | - return lwM2mResource.isMultiInstances() ? lwM2mResource.getInstances().toString() : lwM2mResource.getValue().toString(); | |
274 | - } | |
275 | 246 | |
276 | 247 | public Collection<LwM2mResource> getNewResourceForInstance(String pathRezIdVer, Object params, LwM2mModelProvider modelProvider, |
277 | 248 | LwM2mValueConverter converter) { | ... | ... |
... | ... | @@ -35,6 +35,7 @@ import org.thingsboard.server.gen.transport.TransportProtos; |
35 | 35 | import org.thingsboard.server.queue.util.AfterStartUp; |
36 | 36 | import org.thingsboard.server.queue.util.TbLwM2mTransportComponent; |
37 | 37 | import org.thingsboard.server.transport.lwm2m.config.LwM2MTransportServerConfig; |
38 | +import org.thingsboard.server.transport.lwm2m.config.LwM2mVersion; | |
38 | 39 | import org.thingsboard.server.transport.lwm2m.secure.TbLwM2MSecurityInfo; |
39 | 40 | import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext; |
40 | 41 | import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil; |
... | ... | @@ -339,6 +340,12 @@ public class LwM2mClientContextImpl implements LwM2mClientContext { |
339 | 340 | return lwM2mClientsByRegistrationId.values().stream().filter(e -> deviceId.equals(e.getDeviceId())).findFirst().orElse(null); |
340 | 341 | } |
341 | 342 | |
343 | + @Override | |
344 | + public boolean isComposite(LwM2mClient client) { | |
345 | + return LwM2mVersion.fromVersionStr(client.getRegistration().getLwM2mVersion()).isComposite() & | |
346 | + getProfile(client.getProfileId()).getClientLwM2mSettings().isCompositeOperationsSupport(); | |
347 | + } | |
348 | + | |
342 | 349 | private boolean validateResourceInModel(LwM2mClient lwM2mClient, String pathIdVer, boolean isWritableNotOptional) { |
343 | 350 | ResourceModel resourceModel = lwM2mClient.getResourceModel(pathIdVer, this.config |
344 | 351 | .getModelProvider()); | ... | ... |
... | ... | @@ -21,6 +21,7 @@ import org.eclipse.leshan.core.node.LwM2mResource; |
21 | 21 | import org.eclipse.leshan.core.node.LwM2mResourceInstance; |
22 | 22 | |
23 | 23 | import java.io.Serializable; |
24 | +import java.util.stream.Collectors; | |
24 | 25 | |
25 | 26 | @Data |
26 | 27 | public class ResourceValue implements Serializable { |
... | ... | @@ -45,9 +46,14 @@ public class ResourceValue implements Serializable { |
45 | 46 | |
46 | 47 | private static TbLwM2MResource toTbLwM2MResource(LwM2mResource lwM2mResource) { |
47 | 48 | if (lwM2mResource.isMultiInstances()) { |
48 | - TbLwM2MResourceInstance[] instances = (TbLwM2MResourceInstance[]) lwM2mResource.getInstances().values().stream().map(ResourceValue::toTbLwM2MResourceInstance).toArray(); | |
49 | - return new TbLwM2MMultipleResource(lwM2mResource.getId(), lwM2mResource.getType(), instances); | |
50 | - } else { | |
49 | + if ( lwM2mResource.getInstances().values().size() > 0) { | |
50 | + TbLwM2MResourceInstance [] instances = lwM2mResource.getInstances().values().stream().map(ResourceValue::toTbLwM2MResourceInstance).collect(Collectors.toSet()).toArray(new TbLwM2MResourceInstance[0]); | |
51 | + return new TbLwM2MMultipleResource(lwM2mResource.getId(), lwM2mResource.getType(), instances); | |
52 | + } | |
53 | + else { | |
54 | + return new TbLwM2MMultipleResource(lwM2mResource.getId(), lwM2mResource.getType(), new TbLwM2MResourceInstance[0]); | |
55 | + } | |
56 | + } else { | |
51 | 57 | return new TbLwM2MSingleResource(lwM2mResource.getId(), lwM2mResource.getValue(), lwM2mResource.getType()); |
52 | 58 | } |
53 | 59 | } | ... | ... |
... | ... | @@ -61,9 +61,8 @@ import org.thingsboard.server.transport.lwm2m.server.LwM2mTransportContext; |
61 | 61 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
62 | 62 | import org.thingsboard.server.transport.lwm2m.server.common.LwM2MExecutorAwareService; |
63 | 63 | import org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MReadCompositeRequest; |
64 | -import org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MWriteResponseCompositeCallback; | |
65 | 64 | import org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService; |
66 | -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2MUplinkMsgHandler; | |
65 | +import org.thingsboard.server.transport.lwm2m.server.rpc.composite.RpcWriteCompositeRequest; | |
67 | 66 | import org.thingsboard.server.transport.lwm2m.utils.LwM2mValueConverterImpl; |
68 | 67 | |
69 | 68 | import javax.annotation.PostConstruct; |
... | ... | @@ -73,7 +72,6 @@ import java.util.Collection; |
73 | 72 | import java.util.Date; |
74 | 73 | import java.util.LinkedList; |
75 | 74 | import java.util.List; |
76 | -import java.util.Map; | |
77 | 75 | import java.util.Set; |
78 | 76 | import java.util.function.Function; |
79 | 77 | import java.util.function.Predicate; |
... | ... | @@ -137,19 +135,6 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im |
137 | 135 | } |
138 | 136 | |
139 | 137 | @Override |
140 | - public void sendWriteCompositeRequest(LwM2mClient client, Map<String, Object> nodes, DefaultLwM2MUplinkMsgHandler handler) { | |
141 | -// ResourceModel resourceModelWrite = client.getResourceModel(request.getVersionedId(), this.config.getModelProvider()); | |
142 | - TbLwM2MWriteResponseCompositeCallback callback = new TbLwM2MWriteResponseCompositeCallback(handler, logService, client, null); | |
143 | - ContentFormat contentFormat = ContentFormat.SENML_JSON; | |
144 | - try { | |
145 | - WriteCompositeRequest downlink = new WriteCompositeRequest(contentFormat, nodes); | |
146 | - sendWriteCompositeRequest(client, downlink, this.config.getTimeout(), callback); | |
147 | - } catch (Exception e) { | |
148 | - callback.onError(JacksonUtil.toString(nodes), e); | |
149 | - } | |
150 | - } | |
151 | - | |
152 | - @Override | |
153 | 138 | public void sendObserveRequest(LwM2mClient client, TbLwM2MObserveRequest request, DownlinkRequestCallback<ObserveRequest, ObserveResponse> callback) { |
154 | 139 | validateVersionedId(client, request); |
155 | 140 | LwM2mPath resultIds = new LwM2mPath(request.getObjectId()); |
... | ... | @@ -259,6 +244,17 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im |
259 | 244 | } |
260 | 245 | |
261 | 246 | @Override |
247 | + public void sendWriteCompositeRequest(LwM2mClient client, RpcWriteCompositeRequest rpcWriteCompositeRequest, DownlinkRequestCallback<WriteCompositeRequest, WriteCompositeResponse> callback) { | |
248 | + ContentFormat contentFormat = ContentFormat.SENML_JSON; | |
249 | + try { | |
250 | + WriteCompositeRequest downlink = new WriteCompositeRequest(contentFormat, rpcWriteCompositeRequest.getNodes()); | |
251 | + sendWriteCompositeRequest(client, downlink, this.config.getTimeout(), callback); | |
252 | + } catch (Exception e) { | |
253 | + callback.onError(JacksonUtil.toString(rpcWriteCompositeRequest), e); | |
254 | + } | |
255 | + } | |
256 | + | |
257 | + @Override | |
262 | 258 | public void sendWriteUpdateRequest(LwM2mClient client, TbLwM2MWriteUpdateRequest request, DownlinkRequestCallback<WriteRequest, WriteResponse> callback) { |
263 | 259 | LwM2mPath resultIds = new LwM2mPath(request.getObjectId()); |
264 | 260 | if (resultIds.isResource()) { |
... | ... | @@ -330,7 +326,11 @@ public class DefaultLwM2mDownlinkMsgHandler extends LwM2MExecutorAwareService im |
330 | 326 | context.getServer().send(registration, request, timeoutInMs, response -> { |
331 | 327 | executor.submit(() -> { |
332 | 328 | try { |
333 | - callback.onSuccess(request, response); | |
329 | + if (response.isSuccess()) { | |
330 | + callback.onSuccess(request, response); | |
331 | + } else { | |
332 | + callback.onValidationError(request.getNodes().values().toString(), response.getErrorMessage()); | |
333 | + } | |
334 | 334 | } catch (Exception e) { |
335 | 335 | log.error("[{}] failed to process successful response [{}] ", registration.getEndpoint(), response, e); |
336 | 336 | } | ... | ... |
... | ... | @@ -23,6 +23,7 @@ import org.eclipse.leshan.core.request.ObserveRequest; |
23 | 23 | import org.eclipse.leshan.core.request.ReadCompositeRequest; |
24 | 24 | import org.eclipse.leshan.core.request.ReadRequest; |
25 | 25 | import org.eclipse.leshan.core.request.WriteAttributesRequest; |
26 | +import org.eclipse.leshan.core.request.WriteCompositeRequest; | |
26 | 27 | import org.eclipse.leshan.core.request.WriteRequest; |
27 | 28 | import org.eclipse.leshan.core.response.DeleteResponse; |
28 | 29 | import org.eclipse.leshan.core.response.DiscoverResponse; |
... | ... | @@ -31,13 +32,13 @@ import org.eclipse.leshan.core.response.ObserveResponse; |
31 | 32 | import org.eclipse.leshan.core.response.ReadCompositeResponse; |
32 | 33 | import org.eclipse.leshan.core.response.ReadResponse; |
33 | 34 | import org.eclipse.leshan.core.response.WriteAttributesResponse; |
35 | +import org.eclipse.leshan.core.response.WriteCompositeResponse; | |
34 | 36 | import org.eclipse.leshan.core.response.WriteResponse; |
35 | 37 | import org.thingsboard.server.transport.lwm2m.server.client.LwM2mClient; |
36 | 38 | import org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MReadCompositeRequest; |
37 | -import org.thingsboard.server.transport.lwm2m.server.uplink.DefaultLwM2MUplinkMsgHandler; | |
39 | +import org.thingsboard.server.transport.lwm2m.server.rpc.composite.RpcWriteCompositeRequest; | |
38 | 40 | |
39 | 41 | import java.util.List; |
40 | -import java.util.Map; | |
41 | 42 | import java.util.Set; |
42 | 43 | |
43 | 44 | public interface LwM2mDownlinkMsgHandler { |
... | ... | @@ -66,7 +67,7 @@ public interface LwM2mDownlinkMsgHandler { |
66 | 67 | |
67 | 68 | void sendWriteReplaceRequest(LwM2mClient client, TbLwM2MWriteReplaceRequest request, DownlinkRequestCallback<WriteRequest, WriteResponse> callback); |
68 | 69 | |
69 | - void sendWriteCompositeRequest(LwM2mClient client, Map<String, Object> nodes, DefaultLwM2MUplinkMsgHandler handler); | |
70 | + void sendWriteCompositeRequest(LwM2mClient client, RpcWriteCompositeRequest nodes, DownlinkRequestCallback<WriteCompositeRequest, WriteCompositeResponse> callback); | |
70 | 71 | |
71 | 72 | void sendWriteUpdateRequest(LwM2mClient client, TbLwM2MWriteUpdateRequest request, DownlinkRequestCallback<WriteRequest, WriteResponse> callback); |
72 | 73 | ... | ... |
... | ... | @@ -337,7 +337,7 @@ public class DefaultLwM2MOtaUpdateService extends LwM2MExecutorAwareService impl |
337 | 337 | SoftwareUpdateResult result = SoftwareUpdateResult.fromUpdateResultSwByCode(code.intValue()); |
338 | 338 | Optional<OtaPackageUpdateStatus> status = toOtaPackageUpdateStatus(result); |
339 | 339 | status.ifPresent(otaStatus -> sendStateUpdateToTelemetry(client, swInfo, |
340 | - otaStatus, "Firmware Update Result: " + result.name())); | |
340 | + otaStatus, "Software Update Result: " + result.name())); | |
341 | 341 | if (result.isAgain() && swInfo.getRetryAttempts() <= 2) { |
342 | 342 | swInfo.setRetryAttempts(swInfo.getRetryAttempts() + 1); |
343 | 343 | startSoftwareUpdateIfNeeded(client, swInfo); | ... | ... |
... | ... | @@ -52,6 +52,7 @@ import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteRespon |
52 | 52 | import org.thingsboard.server.transport.lwm2m.server.downlink.TbLwM2MWriteUpdateRequest; |
53 | 53 | import org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MReadCompositeCallback; |
54 | 54 | import org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MReadCompositeRequest; |
55 | +import org.thingsboard.server.transport.lwm2m.server.downlink.composite.TbLwM2MWriteResponseCompositeCallback; | |
55 | 56 | import org.thingsboard.server.transport.lwm2m.server.log.LwM2MTelemetryLogService; |
56 | 57 | import org.thingsboard.server.transport.lwm2m.server.rpc.composite.RpcReadCompositeRequest; |
57 | 58 | import org.thingsboard.server.transport.lwm2m.server.rpc.composite.RpcReadResponseCompositeCallback; |
... | ... | @@ -131,15 +132,20 @@ public class DefaultLwM2MRpcRequestHandler implements LwM2MRpcRequestHandler { |
131 | 132 | throw new IllegalArgumentException("Unsupported operation: " + operationType.name()); |
132 | 133 | } |
133 | 134 | } else if (operationType.isComposite()) { |
134 | - switch (operationType) { | |
135 | - case READ_COMPOSITE: | |
136 | - sendReadCompositeRequest(client, rpcRequst); | |
137 | - break; | |
138 | - case WRITE_COMPOSITE: | |
139 | - sendWriteCompositeRequest(client, rpcRequst); | |
140 | - break; | |
141 | - default: | |
142 | - throw new IllegalArgumentException("Unsupported operation: " + operationType.name()); | |
135 | + if (clientContext.isComposite(client)) { | |
136 | + switch (operationType) { | |
137 | + case READ_COMPOSITE: | |
138 | + sendReadCompositeRequest(client, rpcRequst); | |
139 | + break; | |
140 | + case WRITE_COMPOSITE: | |
141 | + sendWriteCompositeRequest(client, rpcRequst); | |
142 | + break; | |
143 | + default: | |
144 | + throw new IllegalArgumentException("Unsupported operation: " + operationType.name()); | |
145 | + } | |
146 | + } else { | |
147 | + this.sendErrorRpcResponse(sessionInfo, rpcRequst.getRequestId(), | |
148 | + ResponseCode.INTERNAL_SERVER_ERROR.getName(), "This device does not support Composite Operation"); | |
143 | 149 | } |
144 | 150 | } else { |
145 | 151 | switch (operationType) { |
... | ... | @@ -239,14 +245,19 @@ public class DefaultLwM2MRpcRequestHandler implements LwM2MRpcRequestHandler { |
239 | 245 | downlinkHandler.sendWriteReplaceRequest(client, request, rpcCallback); |
240 | 246 | } |
241 | 247 | |
248 | + /** | |
249 | + * WriteComposite {"nodes":{"/3/0/14":"+04", "/1/0/2":100, "/5/0/1":"coap://localhost:5685"}} | |
250 | + * {"result":"CHANGED"} | |
251 | + * Map<String, Object> nodes = new HashMap<>(); | |
252 | + * nodes.put("/3/0/14", "+02"); | |
253 | + * nodes.put("/1/0/2", 100); | |
254 | + * nodes.put("/5/0/1", "coap://localhost:5685"); | |
255 | + */ | |
242 | 256 | private void sendWriteCompositeRequest(LwM2mClient client, TransportProtos.ToDeviceRpcRequestMsg requestMsg) { |
243 | - RpcWriteCompositeRequest nodes = JacksonUtil.fromString(requestMsg.getParams(), RpcWriteCompositeRequest.class); | |
244 | -// TbLwM2MWriteReplaceRequest request = TbLwM2MWriteReplaceRequest.builder().versionedId(versionedId) | |
245 | -// .value(requestBody.getValue()) | |
246 | -// .timeout(this.config.getTimeout()).build(); | |
247 | -// var mainCallback = new TbLwM2MWriteResponseCallback(uplinkHandler, logService, client, versionedId); | |
248 | -// var rpcCallback = new RpcEmptyResponseCallback<>(transportService, client, requestMsg, mainCallback); | |
249 | -// downlinkHandler.sendWriteReplaceRequest(client, request, rpcCallback); | |
257 | + RpcWriteCompositeRequest rpcWriteCompositeRequest = JacksonUtil.fromString(requestMsg.getParams(), RpcWriteCompositeRequest.class); | |
258 | + var mainCallback = new TbLwM2MWriteResponseCompositeCallback(uplinkHandler, logService, client, null); | |
259 | + var rpcCallback = new RpcEmptyResponseCallback<>(transportService, client, requestMsg, mainCallback); | |
260 | + downlinkHandler.sendWriteCompositeRequest(client, rpcWriteCompositeRequest, rpcCallback); | |
250 | 261 | } |
251 | 262 | |
252 | 263 | private void sendCancelObserveRequest(LwM2mClient client, TransportProtos.ToDeviceRpcRequestMsg requestMsg, String versionedId) { | ... | ... |
... | ... | @@ -15,9 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.transport.lwm2m.server.rpc; |
17 | 17 | |
18 | -import org.eclipse.leshan.core.node.LwM2mObject; | |
19 | -import org.eclipse.leshan.core.node.LwM2mObjectInstance; | |
20 | -import org.eclipse.leshan.core.node.LwM2mResource; | |
21 | 18 | import org.eclipse.leshan.core.request.LwM2mRequest; |
22 | 19 | import org.eclipse.leshan.core.response.ReadResponse; |
23 | 20 | import org.thingsboard.server.common.transport.TransportService; |
... | ... | @@ -27,6 +24,8 @@ import org.thingsboard.server.transport.lwm2m.server.downlink.DownlinkRequestCal |
27 | 24 | |
28 | 25 | import java.util.Optional; |
29 | 26 | |
27 | +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.contentToString; | |
28 | + | |
30 | 29 | public class RpcReadResponseCallback<R extends LwM2mRequest<T>, T extends ReadResponse> extends RpcLwM2MDownlinkCallback<R, T> { |
31 | 30 | |
32 | 31 | public RpcReadResponseCallback(TransportService transportService, LwM2mClient client, TransportProtos.ToDeviceRpcRequestMsg requestMsg, DownlinkRequestCallback<R, T> callback) { |
... | ... | @@ -35,14 +34,6 @@ public class RpcReadResponseCallback<R extends LwM2mRequest<T>, T extends ReadRe |
35 | 34 | |
36 | 35 | @Override |
37 | 36 | protected Optional<String> serializeSuccessfulResponse(T response) { |
38 | - Object value = null; | |
39 | - if (response.getContent() instanceof LwM2mObject) { | |
40 | - value = client.objectToString((LwM2mObject) response.getContent()); | |
41 | - } else if (response.getContent() instanceof LwM2mObjectInstance) { | |
42 | - value = client.instanceToString((LwM2mObjectInstance) response.getContent()); | |
43 | - } else if (response.getContent() instanceof LwM2mResource) { | |
44 | - value = client.resourceToString((LwM2mResource) response.getContent()); | |
45 | - } | |
46 | - return Optional.of(String.format("%s", value)); | |
37 | + return contentToString(response.getContent()); | |
47 | 38 | } |
48 | 39 | } | ... | ... |
... | ... | @@ -16,7 +16,6 @@ |
16 | 16 | package org.thingsboard.server.transport.lwm2m.server.rpc.composite; |
17 | 17 | |
18 | 18 | import org.eclipse.leshan.core.request.LwM2mRequest; |
19 | -import org.eclipse.leshan.core.request.ReadCompositeRequest; | |
20 | 19 | import org.eclipse.leshan.core.response.ReadCompositeResponse; |
21 | 20 | import org.thingsboard.server.common.transport.TransportService; |
22 | 21 | import org.thingsboard.server.gen.transport.TransportProtos; |
... | ... | @@ -26,6 +25,8 @@ import org.thingsboard.server.transport.lwm2m.server.rpc.RpcLwM2MDownlinkCallbac |
26 | 25 | |
27 | 26 | import java.util.Optional; |
28 | 27 | |
28 | +import static org.thingsboard.server.transport.lwm2m.server.LwM2mTransportUtil.contentToString; | |
29 | + | |
29 | 30 | public class RpcReadResponseCompositeCallback<R extends LwM2mRequest<T>, T extends ReadCompositeResponse> extends RpcLwM2MDownlinkCallback<R, T> { |
30 | 31 | |
31 | 32 | public RpcReadResponseCompositeCallback(TransportService transportService, LwM2mClient client, TransportProtos.ToDeviceRpcRequestMsg requestMsg, DownlinkRequestCallback<R, T> callback) { |
... | ... | @@ -34,6 +35,6 @@ public class RpcReadResponseCompositeCallback<R extends LwM2mRequest<T>, T exten |
34 | 35 | |
35 | 36 | @Override |
36 | 37 | protected Optional<String> serializeSuccessfulResponse(T response) { |
37 | - return Optional.of(String.format("%s", response.getContent().toString())); | |
38 | + return contentToString(response.getContent()); | |
38 | 39 | } |
39 | 40 | } | ... | ... |
... | ... | @@ -15,8 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.transport.lwm2m.server.store; |
17 | 17 | |
18 | -import org.thingsboard.server.common.data.ota.OtaPackageType; | |
19 | -import org.thingsboard.server.transport.lwm2m.server.ota.LwM2MClientOtaInfo; | |
20 | 18 | import org.thingsboard.server.transport.lwm2m.server.ota.firmware.LwM2MClientFwOtaInfo; |
21 | 19 | import org.thingsboard.server.transport.lwm2m.server.ota.software.LwM2MClientSwOtaInfo; |
22 | 20 | ... | ... |
... | ... | @@ -15,12 +15,10 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.transport.mqtt; |
17 | 17 | |
18 | -import com.google.common.io.Resources; | |
19 | 18 | import io.netty.handler.ssl.SslHandler; |
20 | 19 | import lombok.extern.slf4j.Slf4j; |
21 | 20 | import org.springframework.beans.factory.annotation.Autowired; |
22 | 21 | import org.springframework.beans.factory.annotation.Value; |
23 | -import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; | |
24 | 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
25 | 23 | import org.springframework.stereotype.Component; |
26 | 24 | import org.springframework.util.StringUtils; |
... | ... | @@ -30,8 +28,8 @@ import org.thingsboard.server.common.msg.EncryptionUtil; |
30 | 28 | import org.thingsboard.server.common.transport.TransportService; |
31 | 29 | import org.thingsboard.server.common.transport.TransportServiceCallback; |
32 | 30 | import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsResponse; |
33 | -import org.thingsboard.server.gen.transport.TransportProtos; | |
34 | 31 | import org.thingsboard.server.common.transport.util.SslUtil; |
32 | +import org.thingsboard.server.gen.transport.TransportProtos; | |
35 | 33 | |
36 | 34 | import javax.net.ssl.KeyManager; |
37 | 35 | import javax.net.ssl.KeyManagerFactory; |
... | ... | @@ -40,10 +38,7 @@ import javax.net.ssl.SSLEngine; |
40 | 38 | import javax.net.ssl.TrustManager; |
41 | 39 | import javax.net.ssl.TrustManagerFactory; |
42 | 40 | import javax.net.ssl.X509TrustManager; |
43 | -import java.io.File; | |
44 | -import java.io.FileInputStream; | |
45 | 41 | import java.io.InputStream; |
46 | -import java.net.URL; | |
47 | 42 | import java.security.KeyStore; |
48 | 43 | import java.security.cert.CertificateEncodingException; |
49 | 44 | import java.security.cert.CertificateException; |
... | ... | @@ -73,7 +68,23 @@ public class MqttSslHandlerProvider { |
73 | 68 | @Autowired |
74 | 69 | private TransportService transportService; |
75 | 70 | |
71 | + private SSLContext sslContext; | |
72 | + | |
76 | 73 | public SslHandler getSslHandler() { |
74 | + if (sslContext == null) { | |
75 | + sslContext = createSslContext(); | |
76 | + } | |
77 | + SSLEngine sslEngine = sslContext.createSSLEngine(); | |
78 | + sslEngine.setUseClientMode(false); | |
79 | + sslEngine.setNeedClientAuth(false); | |
80 | + sslEngine.setWantClientAuth(true); | |
81 | + sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols()); | |
82 | + sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites()); | |
83 | + sslEngine.setEnableSessionCreation(true); | |
84 | + return new SslHandler(sslEngine); | |
85 | + } | |
86 | + | |
87 | + private SSLContext createSslContext() { | |
77 | 88 | try { |
78 | 89 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
79 | 90 | KeyStore trustStore = KeyStore.getInstance(keyStoreType); |
... | ... | @@ -97,17 +108,10 @@ public class MqttSslHandlerProvider { |
97 | 108 | } |
98 | 109 | SSLContext sslContext = SSLContext.getInstance(sslProtocol); |
99 | 110 | sslContext.init(km, tm, null); |
100 | - SSLEngine sslEngine = sslContext.createSSLEngine(); | |
101 | - sslEngine.setUseClientMode(false); | |
102 | - sslEngine.setNeedClientAuth(false); | |
103 | - sslEngine.setWantClientAuth(true); | |
104 | - sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols()); | |
105 | - sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites()); | |
106 | - sslEngine.setEnableSessionCreation(true); | |
107 | - return new SslHandler(sslEngine); | |
111 | + return sslContext; | |
108 | 112 | } catch (Exception e) { |
109 | 113 | log.error("Unable to set up SSL context. Reason: " + e.getMessage(), e); |
110 | - throw new RuntimeException("Failed to get SSL handler", e); | |
114 | + throw new RuntimeException("Failed to get SSL context", e); | |
111 | 115 | } |
112 | 116 | } |
113 | 117 | ... | ... |
... | ... | @@ -28,16 +28,18 @@ import io.netty.handler.ssl.SslHandler; |
28 | 28 | public class MqttTransportServerInitializer extends ChannelInitializer<SocketChannel> { |
29 | 29 | |
30 | 30 | private final MqttTransportContext context; |
31 | + private final boolean sslEnabled; | |
31 | 32 | |
32 | - public MqttTransportServerInitializer(MqttTransportContext context) { | |
33 | + public MqttTransportServerInitializer(MqttTransportContext context, boolean sslEnabled) { | |
33 | 34 | this.context = context; |
35 | + this.sslEnabled = sslEnabled; | |
34 | 36 | } |
35 | 37 | |
36 | 38 | @Override |
37 | 39 | public void initChannel(SocketChannel ch) { |
38 | 40 | ChannelPipeline pipeline = ch.pipeline(); |
39 | 41 | SslHandler sslHandler = null; |
40 | - if (context.getSslHandlerProvider() != null) { | |
42 | + if (sslEnabled && context.getSslHandlerProvider() != null) { | |
41 | 43 | sslHandler = context.getSslHandlerProvider().getSslHandler(); |
42 | 44 | pipeline.addLast(sslHandler); |
43 | 45 | } | ... | ... |
... | ... | @@ -46,6 +46,14 @@ public class MqttTransportService implements TbTransportService { |
46 | 46 | @Value("${transport.mqtt.bind_port}") |
47 | 47 | private Integer port; |
48 | 48 | |
49 | + @Value("${transport.mqtt.ssl.enabled}") | |
50 | + private boolean sslEnabled; | |
51 | + | |
52 | + @Value("${transport.mqtt.ssl.bind_address}") | |
53 | + private String sslHost; | |
54 | + @Value("${transport.mqtt.ssl.bind_port}") | |
55 | + private Integer sslPort; | |
56 | + | |
49 | 57 | @Value("${transport.mqtt.netty.leak_detector_level}") |
50 | 58 | private String leakDetectorLevel; |
51 | 59 | @Value("${transport.mqtt.netty.boss_group_thread_count}") |
... | ... | @@ -59,6 +67,7 @@ public class MqttTransportService implements TbTransportService { |
59 | 67 | private MqttTransportContext context; |
60 | 68 | |
61 | 69 | private Channel serverChannel; |
70 | + private Channel sslServerChannel; | |
62 | 71 | private EventLoopGroup bossGroup; |
63 | 72 | private EventLoopGroup workerGroup; |
64 | 73 | |
... | ... | @@ -73,10 +82,18 @@ public class MqttTransportService implements TbTransportService { |
73 | 82 | ServerBootstrap b = new ServerBootstrap(); |
74 | 83 | b.group(bossGroup, workerGroup) |
75 | 84 | .channel(NioServerSocketChannel.class) |
76 | - .childHandler(new MqttTransportServerInitializer(context)) | |
85 | + .childHandler(new MqttTransportServerInitializer(context, false)) | |
77 | 86 | .childOption(ChannelOption.SO_KEEPALIVE, keepAlive); |
78 | 87 | |
79 | 88 | serverChannel = b.bind(host, port).sync().channel(); |
89 | + if (sslEnabled) { | |
90 | + b = new ServerBootstrap(); | |
91 | + b.group(bossGroup, workerGroup) | |
92 | + .channel(NioServerSocketChannel.class) | |
93 | + .childHandler(new MqttTransportServerInitializer(context, true)) | |
94 | + .childOption(ChannelOption.SO_KEEPALIVE, keepAlive); | |
95 | + sslServerChannel = b.bind(sslHost, sslPort).sync().channel(); | |
96 | + } | |
80 | 97 | log.info("Mqtt transport started!"); |
81 | 98 | } |
82 | 99 | |
... | ... | @@ -85,6 +102,9 @@ public class MqttTransportService implements TbTransportService { |
85 | 102 | log.info("Stopping MQTT transport!"); |
86 | 103 | try { |
87 | 104 | serverChannel.close().sync(); |
105 | + if (sslEnabled) { | |
106 | + sslServerChannel.close().sync(); | |
107 | + } | |
88 | 108 | } finally { |
89 | 109 | workerGroup.shutdownGracefully(); |
90 | 110 | bossGroup.shutdownGracefully(); | ... | ... |
... | ... | @@ -75,6 +75,11 @@ |
75 | 75 | <scope>test</scope> |
76 | 76 | </dependency> |
77 | 77 | <dependency> |
78 | + <groupId>org.hamcrest</groupId> | |
79 | + <artifactId>hamcrest</artifactId> | |
80 | + <scope>test</scope> | |
81 | + </dependency> | |
82 | + <dependency> | |
78 | 83 | <groupId>org.mockito</groupId> |
79 | 84 | <artifactId>mockito-core</artifactId> |
80 | 85 | <scope>test</scope> | ... | ... |
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.common.util; | |
17 | + | |
18 | +import lombok.EqualsAndHashCode; | |
19 | +import lombok.Getter; | |
20 | +import lombok.ToString; | |
21 | + | |
22 | +import java.util.LinkedHashMap; | |
23 | +import java.util.Map; | |
24 | +import java.util.function.BiConsumer; | |
25 | + | |
26 | +/** | |
27 | + * LinkedHashMap that removed eldest entries (by insert order) | |
28 | + * It guaranteed that size is not greater then maxEntries parameter. And remove time is constant O(1). | |
29 | + * Example: | |
30 | + * LinkedHashMapRemoveEldest<Long, String> map = | |
31 | + * new LinkedHashMapRemoveEldest<>(MAX_ENTRIES, this::removeConsumer); | |
32 | + * */ | |
33 | +@Getter | |
34 | +@ToString(callSuper = true) | |
35 | +@EqualsAndHashCode(callSuper = true) | |
36 | +public class LinkedHashMapRemoveEldest<K, V> extends LinkedHashMap<K, V> { | |
37 | + final long maxEntries; | |
38 | + final BiConsumer<K, V> removalConsumer; | |
39 | + | |
40 | + public LinkedHashMapRemoveEldest(long maxEntries, BiConsumer<K, V> removalConsumer) { | |
41 | + this.maxEntries = maxEntries; | |
42 | + this.removalConsumer = removalConsumer; | |
43 | + } | |
44 | + | |
45 | + @Override | |
46 | + protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { | |
47 | + if (size() <= maxEntries) { | |
48 | + return false; | |
49 | + } | |
50 | + removalConsumer.accept(eldest.getKey(), eldest.getValue()); | |
51 | + return true; | |
52 | + } | |
53 | +} | ... | ... |
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.common.util; | |
17 | + | |
18 | +import org.hamcrest.Matchers; | |
19 | +import org.junit.Test; | |
20 | + | |
21 | +import java.util.LinkedHashMap; | |
22 | + | |
23 | +import static org.hamcrest.CoreMatchers.instanceOf; | |
24 | +import static org.hamcrest.CoreMatchers.is; | |
25 | +import static org.hamcrest.CoreMatchers.notNullValue; | |
26 | +import static org.hamcrest.MatcherAssert.assertThat; | |
27 | + | |
28 | +public class LinkedHashMapRemoveEldestTest { | |
29 | + | |
30 | + public static final long MAX_ENTRIES = 10L; | |
31 | + long removeCount = 0; | |
32 | + | |
33 | + void removalConsumer(Long id, String name) { | |
34 | + removeCount++; | |
35 | + assertThat(id, is(Matchers.lessThan(MAX_ENTRIES))); | |
36 | + assertThat(name, is(id.toString())); | |
37 | + } | |
38 | + | |
39 | + @Test | |
40 | + public void givenMap_whenOverSized_thenVerifyRemovedEldest() { | |
41 | + //given | |
42 | + LinkedHashMapRemoveEldest<Long, String> map = | |
43 | + new LinkedHashMapRemoveEldest<>(MAX_ENTRIES, this::removalConsumer); | |
44 | + | |
45 | + assertThat(map.getMaxEntries(), is(MAX_ENTRIES)); | |
46 | + assertThat(map.getRemovalConsumer(), notNullValue()); | |
47 | + assertThat(map, instanceOf(LinkedHashMap.class)); | |
48 | + assertThat(map, instanceOf(LinkedHashMapRemoveEldest.class)); | |
49 | + assertThat(map.size(), is(0)); | |
50 | + | |
51 | + //when | |
52 | + for (long i = 0; i < MAX_ENTRIES * 2; i++) { | |
53 | + map.put(i, String.valueOf(i)); | |
54 | + } | |
55 | + | |
56 | + //then | |
57 | + assertThat((long) map.size(), is(MAX_ENTRIES)); | |
58 | + assertThat(removeCount, is(MAX_ENTRIES)); | |
59 | + for (long i = MAX_ENTRIES; i < MAX_ENTRIES * 2; i++) { | |
60 | + assertThat(map.get(i), is(String.valueOf(i))); | |
61 | + } | |
62 | + } | |
63 | + | |
64 | +} | |
\ No newline at end of file | ... | ... |
... | ... | @@ -19,10 +19,12 @@ import com.google.common.util.concurrent.Futures; |
19 | 19 | import com.google.common.util.concurrent.ListenableFuture; |
20 | 20 | import com.google.common.util.concurrent.MoreExecutors; |
21 | 21 | import lombok.extern.slf4j.Slf4j; |
22 | +import org.springframework.beans.factory.annotation.Value; | |
22 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
23 | 24 | import org.springframework.cache.Cache; |
24 | 25 | import org.springframework.context.annotation.Primary; |
25 | 26 | import org.springframework.stereotype.Service; |
27 | +import org.springframework.util.StringUtils; | |
26 | 28 | import org.thingsboard.server.common.data.EntityType; |
27 | 29 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
28 | 30 | import org.thingsboard.server.common.data.id.EntityId; |
... | ... | @@ -34,6 +36,7 @@ import org.thingsboard.server.common.stats.StatsFactory; |
34 | 36 | import org.thingsboard.server.dao.cache.CacheExecutorService; |
35 | 37 | import org.thingsboard.server.dao.service.Validator; |
36 | 38 | |
39 | +import javax.annotation.PostConstruct; | |
37 | 40 | import java.util.ArrayList; |
38 | 41 | import java.util.Collection; |
39 | 42 | import java.util.HashMap; |
... | ... | @@ -43,6 +46,7 @@ import java.util.Map; |
43 | 46 | import java.util.Objects; |
44 | 47 | import java.util.Optional; |
45 | 48 | import java.util.Set; |
49 | +import java.util.concurrent.Executor; | |
46 | 50 | import java.util.stream.Collectors; |
47 | 51 | |
48 | 52 | import static org.thingsboard.server.dao.attributes.AttributeUtils.validate; |
... | ... | @@ -53,12 +57,17 @@ import static org.thingsboard.server.dao.attributes.AttributeUtils.validate; |
53 | 57 | @Slf4j |
54 | 58 | public class CachedAttributesService implements AttributesService { |
55 | 59 | private static final String STATS_NAME = "attributes.cache"; |
60 | + public static final String LOCAL_CACHE_TYPE = "caffeine"; | |
56 | 61 | |
57 | 62 | private final AttributesDao attributesDao; |
58 | 63 | private final AttributesCacheWrapper cacheWrapper; |
64 | + private final CacheExecutorService cacheExecutorService; | |
59 | 65 | private final DefaultCounter hitCounter; |
60 | 66 | private final DefaultCounter missCounter; |
61 | - private final CacheExecutorService cacheExecutorService; | |
67 | + private Executor cacheExecutor; | |
68 | + | |
69 | + @Value("${cache.type}") | |
70 | + private String cacheType; | |
62 | 71 | |
63 | 72 | public CachedAttributesService(AttributesDao attributesDao, |
64 | 73 | AttributesCacheWrapper cacheWrapper, |
... | ... | @@ -72,6 +81,25 @@ public class CachedAttributesService implements AttributesService { |
72 | 81 | this.missCounter = statsFactory.createDefaultCounter(STATS_NAME, "result", "miss"); |
73 | 82 | } |
74 | 83 | |
84 | + @PostConstruct | |
85 | + public void init() { | |
86 | + this.cacheExecutor = getExecutor(cacheType, cacheExecutorService); | |
87 | + } | |
88 | + | |
89 | + /** | |
90 | + * Will return: | |
91 | + * - for the <b>local</b> cache type (cache.type="coffeine"): directExecutor (run callback immediately in the same thread) | |
92 | + * - for the <b>remote</b> cache: dedicated thread pool for the cache IO calls to unblock any caller thread | |
93 | + * */ | |
94 | + Executor getExecutor(String cacheType, CacheExecutorService cacheExecutorService) { | |
95 | + if (StringUtils.isEmpty(cacheType) || LOCAL_CACHE_TYPE.equals(cacheType)) { | |
96 | + log.info("Going to use directExecutor for the local cache type {}", cacheType); | |
97 | + return MoreExecutors.directExecutor(); | |
98 | + } | |
99 | + log.info("Going to use cacheExecutorService for the remote cache type {}", cacheType); | |
100 | + return cacheExecutorService; | |
101 | + } | |
102 | + | |
75 | 103 | @Override |
76 | 104 | public ListenableFuture<Optional<AttributeKvEntry>> find(TenantId tenantId, EntityId entityId, String scope, String attributeKey) { |
77 | 105 | validate(entityId, scope); |
... | ... | @@ -90,7 +118,7 @@ public class CachedAttributesService implements AttributesService { |
90 | 118 | // TODO: think if it's a good idea to store 'empty' attributes |
91 | 119 | cacheWrapper.put(attributeCacheKey, foundAttrKvEntry.orElse(null)); |
92 | 120 | return foundAttrKvEntry; |
93 | - }, cacheExecutorService); | |
121 | + }, cacheExecutor); | |
94 | 122 | } |
95 | 123 | } |
96 | 124 | |
... | ... | @@ -113,7 +141,7 @@ public class CachedAttributesService implements AttributesService { |
113 | 141 | notFoundAttributeKeys.removeAll(wrappedCachedAttributes.keySet()); |
114 | 142 | |
115 | 143 | ListenableFuture<List<AttributeKvEntry>> result = attributesDao.find(tenantId, entityId, scope, notFoundAttributeKeys); |
116 | - return Futures.transform(result, foundInDbAttributes -> mergeDbAndCacheAttributes(entityId, scope, cachedAttributes, notFoundAttributeKeys, foundInDbAttributes), cacheExecutorService); | |
144 | + return Futures.transform(result, foundInDbAttributes -> mergeDbAndCacheAttributes(entityId, scope, cachedAttributes, notFoundAttributeKeys, foundInDbAttributes), cacheExecutor); | |
117 | 145 | |
118 | 146 | } |
119 | 147 | |
... | ... | @@ -171,7 +199,7 @@ public class CachedAttributesService implements AttributesService { |
171 | 199 | |
172 | 200 | // TODO: can do if (attributesCache.get() != null) attributesCache.put() instead, but will be more twice more requests to cache |
173 | 201 | List<String> attributeKeys = attributes.stream().map(KvEntry::getKey).collect(Collectors.toList()); |
174 | - future.addListener(() -> evictAttributesFromCache(tenantId, entityId, scope, attributeKeys), cacheExecutorService); | |
202 | + future.addListener(() -> evictAttributesFromCache(tenantId, entityId, scope, attributeKeys), cacheExecutor); | |
175 | 203 | return future; |
176 | 204 | } |
177 | 205 | |
... | ... | @@ -179,7 +207,7 @@ public class CachedAttributesService implements AttributesService { |
179 | 207 | public ListenableFuture<List<Void>> removeAll(TenantId tenantId, EntityId entityId, String scope, List<String> attributeKeys) { |
180 | 208 | validate(entityId, scope); |
181 | 209 | ListenableFuture<List<Void>> future = attributesDao.removeAll(tenantId, entityId, scope, attributeKeys); |
182 | - future.addListener(() -> evictAttributesFromCache(tenantId, entityId, scope, attributeKeys), cacheExecutorService); | |
210 | + future.addListener(() -> evictAttributesFromCache(tenantId, entityId, scope, attributeKeys), cacheExecutor); | |
183 | 211 | return future; |
184 | 212 | } |
185 | 213 | ... | ... |
... | ... | @@ -56,4 +56,6 @@ public interface DashboardInfoDao extends Dao<DashboardInfo> { |
56 | 56 | */ |
57 | 57 | PageData<DashboardInfo> findDashboardsByTenantIdAndEdgeId(UUID tenantId, UUID edgeId, PageLink pageLink); |
58 | 58 | |
59 | + DashboardInfo findFirstByTenantIdAndName(UUID tenantId, String name); | |
60 | + | |
59 | 61 | } | ... | ... |
... | ... | @@ -34,7 +34,6 @@ import org.thingsboard.server.common.data.id.EdgeId; |
34 | 34 | import org.thingsboard.server.common.data.id.TenantId; |
35 | 35 | import org.thingsboard.server.common.data.page.PageData; |
36 | 36 | import org.thingsboard.server.common.data.page.PageLink; |
37 | -import org.thingsboard.server.common.data.page.TimePageLink; | |
38 | 37 | import org.thingsboard.server.common.data.relation.EntityRelation; |
39 | 38 | import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
40 | 39 | import org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration; |
... | ... | @@ -269,6 +268,11 @@ public class DashboardServiceImpl extends AbstractEntityService implements Dashb |
269 | 268 | return dashboardInfoDao.findDashboardsByTenantIdAndEdgeId(tenantId.getId(), edgeId.getId(), pageLink); |
270 | 269 | } |
271 | 270 | |
271 | + @Override | |
272 | + public DashboardInfo findFirstDashboardInfoByTenantIdAndName(TenantId tenantId, String name) { | |
273 | + return dashboardInfoDao.findFirstByTenantIdAndName(tenantId.getId(), name); | |
274 | + } | |
275 | + | |
272 | 276 | private DataValidator<Dashboard> dashboardValidator = |
273 | 277 | new DataValidator<Dashboard>() { |
274 | 278 | @Override | ... | ... |
... | ... | @@ -27,6 +27,7 @@ import org.springframework.cache.Cache; |
27 | 27 | import org.springframework.cache.CacheManager; |
28 | 28 | import org.springframework.cache.annotation.CacheEvict; |
29 | 29 | import org.springframework.cache.annotation.Cacheable; |
30 | +import org.springframework.cache.annotation.Caching; | |
30 | 31 | import org.springframework.context.annotation.Lazy; |
31 | 32 | import org.springframework.stereotype.Service; |
32 | 33 | import org.springframework.transaction.annotation.Transactional; |
... | ... | @@ -147,6 +148,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
147 | 148 | return deviceDao.findDeviceInfoById(tenantId, deviceId.getId()); |
148 | 149 | } |
149 | 150 | |
151 | + @Cacheable(cacheNames = DEVICE_CACHE, key = "{#tenantId, #deviceId}") | |
150 | 152 | @Override |
151 | 153 | public Device findDeviceById(TenantId tenantId, DeviceId deviceId) { |
152 | 154 | log.trace("Executing findDeviceById [{}]", deviceId); |
... | ... | @@ -178,13 +180,19 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
178 | 180 | return deviceOpt.orElse(null); |
179 | 181 | } |
180 | 182 | |
181 | - @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}") | |
183 | + @Caching(evict= { | |
184 | + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), | |
185 | + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") | |
186 | + }) | |
182 | 187 | @Override |
183 | 188 | public Device saveDeviceWithAccessToken(Device device, String accessToken) { |
184 | 189 | return doSaveDevice(device, accessToken); |
185 | 190 | } |
186 | 191 | |
187 | - @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}") | |
192 | + @Caching(evict= { | |
193 | + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), | |
194 | + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") | |
195 | + }) | |
188 | 196 | @Override |
189 | 197 | public Device saveDevice(Device device) { |
190 | 198 | return doSaveDevice(device, null); |
... | ... | @@ -250,6 +258,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
250 | 258 | if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("device_name_unq_key")) { |
251 | 259 | // remove device from cache in case null value cached in the distributed redis. |
252 | 260 | removeDeviceFromCacheByName(device.getTenantId(), device.getName()); |
261 | + removeDeviceFromCacheById(device.getTenantId(), device.getId()); | |
253 | 262 | throw new DataValidationException("Device with such name already exists!"); |
254 | 263 | } else { |
255 | 264 | throw t; |
... | ... | @@ -327,6 +336,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
327 | 336 | deleteEntityRelations(tenantId, deviceId); |
328 | 337 | |
329 | 338 | removeDeviceFromCacheByName(tenantId, device.getName()); |
339 | + removeDeviceFromCacheById(tenantId, device.getId()); | |
330 | 340 | |
331 | 341 | deviceDao.removeById(tenantId, deviceId.getId()); |
332 | 342 | } |
... | ... | @@ -336,6 +346,14 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
336 | 346 | cache.evict(Arrays.asList(tenantId, name)); |
337 | 347 | } |
338 | 348 | |
349 | + private void removeDeviceFromCacheById(TenantId tenantId, DeviceId deviceId) { | |
350 | + if (deviceId == null) { | |
351 | + return; | |
352 | + } | |
353 | + Cache cache = cacheManager.getCache(DEVICE_CACHE); | |
354 | + cache.evict(Arrays.asList(tenantId, deviceId)); | |
355 | + } | |
356 | + | |
339 | 357 | @Override |
340 | 358 | public PageData<Device> findDevicesByTenantId(TenantId tenantId, PageLink pageLink) { |
341 | 359 | log.trace("Executing findDevicesByTenantId, tenantId [{}], pageLink [{}]", tenantId, pageLink); |
... | ... | @@ -521,7 +539,10 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
521 | 539 | } |
522 | 540 | |
523 | 541 | @Transactional |
524 | - @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}") | |
542 | + @Caching(evict= { | |
543 | + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), | |
544 | + @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") | |
545 | + }) | |
525 | 546 | @Override |
526 | 547 | public Device assignDeviceToTenant(TenantId tenantId, Device device) { |
527 | 548 | log.trace("Executing assignDeviceToTenant [{}][{}]", tenantId, device); |
... | ... | @@ -588,6 +609,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe |
588 | 609 | throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name()); |
589 | 610 | } |
590 | 611 | } |
612 | + removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId()); | |
591 | 613 | return savedDevice; |
592 | 614 | } |
593 | 615 | ... | ... |
... | ... | @@ -29,6 +29,8 @@ import java.util.UUID; |
29 | 29 | */ |
30 | 30 | public interface DashboardInfoRepository extends PagingAndSortingRepository<DashboardInfoEntity, UUID> { |
31 | 31 | |
32 | + DashboardInfoEntity findFirstByTenantIdAndTitle(UUID tenantId, String title); | |
33 | + | |
32 | 34 | @Query("SELECT di FROM DashboardInfoEntity di WHERE di.tenantId = :tenantId " + |
33 | 35 | "AND LOWER(di.searchText) LIKE LOWER(CONCAT(:searchText, '%'))") |
34 | 36 | Page<DashboardInfoEntity> findByTenantId(@Param("tenantId") UUID tenantId, | ... | ... |
... | ... | @@ -83,4 +83,9 @@ public class JpaDashboardInfoDao extends JpaAbstractSearchTextDao<DashboardInfoE |
83 | 83 | Objects.toString(pageLink.getTextSearch(), ""), |
84 | 84 | DaoUtil.toPageable(pageLink))); |
85 | 85 | } |
86 | + | |
87 | + @Override | |
88 | + public DashboardInfo findFirstByTenantIdAndName(UUID tenantId, String name) { | |
89 | + return DaoUtil.getData(dashboardInfoRepository.findFirstByTenantIdAndTitle(tenantId, name)); | |
90 | + } | |
86 | 91 | } | ... | ... |
... | ... | @@ -27,7 +27,7 @@ import java.util.UUID; |
27 | 27 | public interface TbResourceInfoRepository extends CrudRepository<TbResourceInfoEntity, UUID> { |
28 | 28 | |
29 | 29 | @Query("SELECT tr FROM TbResourceInfoEntity tr WHERE " + |
30 | - "LOWER(tr.searchText) LIKE LOWER(CONCAT(:searchText, '%'))" + | |
30 | + "LOWER(tr.title) LIKE LOWER(CONCAT('%', :searchText, '%'))" + | |
31 | 31 | "AND (tr.tenantId = :tenantId " + |
32 | 32 | "OR (tr.tenantId = :systemAdminId " + |
33 | 33 | "AND NOT EXISTS " + |
... | ... | @@ -42,7 +42,7 @@ public interface TbResourceInfoRepository extends CrudRepository<TbResourceInfoE |
42 | 42 | |
43 | 43 | @Query("SELECT ri FROM TbResourceInfoEntity ri WHERE " + |
44 | 44 | "ri.tenantId = :tenantId " + |
45 | - "AND LOWER(ri.searchText) LIKE LOWER(CONCAT(:searchText, '%'))") | |
45 | + "AND LOWER(ri.title) LIKE LOWER(CONCAT('%', :searchText, '%'))") | |
46 | 46 | Page<TbResourceInfoEntity> findTenantResourcesByTenantId(@Param("tenantId") UUID tenantId, |
47 | 47 | @Param("searchText") String searchText, |
48 | 48 | Pageable pageable); | ... | ... |
... | ... | @@ -25,7 +25,10 @@ import org.apache.commons.lang3.StringUtils; |
25 | 25 | import org.springframework.beans.factory.annotation.Value; |
26 | 26 | import org.springframework.context.ApplicationEventPublisher; |
27 | 27 | import org.springframework.context.annotation.Lazy; |
28 | +import org.springframework.security.authentication.DisabledException; | |
29 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | |
28 | 30 | import org.springframework.stereotype.Service; |
31 | +import org.thingsboard.common.util.JacksonUtil; | |
29 | 32 | import org.thingsboard.server.common.data.Customer; |
30 | 33 | import org.thingsboard.server.common.data.EntityType; |
31 | 34 | import org.thingsboard.server.common.data.Tenant; |
... | ... | @@ -49,7 +52,6 @@ import org.thingsboard.server.dao.service.DataValidator; |
49 | 52 | import org.thingsboard.server.dao.service.PaginatedRemover; |
50 | 53 | import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
51 | 54 | import org.thingsboard.server.dao.tenant.TenantDao; |
52 | -import org.thingsboard.common.util.JacksonUtil; | |
53 | 55 | |
54 | 56 | import java.util.HashMap; |
55 | 57 | import java.util.Map; |
... | ... | @@ -194,11 +196,11 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic |
194 | 196 | DataValidator.validateEmail(email); |
195 | 197 | User user = userDao.findByEmail(tenantId, email); |
196 | 198 | if (user == null) { |
197 | - throw new IncorrectParameterException(String.format("Unable to find user by email [%s]", email)); | |
199 | + throw new UsernameNotFoundException(String.format("Unable to find user by email [%s]", email)); | |
198 | 200 | } |
199 | 201 | UserCredentials userCredentials = userCredentialsDao.findByUserId(tenantId, user.getUuidId()); |
200 | 202 | if (!userCredentials.isEnabled()) { |
201 | - throw new IncorrectParameterException("Unable to reset password for inactive user"); | |
203 | + throw new DisabledException(String.format("User credentials not enabled [%s]", email)); | |
202 | 204 | } |
203 | 205 | userCredentials.setResetToken(RandomStringUtils.randomAlphanumeric(DEFAULT_TOKEN_LENGTH)); |
204 | 206 | return saveUserCredentials(tenantId, userCredentials); |
... | ... | @@ -365,7 +367,8 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic |
365 | 367 | JsonNode userPasswordHistoryJson; |
366 | 368 | if (additionalInfo.has(USER_PASSWORD_HISTORY)) { |
367 | 369 | userPasswordHistoryJson = additionalInfo.get(USER_PASSWORD_HISTORY); |
368 | - userPasswordHistoryMap = JacksonUtil.convertValue(userPasswordHistoryJson, new TypeReference<>(){}); | |
370 | + userPasswordHistoryMap = JacksonUtil.convertValue(userPasswordHistoryJson, new TypeReference<>() { | |
371 | + }); | |
369 | 372 | } |
370 | 373 | if (userPasswordHistoryMap != null) { |
371 | 374 | userPasswordHistoryMap.put(Long.toString(System.currentTimeMillis()), userCredentials.getPassword()); | ... | ... |
... | ... | @@ -45,3 +45,19 @@ CREATE INDEX IF NOT EXISTS idx_attribute_kv_by_key_and_last_update_ts ON attribu |
45 | 45 | CREATE INDEX IF NOT EXISTS idx_audit_log_tenant_id_and_created_time ON audit_log(tenant_id, created_time); |
46 | 46 | |
47 | 47 | CREATE INDEX IF NOT EXISTS idx_rpc_tenant_id_device_id ON rpc(tenant_id, device_id); |
48 | + | |
49 | +CREATE INDEX IF NOT EXISTS idx_event_ts | |
50 | + ON public.event USING btree | |
51 | + (ts DESC NULLS LAST) | |
52 | + WITH (FILLFACTOR=95); | |
53 | + | |
54 | +COMMENT ON INDEX public.idx_event_ts | |
55 | + IS 'This index helps to delete events by TTL using timestamp'; | |
56 | + | |
57 | +CREATE INDEX IF NOT EXISTS idx_event_tenant_entity_type_entity_event_type_created_time_des | |
58 | + ON public.event USING btree | |
59 | + (tenant_id ASC NULLS LAST, entity_type ASC NULLS LAST, entity_id ASC NULLS LAST, event_type ASC NULLS LAST, created_time DESC NULLS LAST) | |
60 | + WITH (FILLFACTOR=95); | |
61 | + | |
62 | +COMMENT ON INDEX public.idx_event_tenant_entity_type_entity_event_type_created_time_des | |
63 | + IS 'This index helps to open latest events on UI fast'; | |
\ No newline at end of file | ... | ... |
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.dao.attributes; | |
17 | + | |
18 | +import com.google.common.util.concurrent.MoreExecutors; | |
19 | +import org.junit.Test; | |
20 | +import org.thingsboard.server.dao.cache.CacheExecutorService; | |
21 | + | |
22 | +import static org.hamcrest.CoreMatchers.is; | |
23 | +import static org.hamcrest.MatcherAssert.assertThat; | |
24 | +import static org.mockito.ArgumentMatchers.any; | |
25 | +import static org.mockito.BDDMockito.willCallRealMethod; | |
26 | +import static org.mockito.Mockito.mock; | |
27 | + | |
28 | +public class CachedAttributesServiceTest { | |
29 | + | |
30 | + public static final String REDIS = "redis"; | |
31 | + | |
32 | + @Test | |
33 | + public void givenLocalCacheTypeName_whenEquals_thenOK() { | |
34 | + assertThat(CachedAttributesService.LOCAL_CACHE_TYPE, is("caffeine")); | |
35 | + } | |
36 | + | |
37 | + @Test | |
38 | + public void givenCacheType_whenGetExecutor_thenDirectExecutor() { | |
39 | + CachedAttributesService cachedAttributesService = mock(CachedAttributesService.class); | |
40 | + CacheExecutorService cacheExecutorService = mock(CacheExecutorService.class); | |
41 | + willCallRealMethod().given(cachedAttributesService).getExecutor(any(), any()); | |
42 | + | |
43 | + assertThat(cachedAttributesService.getExecutor(null, cacheExecutorService), is(MoreExecutors.directExecutor())); | |
44 | + | |
45 | + assertThat(cachedAttributesService.getExecutor("", cacheExecutorService), is(MoreExecutors.directExecutor())); | |
46 | + | |
47 | + assertThat(cachedAttributesService.getExecutor(CachedAttributesService.LOCAL_CACHE_TYPE, cacheExecutorService), is(MoreExecutors.directExecutor())); | |
48 | + | |
49 | + } | |
50 | + | |
51 | + @Test | |
52 | + public void givenCacheType_whenGetExecutor_thenReturnCacheExecutorService() { | |
53 | + CachedAttributesService cachedAttributesService = mock(CachedAttributesService.class); | |
54 | + CacheExecutorService cacheExecutorService = mock(CacheExecutorService.class); | |
55 | + willCallRealMethod().given(cachedAttributesService).getExecutor(any(String.class), any(CacheExecutorService.class)); | |
56 | + | |
57 | + assertThat(cachedAttributesService.getExecutor(REDIS, cacheExecutorService), is(cacheExecutorService)); | |
58 | + | |
59 | + assertThat(cachedAttributesService.getExecutor("unknownCacheType", cacheExecutorService), is(cacheExecutorService)); | |
60 | + | |
61 | + } | |
62 | + | |
63 | +} | |
\ No newline at end of file | ... | ... |
... | ... | @@ -80,7 +80,7 @@ |
80 | 80 | <lombok.version>1.18.18</lombok.version> |
81 | 81 | <paho.client.version>1.2.4</paho.client.version> |
82 | 82 | <netty.version>4.1.60.Final</netty.version> |
83 | - <os-maven-plugin.version>1.5.0</os-maven-plugin.version> | |
83 | + <os-maven-plugin.version>1.7.0</os-maven-plugin.version> | |
84 | 84 | <rabbitmq.version>4.8.0</rabbitmq.version> |
85 | 85 | <surfire.version>2.19.1</surfire.version> |
86 | 86 | <jar-plugin.version>3.0.2</jar-plugin.version> |
... | ... | @@ -580,7 +580,7 @@ |
580 | 580 | <extension> |
581 | 581 | <groupId>kr.motd.maven</groupId> |
582 | 582 | <artifactId>os-maven-plugin</artifactId> |
583 | - <version>1.5.0.Final</version> | |
583 | + <version>${os-maven-plugin.version}</version> | |
584 | 584 | </extension> |
585 | 585 | </extensions> |
586 | 586 | <pluginManagement> | ... | ... |