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 com.thinkaurelius.titan.core.TitanGraph;
24 import com.thinkaurelius.titan.core.schema.TitanManagement;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
27 import org.apache.tinkerpop.gremlin.structure.Graph;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.openecomp.aai.dbmap.DBConnectionType;
30 import org.openecomp.aai.introspection.Loader;
31 import org.openecomp.aai.query.builder.GremlinTraversal;
32 import org.openecomp.aai.query.builder.GremlinUnique;
33 import org.openecomp.aai.query.builder.QueryBuilder;
34 import org.openecomp.aai.query.builder.TraversalQuery;
35 import org.openecomp.aai.serialization.db.GraphSingleton;
36 import org.openecomp.aai.serialization.engines.query.GraphTraversalQueryEngine;
37 import org.openecomp.aai.serialization.engines.query.QueryEngine;
39 import java.util.List;
40 import java.util.concurrent.atomic.AtomicInteger;
42 public abstract class TransactionalGraphEngine {
44 protected GraphSingleton singleton = null;
45 protected QueryEngine queryEngine = null;
46 protected QueryBuilder<Vertex> queryBuilder = null;
47 protected QueryStyle style = null;
48 protected final DBConnectionType connectionType;
49 protected final Loader loader;
50 protected Graph currentTx = null;
51 protected GraphTraversalSource currentTraversal = null;
52 protected GraphTraversalSource readOnlyTraversal = null;
53 private final Admin admin;
55 * Instantiates a new transactional graph engine.
57 * @param style the style
58 * @param loader the loader
60 public TransactionalGraphEngine (QueryStyle style, Loader loader, DBConnectionType connectionType, GraphSingleton singleton) {
63 this.singleton = singleton;
64 this.connectionType = connectionType;
69 public TransactionalGraphEngine (QueryStyle style, Loader loader) {
72 this.connectionType = DBConnectionType.REALTIME;
77 * Sets the list property.
80 * @param name the name
82 * @return true, if successful
84 public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
87 * Gets the list property.
90 * @param name the name
91 * @return the list property
93 public abstract List<Object> getListProperty(Vertex v, String name);
100 private TitanGraph getGraph() {
101 return singleton.getTxGraph(this.connectionType);
109 public AtomicInteger getCount() {
110 return singleton.getCount();
115 * Gets the query engine.
117 * @return the query engine
119 public QueryEngine getQueryEngine() {
120 QueryEngine engine = null;
121 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
122 //this.queryEngine = new GremlinQueryEngine(this);
123 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
124 //this.queryEngine = new GremlinQueryEngine(this);
125 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
126 //this.queryEngine = new GremlinPipelineQueryEngine(this);
127 } else if (style.equals(QueryStyle.TRAVERSAL)) {
129 return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
132 throw new IllegalArgumentException("Query Engine type not recognized");
139 * Gets the query builder.
141 * @return the query builder
143 public QueryBuilder<Vertex> getQueryBuilder() {
144 return getQueryBuilder(this.loader);
147 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
148 return getQueryBuilder(style, this.loader);
151 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
152 return getQueryBuilder(this.style, loader);
155 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
156 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
157 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
158 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
159 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
160 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
161 //return new GremlinPipelineTraversal(loader);
162 } else if (style.equals(QueryStyle.TRAVERSAL)) {
163 return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
165 throw new IllegalArgumentException("Query Builder type not recognized");
170 * Gets the query builder.
172 * @param start the start
173 * @return the query builder
175 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
176 return getQueryBuilder(this.loader, start);
179 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
180 return getQueryBuilder(this.style, loader, start);
183 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
184 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
185 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
186 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
187 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
188 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
189 //return new GremlinPipelineTraversal(loader,start);
190 } else if (style.equals(QueryStyle.TRAVERSAL)) {
191 return new TraversalQuery<>(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 TitanManagement getManagementSystem() {
241 return getGraph().openManagement();