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