blob: 1b9e48277209cacb56b1feffdbc62d4ef5504951 [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;
Jonathan Hart23701d12014-04-03 10:45:48 -07007import net.onrc.onos.core.util.serializers.KryoFactory;
Toshio Koidec406e792014-02-14 16:52:42 -08008
9import org.junit.After;
10import org.junit.Before;
11import org.junit.Test;
12
13import com.esotericsoftware.kryo.Kryo;
14import com.esotericsoftware.kryo.io.Input;
15import com.esotericsoftware.kryo.io.Output;
16
Toshio Koide066506e2014-02-20 19:52:09 -080017/**
Yuta HIGUCHId4acc802014-06-19 22:30:31 -070018 * Unit tests for PathIntent.
Toshio Koide066506e2014-02-20 19:52:09 -080019 */
Toshio Koidec406e792014-02-14 16:52:42 -080020public class PathIntentTest {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070021
22 private static final Dpid DPID_1 = new Dpid(1L);
23 private static final Dpid DPID_2 = new Dpid(2L);
24 private static final Dpid DPID_3 = new Dpid(3L);
25 private static final Dpid DPID_4 = new Dpid(4L);
26
27 private static final PortNumber PORT_NUMBER_1 = new PortNumber((short) 1);
28 private static final PortNumber PORT_NUMBER_2 = new PortNumber((short) 2);
29
Ray Milkey269ffb92014-04-03 14:43:30 -070030 @Before
31 public void setUp() throws Exception {
32 }
Toshio Koidec406e792014-02-14 16:52:42 -080033
Ray Milkey269ffb92014-04-03 14:43:30 -070034 @After
35 public void tearDown() throws Exception {
36 }
Toshio Koidec406e792014-02-14 16:52:42 -080037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 @Test
39 public void testCreateFirstId() {
40 String id = PathIntent.createFirstId("100");
41 assertEquals("100___0", id);
42 }
Toshio Koidea10c0372014-02-20 17:28:10 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 @Test
45 public void testCreateNextId() {
46 String id = PathIntent.createNextId("100___999");
47 assertEquals("100___1000", id);
48 }
Toshio Koidea10c0372014-02-20 17:28:10 -080049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 @Test
51 public void test() {
52 KryoFactory factory = new KryoFactory();
53 Kryo kryo = factory.newKryo();
54 Output output = new Output(1024);
Toshio Koidec406e792014-02-14 16:52:42 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 ConstrainedShortestPathIntent cspIntent1 =
57 new ConstrainedShortestPathIntent("1", 2L, 3L, 4L, 5L, 6L, 7L, 1000.0);
Toshio Koidec406e792014-02-14 16:52:42 -080058
Ray Milkey269ffb92014-04-03 14:43:30 -070059 Path path = new Path();
60 path.add(new LinkEvent(1L, 1L, 2L, 2L));
61 path.add(new LinkEvent(2L, 1L, 3L, 2L));
62 path.add(new LinkEvent(3L, 1L, 4L, 2L));
Toshio Koidec406e792014-02-14 16:52:42 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 PathIntent pathIntent1 = new PathIntent("11", path, 123.45, cspIntent1);
Toshio Koidec406e792014-02-14 16:52:42 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 kryo.writeObject(output, pathIntent1);
67 output.close();
Toshio Koidec406e792014-02-14 16:52:42 -080068
Ray Milkey269ffb92014-04-03 14:43:30 -070069 Input input = new Input(output.toBytes());
Toshio Koidec406e792014-02-14 16:52:42 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 // create pathIntent from bytes
Toshio Koidec406e792014-02-14 16:52:42 -080072
Ray Milkey269ffb92014-04-03 14:43:30 -070073 PathIntent pathIntent2 =
74 kryo.readObject(input, PathIntent.class);
75 input.close();
Toshio Koidec406e792014-02-14 16:52:42 -080076
Ray Milkey269ffb92014-04-03 14:43:30 -070077 // check
Toshio Koided9fa2a82014-02-19 17:35:18 -080078
Ray Milkey269ffb92014-04-03 14:43:30 -070079 assertEquals("11", pathIntent2.getId());
80 Path path2 = pathIntent2.getPath();
Toshio Koidec406e792014-02-14 16:52:42 -080081
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070082 assertEquals(DPID_1, path2.get(0).getSrc().getDpid());
83 assertEquals(PORT_NUMBER_1, path2.get(0).getSrc().getNumber());
84 assertEquals(DPID_2, path2.get(0).getDst().getDpid());
85 assertEquals(PORT_NUMBER_2, path2.get(0).getDst().getNumber());
Toshio Koidec406e792014-02-14 16:52:42 -080086
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070087 assertEquals(DPID_2, path2.get(1).getSrc().getDpid());
88 assertEquals(PORT_NUMBER_1, path2.get(1).getSrc().getNumber());
89 assertEquals(DPID_3, path2.get(1).getDst().getDpid());
90 assertEquals(PORT_NUMBER_2, path2.get(1).getDst().getNumber());
Toshio Koidec406e792014-02-14 16:52:42 -080091
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070092 assertEquals(DPID_3, path2.get(2).getSrc().getDpid());
93 assertEquals(PORT_NUMBER_1, path2.get(2).getSrc().getNumber());
94 assertEquals(DPID_4, path2.get(2).getDst().getDpid());
95 assertEquals(PORT_NUMBER_2, path2.get(2).getDst().getNumber());
Toshio Koidec406e792014-02-14 16:52:42 -080096
Ray Milkey269ffb92014-04-03 14:43:30 -070097 assertEquals(123.45, pathIntent2.getBandwidth(), 0.0);
Toshio Koidec406e792014-02-14 16:52:42 -080098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 ConstrainedShortestPathIntent cspIntent2 =
100 (ConstrainedShortestPathIntent) pathIntent2.getParentIntent();
Toshio Koided9fa2a82014-02-19 17:35:18 -0800101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 assertEquals("1", cspIntent2.getId());
103 assertEquals(2L, cspIntent2.getSrcSwitchDpid());
104 assertEquals(3L, cspIntent2.getSrcPortNumber());
105 assertEquals(4L, cspIntent2.getSrcMac());
106 assertEquals(5L, cspIntent2.getDstSwitchDpid());
107 assertEquals(6L, cspIntent2.getDstPortNumber());
108 assertEquals(7L, cspIntent2.getDstMac());
109 assertEquals(1000.0, cspIntent2.getBandwidth(), 0.0);
110 }
Toshio Koidec406e792014-02-14 16:52:42 -0800111}