Search in sources :

Example 26 with RuleChain

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

the class BaseRuleChainServiceTest method testFindRuleChainsByTenantId.

@Test
public void testFindRuleChainsByTenantId() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    List<RuleChain> ruleChains = new ArrayList<>();
    for (int i = 0; i < 165; i++) {
        RuleChain ruleChain = new RuleChain();
        ruleChain.setTenantId(tenantId);
        ruleChain.setName("RuleChain" + i);
        ruleChains.add(ruleChainService.saveRuleChain(ruleChain));
    }
    List<RuleChain> loadedRuleChains = new ArrayList<>();
    PageLink pageLink = new PageLink(16);
    PageData<RuleChain> pageData = null;
    do {
        pageData = ruleChainService.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, pageLink);
        loadedRuleChains.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(ruleChains, idComparator);
    Collections.sort(loadedRuleChains, idComparator);
    Assert.assertEquals(ruleChains, loadedRuleChains);
    ruleChainService.deleteRuleChainsByTenantId(tenantId);
    pageLink = new PageLink(31);
    pageData = ruleChainService.findTenantRuleChainsByType(tenantId, RuleChainType.CORE, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Test(org.junit.Test)

Example 27 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain 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 28 with RuleChain

use of org.thingsboard.server.common.data.rule.RuleChain 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 29 with RuleChain

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

the class InstallScripts method createRuleChainFromFile.

public RuleChain createRuleChainFromFile(TenantId tenantId, Path templateFilePath, String newRuleChainName) throws IOException {
    JsonNode ruleChainJson = objectMapper.readTree(templateFilePath.toFile());
    RuleChain ruleChain = objectMapper.treeToValue(ruleChainJson.get("ruleChain"), RuleChain.class);
    RuleChainMetaData ruleChainMetaData = objectMapper.treeToValue(ruleChainJson.get("metadata"), RuleChainMetaData.class);
    ruleChain.setTenantId(tenantId);
    if (!StringUtils.isEmpty(newRuleChainName)) {
        ruleChain.setName(newRuleChainName);
    }
    ruleChain = ruleChainService.saveRuleChain(ruleChain);
    ruleChainMetaData.setRuleChainId(ruleChain.getId());
    ruleChainService.saveRuleChainMetaData(TenantId.SYS_TENANT_ID, ruleChainMetaData);
    return ruleChain;
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 30 with RuleChain

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

the class RuleChainEdgeProcessor method processRuleChainMetadataToEdge.

public DownlinkMsg processRuleChainMetadataToEdge(EdgeEvent edgeEvent, UpdateMsgType msgType, EdgeVersion edgeVersion) {
    RuleChainId ruleChainId = new RuleChainId(edgeEvent.getEntityId());
    RuleChain ruleChain = ruleChainService.findRuleChainById(edgeEvent.getTenantId(), ruleChainId);
    DownlinkMsg downlinkMsg = null;
    if (ruleChain != null) {
        RuleChainMetaData ruleChainMetaData = ruleChainService.loadRuleChainMetaData(edgeEvent.getTenantId(), ruleChainId);
        RuleChainMetadataUpdateMsg ruleChainMetadataUpdateMsg = ruleChainMsgConstructor.constructRuleChainMetadataUpdatedMsg(msgType, ruleChainMetaData, edgeVersion);
        if (ruleChainMetadataUpdateMsg != null) {
            downlinkMsg = DownlinkMsg.newBuilder().setDownlinkMsgId(EdgeUtils.nextPositiveInt()).addRuleChainMetadataUpdateMsg(ruleChainMetadataUpdateMsg).build();
        }
    }
    return downlinkMsg;
}
Also used : DownlinkMsg(org.thingsboard.server.gen.edge.v1.DownlinkMsg) RuleChainMetadataUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainMetadataUpdateMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

Aggregations

RuleChain (org.thingsboard.server.common.data.rule.RuleChain)67 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)26 Test (org.junit.Test)20 RuleNode (org.thingsboard.server.common.data.rule.RuleNode)19 ArrayList (java.util.ArrayList)16 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)15 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)14 ApiOperation (io.swagger.annotations.ApiOperation)13 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)13 Edge (org.thingsboard.server.common.data.edge.Edge)13 TenantId (org.thingsboard.server.common.data.id.TenantId)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11 PageLink (org.thingsboard.server.common.data.page.PageLink)11 RuleNodeId (org.thingsboard.server.common.data.id.RuleNodeId)10 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)10 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)9 EdgeId (org.thingsboard.server.common.data.id.EdgeId)8 List (java.util.List)7 Device (org.thingsboard.server.common.data.Device)7