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.query;
25 import static org.onap.aai.serialization.db.AAIDirection.IN;
26 import static org.onap.aai.serialization.db.AAIDirection.NONE;
27 import static org.onap.aai.serialization.db.AAIDirection.OUT;
28 import static org.onap.aai.serialization.db.EdgeProperty.CONTAINS;
29 import static org.onap.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
31 import java.util.List;
34 import org.apache.tinkerpop.gremlin.process.traversal.P;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
37 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
38 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
39 import org.apache.tinkerpop.gremlin.structure.Direction;
40 import org.apache.tinkerpop.gremlin.structure.Edge;
41 import org.apache.tinkerpop.gremlin.structure.Element;
42 import org.apache.tinkerpop.gremlin.structure.Vertex;
43 import org.onap.aai.db.props.AAIProperties;
44 import org.onap.aai.introspection.Loader;
47 * This class needs some big explanation despite its compact size.
48 * This controls all the queries performed by the CRUD API in A&AI.
49 * findParents, findChildren, and findDeletable require special attention
50 * These methods use 'repeat'. You cannot use 'emit' with repeat currently
51 * as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
52 * it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
55 public class GraphTraversalQueryEngine extends QueryEngine {
58 * Instantiates a new graph traversal query engine.
60 * @param graphEngine the graph engine
62 public GraphTraversalQueryEngine(GraphTraversalSource g) {
70 public List<Vertex> findParents(Vertex start) {
72 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()));
80 public List<Vertex> findAllChildren(Vertex start) {
82 GraphTraversal<Vertex, Vertex> pipe = this.g
83 .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
90 public List<Vertex> findChildrenOfType(Vertex start, String type) {
91 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
92 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
93 __.inE().has(CONTAINS.toString(), IN.toString()).outV()
94 ).has(AAIProperties.NODE_TYPE, type).dedup();
99 public List<Vertex> findChildren(Vertex start) {
100 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
101 __.outE().has(CONTAINS.toString(), OUT.toString()),
102 __.inE().has(CONTAINS.toString(), IN.toString())
105 return pipe.toList();
112 public List<Vertex> findDeletable(Vertex start) {
113 GraphTraversal<Vertex, Vertex> pipe = this.g
114 .V(start).emit(v -> true).repeat(
117 __.has(CONTAINS.toString(), OUT.toString()),
118 __.has(DELETE_OTHER_V.toString(), OUT.toString())
121 __.has(CONTAINS.toString(), IN.toString()),
122 __.has(DELETE_OTHER_V.toString(), IN.toString())
127 return pipe.toList();
134 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
135 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
150 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
151 return pipe.toList();
155 public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
156 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
158 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
159 __.inE().has(CONTAINS.toString(), IN.toString()).outV())
165 __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
170 return (Tree)t.next();
177 public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
178 final Set<String> objects = loader.getAllObjects().keySet();
179 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
180 __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
181 __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
184 return pipeline.toList();
189 public List<Vertex> findCousinVertices(Vertex start) {
190 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
191 __.inE().has(CONTAINS.toString(), NONE.toString()),
192 __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
194 return pipeline.toList();