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