Add the vnf changes
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / query / builder / GremlinTraversal.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.query.builder;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.core.MultivaluedMap;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.openecomp.aai.exceptions.AAIException;
33 import org.openecomp.aai.introspection.Introspector;
34 import org.openecomp.aai.introspection.Loader;
35 import org.openecomp.aai.parsers.query.QueryParser;
36 import org.openecomp.aai.parsers.query.TraversalStrategy;
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) {
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         protected GremlinTraversal(List<String> traversal, Loader loader, GraphTraversalSource source, GremlinQueryBuilder<E> gtb) {
65                 super(loader, source);
66                 this.list = 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         /**
92          * @{inheritDoc}
93          */
94         @Override
95         public QueryParser createQueryFromURI(URI uri, MultivaluedMap<String, String> queryParams)
96                         throws UnsupportedEncodingException, AAIException {
97                 return factory.buildURIParser(uri, queryParams);
98         }
99         
100         /**
101          * @{inheritDoc}
102          */
103         @Override
104         public QueryParser createQueryFromObjectName(String objName) {
105                 return factory.buildObjectNameParser(objName);
106         }
107         
108         /**
109          * @{inheritDoc}
110          */
111         @Override
112         public QueryBuilder<E> newInstance(Vertex start) {
113                 return new GremlinTraversal<>(loader, source, start);
114         }
115
116         /**
117          * @{inheritDoc}
118          */
119         @Override
120         public QueryBuilder<E> newInstance() {
121                 return new GremlinTraversal<>(loader, source);
122         }
123         
124         @Override
125         protected QueryBuilder<E> cloneQueryAtStep(int index) {
126                 if (index == 0) {
127                         index = stepIndex;
128                 }
129                 List<String> newList = new ArrayList<>();
130                 for (int i = 0; i < index; i++) {
131                         newList.add(this.list.get(i));
132                 }
133                 
134                 return new GremlinTraversal<>(newList, loader, source, this);
135         }
136 }