Github user cestella commented on a diff in the pull request:
https://github.com/apache/incubator-metron/pull/316#discussion_r91817285
--- Diff:
metron-interface/metron-rest/src/main/java/org/apache/metron/rest/service/TransformationService.java
---
@@ -0,0 +1,111 @@
+/**
+ * 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.rest.service;
+
+import org.apache.metron.common.dsl.Context;
+import org.apache.metron.common.dsl.ParseException;
+import org.apache.metron.common.dsl.StellarFunctionInfo;
+import
org.apache.metron.common.dsl.functions.resolver.SingletonFunctionResolver;
+import org.apache.metron.common.field.transformation.FieldTransformations;
+import org.apache.metron.common.stellar.StellarProcessor;
+import org.apache.metron.rest.model.StellarFunctionDescription;
+import org.apache.metron.rest.model.TransformationValidation;
+import org.json.simple.JSONObject;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Service
+public class TransformationService {
+
+ public Map<String, Boolean> validateRules(List<String> rules) {
+ Map<String, Boolean> results = new HashMap<>();
+ StellarProcessor stellarProcessor = new StellarProcessor();
+ for(String rule: rules) {
+ try {
+ boolean result = stellarProcessor.validate(rule,
Context.EMPTY_CONTEXT());
+ results.put(rule, result);
+ } catch (ParseException e) {
+ results.put(rule, false);
+ }
+ }
+ return results;
+ }
+
+ public Map<String, Object>
validateTransformation(TransformationValidation transformationValidation) {
+ JSONObject sampleJson = new
JSONObject(transformationValidation.getSampleData());
+
transformationValidation.getSensorParserConfig().getFieldTransformations().forEach(fieldTransformer
-> {
+ fieldTransformer.transformAndUpdate(sampleJson,
transformationValidation.getSensorParserConfig().getParserConfig(),
Context.EMPTY_CONTEXT());
+ }
+ );
+ return sampleJson;
+ }
+
+ public FieldTransformations[] getTransformations() {
+ return FieldTransformations.values();
+ }
+
+ public List<StellarFunctionDescription> getStellarFunctions() {
+ List<StellarFunctionDescription> stellarFunctionDescriptions = new
ArrayList<>();
+ Iterable<StellarFunctionInfo> stellarFunctionsInfo =
SingletonFunctionResolver.getInstance().getFunctionInfo();
+ stellarFunctionsInfo.forEach(stellarFunctionInfo -> {
+ stellarFunctionDescriptions.add(new StellarFunctionDescription(
+ stellarFunctionInfo.getName(),
+ stellarFunctionInfo.getDescription(),
+ stellarFunctionInfo.getParams(),
+ stellarFunctionInfo.getReturns()));
+ });
+ return stellarFunctionDescriptions;
+ }
+
+ private List<String> simpleFunctionNames = Arrays.asList(
--- End diff --
Can you pull these from the classpath based on the Stellar annotation? I
presume "simple" in this case are stellar functions with one argument (that
might be a relevant comment in the code actually). You should be able to get
the list of stellar functions via reflection and then get the number of params
to filter them out.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---