Search in sources :

Example 16 with RuleChainId

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

the class AbstractEdgeEntity method toEdge.

protected Edge toEdge() {
    Edge edge = new Edge(new EdgeId(getUuid()));
    edge.setCreatedTime(createdTime);
    if (tenantId != null) {
        edge.setTenantId(TenantId.fromUUID(tenantId));
    }
    if (customerId != null) {
        edge.setCustomerId(new CustomerId(customerId));
    }
    if (rootRuleChainId != null) {
        edge.setRootRuleChainId(new RuleChainId(rootRuleChainId));
    }
    edge.setType(type);
    edge.setName(name);
    edge.setLabel(label);
    edge.setRoutingKey(routingKey);
    edge.setSecret(secret);
    edge.setAdditionalInfo(additionalInfo);
    return edge;
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 17 with RuleChainId

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

the class BaseRuleChainServiceTest method testGetDefaultEdgeRuleChains.

@Test
public void testGetDefaultEdgeRuleChains() throws Exception {
    RuleChainId ruleChainId = saveRuleChainAndSetAutoAssignToEdge("Default Edge Rule Chain 1");
    saveRuleChainAndSetAutoAssignToEdge("Default Edge Rule Chain 2");
    PageData<RuleChain> result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, new PageLink(100));
    Assert.assertEquals(2, result.getData().size());
    ruleChainService.unsetAutoAssignToEdgeRuleChain(tenantId, ruleChainId);
    result = ruleChainService.findAutoAssignToEdgeRuleChainsByTenantId(tenantId, new PageLink(100));
    Assert.assertEquals(1, result.getData().size());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) PageLink(org.thingsboard.server.common.data.page.PageLink) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Test(org.junit.Test)

Example 18 with RuleChainId

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

the class RuleChainManagerActor method initRuleChains.

protected void initRuleChains() {
    for (RuleChain ruleChain : new PageDataIterable<>(link -> ruleChainService.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, link), ContextAwareActor.ENTITY_PACK_LIMIT)) {
        RuleChainId ruleChainId = ruleChain.getId();
        log.debug("[{}|{}] Creating rule chain actor", ruleChainId.getEntityType(), ruleChain.getId());
        TbActorRef actorRef = getOrCreateActor(ruleChainId, id -> ruleChain);
        visit(ruleChain, actorRef);
        log.debug("[{}|{}] Rule Chain actor created.", ruleChainId.getEntityType(), ruleChainId.getId());
    }
}
Also used : PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Example 19 with RuleChainId

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

the class AccessValidator method validateRuleChain.

private void validateRuleChain(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<RuleChain> ruleChainFuture = ruleChainService.findRuleChainByIdAsync(currentUser.getTenantId(), new RuleChainId(entityId.getId()));
        Futures.addCallback(ruleChainFuture, getCallback(callback, ruleChain -> {
            if (ruleChain == null) {
                return ValidationResult.entityNotFound("Rule chain with requested id wasn't found!");
            } else {
                try {
                    accessControlService.checkPermission(currentUser, Resource.RULE_CHAIN, operation, entityId, ruleChain);
                } catch (ThingsboardException e) {
                    return ValidationResult.accessDenied(e.getMessage());
                }
                return ValidationResult.ok(ruleChain);
            }
        }), 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) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Example 20 with RuleChainId

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

Aggregations

RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)60 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)22 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)20 Test (org.junit.Test)17 TbMsg (org.thingsboard.server.common.msg.TbMsg)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)12 EntityId (org.thingsboard.server.common.data.id.EntityId)12 ApiOperation (io.swagger.annotations.ApiOperation)11 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 TenantId (org.thingsboard.server.common.data.id.TenantId)11 TbMsgMetaData (org.thingsboard.server.common.msg.TbMsgMetaData)11 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)10 DeviceId (org.thingsboard.server.common.data.id.DeviceId)9 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)8 Futures (com.google.common.util.concurrent.Futures)7 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)7 IOException (java.io.IOException)7 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6