2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.query.builder;
23 import java.io.UnsupportedEncodingException;
25 import java.util.List;
27 import javax.ws.rs.core.MultivaluedMap;
29 import org.apache.tinkerpop.gremlin.process.traversal.Step;
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 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.TraversalStrategy;
41 * The Class TraversalQuery.
43 public class TraversalQuery<E> extends GraphTraversalBuilder<E> {
46 * Instantiates a new traversal query.
48 * @param loader the loader
50 public TraversalQuery(Loader loader, GraphTraversalSource source) {
51 super(loader, source);
52 this.factory = new TraversalStrategy(this.loader, this);
56 * Instantiates a new traversal query.
58 * @param loader the loader
59 * @param start the start
61 public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start) {
62 super(loader, source, start);
63 this.factory = new TraversalStrategy(this.loader, this);
66 protected TraversalQuery(GraphTraversal<Vertex, E> traversal, Loader loader, GraphTraversalSource source, GraphTraversalBuilder<E> gtb) {
67 super(loader, source);
68 this.traversal = traversal;
69 this.stepIndex = gtb.getStepIndex();
70 this.parentStepIndex = gtb.getParentStepIndex();
71 this.containerStepIndex = gtb.getContainerStepIndex();
72 this.factory = new TraversalStrategy(this.loader, this);
73 this.start = gtb.getStart();
80 public QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException {
81 return factory.buildURIParser(uri);
88 public QueryParser createQueryFromRelationship(Introspector relationship) throws UnsupportedEncodingException, AAIException {
89 return factory.buildRelationshipParser(relationship);
96 public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
97 throws UnsupportedEncodingException, AAIException {
98 return factory.buildURIParser(uri, queryParams);
105 public QueryParser createQueryFromObjectName(String objName) {
106 return factory.buildObjectNameParser(objName);
113 public QueryBuilder<E> newInstance(Vertex start) {
114 return new TraversalQuery<>(loader, source, start);
121 public QueryBuilder<E> newInstance() {
122 return new TraversalQuery<>(loader, source);
126 protected QueryBuilder<E> cloneQueryAtStep(int index) {
130 GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
131 GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
132 List<Step> steps = cloneAdmin.getSteps();
134 for (int i = steps.size()-1; i >= index; i--) {
135 cloneAdmin.removeStep(i);
137 return new TraversalQuery<>(cloneAdmin, loader, source, this);
141 protected QueryBuilder<E> removeQueryStepsBetween(int start, int end) {
142 GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
143 GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
145 for (int i = end-2; i >= start; i--) {
146 cloneAdmin.removeStep(i);
148 return new TraversalQuery<>(cloneAdmin, loader, source, this);