Search in sources :

Example 21 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class BaseRuleServiceTest method findSystemRules.

@Test
public void findSystemRules() throws Exception {
    // system tenant id
    TenantId systemTenant = new TenantId(ModelConstants.NULL_UUID);
    PluginMetaData plugin = generatePlugin(systemTenant, "testPluginToken" + ThreadLocalRandom.current().nextInt());
    pluginService.savePlugin(plugin);
    ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
    ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
    ruleService.saveRule(generateRule(systemTenant, null, plugin.getApiToken()));
    TextPageData<RuleMetaData> found = ruleService.findSystemRules(new TextPageLink(100));
    Assert.assertEquals(3, found.getData().size());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest) Test(org.junit.Test)

Example 22 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class DefaultSystemDataLoaderService method loadRules.

private void loadRules(Path rulesDir, TenantId tenantId) throws Exception {
    try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(rulesDir, path -> path.toString().endsWith(JSON_EXT))) {
        dirStream.forEach(path -> {
            try {
                JsonNode ruleJson = objectMapper.readTree(path.toFile());
                RuleMetaData rule = objectMapper.treeToValue(ruleJson, RuleMetaData.class);
                rule.setTenantId(tenantId);
                if (rule.getState() == ComponentLifecycleState.ACTIVE) {
                    rule.setState(ComponentLifecycleState.SUSPENDED);
                    RuleMetaData savedRule = ruleService.saveRule(rule);
                    ruleService.activateRuleById(savedRule.getId());
                } else {
                    ruleService.saveRule(rule);
                }
            } catch (Exception e) {
                log.error("Unable to load rule from json: [{}]", path.toString());
                throw new RuntimeException("Unable to load rule from json", e);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) JsonNode(com.fasterxml.jackson.databind.JsonNode) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) IOException(java.io.IOException)

Example 23 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class RuleController method activateRuleById.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/rule/{ruleId}/activate", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public void activateRuleById(@PathVariable(RULE_ID) String strRuleId) throws ThingsboardException {
    checkParameter(RULE_ID, strRuleId);
    try {
        RuleId ruleId = new RuleId(toUUID(strRuleId));
        RuleMetaData rule = checkRule(ruleService.findRuleById(ruleId));
        ruleService.activateRuleById(ruleId);
        actorService.onRuleStateChange(rule.getTenantId(), rule.getId(), ComponentLifecycleEvent.ACTIVATED);
        logEntityAction(rule.getId(), rule, null, ActionType.ACTIVATED, null, strRuleId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE), null, null, ActionType.ACTIVATED, e, strRuleId);
        throw handleException(e);
    }
}
Also used : RuleId(org.thingsboard.server.common.data.id.RuleId) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 24 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class RuleController method deleteRule.

@PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')")
@RequestMapping(value = "/rule/{ruleId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteRule(@PathVariable(RULE_ID) String strRuleId) throws ThingsboardException {
    checkParameter(RULE_ID, strRuleId);
    try {
        RuleId ruleId = new RuleId(toUUID(strRuleId));
        RuleMetaData rule = checkRule(ruleService.findRuleById(ruleId));
        ruleService.deleteRuleById(ruleId);
        actorService.onRuleStateChange(rule.getTenantId(), rule.getId(), ComponentLifecycleEvent.DELETED);
        logEntityAction(ruleId, rule, null, ActionType.DELETED, null, strRuleId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.RULE), null, null, ActionType.DELETED, e, strRuleId);
        throw handleException(e);
    }
}
Also used : RuleId(org.thingsboard.server.common.data.id.RuleId) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 25 with RuleMetaData

use of org.thingsboard.server.common.data.rule.RuleMetaData in project thingsboard by thingsboard.

the class RuleManager method update.

public Optional<ActorRef> update(ActorContext context, RuleId ruleId, ComponentLifecycleEvent event) {
    if (ruleMap == null) {
        doInit(context);
    }
    RuleMetaData rule;
    if (event != ComponentLifecycleEvent.DELETED) {
        rule = systemContext.getRuleService().findRuleById(ruleId);
    } else {
        rule = ruleMap.keySet().stream().filter(r -> r.getId().equals(ruleId)).peek(r -> r.setState(ComponentLifecycleState.SUSPENDED)).findFirst().orElse(null);
        if (rule != null) {
            ruleMap.remove(rule);
            ruleActors.remove(ruleId);
        }
    }
    if (rule != null) {
        RuleActorMetaData actorMd = ruleMap.get(rule);
        if (actorMd == null) {
            ActorRef ref = getOrCreateRuleActor(context, rule.getId());
            actorMd = RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref);
            ruleMap.put(rule, actorMd);
        }
        refreshRuleChain();
        return Optional.of(actorMd.getActorRef());
    } else {
        log.warn("[{}] Can't process unknown rule!", ruleId);
        return Optional.empty();
    }
}
Also used : RuleId(org.thingsboard.server.common.data.id.RuleId) RuleService(org.thingsboard.server.dao.rule.RuleService) java.util(java.util) ActorContext(akka.actor.ActorContext) SimpleRuleActorChain(org.thingsboard.server.actors.rule.SimpleRuleActorChain) ContextAwareActor(org.thingsboard.server.actors.service.ContextAwareActor) FetchFunction(org.thingsboard.server.common.data.page.PageDataIterable.FetchFunction) TenantId(org.thingsboard.server.common.data.id.TenantId) ComponentLifecycleEvent(org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent) Slf4j(lombok.extern.slf4j.Slf4j) RuleActor(org.thingsboard.server.actors.rule.RuleActor) RuleActorChain(org.thingsboard.server.actors.rule.RuleActorChain) PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) ActorRef(akka.actor.ActorRef) ActorSystemContext(org.thingsboard.server.actors.ActorSystemContext) Props(akka.actor.Props) RuleActorMetaData(org.thingsboard.server.actors.rule.RuleActorMetaData) RuleActorMetaData(org.thingsboard.server.actors.rule.RuleActorMetaData) ActorRef(akka.actor.ActorRef) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Aggregations

RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)43 Test (org.junit.Test)20 TenantId (org.thingsboard.server.common.data.id.TenantId)12 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)12 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)10 RuleId (org.thingsboard.server.common.data.id.RuleId)8 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)7 ArrayList (java.util.ArrayList)6 UUID (java.util.UUID)5 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)3 ActorRef (akka.actor.ActorRef)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 RuleActorMetaData (org.thingsboard.server.actors.rule.RuleActorMetaData)2 SimpleRuleActorChain (org.thingsboard.server.actors.rule.SimpleRuleActorChain)2 PageDataIterable (org.thingsboard.server.common.data.page.PageDataIterable)2