Search in sources :

Example 11 with RuleChain

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

the class BaseRuleChainService method assignRuleChainToEdge.

@Override
public RuleChain assignRuleChainToEdge(TenantId tenantId, RuleChainId ruleChainId, EdgeId edgeId) {
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainId);
    Edge edge = edgeService.findEdgeById(tenantId, edgeId);
    if (edge == null) {
        throw new DataValidationException("Can't assign ruleChain to non-existent edge!");
    }
    if (!edge.getTenantId().equals(ruleChain.getTenantId())) {
        throw new DataValidationException("Can't assign ruleChain to edge from different tenant!");
    }
    if (!RuleChainType.EDGE.equals(ruleChain.getType())) {
        throw new DataValidationException("Can't assign non EDGE ruleChain to edge!");
    }
    try {
        createRelation(tenantId, new EntityRelation(edgeId, ruleChainId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE));
    } catch (Exception e) {
        log.warn("[{}] Failed to create ruleChain relation. Edge Id: [{}]", ruleChainId, edgeId);
        throw new RuntimeException(e);
    }
    return ruleChain;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Edge(org.thingsboard.server.common.data.edge.Edge) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Example 12 with RuleChain

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

the class BaseRuleChainService method exportTenantRuleChains.

@Override
public RuleChainData exportTenantRuleChains(TenantId tenantId, PageLink pageLink) {
    Validator.validateId(tenantId, "Incorrect tenant id for search rule chain request.");
    Validator.validatePageLink(pageLink);
    PageData<RuleChain> ruleChainData = ruleChainDao.findRuleChainsByTenantId(tenantId.getId(), pageLink);
    List<RuleChain> ruleChains = ruleChainData.getData();
    List<RuleChainMetaData> metadata = ruleChains.stream().map(rc -> loadRuleChainMetaData(tenantId, rc.getId())).collect(Collectors.toList());
    RuleChainData rcData = new RuleChainData();
    rcData.setRuleChains(ruleChains);
    rcData.setMetadata(metadata);
    setRandomRuleChainIds(rcData);
    resetRuleNodeIds(metadata);
    return rcData;
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) BaseData(org.thingsboard.server.common.data.BaseData) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) Autowired(org.springframework.beans.factory.annotation.Autowired) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) RuleChainConnectionInfo(org.thingsboard.server.common.data.rule.RuleChainConnectionInfo) JsonNode(com.fasterxml.jackson.databind.JsonNode) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) PageLink(org.thingsboard.server.common.data.page.PageLink) Collection(java.util.Collection) Set(java.util.Set) RuleChainData(org.thingsboard.server.common.data.rule.RuleChainData) Collectors(java.util.stream.Collectors) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) Optional(java.util.Optional) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) TENANT(org.thingsboard.server.common.data.DataConstants.TENANT) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) RuleChainImportResult(org.thingsboard.server.common.data.rule.RuleChainImportResult) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) JacksonUtil(org.thingsboard.common.util.JacksonUtil) HashMap(java.util.HashMap) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) CollectionUtils(org.apache.commons.collections.CollectionUtils) Service(org.springframework.stereotype.Service) EntityId(org.thingsboard.server.common.data.id.EntityId) Iterator(java.util.Iterator) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) ConstraintValidator(org.thingsboard.server.dao.service.ConstraintValidator) Validator(org.thingsboard.server.dao.service.Validator) PageData(org.thingsboard.server.common.data.page.PageData) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RelationTypeGroup(org.thingsboard.server.common.data.relation.RelationTypeGroup) NodeConnectionInfo(org.thingsboard.server.common.data.rule.NodeConnectionInfo) Transactional(org.springframework.transaction.annotation.Transactional) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainData(org.thingsboard.server.common.data.rule.RuleChainData)

Example 13 with RuleChain

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

the class EdgeController method processEdgesBulkImport.

@ApiOperation(value = "Import the bulk of edges (processEdgesBulkImport)", notes = "There's an ability to import the bulk of edges using the only .csv file." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@PostMapping("/edge/bulk_import")
public BulkImportResult<Edge> processEdgesBulkImport(@RequestBody BulkImportRequest request) throws Exception {
    SecurityUser user = getCurrentUser();
    RuleChain edgeTemplateRootRuleChain = ruleChainService.getEdgeTemplateRootRuleChain(user.getTenantId());
    if (edgeTemplateRootRuleChain == null) {
        throw new DataValidationException("Root edge rule chain is not available!");
    }
    return edgeBulkImportService.processBulkImport(request, user, importedAssetInfo -> {
        try {
            onEdgeCreatedOrUpdated(user.getTenantId(), importedAssetInfo.getEntity(), edgeTemplateRootRuleChain, importedAssetInfo.isUpdated(), user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) PostMapping(org.springframework.web.bind.annotation.PostMapping) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 14 with RuleChain

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

the class RuleChainController method saveRuleChainMetaData.

@ApiOperation(value = "Update Rule Chain Metadata", notes = "Updates the rule chain metadata. " + RULE_CHAIN_METADATA_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/metadata", method = RequestMethod.POST)
@ResponseBody
public RuleChainMetaData saveRuleChainMetaData(@ApiParam(value = "A JSON value representing the rule chain metadata.") @RequestBody RuleChainMetaData ruleChainMetaData, @ApiParam(value = "Update related rule nodes.") @RequestParam(value = "updateRelated", required = false, defaultValue = "true") boolean updateRelated) throws ThingsboardException {
    try {
        TenantId tenantId = getTenantId();
        if (debugPerTenantEnabled) {
            ConcurrentMap<TenantId, DebugTbRateLimits> debugPerTenantLimits = actorContext.getDebugPerTenantLimits();
            DebugTbRateLimits debugTbRateLimits = debugPerTenantLimits.getOrDefault(tenantId, null);
            if (debugTbRateLimits != null) {
                debugPerTenantLimits.remove(tenantId, debugTbRateLimits);
            }
        }
        RuleChain ruleChain = checkRuleChain(ruleChainMetaData.getRuleChainId(), Operation.WRITE);
        RuleChainUpdateResult result = ruleChainService.saveRuleChainMetaData(tenantId, ruleChainMetaData);
        checkNotNull(result.isSuccess() ? true : null);
        List<RuleChain> updatedRuleChains;
        if (updateRelated && result.isSuccess()) {
            updatedRuleChains = tbRuleChainService.updateRelatedRuleChains(tenantId, ruleChainMetaData.getRuleChainId(), result);
        } else {
            updatedRuleChains = Collections.emptyList();
        }
        RuleChainMetaData savedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, ruleChainMetaData.getRuleChainId()));
        if (RuleChainType.CORE.equals(ruleChain.getType())) {
            tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED);
            updatedRuleChains.forEach(updatedRuleChain -> {
                tbClusterService.broadcastEntityStateChangeEvent(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), ComponentLifecycleEvent.UPDATED);
            });
        }
        logEntityAction(ruleChain.getId(), ruleChain, null, ActionType.UPDATED, null, ruleChainMetaData);
        for (RuleChain updatedRuleChain : updatedRuleChains) {
            RuleChainMetaData updatedRuleChainMetaData = checkNotNull(ruleChainService.loadRuleChainMetaData(tenantId, updatedRuleChain.getId()));
            logEntityAction(updatedRuleChain.getId(), updatedRuleChain, null, ActionType.UPDATED, null, updatedRuleChainMetaData);
        }
        if (RuleChainType.EDGE.equals(ruleChain.getType())) {
            sendEntityNotificationMsg(ruleChain.getTenantId(), ruleChain.getId(), EdgeEventActionType.UPDATED);
            updatedRuleChains.forEach(updatedRuleChain -> {
                sendEntityNotificationMsg(updatedRuleChain.getTenantId(), updatedRuleChain.getId(), EdgeEventActionType.UPDATED);
            });
        }
        return savedRuleChainMetaData;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, ruleChainMetaData);
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainMetaData(org.thingsboard.server.common.data.rule.RuleChainMetaData) RuleChainUpdateResult(org.thingsboard.server.common.data.rule.RuleChainUpdateResult) DebugTbRateLimits(org.thingsboard.server.actors.tenant.DebugTbRateLimits) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 15 with RuleChain

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

the class RuleChainController method saveRuleChain.

@ApiOperation(value = "Create Default Rule Chain", notes = "Create rule chain from template, based on the specified name in the request. " + "Creates the rule chain based on the template that is used to create root rule chain. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/device/default", method = RequestMethod.POST)
@ResponseBody
public RuleChain saveRuleChain(@ApiParam(value = "A JSON value representing the request.") @RequestBody DefaultRuleChainCreateRequest request) throws ThingsboardException {
    try {
        checkNotNull(request);
        checkParameter(request.getName(), "name");
        RuleChain savedRuleChain = installScripts.createDefaultRuleChain(getCurrentUser().getTenantId(), request.getName());
        tbClusterService.broadcastEntityStateChangeEvent(savedRuleChain.getTenantId(), savedRuleChain.getId(), ComponentLifecycleEvent.CREATED);
        logEntityAction(savedRuleChain.getId(), savedRuleChain, null, ActionType.ADDED, null);
        return savedRuleChain;
    } catch (Exception e) {
        RuleChain ruleChain = new RuleChain();
        ruleChain.setName(request.getName());
        logEntityAction(emptyId(EntityType.RULE_CHAIN), ruleChain, null, ActionType.ADDED, e);
        throw handleException(e);
    }
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

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