You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@metron.apache.org by cestella <gi...@git.apache.org> on 2016/09/24 05:16:45 UTC

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

GitHub user cestella opened a pull request:

    https://github.com/apache/incubator-metron/pull/269

    METRON-452: Add rudimentary configuration management functions to Stellar

    Currently, the only way to add enrichments is via editing JSON files and pushing them to zookeeper. Rather than that, because we have a REPL, we should create management functions around:
    * Shell functions - Functions surrounding interacting with the shell in either a nicer way or a more functional way.
    * Configuration functions - Functions surrounding pulling and pushing configs from zookeeper
    * Parser functions - Functions surrounding adding, viewing, and removing Parser functions.
    * Enrichment functions - Functions surrounding adding, viewing and removing Stellar enrichments as well as managing batch size and index names for the enrichment topology configuration
    * Threat Triage functions - Functions surrounding adding, viewing and removing threat triage functions.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/cestella/incubator-metron METRON-452

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/incubator-metron/pull/269.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #269
    
----
commit 631b1a4adf416da8d9afc01f5798b67cf4c71934
Author: cstella <ce...@gmail.com>
Date:   2016-09-24T05:15:12Z

    METRON-452: Add rudimentary configuration management functions to Stellar

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron issue #269: METRON-452: Add rudimentary configuration manag...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on the issue:

    https://github.com/apache/incubator-metron/pull/269
  
    I have provided some fairly full-featured examples of using these functions within the REPL in the form of REPL transcripts [here](https://github.com/cestella/incubator-metron/blob/631b1a4adf416da8d9afc01f5798b67cf4c71934/metron-platform/metron-management/README.md#examples) in the docs as an aid to both understanding and testing.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80502328
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/EnrichmentConfigFunctions.java ---
    @@ -0,0 +1,351 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.enrichment.EnrichmentConfig;
    +import org.apache.metron.common.configuration.enrichment.SensorEnrichmentConfig;
    +import org.apache.metron.common.dsl.Context;
    +import org.apache.metron.common.dsl.ParseException;
    +import org.apache.metron.common.dsl.Stellar;
    +import org.apache.metron.common.dsl.StellarFunction;
    +import org.apache.metron.common.utils.ConversionUtils;
    +import org.apache.metron.common.utils.JSONUtils;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.ENRICHMENT;
    +
    +public class EnrichmentConfigFunctions {
    +
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +  public enum Type {
    +    ENRICHMENT, THREAT_INTEL, THREATINTEL;
    +  }
    +  public static Map<String, Object> getStellarHandler(EnrichmentConfig enrichmentConfig) {
    +    Map<String, Object> fieldMap = enrichmentConfig.getFieldMap();
    +    Map<String, Object> stellarHandler = (Map<String, Object>) fieldMap.get("stellar");
    +    if(stellarHandler == null ) {
    +      stellarHandler = new HashMap<String, Object>();
    +      fieldMap.put("stellar", stellarHandler);
    +    }
    +
    +    if(stellarHandler.get("config") == null){
    --- End diff --
    
    Can we use `stellarHandler.putIfAbsent("config", new LinkedHashMap<String, Object>());`?  It's a Java 8 thing, and I haven't seen anyone suggest moving back to Java 7, so might as well get some use out of those functions.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron issue #269: METRON-452: Add rudimentary configuration manag...

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on the issue:

    https://github.com/apache/incubator-metron/pull/269
  
    I'm +1, pending the Travis check


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80500701
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/ParserConfigFunctions.java ---
    @@ -0,0 +1,215 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationType;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.SensorParserConfig;
    +import org.apache.metron.common.dsl.*;
    +import org.apache.metron.common.field.transformation.FieldTransformation;
    +import org.apache.metron.common.field.transformation.FieldTransformations;
    +import org.apache.metron.common.stellar.shell.StellarExecutor;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.jboss.aesh.console.Console;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.PARSER;
    +
    +public class ParserConfigFunctions {
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +
    +  private static void pruneEmptyStellarTransformers(SensorParserConfig config) {
    +    List<FieldTransformer> toRemove = new ArrayList<>();
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    --- End diff --
    
    This set of if statements seems questionable.  First, should the `transformer == null` be moved up a level?  It already gets dereferenced, so the check is either unnecessary or should be moved up a level.  At that point, I question why there are nested ifs in the first place (if it's anything other than for clarity, given the length of the first if).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80511243
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/ParserConfigFunctions.java ---
    @@ -0,0 +1,215 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationType;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.SensorParserConfig;
    +import org.apache.metron.common.dsl.*;
    +import org.apache.metron.common.field.transformation.FieldTransformation;
    +import org.apache.metron.common.field.transformation.FieldTransformations;
    +import org.apache.metron.common.stellar.shell.StellarExecutor;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.jboss.aesh.console.Console;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.PARSER;
    +
    +public class ParserConfigFunctions {
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +
    +  private static void pruneEmptyStellarTransformers(SensorParserConfig config) {
    +    List<FieldTransformer> toRemove = new ArrayList<>();
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    --- End diff --
    
    made statements less questionable


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80501076
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/ParserConfigFunctions.java ---
    @@ -0,0 +1,215 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationType;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.SensorParserConfig;
    +import org.apache.metron.common.dsl.*;
    +import org.apache.metron.common.field.transformation.FieldTransformation;
    +import org.apache.metron.common.field.transformation.FieldTransformations;
    +import org.apache.metron.common.stellar.shell.StellarExecutor;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.jboss.aesh.console.Console;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.PARSER;
    +
    +public class ParserConfigFunctions {
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +
    +  private static void pruneEmptyStellarTransformers(SensorParserConfig config) {
    +    List<FieldTransformer> toRemove = new ArrayList<>();
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        if(transformer == null || transformer.getConfig().isEmpty()) {
    +          toRemove.add(transformer);
    +        }
    +      }
    +    }
    +    for(FieldTransformer t : toRemove) {
    +      fieldTransformations.remove(t);
    +    }
    +  }
    +  private static FieldTransformer getStellarTransformer(SensorParserConfig config) {
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    FieldTransformer stellarTransformer = null;
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        stellarTransformer = transformer;
    +      }
    +    }
    +    if(stellarTransformer == null) {
    +      stellarTransformer = new FieldTransformer();
    +      stellarTransformer.setConfig(new LinkedHashMap<>());
    +      stellarTransformer.setTransformation(FieldTransformations.STELLAR.toString());
    +      fieldTransformations.add(stellarTransformer);
    +    }
    +    return stellarTransformer;
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "PRINT"
    +          ,description = "Retrieve stellar field transformations."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    }
    +          ,returns = "The String representation of the transformations"
    +          )
    +  public static class PrintStellarTransformation implements StellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      String[] headers = new String[] { "Field", "Transformation"};
    +      String[][] data = new String[stellarTransformer.getConfig().size()][2];
    +      int i = 0;
    +      for(Map.Entry<String, Object> kv : stellarTransformer.getConfig().entrySet()) {
    +        data[i++] = new String[] {kv.getKey(), kv.getValue().toString()};
    +      }
    +      return FlipTable.of(headers, data);
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "REMOVE"
    +          ,description = "Remove stellar field transformation."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    ,"stellarTransforms - A list of stellar transforms to remove"
    +                    }
    +          ,returns = "The String representation of the config in zookeeper"
    +          )
    +  public static class RemoveStellarTransformation implements StellarFunction{
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      List<String> removals = (List<String>)args.get(1);
    +      if(removals == null || removals.isEmpty()) {
    +        return config;
    +      }
    +      for(String removal : removals) {
    +        stellarTransformer.getConfig().remove(removal);
    +      }
    +      List<String> output = new ArrayList<>();
    +      for(String key : stellarTransformer.getConfig().keySet()) {
    --- End diff --
    
    Can we make this `output.addAll(stellarTransformer.getConfig().keySet());`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80511265
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/ParserConfigFunctions.java ---
    @@ -0,0 +1,215 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationType;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.SensorParserConfig;
    +import org.apache.metron.common.dsl.*;
    +import org.apache.metron.common.field.transformation.FieldTransformation;
    +import org.apache.metron.common.field.transformation.FieldTransformations;
    +import org.apache.metron.common.stellar.shell.StellarExecutor;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.jboss.aesh.console.Console;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.PARSER;
    +
    +public class ParserConfigFunctions {
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +
    +  private static void pruneEmptyStellarTransformers(SensorParserConfig config) {
    +    List<FieldTransformer> toRemove = new ArrayList<>();
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        if(transformer == null || transformer.getConfig().isEmpty()) {
    +          toRemove.add(transformer);
    +        }
    +      }
    +    }
    +    for(FieldTransformer t : toRemove) {
    +      fieldTransformations.remove(t);
    +    }
    +  }
    +  private static FieldTransformer getStellarTransformer(SensorParserConfig config) {
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    FieldTransformer stellarTransformer = null;
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        stellarTransformer = transformer;
    +      }
    +    }
    +    if(stellarTransformer == null) {
    +      stellarTransformer = new FieldTransformer();
    +      stellarTransformer.setConfig(new LinkedHashMap<>());
    +      stellarTransformer.setTransformation(FieldTransformations.STELLAR.toString());
    +      fieldTransformations.add(stellarTransformer);
    +    }
    +    return stellarTransformer;
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "PRINT"
    +          ,description = "Retrieve stellar field transformations."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    }
    +          ,returns = "The String representation of the transformations"
    +          )
    +  public static class PrintStellarTransformation implements StellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      String[] headers = new String[] { "Field", "Transformation"};
    +      String[][] data = new String[stellarTransformer.getConfig().size()][2];
    +      int i = 0;
    +      for(Map.Entry<String, Object> kv : stellarTransformer.getConfig().entrySet()) {
    +        data[i++] = new String[] {kv.getKey(), kv.getValue().toString()};
    +      }
    +      return FlipTable.of(headers, data);
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "REMOVE"
    +          ,description = "Remove stellar field transformation."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    ,"stellarTransforms - A list of stellar transforms to remove"
    +                    }
    +          ,returns = "The String representation of the config in zookeeper"
    +          )
    +  public static class RemoveStellarTransformation implements StellarFunction{
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      List<String> removals = (List<String>)args.get(1);
    +      if(removals == null || removals.isEmpty()) {
    +        return config;
    +      }
    +      for(String removal : removals) {
    +        stellarTransformer.getConfig().remove(removal);
    +      }
    +      List<String> output = new ArrayList<>();
    +      for(String key : stellarTransformer.getConfig().keySet()) {
    --- End diff --
    
    done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by justinleet <gi...@git.apache.org>.
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80501209
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/ParserConfigFunctions.java ---
    @@ -0,0 +1,215 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationType;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.SensorParserConfig;
    +import org.apache.metron.common.dsl.*;
    +import org.apache.metron.common.field.transformation.FieldTransformation;
    +import org.apache.metron.common.field.transformation.FieldTransformations;
    +import org.apache.metron.common.stellar.shell.StellarExecutor;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.jboss.aesh.console.Console;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.PARSER;
    +
    +public class ParserConfigFunctions {
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +
    +  private static void pruneEmptyStellarTransformers(SensorParserConfig config) {
    +    List<FieldTransformer> toRemove = new ArrayList<>();
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        if(transformer == null || transformer.getConfig().isEmpty()) {
    +          toRemove.add(transformer);
    +        }
    +      }
    +    }
    +    for(FieldTransformer t : toRemove) {
    +      fieldTransformations.remove(t);
    +    }
    +  }
    +  private static FieldTransformer getStellarTransformer(SensorParserConfig config) {
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    FieldTransformer stellarTransformer = null;
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        stellarTransformer = transformer;
    +      }
    +    }
    +    if(stellarTransformer == null) {
    +      stellarTransformer = new FieldTransformer();
    +      stellarTransformer.setConfig(new LinkedHashMap<>());
    +      stellarTransformer.setTransformation(FieldTransformations.STELLAR.toString());
    +      fieldTransformations.add(stellarTransformer);
    +    }
    +    return stellarTransformer;
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "PRINT"
    +          ,description = "Retrieve stellar field transformations."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    }
    +          ,returns = "The String representation of the transformations"
    +          )
    +  public static class PrintStellarTransformation implements StellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      String[] headers = new String[] { "Field", "Transformation"};
    +      String[][] data = new String[stellarTransformer.getConfig().size()][2];
    +      int i = 0;
    +      for(Map.Entry<String, Object> kv : stellarTransformer.getConfig().entrySet()) {
    +        data[i++] = new String[] {kv.getKey(), kv.getValue().toString()};
    +      }
    +      return FlipTable.of(headers, data);
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "REMOVE"
    +          ,description = "Remove stellar field transformation."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    ,"stellarTransforms - A list of stellar transforms to remove"
    +                    }
    +          ,returns = "The String representation of the config in zookeeper"
    +          )
    +  public static class RemoveStellarTransformation implements StellarFunction{
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      List<String> removals = (List<String>)args.get(1);
    +      if(removals == null || removals.isEmpty()) {
    +        return config;
    +      }
    +      for(String removal : removals) {
    +        stellarTransformer.getConfig().remove(removal);
    +      }
    +      List<String> output = new ArrayList<>();
    +      for(String key : stellarTransformer.getConfig().keySet()) {
    +        output.add(key);
    +      }
    +      stellarTransformer.setOutput(output);
    +      pruneEmptyStellarTransformers(configObj);
    +      try {
    +        return JSONUtils.INSTANCE.toJSON(configObj, true);
    +      } catch (JsonProcessingException e) {
    +        LOG.error("Unable to convert object to JSON: " + configObj, e);
    +        return config;
    +      }
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "ADD"
    +          ,description = "Add stellar field transformation."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    ,"stellarTransforms - A Map associating fields to stellar expressions"
    +                    }
    +          ,returns = "The String representation of the config in zookeeper"
    +          )
    +  public static class AddStellarTransformation implements StellarFunction{
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      Map<String, String> additionalTransforms = (Map<String, String>) args.get(1);
    +      if(additionalTransforms == null || additionalTransforms.isEmpty()) {
    +        return config;
    +      }
    +      for(Map.Entry<String, String> kv : additionalTransforms.entrySet()) {
    +        stellarTransformer.getConfig().put(kv.getKey(), kv.getValue());
    +
    +      }
    +      List<String> output = new ArrayList<>();
    --- End diff --
    
    Same deal with `output.addAll(stellarTransformer.getConfig().keySet());`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/incubator-metron/pull/269


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] incubator-metron pull request #269: METRON-452: Add rudimentary configuratio...

Posted by cestella <gi...@git.apache.org>.
Github user cestella commented on a diff in the pull request:

    https://github.com/apache/incubator-metron/pull/269#discussion_r80511284
  
    --- Diff: metron-platform/metron-management/src/main/java/org/apache/metron/management/ParserConfigFunctions.java ---
    @@ -0,0 +1,215 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.metron.management;
    +
    +import com.fasterxml.jackson.core.JsonProcessingException;
    +import com.google.common.base.Splitter;
    +import com.google.common.collect.Iterables;
    +import com.jakewharton.fliptables.FlipTable;
    +import org.apache.log4j.Logger;
    +import org.apache.metron.common.configuration.ConfigurationType;
    +import org.apache.metron.common.configuration.FieldTransformer;
    +import org.apache.metron.common.configuration.SensorParserConfig;
    +import org.apache.metron.common.dsl.*;
    +import org.apache.metron.common.field.transformation.FieldTransformation;
    +import org.apache.metron.common.field.transformation.FieldTransformations;
    +import org.apache.metron.common.stellar.shell.StellarExecutor;
    +import org.apache.metron.common.utils.JSONUtils;
    +import org.jboss.aesh.console.Console;
    +
    +import java.util.*;
    +
    +import static org.apache.metron.common.configuration.ConfigurationType.PARSER;
    +
    +public class ParserConfigFunctions {
    +  private static final Logger LOG = Logger.getLogger(ConfigurationFunctions.class);
    +
    +  private static void pruneEmptyStellarTransformers(SensorParserConfig config) {
    +    List<FieldTransformer> toRemove = new ArrayList<>();
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        if(transformer == null || transformer.getConfig().isEmpty()) {
    +          toRemove.add(transformer);
    +        }
    +      }
    +    }
    +    for(FieldTransformer t : toRemove) {
    +      fieldTransformations.remove(t);
    +    }
    +  }
    +  private static FieldTransformer getStellarTransformer(SensorParserConfig config) {
    +    List<FieldTransformer> fieldTransformations = config.getFieldTransformations();
    +    FieldTransformer stellarTransformer = null;
    +    for(FieldTransformer transformer : fieldTransformations) {
    +      if(transformer.getFieldTransformation().getClass().getName()
    +              .equals(FieldTransformations.STELLAR.getMappingClass().getName())) {
    +        stellarTransformer = transformer;
    +      }
    +    }
    +    if(stellarTransformer == null) {
    +      stellarTransformer = new FieldTransformer();
    +      stellarTransformer.setConfig(new LinkedHashMap<>());
    +      stellarTransformer.setTransformation(FieldTransformations.STELLAR.toString());
    +      fieldTransformations.add(stellarTransformer);
    +    }
    +    return stellarTransformer;
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "PRINT"
    +          ,description = "Retrieve stellar field transformations."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    }
    +          ,returns = "The String representation of the transformations"
    +          )
    +  public static class PrintStellarTransformation implements StellarFunction {
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      String[] headers = new String[] { "Field", "Transformation"};
    +      String[][] data = new String[stellarTransformer.getConfig().size()][2];
    +      int i = 0;
    +      for(Map.Entry<String, Object> kv : stellarTransformer.getConfig().entrySet()) {
    +        data[i++] = new String[] {kv.getKey(), kv.getValue().toString()};
    +      }
    +      return FlipTable.of(headers, data);
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "REMOVE"
    +          ,description = "Remove stellar field transformation."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    ,"stellarTransforms - A list of stellar transforms to remove"
    +                    }
    +          ,returns = "The String representation of the config in zookeeper"
    +          )
    +  public static class RemoveStellarTransformation implements StellarFunction{
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      List<String> removals = (List<String>)args.get(1);
    +      if(removals == null || removals.isEmpty()) {
    +        return config;
    +      }
    +      for(String removal : removals) {
    +        stellarTransformer.getConfig().remove(removal);
    +      }
    +      List<String> output = new ArrayList<>();
    +      for(String key : stellarTransformer.getConfig().keySet()) {
    +        output.add(key);
    +      }
    +      stellarTransformer.setOutput(output);
    +      pruneEmptyStellarTransformers(configObj);
    +      try {
    +        return JSONUtils.INSTANCE.toJSON(configObj, true);
    +      } catch (JsonProcessingException e) {
    +        LOG.error("Unable to convert object to JSON: " + configObj, e);
    +        return config;
    +      }
    +    }
    +
    +    @Override
    +    public void initialize(Context context) {
    +
    +    }
    +
    +    @Override
    +    public boolean isInitialized() {
    +      return true;
    +    }
    +  }
    +
    +  @Stellar(
    +           namespace = "PARSER_STELLAR_TRANSFORM"
    +          ,name = "ADD"
    +          ,description = "Add stellar field transformation."
    +          ,params = {"sensorConfig - Sensor config to add transformation to."
    +                    ,"stellarTransforms - A Map associating fields to stellar expressions"
    +                    }
    +          ,returns = "The String representation of the config in zookeeper"
    +          )
    +  public static class AddStellarTransformation implements StellarFunction{
    +
    +    @Override
    +    public Object apply(List<Object> args, Context context) throws ParseException {
    +      String config = (String) args.get(0);
    +      if(config == null) {
    +        return null;
    +      }
    +      SensorParserConfig configObj = (SensorParserConfig) PARSER.deserialize(config);
    +      FieldTransformer stellarTransformer = getStellarTransformer(configObj);
    +      Map<String, String> additionalTransforms = (Map<String, String>) args.get(1);
    +      if(additionalTransforms == null || additionalTransforms.isEmpty()) {
    +        return config;
    +      }
    +      for(Map.Entry<String, String> kv : additionalTransforms.entrySet()) {
    +        stellarTransformer.getConfig().put(kv.getKey(), kv.getValue());
    +
    +      }
    +      List<String> output = new ArrayList<>();
    --- End diff --
    
    done


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---