e8acaecdb89e4ceb22ec8da8932c6217c154152d
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / GremlinPipelineQueryEngine.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.HashSet;
46 import java.util.List;
47 import java.util.Set;
48
49 import org.onap.aai.db.AAIProperties;
50 import org.onap.aai.query.builder.QueryBuilder;
51 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
52 import com.tinkerpop.blueprints.Direction;
53 import com.tinkerpop.blueprints.TransactionalGraph;
54 import com.tinkerpop.blueprints.Vertex;
55 import com.tinkerpop.gremlin.java.GremlinPipeline;
56 import com.tinkerpop.pipes.IdentityPipe;
57 import com.tinkerpop.pipes.PipeFunction;
58 import com.tinkerpop.pipes.branch.LoopPipe;
59
60 public class GremlinPipelineQueryEngine extends QueryEngine {
61
62         public GremlinPipelineQueryEngine(TransactionalGraphEngine graphEngine) {
63                 super(graphEngine);
64         }
65
66         @Override
67         public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) {
68                 List<Vertex> results = null;
69                 Vertex start = query.getStart();
70                 if (start != null) {
71                         results = ((GremlinPipeline)query.getQuery()).cast(Vertex.class).toList();
72                 } else {
73                         GremlinPipeline pipe = new GremlinPipeline(g);
74                         results = process(pipe, (GremlinPipeline)query.getQuery());
75
76                 }
77                 return results;
78         }
79         
80         @Override
81         public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) {
82                 List<Vertex> results = null;
83                 Vertex start = query.getStart();
84                 if (start != null) {
85                         results = ((GremlinPipeline)query.getParentQuery()).cast(Vertex.class).toList();
86                 } else {
87                         GremlinPipeline pipe = new GremlinPipeline(g);
88                         results = process(pipe, (GremlinPipeline)query.getParentQuery());
89
90                 }
91                 return results;
92         }
93
94         @Override
95         public List<Vertex> findParents(Vertex start) {
96                 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").inE()
97                                 .has("isParent", true).outV().loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
98
99                                         @Override
100                                         public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
101                                                 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
102                                                 return pipe.inE().has("isParent", true).count() == 1 || argument.getLoops() < 100;
103                                         }
104                                         
105                                 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
106
107                                         @Override
108                                         public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
109                                                 return true;
110                                         }
111                                         
112                                 });
113                 
114                 List<Vertex> results = pipe.toList();
115                 results.add(0, start);
116                 return results;
117         }
118
119         @Override
120         public List<Vertex> findChildren(Vertex start) {
121                 Set<Vertex> seen = new HashSet<>();
122                 seen.add(start);
123                 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline(start).as("x").outE().has("isParent", true).inV()
124                                 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
125
126                                         @Override
127                                         public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
128                                                 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
129                                                 return pipe.outE().has("isParent", true).count() >= 1 || argument.getLoops() < 100;
130                                         }
131                                         
132                                 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
133
134                                         @Override
135                                         public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
136                                                 return true;
137                                         }
138                                         
139                                 });
140                 
141                 List<Vertex> results = pipe.toList();
142                 results.add(0, start);
143                 return results;
144         }
145
146         @Override
147         public List<Vertex> findDeletable(Vertex start) {
148                 Set<Vertex> seen = new HashSet<>();
149                 seen.add(start);
150                 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start).as("x").outE().or(
151                                 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
152                                 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).inV()
153                                 .except(seen).store(seen).loop("x", new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
154
155                                         @Override
156                                         public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
157                                                 GremlinPipeline<Vertex, Long> pipe = new GremlinPipeline<>(argument.getObject());
158                                                 return pipe.outE().or(
159                                                                 new GremlinPipeline(new IdentityPipe()).has("isParent", true),
160                                                                 new GremlinPipeline(new IdentityPipe()).has("hasDelTarget", true)).count() >= 1 || argument.getLoops() < 100;
161                                         }
162                                         
163                                 }, new PipeFunction<LoopPipe.LoopBundle<Vertex>, Boolean>() {
164
165                                         @Override
166                                         public Boolean compute(LoopPipe.LoopBundle<Vertex> argument) {
167                                                 return true;
168                                         }
169                                         
170                                 });
171                 List<Vertex> results = pipe.toList();
172                 results.add(0, start);
173                 
174                 return results;
175         }
176         
177         private List<Vertex> process(GremlinPipeline start, GremlinPipeline pipe) {
178                 
179                 
180                 return start.add(pipe).cast(Vertex.class).toList();
181         }
182
183         @Override
184         public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
185                 GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<Vertex, Vertex>(start);
186                 switch (direction) {
187                         case OUT:
188                                 pipe.out(label);
189                                 break;
190                         case IN:
191                                 pipe.in(label);
192                                 break;
193                         case BOTH:
194                                 pipe.both(label);
195                                 break;
196                          default:
197                                 break;
198                 }
199                 
200                 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
201                 List<Vertex> result = pipe.toList();
202                 return result;
203         }
204
205 }
206 */