2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.query.builder;
23 import static org.junit.Assert.assertTrue;
25 import java.util.List;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
28 import org.apache.tinkerpop.gremlin.structure.Graph;
29 import org.apache.tinkerpop.gremlin.structure.T;
30 import org.apache.tinkerpop.gremlin.structure.Vertex;
31 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.edges.enums.EdgeType;
36 import org.onap.aai.exceptions.AAIException;
37 import org.onap.aai.introspection.Loader;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.serialization.db.EdgeSerializer;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.test.annotation.DirtiesContext;
43 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
44 public class SimplePathTest extends AAISetup {
49 EdgeSerializer edgeSer;
52 public void setup() throws Exception {
53 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
56 private QueryBuilder<Vertex> buildTestQuery(QueryBuilder<Vertex> qb) throws AAIException {
57 return qb.createEdgeTraversal(EdgeType.TREE, "generic-vnf", "l-interface")
58 .until(qb.newInstance().getVerticesByProperty("aai-node-type", "generic-vnf"))
59 .repeat(qb.newInstance()
60 .union(qb.newInstance().createEdgeTraversal(EdgeType.TREE, "generic-vnf", "l-interface"),
61 qb.newInstance().createEdgeTraversal(EdgeType.TREE, "l-interface", "generic-vnf"),
62 qb.newInstance().createEdgeTraversal(EdgeType.COUSIN, "l-interface", "logical-link"),
63 qb.newInstance().createEdgeTraversal(EdgeType.COUSIN, "logical-link", "l-interface"))
65 .store("x").cap("x").unfold().dedup();
68 private GraphTraversalSource setupGraph() throws AAIException {
69 Graph graph = TinkerGraph.open();
70 GraphTraversalSource g = graph.traversal();
72 Vertex gvnf1 = graph.addVertex(T.label, "generic-vnf", T.id, "00", "aai-node-type", "generic-vnf", "vnf-id",
73 "gvnf1", "vnf-name", "genvnfname1", "nf-type", "sample-nf-type");
75 Vertex lint1 = graph.addVertex(T.label, "l-interface", T.id, "10", "aai-node-type", "l-interface",
76 "interface-name", "lint1", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
78 Vertex loglink1 = graph.addVertex(T.label, "logical-link", T.id, "20", "aai-node-type", "logical-link",
79 "link-name", "loglink1", "in-maint", "false", "link-type", "sausage");
81 Vertex lint2 = graph.addVertex(T.label, "l-interface", T.id, "11", "aai-node-type", "l-interface",
82 "interface-name", "lint2", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
84 Vertex lint3 = graph.addVertex(T.label, "l-interface", T.id, "12", "aai-node-type", "l-interface",
85 "interface-name", "lint3", "is-port-mirrored", "true", "in-maint", "true", "is-ip-unnumbered", "false");
87 Vertex gvnf2 = graph.addVertex(T.label, "generic-vnf", T.id, "01", "aai-node-type", "generic-vnf", "vnf-id",
88 "gvnf2", "vnf-name", "genvnfname2", "nf-type", "sample-nf-type");
90 edgeSer.addTreeEdge(g, gvnf1, lint1);
91 edgeSer.addEdge(g, lint1, loglink1);
92 edgeSer.addEdge(g, loglink1, lint2);
93 edgeSer.addEdge(g, loglink1, lint3);
94 edgeSer.addTreeEdge(g, gvnf2, lint3);
100 public void gremlinQueryTest() throws AAIException {
101 GraphTraversalSource g = setupGraph();
102 List<Vertex> expected = g.V("01").toList();
103 Vertex start = g.V("00").toList().get(0);
105 GremlinTraversal<Vertex> qb = new GremlinTraversal<>(loader, g, start);
106 QueryBuilder<Vertex> q = buildTestQuery(qb);
107 List<Vertex> results = q.toList();
108 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));
112 public void traversalQueryTest() throws AAIException {
113 GraphTraversalSource g = setupGraph();
114 List<Vertex> expected = g.V("01").toList();
115 Vertex start = g.V("00").toList().get(0);
117 TraversalQuery<Vertex> qb = new TraversalQuery<>(loader, g, start);
118 QueryBuilder<Vertex> q = buildTestQuery(qb);
119 List<Vertex> results = q.toList();
120 assertTrue("results match", expected.containsAll(results) && results.containsAll(expected));