4703331c884108ef7770785e67cc02f12200c885
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / restcore / search / AAIAbstractGroovyShell.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 groovy.lang.GroovyShell;
23 import groovy.transform.TimedInterrupt;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
25 import org.codehaus.groovy.ast.ClassHelper;
26 import org.codehaus.groovy.ast.expr.ClassExpression;
27 import org.codehaus.groovy.ast.expr.PropertyExpression;
28 import org.codehaus.groovy.control.CompilerConfiguration;
29 import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
30 import org.codehaus.groovy.control.customizers.ImportCustomizer;
31 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
32
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.concurrent.TimeUnit;
36
37 public abstract class AAIAbstractGroovyShell {
38
39         protected final GroovyShell shell;
40
41         public AAIAbstractGroovyShell() {
42                 Map<String, Object> parameters = new HashMap<>();
43                 parameters.put("value", 30000);
44                 parameters.put("unit", new PropertyExpression(new ClassExpression(ClassHelper.make(TimeUnit.class)),"MILLISECONDS"));
45
46                 ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class);
47                 ImportCustomizer imports = new ImportCustomizer();
48                 imports.addStaticStars(
49                                 "org.apache.tinkerpop.gremlin.process.traversal.P",
50                                 "org.apache.tinkerpop.gremlin.process.traversal.Order"
51                 );
52                 imports.addImports(
53                                 "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__",
54                                 "org.apache.tinkerpop.gremlin.structure.T",
55                                 "org.apache.tinkerpop.gremlin.process.traversal.P",
56                                 "org.onap.aai.edges.enums.EdgeType",
57                                 "java.util.Map.Entry");
58                 imports.addStarImports("java.util");
59                 CompilerConfiguration config = new CompilerConfiguration();
60                 config.addCompilationCustomizers(custom, imports);
61
62                 this.shell = new GroovyShell(config);
63         }
64
65         /**
66          *
67          * @param engine
68          * @param traversal
69          * @param params
70          * @return result of graph traversal
71          */
72         public abstract String executeTraversal (TransactionalGraphEngine engine, String traversal, Map<String, Object> params);
73
74         /**
75          * @param traversal
76          * @param params
77          * @return result of graph traversal
78          */
79         public abstract GraphTraversal<?, ?> executeTraversal (String traversal, Map<String, Object> params);
80 }