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;
24 import org.apache.tinkerpop.gremlin.process.traversal.P;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
28 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.apache.tinkerpop.gremlin.structure.Element;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.openecomp.aai.db.props.AAIProperties;
34 import org.openecomp.aai.introspection.Loader;
36 import java.util.List;
39 import static org.openecomp.aai.serialization.db.AAIDirection.*;
40 import static org.openecomp.aai.serialization.db.EdgeProperty.CONTAINS;
41 import static org.openecomp.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
44 * This class needs some big explanation despite its compact size.
45 * This controls all the queries performed by the CRUD API in A&AI.
46 * findParents, findChildren, and findDeletable require special attention
47 * These methods use 'repeat'. You cannot use 'emit' with repeat currently
48 * as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
49 * it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
52 public class GraphTraversalQueryEngine extends QueryEngine {
55 * Instantiates a new graph traversal query engine.
57 * @param graphEngine the graph engine
59 public GraphTraversalQueryEngine(GraphTraversalSource g) {
67 public List<Vertex> findParents(Vertex start) {
69 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 public List<Vertex> findAllChildren(Vertex start) {
79 GraphTraversal<Vertex, Vertex> pipe = this.g
80 .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
87 public List<Vertex> findChildrenOfType(Vertex start, String type) {
88 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
89 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
90 __.inE().has(CONTAINS.toString(), IN.toString()).outV()
91 ).has(AAIProperties.NODE_TYPE, type).dedup();
96 public List<Vertex> findChildren(Vertex start) {
97 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
98 __.outE().has(CONTAINS.toString(), OUT.toString()),
99 __.inE().has(CONTAINS.toString(), IN.toString())
102 return pipe.toList();
109 public List<Vertex> findDeletable(Vertex start) {
110 GraphTraversal<Vertex, Vertex> pipe = this.g
111 .V(start).emit(v -> true).repeat(
114 __.has(CONTAINS.toString(), OUT.toString()),
115 __.has(DELETE_OTHER_V.toString(), OUT.toString())
118 __.has(CONTAINS.toString(), IN.toString()),
119 __.has(DELETE_OTHER_V.toString(), IN.toString())
124 return pipe.toList();
131 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
132 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
147 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
148 return pipe.toList();
152 public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
153 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
155 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
156 __.inE().has(CONTAINS.toString(), IN.toString()).outV())
162 __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
167 return (Tree)t.next();
174 public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
175 final Set<String> objects = loader.getAllObjects().keySet();
176 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
177 __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
178 __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
181 return pipeline.toList();
186 public List<Vertex> findCousinVertices(Vertex start) {
187 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
188 __.inE().has(CONTAINS.toString(), NONE.toString()),
189 __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
191 return pipeline.toList();