Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / query / builder / TraversalQuery.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.onap.aai.query.builder;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.util.List;
26
27 import javax.ws.rs.core.MultivaluedMap;
28
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;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.introspection.Loader;
36 import org.onap.aai.parsers.query.QueryParser;
37 import org.onap.aai.parsers.query.TraversalStrategy;
38
39 /**
40  * The Class TraversalQuery.
41  */
42 public class TraversalQuery<E> extends GraphTraversalBuilder<E> {
43
44     /**
45      * Instantiates a new traversal query.
46      *
47      * @param loader the loader
48      */
49
50     public TraversalQuery(Loader loader, GraphTraversalSource source) {
51         super(loader, source);
52         this.factory = new TraversalStrategy(this.loader, this);
53     }
54
55     /**
56      * Instantiates a new traversal query.
57      *
58      * @param loader the loader
59      * @param start the start
60      */
61     public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start) {
62         super(loader, source, start);
63         this.factory = new TraversalStrategy(this.loader, this);
64     }
65
66     protected TraversalQuery(GraphTraversal<Vertex, E> traversal, Loader loader, GraphTraversalSource source,
67             GraphTraversalBuilder<E> gtb) {
68         super(loader, source);
69         this.traversal = traversal;
70         this.stepIndex = gtb.getStepIndex();
71         this.parentStepIndex = gtb.getParentStepIndex();
72         this.containerStepIndex = gtb.getContainerStepIndex();
73         this.factory = new TraversalStrategy(this.loader, this);
74         this.start = gtb.getStart();
75     }
76
77     /**
78      * @{inheritDoc}
79      */
80     @Override
81     public QueryParser createQueryFromURI(URI uri) throws UnsupportedEncodingException, AAIException {
82         return factory.buildURIParser(uri);
83     }
84
85     /**
86      * @{inheritDoc}
87      */
88     @Override
89     public QueryParser createQueryFromRelationship(Introspector relationship)
90             throws UnsupportedEncodingException, AAIException {
91         return factory.buildRelationshipParser(relationship);
92     }
93
94     /**
95      * @{inheritDoc}
96      */
97     @Override
98     public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
99             throws UnsupportedEncodingException, AAIException {
100         return factory.buildURIParser(uri, queryParams);
101     }
102
103     /**
104      * @{inheritDoc}
105      */
106     @Override
107     public QueryParser createQueryFromObjectName(String objName) {
108         return factory.buildObjectNameParser(objName);
109     }
110
111     /**
112      * @{inheritDoc}
113      */
114     @Override
115     public QueryBuilder<E> newInstance(Vertex start) {
116         return new TraversalQuery<>(loader, source, start);
117     }
118
119     /**
120      * @{inheritDoc}
121      */
122     @Override
123     public QueryBuilder<E> newInstance() {
124         return new TraversalQuery<>(loader, source);
125     }
126
127     @Override
128     public QueryBuilder<E> fold() {
129         return this;
130     }
131
132     @Override
133     public QueryBuilder<E> id() {
134         return this;
135     }
136
137     @Override
138     protected QueryBuilder<E> cloneQueryAtStep(int index) {
139         GraphTraversal.Admin<Vertex, E> cloneAdmin = getCloneAdmin(index);
140         return new TraversalQuery<>(cloneAdmin, loader, source, this);
141     }
142
143     protected GraphTraversal.Admin<Vertex, E> getCloneAdmin(int index) {
144         int idx = index;
145
146         if (idx == 0) {
147             idx = stepIndex;
148         }
149
150         GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
151         GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
152         List<Step> steps = cloneAdmin.getSteps();
153
154         for (int i = steps.size() - 1; i >= idx; i--) {
155             cloneAdmin.removeStep(i);
156         }
157         return cloneAdmin;
158     }
159
160     @Override
161     protected QueryBuilder<E> removeQueryStepsBetween(int start, int end) {
162         GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
163         GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
164
165         for (int i = end - 2; i >= start; i--) {
166             cloneAdmin.removeStep(i);
167         }
168         return new TraversalQuery<>(cloneAdmin, loader, source, this);
169     }
170 }