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