a51f592aa0d0392616710288cf46928f73cc7f7b
[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 vertices that are excluded by property.
116          *
117          * @param key the key
118          * @param value the value
119          * @return the vertices by property
120          */
121         public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value);
122
123         /**
124          * filters by all the values for this property and excludes the vertices
125          * @param key
126          * @param values
127          * @return vertices that match these values
128          */
129         public QueryBuilder<Vertex> getVerticesExcludeByIndexedProperty(String key, List<?> values) {
130                 return this.getVerticesExcludeByProperty(key, values);
131         }
132
133         /**
134          * filters by all the values for this property and excludes the vertices
135          * @param key
136          * @param values
137          * @return vertices that match these values
138          */
139         public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values);
140
141         /**
142          * Gets the child vertices from parent.
143          *
144          * @param parentKey the parent key
145          * @param parentValue the parent value
146          * @param childType the child type
147          * @return the child vertices from parent
148          */
149         public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
150                 
151         /**
152          * Gets the typed vertices by map.
153          *
154          * @param type the type
155          * @param map the map
156          * @return the typed vertices by map
157          */
158         public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);
159
160         /**
161          * Creates the DB query.
162          *
163          * @param obj the obj
164          * @return the query builder
165          */
166         public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
167                 this.createKeyQuery(obj);
168                 this.createContainerQuery(obj);
169                 return (QueryBuilder<Vertex>) this;
170         }
171         
172         /**
173          * Creates the key query.
174          *
175          * @param obj the obj
176          * @return the query builder
177          */
178         public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
179         
180         /**
181          * Creates the container query.
182          *
183          * @param obj the obj
184          * @return the query builder
185          */
186         public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
187         
188         /**
189          * Creates the edge traversal.
190          *
191          * @param parent the parent
192          * @param child the child
193          * @return the query builder
194          */
195         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
196         
197         /**
198          * Creates the edge traversal.
199          *
200          * @param parent the parent
201          * @param child the child
202          * @return the query builder
203          */
204         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException {
205                 String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
206                 this.createEdgeTraversal(type, nodeType, child.getDbName());
207                 return (QueryBuilder<Vertex>) this;
208
209         }
210
211         /**
212          *
213          * @param type
214          * @param outNodeType
215          * @param inNodeType
216          * @return
217          * @throws AAIException
218          */
219         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
220                 Introspector out = loader.introspectorFromName(outNodeType);
221                 Introspector in = loader.introspectorFromName(inNodeType);
222
223                 return createEdgeTraversal(type, out, in);
224         }
225
226         /**
227          *
228          * @param type
229          * @param outNodeType
230          * @param inNodeType
231          * @param labels
232          * @return
233          * @throws AAIException
234          */
235         public QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException {
236                 Introspector out = loader.introspectorFromName(outNodeType);
237                 Introspector in = loader.introspectorFromName(inNodeType);
238
239                 return createEdgeTraversalWithLabels(type, out, in, labels);
240         }
241
242         /**
243          *
244          * @param type
245          * @param out
246          * @param in
247          * @param labels
248          * @return
249          */
250         public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;
251
252         /**
253          *
254          * @param type
255          * @param outNodeType
256          * @param inNodeType
257          * @return
258          * @throws AAIException
259          */
260         public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
261                 this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);
262
263                 return (QueryBuilder<Edge>)this;
264
265         }
266         /**
267          *
268          * @param type
269          * @param outNodeType
270          * @param inNodeType
271          * @param labels
272          * @return
273          * @throws AAIException
274          */
275         public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;
276
277         /**
278          * Creates the query from URI.
279          *
280          * @param uri the uri
281          * @return the query parser
282          * @throws UnsupportedEncodingException the unsupported encoding exception
283          * @throws AAIException the AAI exception
284          */
285         public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
286         
287         /**
288          * Creates the query from URI.
289          *
290          * @param uri the uri
291          * @param queryParams the query params
292          * @return the query parser
293          * @throws UnsupportedEncodingException the unsupported encoding exception
294          * @throws AAIException the AAI exception
295          */
296         public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
297
298         /**
299          * Creates a queryparser from a given object name.
300          * 
301          * @param objName - name of the object type as it appears in the database
302          * @return
303          */
304         public abstract QueryParser createQueryFromObjectName(String objName);
305         
306         /**
307          * Creates the query from relationship.
308          *
309          * @param relationship the relationship
310          * @return the query parser
311          * @throws UnsupportedEncodingException the unsupported encoding exception
312          * @throws AAIException the AAI exception
313          */
314         public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
315
316         /**
317          * Gets the parent query.
318          *
319          * @return the parent query
320          */
321         public abstract QueryBuilder<E> getParentQuery();
322         
323         /**
324          * Gets the query.
325          *
326          * @return the query
327          */
328         public abstract <E2> E2 getQuery();
329         
330         /**
331          * Form boundary.
332          */
333         public abstract void markParentBoundary();
334         
335         public abstract QueryBuilder<E> limit(long amount);
336
337         /**
338          * New instance.
339          *
340          * @param start the start
341          * @return the query builder
342          */
343         public abstract QueryBuilder<E> newInstance(Vertex start);
344         
345         /**
346          * New instance.
347          *
348          * @return the query builder
349          */
350         public abstract QueryBuilder<E> newInstance();
351         
352         /**
353          * Gets the start.
354          *
355          * @return the start
356          */
357         public abstract Vertex getStart();
358
359         protected Object correctObjectType(Object obj) {
360                 
361                 if (obj != null && obj.getClass().equals(Long.class)) {
362                         return new Integer(obj.toString());
363                 }
364                 
365                 return obj;
366         }
367         /**
368          * uses all fields in the introspector to create a query
369          * 
370          * @param obj
371          * @return
372          */
373         public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
374
375         /**
376          * lets you join any number of QueryBuilders
377          * <b>be careful about starting with a union it will not use indexes</b>
378          * @param builder
379          * @return
380          */
381         public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
382         
383         public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
384         
385         public abstract QueryBuilder<E> store(String name);
386         public abstract QueryBuilder<E> cap(String name);
387         public abstract QueryBuilder<E> unfold();
388         public abstract QueryBuilder<E> dedup();
389         public abstract QueryBuilder<E> emit();
390         public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
391         public abstract QueryBuilder<Edge> outE();
392         public abstract QueryBuilder<Edge> inE();
393         public abstract QueryBuilder<Vertex> inV();
394         public abstract QueryBuilder<Vertex> outV();
395         public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
396         public abstract QueryBuilder<E> as(String name);
397         public abstract QueryBuilder<E> select(String name);
398         public abstract QueryBuilder<E> until(QueryBuilder<E> builder);
399         public abstract QueryBuilder<E> groupCount();
400         public abstract QueryBuilder<E> by(String name);
401         public abstract QueryBuilder<E> both();
402         
403         /**
404          * Used to prevent the traversal from repeating its path through the graph.
405          * See http://tinkerpop.apache.org/docs/3.0.1-incubating/#simplepath-step for more info.
406          * 
407          * @return a QueryBuilder with the simplePath step appended to its traversal
408          */
409         public abstract QueryBuilder<E> simplePath();
410         
411         public abstract void markContainer();
412
413         public abstract QueryBuilder<E> getContainerQuery();
414
415         public abstract List<E> toList();
416         
417         public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
418                 return (QueryBuilder<Vertex>) this;
419         }
420
421                 
422 }