/
DroolsCorrelationEngineBuilder.java
339 lines (293 loc) · 13.6 KB
/
DroolsCorrelationEngineBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2007-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.netmgt.correlation.drools;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.opennms.core.xml.JaxbUtils;
import org.opennms.netmgt.correlation.CorrelationEngine;
import org.opennms.netmgt.correlation.CorrelationEngineRegistrar;
import org.opennms.netmgt.correlation.drools.config.EngineConfiguration;
import org.opennms.netmgt.correlation.drools.config.RuleSet;
import org.opennms.netmgt.events.api.EventConstants;
import org.opennms.netmgt.events.api.EventIpcManager;
import org.opennms.netmgt.events.api.EventListener;
import org.opennms.netmgt.events.api.EventProxyException;
import org.opennms.netmgt.model.events.EventBuilder;
import org.opennms.netmgt.xml.event.Event;
import org.opennms.netmgt.xml.event.Parm;
import org.springframework.beans.PropertyEditorRegistrySupport;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import com.codahale.metrics.MetricRegistry;
/**
* <p>DroolsCorrelationEngineBuilder class.</p>
*
* @author <a href="mailto:brozow@opennms.org">Mathew Brozowski</a>
* @version $Id: $
*/
public class DroolsCorrelationEngineBuilder extends PropertyEditorRegistrySupport implements InitializingBean, EventListener {
private static final Logger LOG = LoggerFactory.getLogger(DroolsCorrelationEngineBuilder.class);
public static final String PLUGIN_CONFIG_FILE_NAME = "drools-engine.xml";
private static class PluginConfiguration {
private Resource m_configResource;
private EngineConfiguration m_configuration;
public PluginConfiguration(Resource configResource) {
m_configResource = configResource;
}
public void readConfig() {
LOG.info("Parsing drools engine configuration at {}.", m_configResource);
m_configuration = JaxbUtils.unmarshal(EngineConfiguration.class, m_configResource);
}
public CorrelationEngine[] constructEngines(ApplicationContext appContext, EventIpcManager eventIpcManager, MetricRegistry metricRegistry) {
LOG.info("Creating drools engines for configuration {}.", m_configResource);
return m_configuration.constructEngines(m_configResource, appContext, eventIpcManager, metricRegistry);
}
public List<RuleSet> getRuleSets() {
if (m_configuration == null) return Collections.emptyList();
return m_configuration.getRuleSetCollection();
}
public Resource getConfigResource() {
return m_configResource;
}
}
// injected
@javax.annotation.Resource(name="droolsCorrelationEngineBuilderConfigurationDirectory")
private File m_configDirectory;
@javax.annotation.Resource(name="droolsCorrelationEngineBuilderConfigurationResource")
private Resource m_configResource;
@Autowired
@Qualifier("eventIpcManager")
private EventIpcManager m_eventIpcManager;
@Autowired
@Qualifier("correlator")
private CorrelationEngineRegistrar m_correlator;
@Autowired
@Qualifier("metricRegistry")
private MetricRegistry m_metricRegistry;
@Autowired
private ApplicationContext m_appContext;
// built
private PluginConfiguration[] m_pluginConfigurations;
/**
* <p>Constructor for DroolsCorrelationEngineBuilder.</p>
*/
public DroolsCorrelationEngineBuilder() {
registerDefaultEditors();
}
/**
* <p>assertSet</p>
*
* @param obj a {@link java.lang.Object} object.
* @param name a {@link java.lang.String} object.
*/
public void assertSet(final Object obj, final String name) {
Assert.state(obj != null, name+" required for DroolsEngineFactoryBean");
}
/**
* <p>afterPropertiesSet</p>
*
* @throws java.lang.Exception if any.
*/
@Override
public void afterPropertiesSet() throws Exception {
assertSet(m_configDirectory, "configurationDirectory");
assertSet(m_eventIpcManager, "eventIpcManager");
assertSet(m_correlator, "correlator");
assertSet(m_metricRegistry, "metricRegistry");
assertSet(m_appContext, "applicationContext");
Assert.state(!m_configDirectory.exists() || m_configDirectory.isDirectory(), m_configDirectory+" must be a directory!");
m_eventIpcManager.addEventListener(this, EventConstants.RELOAD_DAEMON_CONFIG_UEI);
readConfiguration();
registerEngines();
}
private void registerEngines() {
for(PluginConfiguration pluginConfig : m_pluginConfigurations) {
m_correlator.addCorrelationEngines(pluginConfig.constructEngines(m_appContext, m_eventIpcManager, m_metricRegistry));
}
}
/**
* <p>setEventIpcManager</p>
*
* @param eventIpcManager a {@link org.opennms.netmgt.events.api.EventIpcManager} object.
*/
public void setEventIpcManager(final EventIpcManager eventIpcManager) {
m_eventIpcManager = eventIpcManager;
}
/**
* <p>setMetricRegistry</p>
*
* @param metricRegistry a {@link com.codahale.metrics.MetricRegistry} object.
*/
public void setMetricRegistry(final MetricRegistry metricRegistry) {
m_metricRegistry = metricRegistry;
}
/**
* <p>setConfigurationResource</p>
*
* @param configResource a {@link org.springframework.core.io.Resource} object.
*/
public void setConfigurationResource(final Resource configResource) {
m_configResource = configResource;
}
/**
* <p>setConfigurationDirectory</p>
*
* @param configDirectory a {@link java.io.File} object.
*/
public void setConfigurationDirectory(final File configDirectory) {
m_configDirectory = configDirectory;
}
/**
* <p>setCorrelationEngineRegistrar</p>
*
* @param correlator a {@link org.opennms.netmgt.correlation.CorrelationEngineRegistrar} object.
*/
public void setCorrelationEngineRegistrar(final CorrelationEngineRegistrar correlator) {
m_correlator = correlator;
}
private void readConfiguration() throws Exception {
m_pluginConfigurations = locatePluginConfigurations();
// now parse all of the configuration files
for(PluginConfiguration pluginCofig : m_pluginConfigurations) {
pluginCofig.readConfig();
}
}
private PluginConfiguration[] locatePluginConfigurations() throws Exception {
List<PluginConfiguration> pluginConfigs = new LinkedList<PluginConfiguration>();
// first we see if the config is etc exists
if (m_configResource != null && m_configResource.isReadable()) {
LOG.info("Found Drools Plugin config file {}.", m_configResource);
pluginConfigs.add(new PluginConfiguration(m_configResource));
}
// then we look in each plugin dir for a config
File[] pluginDirs = getPluginDirs();
for(File pluginDir : pluginDirs) {
File configFile = new File(pluginDir, PLUGIN_CONFIG_FILE_NAME);
if (!configFile.exists()) {
LOG.error("Drools Plugin directory {} does not contains a {} config file. Ignoring plugin.", pluginDir, PLUGIN_CONFIG_FILE_NAME);
} else {
LOG.info("Found Drools Plugin directory {} containing a {} config file.", pluginDir, PLUGIN_CONFIG_FILE_NAME);
pluginConfigs.add(new PluginConfiguration(new FileSystemResource(configFile)));
}
}
return pluginConfigs.toArray(new PluginConfiguration[0]);
}
private File[] getPluginDirs() throws Exception {
LOG.debug("Checking {} for drools correlation plugins", m_configDirectory);
if (!m_configDirectory.exists()) {
LOG.debug("Plugin configuration directory does not exists.");
return new File[0];
}
File[] pluginDirs = m_configDirectory.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
LOG.debug("Found {} drools correlation plugin sub directories", pluginDirs.length);
return pluginDirs;
}
@Override
public String getName() {
return "DroolsCorrelationEngine";
}
@Override
public void onEvent(Event e) {
LOG.warn("{} received an event: {}", getName(), e.getUei());
// 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
if (e.getUei().equals(EventConstants.RELOAD_DAEMON_CONFIG_UEI)) {
List<Parm> parmCollection = e.getParmCollection();
for (Parm parm : parmCollection) {
String parmName = parm.getParmName();
if(EventConstants.PARM_DAEMON_NAME.equals(parmName)) {
if (parm.getValue() == null || parm.getValue().getContent() == null) {
LOG.warn("The daemonName parameter has no value, ignoring.");
return;
}
if (parm.getValue().getContent().equals(getName())) {
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(p -> p.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);
Optional<CorrelationEngine> correlator = m_correlator.getEngines().stream().filter(c -> c.getName().equals(en.getName())).findFirst();
if (correlator.isPresent()) {
correlator.get().tearDown();
m_correlator.getEngines().remove(correlator);
}
});
// Find new engines to add
newEngines.stream().filter(en -> !currentEngines.contains(en)).forEach(en -> {
LOG.warn("Adding engine {}", en);
Arrays.stream(newPlugins).filter(p -> p.getRuleSets().contains(en)).findFirst().ifPresent(p -> {
m_correlator.addCorrelationEngine(en.constructEngine(p.getConfigResource(), m_appContext, m_eventIpcManager, m_metricRegistry));
});
});
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);
}
}
return;
}
}
}
}
}
}