Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 1 | package net.onrc.onos.intent; |
| 2 | |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import net.onrc.onos.ofcontroller.networkgraph.LinkEvent; |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 5 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 6 | import net.onrc.onos.ofcontroller.networkgraph.Path; |
Toshio Koide | bc116be | 2014-02-19 23:56:48 -0800 | [diff] [blame] | 7 | import net.onrc.onos.ofcontroller.util.serializers.KryoFactory; |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 8 | |
| 9 | import org.junit.After; |
| 10 | import org.junit.Before; |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | import com.esotericsoftware.kryo.Kryo; |
| 14 | import com.esotericsoftware.kryo.io.Input; |
| 15 | import com.esotericsoftware.kryo.io.Output; |
| 16 | |
Toshio Koide | 066506e | 2014-02-20 19:52:09 -0800 | [diff] [blame] | 17 | /** |
| 18 | * @author Toshio Koide (t-koide@onlab.us) |
| 19 | */ |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 20 | public class PathIntentTest { |
| 21 | NetworkGraph g; |
| 22 | |
| 23 | @Before |
| 24 | public void setUp() throws Exception { |
| 25 | MockNetworkGraph graph = new MockNetworkGraph(); |
| 26 | graph.createSampleTopology(); |
| 27 | g = graph; |
| 28 | } |
| 29 | |
| 30 | @After |
| 31 | public void tearDown() throws Exception { |
| 32 | } |
| 33 | |
| 34 | @Test |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 35 | public void testCreateFirstId() { |
| 36 | String id = PathIntent.createFirstId("100"); |
Toshio Koide | 52a6bca | 2014-02-21 21:28:22 -0800 | [diff] [blame] | 37 | assertEquals("100___0", id); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testCreateNextId() { |
Toshio Koide | 52a6bca | 2014-02-21 21:28:22 -0800 | [diff] [blame] | 42 | String id = PathIntent.createNextId("100___999"); |
| 43 | assertEquals("100___1000", id); |
Toshio Koide | a10c037 | 2014-02-20 17:28:10 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Test |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 47 | public void test() { |
Toshio Koide | bc116be | 2014-02-19 23:56:48 -0800 | [diff] [blame] | 48 | KryoFactory factory = new KryoFactory(); |
| 49 | Kryo kryo = factory.newKryo(); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 50 | Output output = new Output(1024); |
| 51 | |
| 52 | ConstrainedShortestPathIntent cspIntent1 = |
| 53 | new ConstrainedShortestPathIntent("1", 2L, 3L, 4L, 5L, 6L, 7L, 1000.0); |
| 54 | |
| 55 | Path path = new Path(); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 56 | path.add(new LinkEvent(g.getSwitch(1L).getPort(1L).getOutgoingLink())); |
| 57 | path.add(new LinkEvent(g.getSwitch(2L).getPort(1L).getOutgoingLink())); |
| 58 | path.add(new LinkEvent(g.getSwitch(3L).getPort(1L).getOutgoingLink())); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 59 | |
| 60 | PathIntent pathIntent1 = new PathIntent("11", path, 123.45, cspIntent1); |
| 61 | |
| 62 | kryo.writeObject(output, pathIntent1); |
| 63 | output.close(); |
| 64 | |
| 65 | Input input = new Input(output.toBytes()); |
| 66 | |
| 67 | // create pathIntent from bytes |
| 68 | |
| 69 | PathIntent pathIntent2 = |
| 70 | kryo.readObject(input, PathIntent.class); |
| 71 | input.close(); |
| 72 | |
| 73 | // check |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 74 | |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 75 | assertEquals("11", pathIntent2.getId()); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 76 | Path path2 = pathIntent2.getPath(); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 77 | |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 78 | assertEquals(Long.valueOf(1L), path2.get(0).getSrc().getDpid()); |
| 79 | assertEquals(Long.valueOf(1L), path2.get(0).getSrc().getNumber()); |
| 80 | assertEquals(Long.valueOf(2L), path2.get(0).getDst().getDpid()); |
| 81 | assertEquals(Long.valueOf(2L), path2.get(0).getDst().getNumber()); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 82 | |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 83 | assertEquals(Long.valueOf(2L), path2.get(1).getSrc().getDpid()); |
| 84 | assertEquals(Long.valueOf(1L), path2.get(1).getSrc().getNumber()); |
| 85 | assertEquals(Long.valueOf(3L), path2.get(1).getDst().getDpid()); |
| 86 | assertEquals(Long.valueOf(2L), path2.get(1).getDst().getNumber()); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 87 | |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 88 | assertEquals(Long.valueOf(3L), path2.get(2).getSrc().getDpid()); |
| 89 | assertEquals(Long.valueOf(1L), path2.get(2).getSrc().getNumber()); |
| 90 | assertEquals(Long.valueOf(4L), path2.get(2).getDst().getDpid()); |
| 91 | assertEquals(Long.valueOf(2L), path2.get(2).getDst().getNumber()); |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 92 | |
| 93 | assertEquals(123.45, pathIntent2.getBandwidth(), 0.0); |
| 94 | |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 95 | ConstrainedShortestPathIntent cspIntent2 = |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 96 | (ConstrainedShortestPathIntent) pathIntent2.getParentIntent(); |
Toshio Koide | d9fa2a8 | 2014-02-19 17:35:18 -0800 | [diff] [blame] | 97 | |
Toshio Koide | c406e79 | 2014-02-14 16:52:42 -0800 | [diff] [blame] | 98 | assertEquals("1", cspIntent2.getId()); |
| 99 | assertEquals(2L, cspIntent2.getSrcSwitchDpid()); |
| 100 | assertEquals(3L, cspIntent2.getSrcPortNumber()); |
| 101 | assertEquals(4L, cspIntent2.getSrcMac()); |
| 102 | assertEquals(5L, cspIntent2.getDstSwitchDpid()); |
| 103 | assertEquals(6L, cspIntent2.getDstPortNumber()); |
| 104 | assertEquals(7L, cspIntent2.getDstMac()); |
| 105 | assertEquals(1000.0, cspIntent2.getBandwidth(), 0.0); |
| 106 | } |
| 107 | } |