Search in sources :

Example 26 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EdgeServiceImpl method deleteEdge.

@Override
public void deleteEdge(TenantId tenantId, EdgeId edgeId) {
    log.trace("Executing deleteEdge [{}]", edgeId);
    validateId(edgeId, INCORRECT_EDGE_ID + edgeId);
    Edge edge = edgeDao.findById(tenantId, edgeId.getId());
    deleteEntityRelations(tenantId, edgeId);
    cacheManager.removeEdgeFromCacheByName(edge.getTenantId(), edge.getName());
    edgeDao.removeById(tenantId, edgeId.getId());
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge)

Example 27 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EdgeServiceImpl method findEdgesByQuery.

@Override
public ListenableFuture<List<Edge>> findEdgesByQuery(TenantId tenantId, EdgeSearchQuery query) {
    log.trace("[{}] Executing findEdgesByQuery [{}]", tenantId, query);
    ListenableFuture<List<EntityRelation>> relations = relationService.findByQuery(tenantId, query.toEntitySearchQuery());
    ListenableFuture<List<Edge>> edges = Futures.transformAsync(relations, r -> {
        EntitySearchDirection direction = query.toEntitySearchQuery().getParameters().getDirection();
        List<ListenableFuture<Edge>> futures = new ArrayList<>();
        for (EntityRelation relation : r) {
            EntityId entityId = direction == EntitySearchDirection.FROM ? relation.getTo() : relation.getFrom();
            if (entityId.getEntityType() == EntityType.EDGE) {
                futures.add(findEdgeByIdAsync(tenantId, new EdgeId(entityId.getId())));
            }
        }
        return Futures.successfulAsList(futures);
    }, MoreExecutors.directExecutor());
    edges = Futures.transform(edges, new Function<List<Edge>, List<Edge>>() {

        @Nullable
        @Override
        public List<Edge> apply(@Nullable List<Edge> edgeList) {
            return edgeList == null ? Collections.emptyList() : edgeList.stream().filter(edge -> query.getEdgeTypes().contains(edge.getType())).collect(Collectors.toList());
        }
    }, MoreExecutors.directExecutor());
    return edges;
}
Also used : EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) ArrayList(java.util.ArrayList) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Function(com.google.common.base.Function) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) Edge(org.thingsboard.server.common.data.edge.Edge) Nullable(javax.annotation.Nullable)

Example 28 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EntityViewServiceImpl method unassignEntityViewFromEdge.

@Override
public EntityView unassignEntityViewFromEdge(TenantId tenantId, EntityViewId entityViewId, EdgeId edgeId) {
    EntityView entityView = findEntityViewById(tenantId, entityViewId);
    Edge edge = edgeService.findEdgeById(tenantId, edgeId);
    if (edge == null) {
        throw new DataValidationException("Can't unassign entityView from non-existent edge!");
    }
    try {
        deleteRelation(tenantId, new EntityRelation(edgeId, entityViewId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE));
    } catch (Exception e) {
        log.warn("[{}] Failed to delete entityView relation. Edge Id: [{}]", entityViewId, edgeId);
        throw new RuntimeException(e);
    }
    return entityView;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) EntityView(org.thingsboard.server.common.data.EntityView) Edge(org.thingsboard.server.common.data.edge.Edge) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Example 29 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EntityViewServiceImpl method assignEntityViewToEdge.

@CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}")
@Override
public EntityView assignEntityViewToEdge(TenantId tenantId, EntityViewId entityViewId, EdgeId edgeId) {
    EntityView entityView = findEntityViewById(tenantId, entityViewId);
    Edge edge = edgeService.findEdgeById(tenantId, edgeId);
    if (edge == null) {
        throw new DataValidationException("Can't assign entityView to non-existent edge!");
    }
    if (!edge.getTenantId().getId().equals(entityView.getTenantId().getId())) {
        throw new DataValidationException("Can't assign entityView to edge from different tenant!");
    }
    try {
        Boolean relationExists = relationService.checkRelation(tenantId, edgeId, entityView.getEntityId(), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE).get();
        if (!relationExists) {
            throw new DataValidationException("Can't assign entity view to edge because related device/asset doesn't assigned to edge!");
        }
    } catch (ExecutionException | InterruptedException e) {
        log.error("Exception during relation check", e);
        throw new RuntimeException("Exception during relation check", e);
    }
    try {
        createRelation(tenantId, new EntityRelation(edgeId, entityViewId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE));
    } catch (Exception e) {
        log.warn("[{}] Failed to create entityView relation. Edge Id: [{}]", entityViewId, edgeId);
        throw new RuntimeException(e);
    }
    return entityView;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) EntityView(org.thingsboard.server.common.data.EntityView) ExecutionException(java.util.concurrent.ExecutionException) Edge(org.thingsboard.server.common.data.edge.Edge) ExecutionException(java.util.concurrent.ExecutionException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 30 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class AbstractEdgeEntity method toEdge.

protected Edge toEdge() {
    Edge edge = new Edge(new EdgeId(getUuid()));
    edge.setCreatedTime(createdTime);
    if (tenantId != null) {
        edge.setTenantId(TenantId.fromUUID(tenantId));
    }
    if (customerId != null) {
        edge.setCustomerId(new CustomerId(customerId));
    }
    if (rootRuleChainId != null) {
        edge.setRootRuleChainId(new RuleChainId(rootRuleChainId));
    }
    edge.setType(type);
    edge.setName(name);
    edge.setLabel(label);
    edge.setRoutingKey(routingKey);
    edge.setSecret(secret);
    edge.setAdditionalInfo(additionalInfo);
    return edge;
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Edge(org.thingsboard.server.common.data.edge.Edge)

Aggregations

Edge (org.thingsboard.server.common.data.edge.Edge)98 Test (org.junit.Test)46 EdgeId (org.thingsboard.server.common.data.id.EdgeId)32 PageLink (org.thingsboard.server.common.data.page.PageLink)31 ArrayList (java.util.ArrayList)25 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)25 ApiOperation (io.swagger.annotations.ApiOperation)23 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)22 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)21 Customer (org.thingsboard.server.common.data.Customer)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)15 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Device (org.thingsboard.server.common.data.Device)13 Asset (org.thingsboard.server.common.data.asset.Asset)13 PageData (org.thingsboard.server.common.data.page.PageData)13