f5d478718f30291d9b219cdccbf6b3355f16b120
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / query / builder / QueryBuilder.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.query.builder;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MultivaluedMap;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
33 import org.apache.tinkerpop.gremlin.structure.Edge;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.onap.aai.db.props.AAIProperties;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Introspector;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.parsers.query.QueryParser;
40 import org.onap.aai.parsers.query.QueryParserStrategy;
41 import org.onap.aai.serialization.db.EdgeType;
42
43 /**
44  * The Class QueryBuilder.
45  */
46 public abstract class QueryBuilder<E> implements Iterator<E> {
47
48         protected final GraphTraversalSource source;
49         protected QueryParserStrategy factory = null;
50         protected Loader loader = null;
51         protected boolean optimize = false;
52         protected Vertex start = null;
53         
54         /**
55          * Instantiates a new query builder.
56          *
57          * @param loader the loader
58          */
59         public QueryBuilder(Loader loader, GraphTraversalSource source) {
60                 this.loader = loader;
61                 this.source = source;
62         }
63         
64         /**
65          * Instantiates a new query builder.
66          *
67          * @param loader the loader
68          * @param start the start
69          */
70         public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
71                 this.loader = loader;
72                 this.start = start;
73                 this.source = source;
74         }
75         
76         /**
77          * Gets the vertices by indexed property.
78          *
79          * @param key the key
80          * @param value the value
81          * @return the vertices by indexed property
82          */
83         public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
84                 return this.getVerticesByProperty(key, value);
85         }
86         
87         /**
88          * Gets the vertices by property.
89          *
90          * @param key the key
91          * @param value the value
92          * @return the vertices by property
93          */
94         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
95         
96         /**
97          * filters by all the values for this property
98          * @param key
99          * @param values
100          * @return vertices that match these values
101          */
102         public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
103                 return this.getVerticesByProperty(key, values);
104         }
105
106         /**
107          * filters by all the values for this property
108          * @param key
109          * @param values
110          * @return vertices that match these values
111          */
112         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
113
114         /**
115          * Gets the child vertices from parent.
116          *
117          * @param parentKey the parent key
118          * @param parentValue the parent value
119          * @param childType the child type
120          * @return the child vertices from parent
121          */
122         public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
123                 
124         /**
125          * Gets the typed vertices by map.
126          *
127          * @param type the type
128          * @param map the map
129          * @return the typed vertices by map
130          */
131         public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);
132
133         /**
134          * Creates the DB query.
135          *
136          * @param obj the obj
137          * @return the query builder
138          */
139         public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
140                 this.createKeyQuery(obj);
141                 this.createContainerQuery(obj);
142                 return (QueryBuilder<Vertex>) this;
143         }
144         
145         /**
146          * Creates the key query.
147          *
148          * @param obj the obj
149          * @return the query builder
150          */
151         public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
152         
153         /**
154          * Creates the container query.
155          *
156          * @param obj the obj
157          * @return the query builder
158          */
159         public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
160         
161         /**
162          * Creates the edge traversal.
163          *
164          * @param parent the parent
165          * @param child the child
166          * @return the query builder
167          */
168         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
169         
170         /**
171          * Creates the edge traversal.
172          *
173          * @param parent the parent
174          * @param child the child
175          * @return the query builder
176          */
177         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException {
178                 String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
179                 this.createEdgeTraversal(type, nodeType, child.getDbName());
180                 return (QueryBuilder<Vertex>) this;
181
182         }
183
184         /**
185          *
186          * @param type
187          * @param outNodeType
188          * @param inNodeType
189          * @return
190          * @throws AAIException
191          */
192         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
193                 Introspector out = loader.introspectorFromName(outNodeType);
194                 Introspector in = loader.introspectorFromName(inNodeType);
195
196                 return createEdgeTraversal(type, out, in);
197         }
198
199         /**
200          *
201          * @param type
202          * @param outNodeType
203          * @param inNodeType
204          * @param labels
205          * @return
206          * @throws AAIException
207          */
208         public QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException {
209                 Introspector out = loader.introspectorFromName(outNodeType);
210                 Introspector in = loader.introspectorFromName(inNodeType);
211
212                 return createEdgeTraversalWithLabels(type, out, in, labels);
213         }
214
215         /**
216          *
217          * @param type
218          * @param out
219          * @param in
220          * @param labels
221          * @return
222          */
223         public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;
224
225         /**
226          *
227          * @param type
228          * @param outNodeType
229          * @param inNodeType
230          * @return
231          * @throws AAIException
232          */
233         public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
234                 this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);
235
236                 return (QueryBuilder<Edge>)this;
237
238         }
239         /**
240          *
241          * @param type
242          * @param outNodeType
243          * @param inNodeType
244          * @param labels
245          * @return
246          * @throws AAIException
247          */
248         public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;
249
250         /**
251          * Creates the query from URI.
252          *
253          * @param uri the uri
254          * @return the query parser
255          * @throws UnsupportedEncodingException the unsupported encoding exception
256          * @throws AAIException the AAI exception
257          */
258         public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
259         
260         /**
261          * Creates the query from URI.
262          *
263          * @param uri the uri
264          * @param queryParams the query params
265          * @return the query parser
266          * @throws UnsupportedEncodingException the unsupported encoding exception
267          * @throws AAIException the AAI exception
268          */
269         public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
270
271         /**
272          * Creates a queryparser from a given object name.
273          * 
274          * @param objName - name of the object type as it appears in the database
275          * @return
276          */
277         public abstract QueryParser createQueryFromObjectName(String objName);
278         
279         /**
280          * Creates the query from relationship.
281          *
282          * @param relationship the relationship
283          * @return the query parser
284          * @throws UnsupportedEncodingException the unsupported encoding exception
285          * @throws AAIException the AAI exception
286          */
287         public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
288
289         /**
290          * Gets the parent query.
291          *
292          * @return the parent query
293          */
294         public abstract QueryBuilder<E> getParentQuery();
295         
296         /**
297          * Gets the query.
298          *
299          * @return the query
300          */
301         public abstract <E2> E2 getQuery();
302         
303         /**
304          * Form boundary.
305          */
306         public abstract void markParentBoundary();
307         
308         public abstract QueryBuilder<E> limit(long amount);
309
310         /**
311          * New instance.
312          *
313          * @param start the start
314          * @return the query builder
315          */
316         public abstract QueryBuilder<E> newInstance(Vertex start);
317         
318         /**
319          * New instance.
320          *
321          * @return the query builder
322          */
323         public abstract QueryBuilder<E> newInstance();
324         
325         /**
326          * Gets the start.
327          *
328          * @return the start
329          */
330         public abstract Vertex getStart();
331
332         protected Object correctObjectType(Object obj) {
333                 
334                 if (obj != null && obj.getClass().equals(Long.class)) {
335                         return new Integer(obj.toString());
336                 }
337                 
338                 return obj;
339         }
340         /**
341          * uses all fields in the introspector to create a query
342          * 
343          * @param obj
344          * @return
345          */
346         public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
347
348         /**
349          * lets you join any number of QueryBuilders
350          * <b>be careful about starting with a union it will not use indexes</b>
351          * @param builder
352          * @return
353          */
354         public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
355         
356         public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
357         
358         public abstract QueryBuilder<E> store(String name);
359         public abstract QueryBuilder<E> cap(String name);
360         public abstract QueryBuilder<E> unfold();
361         public abstract QueryBuilder<E> dedup();
362         public abstract QueryBuilder<E> emit();
363         public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
364         public abstract QueryBuilder<Edge> outE();
365         public abstract QueryBuilder<Edge> inE();
366         public abstract QueryBuilder<Vertex> inV();
367         public abstract QueryBuilder<Vertex> outV();
368         public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
369         public abstract QueryBuilder<E> as(String name);
370         public abstract QueryBuilder<E> select(String name);
371         public abstract QueryBuilder<E> until(QueryBuilder<E> builder);
372         
373         /**
374          * Used to prevent the traversal from repeating its path through the graph.
375          * See http://tinkerpop.apache.org/docs/3.0.1-incubating/#simplepath-step for more info.
376          * 
377          * @return a QueryBuilder with the simplePath step appended to its traversal
378          */
379         public abstract QueryBuilder<E> simplePath();
380         
381         public abstract void markContainer();
382
383         public abstract QueryBuilder<E> getContainerQuery();
384
385         public abstract List<E> toList();
386         
387         public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
388                 return (QueryBuilder<Vertex>) this;
389         }
390
391                 
392 }