use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.
the class BaseRuleChainServiceTest method testSaveRuleChainMetaData.
@Test
public void testSaveRuleChainMetaData() throws Exception {
RuleChainMetaData savedRuleChainMetaData = createRuleChainMetadata();
Assert.assertEquals(3, savedRuleChainMetaData.getNodes().size());
Assert.assertEquals(3, savedRuleChainMetaData.getConnections().size());
for (RuleNode ruleNode : savedRuleChainMetaData.getNodes()) {
Assert.assertNotNull(ruleNode.getId());
List<EntityRelation> relations = ruleChainService.getRuleNodeRelations(tenantId, ruleNode.getId());
if ("name1".equals(ruleNode.getName())) {
Assert.assertEquals(2, relations.size());
} else if ("name2".equals(ruleNode.getName())) {
Assert.assertEquals(1, relations.size());
} else if ("name3".equals(ruleNode.getName())) {
Assert.assertEquals(0, relations.size());
}
}
List<RuleNode> loadedRuleNodes = ruleChainService.getRuleChainNodes(tenantId, savedRuleChainMetaData.getRuleChainId());
Collections.sort(savedRuleChainMetaData.getNodes(), ruleNodeIdComparator);
Collections.sort(loadedRuleNodes, ruleNodeIdComparator);
Assert.assertEquals(savedRuleChainMetaData.getNodes(), loadedRuleNodes);
ruleChainService.deleteRuleChainById(tenantId, savedRuleChainMetaData.getRuleChainId());
}
use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.
the class BaseRuleChainServiceTest method testUpdateRuleChainMetaData.
@Test
public void testUpdateRuleChainMetaData() throws Exception {
RuleChainMetaData savedRuleChainMetaData = createRuleChainMetadata();
List<RuleNode> ruleNodes = savedRuleChainMetaData.getNodes();
int name3Index = -1;
for (int i = 0; i < ruleNodes.size(); i++) {
if ("name3".equals(ruleNodes.get(i).getName())) {
name3Index = i;
break;
}
}
RuleNode ruleNode4 = new RuleNode();
ruleNode4.setName("name4");
ruleNode4.setType("type4");
ruleNode4.setConfiguration(mapper.readTree("\"key4\": \"val4\""));
ruleNodes.set(name3Index, ruleNode4);
Assert.assertTrue(ruleChainService.saveRuleChainMetaData(tenantId, savedRuleChainMetaData).isSuccess());
RuleChainMetaData updatedRuleChainMetaData = ruleChainService.loadRuleChainMetaData(tenantId, savedRuleChainMetaData.getRuleChainId());
Assert.assertEquals(3, updatedRuleChainMetaData.getNodes().size());
Assert.assertEquals(3, updatedRuleChainMetaData.getConnections().size());
for (RuleNode ruleNode : updatedRuleChainMetaData.getNodes()) {
Assert.assertNotNull(ruleNode.getId());
List<EntityRelation> relations = ruleChainService.getRuleNodeRelations(tenantId, ruleNode.getId());
if ("name1".equals(ruleNode.getName())) {
Assert.assertEquals(2, relations.size());
} else if ("name2".equals(ruleNode.getName())) {
Assert.assertEquals(1, relations.size());
} else if ("name4".equals(ruleNode.getName())) {
Assert.assertEquals(0, relations.size());
}
}
List<RuleNode> loadedRuleNodes = ruleChainService.getRuleChainNodes(tenantId, savedRuleChainMetaData.getRuleChainId());
Collections.sort(updatedRuleChainMetaData.getNodes(), ruleNodeIdComparator);
Collections.sort(loadedRuleNodes, ruleNodeIdComparator);
Assert.assertEquals(updatedRuleChainMetaData.getNodes(), loadedRuleNodes);
ruleChainService.deleteRuleChainById(tenantId, savedRuleChainMetaData.getRuleChainId());
}
use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.
the class RuleChainController method deleteRuleChain.
@ApiOperation(value = "Delete rule chain (deleteRuleChain)", notes = "Deletes the rule chain. Referencing non-existing rule chain Id will cause an error. " + "Referencing rule chain that is used in the device profiles will cause an error." + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteRuleChain(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
checkParameter(RULE_CHAIN_ID, strRuleChainId);
try {
RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
RuleChain ruleChain = checkRuleChain(ruleChainId, Operation.DELETE);
List<RuleNode> referencingRuleNodes = ruleChainService.getReferencingRuleChainNodes(getTenantId(), ruleChainId);
Set<RuleChainId> referencingRuleChainIds = referencingRuleNodes.stream().map(RuleNode::getRuleChainId).collect(Collectors.toSet());
List<EdgeId> relatedEdgeIds = null;
if (RuleChainType.EDGE.equals(ruleChain.getType())) {
relatedEdgeIds = findRelatedEdgeIds(getTenantId(), ruleChainId);
}
ruleChainService.deleteRuleChainById(getTenantId(), ruleChainId);
referencingRuleChainIds.remove(ruleChain.getId());
if (RuleChainType.CORE.equals(ruleChain.getType())) {
referencingRuleChainIds.forEach(referencingRuleChainId -> tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), referencingRuleChainId, ComponentLifecycleEvent.UPDATED));
tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.DELETED);
}
logEntityAction(ruleChainId, ruleChain, null, ActionType.DELETED, null, strRuleChainId);
if (RuleChainType.EDGE.equals(ruleChain.getType())) {
sendDeleteNotificationMsg(ruleChain.getTenantId(), ruleChain.getId(), relatedEdgeIds);
}
} catch (Exception e) {
logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.DELETED, e, strRuleChainId);
throw handleException(e);
}
}
use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.
the class RuleChainActorMessageProcessor method initRoutes.
private void initRoutes(RuleChain ruleChain, List<RuleNode> ruleNodeList) {
nodeRoutes.clear();
// Populating the routes map;
for (RuleNode ruleNode : ruleNodeList) {
List<EntityRelation> relations = service.getRuleNodeRelations(TenantId.SYS_TENANT_ID, ruleNode.getId());
log.trace("[{}][{}][{}] Processing rule node relations [{}]", tenantId, entityId, ruleNode.getId(), relations.size());
if (relations.size() == 0) {
nodeRoutes.put(ruleNode.getId(), Collections.emptyList());
} else {
for (EntityRelation relation : relations) {
log.trace("[{}][{}][{}] Processing rule node relation [{}]", tenantId, entityId, ruleNode.getId(), relation.getTo());
if (relation.getTo().getEntityType() == EntityType.RULE_NODE) {
RuleNodeCtx ruleNodeCtx = nodeActors.get(new RuleNodeId(relation.getTo().getId()));
if (ruleNodeCtx == null) {
throw new IllegalArgumentException("Rule Node [" + relation.getFrom() + "] has invalid relation to Rule node [" + relation.getTo() + "]");
}
}
nodeRoutes.computeIfAbsent(ruleNode.getId(), k -> new ArrayList<>()).add(new RuleNodeRelation(ruleNode.getId(), relation.getTo(), relation.getType()));
}
}
}
firstId = ruleChain.getFirstRuleNodeId();
firstNode = nodeActors.get(firstId);
state = ComponentLifecycleState.ACTIVE;
}
use of org.thingsboard.server.common.data.rule.RuleNode in project thingsboard by thingsboard.
the class RuleChainMsgConstructor method filterConnections_V_3_3_0.
private List<NodeConnectionInfo> filterConnections_V_3_3_0(List<RuleNode> nodes, List<NodeConnectionInfo> connections, NavigableSet<Integer> removedNodeIndexes) {
List<NodeConnectionInfo> result = new ArrayList<>();
if (connections != null) {
result = connections.stream().filter(conn -> {
for (int i = 0; i < nodes.size(); i++) {
RuleNode node = nodes.get(i);
if (node.getType().equalsIgnoreCase(RULE_CHAIN_INPUT_NODE) || node.getType().equalsIgnoreCase(TB_RULE_CHAIN_OUTPUT_NODE)) {
if (conn.getFromIndex() == i || conn.getToIndex() == i) {
return false;
}
}
}
return true;
}).map(conn -> {
NodeConnectionInfo newConn = new NodeConnectionInfo();
newConn.setFromIndex(conn.getFromIndex());
newConn.setToIndex(conn.getToIndex());
newConn.setType(conn.getType());
return newConn;
}).collect(Collectors.toList());
}
// decrease index because of removed nodes
for (Integer removedIndex : removedNodeIndexes) {
for (NodeConnectionInfo newConn : result) {
if (newConn.getToIndex() > removedIndex) {
newConn.setToIndex(newConn.getToIndex() - 1);
}
if (newConn.getFromIndex() > removedIndex) {
newConn.setFromIndex(newConn.getFromIndex() - 1);
}
}
}
return result;
}
Aggregations