55002a6f0dd196a2821dc82c9ac880f8c99bd2bc
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / engines / query / GraphTraversalQueryEngineTest.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
24 import static org.junit.Assert.*;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Set;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
36 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
37 import org.apache.tinkerpop.gremlin.structure.Direction;
38 import org.apache.tinkerpop.gremlin.structure.Edge;
39 import org.apache.tinkerpop.gremlin.structure.Element;
40 import org.apache.tinkerpop.gremlin.structure.Graph;
41 import org.apache.tinkerpop.gremlin.structure.T;
42 import org.apache.tinkerpop.gremlin.structure.Vertex;
43 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
44 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex;
45 import org.junit.Test;
46 import org.onap.aai.AAISetup;
47 import org.onap.aai.exceptions.AAIException;
48 import org.onap.aai.introspection.Loader;
49 import org.onap.aai.introspection.LoaderFactory;
50 import org.onap.aai.introspection.ModelType;
51 import org.onap.aai.introspection.Version;
52 import org.onap.aai.serialization.db.EdgeRules;
53
54 public class GraphTraversalQueryEngineTest extends AAISetup {
55
56         @Test
57         public void testFindParents() throws AAIException {
58                 //setup
59                 EdgeRules rules = EdgeRules.getInstance();
60                 Graph graph = TinkerGraph.open();
61                 
62                 Vertex cloudreg = graph.addVertex(T.id, "00", "aai-node-type", "cloud-region");
63                 Vertex tenant = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
64                 Vertex vserver = graph.addVertex(T.id, "20", "aai-node-type", "vserver");
65                 
66                 GraphTraversalSource g = graph.traversal();
67                 
68                 rules.addTreeEdge(g, cloudreg, tenant);
69                 rules.addTreeEdge(g, tenant, vserver);
70                 
71                 //expect start vertex back plus any parents
72                 List<Vertex> crExpected = new ArrayList<>(Arrays.asList(cloudreg)); //no parents
73                 List<Vertex> tenExpected = new ArrayList<>(Arrays.asList(tenant, cloudreg)); //only has the immediate parent
74                 List<Vertex> vsExpected = new ArrayList<>(Arrays.asList(vserver, tenant, cloudreg)); //parent & grandparent
75                 
76                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
77                 
78                 //test
79                 List<Vertex> crRes = engine.findParents(cloudreg);
80                 assertTrue(crRes.containsAll(crExpected) && crExpected.containsAll(crRes));
81                 
82                 List<Vertex> tenRes = engine.findParents(tenant);
83                 assertTrue(tenRes.containsAll(tenExpected) && tenExpected.containsAll(tenRes));
84                 
85                 List<Vertex> vsRes = engine.findParents(vserver);
86                 assertTrue(vsRes.containsAll(vsExpected) && vsExpected.containsAll(vsRes));
87                 //verify expected ordering - start, parent, grandparent
88                 assertTrue(vsRes.get(0).equals(vserver));
89                 assertTrue(vsRes.get(1).equals(tenant));
90                 assertTrue(vsRes.get(2).equals(cloudreg));
91         }
92         
93         @Test
94         public void testFindAllChildren() throws AAIException {
95                 //setup
96                 EdgeRules rules = EdgeRules.getInstance();
97                 Graph graph = TinkerGraph.open();
98                 
99                 Vertex cloudreg = graph.addVertex(T.id, "00", "aai-node-type", "cloud-region");
100                 Vertex tenant = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
101                 Vertex vserver = graph.addVertex(T.id, "20", "aai-node-type", "vserver");
102                 Vertex vserver2 = graph.addVertex(T.id, "21", "aai-node-type", "vserver");
103                 Vertex oam = graph.addVertex(T.id, "30", "aai-node-type", "oam-network");
104                 
105                 GraphTraversalSource g = graph.traversal();
106                 
107                 rules.addTreeEdge(g, cloudreg, tenant);
108                 rules.addTreeEdge(g, tenant, vserver);
109                 rules.addTreeEdge(g, tenant, vserver2);
110                 rules.addTreeEdge(g, cloudreg, oam);
111                 
112                 List<Vertex> crExpected = new ArrayList<>(Arrays.asList(cloudreg, tenant, vserver, vserver2, oam));
113                 List<Vertex> vsExpected = new ArrayList<>(Arrays.asList(vserver));
114                 
115                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
116                 
117                 //test
118                 List<Vertex> crRes = engine.findAllChildren(cloudreg);
119                 assertTrue(crRes.containsAll(crExpected) && crExpected.containsAll(crRes));
120                 
121                 List<Vertex> vsRes = engine.findAllChildren(vserver);
122                 assertTrue(vsRes.containsAll(vsExpected) && vsExpected.containsAll(vsRes));
123         }
124         
125         @Test
126         public void testFindChildrenOfType() throws AAIException {
127                 //setup
128                 EdgeRules rules = EdgeRules.getInstance();
129                 Graph graph = TinkerGraph.open();
130                 
131                 Vertex gv = graph.addVertex(T.id, "00", "aai-node-type", "generic-vnf");
132                 Vertex lint1 = graph.addVertex(T.id, "10", "aai-node-type", "l-interface");
133                 Vertex lint2 = graph.addVertex(T.id, "11", "aai-node-type", "l-interface");
134                 Vertex lag = graph.addVertex(T.id, "20", "aai-node-type", "lag-interface");
135                 Vertex lint3 = graph.addVertex(T.id, "12", "aai-node-type", "l-interface");
136                 
137                 GraphTraversalSource g = graph.traversal();
138                 
139                 rules.addTreeEdge(g, gv, lint1);
140                 rules.addTreeEdge(g, gv, lint2);
141                 rules.addTreeEdge(g, gv, lag);
142                 rules.addTreeEdge(g, lag, lint3);
143                 
144                 List<Vertex> expected = new ArrayList<>(Arrays.asList(lint1, lint2));
145                 
146                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
147                 
148                 //test
149                 List<Vertex> results = engine.findChildrenOfType(gv, "l-interface");
150                 assertTrue(results.containsAll(expected) && expected.containsAll(results));
151         }
152         
153         @Test
154         public void testFindChildren() throws AAIException {
155                 //setup
156                 EdgeRules rules = EdgeRules.getInstance();
157                 Graph graph = TinkerGraph.open();
158                 
159                 Vertex gv = graph.addVertex(T.id, "00", "aai-node-type", "generic-vnf");
160                 Vertex lint1 = graph.addVertex(T.id, "10", "aai-node-type", "l-interface");
161                 Vertex lint2 = graph.addVertex(T.id, "11", "aai-node-type", "l-interface");
162                 Vertex lag = graph.addVertex(T.id, "20", "aai-node-type", "lag-interface");
163                 Vertex lint3 = graph.addVertex(T.id, "12", "aai-node-type", "l-interface");
164                 
165                 GraphTraversalSource g = graph.traversal();
166                 
167                 rules.addTreeEdge(g, gv, lint1);
168                 rules.addTreeEdge(g, gv, lint2);
169                 rules.addTreeEdge(g, gv, lag);
170                 rules.addTreeEdge(g, lag, lint3);
171                 
172                 List<Vertex> expected = new ArrayList<>(Arrays.asList(lint1, lint2, lag));
173                 
174                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
175                 
176                 //test
177                 List<Vertex> results = engine.findChildren(gv);
178                 assertTrue(results.containsAll(expected) && expected.containsAll(results));
179         }
180
181         @Test
182         public void testFindDeletable() throws AAIException {
183                 //setup
184                 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
185                 
186                 Graph graph = TinkerGraph.open();
187                 Vertex parent = graph.addVertex(T.id, "00", "aai-node-type", "test-parent");
188                 Vertex child = graph.addVertex(T.id, "10", "aai-node-type", "test-child");
189                 Vertex cousin = graph.addVertex(T.id, "20", "aai-node-type", "test-cousin");
190                 Vertex grandchild = graph.addVertex(T.id, "30", "aai-node-type", "test-grandchild");
191
192                 GraphTraversalSource g = graph.traversal();
193                 
194                 rules.addTreeEdge(g, parent, child); //delete-other-v=none, no cascade
195                 rules.addTreeEdge(g, child, grandchild); //d-o-v=out, yes from child
196                 rules.addEdge(g, cousin, child); //d-o-v=out, yes from cousin
197                 
198                 List<Vertex> parentExpected = new ArrayList<>(Arrays.asList(parent));
199                 List<Vertex> childExpected = new ArrayList<>(Arrays.asList(child, grandchild));
200                 List<Vertex> cousinExpected = new ArrayList<>(Arrays.asList(cousin, child, grandchild));
201                 
202                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
203                 
204                 //tests
205                 List<Vertex> parentDeletes = engine.findDeletable(parent);
206                 assertTrue(parentExpected.containsAll(parentDeletes) && parentDeletes.containsAll(parentExpected));
207                 
208                 List<Vertex> childDeletes = engine.findDeletable(child);
209                 assertTrue(childExpected.containsAll(childDeletes) && childDeletes.containsAll(childExpected));
210                 
211                 List<Vertex> cousinDeletes = engine.findDeletable(cousin);
212                 assertTrue(cousinExpected.containsAll(cousinDeletes) && cousinDeletes.containsAll(cousinExpected));
213         }
214         
215         @Test
216         public void testFindRelatedVertices() throws AAIException {
217                 //setup
218                 EdgeRules rules = EdgeRules.getInstance();
219                 
220                 Graph graph = TinkerGraph.open();
221                 
222                 Vertex gv = graph.addVertex(T.id, "00", "aai-node-type", "generic-vnf");
223                 Vertex lint = graph.addVertex(T.id, "10", "aai-node-type", "l-interface");
224                 Vertex lint2 = graph.addVertex(T.id, "11", "aai-node-type", "l-interface");
225                 Vertex log = graph.addVertex(T.id, "20", "aai-node-type", "logical-link");
226                 
227                 GraphTraversalSource g = graph.traversal();
228                 
229                 rules.addTreeEdge(g, gv, lint);
230                 rules.addEdge(g, lint, log);
231                 rules.addEdge(g, log, lint2);
232                 
233                 List<Vertex> outExpected = new ArrayList<>(Arrays.asList(lint));
234                 List<Vertex> inExpected = new ArrayList<>(Arrays.asList(lint, lint2));
235                 List<Vertex> bothExpected = new ArrayList<>(Arrays.asList(log));
236                 
237                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
238                         
239                 //test
240                 List<Vertex> outRes = engine.findRelatedVertices(gv, Direction.IN, "org.onap.relationships.inventory.BelongsTo", "l-interface");
241                 assertTrue(outRes.containsAll(outExpected) && outExpected.containsAll(outRes));
242
243                 List<Vertex> inRes = engine.findRelatedVertices(log, Direction.IN, "tosca.relationships.network.LinksTo", "l-interface");
244                 assertTrue(inRes.containsAll(inExpected) && inExpected.containsAll(inRes));
245                 
246                 List<Vertex> bothRes = engine.findRelatedVertices(lint, Direction.BOTH, "tosca.relationships.network.LinksTo", "logical-link");
247                 assertTrue(bothRes.containsAll(bothExpected) && bothExpected.containsAll(bothRes));
248         }
249         
250         @Test
251         public void testFindSubGraph() throws AAIException {
252                 //setup
253                 EdgeRules rules = EdgeRules.getInstance();
254                 Graph graph = TinkerGraph.open();
255                 
256                 Vertex cr = graph.addVertex(T.id, "00", "aai-node-type", "cloud-region");
257                 Vertex ten = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
258                 Vertex ten2 = graph.addVertex(T.id, "11", "aai-node-type", "tenant");
259                 Vertex vs = graph.addVertex(T.id, "20", "aai-node-type", "vserver");
260                 Vertex vs2 = graph.addVertex(T.id, "21", "aai-node-type", "vserver");
261                 Vertex lint = graph.addVertex(T.id, "30", "aai-node-type", "l-interface");
262                 Vertex comp = graph.addVertex(T.id, "40", "aai-node-type", "complex");
263                 Vertex ctag = graph.addVertex(T.id, "50", "aai-node-type", "ctag-pool");
264                 Vertex gv = graph.addVertex(T.id, "60", "aai-node-type", "generic-vnf");
265                 Vertex lag = graph.addVertex(T.id, "70", "aai-node-type", "lag-interface");
266                 Vertex lint2 = graph.addVertex(T.id, "31", "aai-node-type", "l-interface");
267                 Vertex log = graph.addVertex(T.id, "80", "aai-node-type", "logical-link");
268                 Vertex vnfc = graph.addVertex(T.id, "90", "aai-node-type", "vnfc");
269                 
270                 GraphTraversalSource g = graph.traversal();
271                 
272                 Edge crTen = rules.addTreeEdge(g, cr, ten);
273                 Edge crTen2 = rules.addTreeEdge(g, cr, ten2);
274                 Edge tenVs = rules.addTreeEdge(g, ten, vs);
275                 Edge tenVs2 = rules.addTreeEdge(g, ten, vs2);
276                 Edge vsLInt = rules.addTreeEdge(g, vs, lint);
277                 Edge lintLog = rules.addEdge(g, lint, log);
278                 Edge vsGv = rules.addEdge(g, vs, gv);
279                 rules.addEdge(g, gv, vnfc);
280                 
281                 rules.addTreeEdge(g, gv, lag);
282                 rules.addTreeEdge(g, lag, lint2);
283                 
284                 rules.addTreeEdge(g, comp, ctag);
285                 Edge crComp = rules.addEdge(g, cr, comp);
286                 
287                 //findSubGraph(cr, 0, true)
288                 List<Element> expected1 = new ArrayList<>(Arrays.asList(cr));
289                 //findSubGraph(cr, 2, true)
290                 List<Element> expected2 = new ArrayList<>(Arrays.asList(cr, ten, ten2, vs, vs2,
291                                                                                                                                         crTen, crTen2, tenVs, tenVs2));
292                 //findSubGraph(cr)
293                 List<Element> expected3 = new ArrayList<>(Arrays.asList(cr, ten, ten2, comp, vs, vs2, lint, gv, log,
294                                                                                                                                 crTen, crTen2, crComp, tenVs, tenVs2, vsLInt, 
295                                                                                                                                 vsGv, lintLog));
296                 
297                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
298                 
299                 //test
300                 Tree<Element> res1 = engine.findSubGraph(cr, 0, true);
301                 Set<Element> resList1 = treeToList(res1);
302                 assertTrue(resList1.containsAll(expected1) && expected1.containsAll(resList1));
303                 
304                 Tree<Element> res2 = engine.findSubGraph(cr, 2, true);
305                 Set<Element> resList2 = treeToList(res2);
306                 assertTrue(resList2.containsAll(expected2) && expected2.containsAll(resList2));
307                 
308                 Tree<Element> res3 = engine.findSubGraph(cr);
309                 Set<Element> resList3 = treeToList(res3);
310                 assertTrue(resList3.containsAll(expected3) && expected3.containsAll(resList3));
311         }
312         
313         /**
314          * convenience helper method to make it easier to check the contents of the tree against 
315          * a list of expected results
316          * @param tree - the tree whose contents you want in collection form
317          * @return set of the contents of the tree
318          */
319         private Set<Element> treeToList(Tree<Element> tree) {
320                 Set<Element> ret = new HashSet<>();
321                 
322                 for (Element key : tree.keySet()) {
323                         ret.add(key);
324                         ret.addAll(treeToList(tree.get(key)));
325                 }
326                 
327                 return ret;
328         }
329         
330         @Test
331         public void testFindEdgesForVersion() throws AAIException {
332                 //setup
333                 EdgeRules rules = EdgeRules.getInstance();
334                 Graph graph = TinkerGraph.open();
335                 
336                 Vertex gv = graph.addVertex(T.id, "00", "aai-node-type", "generic-vnf");
337                 Vertex vnfc = graph.addVertex(T.id, "10", "aai-node-type", "vnfc");
338                 Vertex lob = graph.addVertex(T.id, "20", "aai-node-type", "line-of-business");
339                 Vertex lint = graph.addVertex(T.id, "30", "aai-node-type", "l-interface");
340                 
341                 GraphTraversalSource g = graph.traversal();
342                 
343                 rules.addTreeEdge(g, gv, lint); //tree edge so shouldn't appear in results
344                 Edge gvVnfc = rules.addEdge(g, gv, vnfc);
345                 rules.addEdge(g, gv, lob); //v11/12 not v10
346                 
347                 List<Edge> expected = new ArrayList<>(Arrays.asList(gvVnfc));
348                 
349                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
350                 
351                 //test
352                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v10);
353                 List<Edge> results = engine.findEdgesForVersion(gv, loader);
354                 assertTrue(results.containsAll(expected) && expected.containsAll(results));
355         }
356         
357         @Test
358         public void testFindCousinVertices() throws AAIException {
359                 //setup
360                 EdgeRules rules = EdgeRules.getInstance();
361                 Graph graph = TinkerGraph.open();
362                 
363                 Vertex gv = graph.addVertex(T.id, "00", "aai-node-type", "generic-vnf");
364                 Vertex vnfc = graph.addVertex(T.id, "10", "aai-node-type", "vnfc");
365                 Vertex lob = graph.addVertex(T.id, "20", "aai-node-type", "line-of-business");
366                 Vertex lint = graph.addVertex(T.id, "30", "aai-node-type", "l-interface");
367                 
368                 GraphTraversalSource g = graph.traversal();
369                 
370                 rules.addTreeEdge(g, gv, lint); //tree edge so shouldn't appear in results
371                 rules.addEdge(g, gv, vnfc);
372                 rules.addEdge(g, gv, lob); 
373                 
374                 List<Vertex> expected = new ArrayList<>(Arrays.asList(vnfc, lob));
375                 
376                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
377                 
378                 //test
379                 List<Vertex> results = engine.findCousinVertices(gv);
380                 assertTrue(results.containsAll(expected) && expected.containsAll(results));
381         }
382 }