2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.query.builder;
24 import java.io.UnsupportedEncodingException;
26 import java.util.Iterator;
27 import java.util.List;
30 import javax.ws.rs.core.MultivaluedMap;
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;
44 * The Class QueryBuilder.
46 public abstract class QueryBuilder<E> implements Iterator<E> {
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;
55 * Instantiates a new query builder.
57 * @param loader the loader
59 public QueryBuilder(Loader loader, GraphTraversalSource source) {
65 * Instantiates a new query builder.
67 * @param loader the loader
68 * @param start the start
70 public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
77 * Gets the vertices by indexed property.
80 * @param value the value
81 * @return the vertices by indexed property
83 public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value) {
84 return this.getVerticesByProperty(key, value);
88 * Gets the vertices by property.
91 * @param value the value
92 * @return the vertices by property
94 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, Object value);
97 * filters by all the values for this property
100 * @return vertices that match these values
102 public QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values) {
103 return this.getVerticesByProperty(key, values);
107 * filters by all the values for this property
110 * @return vertices that match these values
112 public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
115 * Gets the vertices that are excluded by property.
118 * @param value the value
119 * @return the vertices by property
121 public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value);
124 * filters by all the values for this property and excludes the vertices
127 * @return vertices that match these values
129 public QueryBuilder<Vertex> getVerticesExcludeByIndexedProperty(String key, List<?> values) {
130 return this.getVerticesExcludeByProperty(key, values);
134 * filters by all the values for this property and excludes the vertices
137 * @return vertices that match these values
139 public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values);
142 * Gets the child vertices from parent.
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
149 public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
152 * Gets the typed vertices by map.
154 * @param type the type
156 * @return the typed vertices by map
158 public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);
161 * Creates the DB query.
164 * @return the query builder
166 public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
167 this.createKeyQuery(obj);
168 this.createContainerQuery(obj);
169 return (QueryBuilder<Vertex>) this;
173 * Creates the key query.
176 * @return the query builder
178 public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
181 * Creates the container query.
184 * @return the query builder
186 public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
189 * Creates the edge traversal.
191 * @param parent the parent
192 * @param child the child
193 * @return the query builder
195 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
198 * Creates the edge traversal.
200 * @param parent the parent
201 * @param child the child
202 * @return the query builder
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;
217 * @throws AAIException
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);
223 return createEdgeTraversal(type, out, in);
233 * @throws AAIException
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);
239 return createEdgeTraversalWithLabels(type, out, in, labels);
250 public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;
258 * @throws AAIException
260 public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
261 this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);
263 return (QueryBuilder<Edge>)this;
273 * @throws AAIException
275 public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;
278 * Creates the query from URI.
281 * @return the query parser
282 * @throws UnsupportedEncodingException the unsupported encoding exception
283 * @throws AAIException the AAI exception
285 public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
288 * Creates the query from 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
296 public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
299 * Creates a queryparser from a given object name.
301 * @param objName - name of the object type as it appears in the database
304 public abstract QueryParser createQueryFromObjectName(String objName);
307 * Creates the query from relationship.
309 * @param relationship the relationship
310 * @return the query parser
311 * @throws UnsupportedEncodingException the unsupported encoding exception
312 * @throws AAIException the AAI exception
314 public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
317 * Gets the parent query.
319 * @return the parent query
321 public abstract QueryBuilder<E> getParentQuery();
328 public abstract <E2> E2 getQuery();
333 public abstract void markParentBoundary();
335 public abstract QueryBuilder<E> limit(long amount);
340 * @param start the start
341 * @return the query builder
343 public abstract QueryBuilder<E> newInstance(Vertex start);
348 * @return the query builder
350 public abstract QueryBuilder<E> newInstance();
357 public abstract Vertex getStart();
359 protected Object correctObjectType(Object obj) {
361 if (obj != null && obj.getClass().equals(Long.class)) {
362 return new Integer(obj.toString());
368 * uses all fields in the introspector to create a query
373 public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
376 * lets you join any number of QueryBuilders
377 * <b>be careful about starting with a union it will not use indexes</b>
381 public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
383 public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
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();
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.
407 * @return a QueryBuilder with the simplePath step appended to its traversal
409 public abstract QueryBuilder<E> simplePath();
411 public abstract void markContainer();
413 public abstract QueryBuilder<E> getContainerQuery();
415 public abstract List<E> toList();
417 public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
418 return (QueryBuilder<Vertex>) this;