Skip to content
Permalink
Browse files

Cleanup of RulesetStorageService and RulesResource

  • Loading branch information...
richturner committed Mar 11, 2019
1 parent b746964 commit 95761bbeb4800ca53011470ba5d72d65e375cbca
Showing with 332 additions and 402 deletions.
  1. +1 −1 client/src/main/java/org/openremote/app/client/rules/asset/AssetRulesEditorActivity.java
  2. +1 −1 client/src/main/java/org/openremote/app/client/rules/asset/AssetRulesListActivity.java
  3. +1 −1 client/src/main/java/org/openremote/app/client/rules/global/GlobalRulesListActivity.java
  4. +1 −1 client/src/main/java/org/openremote/app/client/rules/tenant/TenantRulesEditorActivity.java
  5. +1 −1 client/src/main/java/org/openremote/app/client/rules/tenant/TenantRulesListActivity.java
  6. +7 −6 manager/src/main/java/org/openremote/manager/rules/RulesResourceImpl.java
  7. +8 −8 manager/src/main/java/org/openremote/manager/rules/RulesService.java
  8. +114 −146 manager/src/main/java/org/openremote/manager/rules/RulesetStorageService.java
  9. +26 −1 manager/src/main/java/org/openremote/manager/setup/builtin/KeycloakDemoSetup.java
  10. +26 −7 manager/src/main/java/org/openremote/manager/setup/builtin/ManagerDemoSetup.java
  11. +7 −7 manager/src/main/java/org/openremote/manager/setup/builtin/RulesDemoSetup.java
  12. +3 −15 model/src/main/java/org/openremote/model/rules/AssetRuleset.java
  13. +2 −6 model/src/main/java/org/openremote/model/rules/GlobalRuleset.java
  14. +3 −3 model/src/main/java/org/openremote/model/rules/RulesResource.java
  15. +3 −13 model/src/main/java/org/openremote/model/rules/TenantRuleset.java
  16. +2 −2 test/src/test/groovy/org/openremote/test/assets/AssetUserLinkingTest.groovy
  17. +5 −8 test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy
  18. +9 −23 test/src/test/groovy/org/openremote/test/failure/RulesExecutionFailureTest.groovy
  19. +5 −4 test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
  20. +12 −16 test/src/test/groovy/org/openremote/test/rules/BasicRulesDeploymentTest.groovy
  21. +19 −30 test/src/test/groovy/org/openremote/test/rules/BasicRulesImport.groovy
  22. +5 −8 test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy
  23. +46 −46 test/src/test/groovy/org/openremote/test/rules/BasicRulesetResourceTest.groovy
  24. +3 −4 test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
  25. +2 −4 test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy
  26. +2 −4 test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy
  27. +2 −4 test/src/test/groovy/org/openremote/test/rules/residence/ResidenceNotifyAlarmTriggerTest.groovy
  28. +4 −8 test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy
  29. +10 −20 test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy
  30. +2 −4 test/src/test/groovy/org/openremote/test/rules/residence/ResidenceVacationModeTest.groovy
@@ -84,7 +84,7 @@ public void start(AcceptsView container, EventBus eventBus, Collection<EventRegi

@Override
protected AssetRuleset newRuleset() {
return new AssetRuleset(assetId, false);
return new AssetRuleset(null, null, null, assetId, false);
}

@Override
@@ -114,7 +114,7 @@ public void start(AcceptsView container, EventBus eventBus, Collection<EventRegi

environment.getApp().getRequests().sendAndReturn(
assetRulesetArrayMapper,
params -> rulesResource.getAssetRulesets(params, assetId),
params -> rulesResource.getAssetRulesets(params, assetId, false),
200,
results -> view.setRulesets(new ArrayList<>(Arrays.asList(results)))
);
@@ -78,7 +78,7 @@ public void start(AcceptsView container, EventBus eventBus, Collection<EventRegi

environment.getApp().getRequests().sendAndReturn(
globalRulesetArrayMapper,
rulesResource::getGlobalRulesets,
requestParams -> rulesResource.getGlobalRulesets(requestParams, false),
200,
results -> view.setRulesets(new ArrayList<>(Arrays.asList(results)))
);
@@ -84,7 +84,7 @@ public void start(AcceptsView container, EventBus eventBus, Collection<EventRegi

@Override
protected TenantRuleset newRuleset() {
return new TenantRuleset(realm, false);
return new TenantRuleset(null, null, null, realm, false);
}

@Override
@@ -115,7 +115,7 @@ public void start(AcceptsView container, EventBus eventBus, Collection<EventRegi

environment.getApp().getRequests().sendAndReturn(
tenantRulesetArrayMapper,
params -> rulesResource.getTenantRulesets(params, realm),
params -> rulesResource.getTenantRulesets(params, realm, false),
200,
results -> view.setRulesets(new ArrayList<>(Arrays.asList(results)))
);
@@ -118,11 +118,12 @@ protected RulesEngineInfo getEngineInfo(RulesEngine engine) {
}

@Override
public GlobalRuleset[] getGlobalRulesets(@BeanParam RequestParams requestParams) {
public GlobalRuleset[] getGlobalRulesets(@BeanParam RequestParams requestParams, boolean fullyPopulate) {
if (!isSuperUser()) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
List<GlobalRuleset> result = rulesetStorageService.findGlobalRulesets();

List<GlobalRuleset> result = rulesetStorageService.findGlobalRulesets(false, fullyPopulate);

// Try and retrieve transient status and error data
result.forEach(ruleset ->
@@ -138,15 +139,15 @@ protected RulesEngineInfo getEngineInfo(RulesEngine engine) {
}

@Override
public TenantRuleset[] getTenantRulesets(@BeanParam RequestParams requestParams, String realmId) {
public TenantRuleset[] getTenantRulesets(@BeanParam RequestParams requestParams, String realmId, boolean fullyPopulate) {

if (isAuthenticated() && !isRealmAccessibleByUser(realmId)) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}

boolean publicOnly = !isAuthenticated() || isRestrictedUser() | !hasResourceRole(ClientRole.READ_RULES.getValue(), Constants.KEYCLOAK_CLIENT_ID);

List<TenantRuleset> result = rulesetStorageService.findTenantRulesets(realmId, publicOnly);
List<TenantRuleset> result = rulesetStorageService.findTenantRulesets(realmId, publicOnly, false, fullyPopulate);

// Try and retrieve transient status and error data
result.forEach(ruleset ->
@@ -162,7 +163,7 @@ protected RulesEngineInfo getEngineInfo(RulesEngine engine) {
}

@Override
public AssetRuleset[] getAssetRulesets(@BeanParam RequestParams requestParams, String assetId) {
public AssetRuleset[] getAssetRulesets(@BeanParam RequestParams requestParams, String assetId, boolean fullyPopulate) {
Asset asset = assetStorageService.find(assetId, false);
if (asset == null)
return new AssetRuleset[0];
@@ -173,7 +174,7 @@ protected RulesEngineInfo getEngineInfo(RulesEngine engine) {

boolean publicOnly = !isAuthenticated() || (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), assetId)) || !hasResourceRole(ClientRole.READ_RULES.getValue(), Constants.KEYCLOAK_CLIENT_ID);

List<AssetRuleset> result = rulesetStorageService.findAssetRulesets(asset.getRealm(), assetId, publicOnly);
List<AssetRuleset> result = rulesetStorageService.findAssetRulesets(asset.getRealm(), assetId, publicOnly, false, fullyPopulate);

// Try and retrieve transient status and error data
result.forEach(ruleset ->
@@ -214,11 +214,11 @@ public void start(Container container) throws Exception {
}

LOG.info("Deploying global rulesets");
rulesetStorageService.findEnabledGlobalRulesets().forEach(this::deployGlobalRuleset);
rulesetStorageService.findGlobalRulesets(true, true).forEach(this::deployGlobalRuleset);

LOG.info("Deploying tenant rulesets");
tenants = identityService.getIdentityProvider().getTenants();
rulesetStorageService.findEnabledTenantRulesets()
rulesetStorageService.findTenantRulesets(false, true, true)
.stream()
.filter(rd ->
Arrays.stream(tenants)
@@ -227,7 +227,7 @@ public void start(Container container) throws Exception {

LOG.info("Deploying asset rulesets");
// Group by asset ID then tenant and check tenant is enabled
deployAssetRulesets(rulesetStorageService.findEnabledAssetRulesets());
deployAssetRulesets(rulesetStorageService.findAssetRulesets(null, null, false, true, true));

LOG.info("Loading all assets with fact attributes to initialize state of rules engines");
Stream<Pair<Asset, Stream<AssetAttribute>>> assetRuleAttributes = findRuleStateAttributes();
@@ -334,21 +334,21 @@ protected void processTenantChange(Tenant tenant, PersistenceEvent.Cause cause)

// Remove any asset rules engines for assets in this realm
assetEngines.values().stream()
.filter(re -> re.getId().getRealm().map(id -> id.equals(tenant.getRealm())).orElse(false))
.filter(re -> re.getId().getRealm().map(realm -> realm.equals(tenant.getRealm())).orElse(false))
.forEach(RulesEngine::stop);
assetEngines.entrySet().removeIf(entry ->
entry.getValue().getId().getRealm().map(id -> id.equals(tenant.getRealm())).orElse(
entry.getValue().getId().getRealm().map(realm -> realm.equals(tenant.getRealm())).orElse(
false)
);

} else {
// Create tenant rules engines for this tenant if it has any rulesets
rulesetStorageService
.findEnabledTenantRulesets(tenant.getRealm())
.findTenantRulesets(tenant.getRealm(), false, true, true)
.forEach(this::deployTenantRuleset);

// Create any asset rules engines for assets in this realm that have rulesets
deployAssetRulesets(rulesetStorageService.findEnabledAssetRulesets(tenant.getRealm()));
deployAssetRulesets(rulesetStorageService.findAssetRulesetsByRealm(tenant.getRealm(), false, true, true));
}
});
}
@@ -484,7 +484,7 @@ protected void processRulesetChange(Ruleset ruleset, PersistenceEvent.Cause caus
} else if (ruleset instanceof AssetRuleset) {

// Must reload from the database, the ruleset might not be completely hydrated on INSERT or UPDATE
AssetRuleset assetRuleset = rulesetStorageService.findEnabledAssetRuleset(ruleset.getId());
AssetRuleset assetRuleset = rulesetStorageService.findById(AssetRuleset.class, ruleset.getId());
RulesEngine newEngine = deployAssetRuleset(assetRuleset);
if (newEngine != null) {
// Push all existing facts for this asset (and it's children into the engine), this is an
Oops, something went wrong.

0 comments on commit 95761bb

Please sign in to comment.
You can’t perform that action at this time.