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