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