2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.serialization.engines.query;
23 import java.util.List;
26 import org.apache.tinkerpop.gremlin.process.traversal.P;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
29 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
30 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
31 import org.apache.tinkerpop.gremlin.structure.Direction;
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.apache.tinkerpop.gremlin.structure.Element;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
36 import org.openecomp.aai.db.props.AAIProperties;
37 import org.openecomp.aai.introspection.Loader;
40 * This class needs some big explanation despite its compact size.
41 * This controls all the queries performed by the CRUD API in A&AI.
42 * findParents, findChildren, and findDeletable require special attention
43 * These methods use 'repeat'. You cannot use 'emit' with repeat currently
44 * as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
45 * it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
48 public class GraphTraversalQueryEngine extends QueryEngine {
51 * Instantiates a new graph traversal query engine.
53 * @param graphEngine the graph engine
55 public GraphTraversalQueryEngine(GraphTraversalSource g) {
63 public List<Vertex> findParents(Vertex start) {
65 final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.inE().has("isParent", true).outV());
73 public List<Vertex> findAllChildren(Vertex start) {
75 GraphTraversal<Vertex, Vertex> pipe = this.g
76 .V(start).emit(v -> true).repeat(__.outE().has("isParent", true).inV());
83 public List<Vertex> findChildrenOfType(Vertex start, String type) {
84 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
85 __.outE().has("isParent", true).inV(),
86 __.inE().has("isParent-REV", true).outV()
87 ).has(AAIProperties.NODE_TYPE, type).dedup();
92 public List<Vertex> findChildren(Vertex start) {
93 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
94 __.outE().has("isParent", true),
95 __.inE().has("isParent-REV", true)
105 public List<Vertex> findDeletable(Vertex start) {
106 GraphTraversal<Vertex, Vertex> pipe = this.g
107 .V(start).emit(v -> true).repeat(__.outE().or(
108 __.has("isParent", true),
109 __.has("hasDelTarget", true)).inV());
111 return pipe.toList();
118 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
119 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
134 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
135 return pipe.toList();
139 public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
140 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
141 __.outE().has("isParent", true).inV());
146 __.bothE().has("isParent", false).dedup().otherV()
151 return (Tree)t.next();
158 public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
159 final Set<String> objects = loader.getAllObjects().keySet();
160 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
161 __.inE().has("isParent", false).has("isParent-REV", false).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
162 __.outE().has("isParent", false).has("isParent-REV", false).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
165 return pipeline.toList();
170 public List<Vertex> findCousinVertices(Vertex start) {
171 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
172 __.inE().has("isParent", false).has("isParent-REV", false),
173 __.outE().has("isParent", false).has("isParent-REV", false)).otherV().dedup();
175 return pipeline.toList();