Search in sources :

Example 21 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseAlarmService method updateRelations.

private void updateRelations(Alarm alarm, AlarmStatus oldStatus, AlarmStatus newStatus) {
    try {
        List<EntityRelation> relations = relationService.findByToAsync(alarm.getId(), RelationTypeGroup.ALARM).get();
        Set<EntityId> parents = relations.stream().map(EntityRelation::getFrom).collect(Collectors.toSet());
        for (EntityId parentId : parents) {
            updateAlarmRelation(parentId, alarm.getId(), oldStatus, newStatus);
        }
    } catch (ExecutionException | InterruptedException e) {
        log.warn("[{}] Failed to update relations. Old status: [{}], New status: [{}]", alarm.getId(), oldStatus, newStatus);
        throw new RuntimeException(e);
    }
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ExecutionException(java.util.concurrent.ExecutionException)

Example 22 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseAlarmService method deleteAlarmRelation.

private void deleteAlarmRelation(EntityId entityId, EntityId alarmId, AlarmStatus status) {
    try {
        deleteRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + status.name(), RelationTypeGroup.ALARM));
        deleteRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + status.getClearSearchStatus().name(), RelationTypeGroup.ALARM));
        deleteRelation(new EntityRelation(entityId, alarmId, ALARM_RELATION_PREFIX + status.getAckSearchStatus().name(), RelationTypeGroup.ALARM));
    } catch (ExecutionException | InterruptedException e) {
        log.warn("[{}] Failed to delete relation. Status: [{}]", alarmId, status);
        throw new RuntimeException(e);
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ExecutionException(java.util.concurrent.ExecutionException)

Example 23 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testFindTo.

@Test
public void testFindTo() throws ExecutionException, InterruptedException {
    AssetId parentA = new AssetId(UUIDs.timeBased());
    AssetId parentB = new AssetId(UUIDs.timeBased());
    AssetId childA = new AssetId(UUIDs.timeBased());
    AssetId childB = new AssetId(UUIDs.timeBased());
    EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
    EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
    saveRelation(relationA1);
    saveRelation(relationA2);
    saveRelation(relationB1);
    saveRelation(relationB2);
    // Data propagation to views is async
    Thread.sleep(3000);
    List<EntityRelation> relations = relationService.findByTo(childA, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(childA, relation.getTo());
        Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
    }
    relations = relationService.findByToAndType(childA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(1, relations.size());
    relations = relationService.findByToAndType(childB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(1, relations.size());
    relations = relationService.findByToAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByToAndType(parentB, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByTo(childB, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(childB, relation.getTo());
        Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 24 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testRecursiveRelation.

@Test
public void testRecursiveRelation() throws ExecutionException, InterruptedException {
    // A -> B -> [C,D]
    AssetId assetA = new AssetId(UUIDs.timeBased());
    AssetId assetB = new AssetId(UUIDs.timeBased());
    AssetId assetC = new AssetId(UUIDs.timeBased());
    DeviceId deviceD = new DeviceId(UUIDs.timeBased());
    EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE);
    saveRelation(relationAB);
    saveRelation(relationBC);
    saveRelation(relationBD);
    EntityRelationsQuery query = new EntityRelationsQuery();
    query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
    query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
    List<EntityRelation> relations = relationService.findByQuery(query).get();
    Assert.assertEquals(2, relations.size());
    Assert.assertTrue(relations.contains(relationAB));
    Assert.assertTrue(relations.contains(relationBC));
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.relation.EntityTypeFilter) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 25 with EntityRelation

use of org.thingsboard.server.common.data.relation.EntityRelation in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testSaveRelationWithEmptyFrom.

@Test(expected = DataValidationException.class)
public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException {
    EntityRelation relation = new EntityRelation();
    relation.setTo(new AssetId(UUIDs.timeBased()));
    relation.setType(EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(saveRelation(relation));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Aggregations

EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)28 AssetId (org.thingsboard.server.common.data.id.AssetId)13 Test (org.junit.Test)12 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)6 Futures (com.google.common.util.concurrent.Futures)6 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)6 Slf4j (lombok.extern.slf4j.Slf4j)6 Autowired (org.springframework.beans.factory.annotation.Autowired)6 EntityId (org.thingsboard.server.common.data.id.EntityId)6 List (java.util.List)5 ExecutionException (java.util.concurrent.ExecutionException)5 EntityType (org.thingsboard.server.common.data.EntityType)5 Function (com.google.common.base.Function)4 ArrayList (java.util.ArrayList)4 UUID (java.util.UUID)4 Component (org.springframework.stereotype.Component)4 CustomerId (org.thingsboard.server.common.data.id.CustomerId)4 TenantId (org.thingsboard.server.common.data.id.TenantId)4 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)4 RelationTypeGroup (org.thingsboard.server.common.data.relation.RelationTypeGroup)4