eaf6829c4f44b2780782ef014e5d2e48bd9907d5
[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 package org.onap.aai.serialization.engines;
21
22 import org.janusgraph.core.JanusGraph;
23 import org.janusgraph.core.schema.JanusGraphManagement;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
26 import org.apache.tinkerpop.gremlin.structure.Graph;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.onap.aai.dbmap.DBConnectionType;
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 QueryEngine queryEngine = null;
42         protected QueryBuilder<Vertex> queryBuilder = null;
43         protected QueryStyle style = null;
44         protected final DBConnectionType connectionType;
45         protected final Loader loader;
46         protected Graph currentTx = null;
47         protected GraphTraversalSource currentTraversal = null;
48         protected GraphTraversalSource readOnlyTraversal = null;
49         private final Admin admin;
50         /**
51          * Instantiates a new transactional graph engine.
52          *
53          * @param style the style
54          * @param loader the loader
55          */
56         public TransactionalGraphEngine (QueryStyle style, Loader loader, DBConnectionType connectionType, GraphSingleton singleton) {
57                 this.loader = loader;
58                 this.style = style;
59                 this.singleton = singleton;
60                 this.connectionType = connectionType;
61                 admin = new Admin();
62         }
63         
64         public TransactionalGraphEngine (QueryStyle style, Loader loader) {
65                 this.loader = loader;
66                 this.style = style;
67                 this.connectionType = DBConnectionType.REALTIME;
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(this.connectionType);
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         /**
111          * Gets the query engine.
112          *
113          * @return the query engine
114          */
115         public QueryEngine getQueryEngine() {
116                 QueryEngine engine = null;
117                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
118                         //this.queryEngine = new GremlinQueryEngine(this);
119                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
120                         //this.queryEngine = new GremlinQueryEngine(this);
121                 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
122                         //this.queryEngine = new GremlinPipelineQueryEngine(this);
123                 } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
124                         
125                         return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
126                         
127                 } else {
128                         throw new IllegalArgumentException("Query Engine type not recognized");
129                 }
130                 
131                 return engine;
132         }
133         
134         /**
135          * Gets the query builder.
136          *
137          * @return the query builder
138          */
139         public QueryBuilder<Vertex> getQueryBuilder() {
140                 return getQueryBuilder(this.loader);
141         }
142         
143         public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
144                 return getQueryBuilder(style, this.loader);
145         }
146         
147         public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
148                 return getQueryBuilder(this.style, loader);
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 {
163                         throw new IllegalArgumentException("Query Builder type not recognized");
164                 }
165                 return queryBuilder;
166         }
167         /**
168          * Gets the query builder.
169          *
170          * @param start the start
171          * @return the query builder
172          */
173         public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
174                 return getQueryBuilder(this.loader, start);
175         }
176         
177         public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
178                 return getQueryBuilder(this.style, loader, start);
179         }
180         
181         public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
182                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
183                         return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
184                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
185                         return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
186                 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
187                         //return new GremlinPipelineTraversal(loader,start);
188                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
189                         return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start);
190                 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
191                         return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start);
192                 } else {
193                         throw new IllegalArgumentException("Query Builder type not recognized");
194                 }
195                 return queryBuilder;
196         }
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()));
202                 }
203                 return currentTx;
204         }
205         
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;
212                 }
213         }
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;
220                 }
221         }
222         
223         public Graph tx() {
224                 return this.currentTx;
225         }
226         
227         public Admin asAdmin() {
228                 return admin;
229         }
230         
231         public class Admin {
232                 
233                 public GraphTraversalSource getTraversalSource() {
234                         return currentTraversal;
235                 }
236                 public GraphTraversalSource getReadOnlyTraversalSource() {
237                         return readOnlyTraversal;
238                 }
239                 
240                 public JanusGraphManagement getManagementSystem() {
241                         return getGraph().openManagement();
242                 }
243         }
244 }