blob: 3149103a018033dcc78b2c049ee96700fab4e860 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koidec406e792014-02-14 16:52:42 -08002
Toshio Koided9fa2a82014-02-19 17:35:18 -08003import static org.junit.Assert.assertEquals;
Jonathan Hart472062d2014-04-03 10:56:48 -07004import net.onrc.onos.core.topology.LinkEvent;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07005import net.onrc.onos.core.util.Dpid;
6import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -07007import net.onrc.onos.core.util.SwitchPort;
Jonathan Hart23701d12014-04-03 10:45:48 -07008import net.onrc.onos.core.util.serializers.KryoFactory;
Toshio Koidec406e792014-02-14 16:52:42 -08009
10import org.junit.After;
11import org.junit.Before;
12import org.junit.Test;
13
14import com.esotericsoftware.kryo.Kryo;
15import com.esotericsoftware.kryo.io.Input;
16import com.esotericsoftware.kryo.io.Output;
17
Toshio Koide066506e2014-02-20 19:52:09 -080018/**
Yuta HIGUCHId4acc802014-06-19 22:30:31 -070019 * Unit tests for PathIntent.
Toshio Koide066506e2014-02-20 19:52:09 -080020 */
Toshio Koidec406e792014-02-14 16:52:42 -080021public class PathIntentTest {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070022
23 private static final Dpid DPID_1 = new Dpid(1L);
24 private static final Dpid DPID_2 = new Dpid(2L);
25 private static final Dpid DPID_3 = new Dpid(3L);
26 private static final Dpid DPID_4 = new Dpid(4L);
27
28 private static final PortNumber PORT_NUMBER_1 = new PortNumber((short) 1);
29 private static final PortNumber PORT_NUMBER_2 = new PortNumber((short) 2);
30
Ray Milkey269ffb92014-04-03 14:43:30 -070031 @Before
32 public void setUp() throws Exception {
33 }
Toshio Koidec406e792014-02-14 16:52:42 -080034
Ray Milkey269ffb92014-04-03 14:43:30 -070035 @After
36 public void tearDown() throws Exception {
37 }
Toshio Koidec406e792014-02-14 16:52:42 -080038
Ray Milkey269ffb92014-04-03 14:43:30 -070039 @Test
40 public void testCreateFirstId() {
41 String id = PathIntent.createFirstId("100");
42 assertEquals("100___0", id);
43 }
Toshio Koidea10c0372014-02-20 17:28:10 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 @Test
46 public void testCreateNextId() {
47 String id = PathIntent.createNextId("100___999");
48 assertEquals("100___1000", id);
49 }
Toshio Koidea10c0372014-02-20 17:28:10 -080050
Ray Milkey269ffb92014-04-03 14:43:30 -070051 @Test
52 public void test() {
53 KryoFactory factory = new KryoFactory();
54 Kryo kryo = factory.newKryo();
55 Output output = new Output(1024);
Toshio Koidec406e792014-02-14 16:52:42 -080056
Ray Milkey269ffb92014-04-03 14:43:30 -070057 ConstrainedShortestPathIntent cspIntent1 =
58 new ConstrainedShortestPathIntent("1", 2L, 3L, 4L, 5L, 6L, 7L, 1000.0);
Toshio Koidec406e792014-02-14 16:52:42 -080059
Ray Milkey269ffb92014-04-03 14:43:30 -070060 Path path = new Path();
Yuta HIGUCHIe2a4e172014-07-03 10:50:39 -070061 path.add(new LinkEvent(new SwitchPort(1L, 1L), new SwitchPort(2L, 2L)));
62 path.add(new LinkEvent(new SwitchPort(2L, 1L), new SwitchPort(3L, 2L)));
63 path.add(new LinkEvent(new SwitchPort(3L, 1L), new SwitchPort(4L, 2L)));
Toshio Koidec406e792014-02-14 16:52:42 -080064
Ray Milkey269ffb92014-04-03 14:43:30 -070065 PathIntent pathIntent1 = new PathIntent("11", path, 123.45, cspIntent1);
Toshio Koidec406e792014-02-14 16:52:42 -080066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 kryo.writeObject(output, pathIntent1);
68 output.close();
Toshio Koidec406e792014-02-14 16:52:42 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 Input input = new Input(output.toBytes());
Toshio Koidec406e792014-02-14 16:52:42 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 // create pathIntent from bytes
Toshio Koidec406e792014-02-14 16:52:42 -080073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 PathIntent pathIntent2 =
75 kryo.readObject(input, PathIntent.class);
76 input.close();
Toshio Koidec406e792014-02-14 16:52:42 -080077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 // check
Toshio Koided9fa2a82014-02-19 17:35:18 -080079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 assertEquals("11", pathIntent2.getId());
81 Path path2 = pathIntent2.getPath();
Toshio Koidec406e792014-02-14 16:52:42 -080082
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070083 assertEquals(DPID_1, path2.get(0).getSrc().getDpid());
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070084 assertEquals(PORT_NUMBER_1, path2.get(0).getSrc().getPortNumber());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070085 assertEquals(DPID_2, path2.get(0).getDst().getDpid());
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070086 assertEquals(PORT_NUMBER_2, path2.get(0).getDst().getPortNumber());
Toshio Koidec406e792014-02-14 16:52:42 -080087
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070088 assertEquals(DPID_2, path2.get(1).getSrc().getDpid());
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070089 assertEquals(PORT_NUMBER_1, path2.get(1).getSrc().getPortNumber());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070090 assertEquals(DPID_3, path2.get(1).getDst().getDpid());
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070091 assertEquals(PORT_NUMBER_2, path2.get(1).getDst().getPortNumber());
Toshio Koidec406e792014-02-14 16:52:42 -080092
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070093 assertEquals(DPID_3, path2.get(2).getSrc().getDpid());
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070094 assertEquals(PORT_NUMBER_1, path2.get(2).getSrc().getPortNumber());
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070095 assertEquals(DPID_4, path2.get(2).getDst().getDpid());
Yuta HIGUCHIb1e2ab72014-06-30 11:01:31 -070096 assertEquals(PORT_NUMBER_2, path2.get(2).getDst().getPortNumber());
Toshio Koidec406e792014-02-14 16:52:42 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 assertEquals(123.45, pathIntent2.getBandwidth(), 0.0);
Toshio Koidec406e792014-02-14 16:52:42 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 ConstrainedShortestPathIntent cspIntent2 =
101 (ConstrainedShortestPathIntent) pathIntent2.getParentIntent();
Toshio Koided9fa2a82014-02-19 17:35:18 -0800102
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 assertEquals("1", cspIntent2.getId());
104 assertEquals(2L, cspIntent2.getSrcSwitchDpid());
105 assertEquals(3L, cspIntent2.getSrcPortNumber());
106 assertEquals(4L, cspIntent2.getSrcMac());
107 assertEquals(5L, cspIntent2.getDstSwitchDpid());
108 assertEquals(6L, cspIntent2.getDstPortNumber());
109 assertEquals(7L, cspIntent2.getDstMac());
110 assertEquals(1000.0, cspIntent2.getBandwidth(), 0.0);
111 }
Toshio Koidec406e792014-02-14 16:52:42 -0800112}