Search in sources :

Example 1 with RulesetQuery

use of org.openremote.model.query.RulesetQuery in project openremote by openremote.

the class RulesService method start.

@Override
public void start(Container container) throws Exception {
    if (!geofenceAssetAdapters.isEmpty()) {
        LOG.info("GeoefenceAssetAdapters found: " + geofenceAssetAdapters.size());
        locationPredicateRulesConsumer = this::onEngineLocationRulesChanged;
        for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {
            geofenceAssetAdapter.start(container);
        }
    }
    LOG.info("Deploying global rulesets");
    rulesetStorageService.findAll(GlobalRuleset.class, new RulesetQuery().setEnabledOnly(true).setFullyPopulate(true)).forEach(this::deployGlobalRuleset);
    LOG.info("Deploying tenant rulesets");
    tenants = Arrays.stream(identityService.getIdentityProvider().getTenants()).filter(Tenant::getEnabled).toArray(Tenant[]::new);
    rulesetStorageService.findAll(TenantRuleset.class, new RulesetQuery().setEnabledOnly(true).setFullyPopulate(true)).stream().filter(rd -> Arrays.stream(tenants).anyMatch(tenant -> rd.getRealm().equals(tenant.getRealm()))).forEach(this::deployTenantRuleset);
    LOG.info("Deploying asset rulesets");
    // Group by asset ID then tenant and check tenant is enabled
    // noinspection ResultOfMethodCallIgnored
    deployAssetRulesets(rulesetStorageService.findAll(AssetRuleset.class, new RulesetQuery().setEnabledOnly(true).setFullyPopulate(true))).count();
    LOG.info("Loading all assets with fact attributes to initialize state of rules engines");
    Stream<Pair<Asset<?>, Stream<Attribute<?>>>> stateAttributes = findRuleStateAttributes();
    // Push each attribute as an asset update through the rule engine chain
    // that will ensure the insert only happens to the engines in scope
    stateAttributes.forEach(pair -> {
        Asset<?> asset = pair.key;
        pair.value.forEach(ruleAttribute -> {
            AssetState<?> assetState = new AssetState<>(asset, ruleAttribute, Source.INTERNAL);
            updateAssetState(assetState);
        });
    });
    // Start the engines
    if (globalEngine != null) {
        globalEngine.start();
    }
    tenantEngines.values().forEach(RulesEngine::start);
    assetEngines.values().forEach(RulesEngine::start);
    startDone = true;
    preInitassetStates.forEach(this::doProcessAssetUpdate);
    preInitassetStates.clear();
}
Also used : ClientRole(org.openremote.model.security.ClientRole) BiFunction(java.util.function.BiFunction) AssetProcessingException(org.openremote.manager.asset.AssetProcessingException) AssetProcessingService(org.openremote.manager.asset.AssetProcessingService) ValueUtil(org.openremote.model.util.ValueUtil) MapAccess.getString(org.openremote.container.util.MapAccess.getString) GeofenceAssetAdapter(org.openremote.manager.rules.geofence.GeofenceAssetAdapter) Attribute.getAddedOrModifiedAttributes(org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes) org.openremote.model.rules(org.openremote.model.rules) AssetPredictedDatapointService(org.openremote.manager.datapoint.AssetPredictedDatapointService) GeofenceDefinition(org.openremote.model.rules.geofence.GeofenceDefinition) ContainerService(org.openremote.model.ContainerService) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Container(org.openremote.model.Container) Stream(java.util.stream.Stream) RouteBuilder(org.apache.camel.builder.RouteBuilder) PERSISTENCE_TOPIC(org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC) RulesetQuery(org.openremote.model.query.RulesetQuery) GatewayService(org.openremote.manager.gateway.GatewayService) GlobalLock.withLock(org.openremote.container.concurrent.GlobalLock.withLock) AssetStorageService(org.openremote.manager.asset.AssetStorageService) Tenant(org.openremote.model.security.Tenant) AttributeMap(org.openremote.model.attribute.AttributeMap) GatewayService.isNotForGateway(org.openremote.manager.gateway.GatewayService.isNotForGateway) java.util(java.util) AssetDatapointService(org.openremote.manager.datapoint.AssetDatapointService) NotificationService(org.openremote.manager.notification.NotificationService) SEVERE(java.util.logging.Level.SEVERE) GlobalLock.withLockReturning(org.openremote.container.concurrent.GlobalLock.withLockReturning) LocationAttributePredicate(org.openremote.model.query.filter.LocationAttributePredicate) Attribute(org.openremote.model.attribute.Attribute) PersistenceService(org.openremote.container.persistence.PersistenceService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ManagerWebService(org.openremote.manager.web.ManagerWebService) PersistenceEvent(org.openremote.model.PersistenceEvent) MessageBrokerService(org.openremote.container.message.MessageBrokerService) FlowResourceImpl(org.openremote.manager.rules.flow.FlowResourceImpl) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) MetaItemType(org.openremote.model.value.MetaItemType) AssetQuery(org.openremote.model.query.AssetQuery) Pair(org.openremote.model.util.Pair) EntityManager(javax.persistence.EntityManager) ClientEventService(org.openremote.manager.event.ClientEventService) Collectors.toList(java.util.stream.Collectors.toList) TimerService(org.openremote.container.timer.TimerService) AssetUpdateProcessor(org.openremote.manager.asset.AssetUpdateProcessor) PersistenceService.isPersistenceEventForEntityType(org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType) Source(org.openremote.model.attribute.AttributeEvent.Source) FINEST(java.util.logging.Level.FINEST) RulesetQuery(org.openremote.model.query.RulesetQuery) Attribute(org.openremote.model.attribute.Attribute) GeofenceAssetAdapter(org.openremote.manager.rules.geofence.GeofenceAssetAdapter) Tenant(org.openremote.model.security.Tenant) Pair(org.openremote.model.util.Pair)

Example 2 with RulesetQuery

use of org.openremote.model.query.RulesetQuery in project openremote by openremote.

the class RulesResourceImpl method getTenantRulesets.

@Override
public TenantRuleset[] getTenantRulesets(@BeanParam RequestParams requestParams, String realm, List<Ruleset.Lang> languages, boolean fullyPopulate) {
    if (isAuthenticated() && !isRealmAccessibleByUser(realm)) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    boolean publicOnly = !isAuthenticated() || isRestrictedUser() | !hasResourceRole(ClientRole.READ_RULES.getValue(), Constants.KEYCLOAK_CLIENT_ID);
    List<TenantRuleset> result = rulesetStorageService.findAll(TenantRuleset.class, new RulesetQuery().setRealm(realm).setLanguages(languages.toArray(new Ruleset.Lang[0])).setFullyPopulate(fullyPopulate).setPublicOnly(publicOnly));
    // Try and retrieve transient status and error data
    result.forEach(ruleset -> rulesService.getRulesetDeployment(ruleset.getId()).ifPresent(rulesetDeployment -> {
        ruleset.setStatus(rulesetDeployment.getStatus());
        ruleset.setError(rulesetDeployment.getErrorMessage());
    }));
    return result.toArray(new TenantRuleset[0]);
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Tenant(org.openremote.model.security.Tenant) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) UserAssetLink(org.openremote.model.asset.UserAssetLink) org.openremote.model.rules(org.openremote.model.rules) ClientRole(org.openremote.model.security.ClientRole) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) AssetQuery(org.openremote.model.query.AssetQuery) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) GeofenceDefinition(org.openremote.model.rules.geofence.GeofenceDefinition) Constants(org.openremote.model.Constants) Logger(java.util.logging.Logger) BeanParam(javax.ws.rs.BeanParam) ManagerWebResource(org.openremote.manager.web.ManagerWebResource) List(java.util.List) Response(javax.ws.rs.core.Response) TimerService(org.openremote.container.timer.TimerService) RulesetQuery(org.openremote.model.query.RulesetQuery) WebApplicationException(javax.ws.rs.WebApplicationException) RequestParams(org.openremote.model.http.RequestParams) RulesetQuery(org.openremote.model.query.RulesetQuery) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 3 with RulesetQuery

use of org.openremote.model.query.RulesetQuery in project openremote by openremote.

the class RulesResourceImpl method getGlobalRulesets.

@Override
public GlobalRuleset[] getGlobalRulesets(@BeanParam RequestParams requestParams, List<Ruleset.Lang> languages, boolean fullyPopulate) {
    if (!isSuperUser()) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    }
    List<GlobalRuleset> result = rulesetStorageService.findAll(GlobalRuleset.class, new RulesetQuery().setLanguages(languages.toArray(new Ruleset.Lang[0])).setFullyPopulate(fullyPopulate));
    // Try and retrieve transient status and error data
    result.forEach(ruleset -> rulesService.getRulesetDeployment(ruleset.getId()).ifPresent(rulesetDeployment -> {
        ruleset.setStatus(rulesetDeployment.getStatus());
        ruleset.setError(rulesetDeployment.getErrorMessage());
    }));
    return result.toArray(new GlobalRuleset[0]);
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) Tenant(org.openremote.model.security.Tenant) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) UserAssetLink(org.openremote.model.asset.UserAssetLink) org.openremote.model.rules(org.openremote.model.rules) ClientRole(org.openremote.model.security.ClientRole) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) AssetQuery(org.openremote.model.query.AssetQuery) NOT_FOUND(javax.ws.rs.core.Response.Status.NOT_FOUND) GeofenceDefinition(org.openremote.model.rules.geofence.GeofenceDefinition) Constants(org.openremote.model.Constants) Logger(java.util.logging.Logger) BeanParam(javax.ws.rs.BeanParam) ManagerWebResource(org.openremote.manager.web.ManagerWebResource) List(java.util.List) Response(javax.ws.rs.core.Response) TimerService(org.openremote.container.timer.TimerService) RulesetQuery(org.openremote.model.query.RulesetQuery) WebApplicationException(javax.ws.rs.WebApplicationException) RequestParams(org.openremote.model.http.RequestParams) RulesetQuery(org.openremote.model.query.RulesetQuery) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

Logger (java.util.logging.Logger)3 TimerService (org.openremote.container.timer.TimerService)3 AssetStorageService (org.openremote.manager.asset.AssetStorageService)3 ManagerIdentityService (org.openremote.manager.security.ManagerIdentityService)3 Asset (org.openremote.model.asset.Asset)3 AssetQuery (org.openremote.model.query.AssetQuery)3 RulesetQuery (org.openremote.model.query.RulesetQuery)3 org.openremote.model.rules (org.openremote.model.rules)3 GeofenceDefinition (org.openremote.model.rules.geofence.GeofenceDefinition)3 ClientRole (org.openremote.model.security.ClientRole)3 Tenant (org.openremote.model.security.Tenant)3 List (java.util.List)2 BeanParam (javax.ws.rs.BeanParam)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Response (javax.ws.rs.core.Response)2 BAD_REQUEST (javax.ws.rs.core.Response.Status.BAD_REQUEST)2 NOT_FOUND (javax.ws.rs.core.Response.Status.NOT_FOUND)2 java.util (java.util)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 BiFunction (java.util.function.BiFunction)1