Search in sources :

Example 1 with PersistenceEvent

use of org.openremote.model.PersistenceEvent in project openremote by openremote.

the class RulesService method configure.

@SuppressWarnings("unchecked")
@Override
public void configure() throws Exception {
    // If any ruleset was modified in the database then check its' status and undeploy, deploy, or update it
    from(PERSISTENCE_TOPIC).routeId("RulesetPersistenceChanges").filter(isPersistenceEventForEntityType(Ruleset.class)).filter(isNotForGateway(gatewayService)).process(exchange -> {
        PersistenceEvent<?> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
        processRulesetChange((Ruleset) persistenceEvent.getEntity(), persistenceEvent.getCause());
    });
    // If any tenant was modified in the database then check its' status and undeploy, deploy or update any
    // associated rulesets
    from(PERSISTENCE_TOPIC).routeId("RuleEngineTenantChanges").filter(isPersistenceEventForEntityType(Tenant.class)).filter(isNotForGateway(gatewayService)).process(exchange -> {
        PersistenceEvent<?> persistenceEvent = exchange.getIn().getBody(PersistenceEvent.class);
        Tenant tenant = (Tenant) persistenceEvent.getEntity();
        processTenantChange(tenant, persistenceEvent.getCause());
    });
    // If any asset was modified in the database, detect changed attributes
    from(PERSISTENCE_TOPIC).routeId("RuleEngineAssetChanges").filter(isPersistenceEventForEntityType(Asset.class)).process(exchange -> {
        PersistenceEvent<Asset<?>> persistenceEvent = (PersistenceEvent<Asset<?>>) exchange.getIn().getBody(PersistenceEvent.class);
        final Asset<?> eventAsset = persistenceEvent.getEntity();
        processAssetChange(eventAsset, persistenceEvent);
    });
}
Also used : Tenant(org.openremote.model.security.Tenant) PersistenceEvent(org.openremote.model.PersistenceEvent) Asset(org.openremote.model.asset.Asset)

Example 2 with PersistenceEvent

use of org.openremote.model.PersistenceEvent in project openremote by openremote.

the class PushNotificationHandler method configure.

@Override
public void configure() throws Exception {
    // If any console asset was modified in the database, detect push provider changes
    from(PersistenceService.PERSISTENCE_TOPIC).routeId("PushNotificationAssetChanges").filter(PersistenceService.isPersistenceEventForEntityType(ConsoleAsset.class)).filter(isNotForGateway(gatewayService)).process(exchange -> {
        @SuppressWarnings("unchecked") PersistenceEvent<ConsoleAsset> persistenceEvent = (PersistenceEvent<ConsoleAsset>) exchange.getIn().getBody(PersistenceEvent.class);
        final ConsoleAsset asset = persistenceEvent.getEntity();
        processConsoleAssetChange(asset, persistenceEvent);
    });
}
Also used : ConsoleAsset(org.openremote.model.asset.impl.ConsoleAsset) PersistenceEvent(org.openremote.model.PersistenceEvent)

Example 3 with PersistenceEvent

use of org.openremote.model.PersistenceEvent in project openremote by openremote.

the class MqttBrokerService method configure.

@SuppressWarnings("unchecked")
@Override
public void configure() throws Exception {
    from(PERSISTENCE_TOPIC).routeId("UserPersistenceChanges").filter(isPersistenceEventForEntityType(User.class)).process(exchange -> {
        PersistenceEvent<User> persistenceEvent = (PersistenceEvent<User>) exchange.getIn().getBody(PersistenceEvent.class);
        User user = persistenceEvent.getEntity();
        if (!user.isServiceAccount()) {
            return;
        }
        boolean forceDisconnect = persistenceEvent.getCause() == PersistenceEvent.Cause.DELETE;
        if (persistenceEvent.getCause() == PersistenceEvent.Cause.UPDATE) {
            // Force disconnect if certain properties have changed
            forceDisconnect = Arrays.stream(persistenceEvent.getPropertyNames()).anyMatch((propertyName) -> (propertyName.equals("enabled") && !user.getEnabled()) || propertyName.equals("username"));
        }
        if (forceDisconnect) {
            // Find existing connection for this user
            Arrays.stream(getConnections()).filter(connection -> user.getUsername().equals(connection.getUsername())).findFirst().ifPresent(connection -> {
                LOG.info("User modified or deleted so forcing connected client to disconnect: connection=" + connection);
                forceDisconnect(connection.getClientId());
            });
        }
    });
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Tenant(org.openremote.model.security.Tenant) MqttMessageBuilders(io.netty.handler.codec.mqtt.MqttMessageBuilders) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) ManagerKeycloakIdentityProvider(org.openremote.manager.security.ManagerKeycloakIdentityProvider) java.util(java.util) MemoryConfig(io.moquette.broker.config.MemoryConfig) ValueUtil(org.openremote.model.util.ValueUtil) MapAccess.getInteger(org.openremote.container.util.MapAccess.getInteger) MapAccess.getString(org.openremote.container.util.MapAccess.getString) IAuthenticator(io.moquette.broker.security.IAuthenticator) Unpooled(io.netty.buffer.Unpooled) Level(java.util.logging.Level) ByteBuf(io.netty.buffer.ByteBuf) Charset(java.nio.charset.Charset) BrokerConstants(io.moquette.BrokerConstants) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SyslogCategory(org.openremote.model.syslog.SyslogCategory) TextUtil(org.openremote.model.util.TextUtil) Method(java.lang.reflect.Method) PersistenceEvent(org.openremote.model.PersistenceEvent) MessageBrokerService(org.openremote.container.message.MessageBrokerService) User(org.openremote.model.security.User) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) API(org.openremote.model.syslog.SyslogCategory.API) ContainerService(org.openremote.model.ContainerService) Field(java.lang.reflect.Field) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) InterceptHandler(io.moquette.interception.InterceptHandler) Container(org.openremote.model.Container) SessionRegistry(io.moquette.broker.SessionRegistry) ClientEventService(org.openremote.manager.event.ClientEventService) StreamSupport.stream(java.util.stream.StreamSupport.stream) Server(io.moquette.broker.Server) RouteBuilder(org.apache.camel.builder.RouteBuilder) TimerService(org.openremote.container.timer.TimerService) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) User(org.openremote.model.security.User) PersistenceEvent(org.openremote.model.PersistenceEvent)

Example 4 with PersistenceEvent

use of org.openremote.model.PersistenceEvent in project openremote by openremote.

the class GatewayService method processGatewayChange.

protected void processGatewayChange(GatewayAsset gateway, PersistenceEvent<Asset<?>> persistenceEvent) {
    switch(persistenceEvent.getCause()) {
        case CREATE:
            createUpdateGatewayServiceUser(gateway);
            synchronized (gatewayConnectorMap) {
                GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);
                gatewayConnectorMap.put(gateway.getId().toLowerCase(Locale.ROOT), connector);
            }
            break;
        case UPDATE:
            // Check if this gateway has a connector
            GatewayConnector connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));
            if (connector == null) {
                break;
            }
            connector.gateway = gateway;
            // Check if disabled
            boolean isNowDisabled = gateway.getDisabled().orElse(false);
            if (isNowDisabled) {
                connector.sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));
            }
            connector.setDisabled(isNowDisabled);
            int attributeIndex = persistenceEvent.getPropertyNames() != null ? IntStream.range(0, persistenceEvent.getPropertyNames().length).filter(i -> "attributes".equals(persistenceEvent.getPropertyNames()[i])).findFirst().orElse(-1) : -1;
            if (attributeIndex >= 0) {
                // Check if disabled attribute has changed
                AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");
                boolean wasDisabled = oldAttributes.getValue(GatewayAsset.DISABLED).orElse(false);
                if (wasDisabled != isNowDisabled) {
                    createUpdateGatewayServiceUser(gateway);
                }
            }
            break;
        case DELETE:
            // Check if this gateway has a connector
            connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));
            if (connector == null) {
                break;
            }
            synchronized (gatewayConnectorMap) {
                connector = gatewayConnectorMap.remove(gateway.getId().toLowerCase(Locale.ROOT));
                if (connector != null) {
                    connector.disconnect();
                }
            }
            removeGatewayServiceUser(gateway);
            break;
    }
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Tenant(org.openremote.model.security.Tenant) IntStream(java.util.stream.IntStream) ManagerKeycloakIdentityProvider(org.openremote.manager.security.ManagerKeycloakIdentityProvider) java.util(java.util) GATEWAY(org.openremote.model.syslog.SyslogCategory.GATEWAY) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Exchange(org.apache.camel.Exchange) AssetProcessingService(org.openremote.manager.asset.AssetProcessingService) Level(java.util.logging.Level) Predicate(org.apache.camel.Predicate) RulesetStorageService(org.openremote.manager.rules.RulesetStorageService) RulesService(org.openremote.manager.rules.RulesService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SyslogCategory(org.openremote.model.syslog.SyslogCategory) TextUtil(org.openremote.model.util.TextUtil) PersistenceEvent(org.openremote.model.PersistenceEvent) MessageBrokerService(org.openremote.container.message.MessageBrokerService) User(org.openremote.model.security.User) Ruleset(org.openremote.model.rules.Ruleset) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) GatewayDisconnectEvent(org.openremote.model.gateway.GatewayDisconnectEvent) AssetQuery(org.openremote.model.query.AssetQuery) GatewayAsset(org.openremote.model.asset.impl.GatewayAsset) ContainerService(org.openremote.model.ContainerService) EntityManager(javax.persistence.EntityManager) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Container(org.openremote.model.Container) Consumer(java.util.function.Consumer) ClientEventService(org.openremote.manager.event.ClientEventService) RouteBuilder(org.apache.camel.builder.RouteBuilder) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) GatewayConnector.mapAssetId(org.openremote.manager.gateway.GatewayConnector.mapAssetId) org.openremote.model.attribute(org.openremote.model.attribute) AssetUpdateProcessor(org.openremote.manager.asset.AssetUpdateProcessor) PredicateBuilder.and(org.apache.camel.builder.PredicateBuilder.and) PredicateBuilder.or(org.apache.camel.builder.PredicateBuilder.or) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) ConnectionConstants(org.openremote.container.web.ConnectionConstants) SharedEvent(org.openremote.model.event.shared.SharedEvent) GatewayDisconnectEvent(org.openremote.model.gateway.GatewayDisconnectEvent)

Aggregations

PersistenceEvent (org.openremote.model.PersistenceEvent)4 Tenant (org.openremote.model.security.Tenant)3 java.util (java.util)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 MessageBrokerService (org.openremote.container.message.MessageBrokerService)2 PERSISTENCE_TOPIC (org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC)2 PersistenceService.isPersistenceEventForEntityType (org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType)2 AssetStorageService (org.openremote.manager.asset.AssetStorageService)2 ClientEventService (org.openremote.manager.event.ClientEventService)2 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)2 ManagerKeycloakIdentityProvider (org.openremote.manager.security.ManagerKeycloakIdentityProvider)2 Container (org.openremote.model.Container)2 ContainerService (org.openremote.model.ContainerService)2 User (org.openremote.model.security.User)2 SyslogCategory (org.openremote.model.syslog.SyslogCategory)2 TextUtil (org.openremote.model.util.TextUtil)2