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