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=========================================================
20 package org.onap.aai.serialization.engines;
22 import org.janusgraph.core.JanusGraph;
23 import org.janusgraph.core.schema.JanusGraphManagement;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.onap.aai.dbmap.DBConnectionType;
29 import org.onap.aai.introspection.Loader;
30 import org.onap.aai.query.builder.*;
31 import org.onap.aai.serialization.db.GraphSingleton;
32 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
33 import org.onap.aai.serialization.engines.query.QueryEngine;
35 import java.util.List;
36 import java.util.concurrent.atomic.AtomicInteger;
38 public abstract class TransactionalGraphEngine {
40 protected GraphSingleton singleton = null;
41 protected QueryEngine queryEngine = null;
42 protected QueryBuilder<Vertex> queryBuilder = null;
43 protected QueryStyle style = null;
44 protected final DBConnectionType connectionType;
45 protected final Loader loader;
46 protected Graph currentTx = null;
47 protected GraphTraversalSource currentTraversal = null;
48 protected GraphTraversalSource readOnlyTraversal = null;
49 private final Admin admin;
51 * Instantiates a new transactional graph engine.
53 * @param style the style
54 * @param loader the loader
56 public TransactionalGraphEngine (QueryStyle style, Loader loader, DBConnectionType connectionType, GraphSingleton singleton) {
59 this.singleton = singleton;
60 this.connectionType = connectionType;
64 public TransactionalGraphEngine (QueryStyle style, Loader loader) {
67 this.connectionType = DBConnectionType.REALTIME;
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(this.connectionType);
105 public AtomicInteger getCount() {
106 return singleton.getCount();
111 * Gets the query engine.
113 * @return the query engine
115 public QueryEngine getQueryEngine() {
116 QueryEngine engine = null;
117 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
118 //this.queryEngine = new GremlinQueryEngine(this);
119 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
120 //this.queryEngine = new GremlinQueryEngine(this);
121 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
122 //this.queryEngine = new GremlinPipelineQueryEngine(this);
123 } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
125 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
128 throw new IllegalArgumentException("Query Engine type not recognized");
135 * Gets the query builder.
137 * @return the query builder
139 public QueryBuilder<Vertex> getQueryBuilder() {
140 return getQueryBuilder(this.loader);
143 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
144 return getQueryBuilder(style, this.loader);
147 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
148 return getQueryBuilder(this.style, loader);
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());
163 throw new IllegalArgumentException("Query Builder type not recognized");
168 * Gets the query builder.
170 * @param start the start
171 * @return the query builder
173 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
174 return getQueryBuilder(this.loader, start);
177 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
178 return getQueryBuilder(this.style, loader, start);
181 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
182 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
183 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
184 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
185 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
186 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
187 //return new GremlinPipelineTraversal(loader,start);
188 } else if (style.equals(QueryStyle.TRAVERSAL)) {
189 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
190 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
191 return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
193 throw new IllegalArgumentException("Query Builder type not recognized");
197 public Graph startTransaction() {
198 if (this.tx() == null) {
199 this.currentTx = this.getGraph().newTransaction();
200 this.currentTraversal = this.tx().traversal();
201 this.readOnlyTraversal = this.tx().traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
206 public void rollback() {
207 if (this.tx() != null) {
208 this.tx().tx().rollback();
209 this.currentTx = null;
210 this.currentTraversal = null;
211 this.readOnlyTraversal = null;
214 public void commit() {
215 if (this.tx() != null) {
216 this.tx().tx().commit();
217 this.currentTx = null;
218 this.currentTraversal = null;
219 this.readOnlyTraversal = null;
224 return this.currentTx;
227 public Admin asAdmin() {
233 public GraphTraversalSource getTraversalSource() {
234 return currentTraversal;
236 public GraphTraversalSource getReadOnlyTraversalSource() {
237 return readOnlyTraversal;
240 public JanusGraphManagement getManagementSystem() {
241 return getGraph().openManagement();