Update from titan to using janusgraph
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / InMemoryDBEngine.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 java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
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.apache.tinkerpop.gremlin.structure.VertexProperty;
31
32 import org.onap.aai.dbmap.DBConnectionType;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.query.builder.GremlinTraversal;
35 import org.onap.aai.query.builder.GremlinUnique;
36 import org.onap.aai.query.builder.QueryBuilder;
37 import org.onap.aai.query.builder.TraversalQuery;
38 import org.onap.aai.serialization.db.InMemoryGraphSingleton;
39 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
40 import org.onap.aai.serialization.engines.query.QueryEngine;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42
43 import org.janusgraph.core.JanusGraph;
44
45 public class InMemoryDBEngine extends TransactionalGraphEngine {
46
47         /**
48          * Instantiates a new JanusGraph DB engine.
49          *
50          * @param style
51          *            the style
52          * @param loader
53          *            the loader
54          */
55         private JanusGraph graph = null;
56
57         private static final TransactionalGraphEngine.Admin admin = null;
58
59         public InMemoryDBEngine(QueryStyle style, DBConnectionType connectionType, Loader loader, JanusGraph graph) {
60                 super(style, loader, connectionType, InMemoryGraphSingleton.getInstance(graph));
61                 this.graph = graph;
62         }
63
64         /**
65          * Instantiates a new JanusGraph DB engine.
66          *
67          * @param style
68          *            the style
69          * @param loader
70          *            the loader
71          * @param connect
72          *            the connect
73          */
74         public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, JanusGraph graph) {
75                 super(style, loader);
76                 if (connect) {
77                         this.singleton = InMemoryGraphSingleton.getInstance(graph);
78                 }
79                 this.graph = graph;
80         }
81
82         @Override
83         public QueryEngine getQueryEngine() {
84
85                 if (style.equals(QueryStyle.TRAVERSAL)) {
86
87                         GraphTraversalSource traversalSource = graph.traversal();
88                         return new GraphTraversalQueryEngine(traversalSource);
89
90                 } else {
91                         throw new IllegalArgumentException("Query Engine type not recognized");
92                 }
93
94         }
95
96         @Override
97         public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
98                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
99                         return new GremlinTraversal<>(loader, graph.traversal());
100                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
101                         return new TraversalQuery<>(loader, graph.traversal());
102
103                 } else {
104                         throw new IllegalArgumentException("Query Builder type is Not recognized");
105                 }
106
107         }
108
109         /**
110          * {@inheritDoc}
111          */
112         @Override
113         public boolean setListProperty(Vertex v, String name, List<?> objs) {
114
115                 // clear out list full replace style
116
117                 Iterator<VertexProperty<Object>> iterator = v.properties(name);
118                 while (iterator.hasNext()) {
119                         iterator.next().remove();
120                 }
121                 if (objs != null) {
122                         for (Object obj : objs) {
123                                 v.property(name, obj);
124                         }
125                 }
126                 return true;
127         }
128
129         /**
130          * {@inheritDoc}
131          */
132         @Override
133         public List<Object> getListProperty(Vertex v, String name) {
134
135                 List<Object> result = new ArrayList<>();
136
137                 Iterator<VertexProperty<Object>> iterator = v.properties(name);
138
139                 while (iterator.hasNext()) {
140                         result.add(iterator.next().value());
141                 }
142
143                 if (result.isEmpty()) {
144                         result = null;
145                 }
146
147                 return result;
148
149         }
150
151         @Override
152         public QueryBuilder<Vertex> getQueryBuilder() {
153                 return getQueryBuilder(this.loader);
154         }
155
156         @Override
157         public QueryBuilder<Vertex> getQueryBuilder(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.TRAVERSAL)) {
163                         return new TraversalQuery<>(loader, graph.traversal());
164                 } else {
165                         throw new IllegalArgumentException("Query Builder type not recognized");
166                 }
167
168         }
169
170         @Override
171         public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
172                 return getQueryBuilder(this.loader, start);
173         }
174
175         public GraphTraversalSource getTraversalSource() {
176                 return graph.traversal();
177         }
178
179         @Override
180         public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
181                 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
182                         return new GremlinTraversal<>(loader, graph.traversal(), start);
183                 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
184                         return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
185                 } else if (style.equals(QueryStyle.TRAVERSAL)) {
186                         return new TraversalQuery<>(loader, graph.traversal(), start);
187                 } else {
188                         throw new IllegalArgumentException("Query Builder type not recognized");
189                 }
190
191         }
192
193         @Override
194         public Graph startTransaction() {
195                 if (this.tx() == null) {
196                         this.currentTx = graph.newTransaction();
197                         this.currentTraversal = this.tx().traversal();
198                         this.readOnlyTraversal = this.tx()
199                                         .traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
200                 }
201                 return currentTx;
202         }
203
204         
205
206 }