Search in sources :

Example 21 with EntityId

use of org.thingsboard.server.common.data.id.EntityId in project thingsboard by thingsboard.

the class SubscriptionManager method processSubscriptionRemoval.

private void processSubscriptionRemoval(PluginContext ctx, String sessionId, Map<Integer, Subscription> sessionSubscriptions, Subscription subscription) {
    EntityId entityId = subscription.getEntityId();
    if (subscription.isLocal() && subscription.getServer() != null) {
        rpcHandler.onSubscriptionClose(ctx, subscription.getServer(), sessionId, subscription.getSubscriptionId());
    }
    if (sessionSubscriptions.isEmpty()) {
        log.debug("[{}] Removed last subscription for particular session.", sessionId);
        subscriptionsByWsSessionId.remove(sessionId);
    } else {
        log.debug("[{}] Removed session subscription.", sessionId);
    }
    Set<Subscription> deviceSubscriptions = subscriptionsByEntityId.get(entityId);
    if (deviceSubscriptions != null) {
        boolean result = deviceSubscriptions.remove(subscription);
        if (result) {
            if (deviceSubscriptions.size() == 0) {
                log.debug("[{}] Removed last subscription for particular device.", sessionId);
                subscriptionsByEntityId.remove(entityId);
            } else {
                log.debug("[{}] Removed device subscription.", sessionId);
            }
        } else {
            log.debug("[{}] Subscription not found!", sessionId);
        }
    } else {
        log.debug("[{}] No device subscriptions found!", sessionId);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) Subscription(org.thingsboard.server.extensions.core.plugin.telemetry.sub.Subscription)

Example 22 with EntityId

use of org.thingsboard.server.common.data.id.EntityId in project thingsboard by thingsboard.

the class BaseRelationService method deleteEntityRelationsAsync.

@Override
public ListenableFuture<Boolean> deleteEntityRelationsAsync(EntityId entity) {
    Cache cache = cacheManager.getCache(RELATIONS_CACHE);
    log.trace("Executing deleteEntityRelationsAsync [{}]", entity);
    validate(entity);
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListTo = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo);
    ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelationsTo, (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> {
        List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, true);
        return Futures.allAsList(results);
    });
    ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction());
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListFrom = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListFrom.add(relationDao.findAllByTo(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom);
    Futures.transform(inboundRelationsFrom, (AsyncFunction<List<List<EntityRelation>>, List<Boolean>>) relations -> {
        List<ListenableFuture<Boolean>> results = getListenableFutures(relations, cache, false);
        return Futures.allAsList(results);
    });
    ListenableFuture<Boolean> outboundFuture = relationDao.deleteOutboundRelationsAsync(entity);
    return Futures.transform(Futures.allAsList(Arrays.asList(inboundFuture, outboundFuture)), getListToBooleanFunction());
}
Also used : java.util(java.util) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) org.thingsboard.server.common.data.relation(org.thingsboard.server.common.data.relation) Cacheable(org.springframework.cache.annotation.Cacheable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) Caching(org.springframework.cache.annotation.Caching) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) EntityService(org.thingsboard.server.dao.entity.EntityService) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RELATIONS_CACHE(org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE) Nullable(javax.annotation.Nullable) StringUtils(org.springframework.util.StringUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache)

Example 23 with EntityId

use of org.thingsboard.server.common.data.id.EntityId in project thingsboard by thingsboard.

the class BaseRelationService method deleteEntityRelations.

@Override
public boolean deleteEntityRelations(EntityId entity) {
    Cache cache = cacheManager.getCache(RELATIONS_CACHE);
    log.trace("Executing deleteEntityRelations [{}]", entity);
    validate(entity);
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListTo = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListTo.add(relationDao.findAllByTo(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsTo = Futures.allAsList(inboundRelationsListTo);
    ListenableFuture<List<Boolean>> inboundDeletions = Futures.transform(inboundRelationsTo, (List<List<EntityRelation>> relations) -> getBooleans(relations, cache, true));
    ListenableFuture<Boolean> inboundFuture = Futures.transform(inboundDeletions, getListToBooleanFunction());
    boolean inboundDeleteResult = false;
    try {
        inboundDeleteResult = inboundFuture.get();
    } catch (InterruptedException | ExecutionException e) {
        log.error("Error deleting entity inbound relations", e);
    }
    List<ListenableFuture<List<EntityRelation>>> inboundRelationsListFrom = new ArrayList<>();
    for (RelationTypeGroup typeGroup : RelationTypeGroup.values()) {
        inboundRelationsListFrom.add(relationDao.findAllByFrom(entity, typeGroup));
    }
    ListenableFuture<List<List<EntityRelation>>> inboundRelationsFrom = Futures.allAsList(inboundRelationsListFrom);
    Futures.transform(inboundRelationsFrom, (Function<List<List<EntityRelation>>, List<Boolean>>) relations -> getBooleans(relations, cache, false));
    boolean outboundDeleteResult = relationDao.deleteOutboundRelations(entity);
    return inboundDeleteResult && outboundDeleteResult;
}
Also used : java.util(java.util) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) org.thingsboard.server.common.data.relation(org.thingsboard.server.common.data.relation) Cacheable(org.springframework.cache.annotation.Cacheable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) Caching(org.springframework.cache.annotation.Caching) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) EntityService(org.thingsboard.server.dao.entity.EntityService) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RELATIONS_CACHE(org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE) Nullable(javax.annotation.Nullable) StringUtils(org.springframework.util.StringUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ExecutionException(java.util.concurrent.ExecutionException) Cache(org.springframework.cache.Cache)

Example 24 with EntityId

use of org.thingsboard.server.common.data.id.EntityId in project thingsboard by thingsboard.

the class BaseRelationService method findRelationsRecursively.

private ListenableFuture<Set<EntityRelation>> findRelationsRecursively(final EntityId rootId, final EntitySearchDirection direction, int lvl, final ConcurrentHashMap<EntityId, Boolean> uniqueMap) throws Exception {
    if (lvl == 0) {
        return Futures.immediateFuture(Collections.emptySet());
    }
    lvl--;
    // TODO: try to remove this blocking operation
    Set<EntityRelation> children = new HashSet<>(findRelations(rootId, direction).get());
    Set<EntityId> childrenIds = new HashSet<>();
    for (EntityRelation childRelation : children) {
        log.trace("Found Relation: {}", childRelation);
        EntityId childId;
        if (direction == EntitySearchDirection.FROM) {
            childId = childRelation.getTo();
        } else {
            childId = childRelation.getFrom();
        }
        if (uniqueMap.putIfAbsent(childId, Boolean.TRUE) == null) {
            log.trace("Adding Relation: {}", childId);
            if (childrenIds.add(childId)) {
                log.trace("Added Relation: {}", childId);
            }
        }
    }
    List<ListenableFuture<Set<EntityRelation>>> futures = new ArrayList<>();
    for (EntityId entityId : childrenIds) {
        futures.add(findRelationsRecursively(entityId, direction, lvl, uniqueMap));
    }
    // TODO: try to remove this blocking operation
    List<Set<EntityRelation>> relations = Futures.successfulAsList(futures).get();
    relations.forEach(r -> r.forEach(d -> children.add(d)));
    return Futures.immediateFuture(children);
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) java.util(java.util) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Cache(org.springframework.cache.Cache) org.thingsboard.server.common.data.relation(org.thingsboard.server.common.data.relation) Cacheable(org.springframework.cache.annotation.Cacheable) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Autowired(org.springframework.beans.factory.annotation.Autowired) CacheEvict(org.springframework.cache.annotation.CacheEvict) Caching(org.springframework.cache.annotation.Caching) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) CacheManager(org.springframework.cache.CacheManager) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) EntityService(org.thingsboard.server.dao.entity.EntityService) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) RELATIONS_CACHE(org.thingsboard.server.common.data.CacheConstants.RELATIONS_CACHE) Nullable(javax.annotation.Nullable) StringUtils(org.springframework.util.StringUtils) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 25 with EntityId

use of org.thingsboard.server.common.data.id.EntityId in project thingsboard by thingsboard.

the class BaseAssetService method findAssetsByQuery.

@Override
public ListenableFuture<List<Asset>> findAssetsByQuery(AssetSearchQuery query) {
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(query.toEntitySearchQuery());
    ListenableFuture<List<Asset>> assets = Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Asset>>) relations1 -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Asset>> futures = new ArrayList<>();
        for (EntityRelation relation : relations1) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.ASSET) {
                futures.add(findAssetByIdAsync(new AssetId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    });
    assets = Futures.transform(assets, (Function<List<Asset>, List<Asset>>) assetList -> assetList == null ? Collections.emptyList() : assetList.stream().filter(asset -> query.getAssetTypes().contains(asset.getType())).collect(Collectors.toList()));
    return assets;
}
Also used : java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Customer(org.thingsboard.server.common.data.Customer) AssetId(org.thingsboard.server.common.data.id.AssetId) Autowired(org.springframework.beans.factory.annotation.Autowired) Tenant(org.thingsboard.server.common.data.Tenant) TextPageData(org.thingsboard.server.common.data.page.TextPageData) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) Function(com.google.common.base.Function) DaoUtil.toUUIDs(org.thingsboard.server.dao.DaoUtil.toUUIDs) NULL_UUID(org.thingsboard.server.dao.model.ModelConstants.NULL_UUID) Validator(org.thingsboard.server.dao.service.Validator) Collectors(java.util.stream.Collectors) AssetSearchQuery(org.thingsboard.server.common.data.asset.AssetSearchQuery) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Futures(com.google.common.util.concurrent.Futures) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) CustomerDao(org.thingsboard.server.dao.customer.CustomerDao) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) TenantDao(org.thingsboard.server.dao.tenant.TenantDao) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySubtype(org.thingsboard.server.common.data.EntitySubtype) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) StringUtils(org.springframework.util.StringUtils) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) AsyncFunction(com.google.common.util.concurrent.AsyncFunction) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId)

Aggregations

EntityId (org.thingsboard.server.common.data.id.EntityId)27 Slf4j (lombok.extern.slf4j.Slf4j)12 StringUtils (org.springframework.util.StringUtils)10 java.util (java.util)9 Function (com.google.common.base.Function)8 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)8 Futures (com.google.common.util.concurrent.Futures)8 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)8 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)7 IOException (java.io.IOException)6 Collectors (java.util.stream.Collectors)6 Service (org.springframework.stereotype.Service)6 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)6 PluginContext (org.thingsboard.server.extensions.api.plugins.PluginContext)6 Nullable (javax.annotation.Nullable)5 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 ExecutionException (java.util.concurrent.ExecutionException)4