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.edges.enums.AAIDirection.IN;
24 import static org.onap.aai.edges.enums.AAIDirection.NONE;
25 import static org.onap.aai.edges.enums.AAIDirection.OUT;
26 import static org.onap.aai.edges.enums.EdgeField.PRIVATE;
27 import static org.onap.aai.edges.enums.EdgeProperty.CONTAINS;
28 import static org.onap.aai.edges.enums.EdgeProperty.DELETE_OTHER_V;
30 import java.util.List;
33 import org.apache.tinkerpop.gremlin.process.traversal.Order;
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;
45 import org.onap.aai.logging.StopWatch;
48 * This class needs some big explanation despite its compact size.
49 * This controls all the queries performed by the CRUD API in A&AI.
50 * findParents, findChildren, and findDeletable require special attention
51 * These methods use 'repeat'. You cannot use 'emit' with repeat currently
52 * as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
53 * it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
56 public class GraphTraversalQueryEngine extends QueryEngine {
59 * Instantiates a new graph traversal query engine.
61 * @param g graph traversal source to traverse the graph
63 public GraphTraversalQueryEngine(GraphTraversalSource g) {
71 public List<Vertex> findParents(Vertex start) {
73 StopWatch.conditionalStart();
75 @SuppressWarnings("unchecked")
76 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 dbTimeMsecs += StopWatch.stopIfStarted();
88 public List<Vertex> findParents(String[] uris) {
90 StopWatch.conditionalStart();
91 final GraphTraversal<Vertex, Vertex> pipe = this.g.V()
92 .has(AAIProperties.AAI_URI, P.within(uris))
93 .order().by(AAIProperties.AAI_URI, Order.decr);
97 dbTimeMsecs += StopWatch.stopIfStarted();
105 public List<Vertex> findAllChildren(Vertex start) {
107 @SuppressWarnings("unchecked")
108 GraphTraversal<Vertex, Vertex> pipe = this.g
109 .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
112 return pipe.toList();
120 public List<Vertex> findChildrenOfType(Vertex start, String type) {
121 @SuppressWarnings("unchecked")
122 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
123 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
124 __.inE().has(CONTAINS.toString(), IN.toString()).outV()
125 ).has(AAIProperties.NODE_TYPE, type).dedup();
127 return pipe.toList();
134 public List<Vertex> findChildren(Vertex start) {
135 @SuppressWarnings("unchecked")
136 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union(
137 __.outE().has(CONTAINS.toString(), OUT.toString()),
138 __.inE().has(CONTAINS.toString(), IN.toString())
141 return pipe.toList();
148 public List<Vertex> findDeletable(Vertex start) {
150 StopWatch.conditionalStart();
151 @SuppressWarnings("unchecked")
152 GraphTraversal<Vertex, Vertex> pipe = this.g
153 .V(start).emit(v -> true).repeat(
155 __.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
156 __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()
160 return pipe.toList();
162 dbTimeMsecs += StopWatch.stopIfStarted();
170 public List<Vertex> findDeletable(List<Vertex> startVertexes) {
172 StopWatch.conditionalStart();
173 Vertex[] vertices = new Vertex[startVertexes.size()];
174 vertices = startVertexes.toArray(vertices);
175 GraphTraversal<Vertex, Vertex> pipe = this.g
176 .V(vertices).emit(v -> true).repeat(
178 __.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
179 __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()
183 return pipe.toList();
186 dbTimeMsecs += StopWatch.stopIfStarted();
194 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
195 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
210 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
211 return pipe.toList();
215 public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
216 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
218 __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
219 __.inE().has(CONTAINS.toString(), IN.toString()).outV())
225 __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
230 return (Tree)t.next();
237 public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
238 // From the given start vertex find both the
239 // out edges that has property CONTAINS set to NONE
240 // whose in vertexes has an object that is declared in the oxm
241 // And do the same thing vice versa to get a list of edges
242 // Then check that the edge should not have the property private set to true
243 // and remove the duplicates and return the list of edges
244 final Set<String> objects = loader.getAllObjects().keySet();
245 GraphTraversal<Vertex, Edge> pipeline = this.g
248 __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
249 __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
252 __.has("private", true)
256 return pipeline.toList();
261 public List<Vertex> findCousinVertices(Vertex start, String... labels) {
262 // Start at the given vertex
263 // Do a union to copy the start vertex to be run against all
264 // so for the start vertex it gets all of in edges that contains other v set to none
265 // and also all the other out edges with contains other v set to none
266 // And filter the edges based on the property private not set
267 // so that means it will be a regular edge
268 // and find the other end of the vertex so if setup like this:
271 GraphTraversal<Vertex, Vertex> pipeline = this.g
274 __.inE(labels).has(CONTAINS.toString(), NONE.toString()),
275 __.outE(labels).has(CONTAINS.toString(), NONE.toString())
278 __.has(PRIVATE.toString(), true)
283 return pipeline.toList();
286 public double getDBTimeMsecs() {
287 return (dbTimeMsecs);