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.dbmap.DBConnectionType;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.query.builder.*;
35 import org.onap.aai.serialization.db.GraphSingleton;
36 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
37 import org.onap.aai.serialization.engines.query.QueryEngine;
39 public abstract class TransactionalGraphEngine {
41 protected GraphSingleton singleton = null;
42 protected QueryEngine queryEngine = null;
43 protected QueryBuilder<Vertex> queryBuilder = null;
44 protected QueryStyle style = null;
45 protected final DBConnectionType connectionType;
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, DBConnectionType connectionType,
59 GraphSingleton singleton) {
62 this.singleton = singleton;
63 this.connectionType = connectionType;
67 public TransactionalGraphEngine(QueryStyle style, Loader loader) {
70 this.connectionType = DBConnectionType.REALTIME;
76 * Sets the list property.
79 * @param name the name
81 * @return true, if successful
83 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
86 * Gets the list property.
89 * @param name the name
90 * @return the list property
92 public abstract List<Object> getListProperty(Vertex v, String name);
99 private JanusGraph getGraph() {
100 return singleton.getTxGraph(this.connectionType);
108 public AtomicInteger getCount() {
109 return singleton.getCount();
113 * Gets the query engine.
115 * @return the query engine
117 public QueryEngine getQueryEngine() {
118 QueryEngine engine = null;
119 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
120 // this.queryEngine = new GremlinQueryEngine(this);
121 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
122 // this.queryEngine = new GremlinQueryEngine(this);
123 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
124 // this.queryEngine = new GremlinPipelineQueryEngine(this);
125 } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
127 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
130 throw new IllegalArgumentException("Query Engine type not recognized");
137 * Gets the query builder.
139 * @return the query builder
141 public QueryBuilder<Vertex> getQueryBuilder() {
142 return getQueryBuilder(this.loader);
145 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
146 return getQueryBuilder(style, this.loader);
149 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
150 return getQueryBuilder(this.style, loader);
153 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
154 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
155 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
156 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
157 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
158 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
159 // return new GremlinPipelineTraversal(loader);
160 } else if (style.equals(QueryStyle.TRAVERSAL)) {
161 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
162 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
163 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource());
165 throw new IllegalArgumentException("Query Builder type not recognized");
171 * Gets the query builder.
173 * @param start the start
174 * @return the query builder
176 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
177 return getQueryBuilder(this.loader, start);
180 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
181 return getQueryBuilder(this.style, loader, start);
184 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
185 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
186 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
187 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
188 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
189 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
190 // return new GremlinPipelineTraversal(loader,start);
191 } else if (style.equals(QueryStyle.TRAVERSAL)) {
192 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
193 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
194 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
196 throw new IllegalArgumentException("Query Builder type not recognized");
201 public Graph startTransaction() {
202 if (this.tx() == null) {
203 this.currentTx = this.getGraph().newTransaction();
204 this.currentTraversal = this.tx().traversal();
205 this.readOnlyTraversal =
206 this.tx().traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
211 public void rollback() {
212 if (this.tx() != null) {
213 this.tx().tx().rollback();
214 this.currentTx = null;
215 this.currentTraversal = null;
216 this.readOnlyTraversal = null;
220 public void commit() {
221 if (this.tx() != null) {
222 this.tx().tx().commit();
223 this.currentTx = null;
224 this.currentTraversal = null;
225 this.readOnlyTraversal = null;
230 return this.currentTx;
233 public Admin asAdmin() {
239 public GraphTraversalSource getTraversalSource() {
240 return currentTraversal;
243 public GraphTraversalSource getReadOnlyTraversalSource() {
244 return readOnlyTraversal;
247 public JanusGraphManagement getManagementSystem() {
248 return getGraph().openManagement();