Search in sources :

Example 1 with RuleChainUpdateResult

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

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

the class DefaultTbRuleChainService method updateRelatedRuleChains.

@Override
public List<RuleChain> updateRelatedRuleChains(TenantId tenantId, RuleChainId ruleChainId, RuleChainUpdateResult result) {
    Set<RuleChainId> ruleChainIds = new HashSet<>();
    log.debug("[{}][{}] Going to update links in related rule chains", tenantId, ruleChainId);
    if (result.getUpdatedRuleNodes() == null || result.getUpdatedRuleNodes().isEmpty()) {
        return Collections.emptyList();
    }
    Set<String> oldLabels = new HashSet<>();
    Set<String> newLabels = new HashSet<>();
    Set<String> confusedLabels = new HashSet<>();
    Map<String, String> updatedLabels = new HashMap<>();
    for (RuleNodeUpdateResult update : result.getUpdatedRuleNodes()) {
        var oldNode = update.getOldRuleNode();
        var newNode = update.getNewRuleNode();
        if (isOutputRuleNode(newNode)) {
            try {
                oldLabels.add(oldNode.getName());
                newLabels.add(newNode.getName());
                if (!oldNode.getName().equals(newNode.getName())) {
                    String oldLabel = oldNode.getName();
                    String newLabel = newNode.getName();
                    if (updatedLabels.containsKey(oldLabel) && !updatedLabels.get(oldLabel).equals(newLabel)) {
                        confusedLabels.add(oldLabel);
                        log.warn("[{}][{}] Can't automatically rename the label from [{}] to [{}] due to conflict [{}]", tenantId, ruleChainId, oldLabel, newLabel, updatedLabels.get(oldLabel));
                    } else {
                        updatedLabels.put(oldLabel, newLabel);
                    }
                }
            } catch (Exception e) {
                log.warn("[{}][{}][{}] Failed to decode rule node configuration", tenantId, ruleChainId, newNode.getId(), e);
            }
        }
    }
    // Remove all output labels that are renamed to two or more different labels, since we don't which new label to use;
    confusedLabels.forEach(updatedLabels::remove);
    // Remove all output labels that are renamed but still present in the rule chain;
    newLabels.forEach(updatedLabels::remove);
    if (!oldLabels.equals(newLabels)) {
        ruleChainIds.addAll(updateRelatedRuleChains(tenantId, ruleChainId, updatedLabels));
    }
    return ruleChainIds.stream().map(id -> ruleChainService.findRuleChainById(tenantId, id)).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) RuleNodeUpdateResult(org.thingsboard.server.common.data.rule.RuleNodeUpdateResult) HashMap(java.util.HashMap) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) HashSet(java.util.HashSet)

Aggregations

TenantId (org.thingsboard.server.common.data.id.TenantId)2 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)2 RuleChainMetaData (org.thingsboard.server.common.data.rule.RuleChainMetaData)2 RuleChainUpdateResult (org.thingsboard.server.common.data.rule.RuleChainUpdateResult)2 ApiOperation (io.swagger.annotations.ApiOperation)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 Service (org.springframework.stereotype.Service)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1