You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metron.apache.org by mm...@apache.org on 2016/10/25 18:36:55 UTC

incubator-metron git commit: METRON-505: Add environment variable and system property functions to the Stellar language (mmiklavc) closes apache/incubator-metron#312

Repository: incubator-metron
Updated Branches:
  refs/heads/master d6d3d4572 -> 61351aabd


METRON-505: Add environment variable and system property functions to the Stellar language (mmiklavc) closes apache/incubator-metron#312


Project: http://git-wip-us.apache.org/repos/asf/incubator-metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-metron/commit/61351aab
Tree: http://git-wip-us.apache.org/repos/asf/incubator-metron/tree/61351aab
Diff: http://git-wip-us.apache.org/repos/asf/incubator-metron/diff/61351aab

Branch: refs/heads/master
Commit: 61351aabd979e014c48dfc7e0e4f290bd093cdfe
Parents: d6d3d45
Author: mmiklavc <mi...@gmail.com>
Authored: Tue Oct 25 14:35:28 2016 -0400
Committer: Michael Miklavcic <mi...@gmail.com>
Committed: Tue Oct 25 14:35:28 2016 -0400

----------------------------------------------------------------------
 metron-platform/metron-common/README.md         | 10 +++
 .../metron/common/dsl/BaseStellarFunction.java  |  4 +-
 .../common/dsl/functions/SystemFunctions.java   | 87 ++++++++++++++++++++
 .../metron/common/system/Environment.java       | 27 ++++++
 .../dsl/functions/SystemFunctionsTest.java      | 78 ++++++++++++++++++
 5 files changed, 205 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/61351aab/metron-platform/metron-common/README.md
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/README.md b/metron-platform/metron-common/README.md
index 42354d7..536449d 100644
--- a/metron-platform/metron-common/README.md
+++ b/metron-platform/metron-common/README.md
@@ -318,6 +318,16 @@ The following functions are supported:
   * Input:
     * stats - The Stellar statistics object.
   * Returns: The variance of the values in the window or NaN if the statistics object is null.
+  `SYSTEM_ENV_GET`
+  * Description: Returns the value associated with an environment variable
+  * Input:
+    * env_var - Environment variable name to get the value for
+  * Returns: String
+  `SYSTEM_PROPERTY_GET`
+  * Description: Returns the value associated with a Java system property
+  * Input:
+    * key - Property to get the value for
+  * Returns: String
 * `TO_DOUBLE`
   * Description: Transforms the first argument to a double precision number
   * Input:

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/61351aab/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/BaseStellarFunction.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/BaseStellarFunction.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/BaseStellarFunction.java
index fa0183c..b62137e 100644
--- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/BaseStellarFunction.java
+++ b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/BaseStellarFunction.java
@@ -18,8 +18,10 @@
 package org.apache.metron.common.dsl;
 
 import java.util.List;
-import java.util.Map;
 
+/**
+ * Functions that do not require initialization can extend this class rather than directly implement StellarFunction
+ */
 public abstract class BaseStellarFunction implements StellarFunction {
   public abstract Object apply(List<Object> args);
 

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/61351aab/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SystemFunctions.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SystemFunctions.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SystemFunctions.java
new file mode 100644
index 0000000..13ca74d
--- /dev/null
+++ b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/SystemFunctions.java
@@ -0,0 +1,87 @@
+/**
+ * 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.common.dsl.functions;
+
+import org.apache.metron.common.dsl.BaseStellarFunction;
+import org.apache.metron.common.dsl.Stellar;
+import org.apache.metron.common.system.Environment;
+
+import java.util.List;
+import java.util.function.Function;
+
+public class SystemFunctions {
+
+  @Stellar(namespace = "SYSTEM",
+          name = "ENV_GET",
+          description = "Returns the value associated with an environment variable",
+          params = {
+                  "env_var - Environment variable name to get the value for"
+          },
+          returns = "String"
+  )
+  public static class EnvGet extends BaseStellarFunction {
+    private Environment env;
+
+    public EnvGet() {
+      this(new Environment());
+    }
+
+    public EnvGet(Environment env) {
+      this.env = env;
+    }
+
+    @Override
+    public Object apply(List<Object> args) {
+      return extractTypeChecked(args, 0, String.class, x -> env.get((String) x.get(0)));
+    }
+  }
+
+  /**
+   * Extract type-checked value from an argument list using the specified type check and extraction function
+   *
+   * @param args Arguments to check
+   * @param i Index of argument to extract
+   * @param clazz Object type to verify
+   * @param extractFunc Function applied to extract the value from args
+   * @return value from args if passes type checks, null otherwise
+   */
+  public static Object extractTypeChecked(List<Object> args, int i, Class clazz, Function<List<Object>, Object> extractFunc) {
+    if (args.size() < i + 1) {
+      return null;
+    } else if (clazz.isInstance(args.get(i))) {
+      return extractFunc.apply(args);
+    } else {
+      return null;
+    }
+  }
+
+  @Stellar(namespace = "SYSTEM",
+          name = "PROPERTY_GET",
+          description = "Returns the value associated with a Java system property",
+          params = {
+                  "key - Property to get the value for"
+          },
+          returns = "String"
+  )
+  public static class PropertyGet extends BaseStellarFunction {
+    @Override
+    public Object apply(List<Object> args) {
+      return extractTypeChecked(args, 0, String.class, x -> System.getProperty((String) args.get(0)));
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/61351aab/metron-platform/metron-common/src/main/java/org/apache/metron/common/system/Environment.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/system/Environment.java b/metron-platform/metron-common/src/main/java/org/apache/metron/common/system/Environment.java
new file mode 100644
index 0000000..6568484
--- /dev/null
+++ b/metron-platform/metron-common/src/main/java/org/apache/metron/common/system/Environment.java
@@ -0,0 +1,27 @@
+/**
+ * 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.common.system;
+
+/**
+ * Useful so we can test mock dependency injection with environment variables
+ */
+public class Environment {
+  public String get(String variable) {
+    return System.getenv().get(variable);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/61351aab/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java
new file mode 100644
index 0000000..67ec325
--- /dev/null
+++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/SystemFunctionsTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.common.dsl.functions;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.metron.common.system.Environment;
+import org.junit.Test;
+
+import java.util.ArrayList;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SystemFunctionsTest {
+
+  @Test
+  public void smoke_test_non_mocked_env() {
+    SystemFunctions.EnvGet envGet = new SystemFunctions.EnvGet();
+    String envVal = (String) envGet.apply(ImmutableList.of("ENV_GET_VAR"));
+    assertThat("Value should not exist", envVal, equalTo(null));
+  }
+
+  @Test
+  public void env_get_returns_value() {
+    Environment env = mock(Environment.class);
+    when(env.get("ENV_GET_VAR")).thenReturn("ENV_GET_VALUE");
+    SystemFunctions.EnvGet envGet = new SystemFunctions.EnvGet(env);
+    String envVal = (String) envGet.apply(ImmutableList.of("ENV_GET_VAR"));
+    assertThat("Value should match", envVal, equalTo("ENV_GET_VALUE"));
+  }
+
+  @Test
+  public void env_get_returns_null_if_key_is_not_string() {
+    SystemFunctions.EnvGet envGet = new SystemFunctions.EnvGet();
+    String envVal = (String) envGet.apply(ImmutableList.of(new ArrayList()));
+    assertThat("Value should be null", envVal, equalTo(null));
+  }
+
+  @Test
+  public void property_get_returns_value() {
+    System.getProperties().put("ENV_GET_VAR", "ENV_GET_VALUE");
+    SystemFunctions.PropertyGet propertyGet = new SystemFunctions.PropertyGet();
+    String propertyVal = (String) propertyGet.apply(ImmutableList.of("ENV_GET_VAR"));
+    assertThat("Value should match", propertyVal, equalTo("ENV_GET_VALUE"));
+  }
+
+  @Test
+  public void property_get_nonexistent_returns_null() {
+    SystemFunctions.PropertyGet propertyGet = new SystemFunctions.PropertyGet();
+    String propertyVal = (String) propertyGet.apply(ImmutableList.of("PROPERTY_MISSING"));
+    assertThat("Value should not exist", propertyVal, equalTo(null));
+  }
+
+  @Test
+  public void property_get_returns_null_if_key_is_not_string() {
+    SystemFunctions.PropertyGet propertyGet = new SystemFunctions.PropertyGet();
+    String propertyVal = (String) propertyGet.apply(ImmutableList.of(new ArrayList()));
+    assertThat("Value should be null", propertyVal, equalTo(null));
+  }
+
+}