2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.restcore.search;
23 import groovy.lang.GroovyShell;
24 import groovy.transform.TimedInterrupt;
26 import java.util.HashMap;
28 import java.util.concurrent.TimeUnit;
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;
46 public abstract class AAIAbstractGroovyShell {
48 protected final GroovyShell shell;
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"));
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);
67 this.shell = new GroovyShell(config);
75 * @return result of graph traversal
77 public abstract String executeTraversal(TransactionalGraphEngine engine, String traversal,
78 Map<String, Object> params);
83 * @return result of graph traversal
85 public abstract GraphTraversal<?, ?> executeTraversal(String traversal, Map<String, Object> params);
92 * @return result of graph traversal
94 public abstract String executeTraversal(TransactionalGraphEngine engine, String traversal,
95 Map<String, Object> params, QueryStyle style, GraphTraversalSource source);
97 protected Loader getLoader() {
98 SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
99 return SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY,
100 schemaVersions.getDefaultVersion());