Search in sources :

Example 11 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method exceptionThrownIfCannotFindNewOriginator.

@Test
public void exceptionThrownIfCannotFindNewOriginator() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(null));
    node.onMsg(ctx, msg);
    verify(ctx).tellNext(same(msg), same(FAILURE));
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 12 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method originatorCanBeChangedToCustomerId.

@Test
public void originatorCanBeChangedToCustomerId() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset));
    node.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals(customerId, originatorCaptor.getValue());
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityId(org.thingsboard.server.common.data.id.EntityId) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 13 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class TbChangeOriginatorNodeTest method newChainCanBeStarted.

@Test
public void newChainCanBeStarted() throws TbNodeException {
    init();
    AssetId assetId = new AssetId(Uuids.timeBased());
    CustomerId customerId = new CustomerId(Uuids.timeBased());
    Asset asset = new Asset();
    asset.setCustomerId(customerId);
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("ASSET", assetId, new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
    when(ctx.getAssetService()).thenReturn(assetService);
    when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset));
    node.onMsg(ctx, msg);
    ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
    ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
    ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class);
    ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class);
    verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture());
    assertEquals(customerId, originatorCaptor.getValue());
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) EntityId(org.thingsboard.server.common.data.id.EntityId) Asset(org.thingsboard.server.common.data.asset.Asset) AssetId(org.thingsboard.server.common.data.id.AssetId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

Example 14 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method onTellNext.

private void onTellNext(TbMsg msg, boolean useRuleNodeIdFromMsg) {
    try {
        checkComponentStateActive(msg);
        RuleNodeId targetId = useRuleNodeIdFromMsg ? msg.getRuleNodeId() : null;
        RuleNodeCtx targetCtx;
        if (targetId == null) {
            targetCtx = firstNode;
            msg = msg.copyWithRuleChainId(entityId);
        } else {
            targetCtx = nodeActors.get(targetId);
        }
        if (targetCtx != null) {
            log.trace("[{}][{}] Pushing message to target rule node", entityId, targetId);
            pushMsgToNode(targetCtx, msg, NA_RELATION_TYPE);
        } else {
            log.trace("[{}][{}] Rule node does not exist. Probably old message", entityId, targetId);
            msg.getCallback().onSuccess();
        }
    } catch (RuleNodeException rne) {
        msg.getCallback().onFailure(rne);
    } catch (Exception e) {
        msg.getCallback().onFailure(new RuleEngineException(e.getMessage()));
    }
}
Also used : RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 15 with RuleNodeId

use of org.thingsboard.server.common.data.id.RuleNodeId in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method initRoutes.

private void initRoutes(RuleChain ruleChain, List<RuleNode> ruleNodeList) {
    nodeRoutes.clear();
    // Populating the routes map;
    for (RuleNode ruleNode : ruleNodeList) {
        List<EntityRelation> relations = service.getRuleNodeRelations(TenantId.SYS_TENANT_ID, ruleNode.getId());
        log.trace("[{}][{}][{}] Processing rule node relations [{}]", tenantId, entityId, ruleNode.getId(), relations.size());
        if (relations.size() == 0) {
            nodeRoutes.put(ruleNode.getId(), Collections.emptyList());
        } else {
            for (EntityRelation relation : relations) {
                log.trace("[{}][{}][{}] Processing rule node relation [{}]", tenantId, entityId, ruleNode.getId(), relation.getTo());
                if (relation.getTo().getEntityType() == EntityType.RULE_NODE) {
                    RuleNodeCtx ruleNodeCtx = nodeActors.get(new RuleNodeId(relation.getTo().getId()));
                    if (ruleNodeCtx == null) {
                        throw new IllegalArgumentException("Rule Node [" + relation.getFrom() + "] has invalid relation to Rule node [" + relation.getTo() + "]");
                    }
                }
                nodeRoutes.computeIfAbsent(ruleNode.getId(), k -> new ArrayList<>()).add(new RuleNodeRelation(ruleNode.getId(), relation.getTo(), relation.getType()));
            }
        }
    }
    firstId = ruleChain.getFirstRuleNodeId();
    firstNode = nodeActors.get(firstId);
    state = ComponentLifecycleState.ACTIVE;
}
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) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ArrayList(java.util.ArrayList) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Aggregations

RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)26 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)21 TbMsg (org.thingsboard.server.common.msg.TbMsg)15 EntityId (org.thingsboard.server.common.data.id.EntityId)14 TenantId (org.thingsboard.server.common.data.id.TenantId)12 Test (org.junit.Test)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)9 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Callable (java.util.concurrent.Callable)6 Consumer (java.util.function.Consumer)6 ScriptException (javax.script.ScriptException)6 NotImplementedException (org.apache.commons.lang3.NotImplementedException)6 Assert.assertEquals (org.junit.Assert.assertEquals)6