Search in sources :

Example 1 with ComponentLifecycleMsg

use of org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg in project thingsboard by thingsboard.

the class RuleChainActorMessageProcessor method onUpdate.

@Override
public void onUpdate(TbActorCtx context) {
    RuleChain ruleChain = service.findRuleChainById(tenantId, entityId);
    if (ruleChain != null && RuleChainType.CORE.equals(ruleChain.getType())) {
        ruleChainName = ruleChain.getName();
        List<RuleNode> ruleNodeList = service.getRuleChainNodes(tenantId, entityId);
        log.trace("[{}][{}] Updating rule chain with {} nodes", tenantId, entityId, ruleNodeList.size());
        for (RuleNode ruleNode : ruleNodeList) {
            RuleNodeCtx existing = nodeActors.get(ruleNode.getId());
            if (existing == null) {
                log.trace("[{}][{}] Creating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                TbActorRef ruleNodeActor = createRuleNodeActor(context, ruleNode);
                nodeActors.put(ruleNode.getId(), new RuleNodeCtx(tenantId, self, ruleNodeActor, ruleNode));
            } else {
                log.trace("[{}][{}] Updating rule node [{}]: {}", entityId, ruleNode.getId(), ruleNode.getName(), ruleNode);
                existing.setSelf(ruleNode);
                existing.getSelfActor().tellWithHighPriority(new RuleNodeUpdatedMsg(tenantId, existing.getSelf().getId()));
            }
        }
        Set<RuleNodeId> existingNodes = ruleNodeList.stream().map(RuleNode::getId).collect(Collectors.toSet());
        List<RuleNodeId> removedRules = nodeActors.keySet().stream().filter(node -> !existingNodes.contains(node)).collect(Collectors.toList());
        removedRules.forEach(ruleNodeId -> {
            log.trace("[{}][{}] Removing rule node [{}]", tenantId, entityId, ruleNodeId);
            RuleNodeCtx removed = nodeActors.remove(ruleNodeId);
            removed.getSelfActor().tellWithHighPriority(new ComponentLifecycleMsg(tenantId, removed.getSelf().getId(), ComponentLifecycleEvent.DELETED));
        });
        initRoutes(ruleChain, ruleNodeList);
    }
}
Also used : TbMsg(org.thingsboard.server.common.msg.TbMsg) HashMap(java.util.HashMap) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId) ComponentMsgProcessor(org.thingsboard.server.actors.shared.ComponentMsgProcessor) TenantId(org.thingsboard.server.common.data.id.TenantId) RuleChainService(org.thingsboard.server.dao.rule.RuleChainService) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) ArrayList(java.util.ArrayList) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) TbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.TbQueueTbMsgCallbackWrapper) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) TbQueueCallback(org.thingsboard.server.queue.TbQueueCallback) TbApiUsageClient(org.thingsboard.server.queue.usagestats.TbApiUsageClient) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) RuleNodeException(org.thingsboard.server.common.msg.queue.RuleNodeException) Set(java.util.Set) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) TbRelationTypes(org.thingsboard.rule.engine.api.TbRelationTypes) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ToRuleEngineMsg(org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) MultipleTbQueueTbMsgCallbackWrapper(org.thingsboard.server.queue.common.MultipleTbQueueTbMsgCallbackWrapper) TbClusterService(org.thingsboard.server.cluster.TbClusterService) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) Collections(java.util.Collections) TbActorRef(org.thingsboard.server.actors.TbActorRef) TopicPartitionInfo(org.thingsboard.server.common.msg.queue.TopicPartitionInfo) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) RuleNode(org.thingsboard.server.common.data.rule.RuleNode) TbActorRef(org.thingsboard.server.actors.TbActorRef) RuleNodeUpdatedMsg(org.thingsboard.server.common.msg.plugin.RuleNodeUpdatedMsg) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleNodeId(org.thingsboard.server.common.data.id.RuleNodeId)

Example 2 with ComponentLifecycleMsg

use of org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg in project thingsboard by thingsboard.

the class DefaultTbClusterService method broadcastEntityStateChangeEvent.

@Override
public void broadcastEntityStateChangeEvent(TenantId tenantId, EntityId entityId, ComponentLifecycleEvent state) {
    log.trace("[{}] Processing {} state change event: {}", tenantId, entityId.getEntityType(), state);
    broadcast(new ComponentLifecycleMsg(tenantId, entityId, state));
}
Also used : ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg)

Example 3 with ComponentLifecycleMsg

use of org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg in project thingsboard by thingsboard.

the class AbstractConsumerService method handleComponentLifecycleMsg.

protected void handleComponentLifecycleMsg(UUID id, ByteString nfMsg) {
    Optional<TbActorMsg> actorMsgOpt = encodingService.decode(nfMsg.toByteArray());
    if (actorMsgOpt.isPresent()) {
        TbActorMsg actorMsg = actorMsgOpt.get();
        if (actorMsg instanceof ComponentLifecycleMsg) {
            ComponentLifecycleMsg componentLifecycleMsg = (ComponentLifecycleMsg) actorMsg;
            log.info("[{}][{}][{}] Received Lifecycle event: {}", componentLifecycleMsg.getTenantId(), componentLifecycleMsg.getEntityId().getEntityType(), componentLifecycleMsg.getEntityId(), componentLifecycleMsg.getEvent());
            if (EntityType.TENANT_PROFILE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                TenantProfileId tenantProfileId = new TenantProfileId(componentLifecycleMsg.getEntityId().getId());
                tenantProfileCache.evict(tenantProfileId);
                if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.UPDATED)) {
                    apiUsageStateService.onTenantProfileUpdate(tenantProfileId);
                }
            } else if (EntityType.TENANT.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                tenantProfileCache.evict(componentLifecycleMsg.getTenantId());
                if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.UPDATED)) {
                    apiUsageStateService.onTenantUpdate(componentLifecycleMsg.getTenantId());
                } else if (componentLifecycleMsg.getEvent().equals(ComponentLifecycleEvent.DELETED)) {
                    apiUsageStateService.onTenantDelete((TenantId) componentLifecycleMsg.getEntityId());
                }
            } else if (EntityType.DEVICE_PROFILE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                deviceProfileCache.evict(componentLifecycleMsg.getTenantId(), new DeviceProfileId(componentLifecycleMsg.getEntityId().getId()));
            } else if (EntityType.DEVICE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                deviceProfileCache.evict(componentLifecycleMsg.getTenantId(), new DeviceId(componentLifecycleMsg.getEntityId().getId()));
            } else if (EntityType.API_USAGE_STATE.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                apiUsageStateService.onApiUsageStateUpdate(componentLifecycleMsg.getTenantId());
            } else if (EntityType.CUSTOMER.equals(componentLifecycleMsg.getEntityId().getEntityType())) {
                if (componentLifecycleMsg.getEvent() == ComponentLifecycleEvent.DELETED) {
                    apiUsageStateService.onCustomerDelete((CustomerId) componentLifecycleMsg.getEntityId());
                }
            }
        }
        log.trace("[{}] Forwarding message to App Actor {}", id, actorMsg);
        actorContext.tellWithHighPriority(actorMsg);
    }
}
Also used : DeviceProfileId(org.thingsboard.server.common.data.id.DeviceProfileId) TenantProfileId(org.thingsboard.server.common.data.id.TenantProfileId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg)

Example 4 with ComponentLifecycleMsg

use of org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg in project thingsboard by thingsboard.

the class TenantActor method doProcess.

@Override
protected boolean doProcess(TbActorMsg msg) {
    if (cantFindTenant) {
        log.info("[{}] Processing missing Tenant msg: {}", tenantId, msg);
        if (msg.getMsgType().equals(MsgType.QUEUE_TO_RULE_ENGINE_MSG)) {
            QueueToRuleEngineMsg queueMsg = (QueueToRuleEngineMsg) msg;
            queueMsg.getMsg().getCallback().onSuccess();
        } else if (msg.getMsgType().equals(MsgType.TRANSPORT_TO_DEVICE_ACTOR_MSG)) {
            TransportToDeviceActorMsgWrapper transportMsg = (TransportToDeviceActorMsgWrapper) msg;
            transportMsg.getCallback().onSuccess();
        }
        return true;
    }
    switch(msg.getMsgType()) {
        case PARTITION_CHANGE_MSG:
            PartitionChangeMsg partitionChangeMsg = (PartitionChangeMsg) msg;
            ServiceType serviceType = partitionChangeMsg.getServiceQueueKey().getServiceType();
            if (ServiceType.TB_RULE_ENGINE.equals(serviceType)) {
                // To Rule Chain Actors
                broadcast(msg);
            } else if (ServiceType.TB_CORE.equals(serviceType)) {
                List<TbActorId> deviceActorIds = ctx.filterChildren(new TbEntityTypeActorIdPredicate(EntityType.DEVICE) {

                    @Override
                    protected boolean testEntityId(EntityId entityId) {
                        return super.testEntityId(entityId) && !isMyPartition(entityId);
                    }
                });
                deviceActorIds.forEach(id -> ctx.stop(id));
            }
            break;
        case COMPONENT_LIFE_CYCLE_MSG:
            onComponentLifecycleMsg((ComponentLifecycleMsg) msg);
            break;
        case QUEUE_TO_RULE_ENGINE_MSG:
            onQueueToRuleEngineMsg((QueueToRuleEngineMsg) msg);
            break;
        case TRANSPORT_TO_DEVICE_ACTOR_MSG:
            onToDeviceActorMsg((DeviceAwareMsg) msg, false);
            break;
        case DEVICE_ATTRIBUTES_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_CREDENTIALS_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_NAME_OR_TYPE_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_EDGE_UPDATE_TO_DEVICE_ACTOR_MSG:
        case DEVICE_RPC_REQUEST_TO_DEVICE_ACTOR_MSG:
        case DEVICE_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG:
        case SERVER_RPC_RESPONSE_TO_DEVICE_ACTOR_MSG:
        case REMOVE_RPC_TO_DEVICE_ACTOR_MSG:
            onToDeviceActorMsg((DeviceAwareMsg) msg, true);
            break;
        case SESSION_TIMEOUT_MSG:
            ctx.broadcastToChildrenByType(msg, EntityType.DEVICE);
            break;
        case RULE_CHAIN_INPUT_MSG:
        case RULE_CHAIN_OUTPUT_MSG:
        case RULE_CHAIN_TO_RULE_CHAIN_MSG:
            onRuleChainMsg((RuleChainAwareMsg) msg);
            break;
        case EDGE_EVENT_UPDATE_TO_EDGE_SESSION_MSG:
            onToEdgeSessionMsg((EdgeEventUpdateMsg) msg);
            break;
        default:
            return false;
    }
    return true;
}
Also used : EntityId(org.thingsboard.server.common.data.id.EntityId) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) Edge(org.thingsboard.server.common.data.edge.Edge) TbMsg(org.thingsboard.server.common.msg.TbMsg) RuleChainManagerActor(org.thingsboard.server.actors.ruleChain.RuleChainManagerActor) EdgeEventUpdateMsg(org.thingsboard.server.common.msg.edge.EdgeEventUpdateMsg) Tenant(org.thingsboard.server.common.data.Tenant) TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceActorCreator(org.thingsboard.server.actors.device.DeviceActorCreator) DefaultActorService(org.thingsboard.server.actors.service.DefaultActorService) TbActorId(org.thingsboard.server.actors.TbActorId) TbActorNotRegisteredException(org.thingsboard.server.actors.TbActorNotRegisteredException) MsgType(org.thingsboard.server.common.msg.MsgType) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) EntityId(org.thingsboard.server.common.data.id.EntityId) TbEntityTypeActorIdPredicate(org.thingsboard.server.actors.TbEntityTypeActorIdPredicate) EntityType(org.thingsboard.server.common.data.EntityType) DeviceAwareMsg(org.thingsboard.server.common.msg.aware.DeviceAwareMsg) EdgeId(org.thingsboard.server.common.data.id.EdgeId) TbEntityActorId(org.thingsboard.server.actors.TbEntityActorId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg) RuleChainType(org.thingsboard.server.common.data.rule.RuleChainType) TbActorCtx(org.thingsboard.server.actors.TbActorCtx) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) ApiUsageState(org.thingsboard.server.common.data.ApiUsageState) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) EdgeRpcService(org.thingsboard.server.service.edge.rpc.EdgeRpcService) RuleEngineException(org.thingsboard.server.common.msg.queue.RuleEngineException) Slf4j(lombok.extern.slf4j.Slf4j) TbActorException(org.thingsboard.server.actors.TbActorException) List(java.util.List) TbActorMsg(org.thingsboard.server.common.msg.TbActorMsg) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Optional(java.util.Optional) ContextBasedCreator(org.thingsboard.server.actors.service.ContextBasedCreator) RuleChainId(org.thingsboard.server.common.data.id.RuleChainId) RuleChainAwareMsg(org.thingsboard.server.common.msg.aware.RuleChainAwareMsg) TbActor(org.thingsboard.server.actors.TbActor) TbActorRef(org.thingsboard.server.actors.TbActorRef) QueueToRuleEngineMsg(org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg) ServiceType(org.thingsboard.server.common.msg.queue.ServiceType) PartitionChangeMsg(org.thingsboard.server.common.msg.queue.PartitionChangeMsg) List(java.util.List) TransportToDeviceActorMsgWrapper(org.thingsboard.server.service.transport.msg.TransportToDeviceActorMsgWrapper) TbEntityTypeActorIdPredicate(org.thingsboard.server.actors.TbEntityTypeActorIdPredicate)

Example 5 with ComponentLifecycleMsg

use of org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg in project thingsboard by thingsboard.

the class DefaultTbClusterService method onApiStateChange.

@Override
public void onApiStateChange(ApiUsageState apiUsageState, TbQueueCallback callback) {
    broadcastEntityChangeToTransport(apiUsageState.getTenantId(), apiUsageState.getId(), apiUsageState, callback);
    broadcast(new ComponentLifecycleMsg(apiUsageState.getTenantId(), apiUsageState.getId(), ComponentLifecycleEvent.UPDATED));
}
Also used : ComponentLifecycleMsg(org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg)

Aggregations

ComponentLifecycleMsg (org.thingsboard.server.common.msg.plugin.ComponentLifecycleMsg)5 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 ActorSystemContext (org.thingsboard.server.actors.ActorSystemContext)2 TbActorCtx (org.thingsboard.server.actors.TbActorCtx)2 TbActorRef (org.thingsboard.server.actors.TbActorRef)2 TbEntityActorId (org.thingsboard.server.actors.TbEntityActorId)2 DefaultActorService (org.thingsboard.server.actors.service.DefaultActorService)2 EntityType (org.thingsboard.server.common.data.EntityType)2 DeviceId (org.thingsboard.server.common.data.id.DeviceId)2 EntityId (org.thingsboard.server.common.data.id.EntityId)2 RuleChainId (org.thingsboard.server.common.data.id.RuleChainId)2 TenantId (org.thingsboard.server.common.data.id.TenantId)2 ComponentLifecycleEvent (org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent)2 RuleChain (org.thingsboard.server.common.data.rule.RuleChain)2 RuleChainType (org.thingsboard.server.common.data.rule.RuleChainType)2 TbActorMsg (org.thingsboard.server.common.msg.TbActorMsg)2 TbMsg (org.thingsboard.server.common.msg.TbMsg)2 PartitionChangeMsg (org.thingsboard.server.common.msg.queue.PartitionChangeMsg)2 QueueToRuleEngineMsg (org.thingsboard.server.common.msg.queue.QueueToRuleEngineMsg)2