7eb600d2ecbc4a8aacf3bda9e15ba4be241c7d2a
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / search / GremlinGroovyShellSingleton.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.restcore.search;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.TimeUnit;
27
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
29 import org.codehaus.groovy.ast.ClassHelper;
30 import org.codehaus.groovy.ast.expr.ClassExpression;
31 import org.codehaus.groovy.ast.expr.PropertyExpression;
32 import org.codehaus.groovy.control.CompilerConfiguration;
33 import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
34 import org.codehaus.groovy.control.customizers.ImportCustomizer;
35
36 import groovy.lang.Binding;
37 import groovy.lang.GroovyShell;
38 import groovy.lang.Script;
39 import groovy.transform.TimedInterrupt;
40
41 /**
42  * Creates and returns a groovy shell with the
43  * configuration to statically import graph classes
44  *
45  */
46 public class GremlinGroovyShellSingleton {
47
48         private final GroovyShell shell;
49         private GremlinGroovyShellSingleton() {
50                 Map<String, Object> parameters = new HashMap<>();
51                 parameters.put("value", 30000);
52                 parameters.put("unit", new PropertyExpression(new ClassExpression(ClassHelper.make(TimeUnit.class)),"MILLISECONDS"));
53
54                 ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class);
55                 ImportCustomizer imports = new ImportCustomizer();
56                 imports.addStaticStars(
57             "org.apache.tinkerpop.gremlin.process.traversal.P"
58                 );
59                 imports.addImports(
60                                 "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__",
61                                 "org.apache.tinkerpop.gremlin.structure.T",
62                                 "org.apache.tinkerpop.gremlin.process.traversal.P",
63                                 "java.util.Map.Entry");
64                 imports.addStarImports("java.util");
65                 CompilerConfiguration config = new CompilerConfiguration();
66                 config.addCompilationCustomizers(custom, imports);
67
68                 this.shell = new GroovyShell(config);
69         }
70         
71          private static class Helper {
72                  private static final GremlinGroovyShellSingleton INSTANCE = new GremlinGroovyShellSingleton();
73          }
74
75          public static GremlinGroovyShellSingleton getInstance() {
76                  
77                  return Helper.INSTANCE;
78          }
79
80         /** 
81          * @param traversal
82          * @param params
83          * @return result of graph traversal
84          */
85         public GraphTraversal<?, ?> executeTraversal (String traversal, Map<String, Object> params) {
86                 Binding binding = new Binding(params);
87                 Script script = shell.parse(traversal);
88                 script.setBinding(binding);
89                 return (GraphTraversal<?, ?>) script.run();
90         }
91 }