Merge "Added docs directory and index file for traversal"
[aai/traversal.git] / aai-traversal / src / main / java / org / openecomp / aai / rest / search / GroovyQueryBuilderSingleton.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.rest.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.structure.Vertex;
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 import org.openecomp.aai.query.builder.QueryBuilder;
35 import org.openecomp.aai.serialization.engines.QueryStyle;
36 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
37
38 import groovy.lang.Binding;
39 import groovy.lang.GroovyShell;
40 import groovy.lang.Script;
41 import groovy.transform.TimedInterrupt;
42
43 /**
44  * Creates and returns a groovy shell with the
45  * configuration to statically import graph classes
46  *
47  */
48 public class GroovyQueryBuilderSingleton {
49
50         private final GroovyShell shell;
51         private GroovyQueryBuilderSingleton() {
52                 Map<String, Object> parameters = new HashMap<>();
53                 parameters.put("value", 30000);
54                 parameters.put("unit", new PropertyExpression(new ClassExpression(ClassHelper.make(TimeUnit.class)),"MILLISECONDS"));
55
56                 ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class);
57                 ImportCustomizer imports = new ImportCustomizer();
58                 imports.addStaticStars(
59             "org.apache.tinkerpop.gremlin.process.traversal.P"
60                 );
61                 imports.addImports(
62                                 "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__",
63                                 "org.apache.tinkerpop.gremlin.structure.T",
64                                 "org.apache.tinkerpop.gremlin.process.traversal.P",
65                                 "org.openecomp.aai.serialization.db.EdgeType");
66                 CompilerConfiguration config = new CompilerConfiguration();
67                 config.addCompilationCustomizers(custom, imports);
68
69                 this.shell = new GroovyShell(config);
70         }
71         
72          private static class Helper {
73                  private static final GroovyQueryBuilderSingleton INSTANCE = new GroovyQueryBuilderSingleton();
74          }
75
76          public static GroovyQueryBuilderSingleton getInstance() {
77                  
78                  return Helper.INSTANCE;
79          }
80
81         /** 
82          * @param traversal
83          * @param params
84          * @return result of graph traversal
85          */
86         public String executeTraversal (TransactionalGraphEngine engine, String traversal, Map<String, Object> params) {
87                 QueryBuilder<Vertex> builder = engine.getQueryBuilder(QueryStyle.GREMLIN_TRAVERSAL);
88                 Binding binding = new Binding(params);
89                 binding.setVariable("builder", builder);
90                 Script script = shell.parse(traversal);
91                 script.setBinding(binding);
92                 script.run();
93                 
94                 return builder.getQuery();
95         }
96 }