Update tinkerpop to 3.2.4
[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  * Modifications Copyright © 2024 DEUTSCHE TELEKOM AG.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.serialization.engines;
24
25 import java.util.List;
26 import java.util.concurrent.atomic.AtomicInteger;
27
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
29 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
31 import org.apache.tinkerpop.gremlin.process.traversal.strategy.optimization.InlineFilterStrategy;
32 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
33 import org.apache.tinkerpop.gremlin.structure.Graph;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.janusgraph.core.JanusGraph;
36 import org.janusgraph.core.schema.JanusGraphManagement;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.query.builder.*;
39 import org.onap.aai.serialization.db.GraphSingleton;
40 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
41 import org.onap.aai.serialization.engines.query.QueryEngine;
42
43 public abstract class TransactionalGraphEngine {
44
45     protected GraphSingleton singleton = null;
46     protected QueryBuilder<Vertex> queryBuilder = null;
47     protected QueryStyle style;
48     protected final Loader loader;
49     protected Graph currentTx = null;
50     protected GraphTraversalSource currentTraversal = null;
51     protected GraphTraversalSource readOnlyTraversal = null;
52     private final Admin admin;
53
54     /**
55      * Instantiates a new transactional graph engine.
56      *
57      * @param style the style
58      * @param loader the loader
59      */
60     public TransactionalGraphEngine(QueryStyle style, Loader loader, GraphSingleton singleton) {
61         this.loader = loader;
62         this.style = style;
63         this.singleton = singleton;
64         admin = new Admin();
65     }
66
67     public TransactionalGraphEngine(QueryStyle style, Loader loader) {
68         this.loader = loader;
69         this.style = style;
70         admin = new Admin();
71
72     }
73
74     /**
75      * Sets the list property.
76      *
77      * @param v the v
78      * @param name the name
79      * @param obj the obj
80      * @return true, if successful
81      */
82     public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
83
84     /**
85      * Gets the list property.
86      *
87      * @param v the v
88      * @param name the name
89      * @return the list property
90      */
91     public abstract List<Object> getListProperty(Vertex v, String name);
92
93     /**
94      * Gets the graph.
95      *
96      * @return the graph
97      */
98     private JanusGraph getGraph() {
99         return singleton.getTxGraph();
100     }
101
102     /**
103      * Gets the count.
104      *
105      * @return the count
106      */
107     public AtomicInteger getCount() {
108         return singleton.getCount();
109     }
110
111     /**
112      * Gets the query engine.
113      *
114      * @return the query engine
115      */
116     public QueryEngine getQueryEngine() {
117         QueryEngine engine = null;
118         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
119             // this.queryEngine = new GremlinQueryEngine(this);
120         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
121             // this.queryEngine = new GremlinQueryEngine(this);
122         } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
123             // this.queryEngine = new GremlinPipelineQueryEngine(this);
124         } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
125
126             return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
127
128         } else {
129             throw new IllegalArgumentException("Query Engine type not recognized");
130         }
131
132         return engine;
133     }
134
135     /**
136      * Gets the query builder.
137      *
138      * @return the query builder
139      */
140     public QueryBuilder<Vertex> getQueryBuilder() {
141         return getQueryBuilder(this.loader);
142     }
143
144     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
145         return getQueryBuilder(style, this.loader);
146     }
147
148     public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
149         return getQueryBuilder(this.style, loader);
150     }
151
152     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, GraphTraversalSource source) {
153         return getQueryBuilder(style, this.loader, source);
154     }
155
156     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
157         GraphTraversalSource source = this.asAdmin().getTraversalSource();
158         return this.getQueryBuilder(style, loader, source);
159     }
160
161     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source) {
162         return this.getQueryBuilder(style, loader, source, (GraphTraversal<Vertex, Vertex>) __.<Vertex>start());
163     }
164
165     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, GraphTraversalSource source, GraphTraversal<Vertex, Vertex> traversal) {
166         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
167             return new GremlinTraversal<>(loader, source);
168         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
169             return new GremlinUnique<>(loader, source);
170         } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
171             // return new GremlinPipelineTraversal(loader);
172         } else if (style.equals(QueryStyle.TRAVERSAL)) {
173             return new TraversalQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source, traversal);
174         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
175             return new TraversalURIOptimizedQuery<>(loader, source != null ? source.withoutStrategies(InlineFilterStrategy.class) : source);
176         } else if (style.equals(QueryStyle.HISTORY_TRAVERSAL)) {
177             return new HistoryTraversalURIOptimizedQuery<>(loader, source);
178         } else if (style.equals(QueryStyle.HISTORY_GREMLIN_TRAVERSAL)) {
179             return new HistoryGremlinTraversal<>(loader, source);
180         } else {
181             throw new IllegalArgumentException("Query Builder type not recognized");
182         }
183         return queryBuilder;
184     }
185
186     /**
187      * Gets the query builder.
188      *
189      * @param start the start
190      * @return the query builder
191      */
192     public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
193         return getQueryBuilder(this.loader, start);
194     }
195
196     public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
197         return getQueryBuilder(this.style, loader, start);
198     }
199
200     public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
201         if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
202             return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
203         } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
204             return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
205         } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
206             // return new GremlinPipelineTraversal(loader,start);
207         } else if (style.equals(QueryStyle.TRAVERSAL)) {
208             return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
209         } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
210             return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
211         } else {
212             throw new IllegalArgumentException("Query Builder type not recognized");
213         }
214         return queryBuilder;
215     }
216
217     public Graph startTransaction() {
218         if (this.tx() == null) {
219             this.currentTx = this.getGraph().newTransaction();
220             this.currentTraversal = this.tx().traversal();
221             this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());
222         }
223         return currentTx;
224     }
225
226     public void rollback() {
227         if (this.tx() != null) {
228             this.tx().tx().rollback();
229             this.currentTx = null;
230             this.currentTraversal = null;
231             this.readOnlyTraversal = null;
232         }
233     }
234
235     public void commit() {
236         if (this.tx() != null) {
237             this.tx().tx().commit();
238             this.currentTx = null;
239             this.currentTraversal = null;
240             this.readOnlyTraversal = null;
241         }
242     }
243
244     public Graph tx() {
245         return this.currentTx;
246     }
247
248     public Admin asAdmin() {
249         return admin;
250     }
251
252     public class Admin {
253
254         public GraphTraversalSource getTraversalSource() {
255             return currentTraversal;
256         }
257
258         public GraphTraversalSource getReadOnlyTraversalSource() {
259             return readOnlyTraversal;
260         }
261
262         public JanusGraphManagement getManagementSystem() {
263             return getGraph().openManagement();
264         }
265     }
266 }