Search in sources :

Example 1 with RuleNode

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

the class RuleNodeActorMessageProcessor method onUpdate.

@Override
public void onUpdate(TbActorCtx context) throws Exception {
    RuleNode newRuleNode = systemContext.getRuleChainService().findRuleNodeById(tenantId, entityId);
    this.info = new RuleNodeInfo(entityId, ruleChainName, newRuleNode != null ? newRuleNode.getName() : "Unknown");
    boolean restartRequired = state != ComponentLifecycleState.ACTIVE || !(ruleNode.getType().equals(newRuleNode.getType()) && ruleNode.getConfiguration().equals(newRuleNode.getConfiguration()));
    this.ruleNode = newRuleNode;
    this.defaultCtx.updateSelf(newRuleNode);
    if (restartRequired) {
        if (tbNode != null) {
            tbNode.destroy();
        }
        try {
            start(context);
        } catch (Exception e) {
            throw new TbRuleNodeUpdateException("Failed to update rule node", e);
        }
    }
}
Also used : RuleNodeInfo(org.thingsboard.server.common.msg.queue.RuleNodeInfo) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbRuleNodeUpdateException(org.thingsboard.server.actors.TbRuleNodeUpdateException) TbRuleNodeUpdateException(org.thingsboard.server.actors.TbRuleNodeUpdateException) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException)

Example 2 with RuleNode

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

the class DefaultTbRuleChainService method getOutputLabelUsage.

@Override
public List<RuleChainOutputLabelsUsage> getOutputLabelUsage(TenantId tenantId, RuleChainId ruleChainId) {
    List<RuleNode> ruleNodes = ruleChainService.findRuleNodesByTenantIdAndType(tenantId, TbRuleChainInputNode.class.getName(), ruleChainId.getId().toString());
    Map<RuleChainId, String> ruleChainNamesCache = new HashMap<>();
    // Additional filter, "just in case" the structure of the JSON configuration will change.
    var filteredRuleNodes = ruleNodes.stream().filter(node -> {
        try {
            TbRuleChainInputNodeConfiguration configuration = JacksonUtil.treeToValue(node.getConfiguration(), TbRuleChainInputNodeConfiguration.class);
            return ruleChainId.getId().toString().equals(configuration.getRuleChainId());
        } catch (Exception e) {
            log.warn("[{}][{}] Failed to decode rule node configuration", tenantId, ruleChainId, e);
            return false;
        }
    }).collect(Collectors.toList());
    return filteredRuleNodes.stream().map(ruleNode -> {
        RuleChainOutputLabelsUsage usage = new RuleChainOutputLabelsUsage();
        usage.setRuleNodeId(ruleNode.getId());
        usage.setRuleNodeName(ruleNode.getName());
        usage.setRuleChainId(ruleNode.getRuleChainId());
        List<EntityRelation> relations = ruleChainService.getRuleNodeRelations(tenantId, ruleNode.getId());
        if (relations != null && !relations.isEmpty()) {
            usage.setLabels(relations.stream().map(EntityRelation::getType).collect(Collectors.toSet()));
        }
        return usage;
    }).filter(usage -> usage.getLabels() != null).peek(usage -> {
        String ruleChainName = ruleChainNamesCache.computeIfAbsent(usage.getRuleChainId(), id -> ruleChainService.findRuleChainById(tenantId, id).getName());
        usage.setRuleChainName(ruleChainName);
    }).sorted(Comparator.comparing(RuleChainOutputLabelsUsage::getRuleChainName).thenComparing(RuleChainOutputLabelsUsage::getRuleNodeName)).collect(Collectors.toList());
}
Also used : RequiredArgsConstructor(lombok.RequiredArgsConstructor) JacksonUtil(org.thingsboard.common.util.JacksonUtil) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) HashMap(java.util.HashMap) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) TreeSet(java.util.TreeSet) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RelationService(org.thingsboard.server.dao.relation.RelationService) HashSet(java.util.HashSet) RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) TbCoreComponent(org.thingsboard.server.queue.util.TbCoreComponent) Service(org.springframework.stereotype.Service) Map(java.util.Map) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) Set(java.util.Set) Collectors(java.util.stream.Collectors) TbRuleChainOutputNode(org.thingsboard.rule.engine.flow.TbRuleChainOutputNode) RuleChainOutputLabelsUsage(org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Comparator(java.util.Comparator) Collections(java.util.Collections) RuleChainOutputLabelsUsage(org.thingsboard.server.common.data.rule.RuleChainOutputLabelsUsage) HashMap(java.util.HashMap) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode)

Example 3 with RuleNode

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

the class RuleChainActorMessageProcessor method start.

@Override
public void start(TbActorCtx context) {
    if (!started) {
        RuleChain ruleChain = service.findRuleChainById(tenantId, entityId);
        if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
            List<RuleNode> ruleNodeList = service.getRuleChainNodes(tenantId, entityId);
            log.trace("[{}][{}] Starting rule chain with {} nodes", tenantId, entityId, ruleNodeList.size());
            // Creating and starting the actors;
            for (RuleNode ruleNode : ruleNodeList) {
                log.trace("[{}][{}] Creating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                TbActorRef ruleNodeActor = createRuleNodeActor(context, ruleNode);
                nodeActors.put(ruleNode.getId(), new RuleNodeCtx(tenantId, self, ruleNodeActor, ruleNode));
            }
            initRoutes(ruleChain, ruleNodeList);
            started = true;
        }
    } else {
        onUpdate(context);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbActorRef(org.thingsboard.server.actors.TbActorRef)

Example 4 with RuleNode

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

the class RuleChainActorMessageProcessor method onUpdate.

@Override
public void onUpdate(TbActorCtx context) {
    RuleChain ruleChain = service.findRuleChainById(tenantId, entityId);
    if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
        ruleChainName = ruleChain.getName();
        List<RuleNode> ruleNodeList = service.getRuleChainNodes(tenantId, entityId);
        log.trace("[{}][{}] Updating rule chain with {} nodes", tenantId, entityId, ruleNodeList.size());
        for (RuleNode ruleNode : ruleNodeList) {
            RuleNodeCtx existing = nodeActors.get(ruleNode.getId());
            if (existing == null) {
                log.trace("[{}][{}] Creating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                TbActorRef ruleNodeActor = createRuleNodeActor(context, ruleNode);
                nodeActors.put(ruleNode.getId(), new RuleNodeCtx(tenantId, self, ruleNodeActor, ruleNode));
            } else {
                log.trace("[{}][{}] Updating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                existing.setSelf(ruleNode);
                existing.getSelfActor().tellWithHighPriority(new RuleNodeUpdatedMsg(tenantId, existing.getSelf().getId()));
            }
        }
        Set<RuleNodeId> existingNodes = ruleNodeList.stream().map(RuleNode::getId).collect(Collectors.toSet());
        List<RuleNodeId> removedRules = nodeActors.keySet().stream().filter(node -> !existingNodes.contains(node)).collect(Collectors.toList());
        removedRules.forEach(ruleNodeId -> {
            log.trace("[{}][{}] Removing rule node [{}]", tenantId, entityId, ruleNodeId);
            RuleNodeCtx removed = nodeActors.remove(ruleNodeId);
            removed.getSelfActor().tellWithHighPriority(new ComponentLifecycleMsg(tenantId, removed.getSelf().getId(), ComponentLifecycleEvent.DELETED));
        });
        initRoutes(ruleChain, ruleNodeList);
    }
}
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) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 5 with RuleNode

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

the class DefaultDataUpdateService method updateNestedRuleChains.

private void updateNestedRuleChains() {
    try {
        var packSize = 1024;
        var updated = 0;
        boolean hasNext = true;
        while (hasNext) {
            List<EntityRelation> relations = relationService.findRuleNodeToRuleChainRelations(TenantId.SYS_TENANT_ID, RuleChainType.CORE, packSize);
            hasNext = relations.size() == packSize;
            for (EntityRelation relation : relations) {
                try {
                    RuleNodeId sourceNodeId = new RuleNodeId(relation.getFrom().getId());
                    RuleNode sourceNode = ruleChainService.findRuleNodeById(TenantId.SYS_TENANT_ID, sourceNodeId);
                    if (sourceNode == null) {
                        log.info("Skip processing of relation for non existing source rule node: [{}]", sourceNodeId);
                        relationService.deleteRelation(TenantId.SYS_TENANT_ID, relation);
                        continue;
                    }
                    RuleChainId sourceRuleChainId = sourceNode.getRuleChainId();
                    RuleChainId targetRuleChainId = new RuleChainId(relation.getTo().getId());
                    RuleChain targetRuleChain = ruleChainService.findRuleChainById(TenantId.SYS_TENANT_ID, targetRuleChainId);
                    if (targetRuleChain == null) {
                        log.info("Skip processing of relation for non existing target rule chain: [{}]", targetRuleChainId);
                        relationService.deleteRelation(TenantId.SYS_TENANT_ID, relation);
                        continue;
                    }
                    TenantId tenantId = targetRuleChain.getTenantId();
                    RuleNode targetNode = new RuleNode();
                    targetNode.setName(targetRuleChain.getName());
                    targetNode.setRuleChainId(sourceRuleChainId);
                    targetNode.setType(TbRuleChainInputNode.class.getName());
                    TbRuleChainInputNodeConfiguration configuration = new TbRuleChainInputNodeConfiguration();
                    configuration.setRuleChainId(targetRuleChain.getId().toString());
                    targetNode.setConfiguration(JacksonUtil.valueToTree(configuration));
                    targetNode.setAdditionalInfo(relation.getAdditionalInfo());
                    targetNode.setDebugMode(false);
                    targetNode = ruleChainService.saveRuleNode(tenantId, targetNode);
                    EntityRelation sourceRuleChainToRuleNode = new EntityRelation();
                    sourceRuleChainToRuleNode.setFrom(sourceRuleChainId);
                    sourceRuleChainToRuleNode.setTo(targetNode.getId());
                    sourceRuleChainToRuleNode.setType(EntityRelation.CONTAINS_TYPE);
                    sourceRuleChainToRuleNode.setTypeGroup(RelationTypeGroup.RULE_CHAIN);
                    relationService.saveRelation(tenantId, sourceRuleChainToRuleNode);
                    EntityRelation sourceRuleNodeToTargetRuleNode = new EntityRelation();
                    sourceRuleNodeToTargetRuleNode.setFrom(sourceNode.getId());
                    sourceRuleNodeToTargetRuleNode.setTo(targetNode.getId());
                    sourceRuleNodeToTargetRuleNode.setType(relation.getType());
                    sourceRuleNodeToTargetRuleNode.setTypeGroup(RelationTypeGroup.RULE_NODE);
                    sourceRuleNodeToTargetRuleNode.setAdditionalInfo(relation.getAdditionalInfo());
                    relationService.saveRelation(tenantId, sourceRuleNodeToTargetRuleNode);
                    // Delete old relation
                    relationService.deleteRelation(tenantId, relation);
                    updated++;
                } catch (Exception e) {
                    log.info("Failed to update RuleNodeToRuleChainRelation: {}", relation, e);
                }
            }
            if (updated > 0) {
                log.info("RuleNodeToRuleChainRelations: {} entities updated so far...", updated);
            }
        }
    } catch (Exception e) {
        log.error("Unable to update Tenant", e);
    }
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbRuleChainInputNodeConfiguration(org.thingsboard.rule.engine.flow.TbRuleChainInputNodeConfiguration) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) ExecutionException(java.util.concurrent.ExecutionException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TbRuleChainInputNode(org.thingsboard.rule.engine.flow.TbRuleChainInputNode)

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