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