Change openecomp to onap and update license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / GremlinQueryEngine.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.serialization.engines.query;/*-
23  * ============LICENSE_START=======================================================
24  * org.onap.aai
25  * ================================================================================
26  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
27  * ================================================================================
28  * Licensed under the Apache License, Version 2.0 (the "License");
29  * you may not use this file except in compliance with the License.
30  * You may obtain a copy of the License at
31  * 
32  *      http://www.apache.org/licenses/LICENSE-2.0
33  * 
34  * Unless required by applicable law or agreed to in writing, software
35  * distributed under the License is distributed on an "AS IS" BASIS,
36  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37  * See the License for the specific language governing permissions and
38  * limitations under the License.
39  * ============LICENSE_END=========================================================
40  */
41
42 //
43 //package org.onap.aai.serialization.engines.query;
44 //
45 //import java.util.List;
46 //import java.util.regex.Matcher;
47 //import java.util.regex.Pattern;
48 //
49 //import org.apache.commons.collections.IteratorUtils;
50 //
51 //import org.onap.aai.db.AAIProperties;
52 //import org.onap.aai.query.builder.QueryBuilder;
53 //import org.onap.aai.serialization.engines.TransactionalGraphEngine;
54 //import com.tinkerpop.blueprints.Direction;
55 //import com.tinkerpop.blueprints.Graph;
56 //import com.tinkerpop.blueprints.TransactionalGraph;
57 //import com.tinkerpop.blueprints.Vertex;
58 //import com.tinkerpop.gremlin.groovy.Gremlin;
59 //import com.tinkerpop.gremlin.java.GremlinPipeline;
60 //import com.tinkerpop.pipes.Pipe;
61 //import com.tinkerpop.pipes.util.iterators.SingleIterator;
62 //
63 //public class GremlinQueryEngine extends QueryEngine {
64 //      
65 //      public GremlinQueryEngine (TransactionalGraphEngine engine) {
66 //              super(engine);
67 //
68 //      }
69 //      
70 //
71 //      @Override
72 //      public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
73 //              List<Vertex> result = null;
74 //              Vertex start = query.getStart();
75 //              if (start != null) {
76 //                      result = this.executeQuery(start, (String)query.getQuery());
77 //              } else {
78 //                      result = this.processGremlinQuery((String)query.getQuery());
79 //              }
80 //              return result;
81 //
82 //      }
83 //      
84 //      @Override
85 //      public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
86 //              
87 //              List<Vertex> result = null;
88 //              Vertex start = query.getStart();
89 //              if (start != null) {
90 //                      result = this.executeQuery(start, (String)query.getParentQuery());
91 //              } else {
92 //                      result = this.processGremlinQuery((String)query.getParentQuery());
93 //              }
94 //              return result;
95 //      }
96 //      
97 //      private List<Vertex> executeQuery(Vertex startVertex, String query) {
98 //              
99 //              return this.processGremlinQuery(startVertex, "_()" + query);
100 //
101 //      }
102 //
103 //      @Override
104 //      public List<Vertex> findParents(Vertex start) {
105 //              
106 //              String findAllParents = ".as('x').inE.has('isParent', true).outV"
107 //                              + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}";
108 //              
109 //              List<Vertex> results = this.executeQuery(start, findAllParents);
110 //              results.add(0, start);
111 //              return results;
112 //              
113 //      }
114 //      
115 //      @Override
116 //      public List<Vertex> findChildren(Vertex start) {
117 //              String findAllChildren = ".as('x').outE.has('isParent', true).inV"
118 //                              + ".loop('x'){it.object.outE.has('isParent', true).count() >= 1}{true}";
119 //              
120 //              List<Vertex> results = this.executeQuery(start, findAllChildren);
121 //              results.add(0, start);
122 //              return results;
123 //              
124 //      }
125 //      
126 //      @Override
127 //      public List<Vertex> findDeletable(Vertex start) {
128 //              String findAllChildren = ".as('x').outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).inV"
129 //                              + ".loop('x'){it.object.outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).count() >= 1}{true}";
130 //              
131 //              List<Vertex> results = this.executeQuery(start, findAllChildren);
132 //              results.add(0, start);
133 //              return results;
134 //      }
135 //      private List<Vertex> processGremlinQuery(String query) {
136 //              
137 //              Pattern firstHasSet = Pattern.compile("^(\\.has\\(.*?\\))(\\.has\\(.*?\\))*(?!\\.has)");
138 //              Pattern p = Pattern.compile("\\.has\\('(.*?)',\\s?'(.*?)'\\)");
139 //              Matcher m = firstHasSet.matcher(query);
140 //              List<Vertex> results = null;
141 //              GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<>(dbEngine.getGraph());
142 //              if (m.find()) {
143 //                      String hasSet = m.group();
144 //                      query = query.replace(m.group(0), "");
145 //                      m = p.matcher(hasSet);
146 //                      pipe.V();
147 //                      while (m.find()) {
148 //                              pipe.has(m.group(1), m.group(2));
149 //                      }
150 //                      results = processGremlinQuery(pipe.toList(), "_()" + query);
151 //              }
152 //              
153 //              return results;
154 //              
155 //      }
156 //      private List<Vertex> processGremlinQuery(Vertex startVertex, String query) {
157 //              
158 //              Pipe pipe = Gremlin.compile(query);
159 //              pipe.setStarts(new SingleIterator<Vertex>(startVertex));
160 //              
161 //              return (List<Vertex>)IteratorUtils.toList(pipe.iterator());
162 //      }
163 //      private List<Vertex> processGremlinQuery(List<Vertex> list, String query) {
164 //              
165 //              Pipe pipe = Gremlin.compile(query);
166 //              
167 //              pipe.setStarts(list);
168 //              
169 //              return (List<Vertex>)IteratorUtils.toList(pipe.iterator());             
170 //      }
171 //
172 //
173 //      @Override
174 //      public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
175 //              String findRelatedVertices = "_()";
176 //              switch (direction) {
177 //                      case OUT:
178 //                              findRelatedVertices += ".out('" + label + "')";
179 //                              break;
180 //                      case IN:
181 //                              findRelatedVertices += ".in('" + label + "')";
182 //                              break;
183 //                      case BOTH:
184 //                              findRelatedVertices += ".both('" + label + "')";
185 //                              break;
186 //                       default:
187 //                              break;
188 //              }
189 //              findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()";
190 //              List<Vertex> results = this.executeQuery(start, findRelatedVertices);
191 //              results.add(0, start);
192 //              return results;
193 //      }
194 //      
195 //}
196 //