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 java.util.List;
25 import org.apache.tinkerpop.gremlin.process.traversal.Path;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
28 import org.apache.tinkerpop.gremlin.structure.Direction;
29 import org.apache.tinkerpop.gremlin.structure.Edge;
30 import org.apache.tinkerpop.gremlin.structure.Element;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.onap.aai.db.props.AAIProperties;
33 import org.onap.aai.introspection.Loader;
35 public abstract class QueryEngine {
37 protected final GraphTraversalSource g;
38 protected double dbTimeMsecs = 0;
41 * Instantiates a new query engine.
43 * @param g graph traversal source to traverse the graph
45 public QueryEngine(GraphTraversalSource g) {
50 * Finds all the parents/grandparents/etc of the given start node.
52 * @param start - the start vertex whose parent chain you want
53 * @return the list of start and start's parent, grandparent, etc, in
54 * order (ie {start, parent, grandparent, etc}
56 public abstract List<Vertex> findParents(Vertex start);
59 * Finds all the parents/grandparents/etc of the given start node.
61 * This method should be used in place of the #findParents(Vertex)
62 * as since the aai-uri is added the lookup for finding the parents
63 * using the given list of aai-uri will be much faster than using
64 * a traversal to follow a start vertex and keep repeating since
65 * as the number of different type of edges keeps growing that call
66 * will be more expensive than using the aai-uri's as they are fast lookup
68 * @param uris - list of the uris representing the aai-uris of
69 * parent, grandparent, etc
70 * @return the list of start and start's parent, grandparent, etc, in
71 * order (ie {start, parent, grandparent, etc}
73 public abstract List<Vertex> findParents(String[] uris);
76 * Finds all children, grandchildren, etc of start
78 * @param start the start vertex
79 * @return the list of child/grandchild/etc vertices
81 public abstract List<Vertex> findAllChildren(Vertex start);
84 * Finds all immediate children of start (no grandchildren or so forth) of the given type
86 * @param start - the start vertex
87 * @param type - the desired aai-node-type
88 * @return the list of immediate child vertices of given type
90 public abstract List<Vertex> findChildrenOfType(Vertex start, String type);
93 * Finds all immediate children of start (no grandchildren or so forth)
95 * @param start - the start vertex
96 * @return the list of immediate child vertices
98 public abstract List<Vertex> findChildren(Vertex start);
101 * Find all vertices that should be deleted in a cascade from a delete of start
103 * @param start - the start vertex
104 * @return the list of vertices to be deleted when start is deleted
106 public abstract List<Vertex> findDeletable(Vertex start);
109 * Find all vertices that should be deleted in a cascade from a delete of start vertexes
111 * @param startVertexes Specifies the list of start vertexes
113 * @return the list of vertices to be deleted when start list of vertexes is deleted
115 public abstract List<Vertex> findDeletable(List<Vertex> startVertexes);
118 * Finds the subgraph under start, including cousins as well as start's children/grandchildren/etc.
119 * More specifically, this includes start, all its descendants, start's cousins, and start's
120 * descendants' cousins (but not any of the cousins' cousins or descendants), and the edges
123 * @param start - the start vertex
124 * @return - Tree containing nodes and edges of the subgraph
126 public Tree<Element> findSubGraph(Vertex start) {
127 return findSubGraph(start, AAIProperties.MAXIMUM_DEPTH, false);
131 * Finds the subgraph under start, including cousins as well as start's children/grandchildren/etc.
132 * More specifically, this includes start, all its descendants, start's cousins, and start's
133 * descendants' cousins (but not any of the cousins' cousins or descendants), and the edges
136 * @param start - the start vertex
137 * @param iterations - depth of the subgraph, this limits how many generations of
138 * descendants are included
139 * @param nodeOnly - if true the subgraph will NOT include the cousins
140 * @return Tree containing nodes and edges of the subgraph
142 public abstract Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly);
145 * Find vertices of type nodeType related to start by edges of the given
146 * direction and label.
148 * @param start - the start vertex
149 * @param direction - the direction of edges to traverse from start
150 * @param label - the label of edges to traverse from start
151 * @param nodeType - the node type the results should be
152 * @return the list of related vertices
154 public abstract List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType);
157 * Finds cousin edges connecting start to other vertices only of types defined in an old version.
158 * The idea is that if a user is using an old version, they won't understand any new node types in
159 * subsequent versions. Thus, revealing edges to new types will cause problems. This methods
160 * filters any such edges out.
162 * @param start - the start vertex
163 * @param loader - loader for retrieving the list of allowed node types for the desired version
164 * (version is set when the loader was instantiated)
165 * @return list of cousin edges between start and any node types understood by the version specified in loader
167 public abstract List<Edge> findEdgesForVersion(Vertex start, Loader loader);
170 * Finds all cousins of start.
172 * @param start - the start vertex
173 * @return list of start's cousin vertices
175 public abstract List<Vertex> findCousinVertices(Vertex start, String... labels);
177 public abstract List<Path> findCousinsAsPath(Vertex start);
179 public abstract double getDBTimeMsecs();