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.serialization.engines;
23 import java.util.List;
24 import java.util.concurrent.atomic.AtomicInteger;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.janusgraph.core.JanusGraph;
31 import org.janusgraph.core.schema.JanusGraphManagement;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.query.builder.*;
34 import org.onap.aai.serialization.db.GraphSingleton;
35 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
36 import org.onap.aai.serialization.engines.query.QueryEngine;
38 public abstract class TransactionalGraphEngine {
40 protected GraphSingleton singleton = null;
41 protected QueryBuilder<Vertex> queryBuilder = null;
42 protected QueryStyle style;
43 protected final Loader loader;
44 protected Graph currentTx = null;
45 protected GraphTraversalSource currentTraversal = null;
46 protected GraphTraversalSource readOnlyTraversal = null;
47 private final Admin admin;
50 * Instantiates a new transactional graph engine.
52 * @param style the style
53 * @param loader the loader
55 public TransactionalGraphEngine(QueryStyle style, Loader loader, GraphSingleton singleton) {
58 this.singleton = singleton;
62 public TransactionalGraphEngine(QueryStyle style, Loader loader) {
70 * Sets the list property.
73 * @param name the name
75 * @return true, if successful
77 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
80 * Gets the list property.
83 * @param name the name
84 * @return the list property
86 public abstract List<Object> getListProperty(Vertex v, String name);
93 private JanusGraph getGraph() {
94 return singleton.getTxGraph();
102 public AtomicInteger getCount() {
103 return singleton.getCount();
107 * Gets the query engine.
109 * @return the query engine
111 public QueryEngine getQueryEngine() {
112 QueryEngine engine = null;
113 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
114 // this.queryEngine = new GremlinQueryEngine(this);
115 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
116 // this.queryEngine = new GremlinQueryEngine(this);
117 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
118 // this.queryEngine = new GremlinPipelineQueryEngine(this);
119 } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
121 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
124 throw new IllegalArgumentException("Query Engine type not recognized");
131 * Gets the query builder.
133 * @return the query builder
135 public QueryBuilder<Vertex> getQueryBuilder() {
136 return getQueryBuilder(this.loader);
139 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
140 return getQueryBuilder(style, this.loader);
143 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
144 return getQueryBuilder(this.style, loader);
147 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, GraphTraversalSource source) {
148 return getQueryBuilder(style, this.loader, source);
151 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
152 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
153 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
154 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
155 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
156 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
157 // return new GremlinPipelineTraversal(loader);
158 } else if (style.equals(QueryStyle.TRAVERSAL)) {
159 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
160 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
161 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource());
162 } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
163 throw new IllegalArgumentException("History Traversal needs history traversal source");
164 } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
165 throw new IllegalArgumentException("History Gremlin Traversal needs history traversal source");
167 throw new IllegalArgumentException("Query Builder type not recognized");
172 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source) {
173 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
174 return new GremlinTraversal<>(loader, source);
175 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
176 return new GremlinUnique<>(loader, source);
177 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
178 // return new GremlinPipelineTraversal(loader);
179 } else if (style.equals(QueryStyle.TRAVERSAL)) {
180 return new TraversalQuery<>(loader, source);
181 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
182 return new TraversalURIOptimizedQuery<>(loader, source);
183 } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
184 return new HistoryTraversalURIOptimizedQuery<>(loader, source);
185 } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
186 return new HistoryGremlinTraversal<>(loader, source);
188 throw new IllegalArgumentException("Query Builder type not recognized");
194 * Gets the query builder.
196 * @param start the start
197 * @return the query builder
199 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
200 return getQueryBuilder(this.loader, start);
203 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
204 return getQueryBuilder(this.style, loader, start);
207 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
208 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
209 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
210 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
211 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
212 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
213 // return new GremlinPipelineTraversal(loader,start);
214 } else if (style.equals(QueryStyle.TRAVERSAL)) {
215 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
216 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
217 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
219 throw new IllegalArgumentException("Query Builder type not recognized");
224 public Graph startTransaction() {
225 if (this.tx() == null) {
226 this.currentTx = this.getGraph().newTransaction();
227 this.currentTraversal = this.tx().traversal();
228 this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());
233 public void rollback() {
234 if (this.tx() != null) {
235 this.tx().tx().rollback();
236 this.currentTx = null;
237 this.currentTraversal = null;
238 this.readOnlyTraversal = null;
242 public void commit() {
243 if (this.tx() != null) {
244 this.tx().tx().commit();
245 this.currentTx = null;
246 this.currentTraversal = null;
247 this.readOnlyTraversal = null;
252 return this.currentTx;
255 public Admin asAdmin() {
261 public GraphTraversalSource getTraversalSource() {
262 return currentTraversal;
265 public GraphTraversalSource getReadOnlyTraversalSource() {
266 return readOnlyTraversal;
269 public JanusGraphManagement getManagementSystem() {
270 return getGraph().openManagement();