Update the license for 2017-2018 license
[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-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
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 package org.onap.aai.restcore.search;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.concurrent.TimeUnit;
25
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;
33
34 import groovy.lang.Binding;
35 import groovy.lang.GroovyShell;
36 import groovy.lang.Script;
37 import groovy.transform.TimedInterrupt;
38
39 /**
40  * Creates and returns a groovy shell with the
41  * configuration to statically import graph classes
42  *
43  */
44 public class GremlinGroovyShellSingleton {
45
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"));
51
52                 ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class);
53                 ImportCustomizer imports = new ImportCustomizer();
54                 imports.addStaticStars(
55             "org.apache.tinkerpop.gremlin.process.traversal.P"
56                 );
57                 imports.addImports(
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);
65
66                 this.shell = new GroovyShell(config);
67         }
68         
69          private static class Helper {
70                  private static final GremlinGroovyShellSingleton INSTANCE = new GremlinGroovyShellSingleton();
71          }
72
73          public static GremlinGroovyShellSingleton getInstance() {
74                  
75                  return Helper.INSTANCE;
76          }
77
78         /** 
79          * @param traversal
80          * @param params
81          * @return result of graph traversal
82          */
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();
88         }
89 }