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; |
| 44 | import net.onrc.onos.ofcontroller.util.Port; |
| 45 | import net.onrc.onos.ofcontroller.util.SwitchPort; |
| 46 | |
| 47 | /** |
| 48 | * A class for testing the TopoRouteService class. |
| 49 | * @see net.onrc.onos.ofcontroller.routing.TopoRouteService |
| 50 | * @author Pavlin Radoslavov (pavlin@onlab.us) |
| 51 | */ |
| 52 | @RunWith(PowerMockRunner.class) |
| 53 | @PrepareForTest({TitanFactory.class, GraphDBConnection.class, GraphDBOperation.class, TopoRouteService.class}) |
| 54 | public class TopoRouteServiceTest { |
| 55 | String conf; |
| 56 | private GraphDBConnection conn = null; |
| 57 | private GraphDBOperation oper = null; |
| 58 | private TitanGraph titanGraph = null; |
| 59 | private TopoRouteService topoRouteService = null; |
| 60 | |
| 61 | /** |
| 62 | * Setup the tests. |
| 63 | */ |
| 64 | @Before |
| 65 | public void setUp() throws Exception { |
| 66 | conf = "/dummy/path/to/db"; |
| 67 | |
| 68 | // |
| 69 | // Make mock database. |
| 70 | // Replace TitanFactory.open() to return the mock database. |
| 71 | // |
| 72 | titanGraph = TestDatabaseManager.getTestDatabase(); |
| 73 | PowerMock.mockStatic(TitanFactory.class); |
| 74 | EasyMock.expect(TitanFactory.open((String)EasyMock.anyObject())).andReturn(titanGraph); |
| 75 | PowerMock.replay(TitanFactory.class); |
| 76 | |
| 77 | // Create the connection to the database |
| 78 | conn = GraphDBConnection.getInstance(conf); |
| 79 | oper = new GraphDBOperation(conn); |
| 80 | |
| 81 | // Populate the database |
| 82 | TestDatabaseManager.populateTestData(titanGraph); |
| 83 | |
| 84 | // Prepare the TopoRouteService instance |
| 85 | topoRouteService = new TopoRouteService(); |
| 86 | topoRouteService.setDbOperationHandler(oper); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Cleanup after the tests. |
| 91 | */ |
| 92 | @After |
| 93 | public void tearDown() throws Exception { |
| 94 | titanGraph.shutdown(); |
| 95 | TestDatabaseManager.deleteTestDatabase(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Test method TopoRouteService.getTopoShortestPath() |
| 100 | * |
| 101 | * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#getTopoShortestPath |
| 102 | */ |
| 103 | @Test |
| 104 | public void test_getTopoShortestPath() { |
| 105 | DataPath dataPath = null; |
| 106 | String srcDpidStr = "00:00:00:00:00:00:0a:01"; |
| 107 | String dstDpidStr = "00:00:00:00:00:00:0a:06"; |
| 108 | short srcPortShort = 1; |
| 109 | short dstPortShort = 1; |
| 110 | |
| 111 | // |
| 112 | // Initialize the source and destination points |
| 113 | // |
| 114 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 115 | Port srcPort = new Port(srcPortShort); |
| 116 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 117 | Port dstPort = new Port(dstPortShort); |
| 118 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 119 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 120 | |
| 121 | // |
| 122 | // Test a valid Shortest-Path computation |
| 123 | // |
| 124 | Map<Long, ?> shortestPathTopo = |
| 125 | topoRouteService.prepareShortestPathTopo(); |
| 126 | dataPath = topoRouteService.getTopoShortestPath(shortestPathTopo, |
| 127 | srcSwitchPort, |
| 128 | dstSwitchPort); |
| 129 | assertTrue(dataPath != null); |
| 130 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
| 131 | // System.out.println(dataPathSummaryStr); |
| 132 | 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;"; |
| 133 | assertEquals(dataPathSummaryStr, expectedResult); |
| 134 | |
| 135 | // |
| 136 | // Test Shortest-Path computation to non-existing destination |
| 137 | // |
| 138 | String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06"; |
| 139 | Dpid noSuchDstDpid = new Dpid(noSuchDpidStr); |
| 140 | SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort); |
| 141 | dataPath = topoRouteService.getTopoShortestPath(shortestPathTopo, |
| 142 | srcSwitchPort, |
| 143 | noSuchDstSwitchPort); |
| 144 | assertTrue(dataPath == null); |
| 145 | |
| 146 | topoRouteService.dropShortestPathTopo(shortestPathTopo); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Test method TopoRouteService.getShortestPath() |
| 151 | * |
| 152 | * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#getShortestPath |
| 153 | */ |
| 154 | @Test |
| 155 | public void test_getShortestPath() { |
| 156 | DataPath dataPath = null; |
| 157 | String srcDpidStr = "00:00:00:00:00:00:0a:01"; |
| 158 | String dstDpidStr = "00:00:00:00:00:00:0a:06"; |
| 159 | short srcPortShort = 1; |
| 160 | short dstPortShort = 1; |
| 161 | |
| 162 | // |
| 163 | // Initialize the source and destination points |
| 164 | // |
| 165 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 166 | Port srcPort = new Port(srcPortShort); |
| 167 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 168 | Port dstPort = new Port(dstPortShort); |
| 169 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 170 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 171 | |
| 172 | // |
| 173 | // Test a valid Shortest-Path computation |
| 174 | // |
| 175 | dataPath = topoRouteService.getShortestPath(srcSwitchPort, |
| 176 | dstSwitchPort); |
| 177 | assertTrue(dataPath != null); |
| 178 | String dataPathSummaryStr = dataPath.dataPathSummary(); |
| 179 | // System.out.println(dataPathSummaryStr); |
| 180 | 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;"; |
| 181 | assertEquals(dataPathSummaryStr, expectedResult); |
| 182 | |
| 183 | // |
| 184 | // Test Shortest-Path computation to non-existing destination |
| 185 | // |
| 186 | String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06"; |
| 187 | Dpid noSuchDstDpid = new Dpid(noSuchDpidStr); |
| 188 | SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort); |
| 189 | |
| 190 | dataPath = topoRouteService.getShortestPath(srcSwitchPort, |
| 191 | noSuchDstSwitchPort); |
| 192 | assertTrue(dataPath == null); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Test method TopoRouteService.routeExists() |
| 197 | * |
| 198 | * @see net.onrc.onos.ofcontroller.routing.TopoRouteService#routeExists |
| 199 | */ |
| 200 | @Test |
| 201 | public void test_routeExists() { |
| 202 | Boolean result; |
| 203 | String srcDpidStr = "00:00:00:00:00:00:0a:01"; |
| 204 | String dstDpidStr = "00:00:00:00:00:00:0a:06"; |
| 205 | short srcPortShort = 1; |
| 206 | short dstPortShort = 1; |
| 207 | |
| 208 | // |
| 209 | // Initialize the source and destination points |
| 210 | // |
| 211 | Dpid srcDpid = new Dpid(srcDpidStr); |
| 212 | Port srcPort = new Port(srcPortShort); |
| 213 | Dpid dstDpid = new Dpid(dstDpidStr); |
| 214 | Port dstPort = new Port(dstPortShort); |
| 215 | SwitchPort srcSwitchPort = new SwitchPort(srcDpid, srcPort); |
| 216 | SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstPort); |
| 217 | |
| 218 | // |
| 219 | // Test a valid route |
| 220 | // |
| 221 | result = topoRouteService.routeExists(srcSwitchPort, dstSwitchPort); |
| 222 | assertTrue(result == true); |
| 223 | |
| 224 | // |
| 225 | // Test a non-existing route |
| 226 | // |
| 227 | String noSuchDpidStr = "ff:ff:00:00:00:00:0a:06"; |
| 228 | Dpid noSuchDstDpid = new Dpid(noSuchDpidStr); |
| 229 | SwitchPort noSuchDstSwitchPort = new SwitchPort(noSuchDstDpid, dstPort); |
| 230 | result = topoRouteService.routeExists(srcSwitchPort, |
| 231 | noSuchDstSwitchPort); |
| 232 | assertTrue(result != true); |
| 233 | } |
| 234 | } |