use of org.apache.metron.common.configuration.ConfigurationType in project metron by apache.
the class ConfigurationManagerIntegrationTest method testPushAll.
@Test
public void testPushAll() throws Exception {
// push all configs; parser, enrichment, indexing, etc
pushAllConfigs();
// validate
final Set<String> sensorsInZookeeper = new HashSet<>();
final BooleanWritable foundGlobal = new BooleanWritable(false);
ConfigurationsUtils.visitConfigs(client, new ConfigurationsUtils.ConfigurationVisitor() {
@Override
public void visit(ConfigurationType configurationType, String name, String data) {
Assert.assertTrue(data.length() > 0);
validateConfig(name, configurationType, data);
if (configurationType == GLOBAL) {
validateConfig(name, configurationType, data);
foundGlobal.set(true);
} else {
sensorsInZookeeper.add(name);
}
}
});
Assert.assertEquals(true, foundGlobal.get());
Assert.assertEquals(sensorsInZookeeper, sensors);
}
use of org.apache.metron.common.configuration.ConfigurationType in project metron by apache.
the class ConfigurationManager method pull.
public void pull(CuratorFramework client, String outFileStr, final boolean force) throws Exception {
final File outputDir = new File(outFileStr);
if (!outputDir.exists()) {
if (!outputDir.mkdirs()) {
throw new IllegalStateException("Unable to make directories: " + outputDir.getAbsolutePath());
}
}
ConfigurationsUtils.visitConfigs(client, new ConfigurationsUtils.ConfigurationVisitor() {
@Override
public void visit(ConfigurationType configurationType, String name, String data) {
File out = getFile(outputDir, configurationType, name);
if (!out.exists() || force) {
if (!out.exists()) {
out.getParentFile().mkdirs();
}
try {
Files.write(data, out, Charset.defaultCharset());
} catch (IOException e) {
throw new RuntimeException("Sorry, something bad happened writing the config to " + out.getAbsolutePath() + ": " + e.getMessage(), e);
}
} else if (out.exists() && !force) {
throw new IllegalStateException("Unable to overwrite existing file (" + out.getAbsolutePath() + ") without the force flag (-f or --force) being set.");
}
}
});
}
use of org.apache.metron.common.configuration.ConfigurationType in project metron by apache.
the class ConfigurationFunctions method setupTreeCache.
private static synchronized void setupTreeCache(Context context) throws Exception {
try {
Optional<Object> treeCacheOpt = context.getCapability("treeCache");
if (treeCacheOpt.isPresent()) {
return;
}
} catch (IllegalStateException ex) {
}
Optional<Object> clientOpt = context.getCapability(Context.Capabilities.ZOOKEEPER_CLIENT);
if (!clientOpt.isPresent()) {
throw new IllegalStateException("I expected a zookeeper client to exist and it did not. Please connect to zookeeper.");
}
CuratorFramework client = (CuratorFramework) clientOpt.get();
TreeCache cache = new TreeCache(client, Constants.ZOOKEEPER_TOPOLOGY_ROOT);
TreeCacheListener listener = new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event.getType().equals(TreeCacheEvent.Type.NODE_ADDED) || event.getType().equals(TreeCacheEvent.Type.NODE_UPDATED)) {
String path = event.getData().getPath();
byte[] data = event.getData().getData();
String sensor = Iterables.getLast(Splitter.on("/").split(path), null);
if (path.startsWith(ConfigurationType.PARSER.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.PARSER);
sensorMap.put(sensor, new String(data));
} else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.GLOBAL, new String(data));
} else if (ConfigurationType.PROFILER.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.PROFILER, new String(data));
} else if (path.startsWith(ConfigurationType.ENRICHMENT.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.ENRICHMENT);
sensorMap.put(sensor, new String(data));
} else if (path.startsWith(ConfigurationType.INDEXING.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.INDEXING);
sensorMap.put(sensor, new String(data));
}
} else if (event.getType().equals(TreeCacheEvent.Type.NODE_REMOVED)) {
String path = event.getData().getPath();
String sensor = Iterables.getLast(Splitter.on("/").split(path), null);
if (path.startsWith(ConfigurationType.PARSER.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.PARSER);
sensorMap.remove(sensor);
} else if (path.startsWith(ConfigurationType.ENRICHMENT.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.ENRICHMENT);
sensorMap.remove(sensor);
} else if (path.startsWith(ConfigurationType.INDEXING.getZookeeperRoot())) {
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ConfigurationType.INDEXING);
sensorMap.remove(sensor);
} else if (ConfigurationType.PROFILER.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.PROFILER, null);
} else if (ConfigurationType.GLOBAL.getZookeeperRoot().equals(path)) {
configMap.put(ConfigurationType.GLOBAL, null);
}
}
}
};
cache.getListenable().addListener(listener);
cache.start();
for (ConfigurationType ct : ConfigurationType.values()) {
switch(ct) {
case GLOBAL:
case PROFILER:
{
String data = "";
try {
byte[] bytes = ConfigurationsUtils.readFromZookeeper(ct.getZookeeperRoot(), client);
data = new String(bytes);
} catch (Exception ex) {
}
configMap.put(ct, data);
}
break;
case INDEXING:
case ENRICHMENT:
case PARSER:
{
List<String> sensorTypes = client.getChildren().forPath(ct.getZookeeperRoot());
Map<String, String> sensorMap = (Map<String, String>) configMap.get(ct);
for (String sensorType : sensorTypes) {
sensorMap.put(sensorType, new String(ConfigurationsUtils.readFromZookeeper(ct.getZookeeperRoot() + "/" + sensorType, client)));
}
}
break;
}
}
context.addCapability("treeCache", () -> cache);
}
Aggregations