[AAI-178 Amsterdam] Make Edge Properties to be
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / engines / query / GraphTraversalQueryEngine.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 package org.openecomp.aai.serialization.engines.query;
22
23
24 import org.apache.tinkerpop.gremlin.process.traversal.P;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
26 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
28 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.apache.tinkerpop.gremlin.structure.Element;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.openecomp.aai.db.props.AAIProperties;
34 import org.openecomp.aai.introspection.Loader;
35
36 import java.util.List;
37 import java.util.Set;
38
39 import static org.openecomp.aai.serialization.db.AAIDirection.*;
40 import static org.openecomp.aai.serialization.db.EdgeProperty.CONTAINS;
41 import static org.openecomp.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
42
43 /*
44  * This class needs some big explanation despite its compact size.
45  * This controls all the queries performed by the CRUD API in A&AI. 
46  * findParents, findChildren, and findDeletable require special attention
47  *   These methods use 'repeat'. You cannot use 'emit' with repeat currently
48  *   as it is extremely buggy as of tinkerpop-3.0.1-incubating. The way around
49  *   it (for now) is to sideEffect all the vertices we traverse into an ArrayList.
50  * 
51  */
52 public class GraphTraversalQueryEngine extends QueryEngine {
53
54         /**
55          * Instantiates a new graph traversal query engine.
56          *
57          * @param graphEngine the graph engine
58          */
59         public GraphTraversalQueryEngine(GraphTraversalSource g) {
60                 super(g);
61         }
62
63         /**
64          * {@inheritDoc}
65          */
66         @Override
67         public List<Vertex> findParents(Vertex start) {
68                 
69                 final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.union(__.inE().has(CONTAINS.toString(), OUT.toString()).outV(), __.outE().has(CONTAINS.toString(), IN.toString()).inV()));
70                 return pipe.toList();
71         }
72
73         /**
74          * {@inheritDoc}
75          */
76         @Override
77         public List<Vertex> findAllChildren(Vertex start) {
78                 
79                 GraphTraversal<Vertex, Vertex> pipe =  this.g
80                                 .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
81                 
82
83                 return pipe.toList();
84                 
85         }
86
87         public List<Vertex> findChildrenOfType(Vertex start, String type) {
88                 GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
89                                         __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
90                                         __.inE().has(CONTAINS.toString(), IN.toString()).outV()
91                                 ).has(AAIProperties.NODE_TYPE, type).dedup();
92  
93                 return pipe.toList();
94         }
95         
96         public List<Vertex> findChildren(Vertex start) {
97                 GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
98                                         __.outE().has(CONTAINS.toString(), OUT.toString()),
99                                         __.inE().has(CONTAINS.toString(), IN.toString())
100                                 ).otherV().dedup();
101  
102                 return pipe.toList();
103         }
104
105         /**
106          * {@inheritDoc}
107          */
108         @Override
109         public List<Vertex> findDeletable(Vertex start) {
110                 GraphTraversal<Vertex, Vertex> pipe = this.g
111                                 .V(start).emit(v -> true).repeat(
112                                         __.union(
113                                                 __.outE().or(
114                                                         __.has(CONTAINS.toString(), OUT.toString()),
115                                                         __.has(DELETE_OTHER_V.toString(), OUT.toString())
116                                                 ).inV(),
117                                                 __.inE().or(
118                                                                 __.has(CONTAINS.toString(), IN.toString()),
119                                                                 __.has(DELETE_OTHER_V.toString(), IN.toString())
120                                                 ).outV()
121                                         )
122                                 );
123                 
124                 return pipe.toList();
125         }
126
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) {
132                 GraphTraversal<Vertex, Vertex> pipe = this.g.V(start);
133                 switch (direction) {
134                         case OUT:
135                                 pipe.out(label);
136                                 break;
137                         case IN:
138                                 pipe.in(label);
139                                 break;
140                         case BOTH:
141                                 pipe.both(label);
142                                 break;
143                          default:
144                                 break;
145                 }
146                 
147                 pipe.has(AAIProperties.NODE_TYPE, nodeType).dedup();
148                 return pipe.toList();
149         }
150         
151         @Override
152         public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
153                 final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
154                                 __.union(
155                                         __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
156                                         __.inE().has(CONTAINS.toString(), IN.toString()).outV())
157                                 );
158                         
159                 if (!nodeOnly) {
160                         t.union(
161                                         __.identity(),
162                                         __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
163                         );
164                 }
165                 t.tree();
166                 if (t.hasNext()) {
167                         return (Tree)t.next();
168                 } else {
169                         return new Tree();
170                 }
171         }
172
173         @Override
174         public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
175                 final Set<String> objects = loader.getAllObjects().keySet();
176                 GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
177                                 __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
178                                 __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
179                         ).dedup();
180                 
181                 return pipeline.toList();
182         }
183
184
185         @Override
186         public List<Vertex> findCousinVertices(Vertex start) {
187                 GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
188                                 __.inE().has(CONTAINS.toString(), NONE.toString()),
189                                 __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
190                                 
191                 return pipeline.toList();
192         }
193         
194 }
195