Add the vnf changes
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / query / builder / QueryBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.query.builder;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28
29 import javax.ws.rs.core.MultivaluedMap;
30
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.structure.Edge;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.openecomp.aai.exceptions.AAIException;
35 import org.openecomp.aai.introspection.Introspector;
36 import org.openecomp.aai.introspection.Loader;
37 import org.openecomp.aai.parsers.query.QueryParser;
38 import org.openecomp.aai.parsers.query.QueryParserStrategy;
39 import org.openecomp.aai.serialization.db.EdgeType;
40 import org.openecomp.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
41
42 /**
43  * The Class QueryBuilder.
44  */
45 public abstract class QueryBuilder<E> implements Iterator<E> {
46
47         protected QueryParserStrategy factory = null;
48         
49         protected Loader loader = null;
50         
51         protected boolean optimize = false;
52         
53         protected Vertex start = null;
54         protected final GraphTraversalSource source;
55         
56         /**
57          * Instantiates a new query builder.
58          *
59          * @param loader the loader
60          */
61         public QueryBuilder(Loader loader, GraphTraversalSource source) {
62                 this.loader = loader;
63                 this.source = source;
64         }
65         
66         /**
67          * Instantiates a new query builder.
68          *
69          * @param loader the loader
70          * @param start the start
71          */
72         public QueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
73                 this.loader = loader;
74                 this.start = start;
75                 this.source = source;
76         }
77         
78         /**
79          * Gets the vertices by indexed property.
80          *
81          * @param key the key
82          * @param value the value
83          * @return the vertices by indexed property
84          */
85         public abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, Object value);
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 abstract QueryBuilder<Vertex> getVerticesByIndexedProperty(String key, List<?> values);
103
104         /**
105          * filters by all the values for this property
106          * @param key
107          * @param values
108          * @return vertices that match these values
109          */
110         public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values);
111
112         /**
113          * Gets the child vertices from parent.
114          *
115          * @param parentKey the parent key
116          * @param parentValue the parent value
117          * @param childType the child type
118          * @return the child vertices from parent
119          */
120         public abstract QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType);
121                 
122         /**
123          * Gets the typed vertices by map.
124          *
125          * @param type the type
126          * @param map the map
127          * @return the typed vertices by map
128          */
129         public abstract QueryBuilder<Vertex> getTypedVerticesByMap(String type, LinkedHashMap<String, String> map);
130
131         /**
132          * Creates the DB query.
133          *
134          * @param obj the obj
135          * @return the query builder
136          */
137         public abstract QueryBuilder<Vertex> createDBQuery(Introspector obj);
138         
139         /**
140          * Creates the key query.
141          *
142          * @param obj the obj
143          * @return the query builder
144          */
145         public abstract QueryBuilder<Vertex> createKeyQuery(Introspector obj);
146         
147         /**
148          * Creates the container query.
149          *
150          * @param obj the obj
151          * @return the query builder
152          */
153         public abstract QueryBuilder<Vertex> createContainerQuery(Introspector obj);
154         
155         /**
156          * Creates the edge traversal.
157          *
158          * @param parent the parent
159          * @param child the child
160          * @return the query builder
161          */
162         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException;
163         
164         /**
165          * Creates the edge traversal.
166          *
167          * @param parent the parent
168          * @param child the child
169          * @return the query builder
170          */
171         public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Vertex parent, Introspector child) throws AAIException;
172
173         public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws NoEdgeRuleFoundException, AAIException {
174                 Introspector out = loader.introspectorFromName(outNodeType);
175                 Introspector in = loader.introspectorFromName(inNodeType);
176                 
177                 return createEdgeTraversal(type, out, in);
178         }
179         
180         public abstract QueryBuilder<Edge> getEdgesBetween(EdgeType type, String outNodeType, String inNodeType) throws AAIException;
181         /**
182          * Creates the query from URI.
183          *
184          * @param uri the uri
185          * @return the query parser
186          * @throws UnsupportedEncodingException the unsupported encoding exception
187          * @throws AAIException the AAI exception
188          */
189         public abstract QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException;
190         
191         /**
192          * Creates the query from URI.
193          *
194          * @param uri the uri
195          * @param queryParams the query params
196          * @return the query parser
197          * @throws UnsupportedEncodingException the unsupported encoding exception
198          * @throws AAIException the AAI exception
199          */
200         public abstract QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams) throws UnsupportedEncodingException, AAIException;
201
202         /**
203          * Creates a queryparser from a given object name.
204          * 
205          * @param objName - name of the object type as it appears in the database
206          * @return
207          */
208         public abstract QueryParser createQueryFromObjectName(String objName);
209         
210         /**
211          * Creates the query from relationship.
212          *
213          * @param relationship the relationship
214          * @return the query parser
215          * @throws UnsupportedEncodingException the unsupported encoding exception
216          * @throws AAIException the AAI exception
217          */
218         public abstract QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException;
219
220         /**
221          * Gets the parent query.
222          *
223          * @return the parent query
224          */
225         public abstract QueryBuilder<E> getParentQuery();
226         
227         /**
228          * Gets the query.
229          *
230          * @return the query
231          */
232         public abstract <E2> E2 getQuery();
233         
234         /**
235          * Form boundary.
236          */
237         public abstract void markParentBoundary();
238         
239         public abstract QueryBuilder<E> limit(long amount);
240         /**
241          * New instance.
242          *
243          * @param start the start
244          * @return the query builder
245          */
246         public abstract QueryBuilder<E> newInstance(Vertex start);
247         
248         /**
249          * New instance.
250          *
251          * @return the query builder
252          */
253         public abstract QueryBuilder<E> newInstance();
254         
255         /**
256          * Gets the start.
257          *
258          * @return the start
259          */
260         public abstract Vertex getStart();
261
262         protected Object correctObjectType(Object obj) {
263                 
264                 if (obj != null && obj.getClass().equals(Long.class)) {
265                         return new Integer(obj.toString());
266                 }
267                 
268                 return obj;
269         }
270         /**
271          * uses all fields in the introspector to create a query
272          * 
273          * @param obj
274          * @return
275          */
276         public abstract QueryBuilder<Vertex> exactMatchQuery(Introspector obj);
277
278         /**
279          * lets you join any number of QueryBuilders
280          * <b>be careful about starting with a union it will not use indexes</b>
281          * @param builder
282          * @return
283          */
284         public abstract QueryBuilder<E> union(QueryBuilder<E>... builder);
285         
286         public abstract QueryBuilder<E> where(QueryBuilder<E>... builder);
287         
288         public abstract QueryBuilder<E> store(String name);
289         public abstract QueryBuilder<E> cap(String name);
290         public abstract QueryBuilder<E> unfold();
291         public abstract QueryBuilder<E> dedup();
292         public abstract QueryBuilder<E> emit();
293         public abstract QueryBuilder<E> repeat(QueryBuilder<E> builder);
294         public abstract QueryBuilder<Edge> outE();
295         public abstract QueryBuilder<Edge> inE();
296         public abstract QueryBuilder<Vertex> inV();
297         public abstract QueryBuilder<Vertex> outV();
298         public abstract QueryBuilder<E> not(QueryBuilder<E> builder);
299         public abstract QueryBuilder<E> as(String name);
300         public abstract QueryBuilder<E> select(String name);
301         
302         public abstract void markContainer();
303
304         public abstract QueryBuilder<E> getContainerQuery();
305
306         public abstract List<E> toList();
307         
308                 
309 }