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