c29294684e7223424bed5a61d5743c9b51a9bc7b
[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 children, grandchildren, etc of start
57          *
58          * @param start the start vertex
59          * @return the list of child/grandchild/etc vertices
60          */
61         public abstract List<Vertex> findAllChildren(Vertex start);
62         
63         /**
64          * Finds all immediate children of start (no grandchildren or so forth) of the given type
65          * @param start - the start vertex
66          * @param type - the desired aai-node-type
67          * @return the list of immediate child vertices of given type
68          */
69         public abstract List<Vertex> findChildrenOfType(Vertex start, String type);
70         
71         /**
72          * Finds all immediate children of start (no grandchildren or so forth)
73          * @param start - the start vertex
74          * @return the list of immediate child vertices
75          */
76         public abstract List<Vertex> findChildren(Vertex start);
77         
78         /**
79          * Find all vertices that should be deleted in a cascade from a delete of start
80          *
81          * @param start - the start vertex
82          * @return the list of vertices to be deleted when start is deleted
83          */
84         public abstract List<Vertex> findDeletable(Vertex start);
85         
86         /**
87          * Finds the subgraph under start, including cousins as well as start's children/grandchildren/etc.
88          * More specifically, this includes start, all its descendants, start's cousins, and start's
89          * descendants' cousins (but not any of the cousins' cousins or descendants), and the edges
90          * connecting them.
91          * 
92          * @param start - the start vertex
93          * @return - Tree containing nodes and edges of the subgraph 
94          */
95         public Tree<Element> findSubGraph(Vertex start) {
96                 return findSubGraph(start, AAIProperties.MAXIMUM_DEPTH, false);
97         }
98         
99         /**
100          * Finds the subgraph under start, including cousins as well as start's children/grandchildren/etc.
101          * More specifically, this includes start, all its descendants, start's cousins, and start's
102          * descendants' cousins (but not any of the cousins' cousins or descendants), and the edges
103          * connecting them.
104          * 
105          * @param start - the start vertex
106          * @param iterations - depth of the subgraph, this limits how many generations of 
107          *                                              descendants are included
108          * @param nodeOnly - if true the subgraph will NOT include the cousins
109          * @return Tree containing nodes and edges of the subgraph 
110          */
111         public abstract Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly);
112         
113         /**
114          * Find vertices of type nodeType related to start by edges of the given
115          *  direction and label.
116          *
117          * @param start - the start vertex
118          * @param direction - the direction of edges to traverse from start
119          * @param label - the label of edges to traverse from start
120          * @param nodeType - the node type the results should be
121          * @return the list of related vertices
122          */
123         public abstract List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType);
124
125         /**
126          * Finds cousin edges connecting start to other vertices only of types defined in an old version.
127          * The idea is that if a user is using an old version, they won't understand any new node types in
128          * subsequent versions. Thus, revealing edges to new types will cause problems. This methods 
129          * filters any such edges out.
130          * 
131          * @param start - the start vertex
132          * @param loader - loader for retrieving the list of allowed node types for the desired version
133          *                                      (version is set when the loader was instantiated)
134          * @return list of cousin edges between start and any node types understood by the version specified in loader
135          */
136         public abstract List<Edge> findEdgesForVersion(Vertex start, Loader loader);
137         
138         /**
139          * Finds all cousins of start. 
140          * 
141          * @param start - the start vertex
142          * @return list of start's cousin vertices
143          */
144         public abstract List<Vertex> findCousinVertices(Vertex start);
145
146         public abstract double getDBTimeMsecs();
147
148 }