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