2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.restcore.search;
22 import java.util.HashMap;
24 import java.util.concurrent.TimeUnit;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
27 import org.codehaus.groovy.ast.ClassHelper;
28 import org.codehaus.groovy.ast.expr.ClassExpression;
29 import org.codehaus.groovy.ast.expr.PropertyExpression;
30 import org.codehaus.groovy.control.CompilerConfiguration;
31 import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
32 import org.codehaus.groovy.control.customizers.ImportCustomizer;
34 import groovy.lang.Binding;
35 import groovy.lang.GroovyShell;
36 import groovy.lang.Script;
37 import groovy.transform.TimedInterrupt;
40 * Creates and returns a groovy shell with the
41 * configuration to statically import graph classes
44 public class GremlinGroovyShellSingleton {
46 private final GroovyShell shell;
47 private GremlinGroovyShellSingleton() {
48 Map<String, Object> parameters = new HashMap<>();
49 parameters.put("value", 30000);
50 parameters.put("unit", new PropertyExpression(new ClassExpression(ClassHelper.make(TimeUnit.class)),"MILLISECONDS"));
52 ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class);
53 ImportCustomizer imports = new ImportCustomizer();
54 imports.addStaticStars(
55 "org.apache.tinkerpop.gremlin.process.traversal.P"
58 "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__",
59 "org.apache.tinkerpop.gremlin.structure.T",
60 "org.apache.tinkerpop.gremlin.process.traversal.P",
61 "java.util.Map.Entry");
62 imports.addStarImports("java.util");
63 CompilerConfiguration config = new CompilerConfiguration();
64 config.addCompilationCustomizers(custom, imports);
66 this.shell = new GroovyShell(config);
69 private static class Helper {
70 private static final GremlinGroovyShellSingleton INSTANCE = new GremlinGroovyShellSingleton();
73 public static GremlinGroovyShellSingleton getInstance() {
75 return Helper.INSTANCE;
81 * @return result of graph traversal
83 public GraphTraversal<?, ?> executeTraversal (String traversal, Map<String, Object> params) {
84 Binding binding = new Binding(params);
85 Script script = shell.parse(traversal);
86 script.setBinding(binding);
87 return (GraphTraversal<?, ?>) script.run();