Search in sources :

Example 36 with RuleChainId

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

the class RuleChainController method getRuleChainOutputLabels.

@ApiOperation(value = "Get Rule Chain output labels (getRuleChainOutputLabels)", notes = "Fetch the unique labels for the \"output\" Rule Nodes that belong to the Rule Chain based on the provided Rule Chain Id. " + RULE_CHAIN_DESCRIPTION + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}/output/labels", method = RequestMethod.GET)
@ResponseBody
public Set<String> getRuleChainOutputLabels(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        checkRuleChain(ruleChainId, Operation.READ);
        return tbRuleChainService.getRuleChainOutputLabels(getTenantId(), ruleChainId);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) 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 37 with RuleChainId

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

the class RuleChainController method setRootRuleChain.

@ApiOperation(value = "Set Root Rule Chain (setRootRuleChain)", notes = "Makes the rule chain to be root rule chain. Updates previous root rule chain as well. " + TENANT_AUTHORITY_PARAGRAPH)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/ruleChain/{ruleChainId}/root", method = RequestMethod.POST)
@ResponseBody
public RuleChain setRootRuleChain(@ApiParam(value = RULE_CHAIN_ID_PARAM_DESCRIPTION) @PathVariable(RULE_CHAIN_ID) String strRuleChainId) throws ThingsboardException {
    checkParameter(RULE_CHAIN_ID, strRuleChainId);
    try {
        RuleChainId ruleChainId = new RuleChainId(toUUID(strRuleChainId));
        RuleChain ruleChain = checkRuleChain(ruleChainId, Operation.WRITE);
        TenantId tenantId = getCurrentUser().getTenantId();
        RuleChain previousRootRuleChain = ruleChainService.getRootTenantRuleChain(tenantId);
        if (ruleChainService.setRootRuleChain(getTenantId(), ruleChainId)) {
            if (previousRootRuleChain != null) {
                previousRootRuleChain = ruleChainService.findRuleChainById(getTenantId(), previousRootRuleChain.getId());
                tbClusterService.broadcastEntityStateChangeEvent(previousRootRuleChain.getTenantId(), previousRootRuleChain.getId(), ComponentLifecycleEvent.UPDATED);
                logEntityAction(previousRootRuleChain.getId(), previousRootRuleChain, null, ActionType.UPDATED, null);
            }
            ruleChain = ruleChainService.findRuleChainById(getTenantId(), ruleChainId);
            tbClusterService.broadcastEntityStateChangeEvent(ruleChain.getTenantId(), ruleChain.getId(), ComponentLifecycleEvent.UPDATED);
            logEntityAction(ruleChain.getId(), ruleChain, null, ActionType.UPDATED, null);
        }
        return ruleChain;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE_CHAIN), null, null, ActionType.UPDATED, e, strRuleChainId);
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) 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 38 with RuleChainId

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

the class TenantActor method onComponentLifecycleMsg.

private void onComponentLifecycleMsg(ComponentLifecycleMsg msg) {
    if (msg.getEntityId().getEntityType().equals(EntityType.API_USAGE_STATE)) {
        ApiUsageState old = getApiUsageState();
        apiUsageState = new ApiUsageState(systemContext.getApiUsageStateService().getApiUsageState(tenantId));
        if (old.isReExecEnabled() && !apiUsageState.isReExecEnabled()) {
            log.info("[{}] Received API state update. Going to DISABLE Rule Engine execution.", tenantId);
            destroyRuleChains();
        } else if (!old.isReExecEnabled() && apiUsageState.isReExecEnabled()) {
            log.info("[{}] Received API state update. Going to ENABLE Rule Engine execution.", tenantId);
            initRuleChains();
        }
    } else if (msg.getEntityId().getEntityType() == EntityType.EDGE) {
        EdgeId edgeId = new EdgeId(msg.getEntityId().getId());
        EdgeRpcService edgeRpcService = systemContext.getEdgeRpcService();
        if (msg.getEvent() == ComponentLifecycleEvent.DELETED) {
            edgeRpcService.deleteEdge(tenantId, edgeId);
        } else {
            Edge edge = systemContext.getEdgeService().findEdgeById(tenantId, edgeId);
            if (msg.getEvent() == ComponentLifecycleEvent.UPDATED) {
                edgeRpcService.updateEdge(tenantId, edge);
            }
        }
    } else if (isRuleEngine) {
        TbActorRef target = getEntityActorRef(msg.getEntityId());
        if (target != null) {
            if (msg.getEntityId().getEntityType() == EntityType.RULE_CHAIN) {
                RuleChain ruleChain = systemContext.getRuleChainService().findRuleChainById(tenantId, new RuleChainId(msg.getEntityId().getId()));
                if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
                    visit(ruleChain, target);
                }
            }
            target.tellWithHighPriority(msg);
        } else {
            log.debug("[{}] Invalid component lifecycle msg: {}", tenantId, msg);
        }
    }
}
Also used : ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) EdgeId(org.thingsboard.server.common.data.id.EdgeId) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) EdgeRpcService(org.thingsboard.server.service.edge.rpc.EdgeRpcService) Edge(org.thingsboard.server.common.data.edge.Edge)

Example 39 with RuleChainId

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

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

the class DefaultTbContext method alarmActionMsg.

public TbMsg alarmActionMsg(Alarm alarm, RuleNodeId ruleNodeId, String action) {
    RuleChainId ruleChainId = null;
    String queueName = ServiceQueue.MAIN;
    if (EntityType.DEVICE.equals(alarm.getOriginator().getEntityType())) {
        DeviceId deviceId = new DeviceId(alarm.getOriginator().getId());
        DeviceProfile deviceProfile = mainCtx.getDeviceProfileCache().get(getTenantId(), deviceId);
        if (deviceProfile == null) {
            log.warn("[{}] Device profile is null!", deviceId);
            ruleChainId = null;
            queueName = ServiceQueue.MAIN;
        } else {
            ruleChainId = deviceProfile.getDefaultRuleChainId();
            String defaultQueueName = deviceProfile.getDefaultQueueName();
            queueName = defaultQueueName != null ? defaultQueueName : ServiceQueue.MAIN;
        }
    }
    return entityActionMsg(alarm, alarm.getId(), ruleNodeId, action, queueName, ruleChainId);
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId)

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