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.Collections;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicInteger;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy;
29 import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.LazyBarrierStrategy;
30 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
31 import org.apache.tinkerpop.gremlin.structure.Graph;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.janusgraph.core.JanusGraph;
34 import org.janusgraph.core.schema.JanusGraphManagement;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.query.builder.*;
37 import org.onap.aai.serialization.db.GraphSingleton;
38 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
39 import org.onap.aai.serialization.engines.query.QueryEngine;
41 public abstract class TransactionalGraphEngine {
43 protected GraphSingleton singleton = null;
44 protected QueryBuilder<Vertex> queryBuilder = null;
45 protected QueryStyle style;
46 protected final Loader loader;
47 protected Graph currentTx = null;
48 protected GraphTraversalSource currentTraversal = null;
49 protected GraphTraversalSource readOnlyTraversal = null;
50 private final Admin admin;
53 * Instantiates a new transactional graph engine.
55 * @param style the style
56 * @param loader the loader
58 public TransactionalGraphEngine(QueryStyle style, Loader loader, GraphSingleton singleton) {
61 this.singleton = singleton;
65 public TransactionalGraphEngine(QueryStyle style, Loader loader) {
73 * Sets the list property.
76 * @param name the name
78 * @return true, if successful
80 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
83 * Gets the list property.
86 * @param name the name
87 * @return the list property
89 public abstract List<Object> getListProperty(Vertex v, String name);
96 private JanusGraph getGraph() {
97 return singleton.getTxGraph();
105 public AtomicInteger getCount() {
106 return singleton.getCount();
110 * Gets the query engine.
112 * @return the query engine
114 public QueryEngine getQueryEngine() {
115 QueryEngine engine = null;
116 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
117 // this.queryEngine = new GremlinQueryEngine(this);
118 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
119 // this.queryEngine = new GremlinQueryEngine(this);
120 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
121 // this.queryEngine = new GremlinPipelineQueryEngine(this);
122 } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
124 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
127 throw new IllegalArgumentException("Query Engine type not recognized");
134 * Gets the query builder.
136 * @return the query builder
138 public QueryBuilder<Vertex> getQueryBuilder() {
139 return getQueryBuilder(this.loader);
142 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
143 return getQueryBuilder(style, this.loader);
146 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
147 return getQueryBuilder(this.style, loader);
150 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, GraphTraversalSource source) {
151 return getQueryBuilder(style, this.loader, source);
154 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
155 GraphTraversalSource source = this.asAdmin().getTraversalSource();
156 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
157 return new GremlinTraversal<>(loader, source);
158 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
159 return new GremlinUnique<>(loader, source);
160 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
161 // return new GremlinPipelineTraversal(loader);
162 } else if (style.equals(QueryStyle.TRAVERSAL)) {
163 return new TraversalQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source);
164 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
165 return new TraversalURIOptimizedQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source);
166 } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
167 throw new IllegalArgumentException("History Traversal needs history traversal source");
168 } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
169 throw new IllegalArgumentException("History Gremlin Traversal needs history traversal source");
171 throw new IllegalArgumentException("Query Builder type not recognized");
176 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source) {
177 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
178 return new GremlinTraversal<>(loader, source);
179 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
180 return new GremlinUnique<>(loader, source);
181 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
182 // return new GremlinPipelineTraversal(loader);
183 } else if (style.equals(QueryStyle.TRAVERSAL)) {
184 return new TraversalQuery<>(loader, source);
185 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
186 return new TraversalURIOptimizedQuery<>(loader, source);
187 } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
188 return new HistoryTraversalURIOptimizedQuery<>(loader, source);
189 } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
190 return new HistoryGremlinTraversal<>(loader, source);
192 throw new IllegalArgumentException("Query Builder type not recognized");
198 * Gets the query builder.
200 * @param start the start
201 * @return the query builder
203 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
204 return getQueryBuilder(this.loader, start);
207 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
208 return getQueryBuilder(this.style, loader, start);
211 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
212 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
213 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
214 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
215 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
216 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
217 // return new GremlinPipelineTraversal(loader,start);
218 } else if (style.equals(QueryStyle.TRAVERSAL)) {
219 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
220 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
221 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
223 throw new IllegalArgumentException("Query Builder type not recognized");
228 public Graph startTransaction() {
229 if (this.tx() == null) {
230 this.currentTx = this.getGraph().newTransaction();
231 this.currentTraversal = this.tx().traversal();
232 this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());
237 public void rollback() {
238 if (this.tx() != null) {
239 this.tx().tx().rollback();
240 this.currentTx = null;
241 this.currentTraversal = null;
242 this.readOnlyTraversal = null;
246 public void commit() {
247 if (this.tx() != null) {
248 this.tx().tx().commit();
249 this.currentTx = null;
250 this.currentTraversal = null;
251 this.readOnlyTraversal = null;
256 return this.currentTx;
259 public Admin asAdmin() {
265 public GraphTraversalSource getTraversalSource() {
266 return currentTraversal;
269 public GraphTraversalSource getReadOnlyTraversalSource() {
270 return readOnlyTraversal;
273 public JanusGraphManagement getManagementSystem() {
274 return getGraph().openManagement();