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=========================================================
21 package org.onap.aai.serialization.engines;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 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 import org.janusgraph.core.JanusGraph;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.query.builder.*;
35 import org.onap.aai.serialization.db.InMemoryGraphSingleton;
36 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
37 import org.onap.aai.serialization.engines.query.QueryEngine;
39 public class InMemoryDBEngine extends TransactionalGraphEngine {
42 * Instantiates a new JanusGraph DB engine.
49 private JanusGraph graph = null;
51 public InMemoryDBEngine(QueryStyle style, Loader loader, JanusGraph graph) {
52 super(style, loader, InMemoryGraphSingleton.getInstance(graph));
57 * Instantiates a new JanusGraph DB engine.
66 public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, JanusGraph graph) {
69 this.singleton = InMemoryGraphSingleton.getInstance(graph);
75 public QueryEngine getQueryEngine() {
77 if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) {
79 GraphTraversalSource traversalSource = graph.traversal();
80 return new GraphTraversalQueryEngine(traversalSource);
83 throw new IllegalArgumentException("Query Engine type not recognized");
89 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
90 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
91 return new GremlinTraversal<>(loader, graph.traversal());
92 } else if (style.equals(QueryStyle.TRAVERSAL)) {
93 return new TraversalQuery<>(loader, graph.traversal());
94 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
95 return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
97 throw new IllegalArgumentException("Query Builder type is Not recognized");
106 public boolean setListProperty(Vertex v, String name, List<?> objs) {
108 // clear out list full replace style
110 Iterator<VertexProperty<Object>> iterator = v.properties(name);
111 while (iterator.hasNext()) {
112 iterator.next().remove();
115 for (Object obj : objs) {
116 v.property(name, obj);
126 public List<Object> getListProperty(Vertex v, String name) {
128 List<Object> result = new ArrayList<>();
130 Iterator<VertexProperty<Object>> iterator = v.properties(name);
132 while (iterator.hasNext()) {
133 result.add(iterator.next().value());
136 if (result.isEmpty()) {
145 public QueryBuilder<Vertex> getQueryBuilder() {
146 return getQueryBuilder(this.loader);
150 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
151 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
152 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
153 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
154 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
155 } else if (style.equals(QueryStyle.TRAVERSAL)) {
156 return new TraversalQuery<>(loader, graph.traversal());
157 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
158 return new TraversalURIOptimizedQuery<>(loader, graph.traversal());
160 throw new IllegalArgumentException("Query Builder type not recognized");
166 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
167 return getQueryBuilder(this.loader, start);
170 public GraphTraversalSource getTraversalSource() {
171 return graph.traversal();
175 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
176 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
177 return new GremlinTraversal<>(loader, graph.traversal(), start);
178 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
179 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
180 } else if (style.equals(QueryStyle.TRAVERSAL)) {
181 return new TraversalQuery<>(loader, graph.traversal(), start);
182 } else if (style.equals(QueryStyle.TRAVERSAL_URI)) {
183 return new TraversalURIOptimizedQuery<>(loader, graph.traversal(), start);
185 throw new IllegalArgumentException("Query Builder type not recognized");
191 public Graph startTransaction() {
192 if (this.tx() == null) {
193 this.currentTx = graph.newTransaction();
194 this.currentTraversal = this.tx().traversal();
195 this.readOnlyTraversal = this.tx().traversal().withStrategies(ReadOnlyStrategy.instance());