Update tinkerpop to 3.2.3 in aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / TransactionalGraphEngine.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.aai.serialization.engines;
22
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy;
29 import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.LazyBarrierStrategy;
30 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
31 import org.apache.tinkerpop.gremlin.structure.Graph;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.janusgraph.core.JanusGraph;
34 import org.janusgraph.core.schema.JanusGraphManagement;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.query.builder.*;
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;
40
41 public abstract class TransactionalGraphEngine {
42
43     protected GraphSingleton singleton = null;
44     protected QueryBuilder<Vertex> queryBuilder = null;
45     protected QueryStyle style;
46     protected final Loader loader;
47     protected Graph currentTx = null;
48     protected GraphTraversalSource currentTraversal = null;
49     protected GraphTraversalSource readOnlyTraversal = null;
50     private final Admin admin;
51
52     /**
53      * Instantiates a new transactional graph engine.
54      *
55      * @param style the style
56      * @param loader the loader
57      */
58     public TransactionalGraphEngine(QueryStyle style, Loader loader, GraphSingleton singleton) {
59         this.loader = loader;
60         this.style = style;
61         this.singleton = singleton;
62         admin = new Admin();
63     }
64
65     public TransactionalGraphEngine(QueryStyle style, Loader loader) {
66         this.loader = loader;
67         this.style = style;
68         admin = new Admin();
69
70     }
71
72     /**
73      * Sets the list property.
74      *
75      * @param v the v
76      * @param name the name
77      * @param obj the obj
78      * @return true, if successful
79      */
80     public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
81
82     /**
83      * Gets the list property.
84      *
85      * @param v the v
86      * @param name the name
87      * @return the list property
88      */
89     public abstract List<Object> getListProperty(Vertex v, String name);
90
91     /**
92      * Gets the graph.
93      *
94      * @return the graph
95      */
96     private JanusGraph getGraph() {
97         return singleton.getTxGraph();
98     }
99
100     /**
101      * Gets the count.
102      *
103      * @return the count
104      */
105     public AtomicInteger getCount() {
106         return singleton.getCount();
107     }
108
109     /**
110      * Gets the query engine.
111      *
112      * @return the query engine
113      */
114     public QueryEngine getQueryEngine() {
115         QueryEngine engine = null;
116         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
117             // this.queryEngine = new GremlinQueryEngine(this);
118         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
119             // this.queryEngine = new GremlinQueryEngine(this);
120         } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
121             // this.queryEngine = new GremlinPipelineQueryEngine(this);
122         } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
123
124             return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
125
126         } else {
127             throw new IllegalArgumentException("Query Engine type not recognized");
128         }
129
130         return engine;
131     }
132
133     /**
134      * Gets the query builder.
135      *
136      * @return the query builder
137      */
138     public QueryBuilder<Vertex> getQueryBuilder() {
139         return getQueryBuilder(this.loader);
140     }
141
142     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
143         return getQueryBuilder(style, this.loader);
144     }
145
146     public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
147         return getQueryBuilder(this.style, loader);
148     }
149
150     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, GraphTraversalSource source) {
151         return getQueryBuilder(style, this.loader, source);
152     }
153
154     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
155         GraphTraversalSource source = this.asAdmin().getTraversalSource();
156         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
157             return new GremlinTraversal<>(loader, source);
158         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
159             return new GremlinUnique<>(loader, source);
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, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source);
164         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
165             return new TraversalURIOptimizedQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source);
166         } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
167             throw new IllegalArgumentException("History Traversal needs history traversal source");
168         } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
169             throw new IllegalArgumentException("History Gremlin Traversal needs history traversal source");
170         } else {
171             throw new IllegalArgumentException("Query Builder type not recognized");
172         }
173         return queryBuilder;
174     }
175
176     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source) {
177         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
178             return new GremlinTraversal<>(loader, source);
179         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
180             return new GremlinUnique<>(loader, source);
181         } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
182             // return new GremlinPipelineTraversal(loader);
183         } else if (style.equals(QueryStyle.TRAVERSAL)) {
184             return new TraversalQuery<>(loader, source);
185         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
186             return new TraversalURIOptimizedQuery<>(loader, source);
187         } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
188             return new HistoryTraversalURIOptimizedQuery<>(loader, source);
189         } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
190             return new HistoryGremlinTraversal<>(loader, source);
191         } else {
192             throw new IllegalArgumentException("Query Builder type not recognized");
193         }
194         return queryBuilder;
195     }
196
197     /**
198      * Gets the query builder.
199      *
200      * @param start the start
201      * @return the query builder
202      */
203     public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
204         return getQueryBuilder(this.loader, start);
205     }
206
207     public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
208         return getQueryBuilder(this.style, loader, start);
209     }
210
211     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
212         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
213             return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
214         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
215             return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
216         } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
217             // return new GremlinPipelineTraversal(loader,start);
218         } else if (style.equals(QueryStyle.TRAVERSAL)) {
219             return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
220         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
221             return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
222         } else {
223             throw new IllegalArgumentException("Query Builder type not recognized");
224         }
225         return queryBuilder;
226     }
227
228     public Graph startTransaction() {
229         if (this.tx() == null) {
230             this.currentTx = this.getGraph().newTransaction();
231             this.currentTraversal = this.tx().traversal();
232             this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());
233         }
234         return currentTx;
235     }
236
237     public void rollback() {
238         if (this.tx() != null) {
239             this.tx().tx().rollback();
240             this.currentTx = null;
241             this.currentTraversal = null;
242             this.readOnlyTraversal = null;
243         }
244     }
245
246     public void commit() {
247         if (this.tx() != null) {
248             this.tx().tx().commit();
249             this.currentTx = null;
250             this.currentTraversal = null;
251             this.readOnlyTraversal = null;
252         }
253     }
254
255     public Graph tx() {
256         return this.currentTx;
257     }
258
259     public Admin asAdmin() {
260         return admin;
261     }
262
263     public class Admin {
264
265         public GraphTraversalSource getTraversalSource() {
266             return currentTraversal;
267         }
268
269         public GraphTraversalSource getReadOnlyTraversalSource() {
270             return readOnlyTraversal;
271         }
272
273         public JanusGraphManagement getManagementSystem() {
274             return getGraph().openManagement();
275         }
276     }
277 }