|
|
|
@ -26,6 +26,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.DeviceProfile; |
|
|
|
import org.thingsboard.server.common.data.EdgeUtils; |
|
|
|
@ -72,7 +73,6 @@ import org.thingsboard.server.gen.edge.v1.RuleChainMetadataRequestMsg; |
|
|
|
import org.thingsboard.server.gen.edge.v1.UserCredentialsRequestMsg; |
|
|
|
import org.thingsboard.server.gen.edge.v1.WidgetBundleTypesRequestMsg; |
|
|
|
import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
@ -121,13 +121,13 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
@Override |
|
|
|
public ListenableFuture<Void> processRuleChainMetadataRequestMsg(TenantId tenantId, Edge edge, RuleChainMetadataRequestMsg ruleChainMetadataRequestMsg) { |
|
|
|
log.trace("[{}] processRuleChainMetadataRequestMsg [{}][{}]", tenantId, edge.getName(), ruleChainMetadataRequestMsg); |
|
|
|
if (ruleChainMetadataRequestMsg.getRuleChainIdMSB() != 0 && ruleChainMetadataRequestMsg.getRuleChainIdLSB() != 0) { |
|
|
|
RuleChainId ruleChainId = |
|
|
|
new RuleChainId(new UUID(ruleChainMetadataRequestMsg.getRuleChainIdMSB(), ruleChainMetadataRequestMsg.getRuleChainIdLSB())); |
|
|
|
saveEdgeEvent(tenantId, edge.getId(), |
|
|
|
EdgeEventType.RULE_CHAIN_METADATA, EdgeEventActionType.ADDED, ruleChainId, null); |
|
|
|
if (ruleChainMetadataRequestMsg.getRuleChainIdMSB() == 0 || ruleChainMetadataRequestMsg.getRuleChainIdLSB() == 0) { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
RuleChainId ruleChainId = |
|
|
|
new RuleChainId(new UUID(ruleChainMetadataRequestMsg.getRuleChainIdMSB(), ruleChainMetadataRequestMsg.getRuleChainIdLSB())); |
|
|
|
return saveEdgeEvent(tenantId, edge.getId(), |
|
|
|
EdgeEventType.RULE_CHAIN_METADATA, EdgeEventActionType.ADDED, ruleChainId, null); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -137,63 +137,72 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
EntityType.valueOf(attributesRequestMsg.getEntityType()), |
|
|
|
new UUID(attributesRequestMsg.getEntityIdMSB(), attributesRequestMsg.getEntityIdLSB())); |
|
|
|
final EdgeEventType type = EdgeUtils.getEdgeEventTypeByEntityType(entityId.getEntityType()); |
|
|
|
if (type != null) { |
|
|
|
SettableFuture<Void> futureToSet = SettableFuture.create(); |
|
|
|
String scope = attributesRequestMsg.getScope(); |
|
|
|
ListenableFuture<List<AttributeKvEntry>> findAttrFuture = attributesService.findAll(tenantId, entityId, scope); |
|
|
|
Futures.addCallback(findAttrFuture, new FutureCallback<List<AttributeKvEntry>>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<AttributeKvEntry> ssAttributes) { |
|
|
|
if (ssAttributes != null && !ssAttributes.isEmpty()) { |
|
|
|
try { |
|
|
|
Map<String, Object> entityData = new HashMap<>(); |
|
|
|
ObjectNode attributes = mapper.createObjectNode(); |
|
|
|
for (AttributeKvEntry attr : ssAttributes) { |
|
|
|
if (attr.getDataType() == DataType.BOOLEAN && attr.getBooleanValue().isPresent()) { |
|
|
|
attributes.put(attr.getKey(), attr.getBooleanValue().get()); |
|
|
|
} else if (attr.getDataType() == DataType.DOUBLE && attr.getDoubleValue().isPresent()) { |
|
|
|
attributes.put(attr.getKey(), attr.getDoubleValue().get()); |
|
|
|
} else if (attr.getDataType() == DataType.LONG && attr.getLongValue().isPresent()) { |
|
|
|
attributes.put(attr.getKey(), attr.getLongValue().get()); |
|
|
|
} else { |
|
|
|
attributes.put(attr.getKey(), attr.getValueAsString()); |
|
|
|
} |
|
|
|
} |
|
|
|
entityData.put("kv", attributes); |
|
|
|
entityData.put("scope", scope); |
|
|
|
JsonNode body = mapper.valueToTree(entityData); |
|
|
|
log.debug("Sending attributes data msg, entityId [{}], attributes [{}]", entityId, body); |
|
|
|
saveEdgeEvent(tenantId, |
|
|
|
edge.getId(), |
|
|
|
type, |
|
|
|
EdgeEventActionType.ATTRIBUTES_UPDATED, |
|
|
|
entityId, |
|
|
|
body); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("[{}] Failed to save attribute updates to the edge", edge.getName(), e); |
|
|
|
futureToSet.setException(new RuntimeException("[" + edge.getName() + "] Failed to send attribute updates to the edge", e)); |
|
|
|
return; |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.trace("[{}][{}] No attributes found for entity {} [{}]", tenantId, |
|
|
|
edge.getName(), |
|
|
|
entityId.getEntityType(), |
|
|
|
entityId.getId()); |
|
|
|
} |
|
|
|
if (type == null) { |
|
|
|
log.warn("[{}] Type doesn't supported {}", tenantId, entityId.getEntityType()); |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
SettableFuture<Void> futureToSet = SettableFuture.create(); |
|
|
|
String scope = attributesRequestMsg.getScope(); |
|
|
|
ListenableFuture<List<AttributeKvEntry>> findAttrFuture = attributesService.findAll(tenantId, entityId, scope); |
|
|
|
Futures.addCallback(findAttrFuture, new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<AttributeKvEntry> ssAttributes) { |
|
|
|
if (ssAttributes == null || ssAttributes.isEmpty()) { |
|
|
|
log.trace("[{}][{}] No attributes found for entity {} [{}]", tenantId, |
|
|
|
edge.getName(), |
|
|
|
entityId.getEntityType(), |
|
|
|
entityId.getId()); |
|
|
|
futureToSet.set(null); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
log.error("Can't find attributes [{}]", attributesRequestMsg, t); |
|
|
|
futureToSet.setException(t); |
|
|
|
try { |
|
|
|
Map<String, Object> entityData = new HashMap<>(); |
|
|
|
ObjectNode attributes = mapper.createObjectNode(); |
|
|
|
for (AttributeKvEntry attr : ssAttributes) { |
|
|
|
if (attr.getDataType() == DataType.BOOLEAN && attr.getBooleanValue().isPresent()) { |
|
|
|
attributes.put(attr.getKey(), attr.getBooleanValue().get()); |
|
|
|
} else if (attr.getDataType() == DataType.DOUBLE && attr.getDoubleValue().isPresent()) { |
|
|
|
attributes.put(attr.getKey(), attr.getDoubleValue().get()); |
|
|
|
} else if (attr.getDataType() == DataType.LONG && attr.getLongValue().isPresent()) { |
|
|
|
attributes.put(attr.getKey(), attr.getLongValue().get()); |
|
|
|
} else { |
|
|
|
attributes.put(attr.getKey(), attr.getValueAsString()); |
|
|
|
} |
|
|
|
} |
|
|
|
entityData.put("kv", attributes); |
|
|
|
entityData.put("scope", scope); |
|
|
|
JsonNode body = mapper.valueToTree(entityData); |
|
|
|
log.debug("Sending attributes data msg, entityId [{}], attributes [{}]", entityId, body); |
|
|
|
ListenableFuture<Void> future = saveEdgeEvent(tenantId, edge.getId(), type, EdgeEventActionType.ATTRIBUTES_UPDATED, entityId, body); |
|
|
|
Futures.addCallback(future, new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable Void unused) { |
|
|
|
futureToSet.set(null); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable throwable) { |
|
|
|
String errMsg = String.format("[%s] Failed to save edge event [%s]", edge.getId(), attributesRequestMsg); |
|
|
|
log.error(errMsg, throwable); |
|
|
|
futureToSet.setException(new RuntimeException(errMsg, throwable)); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} catch (Exception e) { |
|
|
|
String errMsg = String.format("[%s] Failed to save attribute updates to the edge [%s]", edge.getId(), attributesRequestMsg); |
|
|
|
log.error(errMsg, e); |
|
|
|
futureToSet.setException(new RuntimeException(errMsg, e)); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
return futureToSet; |
|
|
|
} else { |
|
|
|
log.warn("[{}] Type doesn't supported {}", tenantId, entityId.getEntityType()); |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
String errMsg = String.format("[%s] Can't find attributes [%s]", edge.getId(), attributesRequestMsg); |
|
|
|
log.error(errMsg, t); |
|
|
|
futureToSet.setException(new RuntimeException(errMsg, t)); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
return futureToSet; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -208,33 +217,49 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
futures.add(findRelationByQuery(tenantId, edge, entityId, EntitySearchDirection.TO)); |
|
|
|
ListenableFuture<List<List<EntityRelation>>> relationsListFuture = Futures.allAsList(futures); |
|
|
|
SettableFuture<Void> futureToSet = SettableFuture.create(); |
|
|
|
Futures.addCallback(relationsListFuture, new FutureCallback<List<List<EntityRelation>>>() { |
|
|
|
Futures.addCallback(relationsListFuture, new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<List<EntityRelation>> relationsList) { |
|
|
|
try { |
|
|
|
if (relationsList != null && !relationsList.isEmpty()) { |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
for (List<EntityRelation> entityRelations : relationsList) { |
|
|
|
log.trace("[{}] [{}] [{}] relation(s) are going to be pushed to edge.", edge.getId(), entityId, entityRelations.size()); |
|
|
|
for (EntityRelation relation : entityRelations) { |
|
|
|
try { |
|
|
|
if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) && |
|
|
|
!relation.getTo().getEntityType().equals(EntityType.EDGE)) { |
|
|
|
saveEdgeEvent(tenantId, |
|
|
|
futures.add(saveEdgeEvent(tenantId, |
|
|
|
edge.getId(), |
|
|
|
EdgeEventType.RELATION, |
|
|
|
EdgeEventActionType.ADDED, |
|
|
|
null, |
|
|
|
mapper.valueToTree(relation)); |
|
|
|
mapper.valueToTree(relation))); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Exception during loading relation [{}] to edge on sync!", relation, e); |
|
|
|
futureToSet.setException(e); |
|
|
|
String errMsg = String.format("[%s] Exception during loading relation [%s] to edge on sync!", edge.getId(), relation); |
|
|
|
log.error(errMsg, e); |
|
|
|
futureToSet.setException(new RuntimeException(errMsg, e)); |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Futures.addCallback(Futures.allAsList(futures), new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<Void> voids) { |
|
|
|
futureToSet.set(null); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable throwable) { |
|
|
|
String errMsg = String.format("[%s] Exception during saving edge events [%s]!", edge.getId(), relationRequestMsg); |
|
|
|
log.error(errMsg, throwable); |
|
|
|
futureToSet.setException(new RuntimeException(errMsg, throwable)); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} else { |
|
|
|
futureToSet.set(null); |
|
|
|
} |
|
|
|
futureToSet.set(null); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Exception during loading relation(s) to edge on sync!", e); |
|
|
|
futureToSet.setException(e); |
|
|
|
@ -243,8 +268,9 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
log.error("[{}] Can't find relation by query. Entity id [{}]", tenantId, entityId, t); |
|
|
|
futureToSet.setException(t); |
|
|
|
String errMsg = String.format("[%s] Can't find relation by query. Entity id [%s]!", tenantId, entityId); |
|
|
|
log.error(errMsg, t); |
|
|
|
futureToSet.setException(new RuntimeException(errMsg, t)); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
return futureToSet; |
|
|
|
@ -260,40 +286,42 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
@Override |
|
|
|
public ListenableFuture<Void> processDeviceCredentialsRequestMsg(TenantId tenantId, Edge edge, DeviceCredentialsRequestMsg deviceCredentialsRequestMsg) { |
|
|
|
log.trace("[{}] processDeviceCredentialsRequestMsg [{}][{}]", tenantId, edge.getName(), deviceCredentialsRequestMsg); |
|
|
|
if (deviceCredentialsRequestMsg.getDeviceIdMSB() != 0 && deviceCredentialsRequestMsg.getDeviceIdLSB() != 0) { |
|
|
|
DeviceId deviceId = new DeviceId(new UUID(deviceCredentialsRequestMsg.getDeviceIdMSB(), deviceCredentialsRequestMsg.getDeviceIdLSB())); |
|
|
|
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, |
|
|
|
EdgeEventActionType.CREDENTIALS_UPDATED, deviceId, null); |
|
|
|
if (deviceCredentialsRequestMsg.getDeviceIdMSB() == 0 || deviceCredentialsRequestMsg.getDeviceIdLSB() == 0) { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
DeviceId deviceId = new DeviceId(new UUID(deviceCredentialsRequestMsg.getDeviceIdMSB(), deviceCredentialsRequestMsg.getDeviceIdLSB())); |
|
|
|
return saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, |
|
|
|
EdgeEventActionType.CREDENTIALS_UPDATED, deviceId, null); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<Void> processUserCredentialsRequestMsg(TenantId tenantId, Edge edge, UserCredentialsRequestMsg userCredentialsRequestMsg) { |
|
|
|
log.trace("[{}] processUserCredentialsRequestMsg [{}][{}]", tenantId, edge.getName(), userCredentialsRequestMsg); |
|
|
|
if (userCredentialsRequestMsg.getUserIdMSB() != 0 && userCredentialsRequestMsg.getUserIdLSB() != 0) { |
|
|
|
UserId userId = new UserId(new UUID(userCredentialsRequestMsg.getUserIdMSB(), userCredentialsRequestMsg.getUserIdLSB())); |
|
|
|
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.USER, |
|
|
|
EdgeEventActionType.CREDENTIALS_UPDATED, userId, null); |
|
|
|
if (userCredentialsRequestMsg.getUserIdMSB() == 0 || userCredentialsRequestMsg.getUserIdLSB() == 0) { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
UserId userId = new UserId(new UUID(userCredentialsRequestMsg.getUserIdMSB(), userCredentialsRequestMsg.getUserIdLSB())); |
|
|
|
return saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.USER, |
|
|
|
EdgeEventActionType.CREDENTIALS_UPDATED, userId, null); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<Void> processDeviceProfileDevicesRequestMsg(TenantId tenantId, Edge edge, DeviceProfileDevicesRequestMsg deviceProfileDevicesRequestMsg) { |
|
|
|
log.trace("[{}] processDeviceProfileDevicesRequestMsg [{}][{}]", tenantId, edge.getName(), deviceProfileDevicesRequestMsg); |
|
|
|
if (deviceProfileDevicesRequestMsg.getDeviceProfileIdMSB() != 0 && deviceProfileDevicesRequestMsg.getDeviceProfileIdLSB() != 0) { |
|
|
|
DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(deviceProfileDevicesRequestMsg.getDeviceProfileIdMSB(), deviceProfileDevicesRequestMsg.getDeviceProfileIdLSB())); |
|
|
|
DeviceProfile deviceProfileById = deviceProfileService.findDeviceProfileById(tenantId, deviceProfileId); |
|
|
|
if (deviceProfileById != null) { |
|
|
|
syncDevices(tenantId, edge, deviceProfileById.getName()); |
|
|
|
} |
|
|
|
if (deviceProfileDevicesRequestMsg.getDeviceProfileIdMSB() == 0 || deviceProfileDevicesRequestMsg.getDeviceProfileIdLSB() == 0) { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
DeviceProfileId deviceProfileId = new DeviceProfileId(new UUID(deviceProfileDevicesRequestMsg.getDeviceProfileIdMSB(), deviceProfileDevicesRequestMsg.getDeviceProfileIdLSB())); |
|
|
|
DeviceProfile deviceProfileById = deviceProfileService.findDeviceProfileById(tenantId, deviceProfileId); |
|
|
|
if (deviceProfileById == null) { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
return syncDevices(tenantId, edge, deviceProfileById.getName()); |
|
|
|
} |
|
|
|
|
|
|
|
private void syncDevices(TenantId tenantId, Edge edge, String deviceType) { |
|
|
|
private ListenableFuture<Void> syncDevices(TenantId tenantId, Edge edge, String deviceType) { |
|
|
|
log.trace("[{}] syncDevices [{}][{}]", tenantId, edge.getName(), deviceType); |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
try { |
|
|
|
PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE); |
|
|
|
PageData<Device> pageData; |
|
|
|
@ -302,7 +330,7 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) { |
|
|
|
log.trace("[{}] [{}] device(s) are going to be pushed to edge.", edge.getId(), pageData.getData().size()); |
|
|
|
for (Device device : pageData.getData()) { |
|
|
|
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, EdgeEventActionType.ADDED, device.getId(), null); |
|
|
|
futures.add(saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.DEVICE, EdgeEventActionType.ADDED, device.getId(), null)); |
|
|
|
} |
|
|
|
if (pageData.hasNext()) { |
|
|
|
pageLink = pageLink.nextPageLink(); |
|
|
|
@ -312,25 +340,26 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Exception during loading edge device(s) on sync!", e); |
|
|
|
} |
|
|
|
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public ListenableFuture<Void> processWidgetBundleTypesRequestMsg(TenantId tenantId, Edge edge, |
|
|
|
WidgetBundleTypesRequestMsg widgetBundleTypesRequestMsg) { |
|
|
|
log.trace("[{}] processWidgetBundleTypesRequestMsg [{}][{}]", tenantId, edge.getName(), widgetBundleTypesRequestMsg); |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
if (widgetBundleTypesRequestMsg.getWidgetBundleIdMSB() != 0 && widgetBundleTypesRequestMsg.getWidgetBundleIdLSB() != 0) { |
|
|
|
WidgetsBundleId widgetsBundleId = new WidgetsBundleId(new UUID(widgetBundleTypesRequestMsg.getWidgetBundleIdMSB(), widgetBundleTypesRequestMsg.getWidgetBundleIdLSB())); |
|
|
|
WidgetsBundle widgetsBundleById = widgetsBundleService.findWidgetsBundleById(tenantId, widgetsBundleId); |
|
|
|
if (widgetsBundleById != null) { |
|
|
|
List<WidgetType> widgetTypesToPush = |
|
|
|
widgetTypeService.findWidgetTypesByTenantIdAndBundleAlias(widgetsBundleById.getTenantId(), widgetsBundleById.getAlias()); |
|
|
|
|
|
|
|
for (WidgetType widgetType : widgetTypesToPush) { |
|
|
|
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.WIDGET_TYPE, EdgeEventActionType.ADDED, widgetType.getId(), null); |
|
|
|
futures.add(saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.WIDGET_TYPE, EdgeEventActionType.ADDED, widgetType.getId(), null)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
return Futures.transform(Futures.allAsList(futures), voids -> null, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -343,46 +372,35 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
Futures.addCallback(entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(tenantId, entityId), new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<EntityView> entityViews) { |
|
|
|
try { |
|
|
|
if (entityViews != null && !entityViews.isEmpty()) { |
|
|
|
List<ListenableFuture<Boolean>> futures = new ArrayList<>(); |
|
|
|
for (EntityView entityView : entityViews) { |
|
|
|
ListenableFuture<Boolean> future = relationService.checkRelation(tenantId, edge.getId(), entityView.getId(), |
|
|
|
EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE); |
|
|
|
futures.add(future); |
|
|
|
Futures.addCallback(future, new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable Boolean result) { |
|
|
|
if (Boolean.TRUE.equals(result)) { |
|
|
|
saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.ENTITY_VIEW, |
|
|
|
EdgeEventActionType.ADDED, entityView.getId(), null); |
|
|
|
} |
|
|
|
} |
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
// Do nothing - error handles in allAsList
|
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
if (entityViews == null || entityViews.isEmpty()) { |
|
|
|
futureToSet.set(null); |
|
|
|
return; |
|
|
|
} |
|
|
|
List<ListenableFuture<Void>> futures = new ArrayList<>(); |
|
|
|
for (EntityView entityView : entityViews) { |
|
|
|
ListenableFuture<Boolean> future = relationService.checkRelation(tenantId, edge.getId(), entityView.getId(), |
|
|
|
EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE); |
|
|
|
futures.add(Futures.transformAsync(future, result -> { |
|
|
|
if (Boolean.TRUE.equals(result)) { |
|
|
|
return saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.ENTITY_VIEW, |
|
|
|
EdgeEventActionType.ADDED, entityView.getId(), null); |
|
|
|
} else { |
|
|
|
return Futures.immediateFuture(null); |
|
|
|
} |
|
|
|
Futures.addCallback(Futures.allAsList(futures), new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<Boolean> result) { |
|
|
|
futureToSet.set(null); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
log.error("Exception during loading relation [{}] to edge on sync!", t, t); |
|
|
|
futureToSet.setException(t); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} else { |
|
|
|
}, dbCallbackExecutorService)); |
|
|
|
} |
|
|
|
Futures.addCallback(Futures.allAsList(futures), new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable List<Void> result) { |
|
|
|
futureToSet.set(null); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("Exception during loading relation(s) to edge on sync!", e); |
|
|
|
futureToSet.setException(e); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
log.error("Exception during loading relation to edge on sync!", t); |
|
|
|
futureToSet.setException(t); |
|
|
|
} |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -394,7 +412,7 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
return futureToSet; |
|
|
|
} |
|
|
|
|
|
|
|
private void saveEdgeEvent(TenantId tenantId, |
|
|
|
private ListenableFuture<Void> saveEdgeEvent(TenantId tenantId, |
|
|
|
EdgeId edgeId, |
|
|
|
EdgeEventType type, |
|
|
|
EdgeEventActionType action, |
|
|
|
@ -405,17 +423,9 @@ public class DefaultEdgeRequestsService implements EdgeRequestsService { |
|
|
|
|
|
|
|
EdgeEvent edgeEvent = EdgeUtils.constructEdgeEvent(tenantId, edgeId, type, action, entityId, body); |
|
|
|
|
|
|
|
Futures.addCallback(edgeEventService.saveAsync(edgeEvent), new FutureCallback<>() { |
|
|
|
@Override |
|
|
|
public void onSuccess(@Nullable Void unused) { |
|
|
|
tbClusterService.onEdgeEventUpdate(tenantId, edgeId); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void onFailure(Throwable t) { |
|
|
|
String errMsg = String.format("Failed to save edge event. edge event [%s]", edgeEvent); |
|
|
|
log.warn(errMsg, t); |
|
|
|
} |
|
|
|
return Futures.transform(edgeEventService.saveAsync(edgeEvent), unused -> { |
|
|
|
tbClusterService.onEdgeEventUpdate(tenantId, edgeId); |
|
|
|
return null; |
|
|
|
}, dbCallbackExecutorService); |
|
|
|
} |
|
|
|
|
|
|
|
|