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