Search in sources :

Example 1 with RuleMetaData

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

the class AbstractServiceTest method generateRule.

protected RuleMetaData generateRule(TenantId tenantId, Integer weight, String pluginToken) throws IOException {
    if (tenantId == null) {
        tenantId = new TenantId(UUIDs.timeBased());
    }
    if (weight == null) {
        weight = ThreadLocalRandom.current().nextInt();
    }
    RuleMetaData ruleMetaData = new RuleMetaData();
    ruleMetaData.setName("Testing");
    ruleMetaData.setTenantId(tenantId);
    ruleMetaData.setWeight(weight);
    ruleMetaData.setPluginToken(pluginToken);
    ruleMetaData.setAction(createNode(ComponentScope.TENANT, ComponentType.ACTION, "org.thingsboard.component.ActionTest", "TestJsonDescriptor.json", "TestJsonData.json"));
    ruleMetaData.setProcessor(createNode(ComponentScope.TENANT, ComponentType.PROCESSOR, "org.thingsboard.component.ProcessorTest", "TestJsonDescriptor.json", "TestJsonData.json"));
    ruleMetaData.setFilters(mapper.createArrayNode().add(createNode(ComponentScope.TENANT, ComponentType.FILTER, "org.thingsboard.component.FilterTest", "TestJsonDescriptor.json", "TestJsonData.json")));
    ruleMetaData.setAdditionalInfo(mapper.readTree("{}"));
    return ruleMetaData;
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 2 with RuleMetaData

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

the class RuleActorMessageProcessor method onUpdate.

@Override
public void onUpdate(ActorContext context) throws RuleException {
    RuleMetaData oldRuleMd = ruleMd;
    ruleMd = systemContext.getRuleService().findRuleById(entityId);
    logger.info("[{}] Rule configuration was updated from {} to {}.", entityId, oldRuleMd, ruleMd);
    try {
        fetchPluginInfo();
        if (filters == null || !Objects.equals(oldRuleMd.getFilters(), ruleMd.getFilters())) {
            logger.info("[{}] Rule filters require restart due to json change from {} to {}.", entityId, mapper.writeValueAsString(oldRuleMd.getFilters()), mapper.writeValueAsString(ruleMd.getFilters()));
            stopFilters();
            initFilters();
        }
        if (processor == null || !Objects.equals(oldRuleMd.getProcessor(), ruleMd.getProcessor())) {
            logger.info("[{}] Rule processor require restart due to configuration change.", entityId);
            stopProcessor();
            initProcessor();
        }
        if (action == null || !Objects.equals(oldRuleMd.getAction(), ruleMd.getAction())) {
            logger.info("[{}] Rule action require restart due to configuration change.", entityId);
            stopAction();
            initAction();
        }
    } catch (RuntimeException e) {
        throw new RuleInitializationException("Unknown runtime exception!", e);
    } catch (InstantiationException e) {
        throw new RuleInitializationException("No default constructor for rule implementation!", e);
    } catch (IllegalAccessException e) {
        throw new RuleInitializationException("Illegal Access Exception during rule initialization!", e);
    } catch (ClassNotFoundException e) {
        throw new RuleInitializationException("Rule Class not found!", e);
    } catch (JsonProcessingException e) {
        throw new RuleInitializationException("Rule configuration is invalid!", e);
    } catch (Exception e) {
        throw new RuleInitializationException(e.getMessage(), e);
    }
}
Also used : RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with RuleMetaData

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

the class RuleManager method refreshRuleChain.

private void refreshRuleChain() {
    Set<RuleActorMetaData> activeRuleSet = new HashSet<>();
    for (Map.Entry<RuleMetaData, RuleActorMetaData> rule : ruleMap.entrySet()) {
        if (rule.getKey().getState() == ComponentLifecycleState.ACTIVE) {
            activeRuleSet.add(rule.getValue());
        }
    }
    ruleChain = new SimpleRuleActorChain(activeRuleSet);
}
Also used : RuleActorMetaData(org.thingsboard.server.actors.rule.RuleActorMetaData) SimpleRuleActorChain(org.thingsboard.server.actors.rule.SimpleRuleActorChain) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 4 with RuleMetaData

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

the class RuleManager method doInit.

private void doInit(ActorContext context) {
    PageDataIterable<RuleMetaData> ruleIterator = new PageDataIterable<>(getFetchRulesFunction(), ContextAwareActor.ENTITY_PACK_LIMIT);
    ruleMap = new HashMap<>();
    for (RuleMetaData rule : ruleIterator) {
        log.debug("[{}] Creating rule actor {}", rule.getId(), rule);
        ActorRef ref = getOrCreateRuleActor(context, rule.getId());
        ruleMap.put(rule, RuleActorMetaData.systemRule(rule.getId(), rule.getWeight(), ref));
        log.debug("[{}] Rule actor created.", rule.getId());
    }
    refreshRuleChain();
}
Also used : PageDataIterable(org.thingsboard.server.common.data.page.PageDataIterable) ActorRef(akka.actor.ActorRef) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData)

Example 5 with RuleMetaData

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

the class BasePluginService method suspendPluginById.

@Override
public void suspendPluginById(PluginId pluginId) {
    PluginMetaData plugin = pluginDao.findById(pluginId);
    List<RuleMetaData> affectedRules = ruleDao.findRulesByPlugin(plugin.getApiToken()).stream().filter(rule -> rule.getState() == ComponentLifecycleState.ACTIVE).collect(Collectors.toList());
    if (affectedRules.isEmpty()) {
        updateLifeCycleState(pluginId, ComponentLifecycleState.SUSPENDED);
    } else {
        throw new DataValidationException("Can't suspend plugin that has active rules!");
    }
}
Also used : ComponentType(org.thingsboard.server.common.data.plugin.ComponentType) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Autowired(org.springframework.beans.factory.annotation.Autowired) TextPageData(org.thingsboard.server.common.data.page.TextPageData) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) DataValidator(org.thingsboard.server.dao.service.DataValidator) ArrayList(java.util.ArrayList) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) ComponentLifecycleState(org.thingsboard.server.common.data.plugin.ComponentLifecycleState) PluginId(org.thingsboard.server.common.data.id.PluginId) Service(org.springframework.stereotype.Service) ComponentDescriptorService(org.thingsboard.server.dao.component.ComponentDescriptorService) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) Validator(org.thingsboard.server.dao.service.Validator) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) DatabaseException(org.thingsboard.server.dao.exception.DatabaseException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleDao(org.thingsboard.server.dao.rule.RuleDao) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) RuleMetaData(org.thingsboard.server.common.data.rule.RuleMetaData) PaginatedRemover(org.thingsboard.server.dao.service.PaginatedRemover) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) PluginMetaData(org.thingsboard.server.common.data.plugin.PluginMetaData) 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