Enhancements for the aai-common library
[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.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.codehaus.groovy.ast.ClassHelper;
33 import org.codehaus.groovy.ast.expr.ClassExpression;
34 import org.codehaus.groovy.ast.expr.PropertyExpression;
35 import org.codehaus.groovy.control.CompilerConfiguration;
36 import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
37 import org.codehaus.groovy.control.customizers.ImportCustomizer;
38 import org.onap.aai.config.SpringContextAware;
39 import org.onap.aai.introspection.Loader;
40 import org.onap.aai.introspection.LoaderFactory;
41 import org.onap.aai.introspection.ModelType;
42 import org.onap.aai.serialization.engines.QueryStyle;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.onap.aai.setup.SchemaVersions;
45
46 public abstract class AAIAbstractGroovyShell {
47
48     protected final GroovyShell shell;
49
50     public AAIAbstractGroovyShell() {
51         Map<String, Object> parameters = new HashMap<>();
52         parameters.put("value", 30000);
53         parameters.put("unit",
54                 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("org.apache.tinkerpop.gremlin.process.traversal.P",
59                 "org.apache.tinkerpop.gremlin.process.traversal.Order");
60         imports.addImports("org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__",
61                 "org.apache.tinkerpop.gremlin.structure.T", "org.apache.tinkerpop.gremlin.process.traversal.P",
62                 "org.onap.aai.edges.enums.EdgeType", "java.util.Map.Entry");
63         imports.addStarImports("java.util");
64         CompilerConfiguration config = new CompilerConfiguration();
65         config.addCompilationCustomizers(custom, imports);
66
67         this.shell = new GroovyShell(config);
68     }
69
70     /**
71      *
72      * @param engine
73      * @param traversal
74      * @param params
75      * @return result of graph traversal
76      */
77     public abstract String executeTraversal(TransactionalGraphEngine engine, String traversal,
78             Map<String, Object> params);
79
80     /**
81      * @param traversal
82      * @param params
83      * @return result of graph traversal
84      */
85     public abstract GraphTraversal<?, ?> executeTraversal(String traversal, Map<String, Object> params);
86
87     /**
88      *
89      * @param engine
90      * @param traversal
91      * @param params
92      * @return result of graph traversal
93      */
94     public abstract String executeTraversal(TransactionalGraphEngine engine, String traversal,
95                                             Map<String, Object> params, QueryStyle style, GraphTraversalSource source);
96
97     protected Loader getLoader(){
98         SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
99         return SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY,
100             schemaVersions.getDefaultVersion());
101     }
102 }