Refactoring for input node relations
This commit is contained in:
parent
ac41220a6d
commit
381e976d87
@ -24,21 +24,18 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.common.data.Tenant;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
|
||||
import org.thingsboard.server.common.data.id.RuleChainId;
|
||||
import org.thingsboard.server.common.data.id.RuleNodeId;
|
||||
import org.thingsboard.server.common.data.id.TenantId;
|
||||
import org.thingsboard.server.common.data.page.PageData;
|
||||
import org.thingsboard.server.common.data.page.PageDataIterable;
|
||||
import org.thingsboard.server.common.data.page.PageLink;
|
||||
import org.thingsboard.server.common.data.query.DynamicValue;
|
||||
import org.thingsboard.server.common.data.query.FilterPredicateValue;
|
||||
import org.thingsboard.server.common.data.relation.EntityRelation;
|
||||
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
|
||||
import org.thingsboard.server.common.data.rule.RuleNode;
|
||||
import org.thingsboard.server.dao.relation.RelationService;
|
||||
import org.thingsboard.server.dao.rule.RuleChainService;
|
||||
import org.thingsboard.server.dao.tenant.TenantService;
|
||||
import org.thingsboard.server.service.component.ComponentDiscoveryService;
|
||||
import org.thingsboard.server.service.component.RuleNodeClassInfo;
|
||||
import org.thingsboard.server.service.install.DbUpgradeExecutorService;
|
||||
@ -46,6 +43,7 @@ import org.thingsboard.server.utils.TbNodeUpgradeUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@ -65,9 +63,6 @@ public class DefaultDataUpdateService implements DataUpdateService {
|
||||
@Autowired
|
||||
private RelationService relationService;
|
||||
|
||||
@Autowired
|
||||
private TenantService tenantService;
|
||||
|
||||
@Autowired
|
||||
private ComponentDiscoveryService componentDiscoveryService;
|
||||
|
||||
@ -78,52 +73,36 @@ public class DefaultDataUpdateService implements DataUpdateService {
|
||||
public void updateData() throws Exception {
|
||||
log.info("Updating data ...");
|
||||
//TODO: should be cleaned after each release
|
||||
inputNodesUpdater.updateEntities();
|
||||
updateInputNodes();
|
||||
log.info("Data updated.");
|
||||
}
|
||||
|
||||
//TODO: should be removed after release
|
||||
private final PaginatedUpdater<String, Tenant> inputNodesUpdater = new PaginatedUpdater<>() {
|
||||
@Override
|
||||
protected String getName() {
|
||||
return "Input nodes updater";
|
||||
private void updateInputNodes() {
|
||||
log.info("Creating relations for input nodes...");
|
||||
int n = 0;
|
||||
var inputNodes = new PageDataIterable<>(pageLink -> ruleChainService.findAllRuleNodesByType(TB_RULE_CHAIN_INPUT_NODE, pageLink), 1024);
|
||||
for (RuleNode inputNode : inputNodes) {
|
||||
try {
|
||||
RuleChainId targetRuleChainId = Optional.ofNullable(inputNode.getConfiguration().get("ruleChainId"))
|
||||
.filter(JsonNode::isTextual).map(JsonNode::asText).map(id -> new RuleChainId(UUID.fromString(id)))
|
||||
.orElse(null);
|
||||
if (targetRuleChainId == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PageData<Tenant> findEntities(String type, PageLink pageLink) {
|
||||
return tenantService.findTenants(pageLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateEntity(Tenant tenant) {
|
||||
TenantId tenantId = tenant.getId();
|
||||
try {
|
||||
var inputNodes = ruleChainService.findRuleNodesByTenantIdAndType(tenantId, TB_RULE_CHAIN_INPUT_NODE);
|
||||
var resultFutures = inputNodes.stream().map(ruleNode -> {
|
||||
try {
|
||||
JsonNode id = ruleNode.getConfiguration().get("ruleChainId");
|
||||
if (id != null) {
|
||||
RuleChainId toRuleChainId = new RuleChainId(UUID.fromString(id.asText()));
|
||||
RuleChainId fromRuleChainId = ruleNode.getRuleChainId();
|
||||
EntityRelation relation = new EntityRelation();
|
||||
relation.setFrom(fromRuleChainId);
|
||||
relation.setTo(toRuleChainId);
|
||||
relation.setFrom(inputNode.getRuleChainId());
|
||||
relation.setTo(targetRuleChainId);
|
||||
relation.setType(EntityRelation.USES_TYPE);
|
||||
relation.setTypeGroup(RelationTypeGroup.COMMON);
|
||||
return relationService.saveRelationAsync(tenantId, relation);
|
||||
}
|
||||
relationService.saveRelation(TenantId.SYS_TENANT_ID, relation);
|
||||
n++;
|
||||
} catch (Exception e) {
|
||||
log.error("[{}] Failed to save relation for input node: [{}]", tenantId, ruleNode, e);
|
||||
}
|
||||
return Futures.immediateFuture(null);
|
||||
}).toList();
|
||||
|
||||
Futures.allAsList(resultFutures).get();
|
||||
} catch (Exception e) {
|
||||
log.error("[{}] Unable to update Tenant input nodes", tenantId, e);
|
||||
log.error("Failed to save relation for input node: {}", inputNode, e);
|
||||
}
|
||||
}
|
||||
};
|
||||
log.info("Created {} relations for input nodes", n);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void upgradeRuleNodes() {
|
||||
|
||||
@ -211,9 +211,8 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
|
||||
for (RuleNode existingNode : existingRuleNodes) {
|
||||
relationService.deleteEntityRelations(tenantId, existingNode.getId());
|
||||
if (existingNode.getType().equals(TB_RULE_CHAIN_INPUT_NODE)) {
|
||||
if (existingNode.getConfiguration().has("ruleChainId")) {
|
||||
RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(existingNode);
|
||||
var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId);
|
||||
EntityRelation relation = getRuleChainInputRelation(ruleChainId, existingNode);
|
||||
if (relation != null) {
|
||||
relationService.deleteRelation(tenantId, relation);
|
||||
}
|
||||
}
|
||||
@ -242,9 +241,8 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
|
||||
relations.add(new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(),
|
||||
EntityRelation.CONTAINS_TYPE, RelationTypeGroup.RULE_CHAIN));
|
||||
if (node.getType().equals(TB_RULE_CHAIN_INPUT_NODE)) {
|
||||
if (node.getConfiguration().has("ruleChainId")) {
|
||||
RuleChainId targetRuleChainId = extractRuleChainIdFromInputNode(node);
|
||||
var relation = createRuleChainInputRelation(ruleChainId, targetRuleChainId);
|
||||
EntityRelation relation = getRuleChainInputRelation(ruleChainId, node);
|
||||
if (relation != null) {
|
||||
relations.add(relation);
|
||||
}
|
||||
}
|
||||
@ -317,19 +315,20 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC
|
||||
return RuleChainUpdateResult.successful(updatedRuleNodes);
|
||||
}
|
||||
|
||||
private EntityRelation createRuleChainInputRelation(RuleChainId ruleChainId, RuleChainId targetRuleChainId) {
|
||||
private EntityRelation getRuleChainInputRelation(RuleChainId ruleChainId, RuleNode inputNode) {
|
||||
RuleChainId targetRuleChainId = Optional.ofNullable(inputNode.getConfiguration().get("ruleChainId"))
|
||||
.filter(JsonNode::isTextual).map(JsonNode::asText).map(id -> new RuleChainId(UUID.fromString(id)))
|
||||
.orElse(null);
|
||||
if (targetRuleChainId != null) {
|
||||
EntityRelation relation = new EntityRelation();
|
||||
relation.setFrom(ruleChainId);
|
||||
relation.setTo(targetRuleChainId);
|
||||
relation.setType(EntityRelation.USES_TYPE);
|
||||
relation.setTypeGroup(RelationTypeGroup.COMMON);
|
||||
return relation;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
private RuleChainId extractRuleChainIdFromInputNode(RuleNode node) {
|
||||
JsonNode configuration = node.getConfiguration();
|
||||
UUID targetUuid = UUID.fromString(configuration.get("ruleChainId").asText());
|
||||
return new RuleChainId(targetUuid);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -16,7 +16,6 @@
|
||||
package org.thingsboard.server.dao.service;
|
||||
|
||||
import com.datastax.oss.driver.api.core.uuid.Uuids;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@ -46,6 +45,7 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.thingsboard.server.common.data.relation.EntityRelation.USES_TYPE;
|
||||
import static org.thingsboard.server.dao.rule.BaseRuleChainService.TB_RULE_CHAIN_INPUT_NODE;
|
||||
@ -277,7 +277,7 @@ public class RuleChainServiceTest extends AbstractServiceTest {
|
||||
|
||||
List<RuleNode> ruleNodes = savedRuleChainMetaData.getNodes();
|
||||
int name3Index = -1;
|
||||
for (int i=0;i<ruleNodes.size();i++) {
|
||||
for (int i = 0; i < ruleNodes.size(); i++) {
|
||||
if ("name3".equals(ruleNodes.get(i).getName())) {
|
||||
name3Index = i;
|
||||
break;
|
||||
@ -363,62 +363,76 @@ public class RuleChainServiceTest extends AbstractServiceTest {
|
||||
|
||||
@Test
|
||||
public void testSaveRuleChainWithInputNode() {
|
||||
RuleChain toRuleChain = new RuleChain();
|
||||
toRuleChain.setName("To Rule Chain");
|
||||
toRuleChain.setTenantId(tenantId);
|
||||
RuleChain savedToRuleChain = ruleChainService.saveRuleChain(toRuleChain);
|
||||
|
||||
RuleChain fromRuleChain = new RuleChain();
|
||||
fromRuleChain.setName("From RuleChain");
|
||||
fromRuleChain.setTenantId(tenantId);
|
||||
RuleChain savedFromRuleChain = ruleChainService.saveRuleChain(fromRuleChain);
|
||||
fromRuleChain = ruleChainService.saveRuleChain(fromRuleChain);
|
||||
RuleChainId fromRuleChainId = fromRuleChain.getId();
|
||||
|
||||
RuleChain toRuleChain1 = new RuleChain();
|
||||
toRuleChain1.setName("To Rule Chain 1");
|
||||
toRuleChain1.setTenantId(tenantId);
|
||||
toRuleChain1 = ruleChainService.saveRuleChain(toRuleChain1);
|
||||
RuleChainId toRuleChain1Id = toRuleChain1.getId();
|
||||
|
||||
RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
|
||||
ruleChainMetaData.setRuleChainId(savedFromRuleChain.getId());
|
||||
ruleChainMetaData.setRuleChainId(fromRuleChainId);
|
||||
|
||||
RuleNode ruleNode = new RuleNode();
|
||||
ruleNode.setName("Input node");
|
||||
ruleNode.setType(TB_RULE_CHAIN_INPUT_NODE);
|
||||
ObjectNode configuration = JacksonUtil.newObjectNode();
|
||||
configuration.put("ruleChainId", savedToRuleChain.getId().toString());
|
||||
ruleNode.setConfiguration(configuration);
|
||||
RuleNode toRuleChain1Node = new RuleNode();
|
||||
toRuleChain1Node.setName("To Rule Chain 1");
|
||||
toRuleChain1Node.setType(TB_RULE_CHAIN_INPUT_NODE);
|
||||
toRuleChain1Node.setConfiguration(JacksonUtil.newObjectNode()
|
||||
.put("ruleChainId", toRuleChain1Id.toString()));
|
||||
|
||||
RuleNode toRuleChain1Node2 = new RuleNode();
|
||||
toRuleChain1Node2.setName("To Rule Chain 1");
|
||||
toRuleChain1Node2.setType(TB_RULE_CHAIN_INPUT_NODE);
|
||||
toRuleChain1Node2.setConfiguration(JacksonUtil.newObjectNode()
|
||||
.put("ruleChainId", toRuleChain1Id.toString()));
|
||||
|
||||
List<RuleNode> ruleNodes = new ArrayList<>();
|
||||
ruleNodes.add(ruleNode);
|
||||
ruleNodes.add(toRuleChain1Node);
|
||||
ruleNodes.add(toRuleChain1Node2);
|
||||
ruleChainMetaData.setFirstNodeIndex(0);
|
||||
ruleChainMetaData.setNodes(ruleNodes);
|
||||
|
||||
ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData, Function.identity());
|
||||
|
||||
List<EntityRelation> relations = relationService.findByFromAndType(tenantId, savedFromRuleChain.getId(), USES_TYPE, RelationTypeGroup.COMMON);
|
||||
Assert.assertEquals(1, relations.size());
|
||||
EntityRelation usesRelation = relations.get(0);
|
||||
Assert.assertEquals(savedFromRuleChain.getId(), usesRelation.getFrom());
|
||||
Assert.assertEquals(savedToRuleChain.getId(), usesRelation.getTo());
|
||||
List<EntityRelation> relations = relationService.findByFromAndType(tenantId, fromRuleChainId, USES_TYPE, RelationTypeGroup.COMMON);
|
||||
assertThat(relations).singleElement().satisfies(relationToRuleChain1 -> {
|
||||
assertThat(relationToRuleChain1.getFrom()).isEqualTo(fromRuleChainId);
|
||||
assertThat(relationToRuleChain1.getTo()).isEqualTo(toRuleChain1Id);
|
||||
});
|
||||
|
||||
RuleChain newToRuleChain = new RuleChain();
|
||||
newToRuleChain.setName("New To Rule Chain");
|
||||
newToRuleChain.setTenantId(tenantId);
|
||||
RuleChain savedNewToRuleChain = ruleChainService.saveRuleChain(newToRuleChain);
|
||||
RuleChain toRuleChain2 = new RuleChain();
|
||||
toRuleChain2.setName("To Rule Chain 2");
|
||||
toRuleChain2.setTenantId(tenantId);
|
||||
toRuleChain2 = ruleChainService.saveRuleChain(toRuleChain2);
|
||||
RuleChainId toRuleChain2Id = toRuleChain2.getId();
|
||||
|
||||
RuleNode newRuleNode = new RuleNode();
|
||||
newRuleNode.setName("Input node");
|
||||
newRuleNode.setType(TB_RULE_CHAIN_INPUT_NODE);
|
||||
ObjectNode newConfiguration = JacksonUtil.newObjectNode();
|
||||
configuration.put("ruleChainId", savedNewToRuleChain.getId().toString());
|
||||
newRuleNode.setConfiguration(newConfiguration);
|
||||
RuleNode toRuleChain2Node = new RuleNode();
|
||||
toRuleChain2Node.setName("To Rule Chain 2");
|
||||
toRuleChain2Node.setType(TB_RULE_CHAIN_INPUT_NODE);
|
||||
toRuleChain2Node.setConfiguration(JacksonUtil.newObjectNode()
|
||||
.put("ruleChainId", toRuleChain2Id.toString()));
|
||||
|
||||
List<RuleNode> newRuleNodes = new ArrayList<>();
|
||||
newRuleNodes.add(newRuleNode);
|
||||
RuleChainMetaData foundRuleChainMetaData = ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId());
|
||||
foundRuleChainMetaData.setNodes(newRuleNodes);
|
||||
newRuleNodes.add(toRuleChain2Node);
|
||||
newRuleNodes.add(toRuleChain1Node);
|
||||
ruleChainMetaData = ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId());
|
||||
ruleChainMetaData.setNodes(newRuleNodes);
|
||||
ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData, Function.identity());
|
||||
|
||||
List<EntityRelation> newRelations = relationService.findByFromAndType(tenantId, savedFromRuleChain.getId(), USES_TYPE, RelationTypeGroup.COMMON);
|
||||
Assert.assertEquals(1, relations.size());
|
||||
EntityRelation newUsesRelation = newRelations.get(0);
|
||||
Assert.assertEquals(savedFromRuleChain.getId(), newUsesRelation.getFrom());
|
||||
Assert.assertEquals(savedNewToRuleChain.getId(), newUsesRelation.getTo());
|
||||
List<EntityRelation> newRelations = relationService.findByFromAndType(tenantId, fromRuleChainId, USES_TYPE, RelationTypeGroup.COMMON);
|
||||
assertThat(newRelations).hasSize(2);
|
||||
assertThat(newRelations).anySatisfy(relationToRuleChain1 -> {
|
||||
assertThat(relationToRuleChain1.getFrom()).isEqualTo(fromRuleChainId);
|
||||
assertThat(relationToRuleChain1.getTo()).isEqualTo(toRuleChain1Id);
|
||||
});
|
||||
assertThat(newRelations).anySatisfy(relationToRuleChain2 -> {
|
||||
assertThat(relationToRuleChain2.getFrom()).isEqualTo(fromRuleChainId);
|
||||
assertThat(relationToRuleChain2.getTo()).isEqualTo(toRuleChain2Id);
|
||||
});
|
||||
}
|
||||
|
||||
private RuleChainId saveRuleChainAndSetAutoAssignToEdge(String name) {
|
||||
@ -462,9 +476,9 @@ public class RuleChainServiceTest extends AbstractServiceTest {
|
||||
ruleChainMetaData.setFirstNodeIndex(0);
|
||||
ruleChainMetaData.setNodes(ruleNodes);
|
||||
|
||||
ruleChainMetaData.addConnectionInfo(0,1,"success");
|
||||
ruleChainMetaData.addConnectionInfo(0,2,"fail");
|
||||
ruleChainMetaData.addConnectionInfo(1,2,"success");
|
||||
ruleChainMetaData.addConnectionInfo(0, 1, "success");
|
||||
ruleChainMetaData.addConnectionInfo(0, 2, "fail");
|
||||
ruleChainMetaData.addConnectionInfo(1, 2, "success");
|
||||
|
||||
Assert.assertTrue(ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData, Function.identity()).isSuccess());
|
||||
return ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId());
|
||||
@ -501,10 +515,10 @@ public class RuleChainServiceTest extends AbstractServiceTest {
|
||||
ruleChainMetaData.setFirstNodeIndex(0);
|
||||
ruleChainMetaData.setNodes(ruleNodes);
|
||||
|
||||
ruleChainMetaData.addConnectionInfo(0,1,"success");
|
||||
ruleChainMetaData.addConnectionInfo(0,2,"fail");
|
||||
ruleChainMetaData.addConnectionInfo(1,2,"success");
|
||||
ruleChainMetaData.addConnectionInfo(2,2,"success");
|
||||
ruleChainMetaData.addConnectionInfo(0, 1, "success");
|
||||
ruleChainMetaData.addConnectionInfo(0, 2, "fail");
|
||||
ruleChainMetaData.addConnectionInfo(1, 2, "success");
|
||||
ruleChainMetaData.addConnectionInfo(2, 2, "success");
|
||||
|
||||
return ruleChainMetaData;
|
||||
}
|
||||
@ -540,10 +554,10 @@ public class RuleChainServiceTest extends AbstractServiceTest {
|
||||
ruleChainMetaData.setFirstNodeIndex(0);
|
||||
ruleChainMetaData.setNodes(ruleNodes);
|
||||
|
||||
ruleChainMetaData.addConnectionInfo(0,1,"success");
|
||||
ruleChainMetaData.addConnectionInfo(0,2,"fail");
|
||||
ruleChainMetaData.addConnectionInfo(1,2,"success");
|
||||
ruleChainMetaData.addConnectionInfo(2,0,"success");
|
||||
ruleChainMetaData.addConnectionInfo(0, 1, "success");
|
||||
ruleChainMetaData.addConnectionInfo(0, 2, "fail");
|
||||
ruleChainMetaData.addConnectionInfo(1, 2, "success");
|
||||
ruleChainMetaData.addConnectionInfo(2, 0, "success");
|
||||
|
||||
return ruleChainMetaData;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user