110f86285d60bfb9259463c830b206b2cd3fef52
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / QueryEngine.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.serialization.engines.query;
21
22 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
24 import org.apache.tinkerpop.gremlin.structure.Direction;
25 import org.apache.tinkerpop.gremlin.structure.Edge;
26 import org.apache.tinkerpop.gremlin.structure.Element;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.onap.aai.db.props.AAIProperties;
29 import org.onap.aai.introspection.Loader;
30
31 import java.util.List;
32
33 public abstract class QueryEngine {
34
35         final protected GraphTraversalSource g;
36         protected double dbTimeMsecs = 0;
37         /**
38          * Instantiates a new query engine.
39          *
40          * @param graphEngine the graph engine
41          */
42         public QueryEngine (GraphTraversalSource g) {
43                 this.g = g;
44         }
45
46         /**
47          * Finds all the parents/grandparents/etc of the given start node.
48          *
49          * @param start - the start vertex whose parent chain you want
50          * @return the list of start and start's parent, grandparent, etc, in
51          *                      order (ie {start, parent, grandparent, etc}
52          */
53         public abstract List<Vertex> findParents(Vertex start);
54
55     /**
56      * Finds all the parents/grandparents/etc of the given start node.
57      *
58      * This method should be used in place of the #findParents(Vertex)
59      * as since the aai-uri is added the lookup for finding the parents
60      * using the given list of aai-uri will be much faster than using
61      * a traversal to follow a start vertex and keep repeating since
62      * as the number of different type of edges keeps growing that call
63      * will be more expensive than using the aai-uri's as they are fast lookup
64      *
65      * @param uris  - list of the uris representing the aai-uris of
66      *                 parent, grandparent, etc
67      * @return the list of start and start's parent, grandparent, etc, in
68      *                  order (ie {start, parent, grandparent, etc}
69      */
70     public abstract List<Vertex> findParents(String [] uris);
71
72         /**
73          * Finds all children, grandchildren, etc of start
74          *
75          * @param start the start vertex
76          * @return the list of child/grandchild/etc vertices
77          */
78         public abstract List<Vertex> findAllChildren(Vertex start);
79
80         /**
81          * Finds all immediate children of start (no grandchildren or so forth) of the given type
82          * @param start - the start vertex
83          * @param type - the desired aai-node-type
84          * @return the list of immediate child vertices of given type
85          */
86         public abstract List<Vertex> findChildrenOfType(Vertex start, String type);
87
88         /**
89          * Finds all immediate children of start (no grandchildren or so forth)
90          * @param start - the start vertex
91          * @return the list of immediate child vertices
92          */
93         public abstract List<Vertex> findChildren(Vertex start);
94
95         /**
96          * Find all vertices that should be deleted in a cascade from a delete of start
97          *
98          * @param start - the start vertex
99          * @return the list of vertices to be deleted when start is deleted
100          */
101         public abstract List<Vertex> findDeletable(Vertex start);
102
103         /**
104          * Finds the subgraph under start, including cousins as well as start's children/grandchildren/etc.
105          * More specifically, this includes start, all its descendants, start's cousins, and start's
106          * descendants' cousins (but not any of the cousins' cousins or descendants), and the edges
107          * connecting them.
108          *
109          * @param start - the start vertex
110          * @return - Tree containing nodes and edges of the subgraph
111          */
112         public Tree<Element> findSubGraph(Vertex start) {
113                 return findSubGraph(start, AAIProperties.MAXIMUM_DEPTH, false);
114         }
115
116         /**
117          * Finds the subgraph under start, including cousins as well as start's children/grandchildren/etc.
118          * More specifically, this includes start, all its descendants, start's cousins, and start's
119          * descendants' cousins (but not any of the cousins' cousins or descendants), and the edges
120          * connecting them.
121          *
122          * @param start - the start vertex
123          * @param iterations - depth of the subgraph, this limits how many generations of
124          *                                              descendants are included
125          * @param nodeOnly - if true the subgraph will NOT include the cousins
126          * @return Tree containing nodes and edges of the subgraph
127          */
128         public abstract Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly);
129
130         /**
131          * Find vertices of type nodeType related to start by edges of the given
132          *  direction and label.
133          *
134          * @param start - the start vertex
135          * @param direction - the direction of edges to traverse from start
136          * @param label - the label of edges to traverse from start
137          * @param nodeType - the node type the results should be
138          * @return the list of related vertices
139          */
140         public abstract List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType);
141
142         /**
143          * Finds cousin edges connecting start to other vertices only of types defined in an old version.
144          * The idea is that if a user is using an old version, they won't understand any new node types in
145          * subsequent versions. Thus, revealing edges to new types will cause problems. This methods
146          * filters any such edges out.
147          *
148          * @param start - the start vertex
149          * @param loader - loader for retrieving the list of allowed node types for the desired version
150          *                                      (version is set when the loader was instantiated)
151          * @return list of cousin edges between start and any node types understood by the version specified in loader
152          */
153         public abstract List<Edge> findEdgesForVersion(Vertex start, Loader loader);
154
155         /**
156          * Finds all cousins of start.
157          *
158          * @param start - the start vertex
159          * @return list of start's cousin vertices
160          */
161         public abstract List<Vertex> findCousinVertices(Vertex start, String... labels);
162
163         public abstract double getDBTimeMsecs();
164
165 }