summaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java
blob: 89c15f25c29ad2383967c16cbba05af02e6a4c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.query.builder;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.introspection.Introspector;
import org.onap.aai.introspection.Loader;
import org.onap.aai.parsers.query.QueryParser;
import org.onap.aai.parsers.query.QueryParserStrategy;
import org.onap.aai.serialization.db.EdgeType;

/**
 * The Class QueryBuilder.
 */
public abstract class QueryBuilder<E> implements Iterator<E> {

	protected final GraphTraversalSource source;
	protected QueryParserStrategy factory = null;
	protected Loader loader = null;
	protected boolean optimize = false;
	protected Vertex start = null;
	
	/**
	 * Instantiates a new query builder.
	 *
	 * @param loader the loader
	 */
	public QueryBuilder(Loader loader, GraphTraversalSource source) {
		this.loader = loader;
		this.source = source;
	}
	
	/**
	 * Instantiates a new query builder.
	 *
	 * @param loader the loader
	 * @param start the start
	 */
	public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
		this.loader = loader;
		this.start = start;
		this.source = source;
	}
	
	/**
	 * Gets the vertices by indexed property.
	 *
	 * @param key the key
	 * @param value the value
	 * @return the vertices by indexed property
	 */
	public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
		return this.getVerticesByProperty(key, value);
	}
	
	/**
	 * Gets the vertices by property.
	 *
	 * @param key the key
	 * @param value the value
	 * @return the vertices by property
	 */
	public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
	
	/**
	 * filters by all the values for this property
	 * @param key
	 * @param values
	 * @return vertices that match these values
	 */
	public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
		return this.getVerticesByProperty(key, values);
	}

	/**
	 * filters by all the values for this property
	 * @param key
	 * @param values
	 * @return vertices that match these values
	 */
	public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);

	/**
	 * Gets the vertices that are excluded by property.
	 *
	 * @param key the key
	 * @param value the value
	 * @return the vertices by property
	 */
	public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value);

	/**
	 * filters by all the values for this property and excludes the vertices
	 * @param key
	 * @param values
	 * @return vertices that match these values
	 */
	public QueryBuilder<Vertex> getVerticesExcludeByIndexedProperty(String key, List<?> values) {
		return this.getVerticesExcludeByProperty(key, values);
	}

	/**
	 * filters by all the values for this property and excludes the vertices
	 * @param key
	 * @param values
	 * @return vertices that match these values
	 */
	public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values);

	/**
	 * Gets the child vertices from parent.
	 *
	 * @param parentKey the parent key
	 * @param parentValue the parent value
	 * @param childType the child type
	 * @return the child vertices from parent
	 */
	public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
		
	/**
	 * Gets the typed vertices by map.
	 *
	 * @param type the type
	 * @param map the map
	 * @return the typed vertices by map
	 */
	public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);

	/**
	 * Creates the DB query.
	 *
	 * @param obj the obj
	 * @return the query builder
	 */
	public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
		this.createKeyQuery(obj);
		this.createContainerQuery(obj);
		return (QueryBuilder<Vertex>) this;
	}
	
	/**
	 * Creates the key query.
	 *
	 * @param obj the obj
	 * @return the query builder
	 */
	public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
	
	/**
	 * Creates the container query.
	 *
	 * @param obj the obj
	 * @return the query builder
	 */
	public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
	
	/**
	 * Creates the edge traversal.
	 *
	 * @param parent the parent
	 * @param child the child
	 * @return the query builder
	 */
	public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
	
	/**
	 * Creates the edge traversal.
	 *
	 * @param parent the parent
	 * @param child the child
	 * @return the query builder
	 */
	public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException {
		String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
		this.createEdgeTraversal(type, nodeType, child.getDbName());
		return (QueryBuilder<Vertex>) this;

	}

	/**
	 *
	 * @param type
	 * @param outNodeType
	 * @param inNodeType
	 * @return
	 * @throws AAIException
	 */
	public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
		Introspector out = loader.introspectorFromName(outNodeType);
		Introspector in = loader.introspectorFromName(inNodeType);

		return createEdgeTraversal(type, out, in);
	}

	/**
	 *
	 * @param type
	 * @param outNodeType
	 * @param inNodeType
	 * @param labels
	 * @return
	 * @throws AAIException
	 */
	public QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException {
		Introspector out = loader.introspectorFromName(outNodeType);
		Introspector in = loader.introspectorFromName(inNodeType);

		return createEdgeTraversalWithLabels(type, out, in, labels);
	}

	/**
	 *
	 * @param type
	 * @param out
	 * @param in
	 * @param labels
	 * @return
	 */
	public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;

	/**
	 *
	 * @param type
	 * @param outNodeType
	 * @param inNodeType
	 * @return
	 * @throws AAIException
	 */
	public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
		this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);

		return (QueryBuilder<Edge>)this;

	}
	/**
	 *
	 * @param type
	 * @param outNodeType
	 * @param inNodeType
	 * @param labels
	 * @return
	 * @throws AAIException
	 */
	public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;

	/**
	 * Creates the query from URI.
	 *
	 * @param uri the uri
	 * @return the query parser
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 * @throws AAIException the AAI exception
	 */
	public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
	
	/**
	 * Creates the query from URI.
	 *
	 * @param uri the uri
	 * @param queryParams the query params
	 * @return the query parser
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 * @throws AAIException the AAI exception
	 */
	public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;

	/**
	 * Creates a queryparser from a given object name.
	 * 
	 * @param objName - name of the object type as it appears in the database
	 * @return
	 */
	public abstract QueryParser createQueryFromObjectName(String objName);
	
	/**
	 * Creates the query from relationship.
	 *
	 * @param relationship the relationship
	 * @return the query parser
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 * @throws AAIException the AAI exception
	 */
	public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;

	/**
	 * Gets the parent query.
	 *
	 * @return the parent query
	 */
	public abstract QueryBuilder<E> getParentQuery();
	
	/**
	 * Gets the query.
	 *
	 * @return the query
	 */
	public abstract <E2> E2 getQuery();
	
	/**
	 * Form boundary.
	 */
	public abstract void markParentBoundary();
	
	public abstract QueryBuilder<E> limit(long amount);

	/**
	 * New instance.
	 *
	 * @param start the start
	 * @return the query builder
	 */
	public abstract QueryBuilder<E> newInstance(Vertex start);
	
	/**
	 * New instance.
	 *
	 * @return the query builder
	 */
	public abstract QueryBuilder<E> newInstance();
	
	/**
	 * Gets the start.
	 *
	 * @return the start
	 */
	public abstract Vertex getStart();

	protected Object correctObjectType(Object obj) {
		
		if (obj != null && obj.getClass().equals(Long.class)) {
			return new Integer(obj.toString());
		}
		
		return obj;
	}
	/**
	 * uses all fields in the introspector to create a query
	 * 
	 * @param obj
	 * @return
	 */
	public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);

	/**
	 * lets you join any number of QueryBuilders
	 * <b>be careful about starting with a union it will not use indexes</b>
	 * @param builder
	 * @return
	 */
	public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
	
	public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
	
	public abstract QueryBuilder<E> store(String name);
	public abstract QueryBuilder<E> cap(String name);
	public abstract QueryBuilder<E> unfold();
	public abstract QueryBuilder<E> dedup();
	public abstract QueryBuilder<E> emit();
	public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
	public abstract QueryBuilder<Edge> outE();
	public abstract QueryBuilder<Edge> inE();
	public abstract QueryBuilder<Vertex> inV();
	public abstract QueryBuilder<Vertex> outV();
	public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
	public abstract QueryBuilder<E> as(String name);
	public abstract QueryBuilder<E> select(String name);
	public abstract QueryBuilder<E> until(QueryBuilder<E> builder);
	public abstract QueryBuilder<E> groupCount();
	public abstract QueryBuilder<E> by(String name);
	public abstract QueryBuilder<E> both();
	
	/**
	 * Used to prevent the traversal from repeating its path through the graph.
	 * See http://tinkerpop.apache.org/docs/3.0.1-incubating/#simplepath-step for more info.
	 * 
	 * @return a QueryBuilder with the simplePath step appended to its traversal
	 */
	public abstract QueryBuilder<E> simplePath();
	
 	public abstract void markContainer();

	public abstract QueryBuilder<E> getContainerQuery();

	public abstract List<E> toList();
	
	public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
		return (QueryBuilder<Vertex>) this;
	}

		
}