[AAI-178 Amsterdam] Make Edge Properties to be
[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 com.thinkaurelius.titan.core.TitanGraph;
24 import com.thinkaurelius.titan.core.schema.TitanManagement;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
26 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
27 import org.apache.tinkerpop.gremlin.structure.Graph;
28 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.openecomp.aai.dbmap.DBConnectionType;
30 import org.openecomp.aai.introspection.Loader;
31 import org.openecomp.aai.query.builder.GremlinTraversal;
32 import org.openecomp.aai.query.builder.GremlinUnique;
33 import org.openecomp.aai.query.builder.QueryBuilder;
34 import org.openecomp.aai.query.builder.TraversalQuery;
35 import org.openecomp.aai.serialization.db.GraphSingleton;
36 import org.openecomp.aai.serialization.engines.query.GraphTraversalQueryEngine;
37 import org.openecomp.aai.serialization.engines.query.QueryEngine;
38
39 import java.util.List;
40 import java.util.concurrent.atomic.AtomicInteger;
41
42 public abstract class TransactionalGraphEngine {
43         
44         protected GraphSingleton singleton = null;
45         protected QueryEngine queryEngine = null;
46         protected QueryBuilder<Vertex> queryBuilder = null;
47         protected QueryStyle style = null;
48         protected final DBConnectionType connectionType;
49         protected final Loader loader;
50         protected Graph currentTx = null;
51         protected GraphTraversalSource currentTraversal = null;
52         protected GraphTraversalSource readOnlyTraversal = null;
53         private final Admin admin;
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, DBConnectionType connectionType, GraphSingleton singleton) {
61                 this.loader = loader;
62                 this.style = style;
63                 this.singleton = singleton;
64                 this.connectionType = connectionType;
65                 admin = new Admin();
66                 
67         }
68         
69         public TransactionalGraphEngine (QueryStyle style, Loader loader) {
70                 this.loader = loader;
71                 this.style = style;
72                 this.connectionType = DBConnectionType.REALTIME;
73                 admin = new Admin();
74         }
75
76         /**
77          * Sets the list property.
78          *
79          * @param v the v
80          * @param name the name
81          * @param obj the obj
82          * @return true, if successful
83          */
84         public abstract boolean setListProperty(Vertex v, String name, List<?> obj);
85         
86         /**
87          * Gets the list property.
88          *
89          * @param v the v
90          * @param name the name
91          * @return the list property
92          */
93         public abstract List<Object> getListProperty(Vertex v, String name);
94         
95         /**
96          * Gets the graph.
97          *
98          * @return the graph
99          */
100         private TitanGraph getGraph() {
101                 return singleton.getTxGraph(this.connectionType);
102         }
103         
104         /**
105          * Gets the count.
106          *
107          * @return the count
108          */
109         public AtomicInteger getCount() {
110                 return singleton.getCount();
111         }
112         
113         
114         /**
115          * Gets the query engine.
116          *
117          * @return the query engine
118          */
119         public QueryEngine getQueryEngine() {
120                 QueryEngine engine = null;
121                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
122                         //this.queryEngine = new GremlinQueryEngine(this);
123                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
124                         //this.queryEngine = new GremlinQueryEngine(this);
125                 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
126                         //this.queryEngine = new GremlinPipelineQueryEngine(this);
127                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
128                         
129                         return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource());
130                         
131                 } else {
132                         throw new IllegalArgumentException("Query Engine type not recognized");
133                 }
134                 
135                 return engine;
136         }
137         
138         /**
139          * Gets the query builder.
140          *
141          * @return the query builder
142          */
143         public QueryBuilder<Vertex> getQueryBuilder() {
144                 return getQueryBuilder(this.loader);
145         }
146         
147         public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
148                 return getQueryBuilder(style, this.loader);
149         }
150         
151         public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
152                 return getQueryBuilder(this.style, loader);
153         }
154         
155         public  QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
156                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
157                         return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
158                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
159                         return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
160                 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
161                         //return new GremlinPipelineTraversal(loader);
162                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
163                         return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
164                 }  else {
165                         throw new IllegalArgumentException("Query Builder type not recognized");
166                 }
167                 return queryBuilder;
168         }
169         /**
170          * Gets the query builder.
171          *
172          * @param start the start
173          * @return the query builder
174          */
175         public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
176                 return getQueryBuilder(this.loader, start);
177         }
178         
179         public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
180                 return getQueryBuilder(this.style, loader, start);
181         }
182         
183         public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader, Vertex start) {
184                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
185                         return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource(), start);
186                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
187                         return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
188                 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
189                         //return new GremlinPipelineTraversal(loader,start);
190                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
191                         return new TraversalQuery<>(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 TitanManagement getManagementSystem() {
241                         return getGraph().openManagement();
242                 }
243         }
244 }