Pavlin Radoslavov | 8e7ba4b | 2013-10-16 04:04:27 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.topology; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 4 | import static org.junit.Assert.assertTrue; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 5 | |
| 6 | import org.easymock.EasyMock; |
| 7 | |
| 8 | import org.junit.After; |
| 9 | import org.junit.Before; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 10 | import org.junit.Test; |
| 11 | import org.junit.runner.RunWith; |
| 12 | |
| 13 | import org.powermock.api.easymock.PowerMock; |
| 14 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 15 | import org.powermock.modules.junit4.PowerMockRunner; |
| 16 | |
| 17 | import com.thinkaurelius.titan.core.TitanGraph; |
| 18 | import com.thinkaurelius.titan.core.TitanFactory; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 19 | import net.onrc.onos.graph.DBConnection; |
| 20 | import net.onrc.onos.graph.DBOperation; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 21 | import net.onrc.onos.graph.GraphDBConnection; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 22 | import net.onrc.onos.graph.GraphDBManager; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 23 | import net.onrc.onos.graph.GraphDBOperation; |
| 24 | import net.onrc.onos.ofcontroller.core.internal.TestDatabaseManager; |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 25 | import net.onrc.onos.ofcontroller.topology.TopologyManager; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 26 | import net.onrc.onos.ofcontroller.util.DataPath; |
| 27 | import net.onrc.onos.ofcontroller.util.Dpid; |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 28 | import net.onrc.onos.ofcontroller.util.FlowPathFlags; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 29 | import net.onrc.onos.ofcontroller.util.Port; |
| 30 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
| 31 | |
| 32 | /** |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 33 | * A class for testing the TopologyManager class. |
| 34 | * @see net.onrc.onos.ofcontroller.topology.TopologyManager |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 35 | */ |
| 36 | @RunWith(PowerMockRunner.class) |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 37 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, TopologyManager.class}) |
Pavlin Radoslavov | 8e7ba4b | 2013-10-16 04:04:27 -0700 | [diff] [blame] | 38 | public class TopologyManagerTest { |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 39 | String conf; |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 40 | String dbStore; |
| 41 | private DBConnection conn = null; |
| 42 | private DBOperation oper = null; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 43 | private TitanGraph titanGraph = null; |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 44 | private TopologyManager topologyManager = null; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Setup the tests. |
| 48 | */ |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 51 | dbStore = "dummyStore"; |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 52 | conf = "/dummy/path/to/db"; |
| 53 | |
| 54 | // |
| 55 | // Make mock database. |
| 56 | // Replace TitanFactory.open() to return the mock database. |
| 57 | // |
| 58 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 59 | PowerMock.mockStatic(TitanFactory.class); |
| 60 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 61 | PowerMock.replay(TitanFactory.class); |
| 62 | |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 63 | oper = GraphDBManager.getDBOperation(dbStore, conf); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 64 | // Create the connection to the database |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 65 | conn = GraphDBManager.getConnection(dbStore, conf); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 66 | |
| 67 | // Populate the database |
| 68 | TestDatabaseManager.populateTestData(titanGraph); |
| 69 | |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 70 | // Prepare the TopologyManager instance |
Pavlin Radoslavov | 0367d35 | 2013-10-19 11:04:43 -0700 | [diff] [blame] | 71 | topologyManager = new TopologyManager(oper); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Cleanup after the tests. |
| 76 | */ |
| 77 | @After |
| 78 | public void tearDown() throws Exception { |
| 79 | titanGraph.shutdown(); |
| 80 | TestDatabaseManager.deleteTestDatabase(); |
| 81 | } |
| 82 | |
| 83 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 84 | * Test method TopologyManager.getTopologyShortestPath() |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 85 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 86 | * @see net.onrc.onos.ofcontroller.topology.TopologyManager#getTopologyShortestPath |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 87 | */ |
| 88 | @Test |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 89 | public void test_getTopologyShortestPath() { |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 90 | DataPath dataPath = null; |
| 91 | String srcDpidStr = "00:00:00:00:00:00:0a:01"; |
| 92 | String dstDpidStr = "00:00:00:00:00:00:0a:06"; |
| 93 | short srcPortShort = 1; |
| 94 | short dstPortShort = 1; |
| 95 | |
| 96 | // |
| 97 | // Initialize the source and destination points |
| 98 | // |
| 99 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 100 | Port srcPort = new Port(srcPortShort); |
| 101 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 102 | Port dstPort = new Port(dstPortShort); |
| 103 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 104 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 105 | |
| 106 | // |
| 107 | // Test a valid Shortest-Path computation |
| 108 | // |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 109 | Topology topology = topologyManager.newDatabaseTopology(); |
| 110 | dataPath = topologyManager.getTopologyShortestPath(topology, |
| 111 | srcSwitchPort, |
| 112 | dstSwitchPort); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 113 | assertTrue(dataPath != null); |
| 114 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
| 115 | // System.out.println(dataPathSummaryStr); |
| 116 | String expectedResult = "1/00:00:00:00:00:00:0a:01/2;1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;"; |
| 117 | assertEquals(dataPathSummaryStr, expectedResult); |
| 118 | |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 119 | // Test if we apply various Flow Path Flags |
| 120 | String expectedResult2 = "1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;"; |
| 121 | String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;"; |
| 122 | FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY"); |
| 123 | FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY"); |
| 124 | // |
| 125 | dataPath.applyFlowPathFlags(flowPathFlags2); |
| 126 | dataPathSummaryStr = dataPath.dataPathSummary(); |
| 127 | assertEquals(dataPathSummaryStr, expectedResult2); |
| 128 | // |
| 129 | dataPath.applyFlowPathFlags(flowPathFlags3); |
| 130 | dataPathSummaryStr = dataPath.dataPathSummary(); |
| 131 | assertEquals(dataPathSummaryStr, expectedResult3); |
| 132 | |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 133 | // |
| 134 | // Test Shortest-Path computation to non-existing destination |
| 135 | // |
| 136 | String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06"; |
| 137 | Dpid noSuchDstDpid = new Dpid(noSuchDpidStr); |
| 138 | SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort); |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 139 | dataPath = topologyManager.getTopologyShortestPath(topology, |
| 140 | srcSwitchPort, |
| 141 | noSuchDstSwitchPort); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 142 | assertTrue(dataPath == null); |
| 143 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 144 | topologyManager.dropTopology(topology); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 148 | * Test method TopologyManager.getDatabaseShortestPath() |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 149 | * |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 150 | * @see net.onrc.onos.ofcontroller.routing.TopologyManager#getDatabaseShortestPath |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 151 | */ |
| 152 | @Test |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 153 | public void test_getDatabaseShortestPath() { |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 154 | DataPath dataPath = null; |
| 155 | String srcDpidStr = "00:00:00:00:00:00:0a:01"; |
| 156 | String dstDpidStr = "00:00:00:00:00:00:0a:06"; |
| 157 | short srcPortShort = 1; |
| 158 | short dstPortShort = 1; |
| 159 | |
| 160 | // |
| 161 | // Initialize the source and destination points |
| 162 | // |
| 163 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 164 | Port srcPort = new Port(srcPortShort); |
| 165 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 166 | Port dstPort = new Port(dstPortShort); |
| 167 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 168 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 169 | |
| 170 | // |
| 171 | // Test a valid Shortest-Path computation |
| 172 | // |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 173 | dataPath = topologyManager.getDatabaseShortestPath(srcSwitchPort, |
| 174 | dstSwitchPort); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 175 | assertTrue(dataPath != null); |
| 176 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
| 177 | // System.out.println(dataPathSummaryStr); |
| 178 | String expectedResult = "1/00:00:00:00:00:00:0a:01/2;1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;"; |
| 179 | assertEquals(dataPathSummaryStr, expectedResult); |
| 180 | |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 181 | // Test if we apply various Flow Path Flags |
| 182 | String expectedResult2 = "1/00:00:00:00:00:00:0a:03/2;2/00:00:00:00:00:00:0a:04/3;1/00:00:00:00:00:00:0a:06/1;"; |
| 183 | String expectedResult3 = "1/00:00:00:00:00:00:0a:03/2;"; |
| 184 | FlowPathFlags flowPathFlags2 = new FlowPathFlags("DISCARD_FIRST_HOP_ENTRY"); |
| 185 | FlowPathFlags flowPathFlags3 = new FlowPathFlags("KEEP_ONLY_FIRST_HOP_ENTRY"); |
| 186 | // |
| 187 | dataPath.applyFlowPathFlags(flowPathFlags2); |
| 188 | dataPathSummaryStr = dataPath.dataPathSummary(); |
| 189 | assertEquals(dataPathSummaryStr, expectedResult2); |
| 190 | // |
| 191 | dataPath.applyFlowPathFlags(flowPathFlags3); |
| 192 | dataPathSummaryStr = dataPath.dataPathSummary(); |
| 193 | assertEquals(dataPathSummaryStr, expectedResult3); |
| 194 | |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 195 | // |
| 196 | // Test Shortest-Path computation to non-existing destination |
| 197 | // |
| 198 | String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06"; |
| 199 | Dpid noSuchDstDpid = new Dpid(noSuchDpidStr); |
| 200 | SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort); |
| 201 | |
Pavlin Radoslavov | 15954d4 | 2013-10-19 15:29:04 -0700 | [diff] [blame] | 202 | dataPath = topologyManager.getDatabaseShortestPath(srcSwitchPort, |
| 203 | noSuchDstSwitchPort); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 204 | assertTrue(dataPath == null); |
| 205 | } |
| 206 | |
| 207 | /** |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 208 | * Test method TopologyManager.routeExists() |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 209 | * |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 210 | * @see net.onrc.onos.ofcontroller.routing.TopologyManager#routeExists |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 211 | */ |
| 212 | @Test |
| 213 | public void test_routeExists() { |
| 214 | Boolean result; |
| 215 | String srcDpidStr = "00:00:00:00:00:00:0a:01"; |
| 216 | String dstDpidStr = "00:00:00:00:00:00:0a:06"; |
| 217 | short srcPortShort = 1; |
| 218 | short dstPortShort = 1; |
| 219 | |
| 220 | // |
| 221 | // Initialize the source and destination points |
| 222 | // |
| 223 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 224 | Port srcPort = new Port(srcPortShort); |
| 225 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 226 | Port dstPort = new Port(dstPortShort); |
| 227 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 228 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 229 | |
| 230 | // |
| 231 | // Test a valid route |
| 232 | // |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 233 | result = topologyManager.routeExists(srcSwitchPort, dstSwitchPort); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 234 | assertTrue(result == true); |
| 235 | |
| 236 | // |
| 237 | // Test a non-existing route |
| 238 | // |
| 239 | String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06"; |
| 240 | Dpid noSuchDstDpid = new Dpid(noSuchDpidStr); |
| 241 | SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort); |
Pavlin Radoslavov | e1b37bc | 2013-10-16 03:57:06 -0700 | [diff] [blame] | 242 | result = topologyManager.routeExists(srcSwitchPort, |
| 243 | noSuchDstSwitchPort); |
Pavlin Radoslavov | e3e03dc | 2013-06-27 15:49:05 -0700 | [diff] [blame] | 244 | assertTrue(result != true); |
| 245 | } |
| 246 | } |