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