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