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));
}
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());
}
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());
}
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()));
}
}
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;
}
Aggregations