Open Kilda Java Documentation
NeoDriverTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.pce.provider;
17 
24 
25 import org.junit.AfterClass;
26 import org.junit.Assert;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.neo4j.driver.v1.AuthTokens;
30 import org.neo4j.driver.v1.GraphDatabase;
31 import org.neo4j.graphdb.GraphDatabaseService;
32 import org.neo4j.graphdb.Label;
33 import org.neo4j.graphdb.Node;
34 import org.neo4j.graphdb.Relationship;
35 import org.neo4j.graphdb.RelationshipType;
36 import org.neo4j.graphdb.Transaction;
37 import org.neo4j.graphdb.factory.GraphDatabaseFactory;
38 import org.neo4j.io.fs.FileUtils;
39 import org.neo4j.kernel.configuration.BoltConnector;
40 
41 import java.io.File;
42 import java.util.List;
43 
44 public class NeoDriverTest {
45 
46  private NeoDriver target = new NeoDriver(GraphDatabase.driver("bolt://localhost:7878",
47  AuthTokens.basic("neo4j", "neo4j")));
48  private static GraphDatabaseService graphDb;
49 
50  private static final File databaseDirectory = new File("target/neo4j-test-db");
51 
52  @BeforeClass
53  public static void setUpOnce() throws Exception {
54  FileUtils.deleteRecursively(databaseDirectory); // delete neo db file
55 
56  // This next area enables Kilda to connect to the local db
57  BoltConnector bolt = new BoltConnector("0");
58  graphDb = new GraphDatabaseFactory()
59  .newEmbeddedDatabaseBuilder(databaseDirectory)
60  .setConfig(bolt.type, "BOLT")
61  .setConfig(bolt.enabled, "true")
62  .setConfig(bolt.listen_address, "localhost:7878")
63  .newGraphDatabase();
64  }
65 
66  @AfterClass
67  public static void tearDownOnce() {
68  graphDb.shutdown();
69  }
70 
71 
72  @Test
73  public void getAllFlows() {
74  try (Transaction tx = graphDb.beginTx()) {
75  Node node1 = graphDb.createNode(Label.label("switch"));
76  node1.setProperty("name", "00:01");
77  Node node2 = graphDb.createNode(Label.label("switch"));
78  node2.setProperty("name", "00:02");
79  Relationship rel1 = node1.createRelationshipTo(node2, RelationshipType.withName("flow"));
80  rel1.setProperty("flowid", "f1");
81  rel1.setProperty("cookie", 3);
82  rel1.setProperty("meter_id", 2);
83  rel1.setProperty("transit_vlan", 1);
84  rel1.setProperty("src_switch", "00:01");
85  rel1.setProperty("dst_switch", "00:02");
86  rel1.setProperty("src_port", 1);
87  rel1.setProperty("dst_port", 2);
88  rel1.setProperty("src_vlan", 5);
89  rel1.setProperty("dst_vlan", 5);
90  rel1.setProperty("path", "\"{\"path\": [], \"latency_ns\": 0, \"timestamp\": 1522528031909}\"");
91  rel1.setProperty("bandwidth", 200);
92  rel1.setProperty("ignore_bandwidth", true);
93  rel1.setProperty("description", "description");
94  rel1.setProperty("last_updated", "last_updated");
95  tx.success();
96  }
97 
98  List<Flow> flows = target.getAllFlows();
99  Flow flow = flows.get(0);
100  Assert.assertEquals(3, flow.getCookie());
101  Assert.assertEquals("f1", flow.getFlowId());
102  Assert.assertEquals(true, flow.isIgnoreBandwidth());
103  }
104 
105 
106  @Test
107  public void getAllIsl() {
108  try (Transaction tx = graphDb.beginTx()) {
109  Node node1 = graphDb.createNode(Label.label("switch"));
110  node1.setProperty("name", "00:01");
111  Node node2 = graphDb.createNode(Label.label("switch"));
112  node2.setProperty("name", "00:02");
113  Relationship rel1 = node1.createRelationshipTo(node2, RelationshipType.withName("isl"));
114  rel1.setProperty("src_switch", "00:01");
115  rel1.setProperty("src_port", 1);
116  rel1.setProperty("dst_switch", "00:02");
117  rel1.setProperty("dst_port", 2);
118  rel1.setProperty("speed", 200);
119  rel1.setProperty("max_bandwidth", 300);
120  rel1.setProperty("latency", 400);
121  rel1.setProperty("available_bandwidth", 500);
122  rel1.setProperty("status", "active");
123 
124  Relationship rel2 = node2.createRelationshipTo(node1, RelationshipType.withName("isl"));
125  rel2.setProperty("src_switch", "00:02");
126  rel2.setProperty("src_port", 3);
127  rel2.setProperty("dst_switch", "00:01");
128  rel2.setProperty("dst_port", 4);
129  rel2.setProperty("speed", 600);
130  rel2.setProperty("max_bandwidth", 700);
131  rel2.setProperty("latency", 800);
132  rel2.setProperty("available_bandwidth", 900);
133  rel2.setProperty("status", "inactive");
134 
135  tx.success();
136  }
137 
138  List<IslInfoData> isls = target.getIsls();
139  IslInfoData isl = isls.get(0);
140  Assert.assertEquals(200, isl.getSpeed());
141  Assert.assertEquals(new SwitchId("00:01"), isl.getPath().get(0).getSwitchId());
142  Assert.assertEquals(IslChangeType.DISCOVERED, isl.getState());
143 
144  isl = isls.get(1);
145  Assert.assertEquals(IslChangeType.FAILED, isl.getState());
146 
147  }
148 
149 
150  @Test
151  public void getAllSwitches() {
152  try (Transaction tx = graphDb.beginTx()) {
153  Node node1 = graphDb.createNode(Label.label("switch"));
154  node1.setProperty("name", "00:01");
155  node1.setProperty("address", "1");
156  node1.setProperty("hostname", "2");
157  node1.setProperty("description", "3");
158  node1.setProperty("controller", "4");
159  node1.setProperty("state", "active");
160 
161  Node node2 = graphDb.createNode(Label.label("switch"));
162  node2.setProperty("name", "00:02");
163  node2.setProperty("address", "5");
164  node2.setProperty("hostname", "6");
165  node2.setProperty("description", "7");
166  node2.setProperty("controller", "8");
167  node2.setProperty("state", "inactive");
168 
169  tx.success();
170  }
171 
172  List<SwitchInfoData> switches = target.getSwitches();
173  SwitchInfoData switch1 = switches.get(0);
174  Assert.assertEquals(new SwitchId("00:01"), switch1.getSwitchId());
175  Assert.assertEquals(SwitchState.ACTIVATED, switch1.getState());
176 
177  SwitchInfoData switch2 = switches.get(1);
178  Assert.assertEquals(new SwitchId("00:02"), switch2.getSwitchId());
179  Assert.assertNotEquals(SwitchState.ACTIVATED, switch2.getState());
180  }
181 
182 
183 }