2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.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.openecomp.aai.dbmap.DBConnectionType;
31 import org.openecomp.aai.introspection.Loader;
32 import org.openecomp.aai.query.builder.GremlinTraversal;
33 import org.openecomp.aai.query.builder.GremlinUnique;
34 import org.openecomp.aai.query.builder.QueryBuilder;
35 import org.openecomp.aai.query.builder.TraversalQuery;
36 import org.openecomp.aai.serialization.db.GraphSingleton;
37 import org.openecomp.aai.serialization.engines.query.GraphTraversalQueryEngine;
38 import org.openecomp.aai.serialization.engines.query.QueryEngine;
40 import com.thinkaurelius.titan.core.TitanGraph;
41 import com.thinkaurelius.titan.core.schema.TitanManagement;
43 public abstract class TransactionalGraphEngine {
45 protected GraphSingleton singleton = null;
46 protected QueryEngine queryEngine = null;
47 protected QueryBuilder<Vertex> queryBuilder = null;
48 protected QueryStyle style = null;
49 protected final DBConnectionType connectionType;
50 protected final Loader loader;
51 protected Graph currentTx = null;
52 protected GraphTraversalSource currentTraversal = null;
53 protected GraphTraversalSource readOnlyTraversal = null;
54 private final Admin admin;
56 * Instantiates a new transactional graph engine.
58 * @param style the style
59 * @param loader the loader
61 public TransactionalGraphEngine (QueryStyle style, Loader loader, DBConnectionType connectionType, GraphSingleton singleton) {
64 this.singleton = singleton;
65 this.connectionType = connectionType;
70 public TransactionalGraphEngine (QueryStyle style, Loader loader) {
73 this.connectionType = DBConnectionType.REALTIME;
78 * Sets the list property.
81 * @param name the name
83 * @return true, if successful
85 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
88 * Gets the list property.
91 * @param name the name
92 * @return the list property
94 public abstract List<Object> getListProperty(Vertex v, String name);
101 private TitanGraph getGraph() {
102 return singleton.getTxGraph(this.connectionType);
110 public AtomicInteger getCount() {
111 return singleton.getCount();
116 * Gets the query engine.
118 * @return the query engine
120 public QueryEngine getQueryEngine() {
121 QueryEngine engine = null;
122 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
123 //this.queryEngine = new GremlinQueryEngine(this);
124 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
125 //this.queryEngine = new GremlinQueryEngine(this);
126 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
127 //this.queryEngine = new GremlinPipelineQueryEngine(this);
128 } else if (style.equals(QueryStyle.TRAVERSAL)) {
130 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
133 throw new IllegalArgumentException("Query Engine type not recognized");
140 * Gets the query builder.
142 * @return the query builder
144 public QueryBuilder<Vertex> getQueryBuilder() {
145 return getQueryBuilder(this.loader);
148 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
149 return getQueryBuilder(style, this.loader);
152 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
153 return getQueryBuilder(this.style, loader);
156 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
157 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
158 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
159 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
160 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
161 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
162 //return new GremlinPipelineTraversal(loader);
163 } else if (style.equals(QueryStyle.TRAVERSAL)) {
164 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
166 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);
194 throw new IllegalArgumentException("Query Builder type not recognized");
198 public Graph startTransaction() {
199 if (this.tx() == null) {
200 this.currentTx = this.getGraph().newTransaction();
201 this.currentTraversal = this.tx().traversal();
202 this.readOnlyTraversal = this.tx().traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
207 public void rollback() {
208 if (this.tx() != null) {
209 this.tx().tx().rollback();
210 this.currentTx = null;
211 this.currentTraversal = null;
212 this.readOnlyTraversal = null;
215 public void commit() {
216 if (this.tx() != null) {
217 this.tx().tx().commit();
218 this.currentTx = null;
219 this.currentTraversal = null;
220 this.readOnlyTraversal = null;
225 return this.currentTx;
228 public Admin asAdmin() {
234 public GraphTraversalSource getTraversalSource() {
235 return currentTraversal;
237 public GraphTraversalSource getReadOnlyTraversalSource() {
238 return readOnlyTraversal;
241 public TitanManagement getManagementSystem() {
242 return getGraph().openManagement();