2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2024 DEUTSCHE TELEKOM AG.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.serialization.engines;
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicInteger;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
29 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
31 import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy;
32 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.janusgraph.core.JanusGraph;
36 import org.janusgraph.core.schema.JanusGraphManagement;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.query.builder.*;
39 import org.onap.aai.serialization.db.GraphSingleton;
40 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
41 import org.onap.aai.serialization.engines.query.QueryEngine;
43 public abstract class TransactionalGraphEngine {
45 protected GraphSingleton singleton = null;
46 protected QueryBuilder<Vertex> queryBuilder = null;
47 protected QueryStyle style;
48 protected final Loader loader;
49 protected Graph currentTx = null;
50 protected GraphTraversalSource currentTraversal = null;
51 protected GraphTraversalSource readOnlyTraversal = null;
52 private final Admin admin;
55 * Instantiates a new transactional graph engine.
57 * @param style the style
58 * @param loader the loader
60 public TransactionalGraphEngine(QueryStyle style, Loader loader, GraphSingleton singleton) {
63 this.singleton = singleton;
67 public TransactionalGraphEngine(QueryStyle style, Loader loader) {
75 * Sets the list property.
78 * @param name the name
80 * @return true, if successful
82 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
85 * Gets the list property.
88 * @param name the name
89 * @return the list property
91 public abstract List<Object> getListProperty(Vertex v, String name);
98 private JanusGraph getGraph() {
99 return singleton.getTxGraph();
107 public AtomicInteger getCount() {
108 return singleton.getCount();
112 * Gets the query engine.
114 * @return the query engine
116 public QueryEngine getQueryEngine() {
117 QueryEngine engine = null;
118 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
119 // this.queryEngine = new GremlinQueryEngine(this);
120 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
121 // this.queryEngine = new GremlinQueryEngine(this);
122 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
123 // this.queryEngine = new GremlinPipelineQueryEngine(this);
124 } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
126 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
129 throw new IllegalArgumentException("Query Engine type not recognized");
136 * Gets the query builder.
138 * @return the query builder
140 public QueryBuilder<Vertex> getQueryBuilder() {
141 return getQueryBuilder(this.loader);
144 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
145 return getQueryBuilder(style, this.loader);
148 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
149 return getQueryBuilder(this.style, loader);
152 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, GraphTraversalSource source) {
153 return getQueryBuilder(style, this.loader, source);
156 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
157 GraphTraversalSource source = this.asAdmin().getTraversalSource();
158 return this.getQueryBuilder(style, loader, source);
161 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source) {
162 return this.getQueryBuilder(style, loader, source, (GraphTraversal<Vertex, Vertex>) __.<Vertex>start());
165 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source, GraphTraversal<Vertex, Vertex> traversal) {
166 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
167 return new GremlinTraversal<>(loader, source);
168 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
169 return new GremlinUnique<>(loader, source);
170 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
171 // return new GremlinPipelineTraversal(loader);
172 } else if (style.equals(QueryStyle.TRAVERSAL)) {
173 return new TraversalQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source, traversal);
174 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
175 return new TraversalURIOptimizedQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source);
176 } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
177 return new HistoryTraversalURIOptimizedQuery<>(loader, source);
178 } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
179 return new HistoryGremlinTraversal<>(loader, source);
181 throw new IllegalArgumentException("Query Builder type not recognized");
187 * Gets the query builder.
189 * @param start the start
190 * @return the query builder
192 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
193 return getQueryBuilder(this.loader, start);
196 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
197 return getQueryBuilder(this.style, loader, start);
200 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
201 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
202 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
203 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
204 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
205 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
206 // return new GremlinPipelineTraversal(loader,start);
207 } else if (style.equals(QueryStyle.TRAVERSAL)) {
208 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
209 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
210 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
212 throw new IllegalArgumentException("Query Builder type not recognized");
217 public Graph startTransaction() {
218 if (this.tx() == null) {
219 this.currentTx = this.getGraph().newTransaction();
220 this.currentTraversal = this.tx().traversal();
221 this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());
226 public void rollback() {
227 if (this.tx() != null) {
228 this.tx().tx().rollback();
229 this.currentTx = null;
230 this.currentTraversal = null;
231 this.readOnlyTraversal = null;
235 public void commit() {
236 if (this.tx() != null) {
237 this.tx().tx().commit();
238 this.currentTx = null;
239 this.currentTraversal = null;
240 this.readOnlyTraversal = null;
245 return this.currentTx;
248 public Admin asAdmin() {
254 public GraphTraversalSource getTraversalSource() {
255 return currentTraversal;
258 public GraphTraversalSource getReadOnlyTraversalSource() {
259 return readOnlyTraversal;
262 public JanusGraphManagement getManagementSystem() {
263 return getGraph().openManagement();