Search in sources :

Example 1 with StellarFunctionInfo

use of org.apache.metron.stellar.dsl.StellarFunctionInfo in project metron by apache.

the class StellarServiceImpl method getStellarFunctions.

@Override
public List<StellarFunctionDescription> getStellarFunctions() {
    List<StellarFunctionDescription> stellarFunctionDescriptions = new ArrayList<>();
    Iterable<StellarFunctionInfo> stellarFunctionsInfo = StellarFunctions.FUNCTION_RESOLVER().getFunctionInfo();
    stellarFunctionsInfo.forEach(stellarFunctionInfo -> {
        stellarFunctionDescriptions.add(new StellarFunctionDescription(stellarFunctionInfo.getName(), stellarFunctionInfo.getDescription(), stellarFunctionInfo.getParams(), stellarFunctionInfo.getReturns()));
    });
    return stellarFunctionDescriptions;
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) ArrayList(java.util.ArrayList) StellarFunctionDescription(org.apache.metron.rest.model.StellarFunctionDescription)

Example 2 with StellarFunctionInfo

use of org.apache.metron.stellar.dsl.StellarFunctionInfo in project metron by apache.

the class BaseFunctionResolver method resolveFunctions.

/**
 * Performs the core process of function resolution.
 */
protected Map<String, StellarFunctionInfo> resolveFunctions() {
    // maps a function name to its definition
    Map<String, StellarFunctionInfo> functions = new HashMap<>();
    for (Class<? extends StellarFunction> clazz : resolvables()) {
        StellarFunctionInfo fn = resolveFunction(clazz);
        if (fn != null) {
            // check for duplicate function names
            StellarFunctionInfo fnSameName = functions.get(fn.getName());
            if (fnSameName != null && ObjectUtils.notEqual(fnSameName, fn)) {
                LOG.warn("Namespace conflict: duplicate function names; `{}` implemented by [{}, {}]", fn.getName(), fnSameName.getFunction(), fn.getFunction());
            }
            functions.put(fn.getName(), fn);
        }
    }
    return functions;
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) HashMap(java.util.HashMap)

Example 3 with StellarFunctionInfo

use of org.apache.metron.stellar.dsl.StellarFunctionInfo in project metron by apache.

the class DocumentationGenerator method main.

public static void main(String... argv) {
    List<StellarFunctionInfo> functions = Lists.newArrayList(SingletonFunctionResolver.getInstance().getFunctionInfo());
    Collections.sort(functions, (o1, o2) -> o1.getName().compareTo(o2.getName()));
    for (StellarFunctionInfo info : functions) {
        System.out.println("### `" + info.getName() + "`");
        System.out.println("  * Description: " + info.getDescription());
        System.out.println("  * Input:");
        for (String param : info.getParams()) {
            System.out.println("    * " + param);
        }
        System.out.println("  * Returns: " + info.getReturns());
        System.out.println("");
    }
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo)

Example 4 with StellarFunctionInfo

use of org.apache.metron.stellar.dsl.StellarFunctionInfo in project metron by apache.

the class DocCommand method execute.

@Override
public StellarResult execute(String command, StellarShellExecutor executor) {
    StellarResult result;
    // expect ?functionName
    String functionName = StringUtils.substring(command, 1);
    // grab any docs for the given function
    Spliterator<StellarFunctionInfo> fnIterator = executor.getFunctionResolver().getFunctionInfo().spliterator();
    Optional<StellarFunctionInfo> functionInfo = StreamSupport.stream(fnIterator, false).filter(info -> StringUtils.equals(functionName, info.getName())).findFirst();
    if (functionInfo.isPresent()) {
        result = success(docFormat(functionInfo.get()));
    } else {
        result = error(String.format("No docs available for function '%s'", functionName));
    }
    return result;
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) StellarResult.error(org.apache.metron.stellar.common.shell.StellarResult.error) StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Optional(java.util.Optional) StreamSupport(java.util.stream.StreamSupport) StringUtils(org.apache.commons.lang3.StringUtils) Spliterator(java.util.Spliterator) Function(java.util.function.Function) StellarResult.success(org.apache.metron.stellar.common.shell.StellarResult.success) StellarShellExecutor(org.apache.metron.stellar.common.shell.StellarShellExecutor) StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) StellarResult(org.apache.metron.stellar.common.shell.StellarResult)

Example 5 with StellarFunctionInfo

use of org.apache.metron.stellar.dsl.StellarFunctionInfo in project metron by apache.

the class BaseFunctionResolver method resolveFunction.

/**
 * Resolves a Stellar function from a given class.
 * @param clazz The class.
 */
public static StellarFunctionInfo resolveFunction(Class<? extends StellarFunction> clazz) {
    StellarFunctionInfo info = null;
    // the class must be annotated
    if (clazz.isAnnotationPresent(Stellar.class)) {
        Stellar annotation = clazz.getAnnotation(Stellar.class);
        String fullyQualifiedName = getNameFromAnnotation(annotation);
        StellarFunction function = createFunction(clazz);
        if (fullyQualifiedName != null && function != null) {
            info = new StellarFunctionInfo(annotation.description(), fullyQualifiedName, annotation.params(), annotation.returns(), function);
        }
    }
    return info;
}
Also used : StellarFunctionInfo(org.apache.metron.stellar.dsl.StellarFunctionInfo) StellarFunction(org.apache.metron.stellar.dsl.StellarFunction) Stellar(org.apache.metron.stellar.dsl.Stellar)

Aggregations

StellarFunctionInfo (org.apache.metron.stellar.dsl.StellarFunctionInfo)5 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 Spliterator (java.util.Spliterator)1 Function (java.util.function.Function)1 StreamSupport (java.util.stream.StreamSupport)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StellarFunctionDescription (org.apache.metron.rest.model.StellarFunctionDescription)1 StellarResult (org.apache.metron.stellar.common.shell.StellarResult)1 StellarResult.error (org.apache.metron.stellar.common.shell.StellarResult.error)1 StellarResult.success (org.apache.metron.stellar.common.shell.StellarResult.success)1 StellarShellExecutor (org.apache.metron.stellar.common.shell.StellarShellExecutor)1 Stellar (org.apache.metron.stellar.dsl.Stellar)1 StellarFunction (org.apache.metron.stellar.dsl.StellarFunction)1