Search in sources :

Example 21 with RuleNode

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

the class BaseController method checkRuleNode.

protected RuleNode checkRuleNode(RuleNodeId ruleNodeId, Operation operation) throws ThingsboardException {
    validateId(ruleNodeId, "Incorrect ruleNodeId " + ruleNodeId);
    RuleNode ruleNode = ruleChainService.findRuleNodeById(getTenantId(), ruleNodeId);
    checkNotNull(ruleNode, "Rule node with id [" + ruleNodeId + "] is not found");
    checkRuleChain(ruleNode.getRuleChainId(), operation);
    return ruleNode;
}
Also used : RuleNode(org.thingsboard.server.common.data.rule.RuleNode)

Example 22 with RuleNode

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

the class MqttClientTest method createRootRuleChainForRpcResponse.

private RuleChainId createRootRuleChainForRpcResponse() throws Exception {
    RuleChain newRuleChain = new RuleChain();
    newRuleChain.setName("testRuleChain");
    ResponseEntity<RuleChain> ruleChainResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain", newRuleChain, RuleChain.class);
    Assert.assertTrue(ruleChainResponse.getStatusCode().is2xxSuccessful());
    RuleChain ruleChain = ruleChainResponse.getBody();
    JsonNode configuration = mapper.readTree(this.getClass().getClassLoader().getResourceAsStream("RpcResponseRuleChainMetadata.json"));
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(ruleChain.getId());
    ruleChainMetaData.setFirstNodeIndex(configuration.get("firstNodeIndex").asInt());
    ruleChainMetaData.setNodes(Arrays.asList(mapper.treeToValue(configuration.get("nodes"), RuleNode[].class)));
    ruleChainMetaData.setConnections(Arrays.asList(mapper.treeToValue(configuration.get("connections"), NodeConnectionInfo[].class)));
    ResponseEntity<RuleChainMetaData> ruleChainMetadataResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain/metadata", ruleChainMetaData, RuleChainMetaData.class);
    Assert.assertTrue(ruleChainMetadataResponse.getStatusCode().is2xxSuccessful());
    // Set a new rule chain as root
    ResponseEntity<RuleChain> rootRuleChainResponse = restClient.getRestTemplate().postForEntity(HTTPS_URL + "/api/ruleChain/{ruleChainId}/root", null, RuleChain.class, ruleChain.getId());
    Assert.assertTrue(rootRuleChainResponse.getStatusCode().is2xxSuccessful());
    return ruleChain.getId();
}
Also used : NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 23 with RuleNode

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

the class BaseRuleChainService method resetRuleChainMetadataTenantIds.

private void resetRuleChainMetadataTenantIds(TenantId tenantId, List<RuleChainMetaData> metaData) {
    for (RuleChainMetaData md : metaData) {
        for (RuleNode node : md.getNodes()) {
            JsonNode nodeConfiguration = node.getConfiguration();
            searchTenantIdRecursive(tenantId, nodeConfiguration);
        }
    }
}
Also used : RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 24 with RuleNode

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

the class BaseRuleChainService method getReferencingRuleChainNodes.

@Override
public List<RuleNode> getReferencingRuleChainNodes(TenantId tenantId, RuleChainId ruleChainId) {
    Validator.validateId(ruleChainId, "Incorrect rule chain id for search request.");
    List<EntityRelation> relations = getNodeToRuleChainRelations(tenantId, ruleChainId);
    List<RuleNode> ruleNodes = new ArrayList<>();
    for (EntityRelation relation : relations) {
        RuleNode ruleNode = ruleNodeDao.findById(tenantId, relation.getFrom().getId());
        if (ruleNode != null) {
            ruleNodes.add(ruleNode);
        }
    }
    return ruleNodes;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList)

Example 25 with RuleNode

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

the class BaseRuleChainService method loadRuleChainMetaData.

@Override
public RuleChainMetaData loadRuleChainMetaData(TenantId tenantId, RuleChainId ruleChainId) {
    Validator.validateId(ruleChainId, "Incorrect rule chain id.");
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainId);
    if (ruleChain == null) {
        return null;
    }
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(ruleChainId);
    List<RuleNode> ruleNodes = getRuleChainNodes(tenantId, ruleChainId);
    Map<RuleNodeId, Integer> ruleNodeIndexMap = new HashMap<>();
    for (RuleNode node : ruleNodes) {
        ruleNodeIndexMap.put(node.getId(), ruleNodes.indexOf(node));
    }
    ruleChainMetaData.setNodes(ruleNodes);
    if (ruleChain.getFirstRuleNodeId() != null) {
        ruleChainMetaData.setFirstNodeIndex(ruleNodeIndexMap.get(ruleChain.getFirstRuleNodeId()));
    }
    for (RuleNode node : ruleNodes) {
        int fromIndex = ruleNodeIndexMap.get(node.getId());
        List<EntityRelation> nodeRelations = getRuleNodeRelations(tenantId, node.getId());
        for (EntityRelation nodeRelation : nodeRelations) {
            String type = nodeRelation.getType();
            if (nodeRelation.getTo().getEntityType() == EntityType.RULE_NODE) {
                RuleNodeId toNodeId = new RuleNodeId(nodeRelation.getTo().getId());
                int toIndex = ruleNodeIndexMap.get(toNodeId);
                ruleChainMetaData.addConnectionInfo(fromIndex, toIndex, type);
            } else if (nodeRelation.getTo().getEntityType() == EntityType.RULE_CHAIN) {
                log.warn("[{}][{}] Unsupported node relation: {}", tenantId, ruleChainId, nodeRelation.getTo());
            }
        }
    }
    return ruleChainMetaData;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) HashMap(java.util.HashMap) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

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