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