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 child vertices from parent.
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
122 public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
125 * Gets the typed vertices by map.
127 * @param type the type
129 * @return the typed vertices by map
131 public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map);
134 * Creates the DB query.
137 * @return the query builder
139 public QueryBuilder<Vertex> createDBQuery(Introspector obj) {
140 this.createKeyQuery(obj);
141 this.createContainerQuery(obj);
142 return (QueryBuilder<Vertex>) this;
146 * Creates the key query.
149 * @return the query builder
151 public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
154 * Creates the container query.
157 * @return the query builder
159 public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
162 * Creates the edge traversal.
164 * @param parent the parent
165 * @param child the child
166 * @return the query builder
168 public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
171 * Creates the edge traversal.
173 * @param parent the parent
174 * @param child the child
175 * @return the query builder
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;
190 * @throws AAIException
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);
196 return createEdgeTraversal(type, out, in);
206 * @throws AAIException
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);
212 return createEdgeTraversalWithLabels(type, out, in, labels);
223 public abstract QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in, List<String> labels) throws AAIException;
231 * @throws AAIException
233 public QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException {
234 this.getEdgesBetweenWithLabels(type, outNodeType, inNodeType, null);
236 return (QueryBuilder<Edge>)this;
246 * @throws AAIException
248 public abstract QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException;
251 * Creates the query from URI.
254 * @return the query parser
255 * @throws UnsupportedEncodingException the unsupported encoding exception
256 * @throws AAIException the AAI exception
258 public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
261 * Creates the query from 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
269 public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
272 * Creates a queryparser from a given object name.
274 * @param objName - name of the object type as it appears in the database
277 public abstract QueryParser createQueryFromObjectName(String objName);
280 * Creates the query from relationship.
282 * @param relationship the relationship
283 * @return the query parser
284 * @throws UnsupportedEncodingException the unsupported encoding exception
285 * @throws AAIException the AAI exception
287 public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
290 * Gets the parent query.
292 * @return the parent query
294 public abstract QueryBuilder<E> getParentQuery();
301 public abstract <E2> E2 getQuery();
306 public abstract void markParentBoundary();
308 public abstract QueryBuilder<E> limit(long amount);
313 * @param start the start
314 * @return the query builder
316 public abstract QueryBuilder<E> newInstance(Vertex start);
321 * @return the query builder
323 public abstract QueryBuilder<E> newInstance();
330 public abstract Vertex getStart();
332 protected Object correctObjectType(Object obj) {
334 if (obj != null && obj.getClass().equals(Long.class)) {
335 return new Integer(obj.toString());
341 * uses all fields in the introspector to create a query
346 public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
349 * lets you join any number of QueryBuilders
350 * <b>be careful about starting with a union it will not use indexes</b>
354 public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
356 public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
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);
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.
377 * @return a QueryBuilder with the simplePath step appended to its traversal
379 public abstract QueryBuilder<E> simplePath();
381 public abstract void markContainer();
383 public abstract QueryBuilder<E> getContainerQuery();
385 public abstract List<E> toList();
387 public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
388 return (QueryBuilder<Vertex>) this;