Search in sources :

Example 16 with RuleNodeId

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

the class DefaultTbContext method enqueueForTellNext.

private void enqueueForTellNext(TopicPartitionInfo tpi, String queueName, TbMsg source, Set<String> relationTypes, String failureMessage, Runnable onSuccess, Consumer<Throwable> onFailure) {
    if (!source.isValid()) {
        log.trace("[{}] Skip invalid message: {}", getTenantId(), source);
        onFailure.accept(new IllegalArgumentException("Source message is no longer valid!"));
        return;
    }
    RuleChainId ruleChainId = nodeCtx.getSelf().getRuleChainId();
    RuleNodeId ruleNodeId = nodeCtx.getSelf().getId();
    TbMsg tbMsg = TbMsg.newMsg(source, queueName, ruleChainId, ruleNodeId);
    TransportProtos.ToRuleEngineMsg.Builder msg = TransportProtos.ToRuleEngineMsg.newBuilder().setTenantIdMSB(getTenantId().getId().getMostSignificantBits()).setTenantIdLSB(getTenantId().getId().getLeastSignificantBits()).setTbMsg(TbMsg.toByteString(tbMsg)).addAllRelationTypes(relationTypes);
    if (failureMessage != null) {
        msg.setFailureMessage(failureMessage);
    }
    if (nodeCtx.getSelf().isDebugMode()) {
        relationTypes.forEach(relationType -> mainCtx.persistDebugOutput(nodeCtx.getTenantId(), nodeCtx.getSelf().getId(), tbMsg, relationType, null, failureMessage));
    }
    mainCtx.getClusterService().pushMsgToRuleEngine(tpi, tbMsg.getId(), msg.build(), new SimpleTbQueueCallback(onSuccess, onFailure));
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 17 with RuleNodeId

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

the class TbMsg method fromBytes.

public static TbMsg fromBytes(String queueName, byte[] data, TbMsgCallback callback) {
    try {
        MsgProtos.TbMsgProto proto = MsgProtos.TbMsgProto.parseFrom(data);
        TbMsgMetaData metaData = new TbMsgMetaData(proto.getMetaData().getDataMap());
        EntityId entityId = EntityIdFactory.getByTypeAndUuid(proto.getEntityType(), new UUID(proto.getEntityIdMSB(), proto.getEntityIdLSB()));
        CustomerId customerId = null;
        RuleChainId ruleChainId = null;
        RuleNodeId ruleNodeId = null;
        if (proto.getCustomerIdMSB() != 0L && proto.getCustomerIdLSB() != 0L) {
            customerId = new CustomerId(new UUID(proto.getCustomerIdMSB(), proto.getCustomerIdLSB()));
        }
        if (proto.getRuleChainIdMSB() != 0L && proto.getRuleChainIdLSB() != 0L) {
            ruleChainId = new RuleChainId(new UUID(proto.getRuleChainIdMSB(), proto.getRuleChainIdLSB()));
        }
        if (proto.getRuleNodeIdMSB() != 0L && proto.getRuleNodeIdLSB() != 0L) {
            ruleNodeId = new RuleNodeId(new UUID(proto.getRuleNodeIdMSB(), proto.getRuleNodeIdLSB()));
        }
        TbMsgProcessingCtx ctx;
        if (proto.hasCtx()) {
            ctx = TbMsgProcessingCtx.fromProto(proto.getCtx());
        } else {
            // Backward compatibility with unprocessed messages fetched from queue after update.
            ctx = new TbMsgProcessingCtx(proto.getRuleNodeExecCounter());
        }
        TbMsgDataType dataType = TbMsgDataType.values()[proto.getDataType()];
        return new TbMsg(queueName, UUID.fromString(proto.getId()), proto.getTs(), proto.getType(), entityId, customerId, metaData, dataType, proto.getData(), ruleChainId, ruleNodeId, ctx, callback);
    } catch (InvalidProtocolBufferException e) {
        throw new IllegalStateException("Could not parse protobuf for TbMsg", e);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) MsgProtos(org.thingsboard.server.common.msg.gen.MsgProtos) EntityId(org.thingsboard.server.common.data.id.EntityId) UUID(java.util.UUID) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 18 with RuleNodeId

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

the class AccessValidator method validateRule.

private void validateRule(final SecurityUser currentUser, Operation operation, EntityId entityId, FutureCallback<ValidationResult> callback) {
    if (currentUser.isCustomerUser()) {
        callback.onSuccess(ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION));
    } else {
        ListenableFuture<RuleNode> ruleNodeFuture = ruleChainService.findRuleNodeByIdAsync(currentUser.getTenantId(), new RuleNodeId(entityId.getId()));
        Futures.addCallback(ruleNodeFuture, getCallback(callback, ruleNodeTmp -> {
            RuleNode ruleNode = ruleNodeTmp;
            if (ruleNode == null) {
                return ValidationResult.entityNotFound("Rule node with requested id wasn't found!");
            } else if (ruleNode.getRuleChainId() == null) {
                return ValidationResult.entityNotFound("Rule chain with requested node id wasn't found!");
            } else {
                // TODO: make async
                RuleChain ruleChain = ruleChainService.findRuleChainById(currentUser.getTenantId(), ruleNode.getRuleChainId());
                try {
                    accessControlService.checkPermission(currentUser, Resource.RULE_CHAIN, operation, ruleNode.getRuleChainId(), ruleChain);
                } catch (ThingsboardException e) {
                    return ValidationResult.accessDenied(e.getMessage());
                }
                return ValidationResult.ok(ruleNode);
            }
        }), executor);
    }
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) HttpValidationCallback(org.thingsboard.server.controller.HttpValidationCallback) Customer(org.thingsboard.server.common.data.Customer) Autowired(org.springframework.beans.factory.annotation.Autowired) DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) ToErrorResponseEntity(org.thingsboard.server.service.telemetry.exception.ToErrorResponseEntity) PreDestroy(javax.annotation.PreDestroy) TenantService(org.thingsboard.server.dao.tenant.TenantService) Rpc(org.thingsboard.server.common.data.rpc.Rpc) ResourceService(org.thingsboard.server.dao.resource.ResourceService) RpcService(org.thingsboard.server.dao.rpc.RpcService) ApiUsageStateService(org.thingsboard.server.dao.usagerecord.ApiUsageStateService) EntityViewService(org.thingsboard.server.dao.entityview.EntityViewService) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) EdgeId(org.thingsboard.server.common.data.id.EdgeId) AssetService(org.thingsboard.server.dao.asset.AssetService) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Function(com.google.common.base.Function) DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) EdgeService(org.thingsboard.server.dao.edge.EdgeService) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) ApiUsageStateId(org.thingsboard.server.common.data.id.ApiUsageStateId) OtaPackageId(org.thingsboard.server.common.data.id.OtaPackageId) Executors(java.util.concurrent.Executors) EntityView(org.thingsboard.server.common.data.EntityView) PostConstruct(javax.annotation.PostConstruct) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Operation(org.thingsboard.server.service.security.permission.Operation) EntityViewId(org.thingsboard.server.common.data.id.EntityViewId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) OtaPackageService(org.thingsboard.server.dao.ota.OtaPackageService) UserService(org.thingsboard.server.dao.user.UserService) OtaPackageInfo(org.thingsboard.server.common.data.OtaPackageInfo) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Device(org.thingsboard.server.common.data.Device) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AssetId(org.thingsboard.server.common.data.id.AssetId) Tenant(org.thingsboard.server.common.data.Tenant) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) User(org.thingsboard.server.common.data.User) EntityIdFactory(org.thingsboard.server.common.data.id.EntityIdFactory) DeviceService(org.thingsboard.server.dao.device.DeviceService) AlarmService(org.thingsboard.server.dao.alarm.AlarmService) EntityId(org.thingsboard.server.common.data.id.EntityId) BiConsumer(java.util.function.BiConsumer) CustomerService(org.thingsboard.server.dao.customer.CustomerService) Nullable(javax.annotation.Nullable) ExecutorService(java.util.concurrent.ExecutorService) RpcId(org.thingsboard.server.common.data.id.RpcId) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) TbResourceId(org.thingsboard.server.common.data.id.TbResourceId) FutureCallback(com.google.common.util.concurrent.FutureCallback) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) DeviceProfileService(org.thingsboard.server.dao.device.DeviceProfileService) HttpStatus(org.springframework.http.HttpStatus) Futures(com.google.common.util.concurrent.Futures) Component(org.springframework.stereotype.Component) UserId(org.thingsboard.server.common.data.id.UserId) TbResourceInfo(org.thingsboard.server.common.data.TbResourceInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Resource(org.thingsboard.server.service.security.permission.Resource) ResponseEntity(org.springframework.http.ResponseEntity) Asset(org.thingsboard.server.common.data.asset.Asset) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 19 with RuleNodeId

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

the class RuleChainMsgConstructorTest method createRuleNode.

private RuleNode createRuleNode(RuleChainId ruleChainId, String type, String name, JsonNode configuration, JsonNode additionalInfo) {
    RuleNode e = new RuleNode();
    e.setRuleChainId(ruleChainId);
    e.setType(type);
    e.setName(name);
    e.setDebugMode(false);
    e.setConfiguration(configuration);
    e.setAdditionalInfo(additionalInfo);
    e.setId(new RuleNodeId(UUID.randomUUID()));
    return e;
}
Also used : RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 20 with RuleNodeId

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

the class TbTransformMsgNodeTest method exceptionHandledCorrectly.

@Test
public void exceptionHandledCorrectly() throws TbNodeException, ScriptException {
    initWithScript();
    TbMsgMetaData metaData = new TbMsgMetaData();
    metaData.putValue("temp", "7");
    String rawJson = "{\"passed\": 5";
    RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
    RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
    TbMsg msg = TbMsg.newMsg("USER", null, metaData, TbMsgDataType.JSON, rawJson, ruleChainId, ruleNodeId);
    when(scriptEngine.executeUpdateAsync(msg)).thenReturn(Futures.immediateFailedFuture(new IllegalStateException("error")));
    node.onMsg(ctx, msg);
    verifyError(msg, "error", IllegalStateException.class);
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TbMsgMetaData(org.thingsboard.server.common.msg.TbMsgMetaData) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) Test(org.junit.Test)

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