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=========================================================
21 package org.onap.aai.serialization.engines.query;
23 import static org.onap.aai.edges.enums.AAIDirection.*;
24 import static org.onap.aai.edges.enums.EdgeField.PRIVATE;
25 import static org.onap.aai.edges.enums.EdgeProperty.CONTAINS;
26 import static org.onap.aai.edges.enums.EdgeProperty.DELETE_OTHER_V;
28 import java.util.List;
31 import org.apache.tinkerpop.gremlin.process.traversal.Order;
32 import org.apache.tinkerpop.gremlin.process.traversal.P;
33 import org.apache.tinkerpop.gremlin.process.traversal.Path;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
36 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
37 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
38 import org.apache.tinkerpop.gremlin.structure.Direction;
39 import org.apache.tinkerpop.gremlin.structure.Edge;
40 import org.apache.tinkerpop.gremlin.structure.Element;
41 import org.apache.tinkerpop.gremlin.structure.Vertex;
42 import org.onap.aai.db.props.AAIProperties;
43 import org.onap.aai.edges.enums.EdgeField;
44 import org.onap.aai.edges.enums.EdgeProperty;
45 import org.onap.aai.introspection.Loader;
46 import org.onap.aai.logging.StopWatch;
49 * This class needs some big explanation despite its compact size.
50 * This controls all the queries performed by the CRUD API in A&AI.
51 * findParents, findChildren, and findDeletable require special attention
52 * These methods use 'repeat'. You cannot use 'emit' with repeat currently
53 * as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
54 * it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
57 public class GraphTraversalQueryEngine extends QueryEngine {
60 * Instantiates a new graph traversal query engine.
62 * @param g graph traversal source to traverse the graph
64 public GraphTraversalQueryEngine(GraphTraversalSource g) {
72 public List<Vertex> findParents(Vertex start) {
74 StopWatch.conditionalStart();
76 @SuppressWarnings("unchecked")
77 final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true)
78 .repeat(__.union(__.inE().has(CONTAINS.toString(), OUT.toString()).outV(),
79 __.outE().has(CONTAINS.toString(), IN.toString()).inV()));
82 dbTimeMsecs += StopWatch.stopIfStarted();
90 public List<Vertex> findParents(String[] uris) {
92 StopWatch.conditionalStart();
93 final GraphTraversal<Vertex, Vertex> pipe =
94 this.g.V().has(AAIProperties.AAI_URI, P.within(uris)).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.V(start).emit(v -> true)
109 .repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
110 __.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)
123 .union(__.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(__.outE().has(CONTAINS.toString(), OUT.toString()),
137 __.inE().has(CONTAINS.toString(), IN.toString())).otherV().dedup();
139 return pipe.toList();
146 public List<Vertex> findDeletable(Vertex start) {
148 StopWatch.conditionalStart();
149 @SuppressWarnings("unchecked")
150 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true)
151 .repeat(__.union(__.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
152 __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()))
155 return pipe.toList();
157 dbTimeMsecs += StopWatch.stopIfStarted();
165 public List<Vertex> findDeletable(List<Vertex> startVertexes) {
167 StopWatch.conditionalStart();
168 Vertex[] vertices = new Vertex[startVertexes.size()];
169 vertices = startVertexes.toArray(vertices);
170 GraphTraversal<Vertex, Vertex> pipe = this.g.V(vertices).emit(v -> true)
171 .repeat(__.union(__.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
172 __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()))
175 return pipe.toList();
177 dbTimeMsecs += StopWatch.stopIfStarted();
185 public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
186 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
201 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
202 return pipe.toList();
206 public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
207 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations)
208 .repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
209 __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
212 t.union(__.identity(), __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV());
216 return (Tree) t.next();
223 public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
224 // From the given start vertex find both the
225 // out edges that has property CONTAINS set to NONE
226 // whose in vertexes has an object that is declared in the oxm
227 // And do the same thing vice versa to get a list of edges
228 // Then check that the edge should not have the property private set to true
229 // and remove the duplicates and return the list of edges
230 final Set<String> objects = loader.getAllObjects().keySet();
231 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start)
232 .union(__.inE().has(CONTAINS.toString(), NONE.toString())
233 .where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
234 __.outE().has(CONTAINS.toString(), NONE.toString())
235 .where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects))))
236 .not(__.has("private", true)).dedup();
238 return pipeline.toList();
242 public List<Vertex> findCousinVertices(Vertex start, String... labels) {
243 // Start at the given vertex
244 // Do a union to copy the start vertex to be run against all
245 // so for the start vertex it gets all of in edges that contains other v set to none
246 // and also all the other out edges with contains other v set to none
247 // And filter the edges based on the property private not set
248 // so that means it will be a regular edge
249 // and find the other end of the vertex so if setup like this:
252 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start)
253 .union(__.inE(labels).has(CONTAINS.toString(), NONE.toString()),
254 __.outE(labels).has(CONTAINS.toString(), NONE.toString()))
255 .not(__.has(PRIVATE.toString(), true)).otherV().dedup();
257 return pipeline.toList();
260 public List<Path> findCousinsAsPath(Vertex start) {
261 return this.g.V(start).bothE().where(__.and(__.has(EdgeProperty.CONTAINS.toString(), NONE.toString()),
262 __.not(__.has(EdgeField.PRIVATE.toString(), true)))).otherV().path().toList();
265 public double getDBTimeMsecs() {
266 return (dbTimeMsecs);