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