2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.query.builder;
22 import java.io.UnsupportedEncodingException;
24 import java.util.Iterator;
25 import java.util.List;
28 import javax.ws.rs.core.MultivaluedMap;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.structure.Edge;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.onap.aai.db.props.AAIProperties;
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.introspection.Introspector;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.parsers.query.QueryParser;
38 import org.onap.aai.parsers.query.QueryParserStrategy;
39 import org.onap.aai.serialization.db.EdgeType;
42 * The Class QueryBuilder.
44 public abstract class QueryBuilder<E> implements Iterator<E> {
46 protected final GraphTraversalSource source;
47 protected QueryParserStrategy factory = null;
48 protected Loader loader = null;
49 protected boolean optimize = false;
50 protected Vertex start = null;
53 * Instantiates a new query builder.
55 * @param loader the loader
57 public QueryBuilder(Loader loader, GraphTraversalSource source) {
63 * Instantiates a new query builder.
65 * @param loader the loader
66 * @param start the start
68 public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
75 * Gets the vertices by indexed property.
78 * @param value the value
79 * @return the vertices by indexed property
81 public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
82 return this.getVerticesByProperty(key, value);
86 * Gets the vertices by property.
89 * @param value the value
90 * @return the vertices by property
92 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
95 * filters by all the values for this property
98 * @return vertices that match these values
100 public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
101 return this.getVerticesByProperty(key, values);
105 * filters by all the values for this property
108 * @return vertices that match these values
110 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
113 * Gets the vertices that are excluded by property.
116 * @param value the value
117 * @return the vertices by property
119 public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value);
122 * filters by all the values for this property and excludes the vertices
125 * @return vertices that match these values
127 public QueryBuilder<Vertex> getVerticesExcludeByIndexedProperty(String key, List<?> values) {
128 return this.getVerticesExcludeByProperty(key, values);
132 * filters by all the values for this property and excludes the vertices
135 * @return vertices that match these values
137 public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values);
140 * Gets the child vertices from parent.
142 * @param parentKey the parent key
143 * @param parentValue the parent value
144 * @param childType the child type
145 * @return the child vertices from parent
147 public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
150 * Gets the typed vertices by map.
152 * @param type the type
154 * @return the typed vertices by map
156 public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);
159 * Creates the DB query.
162 * @return the query builder
164 public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
165 this.createKeyQuery(obj);
166 this.createContainerQuery(obj);
167 return (QueryBuilder<Vertex>) this;
171 * Creates the key query.
174 * @return the query builder
176 public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
179 * Creates the container query.
182 * @return the query builder
184 public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
187 * Creates the edge traversal.
189 * @param parent the parent
190 * @param child the child
191 * @return the query builder
193 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
196 * Creates the edge traversal.
198 * @param parent the parent
199 * @param child the child
200 * @return the query builder
202 public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException {
203 String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null);
204 this.createEdgeTraversal(type, nodeType, child.getDbName());
205 return (QueryBuilder<Vertex>) this;
215 * @throws AAIException
217 public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
218 Introspector out = loader.introspectorFromName(outNodeType);
219 Introspector in = loader.introspectorFromName(inNodeType);
221 return createEdgeTraversal(type, out, in);
231 * @throws AAIException
233 public QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException {
234 Introspector out = loader.introspectorFromName(outNodeType);
235 Introspector in = loader.introspectorFromName(inNodeType);
237 return createEdgeTraversalWithLabels(type, out, in, labels);
248 public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;
256 * @throws AAIException
258 public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
259 this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);
261 return (QueryBuilder<Edge>)this;
271 * @throws AAIException
273 public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;
276 * Creates the query from URI.
279 * @return the query parser
280 * @throws UnsupportedEncodingException the unsupported encoding exception
281 * @throws AAIException the AAI exception
283 public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
286 * Creates the query from URI.
289 * @param queryParams the query params
290 * @return the query parser
291 * @throws UnsupportedEncodingException the unsupported encoding exception
292 * @throws AAIException the AAI exception
294 public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
297 * Creates a queryparser from a given object name.
299 * @param objName - name of the object type as it appears in the database
302 public abstract QueryParser createQueryFromObjectName(String objName);
305 * Creates the query from relationship.
307 * @param relationship the relationship
308 * @return the query parser
309 * @throws UnsupportedEncodingException the unsupported encoding exception
310 * @throws AAIException the AAI exception
312 public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
315 * Gets the parent query.
317 * @return the parent query
319 public abstract QueryBuilder<E> getParentQuery();
326 public abstract <E2> E2 getQuery();
331 public abstract void markParentBoundary();
333 public abstract QueryBuilder<E> limit(long amount);
338 * @param start the start
339 * @return the query builder
341 public abstract QueryBuilder<E> newInstance(Vertex start);
346 * @return the query builder
348 public abstract QueryBuilder<E> newInstance();
355 public abstract Vertex getStart();
357 protected Object correctObjectType(Object obj) {
359 if (obj != null && obj.getClass().equals(Long.class)) {
360 return new Integer(obj.toString());
366 * uses all fields in the introspector to create a query
371 public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
374 * lets you join any number of QueryBuilders
375 * <b>be careful about starting with a union it will not use indexes</b>
379 public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
381 public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
383 public abstract QueryBuilder<E> store(String name);
384 public abstract QueryBuilder<E> cap(String name);
385 public abstract QueryBuilder<E> unfold();
386 public abstract QueryBuilder<E> dedup();
387 public abstract QueryBuilder<E> emit();
388 public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
389 public abstract QueryBuilder<Edge> outE();
390 public abstract QueryBuilder<Edge> inE();
391 public abstract QueryBuilder<Vertex> inV();
392 public abstract QueryBuilder<Vertex> outV();
393 public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
394 public abstract QueryBuilder<E> as(String name);
395 public abstract QueryBuilder<E> select(String name);
396 public abstract QueryBuilder<E> until(QueryBuilder<E> builder);
397 public abstract QueryBuilder<E> groupCount();
398 public abstract QueryBuilder<E> by(String name);
399 public abstract QueryBuilder<E> both();
402 * Used to prevent the traversal from repeating its path through the graph.
403 * See http://tinkerpop.apache.org/docs/3.0.1-incubating/#simplepath-step for more info.
405 * @return a QueryBuilder with the simplePath step appended to its traversal
407 public abstract QueryBuilder<E> simplePath();
409 public abstract void markContainer();
411 public abstract QueryBuilder<E> getContainerQuery();
413 public abstract List<E> toList();
415 public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
416 return (QueryBuilder<Vertex>) this;