Search in sources :

Example 1 with RuleChainConnectionInfo

use of org.thingsboard.server.common.data.rule.RuleChainConnectionInfo in project thingsboard by thingsboard.

the class EdgeServiceImpl method findMissingToRelatedRuleChains.

@Override
public String findMissingToRelatedRuleChains(TenantId tenantId, EdgeId edgeId) {
    List<RuleChain> edgeRuleChains = findEdgeRuleChains(tenantId, edgeId);
    List<RuleChainId> edgeRuleChainIds = edgeRuleChains.stream().map(IdBased::getId).collect(Collectors.toList());
    ObjectNode result = mapper.createObjectNode();
    for (RuleChain edgeRuleChain : edgeRuleChains) {
        List<RuleChainConnectionInfo> connectionInfos = ruleChainService.loadRuleChainMetaData(edgeRuleChain.getTenantId(), edgeRuleChain.getId()).getRuleChainConnections();
        if (connectionInfos != null && !connectionInfos.isEmpty()) {
            List<RuleChainId> connectedRuleChains = connectionInfos.stream().map(RuleChainConnectionInfo::getTargetRuleChainId).collect(Collectors.toList());
            List<String> missingRuleChains = new ArrayList<>();
            for (RuleChainId connectedRuleChain : connectedRuleChains) {
                if (!edgeRuleChainIds.contains(connectedRuleChain)) {
                    RuleChain ruleChainById = ruleChainService.findRuleChainById(tenantId, connectedRuleChain);
                    missingRuleChains.add(ruleChainById.getName());
                }
            }
            if (!missingRuleChains.isEmpty()) {
                ArrayNode array = mapper.createArrayNode();
                for (String missingRuleChain : missingRuleChains) {
                    array.add(missingRuleChain);
                }
                result.set(edgeRuleChain.getName(), array);
            }
        }
    }
    return result.toString();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo)

Example 2 with RuleChainConnectionInfo

use of org.thingsboard.server.common.data.rule.RuleChainConnectionInfo in project thingsboard by thingsboard.

the class EntityEdgeProcessor method updateDependentRuleChains.

private void updateDependentRuleChains(TenantId tenantId, RuleChainId processingRuleChainId, EdgeId edgeId) {
    PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
    PageData<RuleChain> pageData;
    do {
        pageData = ruleChainService.findRuleChainsByTenantIdAndEdgeId(tenantId, edgeId, pageLink);
        if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
            for (RuleChain ruleChain : pageData.getData()) {
                if (!ruleChain.getId().equals(processingRuleChainId)) {
                    List<RuleChainConnectionInfo> connectionInfos = ruleChainService.loadRuleChainMetaData(ruleChain.getTenantId(), ruleChain.getId()).getRuleChainConnections();
                    if (connectionInfos != null && !connectionInfos.isEmpty()) {
                        for (RuleChainConnectionInfo connectionInfo : connectionInfos) {
                            if (connectionInfo.getTargetRuleChainId().equals(processingRuleChainId)) {
                                saveEdgeEvent(tenantId, edgeId, EdgeEventType.RULE_CHAIN_METADATA, EdgeEventActionType.UPDATED, ruleChain.getId(), null);
                            }
                        }
                    }
                }
            }
            if (pageData.hasNext()) {
                pageLink = pageLink.nextPageLink();
            }
        }
    } while (pageData != null && pageData.hasNext());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) PageLink(org.thingsboard.server.common.data.page.PageLink) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo)

Example 3 with RuleChainConnectionInfo

use of org.thingsboard.server.common.data.rule.RuleChainConnectionInfo in project thingsboard by thingsboard.

the class RuleChainMsgConstructor method addRuleChainConnections_V_3_3_0.

private List<RuleChainConnectionInfo> addRuleChainConnections_V_3_3_0(List<RuleNode> nodes, List<NodeConnectionInfo> connections) throws JsonProcessingException {
    List<RuleChainConnectionInfo> result = new ArrayList<>();
    for (int i = 0; i < nodes.size(); i++) {
        RuleNode node = nodes.get(i);
        if (node.getType().equalsIgnoreCase(RULE_CHAIN_INPUT_NODE)) {
            for (NodeConnectionInfo connection : connections) {
                if (connection.getToIndex() == i) {
                    RuleChainConnectionInfo e = new RuleChainConnectionInfo();
                    e.setFromIndex(connection.getFromIndex());
                    TbRuleChainInputNodeConfiguration configuration = JacksonUtil.treeToValue(node.getConfiguration(), TbRuleChainInputNodeConfiguration.class);
                    e.setTargetRuleChainId(new RuleChainId(UUID.fromString(configuration.getRuleChainId())));
                    e.setAdditionalInfo(node.getAdditionalInfo());
                    e.setType(connection.getType());
                    result.add(e);
                }
            }
        }
    }
    return result;
}
Also used : NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) ArrayList(java.util.ArrayList) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo)

Example 4 with RuleChainConnectionInfo

use of org.thingsboard.server.common.data.rule.RuleChainConnectionInfo in project thingsboard by thingsboard.

the class BaseRuleChainService method saveRuleChainMetaData.

@Override
@Transactional
public RuleChainUpdateResult saveRuleChainMetaData(TenantId tenantId, RuleChainMetaData ruleChainMetaData) {
    Validator.validateId(ruleChainMetaData.getRuleChainId(), "Incorrect rule chain id.");
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainMetaData.getRuleChainId());
    if (ruleChain == null) {
        return RuleChainUpdateResult.failed();
    }
    ConstraintValidator.validateFields(ruleChainMetaData);
    List<RuleNodeUpdateResult> updatedRuleNodes = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(ruleChainMetaData.getConnections())) {
        validateCircles(ruleChainMetaData.getConnections());
    }
    List<RuleNode> nodes = ruleChainMetaData.getNodes();
    List<RuleNode> toAddOrUpdate = new ArrayList<>();
    List<RuleNode> toDelete = new ArrayList<>();
    Map<RuleNodeId, Integer> ruleNodeIndexMap = new HashMap<>();
    if (nodes != null) {
        for (RuleNode node : nodes) {
            if (node.getId() != null) {
                ruleNodeIndexMap.put(node.getId(), nodes.indexOf(node));
            } else {
                toAddOrUpdate.add(node);
            }
        }
    }
    List<RuleNode> existingRuleNodes = getRuleChainNodes(tenantId, ruleChainMetaData.getRuleChainId());
    for (RuleNode existingNode : existingRuleNodes) {
        deleteEntityRelations(tenantId, existingNode.getId());
        Integer index = ruleNodeIndexMap.get(existingNode.getId());
        RuleNode newRuleNode = null;
        if (index != null) {
            newRuleNode = ruleChainMetaData.getNodes().get(index);
            toAddOrUpdate.add(newRuleNode);
        } else {
            updatedRuleNodes.add(new RuleNodeUpdateResult(existingNode, null));
            toDelete.add(existingNode);
        }
        updatedRuleNodes.add(new RuleNodeUpdateResult(existingNode, newRuleNode));
    }
    if (nodes != null) {
        for (RuleNode node : toAddOrUpdate) {
            node.setRuleChainId(ruleChain.getId());
            RuleNode savedNode = ruleNodeDao.save(tenantId, node);
            createRelation(tenantId, new EntityRelation(ruleChainMetaData.getRuleChainId(), savedNode.getId(), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.RULE_CHAIN));
            int index = nodes.indexOf(node);
            nodes.set(index, savedNode);
            ruleNodeIndexMap.put(savedNode.getId(), index);
        }
    }
    for (RuleNode node : toDelete) {
        deleteRuleNode(tenantId, node.getId());
    }
    RuleNodeId firstRuleNodeId = null;
    if (nodes != null) {
        if (ruleChainMetaData.getFirstNodeIndex() != null) {
            firstRuleNodeId = nodes.get(ruleChainMetaData.getFirstNodeIndex()).getId();
        }
        if ((ruleChain.getFirstRuleNodeId() != null && !ruleChain.getFirstRuleNodeId().equals(firstRuleNodeId)) || (ruleChain.getFirstRuleNodeId() == null && firstRuleNodeId != null)) {
            ruleChain.setFirstRuleNodeId(firstRuleNodeId);
            ruleChainDao.save(tenantId, ruleChain);
        }
        if (ruleChainMetaData.getConnections() != null) {
            for (NodeConnectionInfo nodeConnection : ruleChainMetaData.getConnections()) {
                EntityId from = nodes.get(nodeConnection.getFromIndex()).getId();
                EntityId to = nodes.get(nodeConnection.getToIndex()).getId();
                String type = nodeConnection.getType();
                createRelation(tenantId, new EntityRelation(from, to, type, RelationTypeGroup.RULE_NODE));
            }
        }
        if (ruleChainMetaData.getRuleChainConnections() != null) {
            for (RuleChainConnectionInfo nodeToRuleChainConnection : ruleChainMetaData.getRuleChainConnections()) {
                RuleChainId targetRuleChainId = nodeToRuleChainConnection.getTargetRuleChainId();
                RuleChain targetRuleChain = findRuleChainById(TenantId.SYS_TENANT_ID, targetRuleChainId);
                RuleNode targetNode = new RuleNode();
                targetNode.setName(targetRuleChain != null ? targetRuleChain.getName() : "Rule Chain Input");
                targetNode.setRuleChainId(ruleChain.getId());
                targetNode.setType("org.thingsboard.rule.engine.flow.TbRuleChainInputNode");
                var configuration = JacksonUtil.newObjectNode();
                configuration.put("ruleChainId", targetRuleChainId.getId().toString());
                targetNode.setConfiguration(configuration);
                ObjectNode layout = (ObjectNode) nodeToRuleChainConnection.getAdditionalInfo();
                layout.remove("description");
                layout.remove("ruleChainNodeId");
                targetNode.setAdditionalInfo(layout);
                targetNode.setDebugMode(false);
                targetNode = ruleNodeDao.save(tenantId, targetNode);
                EntityRelation sourceRuleChainToRuleNode = new EntityRelation();
                sourceRuleChainToRuleNode.setFrom(ruleChain.getId());
                sourceRuleChainToRuleNode.setTo(targetNode.getId());
                sourceRuleChainToRuleNode.setType(EntityRelation.CONTAINS_TYPE);
                sourceRuleChainToRuleNode.setTypeGroup(RelationTypeGroup.RULE_CHAIN);
                relationService.saveRelation(tenantId, sourceRuleChainToRuleNode);
                EntityRelation sourceRuleNodeToTargetRuleNode = new EntityRelation();
                EntityId from = nodes.get(nodeToRuleChainConnection.getFromIndex()).getId();
                sourceRuleNodeToTargetRuleNode.setFrom(from);
                sourceRuleNodeToTargetRuleNode.setTo(targetNode.getId());
                sourceRuleNodeToTargetRuleNode.setType(nodeToRuleChainConnection.getType());
                sourceRuleNodeToTargetRuleNode.setTypeGroup(RelationTypeGroup.RULE_NODE);
                relationService.saveRelation(tenantId, sourceRuleNodeToTargetRuleNode);
            }
        }
    }
    return RuleChainUpdateResult.successful(updatedRuleNodes);
}
Also used : RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HashMap(java.util.HashMap) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RuleChainConnectionInfo (org.thingsboard.server.common.data.rule.RuleChainConnectionInfo)4 ArrayList (java.util.ArrayList)3 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)3 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 NodeConnectionInfo (org.thingsboard.server.common.data.rule.NodeConnectionInfo)2 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)2 Validator.validateString (org.thingsboard.server.dao.service.Validator.validateString)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 HashMap (java.util.HashMap)1 Transactional (org.springframework.transaction.annotation.Transactional)1 TbRuleChainInputNodeConfiguration (org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)1 PageLink (org.thingsboard.server.common.data.page.PageLink)1 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)1 RuleNodeUpdateResult (org.thingsboard.server.common.data.rule.RuleNodeUpdateResult)1