Search in sources :

Example 1 with RuleSet

use of org.opennms.netmgt.correlation.drools.config.RuleSet in project opennms by OpenNMS.

the class DroolsCorrelationEngine method reloadConfig.

@Override
public void reloadConfig() {
    EventBuilder ebldr = new EventBuilder(EventConstants.RELOAD_DAEMON_CONFIG_SUCCESSFUL_UEI, getName());
    ebldr.addParam(EventConstants.PARM_DAEMON_NAME, "DroolsCorrelationEngine-" + m_name);
    try {
        LOG.info("Reloading configuration for engine {}", m_name);
        EngineConfiguration cfg = JaxbUtils.unmarshal(EngineConfiguration.class, m_configPath);
        Optional<RuleSet> opt = cfg.getRuleSetCollection().stream().filter(rs -> rs.getName().equals(getName())).findFirst();
        if (opt.isPresent()) {
            marshallStateToDisk(true);
            opt.get().updateEngine(this);
            initialize();
        } else {
            ebldr.setUei(EventConstants.RELOAD_DAEMON_CONFIG_FAILED_UEI);
            ebldr.addParam(EventConstants.PARM_REASON, "RuleSet not found on " + m_configPath);
        }
    } catch (Exception e) {
        ebldr.setUei(EventConstants.RELOAD_DAEMON_CONFIG_FAILED_UEI);
        ebldr.addParam(EventConstants.PARM_REASON, e.getMessage());
    } finally {
        sendEvent(ebldr.getEvent());
    }
}
Also used : EngineConfiguration(org.opennms.netmgt.correlation.drools.config.EngineConfiguration) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) EventConstants(org.opennms.netmgt.events.api.EventConstants) AbstractCorrelationEngine(org.opennms.netmgt.correlation.AbstractCorrelationEngine) Meter(com.codahale.metrics.Meter) Level(org.kie.api.builder.Message.Level) Event(org.opennms.netmgt.xml.event.Event) Map(java.util.Map) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KieServices(org.kie.api.KieServices) JaxbUtils(org.opennms.core.xml.JaxbUtils) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Path(java.nio.file.Path) KieMarshallers(org.kie.api.marshalling.KieMarshallers) AssertBehaviour(org.drools.core.RuleBaseConfiguration.AssertBehaviour) Resource(org.springframework.core.io.Resource) KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) Logger(org.slf4j.Logger) MetricRegistry(com.codahale.metrics.MetricRegistry) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Logging(org.opennms.core.logging.Logging) FileOutputStream(java.io.FileOutputStream) KieContainer(org.kie.api.runtime.KieContainer) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) List(java.util.List) Paths(java.nio.file.Paths) RuleSet(org.opennms.netmgt.correlation.drools.config.RuleSet) ByteStreams(com.google.common.io.ByteStreams) Optional(java.util.Optional) Marshaller(org.kie.api.marshalling.Marshaller) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Gauge(com.codahale.metrics.Gauge) InputStream(java.io.InputStream) RuleSet(org.opennms.netmgt.correlation.drools.config.RuleSet) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) EngineConfiguration(org.opennms.netmgt.correlation.drools.config.EngineConfiguration) IOException(java.io.IOException) DroolsParserException(org.drools.compiler.compiler.DroolsParserException)

Example 2 with RuleSet

use of org.opennms.netmgt.correlation.drools.config.RuleSet in project opennms by OpenNMS.

the class DroolsCorrelationEngineBuilder method doAddAndRemoveEngines.

// This handles only the addition of new engines and the removal of existing engines.
// For updating existing engines, use the appropriate value for the daemonName
private void doAddAndRemoveEngines() {
    LOG.info("Analyzing directory {} to add/remove drools engines...", m_configDirectory);
    EventBuilder ebldr = null;
    try {
        final PluginConfiguration[] newPlugins = locatePluginConfigurations();
        final List<RuleSet> newEngines = Arrays.stream(newPlugins).peek(PluginConfiguration::readConfig).flatMap(pc -> pc.getRuleSets().stream()).collect(Collectors.toList());
        final List<RuleSet> currentEngines = Arrays.stream(m_pluginConfigurations).flatMap(pc -> pc.getRuleSets().stream()).collect(Collectors.toList());
        LOG.debug("Current engines: {}", currentEngines);
        LOG.debug("New engines: {}", newEngines);
        // Find old engines to remove
        currentEngines.stream().filter(en -> !newEngines.contains(en)).forEach(en -> {
            LOG.warn("Deleting engine {}", en);
            m_correlator.removeCorrelationEngine(en.getName());
        });
        // Find new engines to add
        newEngines.stream().filter(en -> !currentEngines.contains(en)).forEach(en -> {
            LOG.warn("Adding engine {}", en);
            addEngine(newPlugins, en);
        });
        m_pluginConfigurations = newPlugins;
        ebldr = new EventBuilder(EventConstants.RELOAD_DAEMON_CONFIG_SUCCESSFUL_UEI, getName());
        ebldr.addParam(EventConstants.PARM_DAEMON_NAME, getName());
    } catch (Exception ex) {
        LOG.error("Cannot process reloadDaemonConfig", ex);
        ebldr = new EventBuilder(EventConstants.RELOAD_DAEMON_CONFIG_FAILED_UEI, getName());
        ebldr.addParam(EventConstants.PARM_DAEMON_NAME, getName());
        ebldr.addParam(EventConstants.PARM_REASON, ex.getMessage());
    } finally {
        if (ebldr != null)
            try {
                m_eventIpcManager.send(ebldr.getEvent());
            } catch (EventProxyException epx) {
                LOG.error("Can't send reloadDaemonConfig status event", epx);
            }
    }
}
Also used : Parm(org.opennms.netmgt.xml.event.Parm) Arrays(java.util.Arrays) PropertyEditorRegistrySupport(org.springframework.beans.PropertyEditorRegistrySupport) EngineConfiguration(org.opennms.netmgt.correlation.drools.config.EngineConfiguration) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) EventConstants(org.opennms.netmgt.events.api.EventConstants) InitializingBean(org.springframework.beans.factory.InitializingBean) CorrelationEngine(org.opennms.netmgt.correlation.CorrelationEngine) EventListener(org.opennms.netmgt.events.api.EventListener) Matcher(java.util.regex.Matcher) Event(org.opennms.netmgt.xml.event.Event) Qualifier(org.springframework.beans.factory.annotation.Qualifier) JaxbUtils(org.opennms.core.xml.JaxbUtils) LinkedList(java.util.LinkedList) Resource(org.springframework.core.io.Resource) Logger(org.slf4j.Logger) MetricRegistry(com.codahale.metrics.MetricRegistry) EventIpcManager(org.opennms.netmgt.events.api.EventIpcManager) FileSystemResource(org.springframework.core.io.FileSystemResource) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) File(java.io.File) List(java.util.List) FileFilter(java.io.FileFilter) RuleSet(org.opennms.netmgt.correlation.drools.config.RuleSet) Pattern(java.util.regex.Pattern) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) Collections(java.util.Collections) CorrelationEngineRegistrar(org.opennms.netmgt.correlation.CorrelationEngineRegistrar) Assert(org.springframework.util.Assert) RuleSet(org.opennms.netmgt.correlation.drools.config.RuleSet) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) EventProxyException(org.opennms.netmgt.events.api.EventProxyException) EventProxyException(org.opennms.netmgt.events.api.EventProxyException)

Example 3 with RuleSet

use of org.opennms.netmgt.correlation.drools.config.RuleSet in project opennms by OpenNMS.

the class DroolsCorrelationEngineBuilder method onEvent.

@Override
public void onEvent(Event e) {
    if (e.getUei().equals(EventConstants.RELOAD_DAEMON_CONFIG_UEI)) {
        final String daemonName = getDaemonNameFromReloadDaemonEvent(e);
        if (daemonName != null && daemonName.equals(getName())) {
            doAddAndRemoveEngines();
            return;
        }
    }
    if (e.getUei().equals(EventConstants.RELOAD_DAEMON_CONFIG_FAILED_UEI)) {
        final String daemonName = getDaemonNameFromReloadDaemonEvent(e);
        Matcher m = Pattern.compile(getName() + "-(.+)$").matcher(daemonName);
        if (m.find()) {
            final String engineName = m.group(1);
            LOG.warn("An error was detected while reloading engine {}, this engine will be removed. Fix the error and try again.", engineName);
            m_correlator.removeCorrelationEngine(engineName);
            for (PluginConfiguration p : m_pluginConfigurations) {
                final RuleSet set = p.getRuleSets().stream().filter(r -> r.getName().equals(engineName)).findFirst().orElse(null);
                if (set != null) {
                    p.getRuleSets().remove(set);
                }
            }
        }
    }
}
Also used : RuleSet(org.opennms.netmgt.correlation.drools.config.RuleSet) Matcher(java.util.regex.Matcher)

Aggregations

RuleSet (org.opennms.netmgt.correlation.drools.config.RuleSet)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 File (java.io.File)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 JaxbUtils (org.opennms.core.xml.JaxbUtils)2 EngineConfiguration (org.opennms.netmgt.correlation.drools.config.EngineConfiguration)2 EventConstants (org.opennms.netmgt.events.api.EventConstants)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 Event (org.opennms.netmgt.xml.event.Event)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Resource (org.springframework.core.io.Resource)2 Gauge (com.codahale.metrics.Gauge)1 Meter (com.codahale.metrics.Meter)1 ByteStreams (com.google.common.io.ByteStreams)1 FileFilter (java.io.FileFilter)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1