Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / GraphTraversalQueryEngine.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
23 import static org.onap.aai.serialization.db.AAIDirection.IN;
24 import static org.onap.aai.serialization.db.AAIDirection.NONE;
25 import static org.onap.aai.serialization.db.AAIDirection.OUT;
26 import static org.onap.aai.serialization.db.EdgeProperty.CONTAINS;
27 import static org.onap.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
28
29 import java.util.List;
30 import java.util.Set;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.P;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
36 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
37 import org.apache.tinkerpop.gremlin.structure.Direction;
38 import org.apache.tinkerpop.gremlin.structure.Edge;
39 import org.apache.tinkerpop.gremlin.structure.Element;
40 import org.apache.tinkerpop.gremlin.structure.Vertex;
41 import org.onap.aai.db.props.AAIProperties;
42 import org.onap.aai.introspection.Loader;
43 import org.onap.aai.logging.StopWatch;
44
45 /*
46  * This class needs some big explanation despite its compact size.
47  * This controls all the queries performed by the CRUD API in A&AI. 
48  * findParents, findChildren, and findDeletable require special attention
49  *   These methods use 'repeat'. You cannot use 'emit' with repeat currently
50  *   as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
51  *   it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
52  * 
53  */
54 public class GraphTraversalQueryEngine extends QueryEngine {
55
56         /**
57          * Instantiates a new graph traversal query engine.
58          *
59          * @param graphEngine the graph engine
60          */
61         public GraphTraversalQueryEngine(GraphTraversalSource g) {
62                 super(g);
63         }
64
65         /**
66          * {@inheritDoc}
67          */
68         @Override
69         public List<Vertex> findParents(Vertex start) {
70                 try {
71                         StopWatch.conditionalStart();
72                         @SuppressWarnings("unchecked")
73                         final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.union(__.inE().has(CONTAINS.toString(), OUT.toString()).outV(), __.outE().has(CONTAINS.toString(), IN.toString()).inV()));
74                         return pipe.toList();
75                 }
76                 finally {
77                         dbTimeMsecs += StopWatch.stopIfStarted();
78                 }
79         }
80
81         /**
82          * {@inheritDoc}
83          */
84         @Override
85         public List<Vertex> findAllChildren(Vertex start) {
86                 
87                 @SuppressWarnings("unchecked")
88                 GraphTraversal<Vertex, Vertex> pipe =  this.g
89                                 .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
90                 
91
92                 return pipe.toList();
93                 
94         }
95
96         /**
97          * {@inheritDoc}
98          */
99         @Override
100         public List<Vertex> findChildrenOfType(Vertex start, String type) {
101                 @SuppressWarnings("unchecked")
102                 GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
103                                         __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
104                                         __.inE().has(CONTAINS.toString(), IN.toString()).outV()
105                                 ).has(AAIProperties.NODE_TYPE, type).dedup();
106  
107                 return pipe.toList();
108         }
109         
110         /**
111          * {@inheritDoc}
112          */
113         @Override
114         public List<Vertex> findChildren(Vertex start) {
115                 @SuppressWarnings("unchecked")
116                 GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
117                                         __.outE().has(CONTAINS.toString(), OUT.toString()),
118                                         __.inE().has(CONTAINS.toString(), IN.toString())
119                                 ).otherV().dedup();
120  
121                 return pipe.toList();
122         }
123
124         /**
125          * {@inheritDoc}
126          */
127         @Override
128         public List<Vertex> findDeletable(Vertex start) {
129                 @SuppressWarnings("unchecked")
130                 GraphTraversal<Vertex, Vertex> pipe = this.g
131                                 .V(start).emit(v -> true).repeat(
132                                         __.union(
133                                                 __.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
134                                                 __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()
135                                         )
136                                 ).dedup();
137                 
138                 return pipe.toList();
139         }
140
141         /**
142          * {@inheritDoc}
143          */
144         @Override
145         public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
146                 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
147                 switch (direction) {
148                         case OUT:
149                                 pipe.out(label);
150                                 break;
151                         case IN:
152                                 pipe.in(label);
153                                 break;
154                         case BOTH:
155                                 pipe.both(label);
156                                 break;
157                          default:
158                                 break;
159                 }
160                 
161                 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
162                 return pipe.toList();
163         }
164         
165         @Override
166         public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
167                 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
168                                 __.union(
169                                         __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
170                                         __.inE().has(CONTAINS.toString(), IN.toString()).outV())
171                                 );
172                         
173                 if (!nodeOnly) {
174                         t.union(
175                                         __.identity(),
176                                         __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
177                         );
178                 }
179                 t.tree();
180                 if (t.hasNext()) {
181                         return (Tree)t.next();
182                 } else {
183                         return new Tree();
184                 }
185         }
186
187         @Override
188         public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
189                 final Set<String> objects = loader.getAllObjects().keySet();
190                 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
191                                 __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
192                                 __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
193                         ).dedup();
194                 
195                 return pipeline.toList();
196         }
197
198
199         @Override
200         public List<Vertex> findCousinVertices(Vertex start) {
201                 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
202                                 __.inE().has(CONTAINS.toString(), NONE.toString()),
203                                 __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
204                                 
205                 return pipeline.toList();
206         }
207         
208         public double getDBTimeMsecs() {
209                 return (dbTimeMsecs);
210         }
211 }
212