2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.engines;
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;
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;
44 import com.thinkaurelius.titan.core.TitanGraph;
46 public class InMemoryDBEngine extends TransactionalGraphEngine {
49 * Instantiates a new titan DB engine.
56 private TitanGraph graph = null;
58 private static final TransactionalGraphEngine.Admin admin = null;
60 public InMemoryDBEngine(QueryStyle style, DBConnectionType connectionType, Loader loader, TitanGraph graph) {
61 super(style, loader, connectionType, InMemoryGraphSingleton.getInstance(graph));
66 * Instantiates a new titan DB engine.
75 public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, TitanGraph graph) {
78 this.singleton = InMemoryGraphSingleton.getInstance(graph);
84 public QueryEngine getQueryEngine() {
86 if (style.equals(QueryStyle.TRAVERSAL)) {
88 GraphTraversalSource traversalSource = graph.traversal();
89 return new GraphTraversalQueryEngine(traversalSource);
92 throw new IllegalArgumentException("Query Engine type not recognized");
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());
105 throw new IllegalArgumentException("Query Builder type is Not recognized");
114 public boolean setListProperty(Vertex v, String name, List<?> objs) {
116 // clear out list full replace style
118 Iterator<VertexProperty<Object>> iterator = v.properties(name);
119 while (iterator.hasNext()) {
120 iterator.next().remove();
123 for (Object obj : objs) {
124 v.property(name, obj);
134 public List<Object> getListProperty(Vertex v, String name) {
136 List<Object> result = new ArrayList<>();
138 Iterator<VertexProperty<Object>> iterator = v.properties(name);
140 while (iterator.hasNext()) {
141 result.add(iterator.next().value());
144 if (result.isEmpty()) {
153 public QueryBuilder<Vertex> getQueryBuilder() {
154 return getQueryBuilder(this.loader);
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());
166 throw new IllegalArgumentException("Query Builder type not recognized");
172 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
173 return getQueryBuilder(this.loader, start);
176 public GraphTraversalSource getTraversalSource() {
177 return graph.traversal();
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);
189 throw new IllegalArgumentException("Query Builder type not recognized");
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()));