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