Search in sources :

Example 6 with RuleChainMetaData

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

the class BaseEdgeTest method createRuleChainMetadata.

private void createRuleChainMetadata(RuleChain ruleChain) throws Exception {
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(ruleChain.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");
    doPost("/api/ruleChain/metadata", ruleChainMetaData, RuleChainMetaData.class);
}
Also used : 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)

Example 7 with RuleChainMetaData

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

the class RuleChainMsgConstructorTest method createRuleChainMetaData.

private RuleChainMetaData createRuleChainMetaData(RuleChainId ruleChainId, Integer firstNodeIndex, List<RuleNode> nodes, List<NodeConnectionInfo> connections) {
    RuleChainMetaData ruleChainMetaData = new RuleChainMetaData();
    ruleChainMetaData.setRuleChainId(ruleChainId);
    ruleChainMetaData.setFirstNodeIndex(firstNodeIndex);
    ruleChainMetaData.setNodes(nodes);
    ruleChainMetaData.setConnections(connections);
    ruleChainMetaData.setRuleChainConnections(null);
    return ruleChainMetaData;
}
Also used : RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData)

Example 8 with RuleChainMetaData

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

the class RuleChainController method saveRuleChainMetaData.

@ApiOperation(value = "Update Rule Chain Metadata", notes = "Updates the rule chain metadata. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/metadata", method = RequestMethod.POST)
@ResponseBody
public RuleChainMetaData saveRuleChainMetaData(@ApiParam(value = "A JSON value representing the rule chain metadata.") @RequestBody RuleChainMetaData ruleChainMetaData, @ApiParam(value = "Update related rule nodes.") @RequestParam(value = "updateRelated", required = false, defaultValue = "true") boolean updateRelated) throws ThingsboardException {
    try {
        TenantId tenantId = getTenantId();
        if (debugPerTenantEnabled) {
            ConcurrentMap<TenantId, DebugTbRateLimits> debugPerTenantLimits = actorContext.getDebugPerTenantLimits();
            DebugTbRateLimits debugTbRateLimits = debugPerTenantLimits.getOrDefault(tenantId, null);
            if (debugTbRateLimits != null) {
                debugPerTenantLimits.remove(tenantId, debugTbRateLimits);
            }
        }
        RuleChain ruleChain = checkRuleChain(ruleChainMetaData.getRuleChainId(), Operation.WRITE);
        RuleChainUpdateResult result = ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData);
        checkNotNull(result.isSuccess() ? true : null);
        List<RuleChain> updatedRuleChains;
        if (updateRelated && result.isSuccess()) {
            updatedRuleChains = tbRuleChainService.updateRelatedRuleChains(tenantId, ruleChainMetaData.getRuleChainId(), result);
        } else {
            updatedRuleChains = Collections.emptyList();
        }
        RuleChainMetaData savedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId()));
        if (RuleChainType.CORE.equals(ruleChain.getType())) {
            tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED);
            updatedRuleChains.forEach(updatedRuleChain -> {
                tbClusterService.broadcastEntityStateChangeEvent(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), ComponentLifecycleEvent.UPDATED);
            });
        }
        logEntityAction(ruleChain.getId(), ruleChain, null, ActionType.UPDATED, null, ruleChainMetaData);
        for (RuleChain updatedRuleChain : updatedRuleChains) {
            RuleChainMetaData updatedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, updatedRuleChain.getId()));
            logEntityAction(updatedRuleChain.getId(), updatedRuleChain, null, ActionType.UPDATED, null, updatedRuleChainMetaData);
        }
        if (RuleChainType.EDGE.equals(ruleChain.getType())) {
            sendEntityNotificationMsg(ruleChain.getTenantId(), ruleChain.getId(), EdgeEventActionType.UPDATED);
            updatedRuleChains.forEach(updatedRuleChain -> {
                sendEntityNotificationMsg(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), EdgeEventActionType.UPDATED);
            });
        }
        return savedRuleChainMetaData;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, ruleChainMetaData);
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 9 with RuleChainMetaData

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

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

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