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