Add the vnf changes
[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.Graph;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
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
40 import com.thinkaurelius.titan.core.TitanGraph;
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<Vertex> queryBuilder = null;
48         protected QueryStyle style = null;
49         protected final DBConnectionType connectionType;
50         protected final Loader loader;
51         protected Graph 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<Vertex> getQueryBuilder() {
145                 return getQueryBuilder(this.loader);
146         }
147         
148         public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style) {
149                 return getQueryBuilder(style, this.loader);
150         }
151         
152         public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
153                 return getQueryBuilder(this.style, loader);
154         }
155         
156         public  QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
157                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
158                         return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
159                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
160                         return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
161                 } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) {
162                         //return new GremlinPipelineTraversal(loader);
163                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
164                         return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource());
165                 }  else {
166                         throw new IllegalArgumentException("Query Builder type not recognized");
167                 }
168                 return queryBuilder;
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 {
194                         throw new IllegalArgumentException("Query Builder type not recognized");
195                 }
196                 return queryBuilder;
197         }
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 }