Change openecomp to onap and update license
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.query.builder;
23
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.util.List;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
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
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.Introspector;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.parsers.query.QueryParser;
39 import org.onap.aai.parsers.query.TraversalStrategy;
40
41 /**
42  * The Class TraversalQuery.
43  */
44 public class TraversalQuery<E> extends GraphTraversalBuilder<E> {
45
46         /**
47          * Instantiates a new traversal query.
48          *
49          * @param loader the loader
50          */
51         public TraversalQuery(Loader loader, GraphTraversalSource source) {
52                 super(loader, source);
53                 this.factory = new TraversalStrategy(this.loader, this);
54         }
55         
56         /**
57          * Instantiates a new traversal query.
58          *
59          * @param loader the loader
60          * @param start the start
61          */
62         public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start) {
63                 super(loader, source, start);
64                 this.factory = new TraversalStrategy(this.loader, this);
65         }
66         
67         protected TraversalQuery(GraphTraversal<Vertex, E> traversal, Loader loader, GraphTraversalSource source, 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) throws UnsupportedEncodingException, AAIException {
90                 return factory.buildRelationshipParser(relationship);
91         }
92
93         /**
94          * @{inheritDoc}
95          */
96         @Override
97         public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
98                         throws UnsupportedEncodingException, AAIException {
99                 return factory.buildURIParser(uri, queryParams);
100         }
101         
102         /**
103          * @{inheritDoc}
104          */
105         @Override
106         public QueryParser createQueryFromObjectName(String objName) {
107                 return factory.buildObjectNameParser(objName);
108         }
109
110         /**
111          * @{inheritDoc}
112          */
113         @Override
114         public QueryBuilder<E> newInstance(Vertex start) {
115                 return new TraversalQuery<>(loader, source, start);
116         }
117         
118         /**
119          * @{inheritDoc}
120          */
121         @Override
122         public QueryBuilder<E> newInstance() {
123                 return new TraversalQuery<>(loader, source);
124         }
125         
126         @Override
127         protected QueryBuilder<E> cloneQueryAtStep(int index) {
128                 if (index == 0) {
129                         index = stepIndex;
130                 }
131                 GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
132                 GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
133                 List<Step> steps = cloneAdmin.getSteps();
134
135                 for (int i = steps.size()-1; i >= index; i--) {
136                         cloneAdmin.removeStep(i);
137                 }
138                 return new TraversalQuery<>(cloneAdmin, loader, source, this);
139         }
140         
141         @Override
142         protected QueryBuilder<E> removeQueryStepsBetween(int start, int end) {
143                 GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone();
144                 GraphTraversal.Admin<Vertex, E> cloneAdmin = clone.asAdmin();
145
146                 for (int i = end-2; i >= start; i--) {
147                         cloneAdmin.removeStep(i);
148                 }
149                 return new TraversalQuery<>(cloneAdmin, loader, source, this);
150         }
151 }