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