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