Search in sources :

Example 11 with RuleNode

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());
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Test(org.junit.Test)

Example 12 with RuleNode

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());
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Test(org.junit.Test)

Example 13 with RuleNode

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);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with RuleNode

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;
}
Also used : TbMsg(org.thingsboard.server.common.msg.TbMsg) HashMap(java.util.HashMap) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) ComponentMsgProcessor(org.thingsboard.server.actors.shared.ComponentMsgProcessor) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) TbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbApiUsageClient(org.thingsboard.server.queue.usagestats.TbApiUsageClient) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) TbRelationTypes(org.thingsboard.rule.engine.api.TbRelationTypes) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) MultipleTbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper) TbClusterService(org.thingsboard.server.cluster.TbClusterService) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Collections(java.util.Collections) TbActorRef(org.thingsboard.server.actors.TbActorRef) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 15 with RuleNode

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;
}
Also used : RuleChainConnectionInfoProto(org.thingsboard.server.gen.edge.v1.RuleChainConnectionInfoProto) JacksonUtil(org.thingsboard.common.util.JacksonUtil) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainMetadataUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo) EdgeVersion(org.thingsboard.server.gen.edge.v1.EdgeVersion) RuleNodeProto(org.thingsboard.server.gen.edge.v1.RuleNodeProto) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) NodeConnectionInfoProto(org.thingsboard.server.gen.edge.v1.NodeConnectionInfoProto) NavigableSet(java.util.NavigableSet) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) RuleChainUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainUpdateMsg) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) UpdateMsgType(org.thingsboard.server.gen.edge.v1.UpdateMsgType) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList)

Aggregations

RuleNode (org.thingsboard.server.common.data.rule.RuleNode)30 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)17 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)16 ArrayList (java.util.ArrayList)11 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)10 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)10 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)9 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 Slf4j (lombok.extern.slf4j.Slf4j)6 TbRuleChainInputNodeConfiguration (org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 TenantId (org.thingsboard.server.common.data.id.TenantId)5 NodeConnectionInfo (org.thingsboard.server.common.data.rule.NodeConnectionInfo)5 TbMsg (org.thingsboard.server.common.msg.TbMsg)5 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)5 Test (org.junit.Test)4 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)4