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.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;
39 import com.thinkaurelius.titan.core.TitanGraph;
40 import com.thinkaurelius.titan.core.TitanTransaction;
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 queryBuilder = null;
48 protected QueryStyle style = null;
49 protected final DBConnectionType connectionType;
50 protected final Loader loader;
51 protected TitanTransaction 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 getQueryBuilder() {
145 return getQueryBuilder(this.loader);
148 public QueryBuilder getQueryBuilder(Loader loader) {
149 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
150 return new GremlinTraversal(loader, this.asAdmin().getTraversalSource());
151 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
152 return new GremlinUnique(loader, this.asAdmin().getTraversalSource());
153 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
154 //return new GremlinPipelineTraversal(loader);
155 } else if (style.equals(QueryStyle.TRAVERSAL)) {
156 return new TraversalQuery(loader, this.asAdmin().getTraversalSource());
158 throw new IllegalArgumentException("Query Builder type not recognized");
163 * Gets the query builder.
165 * @param start the start
166 * @return the query builder
168 public QueryBuilder getQueryBuilder(Vertex start) {
169 return getQueryBuilder(this.loader, start);
172 public QueryBuilder getQueryBuilder(Loader loader, Vertex start) {
173 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
174 return new GremlinTraversal(loader, this.asAdmin().getTraversalSource(), start);
175 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
176 return new GremlinUnique(loader, this.asAdmin().getTraversalSource(), start);
177 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
178 //return new GremlinPipelineTraversal(loader,start);
179 } else if (style.equals(QueryStyle.TRAVERSAL)) {
180 return new TraversalQuery(loader, this.asAdmin().getTraversalSource(), start);
182 throw new IllegalArgumentException("Query Builder type not recognized");
187 public TitanTransaction startTransaction() {
188 if (this.tx() == null) {
189 this.currentTx = this.getGraph().newTransaction();
190 this.currentTraversal = this.tx().traversal();
191 this.readOnlyTraversal = this.tx().traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
196 public void rollback() {
197 if (this.tx() != null) {
198 this.tx().rollback();
199 this.currentTx = null;
200 this.currentTraversal = null;
201 this.readOnlyTraversal = null;
204 public void commit() {
205 if (this.tx() != null) {
207 this.currentTx = null;
208 this.currentTraversal = null;
209 this.readOnlyTraversal = null;
213 public TitanTransaction tx() {
214 return this.currentTx;
217 public Admin asAdmin() {
223 public GraphTraversalSource getTraversalSource() {
224 return currentTraversal;
226 public GraphTraversalSource getReadOnlyTraversalSource() {
227 return readOnlyTraversal;
230 public TitanManagement getManagementSystem() {
231 return getGraph().openManagement();