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;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
32 import org.onap.aai.dbmap.DBConnectionType;
33 import org.onap.aai.introspection.Loader;
34 import org.onap.aai.query.builder.GremlinTraversal;
35 import org.onap.aai.query.builder.GremlinUnique;
36 import org.onap.aai.query.builder.QueryBuilder;
37 import org.onap.aai.query.builder.TraversalQuery;
38 import org.onap.aai.serialization.db.InMemoryGraphSingleton;
39 import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine;
40 import org.onap.aai.serialization.engines.query.QueryEngine;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
43 import org.janusgraph.core.JanusGraph;
45 public class InMemoryDBEngine extends TransactionalGraphEngine {
48 * Instantiates a new JanusGraph DB engine.
55 private JanusGraph graph = null;
57 private static final TransactionalGraphEngine.Admin admin = null;
59 public InMemoryDBEngine(QueryStyle style, DBConnectionType connectionType, Loader loader, JanusGraph graph) {
60 super(style, loader, connectionType, InMemoryGraphSingleton.getInstance(graph));
65 * Instantiates a new JanusGraph DB engine.
74 public InMemoryDBEngine(QueryStyle style, Loader loader, boolean connect, JanusGraph graph) {
77 this.singleton = InMemoryGraphSingleton.getInstance(graph);
83 public QueryEngine getQueryEngine() {
85 if (style.equals(QueryStyle.TRAVERSAL)) {
87 GraphTraversalSource traversalSource = graph.traversal();
88 return new GraphTraversalQueryEngine(traversalSource);
91 throw new IllegalArgumentException("Query Engine type not recognized");
97 public QueryBuilder<Vertex> getQueryBuilder(QueryStyle style, Loader loader) {
98 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
99 return new GremlinTraversal<>(loader, graph.traversal());
100 } else if (style.equals(QueryStyle.TRAVERSAL)) {
101 return new TraversalQuery<>(loader, graph.traversal());
104 throw new IllegalArgumentException("Query Builder type is Not recognized");
113 public boolean setListProperty(Vertex v, String name, List<?> objs) {
115 // clear out list full replace style
117 Iterator<VertexProperty<Object>> iterator = v.properties(name);
118 while (iterator.hasNext()) {
119 iterator.next().remove();
122 for (Object obj : objs) {
123 v.property(name, obj);
133 public List<Object> getListProperty(Vertex v, String name) {
135 List<Object> result = new ArrayList<>();
137 Iterator<VertexProperty<Object>> iterator = v.properties(name);
139 while (iterator.hasNext()) {
140 result.add(iterator.next().value());
143 if (result.isEmpty()) {
152 public QueryBuilder<Vertex> getQueryBuilder() {
153 return getQueryBuilder(this.loader);
157 public QueryBuilder<Vertex> getQueryBuilder(Loader loader) {
158 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
159 return new GremlinTraversal<>(loader, this.asAdmin().getTraversalSource());
160 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
161 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource());
162 } else if (style.equals(QueryStyle.TRAVERSAL)) {
163 return new TraversalQuery<>(loader, graph.traversal());
165 throw new IllegalArgumentException("Query Builder type not recognized");
171 public QueryBuilder<Vertex> getQueryBuilder(Vertex start) {
172 return getQueryBuilder(this.loader, start);
175 public GraphTraversalSource getTraversalSource() {
176 return graph.traversal();
180 public QueryBuilder<Vertex> getQueryBuilder(Loader loader, Vertex start) {
181 if (style.equals(QueryStyle.GREMLIN_TRAVERSAL)) {
182 return new GremlinTraversal<>(loader, graph.traversal(), start);
183 } else if (style.equals(QueryStyle.GREMLIN_UNIQUE)) {
184 return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start);
185 } else if (style.equals(QueryStyle.TRAVERSAL)) {
186 return new TraversalQuery<>(loader, graph.traversal(), start);
188 throw new IllegalArgumentException("Query Builder type not recognized");
194 public Graph startTransaction() {
195 if (this.tx() == null) {
196 this.currentTx = graph.newTransaction();
197 this.currentTraversal = this.tx().traversal();
198 this.readOnlyTraversal = this.tx()
199 .traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));