|
|
|
@ -311,7 +311,9 @@ public abstract class BaseEdgeProcessor { |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
protected ListenableFuture<Void> processActionForAllEdges(TenantId tenantId, EdgeEventType type, EdgeEventActionType actionType, EntityId entityId) { |
|
|
|
protected ListenableFuture<Void> processActionForAllEdges(TenantId tenantId, EdgeEventType type, |
|
|
|
EdgeEventActionType actionType, EntityId entityId, |
|
|
|
EdgeId sourceEdgeId) { |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
if (TenantId.SYS_TENANT_ID.equals(tenantId)) { |
|
|
|
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|
|
|
@ -319,21 +321,22 @@ public abstract class BaseEdgeProcessor { |
|
|
|
do { |
|
|
|
tenantsIds = tenantService.findTenantsIds(pageLink); |
|
|
|
for (TenantId tenantId1 : tenantsIds.getData()) { |
|
|
|
futures.addAll(processActionForAllEdgesByTenantId(tenantId1, type, actionType, entityId, null)); |
|
|
|
futures.addAll(processActionForAllEdgesByTenantId(tenantId1, type, actionType, entityId, null, sourceEdgeId)); |
|
|
|
} |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
} while (tenantsIds.hasNext()); |
|
|
|
} else { |
|
|
|
futures = processActionForAllEdgesByTenantId(tenantId, type, actionType, entityId, null); |
|
|
|
futures = processActionForAllEdgesByTenantId(tenantId, type, actionType, entityId, null, sourceEdgeId); |
|
|
|
} |
|
|
|
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
private List<ListenableFuture<Void>> processActionForAllEdgesByTenantId(TenantId tenantId, |
|
|
|
EdgeEventType type, |
|
|
|
EdgeEventActionType actionType, |
|
|
|
EntityId entityId, |
|
|
|
JsonNode body) { |
|
|
|
EdgeEventType type, |
|
|
|
EdgeEventActionType actionType, |
|
|
|
EntityId entityId, |
|
|
|
JsonNode body, |
|
|
|
EdgeId sourceEdgeId) { |
|
|
|
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|
|
|
PageData<Edge> pageData; |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
@ -341,7 +344,9 @@ public abstract class BaseEdgeProcessor { |
|
|
|
pageData = edgeService.findEdgesByTenantId(tenantId, pageLink); |
|
|
|
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) { |
|
|
|
for (Edge edge : pageData.getData()) { |
|
|
|
futures.add(saveEdgeEvent(tenantId, edge.getId(), type, actionType, entityId, body)); |
|
|
|
if (!edge.getId().equals(sourceEdgeId)) { |
|
|
|
futures.add(saveEdgeEvent(tenantId, edge.getId(), type, actionType, entityId, body)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
@ -385,11 +390,12 @@ public abstract class BaseEdgeProcessor { |
|
|
|
EdgeEventType type = EdgeEventType.valueOf(edgeNotificationMsg.getType()); |
|
|
|
EdgeEventActionType actionType = EdgeEventActionType.valueOf(edgeNotificationMsg.getAction()); |
|
|
|
EntityId entityId = EntityIdFactory.getByEdgeEventTypeAndUuid(type, new UUID(edgeNotificationMsg.getEntityIdMSB(), edgeNotificationMsg.getEntityIdLSB())); |
|
|
|
EdgeId sourceEdgeId = safeGetEdgeId(edgeNotificationMsg.getSourceEdgeIdMSB(), edgeNotificationMsg.getSourceEdgeIdLSB()); |
|
|
|
if (type.isAllEdgesRelated()) { |
|
|
|
return processEntityNotificationForAllEdges(tenantId, type, actionType, entityId); |
|
|
|
return processEntityNotificationForAllEdges(tenantId, type, actionType, entityId, sourceEdgeId); |
|
|
|
} else { |
|
|
|
JsonNode body = JacksonUtil.toJsonNode(edgeNotificationMsg.getBody()); |
|
|
|
EdgeId edgeId = safeGetEdgeId(edgeNotificationMsg); |
|
|
|
EdgeId edgeId = safeGetEdgeId(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB()); |
|
|
|
switch (actionType) { |
|
|
|
case UPDATED: |
|
|
|
case CREDENTIALS_UPDATED: |
|
|
|
@ -398,41 +404,44 @@ public abstract class BaseEdgeProcessor { |
|
|
|
if (edgeId != null) { |
|
|
|
return saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, body); |
|
|
|
} else { |
|
|
|
return processNotificationToRelatedEdges(tenantId, entityId, type, actionType); |
|
|
|
return processNotificationToRelatedEdges(tenantId, entityId, type, actionType, sourceEdgeId); |
|
|
|
} |
|
|
|
case DELETED: |
|
|
|
EdgeEventActionType deleted = EdgeEventActionType.DELETED; |
|
|
|
if (edgeId != null) { |
|
|
|
return saveEdgeEvent(tenantId, edgeId, type, deleted, entityId, body); |
|
|
|
} else { |
|
|
|
return Futures.transform(Futures.allAsList(processActionForAllEdgesByTenantId(tenantId, type, deleted, entityId, body)), |
|
|
|
return Futures.transform(Futures.allAsList(processActionForAllEdgesByTenantId(tenantId, type, deleted, entityId, body, sourceEdgeId)), |
|
|
|
voids -> null, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
case ASSIGNED_TO_EDGE: |
|
|
|
case UNASSIGNED_FROM_EDGE: |
|
|
|
ListenableFuture<Void> future = saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, body); |
|
|
|
return Futures.transformAsync(future, unused -> { |
|
|
|
if (type.equals(EdgeEventType.RULE_CHAIN)) { |
|
|
|
return updateDependentRuleChains(tenantId, new RuleChainId(entityId.getId()), edgeId); |
|
|
|
} else { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
if (sourceEdgeId == null) { |
|
|
|
ListenableFuture<Void> future = saveEdgeEvent(tenantId, edgeId, type, actionType, entityId, body); |
|
|
|
return Futures.transformAsync(future, unused -> { |
|
|
|
if (type.equals(EdgeEventType.RULE_CHAIN)) { |
|
|
|
return updateDependentRuleChains(tenantId, new RuleChainId(entityId.getId()), edgeId); |
|
|
|
} else { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
default: |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private EdgeId safeGetEdgeId(TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) { |
|
|
|
if (edgeNotificationMsg.getEdgeIdMSB() != 0 && edgeNotificationMsg.getEdgeIdLSB() != 0) { |
|
|
|
return new EdgeId(new UUID(edgeNotificationMsg.getEdgeIdMSB(), edgeNotificationMsg.getEdgeIdLSB())); |
|
|
|
protected EdgeId safeGetEdgeId(long edgeIdMSB, long edgeIdLSB) { |
|
|
|
if (edgeIdMSB != 0 && edgeIdLSB != 0) { |
|
|
|
return new EdgeId(new UUID(edgeIdMSB, edgeIdLSB)); |
|
|
|
} else { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private ListenableFuture<Void> processNotificationToRelatedEdges(TenantId tenantId, EntityId entityId, EdgeEventType type, EdgeEventActionType actionType) { |
|
|
|
private ListenableFuture<Void> processNotificationToRelatedEdges(TenantId tenantId, EntityId entityId, EdgeEventType type, |
|
|
|
EdgeEventActionType actionType, EdgeId sourceEdgeId) { |
|
|
|
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|
|
|
PageData<EdgeId> pageData; |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
@ -440,7 +449,9 @@ public abstract class BaseEdgeProcessor { |
|
|
|
pageData = edgeService.findRelatedEdgeIdsByEntityId(tenantId, entityId, pageLink); |
|
|
|
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) { |
|
|
|
for (EdgeId relatedEdgeId : pageData.getData()) { |
|
|
|
futures.add(saveEdgeEvent(tenantId, relatedEdgeId, type, actionType, entityId, null)); |
|
|
|
if (!relatedEdgeId.equals(sourceEdgeId)) { |
|
|
|
futures.add(saveEdgeEvent(tenantId, relatedEdgeId, type, actionType, entityId, null)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
@ -483,13 +494,13 @@ public abstract class BaseEdgeProcessor { |
|
|
|
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
private ListenableFuture<Void> processEntityNotificationForAllEdges(TenantId tenantId, EdgeEventType type, EdgeEventActionType actionType, EntityId entityId) { |
|
|
|
private ListenableFuture<Void> processEntityNotificationForAllEdges(TenantId tenantId, EdgeEventType type, EdgeEventActionType actionType, EntityId entityId, EdgeId sourceEdgeId) { |
|
|
|
switch (actionType) { |
|
|
|
case ADDED: |
|
|
|
case UPDATED: |
|
|
|
case DELETED: |
|
|
|
case CREDENTIALS_UPDATED: // used by USER entity
|
|
|
|
return processActionForAllEdges(tenantId, type, actionType, entityId); |
|
|
|
return processActionForAllEdges(tenantId, type, actionType, entityId, sourceEdgeId); |
|
|
|
default: |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
|