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