Search in sources :

Example 16 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData 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 17 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData 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 18 with RuleChainMetaData

use of org.thingsboard.server.common.data.rule.RuleChainMetaData 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)

Example 19 with RuleChainMetaData

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

the class BaseRuleChainService method resetRuleNodeIds.

private void resetRuleNodeIds(List<RuleChainMetaData> metaData) {
    for (RuleChainMetaData md : metaData) {
        for (RuleNode node : md.getNodes()) {
            node.setId(null);
            node.setRuleChainId(null);
        }
    }
}
Also used : RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode)

Example 20 with RuleChainMetaData

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

the class BaseRuleChainServiceTest method createRuleChainMetadata.

private RuleChainMetaData createRuleChainMetadata() throws Exception {
    RuleChain ruleChain = new RuleChain();
    ruleChain.setName("My RuleChain");
    ruleChain.setTenantId(tenantId);
    RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain);
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(savedRuleChain.getId());
    ObjectMapper mapper = new ObjectMapper();
    RuleNode ruleNode1 = new RuleNode();
    ruleNode1.setName("name1");
    ruleNode1.setType("type1");
    ruleNode1.setConfiguration(mapper.readTree("\"key1\": \"val1\""));
    RuleNode ruleNode2 = new RuleNode();
    ruleNode2.setName("name2");
    ruleNode2.setType("type2");
    ruleNode2.setConfiguration(mapper.readTree("\"key2\": \"val2\""));
    RuleNode ruleNode3 = new RuleNode();
    ruleNode3.setName("name3");
    ruleNode3.setType("type3");
    ruleNode3.setConfiguration(mapper.readTree("\"key3\": \"val3\""));
    List<RuleNode> ruleNodes = new ArrayList<>();
    ruleNodes.add(ruleNode1);
    ruleNodes.add(ruleNode2);
    ruleNodes.add(ruleNode3);
    ruleChainMetaData.setFirstNodeIndex(0);
    ruleChainMetaData.setNodes(ruleNodes);
    ruleChainMetaData.addConnectionInfo(0, 1, "success");
    ruleChainMetaData.addConnectionInfo(0, 2, "fail");
    ruleChainMetaData.addConnectionInfo(1, 2, "success");
    Assert.assertTrue(ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData).isSuccess());
    return ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)23 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)15 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)12 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)5 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 NodeConnectionInfo (org.thingsboard.server.common.data.rule.NodeConnectionInfo)4 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Slf4j (lombok.extern.slf4j.Slf4j)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 TbGetAttributesNodeConfiguration (org.thingsboard.rule.engine.metadata.TbGetAttributesNodeConfiguration)3 Device (org.thingsboard.server.common.data.Device)3 Event (org.thingsboard.server.common.data.Event)3 RuleChainMetadataUpdateMsg (org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2