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.List;
28 import javax.ws.rs.core.MultivaluedMap;
30 import org.apache.tinkerpop.gremlin.process.traversal.Step;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
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.TraversalStrategy;
39 import org.onap.aai.serialization.db.EdgeRules;
42 * The Class TraversalQuery.
44 public class TraversalQuery<E> extends GraphTraversalBuilder<E> {
47 * Instantiates a new traversal query.
49 * @param loader the loader
51 public TraversalQuery(Loader loader, GraphTraversalSource source) {
52 super(loader, source);
53 this.factory = new TraversalStrategy(this.loader, this);
57 * Instantiates a new traversal query.
59 * @param loader the loader
60 * @param source graph traversal source
61 * @param edgeRules the edgeRules to use
63 public TraversalQuery(Loader loader, GraphTraversalSource source, EdgeRules edgeRules) {
64 super(loader, source, edgeRules);
65 this.factory = new TraversalStrategy(this.loader, this);
70 * Instantiates a new traversal query.
72 * @param loader the loader
73 * @param start the start
75 public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start) {
76 super(loader, source, start);
77 this.factory = new TraversalStrategy(this.loader, this);
81 * Instantiates a new traversal query.
83 * @param loader the loader
84 * @param start the start
86 public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start, EdgeRules edgeRules) {
87 super(loader, source, start, edgeRules);
88 this.factory = new TraversalStrategy(this.loader, this);
91 protected TraversalQuery(GraphTraversal<Vertex, E> traversal, Loader loader, GraphTraversalSource source, GraphTraversalBuilder<E> gtb) {
92 super(loader, source);
93 this.traversal = traversal;
94 this.stepIndex = gtb.getStepIndex();
95 this.parentStepIndex = gtb.getParentStepIndex();
96 this.containerStepIndex = gtb.getContainerStepIndex();
97 this.factory = new TraversalStrategy(this.loader, this);
98 this.start = gtb.getStart();
105 public QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException {
106 return factory.buildURIParser(uri);
113 public QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException {
114 return factory.buildRelationshipParser(relationship);
121 public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
122 throws UnsupportedEncodingException, AAIException {
123 return factory.buildURIParser(uri, queryParams);
130 public QueryParser createQueryFromObjectName(String objName) {
131 return factory.buildObjectNameParser(objName);
138 public QueryBuilder<E> newInstance(Vertex start) {
139 return new TraversalQuery<>(loader, source, start);
146 public QueryBuilder<E> newInstance() {
147 return new TraversalQuery<>(loader, source);
151 protected QueryBuilder<E> cloneQueryAtStep(int index) {
158 GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
159 GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
160 List<Step> steps = cloneAdmin.getSteps();
162 for (int i = steps.size()-1; i >= idx; i--) {
163 cloneAdmin.removeStep(i);
165 return new TraversalQuery<>(cloneAdmin, loader, source, this);
169 protected QueryBuilder<E> removeQueryStepsBetween(int start, int end) {
170 GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
171 GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
173 for (int i = end-2; i >= start; i--) {
174 cloneAdmin.removeStep(i);
176 return new TraversalQuery<>(cloneAdmin, loader, source, this);