2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.serialization.engines;
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;
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;
42 import com.thinkaurelius.titan.core.TitanGraph;
44 public class InMemoryDBEngine extends TransactionalGraphEngine {
47 * Instantiates a new titan DB engine.
54 private TitanGraph graph = null;
56 private static final TransactionalGraphEngine.Admin admin = null;
58 public InMemoryDBEngine(QueryStyle style, DBConnectionType connectionType, Loader loader, TitanGraph graph) {
59 super(style, loader, connectionType, InMemoryGraphSingleton.getInstance(graph));
64 * Instantiates a new titan DB engine.
73 public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, TitanGraph graph) {
76 this.singleton = InMemoryGraphSingleton.getInstance(graph);
82 public QueryEngine getQueryEngine() {
84 if (style.equals(QueryStyle.TRAVERSAL)) {
86 GraphTraversalSource traversalSource = graph.traversal();
87 return new GraphTraversalQueryEngine(traversalSource);
90 throw new IllegalArgumentException("Query Engine type not recognized");
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());
103 throw new IllegalArgumentException("Query Builder type is Not recognized");
112 public boolean setListProperty(Vertex v, String name, List<?> objs) {
114 // clear out list full replace style
116 Iterator<VertexProperty<Object>> iterator = v.properties(name);
117 while (iterator.hasNext()) {
118 iterator.next().remove();
121 for (Object obj : objs) {
122 v.property(name, obj);
132 public List<Object> getListProperty(Vertex v, String name) {
134 List<Object> result = new ArrayList<>();
136 Iterator<VertexProperty<Object>> iterator = v.properties(name);
138 while (iterator.hasNext()) {
139 result.add(iterator.next().value());
142 if (result.isEmpty()) {
151 public QueryBuilder<Vertex> getQueryBuilder() {
152 return getQueryBuilder(this.loader);
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());
164 throw new IllegalArgumentException("Query Builder type not recognized");
170 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
171 return getQueryBuilder(this.loader, start);
174 public GraphTraversalSource getTraversalSource() {
175 return graph.traversal();
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);
187 throw new IllegalArgumentException("Query Builder type not recognized");
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()));