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.query;
23 import static org.onap.aai.serialization.db.AAIDirection.IN;
24 import static org.onap.aai.serialization.db.AAIDirection.NONE;
25 import static org.onap.aai.serialization.db.AAIDirection.OUT;
26 import static org.onap.aai.serialization.db.EdgeProperty.CONTAINS;
27 import static org.onap.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
29 import java.util.List;
32 import org.apache.tinkerpop.gremlin.process.traversal.P;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
36 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
37 import org.apache.tinkerpop.gremlin.structure.Direction;
38 import org.apache.tinkerpop.gremlin.structure.Edge;
39 import org.apache.tinkerpop.gremlin.structure.Element;
40 import org.apache.tinkerpop.gremlin.structure.Vertex;
41 import org.onap.aai.db.props.AAIProperties;
42 import org.onap.aai.introspection.Loader;
43 import org.onap.aai.logging.StopWatch;
46 * This class needs some big explanation despite its compact size.
47 * This controls all the queries performed by the CRUD API in A&AI.
48 * findParents, findChildren, and findDeletable require special attention
49 * These methods use 'repeat'. You cannot use 'emit' with repeat currently
50 * as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
51 * it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
54 public class GraphTraversalQueryEngine extends QueryEngine {
57 * Instantiates a new graph traversal query engine.
59 * @param graphEngine the graph engine
61 public GraphTraversalQueryEngine(GraphTraversalSource g) {
69 public List<Vertex> findParents(Vertex start) {
71 StopWatch.conditionalStart();
72 @SuppressWarnings("unchecked")
73 final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.union(__.inE().has(CONTAINS.toString(), OUT.toString()).outV(), __.outE().has(CONTAINS.toString(), IN.toString()).inV()));
77 dbTimeMsecs += StopWatch.stopIfStarted();
85 public List<Vertex> findAllChildren(Vertex start) {
87 @SuppressWarnings("unchecked")
88 GraphTraversal<Vertex, Vertex> pipe = this.g
89 .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
100 public List<Vertex> findChildrenOfType(Vertex start, String type) {
101 @SuppressWarnings("unchecked")
102 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
103 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
104 __.inE().has(CONTAINS.toString(), IN.toString()).outV()
105 ).has(AAIProperties.NODE_TYPE, type).dedup();
107 return pipe.toList();
114 public List<Vertex> findChildren(Vertex start) {
115 @SuppressWarnings("unchecked")
116 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
117 __.outE().has(CONTAINS.toString(), OUT.toString()),
118 __.inE().has(CONTAINS.toString(), IN.toString())
121 return pipe.toList();
128 public List<Vertex> findDeletable(Vertex start) {
129 @SuppressWarnings("unchecked")
130 GraphTraversal<Vertex, Vertex> pipe = this.g
131 .V(start).emit(v -> true).repeat(
133 __.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
134 __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()
138 return pipe.toList();
145 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
146 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
161 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
162 return pipe.toList();
166 public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
167 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
169 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
170 __.inE().has(CONTAINS.toString(), IN.toString()).outV())
176 __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
181 return (Tree)t.next();
188 public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
189 final Set<String> objects = loader.getAllObjects().keySet();
190 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
191 __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
192 __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
195 return pipeline.toList();
200 public List<Vertex> findCousinVertices(Vertex start) {
201 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
202 __.inE().has(CONTAINS.toString(), NONE.toString()),
203 __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
205 return pipeline.toList();
208 public double getDBTimeMsecs() {
209 return (dbTimeMsecs);