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