blob: 9a3aaa209d89b893bb716f2a6b087883b9a4b5d9 [file] [log] [blame]
Jonathan Hartaa380972014-04-03 10:24:46 -07001package net.onrc.onos.core.intent;
Toshio Koidefe1d5d92014-02-26 20:09:48 -08002
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
Jonathan Hartaa380972014-04-03 10:24:46 -07006import net.onrc.onos.core.intent.IntentOperation.Operator;
Jonathan Hart472062d2014-04-03 10:56:48 -07007import net.onrc.onos.core.topology.LinkEvent;
TeruU5d2c9392014-06-09 20:02:02 -07008import net.onrc.onos.core.topology.MockTopology;
Toshio Koidefe1d5d92014-02-26 20:09:48 -08009
10import org.junit.After;
11import org.junit.Before;
12import org.junit.Test;
13
14/**
Yuta HIGUCHId4acc802014-06-19 22:30:31 -070015 * Unit tests for ConstrainedBFSTree class.
Toshio Koidefe1d5d92014-02-26 20:09:48 -080016 */
17public class ConstrainedBFSTreeTest {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070018 static final long LOCAL_PORT = 0xFFFEL;
Toshio Koidefe1d5d92014-02-26 20:09:48 -080019
Ray Milkey269ffb92014-04-03 14:43:30 -070020 @Before
21 public void setUp() throws Exception {
22 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080023
Ray Milkey269ffb92014-04-03 14:43:30 -070024 @After
25 public void tearDown() throws Exception {
26 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080027
Ray Milkey269ffb92014-04-03 14:43:30 -070028 @Test
29 public void testCreate() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070030 MockTopology topology = new MockTopology();
31 topology.createSampleTopology1();
32 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L));
Ray Milkey269ffb92014-04-03 14:43:30 -070033 assertNotNull(tree);
34 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080035
Ray Milkey269ffb92014-04-03 14:43:30 -070036 @Test
37 public void testCreateConstrained() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070038 MockTopology topology = new MockTopology();
39 topology.createSampleTopology1();
Ray Milkey269ffb92014-04-03 14:43:30 -070040 PathIntentMap intents = new PathIntentMap();
Jonathan Harte37e4e22014-05-13 19:12:02 -070041 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 1000.0);
Ray Milkey269ffb92014-04-03 14:43:30 -070042 assertNotNull(tree);
43 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 @Test
46 public void testGetPath() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070047 MockTopology topology = new MockTopology();
48 topology.createSampleTopology1();
49 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L));
50 Path path11 = tree.getPath(topology.getSwitch(1L));
51 Path path12 = tree.getPath(topology.getSwitch(2L));
52 Path path13 = tree.getPath(topology.getSwitch(3L));
53 Path path14 = tree.getPath(topology.getSwitch(4L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 assertNotNull(path11);
56 assertEquals(0, path11.size());
Toshio Koidefe1d5d92014-02-26 20:09:48 -080057
Ray Milkey269ffb92014-04-03 14:43:30 -070058 assertNotNull(path12);
59 assertEquals(1, path12.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -070060 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 12L)), path12.get(0));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 assertNotNull(path13);
63 assertEquals(2, path13.size());
64 if (path13.get(0).getDst().getDpid() == 2L) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070065 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 12L)), path13.get(0));
66 assertEquals(new LinkEvent(topology.getOutgoingLink(2L, 23L)), path13.get(1));
Ray Milkey269ffb92014-04-03 14:43:30 -070067 } else {
Jonathan Harte37e4e22014-05-13 19:12:02 -070068 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 14L)), path13.get(0));
69 assertEquals(new LinkEvent(topology.getOutgoingLink(4L, 43L)), path13.get(1));
Ray Milkey269ffb92014-04-03 14:43:30 -070070 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 assertNotNull(path14);
73 assertEquals(1, path14.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -070074 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 14L)), path14.get(0));
Ray Milkey269ffb92014-04-03 14:43:30 -070075 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080076
Ray Milkey269ffb92014-04-03 14:43:30 -070077 @Test
78 public void testGetPathNull() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070079 MockTopology topology = new MockTopology();
80 topology.createSampleTopology1();
81 topology.removeLink(1L, 12L, 2L, 21L);
82 topology.removeLink(1L, 14L, 4L, 41L);
Ray Milkey269ffb92014-04-03 14:43:30 -070083 // now, there is no path from switch 1, but to switch1
Toshio Koidefe1d5d92014-02-26 20:09:48 -080084
Jonathan Harte37e4e22014-05-13 19:12:02 -070085 ConstrainedBFSTree tree1 = new ConstrainedBFSTree(topology.getSwitch(1L));
86 Path path12 = tree1.getPath(topology.getSwitch(2L));
87 Path path13 = tree1.getPath(topology.getSwitch(3L));
88 Path path14 = tree1.getPath(topology.getSwitch(4L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080089
Jonathan Harte37e4e22014-05-13 19:12:02 -070090 ConstrainedBFSTree tree2 = new ConstrainedBFSTree(topology.getSwitch(2L));
91 Path path21 = tree2.getPath(topology.getSwitch(1L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 assertNull(path12);
94 assertNull(path13);
95 assertNull(path14);
96 assertNotNull(path21);
97 assertEquals(1, path21.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -070098 assertEquals(new LinkEvent(topology.getOutgoingLink(2L, 21L)), path21.get(0));
Ray Milkey269ffb92014-04-03 14:43:30 -070099 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 @Test
102 public void testGetConstrainedPath() {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700103 MockTopology topology = new MockTopology();
104 topology.createSampleTopology1();
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 PathIntentMap intents = new PathIntentMap();
106 IntentOperationList intentOps = new IntentOperationList();
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800107
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 // create constrained shortest path intents that have the same source destination ports
109 ConstrainedShortestPathIntent intent1 = new ConstrainedShortestPathIntent(
110 "1", 1L, LOCAL_PORT, 0x111L, 2L, LOCAL_PORT, 0x222L, 600.0);
111 ConstrainedShortestPathIntent intent2 = new ConstrainedShortestPathIntent(
112 "2", 1L, LOCAL_PORT, 0x333L, 2L, LOCAL_PORT, 0x444L, 600.0);
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800113
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 // calculate path of the intent1
Jonathan Harte37e4e22014-05-13 19:12:02 -0700115 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 600.0);
116 Path path1 = tree.getPath(topology.getSwitch(2L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 assertNotNull(path1);
119 assertEquals(1, path1.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -0700120 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 12L)), path1.get(0));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800121
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 PathIntent pathIntent1 = new PathIntent("pi1", path1, 600.0, intent1);
123 intentOps.add(Operator.ADD, pathIntent1);
124 intents.executeOperations(intentOps);
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800125
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 // calculate path of the intent2
Jonathan Harte37e4e22014-05-13 19:12:02 -0700127 tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 600.0);
128 Path path2 = tree.getPath(topology.getSwitch(2L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800129
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 assertNotNull(path2);
131 assertEquals(2, path2.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -0700132 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 14L)), path2.get(0));
133 assertEquals(new LinkEvent(topology.getOutgoingLink(4L, 42L)), path2.get(1));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800134
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 PathIntent pathIntent2 = new PathIntent("pi2", path2, 600.0, intent2);
136 intentOps.add(Operator.ADD, pathIntent2);
137 intents.executeOperations(intentOps);
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800138
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 // calculate path of the intent3
Jonathan Harte37e4e22014-05-13 19:12:02 -0700140 tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 600.0);
141 Path path3 = tree.getPath(topology.getSwitch(2L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800142
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 assertNull(path3);
144 }
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700145}