Commit 2078c71d32a6e8a2b2e3b691b3a61258cba6ef2d
Committed by
Andrew Shvayka
1 parent
c0717e70
Added module cluster-api and used it in rule engine and other services
Showing
77 changed files
with
394 additions
and
263 deletions
... | ... | @@ -66,6 +66,10 @@ |
66 | 66 | <artifactId>rule-engine-api</artifactId> |
67 | 67 | </dependency> |
68 | 68 | <dependency> |
69 | + <groupId>org.thingsboard.common</groupId> | |
70 | + <artifactId>cluster-api</artifactId> | |
71 | + </dependency> | |
72 | + <dependency> | |
69 | 73 | <groupId>org.thingsboard.rule-engine</groupId> |
70 | 74 | <artifactId>rule-engine-components</artifactId> |
71 | 75 | </dependency> | ... | ... |
... | ... | @@ -81,7 +81,7 @@ import org.thingsboard.server.service.executors.ExternalCallExecutorService; |
81 | 81 | import org.thingsboard.server.service.executors.SharedEventLoopGroupService; |
82 | 82 | import org.thingsboard.server.service.mail.MailExecutorService; |
83 | 83 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
84 | -import org.thingsboard.server.service.queue.TbClusterService; | |
84 | +import org.thingsboard.server.cluster.TbClusterService; | |
85 | 85 | import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService; |
86 | 86 | import org.thingsboard.server.service.rpc.TbRpcService; |
87 | 87 | import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService; | ... | ... |
... | ... | @@ -26,7 +26,7 @@ import lombok.extern.slf4j.Slf4j; |
26 | 26 | import org.apache.commons.collections.CollectionUtils; |
27 | 27 | import org.thingsboard.common.util.JacksonUtil; |
28 | 28 | import org.thingsboard.common.util.LinkedHashMapRemoveEldest; |
29 | -import org.thingsboard.rule.engine.api.RpcError; | |
29 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
30 | 30 | import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg; |
31 | 31 | import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; |
32 | 32 | import org.thingsboard.rule.engine.api.msg.DeviceEdgeUpdateMsg; |
... | ... | @@ -86,7 +86,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg; |
86 | 86 | import org.thingsboard.server.gen.transport.TransportProtos.ToTransportUpdateCredentialsProto; |
87 | 87 | import org.thingsboard.server.gen.transport.TransportProtos.TransportToDeviceActorMsg; |
88 | 88 | import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto; |
89 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
89 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
90 | 90 | import org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg; |
91 | 91 | import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg; |
92 | 92 | import org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper; |
... | ... | @@ -585,7 +585,7 @@ class DeviceActorMessageProcessor extends AbstractContextAwareMsgProcessor { |
585 | 585 | log.debug("[{}] Received duplicate session open event [{}]", deviceId, sessionId); |
586 | 586 | return; |
587 | 587 | } |
588 | - log.info("[{}] Processing new session [{}]. Current sessions size {}", deviceId, sessionId, sessions.size()); | |
588 | + log.debug("[{}] Processing new session [{}]. Current sessions size {}", deviceId, sessionId, sessions.size()); | |
589 | 589 | |
590 | 590 | sessions.put(sessionId, new SessionInfoMetaData(new SessionInfo(SessionType.ASYNC, sessionInfo.getNodeId()))); |
591 | 591 | if (sessions.size() == 1) { | ... | ... |
... | ... | @@ -33,6 +33,7 @@ import org.thingsboard.rule.engine.api.TbRelationTypes; |
33 | 33 | import org.thingsboard.rule.engine.api.sms.SmsSenderFactory; |
34 | 34 | import org.thingsboard.server.actors.ActorSystemContext; |
35 | 35 | import org.thingsboard.server.actors.TbActorRef; |
36 | +import org.thingsboard.server.cluster.TbClusterService; | |
36 | 37 | import org.thingsboard.server.common.data.Customer; |
37 | 38 | import org.thingsboard.server.common.data.DataConstants; |
38 | 39 | import org.thingsboard.server.common.data.Device; |
... | ... | @@ -454,6 +455,11 @@ class DefaultTbContext implements TbContext { |
454 | 455 | } |
455 | 456 | |
456 | 457 | @Override |
458 | + public TbClusterService getClusterService() { | |
459 | + return mainCtx.getClusterService(); | |
460 | + } | |
461 | + | |
462 | + @Override | |
457 | 463 | public DashboardService getDashboardService() { |
458 | 464 | return mainCtx.getDashboardService(); |
459 | 465 | } | ... | ... |
... | ... | @@ -49,7 +49,7 @@ import org.thingsboard.server.queue.TbQueueCallback; |
49 | 49 | import org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper; |
50 | 50 | import org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper; |
51 | 51 | import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
52 | -import org.thingsboard.server.service.queue.TbClusterService; | |
52 | +import org.thingsboard.server.cluster.TbClusterService; | |
53 | 53 | |
54 | 54 | import java.util.ArrayList; |
55 | 55 | import java.util.Collections; | ... | ... |
... | ... | @@ -22,33 +22,22 @@ import org.springframework.beans.factory.annotation.Autowired; |
22 | 22 | import org.springframework.beans.factory.annotation.Value; |
23 | 23 | import org.springframework.http.HttpStatus; |
24 | 24 | import org.springframework.http.ResponseEntity; |
25 | -import org.springframework.security.access.prepost.PreAuthorize; | |
26 | 25 | import org.springframework.util.StringUtils; |
27 | -import org.springframework.web.bind.annotation.PathVariable; | |
28 | -import org.springframework.web.bind.annotation.RequestMapping; | |
29 | -import org.springframework.web.bind.annotation.RequestMethod; | |
30 | -import org.springframework.web.bind.annotation.RequestParam; | |
31 | -import org.springframework.web.bind.annotation.ResponseBody; | |
32 | 26 | import org.springframework.web.context.request.async.DeferredResult; |
33 | 27 | import org.thingsboard.common.util.JacksonUtil; |
34 | -import org.thingsboard.rule.engine.api.RpcError; | |
28 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
35 | 29 | import org.thingsboard.server.common.data.DataConstants; |
36 | 30 | import org.thingsboard.server.common.data.audit.ActionType; |
37 | 31 | import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
38 | 32 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
39 | 33 | import org.thingsboard.server.common.data.id.DeviceId; |
40 | 34 | import org.thingsboard.server.common.data.id.EntityId; |
41 | -import org.thingsboard.server.common.data.id.RpcId; | |
42 | 35 | import org.thingsboard.server.common.data.id.TenantId; |
43 | 36 | import org.thingsboard.server.common.data.id.UUIDBased; |
44 | -import org.thingsboard.server.common.data.page.PageData; | |
45 | -import org.thingsboard.server.common.data.page.PageLink; | |
46 | -import org.thingsboard.server.common.data.rpc.Rpc; | |
47 | -import org.thingsboard.server.common.data.rpc.RpcStatus; | |
48 | 37 | import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody; |
49 | 38 | import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; |
50 | 39 | import org.thingsboard.server.queue.util.TbCoreComponent; |
51 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
40 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
52 | 41 | import org.thingsboard.server.service.rpc.LocalRequestMetaData; |
53 | 42 | import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService; |
54 | 43 | import org.thingsboard.server.service.security.AccessValidator; | ... | ... |
... | ... | @@ -33,7 +33,6 @@ import org.thingsboard.server.common.data.DashboardInfo; |
33 | 33 | import org.thingsboard.server.common.data.Device; |
34 | 34 | import org.thingsboard.server.common.data.DeviceInfo; |
35 | 35 | import org.thingsboard.server.common.data.DeviceProfile; |
36 | -import org.thingsboard.server.common.data.EdgeUtils; | |
37 | 36 | import org.thingsboard.server.common.data.EntityType; |
38 | 37 | import org.thingsboard.server.common.data.EntityView; |
39 | 38 | import org.thingsboard.server.common.data.EntityViewInfo; |
... | ... | @@ -118,7 +117,6 @@ import org.thingsboard.server.dao.user.UserService; |
118 | 117 | import org.thingsboard.server.dao.widget.WidgetTypeService; |
119 | 118 | import org.thingsboard.server.dao.widget.WidgetsBundleService; |
120 | 119 | import org.thingsboard.server.exception.ThingsboardErrorResponseHandler; |
121 | -import org.thingsboard.server.gen.transport.TransportProtos; | |
122 | 120 | import org.thingsboard.server.queue.discovery.PartitionService; |
123 | 121 | import org.thingsboard.server.queue.provider.TbQueueProducerProvider; |
124 | 122 | import org.thingsboard.server.queue.util.TbCoreComponent; |
... | ... | @@ -129,7 +127,7 @@ import org.thingsboard.server.service.edge.rpc.EdgeRpcService; |
129 | 127 | import org.thingsboard.server.service.lwm2m.LwM2MServerSecurityInfoRepository; |
130 | 128 | import org.thingsboard.server.service.ota.OtaPackageStateService; |
131 | 129 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
132 | -import org.thingsboard.server.service.queue.TbClusterService; | |
130 | +import org.thingsboard.server.cluster.TbClusterService; | |
133 | 131 | import org.thingsboard.server.service.resource.TbResourceService; |
134 | 132 | import org.thingsboard.server.service.security.model.SecurityUser; |
135 | 133 | import org.thingsboard.server.service.security.permission.AccessControlService; |
... | ... | @@ -888,42 +886,7 @@ public abstract class BaseController { |
888 | 886 | } |
889 | 887 | |
890 | 888 | private void sendNotificationMsgToEdgeService(TenantId tenantId, EdgeId edgeId, EntityId entityId, String body, EdgeEventType type, EdgeEventActionType action) { |
891 | - if (!edgesEnabled) { | |
892 | - return; | |
893 | - } | |
894 | - if (type == null) { | |
895 | - if (entityId != null) { | |
896 | - type = EdgeUtils.getEdgeEventTypeByEntityType(entityId.getEntityType()); | |
897 | - } else { | |
898 | - log.trace("[{}] entity id and type are null. Ignoring this notification", tenantId); | |
899 | - return; | |
900 | - } | |
901 | - if (type == null) { | |
902 | - log.trace("[{}] edge event type is null. Ignoring this notification [{}]", tenantId, entityId); | |
903 | - return; | |
904 | - } | |
905 | - } | |
906 | - TransportProtos.EdgeNotificationMsgProto.Builder builder = TransportProtos.EdgeNotificationMsgProto.newBuilder(); | |
907 | - builder.setTenantIdMSB(tenantId.getId().getMostSignificantBits()); | |
908 | - builder.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()); | |
909 | - builder.setType(type.name()); | |
910 | - builder.setAction(action.name()); | |
911 | - if (entityId != null) { | |
912 | - builder.setEntityIdMSB(entityId.getId().getMostSignificantBits()); | |
913 | - builder.setEntityIdLSB(entityId.getId().getLeastSignificantBits()); | |
914 | - builder.setEntityType(entityId.getEntityType().name()); | |
915 | - } | |
916 | - if (edgeId != null) { | |
917 | - builder.setEdgeIdMSB(edgeId.getId().getMostSignificantBits()); | |
918 | - builder.setEdgeIdLSB(edgeId.getId().getLeastSignificantBits()); | |
919 | - } | |
920 | - if (body != null) { | |
921 | - builder.setBody(body); | |
922 | - } | |
923 | - TransportProtos.EdgeNotificationMsgProto msg = builder.build(); | |
924 | - log.trace("[{}] sending notification to edge service {}", tenantId.getId(), msg); | |
925 | - tbClusterService.pushMsgToCore(tenantId, entityId != null ? entityId : tenantId, | |
926 | - TransportProtos.ToCoreMsg.newBuilder().setEdgeNotificationMsg(msg).build(), null); | |
889 | + tbClusterService.sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, body, type, action); | |
927 | 890 | } |
928 | 891 | |
929 | 892 | protected List<EdgeId> findRelatedEdgeIds(TenantId tenantId, EntityId entityId) { | ... | ... |
... | ... | @@ -149,7 +149,7 @@ public class CustomerController extends BaseController { |
149 | 149 | ActionType.DELETED, null, strCustomerId); |
150 | 150 | |
151 | 151 | sendDeleteNotificationMsg(getTenantId(), customerId, relatedEdgeIds); |
152 | - tbClusterService.onEntityStateChange(getTenantId(), customerId, ComponentLifecycleEvent.DELETED); | |
152 | + tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), customerId, ComponentLifecycleEvent.DELETED); | |
153 | 153 | } catch (Exception e) { |
154 | 154 | |
155 | 155 | logEntityAction(emptyId(EntityType.CUSTOMER), | ... | ... |
... | ... | @@ -33,7 +33,6 @@ import org.springframework.web.bind.annotation.RestController; |
33 | 33 | import org.springframework.web.context.request.async.DeferredResult; |
34 | 34 | import org.thingsboard.rule.engine.api.msg.DeviceCredentialsUpdateNotificationMsg; |
35 | 35 | import org.thingsboard.rule.engine.api.msg.DeviceEdgeUpdateMsg; |
36 | -import org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg; | |
37 | 36 | import org.thingsboard.server.common.data.ClaimRequest; |
38 | 37 | import org.thingsboard.server.common.data.Customer; |
39 | 38 | import org.thingsboard.server.common.data.DataConstants; |
... | ... | @@ -136,27 +135,12 @@ public class DeviceController extends BaseController { |
136 | 135 | |
137 | 136 | Device savedDevice = checkNotNull(deviceService.saveDeviceWithAccessToken(device, accessToken)); |
138 | 137 | |
139 | - tbClusterService.onDeviceChange(savedDevice, null); | |
140 | - tbClusterService.pushMsgToCore(new DeviceNameOrTypeUpdateMsg(savedDevice.getTenantId(), | |
141 | - savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); | |
142 | - tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); | |
143 | - | |
144 | - if (!created) { | |
145 | - sendEntityNotificationMsg(savedDevice.getTenantId(), savedDevice.getId(), EdgeEventActionType.UPDATED); | |
146 | - } | |
138 | + tbClusterService.onDeviceUpdated(savedDevice, oldDevice); | |
147 | 139 | |
148 | 140 | logEntityAction(savedDevice.getId(), savedDevice, |
149 | 141 | savedDevice.getCustomerId(), |
150 | 142 | created ? ActionType.ADDED : ActionType.UPDATED, null); |
151 | 143 | |
152 | - if (device.getId() == null) { | |
153 | - deviceStateService.onDeviceAdded(savedDevice); | |
154 | - } else { | |
155 | - deviceStateService.onDeviceUpdated(savedDevice); | |
156 | - } | |
157 | - | |
158 | - otaPackageStateService.update(savedDevice, oldDevice); | |
159 | - | |
160 | 144 | return savedDevice; |
161 | 145 | } catch (Exception e) { |
162 | 146 | logEntityAction(emptyId(EntityType.DEVICE), device, |
... | ... | @@ -180,15 +164,12 @@ public class DeviceController extends BaseController { |
180 | 164 | deviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId); |
181 | 165 | |
182 | 166 | tbClusterService.onDeviceDeleted(device, null); |
183 | - tbClusterService.onEntityStateChange(device.getTenantId(), deviceId, ComponentLifecycleEvent.DELETED); | |
184 | 167 | |
185 | 168 | logEntityAction(deviceId, device, |
186 | 169 | device.getCustomerId(), |
187 | 170 | ActionType.DELETED, null, strDeviceId); |
188 | 171 | |
189 | 172 | sendDeleteNotificationMsg(getTenantId(), deviceId, relatedEdgeIds); |
190 | - | |
191 | - deviceStateService.onDeviceDeleted(device); | |
192 | 173 | } catch (Exception e) { |
193 | 174 | logEntityAction(emptyId(EntityType.DEVICE), |
194 | 175 | null, | ... | ... |
... | ... | @@ -161,7 +161,7 @@ public class DeviceProfileController extends BaseController { |
161 | 161 | DeviceProfile savedDeviceProfile = checkNotNull(deviceProfileService.saveDeviceProfile(deviceProfile)); |
162 | 162 | |
163 | 163 | tbClusterService.onDeviceProfileChange(savedDeviceProfile, null); |
164 | - tbClusterService.onEntityStateChange(deviceProfile.getTenantId(), savedDeviceProfile.getId(), | |
164 | + tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), savedDeviceProfile.getId(), | |
165 | 165 | created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
166 | 166 | |
167 | 167 | logEntityAction(savedDeviceProfile.getId(), savedDeviceProfile, |
... | ... | @@ -191,7 +191,7 @@ public class DeviceProfileController extends BaseController { |
191 | 191 | deviceProfileService.deleteDeviceProfile(getTenantId(), deviceProfileId); |
192 | 192 | |
193 | 193 | tbClusterService.onDeviceProfileDelete(deviceProfile, null); |
194 | - tbClusterService.onEntityStateChange(deviceProfile.getTenantId(), deviceProfile.getId(), ComponentLifecycleEvent.DELETED); | |
194 | + tbClusterService.broadcastEntityStateChangeEvent(deviceProfile.getTenantId(), deviceProfile.getId(), ComponentLifecycleEvent.DELETED); | |
195 | 195 | |
196 | 196 | logEntityAction(deviceProfileId, deviceProfile, |
197 | 197 | null, | ... | ... |
... | ... | @@ -135,7 +135,7 @@ public class EdgeController extends BaseController { |
135 | 135 | edgeService.assignDefaultRuleChainsToEdge(tenantId, savedEdge.getId()); |
136 | 136 | } |
137 | 137 | |
138 | - tbClusterService.onEntityStateChange(savedEdge.getTenantId(), savedEdge.getId(), | |
138 | + tbClusterService.broadcastEntityStateChangeEvent(savedEdge.getTenantId(), savedEdge.getId(), | |
139 | 139 | created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
140 | 140 | |
141 | 141 | logEntityAction(savedEdge.getId(), savedEdge, null, created ? ActionType.ADDED : ActionType.UPDATED, null); |
... | ... | @@ -157,7 +157,7 @@ public class EdgeController extends BaseController { |
157 | 157 | Edge edge = checkEdgeId(edgeId, Operation.DELETE); |
158 | 158 | edgeService.deleteEdge(getTenantId(), edgeId); |
159 | 159 | |
160 | - tbClusterService.onEntityStateChange(getTenantId(), edgeId, | |
160 | + tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, | |
161 | 161 | ComponentLifecycleEvent.DELETED); |
162 | 162 | |
163 | 163 | logEntityAction(edgeId, edge, |
... | ... | @@ -208,7 +208,7 @@ public class EdgeController extends BaseController { |
208 | 208 | |
209 | 209 | Edge savedEdge = checkNotNull(edgeService.assignEdgeToCustomer(getCurrentUser().getTenantId(), edgeId, customerId)); |
210 | 210 | |
211 | - tbClusterService.onEntityStateChange(getTenantId(), edgeId, | |
211 | + tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, | |
212 | 212 | ComponentLifecycleEvent.UPDATED); |
213 | 213 | |
214 | 214 | logEntityAction(edgeId, savedEdge, |
... | ... | @@ -242,7 +242,7 @@ public class EdgeController extends BaseController { |
242 | 242 | |
243 | 243 | Edge savedEdge = checkNotNull(edgeService.unassignEdgeFromCustomer(getCurrentUser().getTenantId(), edgeId)); |
244 | 244 | |
245 | - tbClusterService.onEntityStateChange(getTenantId(), edgeId, | |
245 | + tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, | |
246 | 246 | ComponentLifecycleEvent.UPDATED); |
247 | 247 | |
248 | 248 | logEntityAction(edgeId, edge, |
... | ... | @@ -272,7 +272,7 @@ public class EdgeController extends BaseController { |
272 | 272 | Customer publicCustomer = customerService.findOrCreatePublicCustomer(edge.getTenantId()); |
273 | 273 | Edge savedEdge = checkNotNull(edgeService.assignEdgeToCustomer(getCurrentUser().getTenantId(), edgeId, publicCustomer.getId())); |
274 | 274 | |
275 | - tbClusterService.onEntityStateChange(getTenantId(), edgeId, | |
275 | + tbClusterService.broadcastEntityStateChangeEvent(getTenantId(), edgeId, | |
276 | 276 | ComponentLifecycleEvent.UPDATED); |
277 | 277 | |
278 | 278 | logEntityAction(edgeId, savedEdge, |
... | ... | @@ -364,7 +364,7 @@ public class EdgeController extends BaseController { |
364 | 364 | |
365 | 365 | Edge updatedEdge = edgeNotificationService.setEdgeRootRuleChain(getTenantId(), edge, ruleChainId); |
366 | 366 | |
367 | - tbClusterService.onEntityStateChange(updatedEdge.getTenantId(), updatedEdge.getId(), ComponentLifecycleEvent.UPDATED); | |
367 | + tbClusterService.broadcastEntityStateChangeEvent(updatedEdge.getTenantId(), updatedEdge.getId(), ComponentLifecycleEvent.UPDATED); | |
368 | 368 | |
369 | 369 | logEntityAction(updatedEdge.getId(), updatedEdge, null, ActionType.UPDATED, null); |
370 | 370 | ... | ... |
... | ... | @@ -24,13 +24,11 @@ import org.springframework.web.bind.annotation.RequestMapping; |
24 | 24 | import org.springframework.web.bind.annotation.RequestMethod; |
25 | 25 | import org.springframework.web.bind.annotation.ResponseBody; |
26 | 26 | import org.springframework.web.bind.annotation.RestController; |
27 | -import org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg; | |
28 | 27 | import org.thingsboard.server.common.data.Device; |
29 | 28 | import org.thingsboard.server.common.data.EntityType; |
30 | 29 | import org.thingsboard.server.common.data.audit.ActionType; |
31 | 30 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
32 | 31 | import org.thingsboard.server.common.data.lwm2m.ServerSecurityConfig; |
33 | -import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; | |
34 | 32 | import org.thingsboard.server.common.data.security.DeviceCredentials; |
35 | 33 | import org.thingsboard.server.queue.util.TbCoreComponent; |
36 | 34 | import org.thingsboard.server.service.security.permission.Resource; |
... | ... | @@ -66,22 +64,11 @@ public class Lwm2mController extends BaseController { |
66 | 64 | checkEntity(device.getId(), device, Resource.DEVICE); |
67 | 65 | Device savedDevice = deviceService.saveDeviceWithCredentials(device, credentials); |
68 | 66 | checkNotNull(savedDevice); |
69 | - | |
70 | - tbClusterService.onDeviceChange(savedDevice, null); | |
71 | - tbClusterService.pushMsgToCore(new DeviceNameOrTypeUpdateMsg(savedDevice.getTenantId(), | |
72 | - savedDevice.getId(), savedDevice.getName(), savedDevice.getType()), null); | |
73 | - tbClusterService.onEntityStateChange(savedDevice.getTenantId(), savedDevice.getId(), | |
74 | - device.getId() == null ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); | |
75 | - | |
67 | + tbClusterService.onDeviceUpdated(savedDevice, device); | |
76 | 68 | logEntityAction(savedDevice.getId(), savedDevice, |
77 | 69 | savedDevice.getCustomerId(), |
78 | 70 | device.getId() == null ? ActionType.ADDED : ActionType.UPDATED, null); |
79 | 71 | |
80 | - if (device.getId() == null) { | |
81 | - deviceStateService.onDeviceAdded(savedDevice); | |
82 | - } else { | |
83 | - deviceStateService.onDeviceUpdated(savedDevice); | |
84 | - } | |
85 | 72 | return savedDevice; |
86 | 73 | } catch (Exception e) { |
87 | 74 | logEntityAction(emptyId(EntityType.DEVICE), device, | ... | ... |
... | ... | @@ -149,7 +149,7 @@ public class RuleChainController extends BaseController { |
149 | 149 | RuleChain savedRuleChain = checkNotNull(ruleChainService.saveRuleChain(ruleChain)); |
150 | 150 | |
151 | 151 | if (RuleChainType.CORE.equals(savedRuleChain.getType())) { |
152 | - tbClusterService.onEntityStateChange(ruleChain.getTenantId(), savedRuleChain.getId(), | |
152 | + tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), savedRuleChain.getId(), | |
153 | 153 | created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
154 | 154 | } |
155 | 155 | |
... | ... | @@ -183,7 +183,7 @@ public class RuleChainController extends BaseController { |
183 | 183 | |
184 | 184 | RuleChain savedRuleChain = installScripts.createDefaultRuleChain(getCurrentUser().getTenantId(), request.getName()); |
185 | 185 | |
186 | - tbClusterService.onEntityStateChange(savedRuleChain.getTenantId(), savedRuleChain.getId(), ComponentLifecycleEvent.CREATED); | |
186 | + tbClusterService.broadcastEntityStateChangeEvent(savedRuleChain.getTenantId(), savedRuleChain.getId(), ComponentLifecycleEvent.CREATED); | |
187 | 187 | |
188 | 188 | logEntityAction(savedRuleChain.getId(), savedRuleChain, null, ActionType.ADDED, null); |
189 | 189 | |
... | ... | @@ -210,7 +210,7 @@ public class RuleChainController extends BaseController { |
210 | 210 | if (previousRootRuleChain != null) { |
211 | 211 | previousRootRuleChain = ruleChainService.findRuleChainById(getTenantId(), previousRootRuleChain.getId()); |
212 | 212 | |
213 | - tbClusterService.onEntityStateChange(previousRootRuleChain.getTenantId(), previousRootRuleChain.getId(), | |
213 | + tbClusterService.broadcastEntityStateChangeEvent(previousRootRuleChain.getTenantId(), previousRootRuleChain.getId(), | |
214 | 214 | ComponentLifecycleEvent.UPDATED); |
215 | 215 | |
216 | 216 | logEntityAction(previousRootRuleChain.getId(), previousRootRuleChain, |
... | ... | @@ -218,7 +218,7 @@ public class RuleChainController extends BaseController { |
218 | 218 | } |
219 | 219 | ruleChain = ruleChainService.findRuleChainById(getTenantId(), ruleChainId); |
220 | 220 | |
221 | - tbClusterService.onEntityStateChange(ruleChain.getTenantId(), ruleChain.getId(), | |
221 | + tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), | |
222 | 222 | ComponentLifecycleEvent.UPDATED); |
223 | 223 | |
224 | 224 | logEntityAction(ruleChain.getId(), ruleChain, |
... | ... | @@ -254,7 +254,7 @@ public class RuleChainController extends BaseController { |
254 | 254 | RuleChainMetaData savedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId())); |
255 | 255 | |
256 | 256 | if (RuleChainType.CORE.equals(ruleChain.getType())) { |
257 | - tbClusterService.onEntityStateChange(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED); | |
257 | + tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED); | |
258 | 258 | } |
259 | 259 | |
260 | 260 | logEntityAction(ruleChain.getId(), ruleChain, |
... | ... | @@ -323,9 +323,9 @@ public class RuleChainController extends BaseController { |
323 | 323 | |
324 | 324 | if (RuleChainType.CORE.equals(ruleChain.getType())) { |
325 | 325 | referencingRuleChainIds.forEach(referencingRuleChainId -> |
326 | - tbClusterService.onEntityStateChange(ruleChain.getTenantId(), referencingRuleChainId, ComponentLifecycleEvent.UPDATED)); | |
326 | + tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), referencingRuleChainId, ComponentLifecycleEvent.UPDATED)); | |
327 | 327 | |
328 | - tbClusterService.onEntityStateChange(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.DELETED); | |
328 | + tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.DELETED); | |
329 | 329 | } |
330 | 330 | |
331 | 331 | logEntityAction(ruleChainId, ruleChain, |
... | ... | @@ -456,7 +456,7 @@ public class RuleChainController extends BaseController { |
456 | 456 | List<RuleChainImportResult> importResults = ruleChainService.importTenantRuleChains(tenantId, ruleChainData, RuleChainType.CORE, overwrite); |
457 | 457 | if (!CollectionUtils.isEmpty(importResults)) { |
458 | 458 | for (RuleChainImportResult importResult : importResults) { |
459 | - tbClusterService.onEntityStateChange(importResult.getTenantId(), importResult.getRuleChainId(), importResult.getLifecycleEvent()); | |
459 | + tbClusterService.broadcastEntityStateChangeEvent(importResult.getTenantId(), importResult.getRuleChainId(), importResult.getLifecycleEvent()); | |
460 | 460 | } |
461 | 461 | } |
462 | 462 | } catch (Exception e) { | ... | ... |
... | ... | @@ -99,7 +99,7 @@ public class TenantController extends BaseController { |
99 | 99 | } |
100 | 100 | tenantProfileCache.evict(tenant.getId()); |
101 | 101 | tbClusterService.onTenantChange(tenant, null); |
102 | - tbClusterService.onEntityStateChange(tenant.getId(), tenant.getId(), | |
102 | + tbClusterService.broadcastEntityStateChangeEvent(tenant.getId(), tenant.getId(), | |
103 | 103 | newTenant ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
104 | 104 | return tenant; |
105 | 105 | } catch (Exception e) { |
... | ... | @@ -118,7 +118,7 @@ public class TenantController extends BaseController { |
118 | 118 | tenantService.deleteTenant(tenantId); |
119 | 119 | tenantProfileCache.evict(tenantId); |
120 | 120 | tbClusterService.onTenantDelete(tenant, null); |
121 | - tbClusterService.onEntityStateChange(tenantId, tenantId, ComponentLifecycleEvent.DELETED); | |
121 | + tbClusterService.broadcastEntityStateChangeEvent(tenantId, tenantId, ComponentLifecycleEvent.DELETED); | |
122 | 122 | } catch (Exception e) { |
123 | 123 | throw handleException(e); |
124 | 124 | } | ... | ... |
... | ... | @@ -34,7 +34,6 @@ import org.thingsboard.server.common.data.id.TenantProfileId; |
34 | 34 | import org.thingsboard.server.common.data.page.PageData; |
35 | 35 | import org.thingsboard.server.common.data.page.PageLink; |
36 | 36 | import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
37 | -import org.thingsboard.server.dao.exception.DataValidationException; | |
38 | 37 | import org.thingsboard.server.queue.util.TbCoreComponent; |
39 | 38 | import org.thingsboard.server.service.security.permission.Operation; |
40 | 39 | import org.thingsboard.server.service.security.permission.Resource; |
... | ... | @@ -98,7 +97,7 @@ public class TenantProfileController extends BaseController { |
98 | 97 | tenantProfile = checkNotNull(tenantProfileService.saveTenantProfile(getTenantId(), tenantProfile)); |
99 | 98 | tenantProfileCache.put(tenantProfile); |
100 | 99 | tbClusterService.onTenantProfileChange(tenantProfile, null); |
101 | - tbClusterService.onEntityStateChange(TenantId.SYS_TENANT_ID, tenantProfile.getId(), | |
100 | + tbClusterService.broadcastEntityStateChangeEvent(TenantId.SYS_TENANT_ID, tenantProfile.getId(), | |
102 | 101 | newTenantProfile ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); |
103 | 102 | return tenantProfile; |
104 | 103 | } catch (Exception e) { | ... | ... |
... | ... | @@ -39,7 +39,7 @@ import org.thingsboard.server.common.msg.TbMsg; |
39 | 39 | import org.thingsboard.server.common.msg.TbMsgDataType; |
40 | 40 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
41 | 41 | import org.thingsboard.server.queue.util.TbCoreComponent; |
42 | -import org.thingsboard.server.service.queue.TbClusterService; | |
42 | +import org.thingsboard.server.cluster.TbClusterService; | |
43 | 43 | |
44 | 44 | import java.util.List; |
45 | 45 | import java.util.Map; | ... | ... |
application/src/main/java/org/thingsboard/server/service/apiusage/DefaultTbApiUsageStateService.java
... | ... | @@ -61,7 +61,7 @@ import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; |
61 | 61 | import org.thingsboard.server.queue.discovery.PartitionService; |
62 | 62 | import org.thingsboard.server.queue.discovery.TbApplicationEventListener; |
63 | 63 | import org.thingsboard.server.queue.scheduler.SchedulerComponent; |
64 | -import org.thingsboard.server.service.queue.TbClusterService; | |
64 | +import org.thingsboard.server.cluster.TbClusterService; | |
65 | 65 | import org.thingsboard.server.service.telemetry.InternalTelemetryService; |
66 | 66 | |
67 | 67 | import javax.annotation.PostConstruct; | ... | ... |
... | ... | @@ -29,6 +29,7 @@ import org.springframework.cache.CacheManager; |
29 | 29 | import org.springframework.stereotype.Service; |
30 | 30 | import org.springframework.util.StringUtils; |
31 | 31 | import org.thingsboard.rule.engine.api.RuleEngineTelemetryService; |
32 | +import org.thingsboard.server.cluster.TbClusterService; | |
32 | 33 | import org.thingsboard.server.common.data.Customer; |
33 | 34 | import org.thingsboard.server.common.data.DataConstants; |
34 | 35 | import org.thingsboard.server.common.data.Device; |
... | ... | @@ -70,6 +71,8 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
70 | 71 | private static final ObjectMapper mapper = new ObjectMapper(); |
71 | 72 | |
72 | 73 | @Autowired |
74 | + private TbClusterService clusterService; | |
75 | + @Autowired | |
73 | 76 | private DeviceService deviceService; |
74 | 77 | @Autowired |
75 | 78 | private AttributesService attributesService; |
... | ... | @@ -155,6 +158,7 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
155 | 158 | if (device.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) { |
156 | 159 | device.setCustomerId(customerId); |
157 | 160 | Device savedDevice = deviceService.saveDevice(device); |
161 | + clusterService.onDeviceUpdated(savedDevice, device); | |
158 | 162 | return Futures.transform(removeClaimingSavedData(cache, claimData, device), result -> new ClaimResult(savedDevice, ClaimResponse.SUCCESS), MoreExecutors.directExecutor()); |
159 | 163 | } |
160 | 164 | return Futures.transform(removeClaimingSavedData(cache, claimData, device), result -> new ClaimResult(null, ClaimResponse.CLAIMED), MoreExecutors.directExecutor()); |
... | ... | @@ -179,13 +183,14 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
179 | 183 | cacheEviction(device.getId()); |
180 | 184 | Customer unassignedCustomer = customerService.findCustomerById(tenantId, device.getCustomerId()); |
181 | 185 | device.setCustomerId(null); |
182 | - deviceService.saveDevice(device); | |
186 | + Device savedDevice = deviceService.saveDevice(device); | |
187 | + clusterService.onDeviceUpdated(savedDevice, device); | |
183 | 188 | if (isAllowedClaimingByDefault) { |
184 | 189 | return Futures.immediateFuture(new ReclaimResult(unassignedCustomer)); |
185 | 190 | } |
186 | 191 | SettableFuture<ReclaimResult> result = SettableFuture.create(); |
187 | 192 | telemetryService.saveAndNotify( |
188 | - tenantId, device.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( | |
193 | + tenantId, savedDevice.getId(), DataConstants.SERVER_SCOPE, Collections.singletonList( | |
189 | 194 | new BaseAttributeKvEntry(new BooleanDataEntry(CLAIM_ATTRIBUTE_NAME, true), System.currentTimeMillis()) |
190 | 195 | ), |
191 | 196 | new FutureCallback<>() { |
... | ... | @@ -198,7 +203,7 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
198 | 203 | public void onFailure(Throwable t) { |
199 | 204 | result.setException(t); |
200 | 205 | } |
201 | - }); | |
206 | + }); | |
202 | 207 | return result; |
203 | 208 | } |
204 | 209 | cacheEviction(device.getId()); |
... | ... | @@ -238,7 +243,7 @@ public class ClaimDevicesServiceImpl implements ClaimDevicesService { |
238 | 243 | public void onFailure(Throwable t) { |
239 | 244 | result.setException(t); |
240 | 245 | } |
241 | - }); | |
246 | + }); | |
242 | 247 | return result; |
243 | 248 | } |
244 | 249 | ... | ... |
... | ... | @@ -24,6 +24,7 @@ import org.apache.commons.lang3.RandomStringUtils; |
24 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 25 | import org.springframework.stereotype.Service; |
26 | 26 | import org.springframework.util.StringUtils; |
27 | +import org.thingsboard.server.cluster.TbClusterService; | |
27 | 28 | import org.thingsboard.server.common.data.DataConstants; |
28 | 29 | import org.thingsboard.server.common.data.Device; |
29 | 30 | import org.thingsboard.server.common.data.DeviceProfile; |
... | ... | @@ -78,6 +79,9 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
78 | 79 | private static final String PROVISIONED_STATE = "provisioned"; |
79 | 80 | |
80 | 81 | @Autowired |
82 | + TbClusterService clusterService; | |
83 | + | |
84 | + @Autowired | |
81 | 85 | DeviceDao deviceDao; |
82 | 86 | |
83 | 87 | @Autowired |
... | ... | @@ -190,8 +194,7 @@ public class DeviceProvisionServiceImpl implements DeviceProvisionService { |
190 | 194 | provisionRequest.setDeviceName(newDeviceName); |
191 | 195 | } |
192 | 196 | Device savedDevice = deviceService.saveDevice(provisionRequest, profile); |
193 | - | |
194 | - deviceStateService.onDeviceAdded(savedDevice); | |
197 | + clusterService.onDeviceUpdated(savedDevice, null); | |
195 | 198 | saveProvisionStateAttribute(savedDevice).get(); |
196 | 199 | pushDeviceCreatedEventToRuleEngine(savedDevice); |
197 | 200 | notify(savedDevice, provisionRequest, DataConstants.PROVISION_SUCCESS, true); | ... | ... |
... | ... | @@ -37,7 +37,7 @@ import org.thingsboard.server.service.edge.rpc.processor.CustomerEdgeProcessor; |
37 | 37 | import org.thingsboard.server.service.edge.rpc.processor.EdgeProcessor; |
38 | 38 | import org.thingsboard.server.service.edge.rpc.processor.EntityEdgeProcessor; |
39 | 39 | import org.thingsboard.server.service.edge.rpc.processor.RelationEdgeProcessor; |
40 | -import org.thingsboard.server.service.queue.TbClusterService; | |
40 | +import org.thingsboard.server.cluster.TbClusterService; | |
41 | 41 | |
42 | 42 | import javax.annotation.PostConstruct; |
43 | 43 | import javax.annotation.PreDestroy; | ... | ... |
... | ... | @@ -62,7 +62,7 @@ import org.thingsboard.server.service.edge.rpc.constructor.WidgetTypeMsgConstruc |
62 | 62 | import org.thingsboard.server.service.edge.rpc.constructor.WidgetsBundleMsgConstructor; |
63 | 63 | import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
64 | 64 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
65 | -import org.thingsboard.server.service.queue.TbClusterService; | |
65 | +import org.thingsboard.server.cluster.TbClusterService; | |
66 | 66 | import org.thingsboard.server.service.state.DeviceStateService; |
67 | 67 | |
68 | 68 | @Slf4j | ... | ... |
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/DeviceEdgeProcessor.java
... | ... | @@ -18,7 +18,6 @@ package org.thingsboard.server.service.edge.rpc.processor; |
18 | 18 | import com.datastax.oss.driver.api.core.uuid.Uuids; |
19 | 19 | import com.fasterxml.jackson.core.JsonProcessingException; |
20 | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
21 | -import com.google.common.util.concurrent.FutureCallback; | |
22 | 21 | import com.google.common.util.concurrent.Futures; |
23 | 22 | import com.google.common.util.concurrent.ListenableFuture; |
24 | 23 | import com.google.common.util.concurrent.SettableFuture; |
... | ... | @@ -27,7 +26,7 @@ import org.apache.commons.lang3.RandomStringUtils; |
27 | 26 | import org.apache.commons.lang3.StringUtils; |
28 | 27 | import org.springframework.stereotype.Component; |
29 | 28 | import org.thingsboard.common.util.JacksonUtil; |
30 | -import org.thingsboard.rule.engine.api.RpcError; | |
29 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
31 | 30 | import org.thingsboard.server.common.data.Customer; |
32 | 31 | import org.thingsboard.server.common.data.DataConstants; |
33 | 32 | import org.thingsboard.server.common.data.Device; |
... | ... | @@ -60,7 +59,7 @@ import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
60 | 59 | import org.thingsboard.server.queue.TbQueueCallback; |
61 | 60 | import org.thingsboard.server.queue.TbQueueMsgMetadata; |
62 | 61 | import org.thingsboard.server.queue.util.TbCoreComponent; |
63 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
62 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
64 | 63 | import org.thingsboard.server.service.rpc.FromDeviceRpcResponseActorMsg; |
65 | 64 | |
66 | 65 | import java.util.UUID; |
... | ... | @@ -176,7 +175,8 @@ public class DeviceEdgeProcessor extends BaseEdgeProcessor { |
176 | 175 | deviceUpdateMsg.getDeviceProfileIdLSB().getValue())); |
177 | 176 | device.setDeviceProfileId(deviceProfileId); |
178 | 177 | } |
179 | - deviceService.saveDevice(device); | |
178 | + Device savedDevice = deviceService.saveDevice(device); | |
179 | + tbClusterService.onDeviceUpdated(savedDevice, device); | |
180 | 180 | saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, EdgeEventActionType.CREDENTIALS_REQUEST, deviceId, null); |
181 | 181 | } else { |
182 | 182 | log.warn("[{}] can't find device [{}], edge [{}]", tenantId, deviceUpdateMsg, edge.getId()); |
... | ... | @@ -215,14 +215,13 @@ public class DeviceEdgeProcessor extends BaseEdgeProcessor { |
215 | 215 | device.setDeviceProfileId(deviceProfileId); |
216 | 216 | } |
217 | 217 | Device savedDevice = deviceService.saveDevice(device, false); |
218 | + tbClusterService.onDeviceUpdated(savedDevice, device); | |
218 | 219 | if (created) { |
219 | 220 | DeviceCredentials deviceCredentials = new DeviceCredentials(); |
220 | 221 | deviceCredentials.setDeviceId(new DeviceId(savedDevice.getUuidId())); |
221 | 222 | deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN); |
222 | 223 | deviceCredentials.setCredentialsId(org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(20)); |
223 | 224 | deviceCredentialsService.createDeviceCredentials(device.getTenantId(), deviceCredentials); |
224 | - | |
225 | - deviceStateService.onDeviceAdded(savedDevice); | |
226 | 225 | } |
227 | 226 | createRelationFromEdge(tenantId, edge.getId(), device.getId()); |
228 | 227 | pushDeviceCreatedEventToRuleEngine(tenantId, edge, device); | ... | ... |
... | ... | @@ -73,7 +73,7 @@ import org.thingsboard.server.gen.edge.v1.UserCredentialsRequestMsg; |
73 | 73 | import org.thingsboard.server.gen.edge.v1.WidgetBundleTypesRequestMsg; |
74 | 74 | import org.thingsboard.server.service.edge.rpc.EdgeEventUtils; |
75 | 75 | import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
76 | -import org.thingsboard.server.service.queue.TbClusterService; | |
76 | +import org.thingsboard.server.cluster.TbClusterService; | |
77 | 77 | |
78 | 78 | import java.util.ArrayList; |
79 | 79 | import java.util.HashMap; | ... | ... |
application/src/main/java/org/thingsboard/server/service/install/DefaultSystemDataLoaderService.java
... | ... | @@ -481,6 +481,7 @@ public class DefaultSystemDataLoaderService implements SystemDataLoaderService { |
481 | 481 | device.setAdditionalInfo(additionalInfo); |
482 | 482 | } |
483 | 483 | device = deviceService.saveDevice(device); |
484 | + //TODO: No access to cluster service, so we should manually update the status of device. | |
484 | 485 | DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(TenantId.SYS_TENANT_ID, device.getId()); |
485 | 486 | deviceCredentials.setCredentialsId(accessToken); |
486 | 487 | deviceCredentialsService.updateDeviceCredentials(TenantId.SYS_TENANT_ID, deviceCredentials); | ... | ... |
... | ... | @@ -17,6 +17,7 @@ package org.thingsboard.server.service.ota; |
17 | 17 | |
18 | 18 | import com.google.common.util.concurrent.FutureCallback; |
19 | 19 | import lombok.extern.slf4j.Slf4j; |
20 | +import org.springframework.context.annotation.Lazy; | |
20 | 21 | import org.springframework.stereotype.Service; |
21 | 22 | import org.thingsboard.rule.engine.api.RuleEngineTelemetryService; |
22 | 23 | import org.thingsboard.rule.engine.api.msg.DeviceAttributesEventNotificationMsg; |
... | ... | @@ -49,7 +50,7 @@ import org.thingsboard.server.queue.TbQueueProducer; |
49 | 50 | import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
50 | 51 | import org.thingsboard.server.queue.provider.TbCoreQueueFactory; |
51 | 52 | import org.thingsboard.server.queue.util.TbCoreComponent; |
52 | -import org.thingsboard.server.service.queue.TbClusterService; | |
53 | +import org.thingsboard.server.cluster.TbClusterService; | |
53 | 54 | |
54 | 55 | import javax.annotation.Nullable; |
55 | 56 | import java.util.ArrayList; |
... | ... | @@ -87,10 +88,11 @@ public class DefaultOtaPackageStateService implements OtaPackageStateService { |
87 | 88 | private final RuleEngineTelemetryService telemetryService; |
88 | 89 | private final TbQueueProducer<TbProtoQueueMsg<ToOtaPackageStateServiceMsg>> otaPackageStateMsgProducer; |
89 | 90 | |
90 | - public DefaultOtaPackageStateService(TbClusterService tbClusterService, OtaPackageService otaPackageService, | |
91 | + public DefaultOtaPackageStateService(@Lazy TbClusterService tbClusterService, | |
92 | + OtaPackageService otaPackageService, | |
91 | 93 | DeviceService deviceService, |
92 | 94 | DeviceProfileService deviceProfileService, |
93 | - RuleEngineTelemetryService telemetryService, | |
95 | + @Lazy RuleEngineTelemetryService telemetryService, | |
94 | 96 | TbCoreQueueFactory coreQueueFactory) { |
95 | 97 | this.tbClusterService = tbClusterService; |
96 | 98 | this.otaPackageService = otaPackageService; | ... | ... |
... | ... | @@ -16,11 +16,17 @@ |
16 | 16 | package org.thingsboard.server.service.queue; |
17 | 17 | |
18 | 18 | import com.google.protobuf.ByteString; |
19 | +import lombok.RequiredArgsConstructor; | |
19 | 20 | import lombok.extern.slf4j.Slf4j; |
20 | 21 | import org.springframework.beans.factory.annotation.Value; |
21 | 22 | import org.springframework.scheduling.annotation.Scheduled; |
22 | 23 | import org.springframework.stereotype.Service; |
23 | -import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; | |
24 | +import org.thingsboard.rule.engine.api.msg.DeviceNameOrTypeUpdateMsg; | |
25 | +import org.thingsboard.server.cluster.TbClusterService; | |
26 | +import org.thingsboard.server.common.data.EdgeUtils; | |
27 | +import org.thingsboard.server.common.data.edge.EdgeEventActionType; | |
28 | +import org.thingsboard.server.common.data.edge.EdgeEventType; | |
29 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
24 | 30 | import org.thingsboard.server.common.data.ApiUsageState; |
25 | 31 | import org.thingsboard.server.common.data.Device; |
26 | 32 | import org.thingsboard.server.common.data.DeviceProfile; |
... | ... | @@ -29,7 +35,6 @@ import org.thingsboard.server.common.data.HasName; |
29 | 35 | import org.thingsboard.server.common.data.TbResource; |
30 | 36 | import org.thingsboard.server.common.data.Tenant; |
31 | 37 | import org.thingsboard.server.common.data.TenantProfile; |
32 | -import org.thingsboard.server.common.data.id.CustomerId; | |
33 | 38 | import org.thingsboard.server.common.data.id.DeviceId; |
34 | 39 | import org.thingsboard.server.common.data.id.DeviceProfileId; |
35 | 40 | import org.thingsboard.server.common.data.id.EdgeId; |
... | ... | @@ -56,8 +61,9 @@ import org.thingsboard.server.queue.common.MultipleTbQueueCallbackWrapper; |
56 | 61 | import org.thingsboard.server.queue.common.TbProtoQueueMsg; |
57 | 62 | import org.thingsboard.server.queue.discovery.PartitionService; |
58 | 63 | import org.thingsboard.server.queue.provider.TbQueueProducerProvider; |
64 | +import org.thingsboard.server.service.ota.OtaPackageStateService; | |
59 | 65 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
60 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
66 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
61 | 67 | |
62 | 68 | import java.util.HashSet; |
63 | 69 | import java.util.Set; |
... | ... | @@ -66,10 +72,13 @@ import java.util.concurrent.atomic.AtomicInteger; |
66 | 72 | |
67 | 73 | @Service |
68 | 74 | @Slf4j |
75 | +@RequiredArgsConstructor | |
69 | 76 | public class DefaultTbClusterService implements TbClusterService { |
70 | 77 | |
71 | 78 | @Value("${cluster.stats.enabled:false}") |
72 | 79 | private boolean statsEnabled; |
80 | + @Value("${edges.enabled}") | |
81 | + protected boolean edgesEnabled; | |
73 | 82 | |
74 | 83 | private final AtomicInteger toCoreMsgs = new AtomicInteger(0); |
75 | 84 | private final AtomicInteger toCoreNfs = new AtomicInteger(0); |
... | ... | @@ -81,13 +90,7 @@ public class DefaultTbClusterService implements TbClusterService { |
81 | 90 | private final PartitionService partitionService; |
82 | 91 | private final DataDecodingEncodingService encodingService; |
83 | 92 | private final TbDeviceProfileCache deviceProfileCache; |
84 | - | |
85 | - public DefaultTbClusterService(TbQueueProducerProvider producerProvider, PartitionService partitionService, DataDecodingEncodingService encodingService, TbDeviceProfileCache deviceProfileCache) { | |
86 | - this.producerProvider = producerProvider; | |
87 | - this.partitionService = partitionService; | |
88 | - this.encodingService = encodingService; | |
89 | - this.deviceProfileCache = deviceProfileCache; | |
90 | - } | |
93 | + private final OtaPackageStateService otaPackageStateService; | |
91 | 94 | |
92 | 95 | @Override |
93 | 96 | public void pushMsgToCore(TenantId tenantId, EntityId entityId, ToCoreMsg msg, TbQueueCallback callback) { |
... | ... | @@ -200,55 +203,52 @@ public class DefaultTbClusterService implements TbClusterService { |
200 | 203 | } |
201 | 204 | |
202 | 205 | @Override |
203 | - public void onEntityStateChange(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent state) { | |
206 | + public void broadcastEntityStateChangeEvent(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent state) { | |
204 | 207 | log.trace("[{}] Processing {} state change event: {}", tenantId, entityId.getEntityType(), state); |
205 | 208 | broadcast(new ComponentLifecycleMsg(tenantId, entityId, state)); |
206 | 209 | } |
207 | 210 | |
208 | 211 | @Override |
209 | 212 | public void onDeviceProfileChange(DeviceProfile deviceProfile, TbQueueCallback callback) { |
210 | - onEntityChange(deviceProfile.getTenantId(), deviceProfile.getId(), deviceProfile, callback); | |
213 | + broadcastEntityChangeToTransport(deviceProfile.getTenantId(), deviceProfile.getId(), deviceProfile, callback); | |
211 | 214 | } |
212 | 215 | |
213 | 216 | @Override |
214 | 217 | public void onTenantProfileChange(TenantProfile tenantProfile, TbQueueCallback callback) { |
215 | - onEntityChange(TenantId.SYS_TENANT_ID, tenantProfile.getId(), tenantProfile, callback); | |
218 | + broadcastEntityChangeToTransport(TenantId.SYS_TENANT_ID, tenantProfile.getId(), tenantProfile, callback); | |
216 | 219 | } |
217 | 220 | |
218 | 221 | @Override |
219 | 222 | public void onTenantChange(Tenant tenant, TbQueueCallback callback) { |
220 | - onEntityChange(TenantId.SYS_TENANT_ID, tenant.getId(), tenant, callback); | |
223 | + broadcastEntityChangeToTransport(TenantId.SYS_TENANT_ID, tenant.getId(), tenant, callback); | |
221 | 224 | } |
222 | 225 | |
223 | 226 | @Override |
224 | 227 | public void onApiStateChange(ApiUsageState apiUsageState, TbQueueCallback callback) { |
225 | - onEntityChange(apiUsageState.getTenantId(), apiUsageState.getId(), apiUsageState, callback); | |
228 | + broadcastEntityChangeToTransport(apiUsageState.getTenantId(), apiUsageState.getId(), apiUsageState, callback); | |
226 | 229 | broadcast(new ComponentLifecycleMsg(apiUsageState.getTenantId(), apiUsageState.getId(), ComponentLifecycleEvent.UPDATED)); |
227 | 230 | } |
228 | 231 | |
229 | 232 | @Override |
230 | 233 | public void onDeviceProfileDelete(DeviceProfile entity, TbQueueCallback callback) { |
231 | - onEntityDelete(entity.getTenantId(), entity.getId(), entity.getName(), callback); | |
234 | + broadcastEntityDeleteToTransport(entity.getTenantId(), entity.getId(), entity.getName(), callback); | |
232 | 235 | } |
233 | 236 | |
234 | 237 | @Override |
235 | 238 | public void onTenantProfileDelete(TenantProfile entity, TbQueueCallback callback) { |
236 | - onEntityDelete(TenantId.SYS_TENANT_ID, entity.getId(), entity.getName(), callback); | |
239 | + broadcastEntityDeleteToTransport(TenantId.SYS_TENANT_ID, entity.getId(), entity.getName(), callback); | |
237 | 240 | } |
238 | 241 | |
239 | 242 | @Override |
240 | 243 | public void onTenantDelete(Tenant entity, TbQueueCallback callback) { |
241 | - onEntityDelete(TenantId.SYS_TENANT_ID, entity.getId(), entity.getName(), callback); | |
244 | + broadcastEntityDeleteToTransport(TenantId.SYS_TENANT_ID, entity.getId(), entity.getName(), callback); | |
242 | 245 | } |
243 | 246 | |
244 | 247 | @Override |
245 | - public void onDeviceChange(Device entity, TbQueueCallback callback) { | |
246 | - onEntityChange(entity.getTenantId(), entity.getId(), entity, callback); | |
247 | - } | |
248 | - | |
249 | - @Override | |
250 | - public void onDeviceDeleted(Device entity, TbQueueCallback callback) { | |
251 | - onEntityDelete(entity.getTenantId(), entity.getId(), entity.getName(), callback); | |
248 | + public void onDeviceDeleted(Device device, TbQueueCallback callback) { | |
249 | + broadcastEntityDeleteToTransport(device.getTenantId(), device.getId(), device.getName(), callback); | |
250 | + sendDeviceStateServiceEvent(device.getTenantId(), device.getId(), false, false, true); | |
251 | + broadcastEntityStateChangeEvent(device.getTenantId(), device.getId(), ComponentLifecycleEvent.DELETED); | |
252 | 252 | } |
253 | 253 | |
254 | 254 | @Override |
... | ... | @@ -278,7 +278,7 @@ public class DefaultTbClusterService implements TbClusterService { |
278 | 278 | broadcast(transportMsg, callback); |
279 | 279 | } |
280 | 280 | |
281 | - public <T> void onEntityChange(TenantId tenantId, EntityId entityid, T entity, TbQueueCallback callback) { | |
281 | + public <T> void broadcastEntityChangeToTransport(TenantId tenantId, EntityId entityid, T entity, TbQueueCallback callback) { | |
282 | 282 | String entityName = (entity instanceof HasName) ? ((HasName) entity).getName() : entity.getClass().getName(); |
283 | 283 | log.trace("[{}][{}][{}] Processing [{}] change event", tenantId, entityid.getEntityType(), entityid.getId(), entityName); |
284 | 284 | TransportProtos.EntityUpdateMsg entityUpdateMsg = TransportProtos.EntityUpdateMsg.newBuilder() |
... | ... | @@ -288,7 +288,7 @@ public class DefaultTbClusterService implements TbClusterService { |
288 | 288 | broadcast(transportMsg, callback); |
289 | 289 | } |
290 | 290 | |
291 | - private void onEntityDelete(TenantId tenantId, EntityId entityId, String name, TbQueueCallback callback) { | |
291 | + private void broadcastEntityDeleteToTransport(TenantId tenantId, EntityId entityId, String name, TbQueueCallback callback) { | |
292 | 292 | log.trace("[{}][{}][{}] Processing [{}] delete event", tenantId, entityId.getEntityType(), entityId.getId(), name); |
293 | 293 | TransportProtos.EntityDeleteMsg entityDeleteMsg = TransportProtos.EntityDeleteMsg.newBuilder() |
294 | 294 | .setEntityType(entityId.getEntityType().name()) |
... | ... | @@ -369,4 +369,72 @@ public class DefaultTbClusterService implements TbClusterService { |
369 | 369 | } |
370 | 370 | } |
371 | 371 | } |
372 | + | |
373 | + private void sendDeviceStateServiceEvent(TenantId tenantId, DeviceId deviceId, boolean added, boolean updated, boolean deleted) { | |
374 | + TransportProtos.DeviceStateServiceMsgProto.Builder builder = TransportProtos.DeviceStateServiceMsgProto.newBuilder(); | |
375 | + builder.setTenantIdMSB(tenantId.getId().getMostSignificantBits()); | |
376 | + builder.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()); | |
377 | + builder.setDeviceIdMSB(deviceId.getId().getMostSignificantBits()); | |
378 | + builder.setDeviceIdLSB(deviceId.getId().getLeastSignificantBits()); | |
379 | + builder.setAdded(added); | |
380 | + builder.setUpdated(updated); | |
381 | + builder.setDeleted(deleted); | |
382 | + TransportProtos.DeviceStateServiceMsgProto msg = builder.build(); | |
383 | + pushMsgToCore(tenantId, deviceId, TransportProtos.ToCoreMsg.newBuilder().setDeviceStateServiceMsg(msg).build(), null); | |
384 | + } | |
385 | + | |
386 | + @Override | |
387 | + public void onDeviceUpdated(Device device, Device old) { | |
388 | + var created = old == null; | |
389 | + broadcastEntityChangeToTransport(device.getTenantId(), device.getId(), device, null); | |
390 | + if (old != null && (!device.getName().equals(old.getName()) || !device.getType().equals(old.getType()))) { | |
391 | + pushMsgToCore(new DeviceNameOrTypeUpdateMsg(device.getTenantId(), device.getId(), device.getName(), device.getType()), null); | |
392 | + } | |
393 | + broadcastEntityStateChangeEvent(device.getTenantId(), device.getId(), created ? ComponentLifecycleEvent.CREATED : ComponentLifecycleEvent.UPDATED); | |
394 | + sendDeviceStateServiceEvent(device.getTenantId(), device.getId(), created, !created, false); | |
395 | + otaPackageStateService.update(device, old); | |
396 | + if (!created) { | |
397 | + sendNotificationMsgToEdgeService(device.getTenantId(), null, device.getId(), null, null, EdgeEventActionType.UPDATED); | |
398 | + } | |
399 | + } | |
400 | + | |
401 | + @Override | |
402 | + public void sendNotificationMsgToEdgeService(TenantId tenantId, EdgeId edgeId, EntityId entityId, String body, EdgeEventType type, EdgeEventActionType action) { | |
403 | + if (!edgesEnabled) { | |
404 | + return; | |
405 | + } | |
406 | + if (type == null) { | |
407 | + if (entityId != null) { | |
408 | + type = EdgeUtils.getEdgeEventTypeByEntityType(entityId.getEntityType()); | |
409 | + } else { | |
410 | + log.trace("[{}] entity id and type are null. Ignoring this notification", tenantId); | |
411 | + return; | |
412 | + } | |
413 | + if (type == null) { | |
414 | + log.trace("[{}] edge event type is null. Ignoring this notification [{}]", tenantId, entityId); | |
415 | + return; | |
416 | + } | |
417 | + } | |
418 | + TransportProtos.EdgeNotificationMsgProto.Builder builder = TransportProtos.EdgeNotificationMsgProto.newBuilder(); | |
419 | + builder.setTenantIdMSB(tenantId.getId().getMostSignificantBits()); | |
420 | + builder.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()); | |
421 | + builder.setType(type.name()); | |
422 | + builder.setAction(action.name()); | |
423 | + if (entityId != null) { | |
424 | + builder.setEntityIdMSB(entityId.getId().getMostSignificantBits()); | |
425 | + builder.setEntityIdLSB(entityId.getId().getLeastSignificantBits()); | |
426 | + builder.setEntityType(entityId.getEntityType().name()); | |
427 | + } | |
428 | + if (edgeId != null) { | |
429 | + builder.setEdgeIdMSB(edgeId.getId().getMostSignificantBits()); | |
430 | + builder.setEdgeIdLSB(edgeId.getId().getLeastSignificantBits()); | |
431 | + } | |
432 | + if (body != null) { | |
433 | + builder.setBody(body); | |
434 | + } | |
435 | + TransportProtos.EdgeNotificationMsgProto msg = builder.build(); | |
436 | + log.trace("[{}] sending notification to edge service {}", tenantId.getId(), msg); | |
437 | + pushMsgToCore(tenantId, entityId != null ? entityId : tenantId, TransportProtos.ToCoreMsg.newBuilder().setEdgeNotificationMsg(msg).build(), null); | |
438 | + } | |
439 | + | |
372 | 440 | } | ... | ... |
... | ... | @@ -26,7 +26,7 @@ import org.springframework.scheduling.annotation.Scheduled; |
26 | 26 | import org.springframework.stereotype.Service; |
27 | 27 | import org.thingsboard.common.util.JacksonUtil; |
28 | 28 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
29 | -import org.thingsboard.rule.engine.api.RpcError; | |
29 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
30 | 30 | import org.thingsboard.server.actors.ActorSystemContext; |
31 | 31 | import org.thingsboard.server.common.data.alarm.Alarm; |
32 | 32 | import org.thingsboard.server.common.data.id.TenantId; |
... | ... | @@ -64,7 +64,7 @@ import org.thingsboard.server.service.ota.OtaPackageStateService; |
64 | 64 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
65 | 65 | import org.thingsboard.server.service.queue.processing.AbstractConsumerService; |
66 | 66 | import org.thingsboard.server.service.queue.processing.IdMsgPair; |
67 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
67 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
68 | 68 | import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService; |
69 | 69 | import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg; |
70 | 70 | import org.thingsboard.server.service.state.DeviceStateService; | ... | ... |
... | ... | @@ -21,7 +21,7 @@ import org.springframework.beans.factory.annotation.Value; |
21 | 21 | import org.springframework.scheduling.annotation.Scheduled; |
22 | 22 | import org.springframework.stereotype.Service; |
23 | 23 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
24 | -import org.thingsboard.rule.engine.api.RpcError; | |
24 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
25 | 25 | import org.thingsboard.server.actors.ActorSystemContext; |
26 | 26 | import org.thingsboard.server.common.data.id.TenantId; |
27 | 27 | import org.thingsboard.server.common.msg.TbMsg; |
... | ... | @@ -55,7 +55,7 @@ import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStr |
55 | 55 | import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingStrategyFactory; |
56 | 56 | import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategy; |
57 | 57 | import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrategyFactory; |
58 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
58 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
59 | 59 | import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService; |
60 | 60 | import org.thingsboard.server.service.stats.RuleEngineStatisticsService; |
61 | 61 | ... | ... |
... | ... | @@ -22,18 +22,19 @@ import lombok.extern.slf4j.Slf4j; |
22 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
23 | 23 | import org.springframework.stereotype.Service; |
24 | 24 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
25 | -import org.thingsboard.rule.engine.api.RpcError; | |
25 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
26 | 26 | import org.thingsboard.server.actors.ActorSystemContext; |
27 | 27 | import org.thingsboard.server.common.data.DataConstants; |
28 | 28 | import org.thingsboard.server.common.data.Device; |
29 | 29 | import org.thingsboard.server.common.msg.TbMsg; |
30 | 30 | import org.thingsboard.server.common.msg.TbMsgDataType; |
31 | 31 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
32 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
32 | 33 | import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; |
33 | 34 | import org.thingsboard.server.dao.device.DeviceService; |
34 | 35 | import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
35 | 36 | import org.thingsboard.server.queue.util.TbCoreComponent; |
36 | -import org.thingsboard.server.service.queue.TbClusterService; | |
37 | +import org.thingsboard.server.cluster.TbClusterService; | |
37 | 38 | import org.thingsboard.server.service.security.model.SecurityUser; |
38 | 39 | |
39 | 40 | import javax.annotation.PostConstruct; | ... | ... |
... | ... | @@ -19,18 +19,19 @@ import lombok.extern.slf4j.Slf4j; |
19 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
20 | 20 | import org.springframework.stereotype.Service; |
21 | 21 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
22 | -import org.thingsboard.rule.engine.api.RpcError; | |
22 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
23 | 23 | import org.thingsboard.rule.engine.api.RuleEngineDeviceRpcRequest; |
24 | 24 | import org.thingsboard.rule.engine.api.RuleEngineDeviceRpcResponse; |
25 | 25 | import org.thingsboard.server.common.data.rpc.ToDeviceRpcRequestBody; |
26 | 26 | import org.thingsboard.server.common.msg.queue.ServiceType; |
27 | 27 | import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
28 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
28 | 29 | import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; |
29 | 30 | import org.thingsboard.server.gen.transport.TransportProtos; |
30 | 31 | import org.thingsboard.server.queue.discovery.PartitionService; |
31 | 32 | import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
32 | 33 | import org.thingsboard.server.queue.util.TbRuleEngineComponent; |
33 | -import org.thingsboard.server.service.queue.TbClusterService; | |
34 | +import org.thingsboard.server.cluster.TbClusterService; | |
34 | 35 | |
35 | 36 | import javax.annotation.PostConstruct; |
36 | 37 | import javax.annotation.PreDestroy; | ... | ... |
... | ... | @@ -18,10 +18,11 @@ package org.thingsboard.server.service.rpc; |
18 | 18 | import lombok.Getter; |
19 | 19 | import lombok.RequiredArgsConstructor; |
20 | 20 | import lombok.ToString; |
21 | -import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; | |
21 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
22 | 22 | import org.thingsboard.server.common.data.id.DeviceId; |
23 | 23 | import org.thingsboard.server.common.data.id.TenantId; |
24 | 24 | import org.thingsboard.server.common.msg.MsgType; |
25 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
25 | 26 | |
26 | 27 | @ToString |
27 | 28 | @RequiredArgsConstructor | ... | ... |
... | ... | @@ -15,6 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.service.rpc; |
17 | 17 | |
18 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
18 | 19 | import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest; |
19 | 20 | import org.thingsboard.server.service.security.model.SecurityUser; |
20 | 21 | ... | ... |
... | ... | @@ -31,7 +31,7 @@ import org.thingsboard.server.common.msg.TbMsg; |
31 | 31 | import org.thingsboard.server.common.msg.TbMsgMetaData; |
32 | 32 | import org.thingsboard.server.dao.rpc.RpcService; |
33 | 33 | import org.thingsboard.server.queue.util.TbCoreComponent; |
34 | -import org.thingsboard.server.service.queue.TbClusterService; | |
34 | +import org.thingsboard.server.cluster.TbClusterService; | |
35 | 35 | |
36 | 36 | @TbCoreComponent |
37 | 37 | @Service | ... | ... |
... | ... | @@ -18,7 +18,7 @@ package org.thingsboard.server.service.rpc; |
18 | 18 | import lombok.Getter; |
19 | 19 | import lombok.RequiredArgsConstructor; |
20 | 20 | import lombok.ToString; |
21 | -import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; | |
21 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
22 | 22 | import org.thingsboard.server.common.data.id.DeviceId; |
23 | 23 | import org.thingsboard.server.common.data.id.TenantId; |
24 | 24 | import org.thingsboard.server.common.msg.MsgType; | ... | ... |
... | ... | @@ -47,7 +47,7 @@ import org.thingsboard.server.dao.tenant.TbTenantProfileCache; |
47 | 47 | import org.thingsboard.server.dao.tenant.TenantService; |
48 | 48 | import org.thingsboard.server.dao.user.UserService; |
49 | 49 | import org.thingsboard.server.service.install.InstallScripts; |
50 | -import org.thingsboard.server.service.queue.TbClusterService; | |
50 | +import org.thingsboard.server.cluster.TbClusterService; | |
51 | 51 | import org.thingsboard.server.service.security.model.SecurityUser; |
52 | 52 | import org.thingsboard.server.service.security.model.UserPrincipal; |
53 | 53 | |
... | ... | @@ -180,7 +180,7 @@ public abstract class AbstractOAuth2ClientMapper { |
180 | 180 | installScripts.createDefaultEdgeRuleChains(tenant.getId()); |
181 | 181 | tenantProfileCache.evict(tenant.getId()); |
182 | 182 | tbClusterService.onTenantChange(tenant, null); |
183 | - tbClusterService.onEntityStateChange(tenant.getId(), tenant.getId(), | |
183 | + tbClusterService.broadcastEntityStateChangeEvent(tenant.getId(), tenant.getId(), | |
184 | 184 | ComponentLifecycleEvent.CREATED); |
185 | 185 | } else { |
186 | 186 | tenant = tenants.get(0); | ... | ... |
... | ... | @@ -22,7 +22,6 @@ import com.google.common.util.concurrent.Futures; |
22 | 22 | import com.google.common.util.concurrent.ListenableFuture; |
23 | 23 | import com.google.common.util.concurrent.ListeningScheduledExecutorService; |
24 | 24 | import com.google.common.util.concurrent.MoreExecutors; |
25 | -import com.google.common.util.concurrent.SettableFuture; | |
26 | 25 | import lombok.Getter; |
27 | 26 | import lombok.extern.slf4j.Slf4j; |
28 | 27 | import org.springframework.beans.factory.annotation.Autowired; |
... | ... | @@ -59,7 +58,7 @@ import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; |
59 | 58 | import org.thingsboard.server.queue.discovery.PartitionService; |
60 | 59 | import org.thingsboard.server.queue.discovery.TbApplicationEventListener; |
61 | 60 | import org.thingsboard.server.queue.util.TbCoreComponent; |
62 | -import org.thingsboard.server.service.queue.TbClusterService; | |
61 | +import org.thingsboard.server.cluster.TbClusterService; | |
63 | 62 | import org.thingsboard.server.service.telemetry.TelemetrySubscriptionService; |
64 | 63 | |
65 | 64 | import javax.annotation.Nonnull; |
... | ... | @@ -71,7 +70,6 @@ import java.util.Arrays; |
71 | 70 | import java.util.Collections; |
72 | 71 | import java.util.HashSet; |
73 | 72 | import java.util.List; |
74 | -import java.util.Optional; | |
75 | 73 | import java.util.Queue; |
76 | 74 | import java.util.Random; |
77 | 75 | import java.util.Set; |
... | ... | @@ -176,21 +174,6 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
176 | 174 | } |
177 | 175 | |
178 | 176 | @Override |
179 | - public void onDeviceAdded(Device device) { | |
180 | - sendDeviceEvent(device.getTenantId(), device.getId(), true, false, false); | |
181 | - } | |
182 | - | |
183 | - @Override | |
184 | - public void onDeviceUpdated(Device device) { | |
185 | - sendDeviceEvent(device.getTenantId(), device.getId(), false, true, false); | |
186 | - } | |
187 | - | |
188 | - @Override | |
189 | - public void onDeviceDeleted(Device device) { | |
190 | - sendDeviceEvent(device.getTenantId(), device.getId(), false, false, true); | |
191 | - } | |
192 | - | |
193 | - @Override | |
194 | 177 | public void onDeviceConnect(TenantId tenantId, DeviceId deviceId) { |
195 | 178 | log.trace("on Device Connect [{}]", deviceId.getId()); |
196 | 179 | DeviceStateData stateData = getOrFetchDeviceStateData(deviceId); |
... | ... | @@ -269,6 +252,7 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
269 | 252 | TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, device.getId()); |
270 | 253 | if (partitionedDevices.containsKey(tpi)) { |
271 | 254 | addDeviceUsingState(tpi, state); |
255 | + save(deviceId, ACTIVITY_STATE, false); | |
272 | 256 | callback.onSuccess(); |
273 | 257 | } else { |
274 | 258 | log.warn("[{}][{}] Device belongs to external partition. Probably rebalancing is in progress. Topic: {}" |
... | ... | @@ -454,7 +438,7 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
454 | 438 | deviceStates.put(state.getDeviceId(), state); |
455 | 439 | } else { |
456 | 440 | log.warn("Device belongs to external partition {}" + tpi.getFullTopicName()); |
457 | - new RuntimeException("Device belongs to external partition " + tpi.getFullTopicName() + "!"); | |
441 | + throw new RuntimeException("Device belongs to external partition " + tpi.getFullTopicName() + "!"); | |
458 | 442 | } |
459 | 443 | } |
460 | 444 | |
... | ... | @@ -528,19 +512,6 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
528 | 512 | } |
529 | 513 | } |
530 | 514 | |
531 | - private void sendDeviceEvent(TenantId tenantId, DeviceId deviceId, boolean added, boolean updated, boolean deleted) { | |
532 | - TransportProtos.DeviceStateServiceMsgProto.Builder builder = TransportProtos.DeviceStateServiceMsgProto.newBuilder(); | |
533 | - builder.setTenantIdMSB(tenantId.getId().getMostSignificantBits()); | |
534 | - builder.setTenantIdLSB(tenantId.getId().getLeastSignificantBits()); | |
535 | - builder.setDeviceIdMSB(deviceId.getId().getMostSignificantBits()); | |
536 | - builder.setDeviceIdLSB(deviceId.getId().getLeastSignificantBits()); | |
537 | - builder.setAdded(added); | |
538 | - builder.setUpdated(updated); | |
539 | - builder.setDeleted(deleted); | |
540 | - TransportProtos.DeviceStateServiceMsgProto msg = builder.build(); | |
541 | - clusterService.pushMsgToCore(tenantId, deviceId, TransportProtos.ToCoreMsg.newBuilder().setDeviceStateServiceMsg(msg).build(), null); | |
542 | - } | |
543 | - | |
544 | 515 | private void onDeviceDeleted(TenantId tenantId, DeviceId deviceId) { |
545 | 516 | cleanUpDeviceStateMap(deviceId); |
546 | 517 | TopicPartitionInfo tpi = partitionService.resolve(ServiceType.TB_CORE, tenantId, deviceId); |
... | ... | @@ -582,7 +553,7 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
582 | 553 | } |
583 | 554 | |
584 | 555 | private <T extends KvEntry> Function<List<T>, DeviceStateData> extractDeviceStateData(Device device) { |
585 | - return new Function<List<T>, DeviceStateData>() { | |
556 | + return new Function<>() { | |
586 | 557 | @Nonnull |
587 | 558 | @Override |
588 | 559 | public DeviceStateData apply(@Nullable List<T> data) { |
... | ... | @@ -669,9 +640,9 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
669 | 640 | tsSubService.saveAndNotifyInternal( |
670 | 641 | TenantId.SYS_TENANT_ID, deviceId, |
671 | 642 | Collections.singletonList(new BasicTsKvEntry(System.currentTimeMillis(), new LongDataEntry(key, value))), |
672 | - new AttributeSaveCallback<>(deviceId, key, value)); | |
643 | + new TelemetrySaveCallback<>(deviceId, key, value)); | |
673 | 644 | } else { |
674 | - tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new AttributeSaveCallback<>(deviceId, key, value)); | |
645 | + tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new TelemetrySaveCallback<>(deviceId, key, value)); | |
675 | 646 | } |
676 | 647 | } |
677 | 648 | |
... | ... | @@ -680,18 +651,18 @@ public class DefaultDeviceStateService extends TbApplicationEventListener<Partit |
680 | 651 | tsSubService.saveAndNotifyInternal( |
681 | 652 | TenantId.SYS_TENANT_ID, deviceId, |
682 | 653 | Collections.singletonList(new BasicTsKvEntry(System.currentTimeMillis(), new BooleanDataEntry(key, value))), |
683 | - new AttributeSaveCallback<>(deviceId, key, value)); | |
654 | + new TelemetrySaveCallback<>(deviceId, key, value)); | |
684 | 655 | } else { |
685 | - tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new AttributeSaveCallback<>(deviceId, key, value)); | |
656 | + tsSubService.saveAttrAndNotify(TenantId.SYS_TENANT_ID, deviceId, DataConstants.SERVER_SCOPE, key, value, new TelemetrySaveCallback<>(deviceId, key, value)); | |
686 | 657 | } |
687 | 658 | } |
688 | 659 | |
689 | - private static class AttributeSaveCallback<T> implements FutureCallback<T> { | |
660 | + private static class TelemetrySaveCallback<T> implements FutureCallback<T> { | |
690 | 661 | private final DeviceId deviceId; |
691 | 662 | private final String key; |
692 | 663 | private final Object value; |
693 | 664 | |
694 | - AttributeSaveCallback(DeviceId deviceId, String key, Object value) { | |
665 | + TelemetrySaveCallback(DeviceId deviceId, String key, Object value) { | |
695 | 666 | this.deviceId = deviceId; |
696 | 667 | this.key = key; |
697 | 668 | this.value = value; | ... | ... |
... | ... | @@ -28,12 +28,6 @@ import org.thingsboard.server.common.msg.queue.TbCallback; |
28 | 28 | */ |
29 | 29 | public interface DeviceStateService extends ApplicationListener<PartitionChangeEvent> { |
30 | 30 | |
31 | - void onDeviceAdded(Device device); | |
32 | - | |
33 | - void onDeviceUpdated(Device device); | |
34 | - | |
35 | - void onDeviceDeleted(Device device); | |
36 | - | |
37 | 31 | void onDeviceConnect(TenantId tenantId, DeviceId deviceId); |
38 | 32 | |
39 | 33 | void onDeviceActivity(TenantId tenantId, DeviceId deviceId, long lastReportedActivityTime); | ... | ... |
... | ... | @@ -53,7 +53,7 @@ import org.thingsboard.server.queue.discovery.TbApplicationEventListener; |
53 | 53 | import org.thingsboard.server.queue.discovery.TbServiceInfoProvider; |
54 | 54 | import org.thingsboard.server.queue.provider.TbQueueProducerProvider; |
55 | 55 | import org.thingsboard.server.queue.util.TbCoreComponent; |
56 | -import org.thingsboard.server.service.queue.TbClusterService; | |
56 | +import org.thingsboard.server.cluster.TbClusterService; | |
57 | 57 | import org.thingsboard.server.service.state.DefaultDeviceStateService; |
58 | 58 | import org.thingsboard.server.service.state.DeviceStateService; |
59 | 59 | import org.thingsboard.server.service.telemetry.sub.AlarmSubscriptionUpdate; | ... | ... |
... | ... | @@ -30,7 +30,7 @@ import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
30 | 30 | import org.thingsboard.server.common.msg.queue.TbCallback; |
31 | 31 | import org.thingsboard.server.queue.discovery.TbApplicationEventListener; |
32 | 32 | import org.thingsboard.server.queue.util.TbCoreComponent; |
33 | -import org.thingsboard.server.service.queue.TbClusterService; | |
33 | +import org.thingsboard.server.cluster.TbClusterService; | |
34 | 34 | import org.thingsboard.server.service.telemetry.sub.AlarmSubscriptionUpdate; |
35 | 35 | import org.thingsboard.server.service.telemetry.sub.TelemetrySubscriptionUpdate; |
36 | 36 | |
... | ... | @@ -42,7 +42,6 @@ import java.util.Map; |
42 | 42 | import java.util.Set; |
43 | 43 | import java.util.concurrent.ConcurrentHashMap; |
44 | 44 | import java.util.concurrent.ExecutorService; |
45 | -import java.util.concurrent.Executors; | |
46 | 45 | |
47 | 46 | @Slf4j |
48 | 47 | @TbCoreComponent | ... | ... |
... | ... | @@ -20,15 +20,13 @@ import com.google.common.util.concurrent.Futures; |
20 | 20 | import com.google.common.util.concurrent.ListenableFuture; |
21 | 21 | import lombok.extern.slf4j.Slf4j; |
22 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
23 | -import org.springframework.context.ApplicationListener; | |
24 | -import org.springframework.context.event.EventListener; | |
25 | 23 | import org.thingsboard.common.util.ThingsBoardThreadFactory; |
26 | 24 | import org.thingsboard.server.common.msg.queue.ServiceType; |
27 | 25 | import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
28 | 26 | import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent; |
29 | 27 | import org.thingsboard.server.queue.discovery.PartitionService; |
30 | 28 | import org.thingsboard.server.queue.discovery.TbApplicationEventListener; |
31 | -import org.thingsboard.server.service.queue.TbClusterService; | |
29 | +import org.thingsboard.server.cluster.TbClusterService; | |
32 | 30 | import org.thingsboard.server.service.subscription.SubscriptionManagerService; |
33 | 31 | |
34 | 32 | import javax.annotation.Nullable; | ... | ... |
... | ... | @@ -46,7 +46,7 @@ import org.thingsboard.server.gen.transport.TransportProtos; |
46 | 46 | import org.thingsboard.server.queue.discovery.PartitionService; |
47 | 47 | import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
48 | 48 | import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
49 | -import org.thingsboard.server.service.queue.TbClusterService; | |
49 | +import org.thingsboard.server.cluster.TbClusterService; | |
50 | 50 | import org.thingsboard.server.service.subscription.SubscriptionManagerService; |
51 | 51 | import org.thingsboard.server.service.subscription.TbSubscriptionUtils; |
52 | 52 | ... | ... |
... | ... | @@ -45,7 +45,7 @@ import org.thingsboard.server.gen.transport.TransportProtos; |
45 | 45 | import org.thingsboard.server.queue.discovery.PartitionService; |
46 | 46 | import org.thingsboard.server.queue.usagestats.TbApiUsageClient; |
47 | 47 | import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
48 | -import org.thingsboard.server.service.queue.TbClusterService; | |
48 | +import org.thingsboard.server.cluster.TbClusterService; | |
49 | 49 | import org.thingsboard.server.service.subscription.TbSubscriptionUtils; |
50 | 50 | |
51 | 51 | import javax.annotation.Nullable; | ... | ... |
... | ... | @@ -95,7 +95,7 @@ import org.thingsboard.server.queue.util.TbCoreComponent; |
95 | 95 | import org.thingsboard.server.service.apiusage.TbApiUsageStateService; |
96 | 96 | import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
97 | 97 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
98 | -import org.thingsboard.server.service.queue.TbClusterService; | |
98 | +import org.thingsboard.server.cluster.TbClusterService; | |
99 | 99 | import org.thingsboard.server.service.resource.TbResourceService; |
100 | 100 | import org.thingsboard.server.service.state.DeviceStateService; |
101 | 101 | |
... | ... | @@ -265,9 +265,11 @@ public class DefaultTransportApiService implements TransportApiService { |
265 | 265 | device.setCustomerId(gateway.getCustomerId()); |
266 | 266 | DeviceProfile deviceProfile = deviceProfileCache.findOrCreateDeviceProfile(gateway.getTenantId(), requestMsg.getDeviceType()); |
267 | 267 | device.setDeviceProfileId(deviceProfile.getId()); |
268 | - device = deviceService.saveDevice(device); | |
268 | + Device savedDevice = deviceService.saveDevice(device); | |
269 | + tbClusterService.onDeviceUpdated(savedDevice, device); | |
270 | + device = savedDevice; | |
271 | + | |
269 | 272 | relationService.saveRelationAsync(TenantId.SYS_TENANT_ID, new EntityRelation(gateway.getId(), device.getId(), "Created")); |
270 | - deviceStateService.onDeviceAdded(device); | |
271 | 273 | |
272 | 274 | TbMsgMetaData metaData = new TbMsgMetaData(); |
273 | 275 | CustomerId customerId = gateway.getCustomerId(); |
... | ... | @@ -581,7 +583,7 @@ public class DefaultTransportApiService implements TransportApiService { |
581 | 583 | device.setName(deviceName); |
582 | 584 | device.setType("LwM2M"); |
583 | 585 | device = deviceService.saveDevice(device); |
584 | - deviceStateService.onDeviceAdded(device); | |
586 | + tbClusterService.onDeviceUpdated(device, null); | |
585 | 587 | } |
586 | 588 | TransportProtos.LwM2MRegistrationResponseMsg registrationResponseMsg = |
587 | 589 | TransportProtos.LwM2MRegistrationResponseMsg.newBuilder() | ... | ... |
... | ... | @@ -28,7 +28,6 @@ import com.google.protobuf.MessageLite; |
28 | 28 | import lombok.extern.slf4j.Slf4j; |
29 | 29 | import org.apache.commons.lang3.RandomStringUtils; |
30 | 30 | import org.awaitility.Awaitility; |
31 | -import org.hamcrest.Matchers; | |
32 | 31 | import org.junit.After; |
33 | 32 | import org.junit.Assert; |
34 | 33 | import org.junit.Before; |
... | ... | @@ -116,8 +115,7 @@ import org.thingsboard.server.gen.edge.v1.UserUpdateMsg; |
116 | 115 | import org.thingsboard.server.gen.edge.v1.WidgetTypeUpdateMsg; |
117 | 116 | import org.thingsboard.server.gen.edge.v1.WidgetsBundleUpdateMsg; |
118 | 117 | import org.thingsboard.server.gen.transport.TransportProtos; |
119 | -import org.thingsboard.server.service.edge.rpc.EdgeProtoUtils; | |
120 | -import org.thingsboard.server.service.queue.TbClusterService; | |
118 | +import org.thingsboard.server.cluster.TbClusterService; | |
121 | 119 | |
122 | 120 | import java.util.ArrayList; |
123 | 121 | import java.util.List; | ... | ... |
... | ... | @@ -27,7 +27,7 @@ import org.thingsboard.server.dao.device.DeviceService; |
27 | 27 | import org.thingsboard.server.dao.tenant.TenantService; |
28 | 28 | import org.thingsboard.server.dao.timeseries.TimeseriesService; |
29 | 29 | import org.thingsboard.server.queue.discovery.PartitionService; |
30 | -import org.thingsboard.server.service.queue.TbClusterService; | |
30 | +import org.thingsboard.server.cluster.TbClusterService; | |
31 | 31 | |
32 | 32 | import static org.hamcrest.CoreMatchers.is; |
33 | 33 | import static org.hamcrest.MatcherAssert.assertThat; | ... | ... |
common/cluster-api/pom.xml
0 → 100644
1 | +<!-- | |
2 | + | |
3 | + Copyright © 2016-2021 The Thingsboard Authors | |
4 | + | |
5 | + Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | + you may not use this file except in compliance with the License. | |
7 | + You may obtain a copy of the License at | |
8 | + | |
9 | + http://www.apache.org/licenses/LICENSE-2.0 | |
10 | + | |
11 | + Unless required by applicable law or agreed to in writing, software | |
12 | + distributed under the License is distributed on an "AS IS" BASIS, | |
13 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | + See the License for the specific language governing permissions and | |
15 | + limitations under the License. | |
16 | + | |
17 | +--> | |
18 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
19 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
20 | + <modelVersion>4.0.0</modelVersion> | |
21 | + <parent> | |
22 | + <groupId>org.thingsboard</groupId> | |
23 | + <version>3.3.0-SNAPSHOT</version> | |
24 | + <artifactId>common</artifactId> | |
25 | + </parent> | |
26 | + <groupId>org.thingsboard.common</groupId> | |
27 | + <artifactId>cluster-api</artifactId> | |
28 | + <packaging>jar</packaging> | |
29 | + | |
30 | + <name>Thingsboard Server Common Cluster API</name> | |
31 | + <url>https://thingsboard.io</url> | |
32 | + | |
33 | + <properties> | |
34 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
35 | + <main.dir>${basedir}/../..</main.dir> | |
36 | + </properties> | |
37 | + | |
38 | + <dependencies> | |
39 | + <dependency> | |
40 | + <groupId>org.thingsboard.common</groupId> | |
41 | + <artifactId>data</artifactId> | |
42 | + </dependency> | |
43 | + <dependency> | |
44 | + <groupId>org.thingsboard.common</groupId> | |
45 | + <artifactId>message</artifactId> | |
46 | + </dependency> | |
47 | + <dependency> | |
48 | + <groupId>org.thingsboard.common</groupId> | |
49 | + <artifactId>stats</artifactId> | |
50 | + </dependency> | |
51 | + <dependency> | |
52 | + <groupId>com.google.guava</groupId> | |
53 | + <artifactId>guava</artifactId> | |
54 | + </dependency> | |
55 | + <dependency> | |
56 | + <groupId>javax.annotation</groupId> | |
57 | + <artifactId>javax.annotation-api</artifactId> | |
58 | + </dependency> | |
59 | + <dependency> | |
60 | + <groupId>com.github.fge</groupId> | |
61 | + <artifactId>json-schema-validator</artifactId> | |
62 | + </dependency> | |
63 | + <dependency> | |
64 | + <groupId>org.slf4j</groupId> | |
65 | + <artifactId>slf4j-api</artifactId> | |
66 | + </dependency> | |
67 | + <dependency> | |
68 | + <groupId>org.slf4j</groupId> | |
69 | + <artifactId>log4j-over-slf4j</artifactId> | |
70 | + </dependency> | |
71 | + <dependency> | |
72 | + <groupId>ch.qos.logback</groupId> | |
73 | + <artifactId>logback-core</artifactId> | |
74 | + </dependency> | |
75 | + <dependency> | |
76 | + <groupId>ch.qos.logback</groupId> | |
77 | + <artifactId>logback-classic</artifactId> | |
78 | + </dependency> | |
79 | + <dependency> | |
80 | + <groupId>com.fasterxml.jackson.core</groupId> | |
81 | + <artifactId>jackson-databind</artifactId> | |
82 | + </dependency> | |
83 | + <dependency> | |
84 | + <groupId>org.springframework.boot</groupId> | |
85 | + <artifactId>spring-boot-autoconfigure</artifactId> | |
86 | + <scope>provided</scope> | |
87 | + </dependency> | |
88 | + <dependency> | |
89 | + <groupId>com.datastax.oss</groupId> | |
90 | + <artifactId>java-driver-core</artifactId> | |
91 | + <scope>provided</scope> | |
92 | + </dependency> | |
93 | + <dependency> | |
94 | + <groupId>io.dropwizard.metrics</groupId> | |
95 | + <artifactId>metrics-jmx</artifactId> | |
96 | + <scope>provided</scope> | |
97 | + </dependency> | |
98 | + <dependency> | |
99 | + <groupId>org.apache.commons</groupId> | |
100 | + <artifactId>commons-lang3</artifactId> | |
101 | + <scope>provided</scope> | |
102 | + </dependency> | |
103 | + <dependency> | |
104 | + <groupId>junit</groupId> | |
105 | + <artifactId>junit</artifactId> | |
106 | + <scope>test</scope> | |
107 | + </dependency> | |
108 | + <dependency> | |
109 | + <groupId>org.mockito</groupId> | |
110 | + <artifactId>mockito-core</artifactId> | |
111 | + <scope>test</scope> | |
112 | + </dependency> | |
113 | + </dependencies> | |
114 | + | |
115 | + <build> | |
116 | + <plugins> | |
117 | + <plugin> | |
118 | + <groupId>org.xolstice.maven.plugins</groupId> | |
119 | + <artifactId>protobuf-maven-plugin</artifactId> | |
120 | + </plugin> | |
121 | + <plugin> | |
122 | + <groupId>org.apache.maven.plugins</groupId> | |
123 | + <artifactId>maven-source-plugin</artifactId> | |
124 | + <executions> | |
125 | + <execution> | |
126 | + <id>attach-sources</id> | |
127 | + <goals> | |
128 | + <goal>jar</goal> | |
129 | + </goals> | |
130 | + </execution> | |
131 | + </executions> | |
132 | + </plugin> | |
133 | + <plugin> | |
134 | + <groupId>org.apache.maven.plugins</groupId> | |
135 | + <artifactId>maven-deploy-plugin</artifactId> | |
136 | + <configuration> | |
137 | + <skip>false</skip> | |
138 | + </configuration> | |
139 | + </plugin> | |
140 | + </plugins> | |
141 | + </build> | |
142 | + | |
143 | + | |
144 | +</project> | ... | ... |
common/cluster-api/src/main/java/org/thingsboard/server/cluster/TbClusterService.java
renamed from
application/src/main/java/org/thingsboard/server/service/queue/TbClusterService.java
... | ... | @@ -13,26 +13,29 @@ |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | -package org.thingsboard.server.service.queue; | |
16 | +package org.thingsboard.server.cluster; | |
17 | 17 | |
18 | -import org.thingsboard.rule.engine.api.msg.ToDeviceActorNotificationMsg; | |
18 | +import org.thingsboard.server.common.data.edge.EdgeEventActionType; | |
19 | +import org.thingsboard.server.common.data.edge.EdgeEventType; | |
20 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
19 | 21 | import org.thingsboard.server.common.data.ApiUsageState; |
20 | 22 | import org.thingsboard.server.common.data.Device; |
21 | 23 | import org.thingsboard.server.common.data.DeviceProfile; |
22 | 24 | import org.thingsboard.server.common.data.TbResource; |
23 | 25 | import org.thingsboard.server.common.data.Tenant; |
24 | 26 | import org.thingsboard.server.common.data.TenantProfile; |
27 | +import org.thingsboard.server.common.data.id.DeviceId; | |
25 | 28 | import org.thingsboard.server.common.data.id.EdgeId; |
26 | 29 | import org.thingsboard.server.common.data.id.EntityId; |
27 | 30 | import org.thingsboard.server.common.data.id.TenantId; |
28 | 31 | import org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent; |
29 | 32 | import org.thingsboard.server.common.msg.TbMsg; |
30 | 33 | import org.thingsboard.server.common.msg.queue.TopicPartitionInfo; |
34 | +import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse; | |
31 | 35 | import org.thingsboard.server.gen.transport.TransportProtos; |
32 | 36 | import org.thingsboard.server.gen.transport.TransportProtos.ToCoreMsg; |
33 | 37 | import org.thingsboard.server.gen.transport.TransportProtos.ToTransportMsg; |
34 | 38 | import org.thingsboard.server.queue.TbQueueCallback; |
35 | -import org.thingsboard.server.service.rpc.FromDeviceRpcResponse; | |
36 | 39 | |
37 | 40 | import java.util.UUID; |
38 | 41 | |
... | ... | @@ -54,7 +57,7 @@ public interface TbClusterService { |
54 | 57 | |
55 | 58 | void pushNotificationToTransport(String targetServiceId, ToTransportMsg response, TbQueueCallback callback); |
56 | 59 | |
57 | - void onEntityStateChange(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent state); | |
60 | + void broadcastEntityStateChangeEvent(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent state); | |
58 | 61 | |
59 | 62 | void onDeviceProfileChange(DeviceProfile deviceProfile, TbQueueCallback callback); |
60 | 63 | |
... | ... | @@ -70,7 +73,7 @@ public interface TbClusterService { |
70 | 73 | |
71 | 74 | void onApiStateChange(ApiUsageState apiUsageState, TbQueueCallback callback); |
72 | 75 | |
73 | - void onDeviceChange(Device device, TbQueueCallback callback); | |
76 | + void onDeviceUpdated(Device device, Device old); | |
74 | 77 | |
75 | 78 | void onDeviceDeleted(Device device, TbQueueCallback callback); |
76 | 79 | |
... | ... | @@ -79,4 +82,6 @@ public interface TbClusterService { |
79 | 82 | void onResourceDeleted(TbResource resource, TbQueueCallback callback); |
80 | 83 | |
81 | 84 | void onEdgeEventUpdate(TenantId tenantId, EdgeId edgeId); |
85 | + | |
86 | + void sendNotificationMsgToEdgeService(TenantId tenantId, EdgeId edgeId, EntityId entityId, String body, EdgeEventType type, EdgeEventActionType action); | |
82 | 87 | } | ... | ... |
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueAdmin.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueAdmin.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueCallback.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueCallback.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueConsumer.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueConsumer.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueHandler.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueHandler.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueMsg.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueMsg.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueMsgDecoder.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueMsgDecoder.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueMsgHeaders.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueMsgHeaders.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueMsgMetadata.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueMsgMetadata.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueProducer.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueProducer.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueRequestTemplate.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueRequestTemplate.java
common/cluster-api/src/main/java/org/thingsboard/server/queue/TbQueueResponseTemplate.java
renamed from
common/queue/src/main/java/org/thingsboard/server/queue/TbQueueResponseTemplate.java
common/cluster-api/src/main/proto/jsinvoke.proto
renamed from
common/queue/src/main/proto/jsinvoke.proto
common/cluster-api/src/main/proto/queue.proto
renamed from
common/queue/src/main/proto/queue.proto
... | ... | @@ -54,6 +54,8 @@ public interface DeviceService { |
54 | 54 | |
55 | 55 | Device saveDeviceWithCredentials(Device device, DeviceCredentials deviceCredentials); |
56 | 56 | |
57 | + Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile); | |
58 | + | |
57 | 59 | Device assignDeviceToCustomer(TenantId tenantId, DeviceId deviceId, CustomerId customerId); |
58 | 60 | |
59 | 61 | Device unassignDeviceFromCustomer(TenantId tenantId, DeviceId deviceId); |
... | ... | @@ -98,8 +100,6 @@ public interface DeviceService { |
98 | 100 | |
99 | 101 | Device assignDeviceToTenant(TenantId tenantId, Device device); |
100 | 102 | |
101 | - Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile); | |
102 | - | |
103 | 103 | PageData<UUID> findDevicesIdsByDeviceProfileTransportType(DeviceTransportType transportType, PageLink pageLink); |
104 | 104 | |
105 | 105 | Device assignDeviceToEdge(TenantId tenantId, DeviceId deviceId, EdgeId edgeId); | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/rpc/RpcError.java
renamed from
rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/RpcError.java
... | ... | @@ -13,7 +13,7 @@ |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | -package org.thingsboard.rule.engine.api; | |
16 | +package org.thingsboard.server.common.data.rpc; | |
17 | 17 | |
18 | 18 | /** |
19 | 19 | * @author Andrew Shvayka | ... | ... |
common/message/src/main/java/org/thingsboard/server/common/msg/ToDeviceActorNotificationMsg.java
renamed from
rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/msg/ToDeviceActorNotificationMsg.java
... | ... | @@ -13,9 +13,8 @@ |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | -package org.thingsboard.rule.engine.api.msg; | |
16 | +package org.thingsboard.server.common.msg; | |
17 | 17 | |
18 | -import org.thingsboard.server.common.msg.TbActorMsg; | |
19 | 18 | import org.thingsboard.server.common.msg.aware.DeviceAwareMsg; |
20 | 19 | import org.thingsboard.server.common.msg.aware.TenantAwareMsg; |
21 | 20 | ... | ... |
common/message/src/main/java/org/thingsboard/server/common/msg/rpc/FromDeviceRpcResponse.java
renamed from
application/src/main/java/org/thingsboard/server/service/rpc/FromDeviceRpcResponse.java
... | ... | @@ -13,12 +13,12 @@ |
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | -package org.thingsboard.server.service.rpc; | |
16 | +package org.thingsboard.server.common.msg.rpc; | |
17 | 17 | |
18 | 18 | import lombok.Getter; |
19 | 19 | import lombok.RequiredArgsConstructor; |
20 | 20 | import lombok.ToString; |
21 | -import org.thingsboard.rule.engine.api.RpcError; | |
21 | +import org.thingsboard.server.common.data.rpc.RpcError; | |
22 | 22 | |
23 | 23 | import java.io.Serializable; |
24 | 24 | import java.util.Optional; | ... | ... |
... | ... | @@ -53,6 +53,10 @@ |
53 | 53 | <artifactId>stats</artifactId> |
54 | 54 | </dependency> |
55 | 55 | <dependency> |
56 | + <groupId>org.thingsboard.common</groupId> | |
57 | + <artifactId>cluster-api</artifactId> | |
58 | + </dependency> | |
59 | + <dependency> | |
56 | 60 | <groupId>org.apache.kafka</groupId> |
57 | 61 | <artifactId>kafka-clients</artifactId> |
58 | 62 | </dependency> |
... | ... | @@ -137,13 +141,4 @@ |
137 | 141 | </dependency> |
138 | 142 | </dependencies> |
139 | 143 | |
140 | - <build> | |
141 | - <plugins> | |
142 | - <plugin> | |
143 | - <groupId>org.xolstice.maven.plugins</groupId> | |
144 | - <artifactId>protobuf-maven-plugin</artifactId> | |
145 | - </plugin> | |
146 | - </plugins> | |
147 | - </build> | |
148 | - | |
149 | 144 | </project> | ... | ... |
... | ... | @@ -341,7 +341,7 @@ public class GatewaySessionHandler { |
341 | 341 | for (Map.Entry<String, JsonElement> deviceEntry : jsonObj.entrySet()) { |
342 | 342 | String deviceName = deviceEntry.getKey(); |
343 | 343 | Futures.addCallback(checkDeviceConnected(deviceName), |
344 | - new FutureCallback<GatewayDeviceSessionCtx>() { | |
344 | + new FutureCallback<>() { | |
345 | 345 | @Override |
346 | 346 | public void onSuccess(@Nullable GatewayDeviceSessionCtx deviceCtx) { |
347 | 347 | if (!deviceEntry.getValue().isJsonArray()) { | ... | ... |
... | ... | @@ -881,6 +881,11 @@ |
881 | 881 | <version>${project.version}</version> |
882 | 882 | </dependency> |
883 | 883 | <dependency> |
884 | + <groupId>org.thingsboard.common</groupId> | |
885 | + <artifactId>cluster-api</artifactId> | |
886 | + <version>${project.version}</version> | |
887 | + </dependency> | |
888 | + <dependency> | |
884 | 889 | <groupId>org.thingsboard.rule-engine</groupId> |
885 | 890 | <artifactId>rule-engine-api</artifactId> |
886 | 891 | <version>${project.version}</version> | ... | ... |
... | ... | @@ -50,6 +50,11 @@ |
50 | 50 | </dependency> |
51 | 51 | <dependency> |
52 | 52 | <groupId>org.thingsboard.common</groupId> |
53 | + <artifactId>cluster-api</artifactId> | |
54 | + <scope>provided</scope> | |
55 | + </dependency> | |
56 | + <dependency> | |
57 | + <groupId>org.thingsboard.common</groupId> | |
53 | 58 | <artifactId>util</artifactId> |
54 | 59 | <scope>provided</scope> |
55 | 60 | </dependency> | ... | ... |
... | ... | @@ -18,6 +18,7 @@ package org.thingsboard.rule.engine.api; |
18 | 18 | import io.netty.channel.EventLoopGroup; |
19 | 19 | import org.thingsboard.common.util.ListeningExecutor; |
20 | 20 | import org.thingsboard.rule.engine.api.sms.SmsSenderFactory; |
21 | +import org.thingsboard.server.cluster.TbClusterService; | |
21 | 22 | import org.thingsboard.server.common.data.Customer; |
22 | 23 | import org.thingsboard.server.common.data.Device; |
23 | 24 | import org.thingsboard.server.common.data.DeviceProfile; |
... | ... | @@ -188,6 +189,8 @@ public interface TbContext { |
188 | 189 | |
189 | 190 | DeviceService getDeviceService(); |
190 | 191 | |
192 | + TbClusterService getClusterService(); | |
193 | + | |
191 | 194 | DashboardService getDashboardService(); |
192 | 195 | |
193 | 196 | RuleEngineAlarmService getAlarmService(); | ... | ... |
... | ... | @@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId; |
23 | 23 | import org.thingsboard.server.common.data.kv.AttributeKey; |
24 | 24 | import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
25 | 25 | import org.thingsboard.server.common.msg.MsgType; |
26 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
26 | 27 | |
27 | 28 | import java.util.List; |
28 | 29 | import java.util.Set; | ... | ... |
... | ... | @@ -16,16 +16,11 @@ |
16 | 16 | package org.thingsboard.rule.engine.api.msg; |
17 | 17 | |
18 | 18 | import lombok.Data; |
19 | -import lombok.Getter; | |
20 | -import lombok.ToString; | |
21 | 19 | import org.thingsboard.server.common.data.id.DeviceId; |
22 | 20 | import org.thingsboard.server.common.data.id.TenantId; |
23 | -import org.thingsboard.server.common.data.kv.AttributeKey; | |
24 | 21 | import org.thingsboard.server.common.data.security.DeviceCredentials; |
25 | -import org.thingsboard.server.common.data.security.DeviceCredentialsType; | |
26 | 22 | import org.thingsboard.server.common.msg.MsgType; |
27 | - | |
28 | -import java.util.Set; | |
23 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
29 | 24 | |
30 | 25 | /** |
31 | 26 | * @author Andrew Shvayka | ... | ... |
... | ... | @@ -21,6 +21,7 @@ import org.thingsboard.server.common.data.id.DeviceId; |
21 | 21 | import org.thingsboard.server.common.data.id.EdgeId; |
22 | 22 | import org.thingsboard.server.common.data.id.TenantId; |
23 | 23 | import org.thingsboard.server.common.msg.MsgType; |
24 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
24 | 25 | |
25 | 26 | @Data |
26 | 27 | @AllArgsConstructor | ... | ... |
... | ... | @@ -20,6 +20,7 @@ import lombok.Data; |
20 | 20 | import org.thingsboard.server.common.data.id.DeviceId; |
21 | 21 | import org.thingsboard.server.common.data.id.TenantId; |
22 | 22 | import org.thingsboard.server.common.msg.MsgType; |
23 | +import org.thingsboard.server.common.msg.ToDeviceActorNotificationMsg; | |
23 | 24 | |
24 | 25 | @Data |
25 | 26 | @AllArgsConstructor | ... | ... |
... | ... | @@ -191,6 +191,7 @@ public abstract class TbAbstractRelationActionNode<C extends TbAbstractRelationA |
191 | 191 | newDevice.setType(entitykey.getType()); |
192 | 192 | newDevice.setTenantId(ctx.getTenantId()); |
193 | 193 | Device savedDevice = deviceService.saveDevice(newDevice); |
194 | + ctx.getClusterService().onDeviceUpdated(savedDevice, null); | |
194 | 195 | ctx.enqueue(ctx.deviceCreatedMsg(savedDevice, ctx.getSelfId()), |
195 | 196 | () -> log.trace("Pushed Device Created message: {}", savedDevice), |
196 | 197 | throwable -> log.warn("Failed to push Device Created message: {}", savedDevice, throwable)); |
... | ... | @@ -259,10 +260,10 @@ public abstract class TbAbstractRelationActionNode<C extends TbAbstractRelationA |
259 | 260 | case USER: |
260 | 261 | UserService userService = ctx.getUserService(); |
261 | 262 | User user = userService.findUserByEmail(ctx.getTenantId(), entitykey.getEntityName()); |
262 | - if(user != null){ | |
263 | + if (user != null) { | |
263 | 264 | targetEntity.setEntityId(user.getId()); |
264 | 265 | } |
265 | - break; | |
266 | + break; | |
266 | 267 | default: |
267 | 268 | return targetEntity; |
268 | 269 | } | ... | ... |