2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.engines;
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.verification.ReadOnlyStrategy;
29 import org.apache.tinkerpop.gremlin.structure.Graph;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.onap.aai.dbmap.DBConnectionType;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.query.builder.GremlinTraversal;
34 import org.onap.aai.query.builder.GremlinUnique;
35 import org.onap.aai.query.builder.QueryBuilder;
36 import org.onap.aai.query.builder.TraversalQuery;
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 import com.thinkaurelius.titan.core.TitanGraph;
42 import com.thinkaurelius.titan.core.schema.TitanManagement;
44 public abstract class TransactionalGraphEngine {
46 protected GraphSingleton singleton = null;
47 protected QueryEngine queryEngine = null;
48 protected QueryBuilder<Vertex> queryBuilder = null;
49 protected QueryStyle style = null;
50 protected final DBConnectionType connectionType;
51 protected final Loader loader;
52 protected Graph currentTx = null;
53 protected GraphTraversalSource currentTraversal = null;
54 protected GraphTraversalSource readOnlyTraversal = null;
55 private final Admin admin;
57 * Instantiates a new transactional graph engine.
59 * @param style the style
60 * @param loader the loader
62 public TransactionalGraphEngine (QueryStyle style, Loader loader, DBConnectionType connectionType, GraphSingleton singleton) {
65 this.singleton = singleton;
66 this.connectionType = connectionType;
71 public TransactionalGraphEngine (QueryStyle style, Loader loader) {
74 this.connectionType = DBConnectionType.REALTIME;
79 * Sets the list property.
82 * @param name the name
84 * @return true, if successful
86 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
89 * Gets the list property.
92 * @param name the name
93 * @return the list property
95 public abstract List<Object> getListProperty(Vertex v, String name);
102 private TitanGraph getGraph() {
103 return singleton.getTxGraph(this.connectionType);
111 public AtomicInteger getCount() {
112 return singleton.getCount();
117 * Gets the query engine.
119 * @return the query engine
121 public QueryEngine getQueryEngine() {
122 QueryEngine engine = null;
123 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
124 //this.queryEngine = new GremlinQueryEngine(this);
125 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
126 //this.queryEngine = new GremlinQueryEngine(this);
127 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
128 //this.queryEngine = new GremlinPipelineQueryEngine(this);
129 } else if (style.equals(QueryStyle.TRAVERSAL)) {
131 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
134 throw new IllegalArgumentException("Query Engine type not recognized");
141 * Gets the query builder.
143 * @return the query builder
145 public QueryBuilder<Vertex> getQueryBuilder() {
146 return getQueryBuilder(this.loader);
149 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
150 return getQueryBuilder(style, this.loader);
153 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
154 return getQueryBuilder(this.style, loader);
157 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
158 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
159 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
160 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
161 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
162 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
163 //return new GremlinPipelineTraversal(loader);
164 } else if (style.equals(QueryStyle.TRAVERSAL)) {
165 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
167 throw new IllegalArgumentException("Query Builder type not recognized");
172 * Gets the query builder.
174 * @param start the start
175 * @return the query builder
177 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
178 return getQueryBuilder(this.loader, start);
181 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
182 return getQueryBuilder(this.style, loader, start);
185 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
186 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
187 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
188 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
189 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
190 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
191 //return new GremlinPipelineTraversal(loader,start);
192 } else if (style.equals(QueryStyle.TRAVERSAL)) {
193 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
195 throw new IllegalArgumentException("Query Builder type not recognized");
199 public Graph startTransaction() {
200 if (this.tx() == null) {
201 this.currentTx = this.getGraph().newTransaction();
202 this.currentTraversal = this.tx().traversal();
203 this.readOnlyTraversal = this.tx().traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
208 public void rollback() {
209 if (this.tx() != null) {
210 this.tx().tx().rollback();
211 this.currentTx = null;
212 this.currentTraversal = null;
213 this.readOnlyTraversal = null;
216 public void commit() {
217 if (this.tx() != null) {
218 this.tx().tx().commit();
219 this.currentTx = null;
220 this.currentTraversal = null;
221 this.readOnlyTraversal = null;
226 return this.currentTx;
229 public Admin asAdmin() {
235 public GraphTraversalSource getTraversalSource() {
236 return currentTraversal;
238 public GraphTraversalSource getReadOnlyTraversalSource() {
239 return readOnlyTraversal;
242 public TitanManagement getManagementSystem() {
243 return getGraph().openManagement();