Commit 3b909f6e47a2189f2c9b7d12ec3568ec0b90882a

Authored by Volodymyr Babak
2 parents 053d2654 79089ad6

Merge remote-tracking branch 'origin/develop/2.6-edge' into develop/3.3-edge

... ... @@ -132,13 +132,16 @@ public class AssetController extends BaseController {
132 132 try {
133 133 AssetId assetId = new AssetId(toUUID(strAssetId));
134 134 Asset asset = checkAssetId(assetId, Operation.DELETE);
  135 +
  136 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), assetId);
  137 +
135 138 assetService.deleteAsset(getTenantId(), assetId);
136 139
137 140 logEntityAction(assetId, asset,
138 141 asset.getCustomerId(),
139 142 ActionType.DELETED, null, strAssetId);
140 143
141   - sendNotificationMsgToEdgeService(getTenantId(), assetId, EntityType.ASSET, EdgeEventActionType.DELETED);
  144 + sendDeleteNotificationMsgToEdgeService(getTenantId(), assetId, EntityType.ASSET, relatedEdgeIds);
142 145 } catch (Exception e) {
143 146 logEntityAction(emptyId(EntityType.ASSET),
144 147 null,
... ...
... ... @@ -19,6 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
19 19 import com.fasterxml.jackson.databind.ObjectMapper;
20 20 import com.fasterxml.jackson.databind.node.ArrayNode;
21 21 import com.fasterxml.jackson.databind.node.ObjectNode;
  22 +import com.google.common.util.concurrent.ListenableFuture;
22 23 import lombok.Getter;
23 24 import lombok.extern.slf4j.Slf4j;
24 25 import org.apache.commons.lang3.StringUtils;
... ... @@ -935,6 +936,30 @@ public abstract class BaseController {
935 936 }
936 937 }
937 938
  939 + protected List<EdgeId> findRelatedEdgeIds(TenantId tenantId, EntityId entityId) {
  940 + if (!edgesEnabled) {
  941 + return null;
  942 + }
  943 + List<EdgeId> result = null;
  944 + try {
  945 + result = edgeService.findRelatedEdgeIdsByEntityId(tenantId, entityId).get();
  946 + } catch (Exception e) {
  947 + log.error("[{}] can't find related edge ids for entity [{}]", tenantId, entityId, e);
  948 + }
  949 + return result;
  950 + }
  951 +
  952 + protected void sendDeleteNotificationMsgToEdgeService(TenantId tenantId, EntityId entityId, EntityType entityType, List<EdgeId> edgeIds) {
  953 + if (!edgesEnabled) {
  954 + return;
  955 + }
  956 + if (edgeIds != null && !edgeIds.isEmpty()) {
  957 + for (EdgeId edgeId : edgeIds) {
  958 + sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, entityType, EdgeEventActionType.DELETED);
  959 + }
  960 + }
  961 + }
  962 +
938 963 protected void sendNotificationMsgToEdgeService(TenantId tenantId, EntityId entityId, EntityType entityType, EdgeEventActionType action) {
939 964 sendNotificationMsgToEdgeService(tenantId, null, entityId, entityType, action);
940 965 }
... ...
... ... @@ -34,6 +34,7 @@ import org.thingsboard.server.common.data.audit.ActionType;
34 34 import org.thingsboard.server.common.data.edge.EdgeEventActionType;
35 35 import org.thingsboard.server.common.data.exception.ThingsboardException;
36 36 import org.thingsboard.server.common.data.id.CustomerId;
  37 +import org.thingsboard.server.common.data.id.EdgeId;
37 38 import org.thingsboard.server.common.data.id.TenantId;
38 39 import org.thingsboard.server.common.data.page.PageData;
39 40 import org.thingsboard.server.common.data.page.PageLink;
... ... @@ -41,6 +42,8 @@ import org.thingsboard.server.queue.util.TbCoreComponent;
41 42 import org.thingsboard.server.service.security.permission.Operation;
42 43 import org.thingsboard.server.service.security.permission.Resource;
43 44
  45 +import java.util.List;
  46 +
44 47 @RestController
45 48 @TbCoreComponent
46 49 @RequestMapping("/api")
... ... @@ -131,13 +134,16 @@ public class CustomerController extends BaseController {
131 134 try {
132 135 CustomerId customerId = new CustomerId(toUUID(strCustomerId));
133 136 Customer customer = checkCustomerId(customerId, Operation.DELETE);
  137 +
  138 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), customerId);
  139 +
134 140 customerService.deleteCustomer(getTenantId(), customerId);
135 141
136 142 logEntityAction(customerId, customer,
137 143 customer.getId(),
138 144 ActionType.DELETED, null, strCustomerId);
139 145
140   - sendNotificationMsgToEdgeService(getTenantId(), customerId, EntityType.CUSTOMER, EdgeEventActionType.DELETED);
  146 + sendDeleteNotificationMsgToEdgeService(getTenantId(), customerId, EntityType.CUSTOMER, relatedEdgeIds);
141 147 } catch (Exception e) {
142 148
143 149 logEntityAction(emptyId(EntityType.CUSTOMER),
... ...
... ... @@ -47,6 +47,7 @@ import org.thingsboard.server.service.security.permission.Operation;
47 47 import org.thingsboard.server.service.security.permission.Resource;
48 48
49 49 import java.util.HashSet;
  50 +import java.util.List;
50 51 import java.util.Set;
51 52
52 53 @RestController
... ... @@ -136,13 +137,16 @@ public class DashboardController extends BaseController {
136 137 try {
137 138 DashboardId dashboardId = new DashboardId(toUUID(strDashboardId));
138 139 Dashboard dashboard = checkDashboardId(dashboardId, Operation.DELETE);
  140 +
  141 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), dashboardId);
  142 +
139 143 dashboardService.deleteDashboard(getCurrentUser().getTenantId(), dashboardId);
140 144
141 145 logEntityAction(dashboardId, dashboard,
142 146 null,
143 147 ActionType.DELETED, null, strDashboardId);
144 148
145   - sendNotificationMsgToEdgeService(getTenantId(), dashboardId, EntityType.DASHBOARD, EdgeEventActionType.DELETED);
  149 + sendDeleteNotificationMsgToEdgeService(getTenantId(), dashboardId, EntityType.DASHBOARD, relatedEdgeIds);
146 150 } catch (Exception e) {
147 151
148 152 logEntityAction(emptyId(EntityType.DASHBOARD),
... ...
... ... @@ -160,6 +160,9 @@ public class DeviceController extends BaseController {
160 160 try {
161 161 DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
162 162 Device device = checkDeviceId(deviceId, Operation.DELETE);
  163 +
  164 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), deviceId);
  165 +
163 166 deviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId);
164 167
165 168 tbClusterService.onDeviceDeleted(device, null);
... ... @@ -169,7 +172,7 @@ public class DeviceController extends BaseController {
169 172 device.getCustomerId(),
170 173 ActionType.DELETED, null, strDeviceId);
171 174
172   - sendNotificationMsgToEdgeService(getTenantId(), deviceId, EntityType.DEVICE, EdgeEventActionType.DELETED);
  175 + sendDeleteNotificationMsgToEdgeService(getTenantId(), deviceId, EntityType.DEVICE, relatedEdgeIds);
173 176
174 177 deviceStateService.onDeviceDeleted(device);
175 178 } catch (Exception e) {
... ...
... ... @@ -360,11 +360,14 @@ public class EntityViewController extends BaseController {
360 360 try {
361 361 EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId));
362 362 EntityView entityView = checkEntityViewId(entityViewId, Operation.DELETE);
  363 +
  364 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), entityViewId);
  365 +
363 366 entityViewService.deleteEntityView(getTenantId(), entityViewId);
364 367 logEntityAction(entityViewId, entityView, entityView.getCustomerId(),
365 368 ActionType.DELETED, null, strEntityViewId);
366 369
367   - sendNotificationMsgToEdgeService(getTenantId(), entityViewId, EntityType.ENTITY_VIEW, EdgeEventActionType.DELETED);
  370 + sendDeleteNotificationMsgToEdgeService(getTenantId(), entityViewId, EntityType.ENTITY_VIEW, relatedEdgeIds);
368 371 } catch (Exception e) {
369 372 logEntityAction(emptyId(EntityType.ENTITY_VIEW),
370 373 null,
... ...
... ... @@ -305,6 +305,11 @@ public class RuleChainController extends BaseController {
305 305
306 306 Set<RuleChainId> referencingRuleChainIds = referencingRuleNodes.stream().map(RuleNode::getRuleChainId).collect(Collectors.toSet());
307 307
  308 + List<EdgeId> relatedEdgeIds = null;
  309 + if (RuleChainType.EDGE.equals(ruleChain.getType())) {
  310 + relatedEdgeIds = findRelatedEdgeIds(getTenantId(), ruleChainId);
  311 + }
  312 +
308 313 ruleChainService.deleteRuleChainById(getTenantId(), ruleChainId);
309 314
310 315 referencingRuleChainIds.remove(ruleChain.getId());
... ... @@ -321,7 +326,7 @@ public class RuleChainController extends BaseController {
321 326 ActionType.DELETED, null, strRuleChainId);
322 327
323 328 if (RuleChainType.EDGE.equals(ruleChain.getType())) {
324   - sendNotificationMsgToEdgeService(ruleChain.getTenantId(), ruleChain.getId(), EntityType.RULE_CHAIN, EdgeEventActionType.DELETED);
  329 + sendDeleteNotificationMsgToEdgeService(ruleChain.getTenantId(), ruleChain.getId(), EntityType.RULE_CHAIN, relatedEdgeIds);
325 330 }
326 331
327 332 } catch (Exception e) {
... ...
... ... @@ -39,6 +39,7 @@ import org.thingsboard.server.common.data.edge.EdgeEventActionType;
39 39 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
40 40 import org.thingsboard.server.common.data.exception.ThingsboardException;
41 41 import org.thingsboard.server.common.data.id.CustomerId;
  42 +import org.thingsboard.server.common.data.id.EdgeId;
42 43 import org.thingsboard.server.common.data.id.TenantId;
43 44 import org.thingsboard.server.common.data.id.UserId;
44 45 import org.thingsboard.server.common.data.page.PageData;
... ... @@ -54,9 +55,9 @@ import org.thingsboard.server.service.security.model.token.JwtTokenFactory;
54 55 import org.thingsboard.server.service.security.permission.Operation;
55 56 import org.thingsboard.server.service.security.permission.Resource;
56 57 import org.thingsboard.server.service.security.system.SystemSecurityService;
57   -import org.thingsboard.server.utils.MiscUtils;
58 58
59 59 import javax.servlet.http.HttpServletRequest;
  60 +import java.util.List;
60 61
61 62 @RestController
62 63 @TbCoreComponent
... ... @@ -239,13 +240,16 @@ public class UserController extends BaseController {
239 240 try {
240 241 UserId userId = new UserId(toUUID(strUserId));
241 242 User user = checkUserId(userId, Operation.DELETE);
  243 +
  244 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), userId);
  245 +
242 246 userService.deleteUser(getCurrentUser().getTenantId(), userId);
243 247
244 248 logEntityAction(userId, user,
245 249 user.getCustomerId(),
246 250 ActionType.DELETED, null, strUserId);
247 251
248   - sendNotificationMsgToEdgeService(getTenantId(), user.getId(), EntityType.USER, EdgeEventActionType.DELETED);
  252 + sendDeleteNotificationMsgToEdgeService(getTenantId(), userId, EntityType.USER, relatedEdgeIds);
249 253
250 254 } catch (Exception e) {
251 255 logEntityAction(emptyId(EntityType.USER),
... ...
... ... @@ -288,29 +288,28 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
288 288 private void processCustomer(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
289 289 EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction());
290 290 EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
291   - EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type, new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
292   - PageLink pageLink = new PageLink(DEFAULT_LIMIT);
293   - PageData<Edge> pageData;
294   - do {
295   - pageData = edgeService.findEdgesByTenantId(tenantId, pageLink);
296   - if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
297   - for (Edge edge : pageData.getData()) {
298   - switch (actionType) {
299   - case UPDATED:
300   - if (!edge.getCustomerId().isNullUid() && edge.getCustomerId().equals(entityId)) {
301   - saveEdgeEvent(tenantId, edge.getId(), type, actionType, entityId, null);
302   - }
303   - break;
304   - case DELETED:
305   - saveEdgeEvent(tenantId, edge.getId(), type, actionType, entityId, null);
306   - break;
  291 + UUID uuid = new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB());
  292 + CustomerId customerId = new CustomerId(EntityIdFactory.getByEdgeEventTypeAndUuid(type, uuid).getId());
  293 + switch (actionType) {
  294 + case UPDATED:
  295 + PageLink pageLink = new PageLink(DEFAULT_LIMIT);
  296 + PageData<Edge> pageData;
  297 + do {
  298 + pageData = edgeService.findEdgesByTenantIdAndCustomerId(tenantId, customerId, pageLink);
  299 + if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
  300 + for (Edge edge : pageData.getData()) {
  301 + saveEdgeEvent(tenantId, edge.getId(), type, actionType, customerId, null);
  302 + }
  303 + if (pageData.hasNext()) {
  304 + pageLink = pageLink.nextPageLink();
  305 + }
307 306 }
308   - }
309   - if (pageData.hasNext()) {
310   - pageLink = pageLink.nextPageLink();
311   - }
312   - }
313   - } while (pageData != null && pageData.hasNext());
  307 + } while (pageData != null && pageData.hasNext());
  308 + case DELETED:
  309 + EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
  310 + saveEdgeEvent(tenantId, edgeId, type, actionType, customerId, null);
  311 + break;
  312 + }
314 313 }
315 314
316 315 private void processEntity(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
... ... @@ -318,6 +317,7 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
318 317 EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType());
319 318 EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type,
320 319 new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB()));
  320 + EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
321 321 ListenableFuture<List<EdgeId>> edgeIdsFuture;
322 322 switch (actionType) {
323 323 case ADDED: // used only for USER entity
... ... @@ -377,23 +377,10 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
377 377 }, dbCallbackExecutorService);
378 378 break;
379 379 case DELETED:
380   - PageLink pageLink = new PageLink(DEFAULT_LIMIT);
381   - PageData<Edge> pageData;
382   - do {
383   - pageData = edgeService.findEdgesByTenantId(tenantId, pageLink);
384   - if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
385   - for (Edge edge : pageData.getData()) {
386   - saveEdgeEvent(tenantId, edge.getId(), type, actionType, entityId, null);
387   - }
388   - if (pageData.hasNext()) {
389   - pageLink = pageLink.nextPageLink();
390   - }
391   - }
392   - } while (pageData != null && pageData.hasNext());
  380 + saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, null);
393 381 break;
394 382 case ASSIGNED_TO_EDGE:
395 383 case UNASSIGNED_FROM_EDGE:
396   - EdgeId edgeId = new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()));
397 384 saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, null);
398 385 if (type.equals(EdgeEventType.RULE_CHAIN)) {
399 386 updateDependentRuleChains(tenantId, new RuleChainId(entityId.getId()), edgeId);
... ...