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