Commit 119be4c4bb51466012fe3503191095e85bb62bab

Authored by Volodymyr Babak
1 parent 6d716d1c

Improved DELETE action - push notification only to related edges

... ... @@ -111,13 +111,16 @@ public class AssetController extends BaseController {
111 111 try {
112 112 AssetId assetId = new AssetId(toUUID(strAssetId));
113 113 Asset asset = checkAssetId(assetId, Operation.DELETE);
  114 +
  115 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), assetId);
  116 +
114 117 assetService.deleteAsset(getTenantId(), assetId);
115 118
116 119 logEntityAction(assetId, asset,
117 120 asset.getCustomerId(),
118 121 ActionType.DELETED, null, strAssetId);
119 122
120   - sendNotificationMsgToEdgeService(getTenantId(), assetId, EntityType.ASSET, EdgeEventActionType.DELETED);
  123 + sendDeleteNotificationMsgToEdgeService(getTenantId(), assetId, EntityType.ASSET, relatedEdgeIds);
121 124 } catch (Exception e) {
122 125 logEntityAction(emptyId(EntityType.ASSET),
123 126 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;
... ... @@ -799,6 +800,30 @@ public abstract class BaseController {
799 800 }
800 801 }
801 802
  803 + protected List<EdgeId> findRelatedEdgeIds(TenantId tenantId, EntityId entityId) {
  804 + if (!edgesEnabled) {
  805 + return null;
  806 + }
  807 + List<EdgeId> result = null;
  808 + try {
  809 + result = edgeService.findRelatedEdgeIdsByEntityId(tenantId, entityId).get();
  810 + } catch (Exception e) {
  811 + log.error("[{}] can't find related edge ids for entity [{}]", tenantId, entityId, e);
  812 + }
  813 + return result;
  814 + }
  815 +
  816 + protected void sendDeleteNotificationMsgToEdgeService(TenantId tenantId, EntityId entityId, EntityType entityType, List<EdgeId> edgeIds) {
  817 + if (!edgesEnabled) {
  818 + return;
  819 + }
  820 + if (edgeIds != null && !edgeIds.isEmpty()) {
  821 + for (EdgeId edgeId : edgeIds) {
  822 + sendNotificationMsgToEdgeService(tenantId, edgeId, entityId, entityType, EdgeEventActionType.DELETED);
  823 + }
  824 + }
  825 + }
  826 +
802 827 protected void sendNotificationMsgToEdgeService(TenantId tenantId, EntityId entityId, EntityType entityType, EdgeEventActionType action) {
803 828 sendNotificationMsgToEdgeService(tenantId, null, entityId, entityType, action);
804 829 }
... ...
... ... @@ -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.TextPageData;
39 40 import org.thingsboard.server.common.data.page.TextPageLink;
... ... @@ -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),
... ...
... ... @@ -48,6 +48,7 @@ import org.thingsboard.server.service.security.permission.Operation;
48 48 import org.thingsboard.server.service.security.permission.Resource;
49 49
50 50 import java.util.HashSet;
  51 +import java.util.List;
51 52 import java.util.Set;
52 53
53 54 @RestController
... ... @@ -137,13 +138,16 @@ public class DashboardController extends BaseController {
137 138 try {
138 139 DashboardId dashboardId = new DashboardId(toUUID(strDashboardId));
139 140 Dashboard dashboard = checkDashboardId(dashboardId, Operation.DELETE);
  141 +
  142 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), dashboardId);
  143 +
140 144 dashboardService.deleteDashboard(getCurrentUser().getTenantId(), dashboardId);
141 145
142 146 logEntityAction(dashboardId, dashboard,
143 147 null,
144 148 ActionType.DELETED, null, strDashboardId);
145 149
146   - sendNotificationMsgToEdgeService(getTenantId(), dashboardId, EntityType.DASHBOARD, EdgeEventActionType.DELETED);
  150 + sendDeleteNotificationMsgToEdgeService(getTenantId(), dashboardId, EntityType.DASHBOARD, relatedEdgeIds);
147 151 } catch (Exception e) {
148 152
149 153 logEntityAction(emptyId(EntityType.DASHBOARD),
... ...
... ... @@ -142,13 +142,16 @@ public class DeviceController extends BaseController {
142 142 try {
143 143 DeviceId deviceId = new DeviceId(toUUID(strDeviceId));
144 144 Device device = checkDeviceId(deviceId, Operation.DELETE);
  145 +
  146 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), deviceId);
  147 +
145 148 deviceService.deleteDevice(getCurrentUser().getTenantId(), deviceId);
146 149
147 150 logEntityAction(deviceId, device,
148 151 device.getCustomerId(),
149 152 ActionType.DELETED, null, strDeviceId);
150 153
151   - sendNotificationMsgToEdgeService(getTenantId(), deviceId, EntityType.DEVICE, EdgeEventActionType.DELETED);
  154 + sendDeleteNotificationMsgToEdgeService(getTenantId(), deviceId, EntityType.DEVICE, relatedEdgeIds);
152 155
153 156 deviceStateService.onDeviceDeleted(device);
154 157 } catch (Exception e) {
... ...
... ... @@ -188,11 +188,14 @@ public class EntityViewController extends BaseController {
188 188 try {
189 189 EntityViewId entityViewId = new EntityViewId(toUUID(strEntityViewId));
190 190 EntityView entityView = checkEntityViewId(entityViewId, Operation.DELETE);
  191 +
  192 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), entityViewId);
  193 +
191 194 entityViewService.deleteEntityView(getTenantId(), entityViewId);
192 195 logEntityAction(entityViewId, entityView, entityView.getCustomerId(),
193 196 ActionType.DELETED, null, strEntityViewId);
194 197
195   - sendNotificationMsgToEdgeService(getTenantId(), entityViewId, EntityType.ENTITY_VIEW, EdgeEventActionType.DELETED);
  198 + sendDeleteNotificationMsgToEdgeService(getTenantId(), entityViewId, EntityType.ENTITY_VIEW, relatedEdgeIds);
196 199 } catch (Exception e) {
197 200 logEntityAction(emptyId(EntityType.ENTITY_VIEW),
198 201 null,
... ...
... ... @@ -274,6 +274,11 @@ public class RuleChainController extends BaseController {
274 274
275 275 Set<RuleChainId> referencingRuleChainIds = referencingRuleNodes.stream().map(RuleNode::getRuleChainId).collect(Collectors.toSet());
276 276
  277 + List<EdgeId> relatedEdgeIds = null;
  278 + if (RuleChainType.EDGE.equals(ruleChain.getType())) {
  279 + relatedEdgeIds = findRelatedEdgeIds(getTenantId(), ruleChainId);
  280 + }
  281 +
277 282 ruleChainService.deleteRuleChainById(getTenantId(), ruleChainId);
278 283
279 284 referencingRuleChainIds.remove(ruleChain.getId());
... ... @@ -290,7 +295,7 @@ public class RuleChainController extends BaseController {
290 295 ActionType.DELETED, null, strRuleChainId);
291 296
292 297 if (RuleChainType.EDGE.equals(ruleChain.getType())) {
293   - sendNotificationMsgToEdgeService(ruleChain.getTenantId(), ruleChain.getId(), EntityType.RULE_CHAIN, EdgeEventActionType.DELETED);
  298 + sendDeleteNotificationMsgToEdgeService(ruleChain.getTenantId(), ruleChain.getId(), EntityType.RULE_CHAIN, relatedEdgeIds);
294 299 }
295 300
296 301 } 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.TextPageData;
... ... @@ -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
... ... @@ -238,13 +239,16 @@ public class UserController extends BaseController {
238 239 try {
239 240 UserId userId = new UserId(toUUID(strUserId));
240 241 User user = checkUserId(userId, Operation.DELETE);
  242 +
  243 + List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(getTenantId(), userId);
  244 +
241 245 userService.deleteUser(getCurrentUser().getTenantId(), userId);
242 246
243 247 logEntityAction(userId, user,
244 248 user.getCustomerId(),
245 249 ActionType.DELETED, null, strUserId);
246 250
247   - sendNotificationMsgToEdgeService(getTenantId(), user.getId(), EntityType.USER, EdgeEventActionType.DELETED);
  251 + sendDeleteNotificationMsgToEdgeService(getTenantId(), userId, EntityType.USER, relatedEdgeIds);
248 252
249 253 } catch (Exception e) {
250 254 logEntityAction(emptyId(EntityType.USER),
... ...
... ... @@ -26,7 +26,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
26 26 import org.springframework.web.bind.annotation.ResponseStatus;
27 27 import org.springframework.web.bind.annotation.RestController;
28 28 import org.thingsboard.server.common.data.EntityType;
29   -import org.thingsboard.server.common.data.audit.ActionType;
30 29 import org.thingsboard.server.common.data.edge.EdgeEventActionType;
31 30 import org.thingsboard.server.common.data.exception.ThingsboardException;
32 31 import org.thingsboard.server.common.data.id.TenantId;
... ...
... ... @@ -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   - TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT);
293   - TextPageData<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 + TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT);
  296 + TextPageData<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 = pageData.getNextPageLink();
  305 + }
307 306 }
308   - }
309   - if (pageData.hasNext()) {
310   - pageLink = pageData.getNextPageLink();
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   - TextPageLink pageLink = new TextPageLink(DEFAULT_LIMIT);
381   - TextPageData<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 = pageData.getNextPageLink();
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, new TimePageLink(DEFAULT_LIMIT));
... ...