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