Search in sources :

Example 1 with ConfigurationType

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);
}
Also used : ConfigurationType(org.apache.metron.common.configuration.ConfigurationType) BooleanWritable(org.apache.hadoop.io.BooleanWritable) ConfigurationsUtils(org.apache.metron.common.configuration.ConfigurationsUtils) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ConfigurationType

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.");
            }
        }
    });
}
Also used : ConfigurationType(org.apache.metron.common.configuration.ConfigurationType) ConfigurationsUtils(org.apache.metron.common.configuration.ConfigurationsUtils) IOException(java.io.IOException) File(java.io.File)

Example 3 with ConfigurationType

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);
}
Also used : ConfigurationType(org.apache.metron.common.configuration.ConfigurationType) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ParseException(org.apache.metron.stellar.dsl.ParseException) CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Aggregations

ConfigurationType (org.apache.metron.common.configuration.ConfigurationType)3 ConfigurationsUtils (org.apache.metron.common.configuration.ConfigurationsUtils)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 File (java.io.File)1 IOException (java.io.IOException)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)1 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)1 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)1 BooleanWritable (org.apache.hadoop.io.BooleanWritable)1 ParseException (org.apache.metron.stellar.dsl.ParseException)1 Test (org.junit.Test)1