blob: 8d5a6d6bd71d64d1391d3244c5312c25cd34f537 [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;
Toshio Koidefe1d5d92014-02-26 20:09:48 -08008
9import org.junit.After;
10import org.junit.Before;
11import org.junit.Test;
12
13/**
14 * @author Toshio Koide (t-koide@onlab.us)
15 */
16public class ConstrainedBFSTreeTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070017 static long LOCAL_PORT = 0xFFFEL;
Toshio Koidefe1d5d92014-02-26 20:09:48 -080018
Ray Milkey269ffb92014-04-03 14:43:30 -070019 @Before
20 public void setUp() throws Exception {
21 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080022
Ray Milkey269ffb92014-04-03 14:43:30 -070023 @After
24 public void tearDown() throws Exception {
25 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080026
Ray Milkey269ffb92014-04-03 14:43:30 -070027 @Test
28 public void testCreate() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070029 MockTopology topology = new MockTopology();
30 topology.createSampleTopology1();
31 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L));
Ray Milkey269ffb92014-04-03 14:43:30 -070032 assertNotNull(tree);
33 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080034
Ray Milkey269ffb92014-04-03 14:43:30 -070035 @Test
36 public void testCreateConstrained() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070037 MockTopology topology = new MockTopology();
38 topology.createSampleTopology1();
Ray Milkey269ffb92014-04-03 14:43:30 -070039 PathIntentMap intents = new PathIntentMap();
Jonathan Harte37e4e22014-05-13 19:12:02 -070040 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 1000.0);
Ray Milkey269ffb92014-04-03 14:43:30 -070041 assertNotNull(tree);
42 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 @Test
45 public void testGetPath() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070046 MockTopology topology = new MockTopology();
47 topology.createSampleTopology1();
48 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L));
49 Path path11 = tree.getPath(topology.getSwitch(1L));
50 Path path12 = tree.getPath(topology.getSwitch(2L));
51 Path path13 = tree.getPath(topology.getSwitch(3L));
52 Path path14 = tree.getPath(topology.getSwitch(4L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 assertNotNull(path11);
55 assertEquals(0, path11.size());
Toshio Koidefe1d5d92014-02-26 20:09:48 -080056
Ray Milkey269ffb92014-04-03 14:43:30 -070057 assertNotNull(path12);
58 assertEquals(1, path12.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -070059 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 12L)), path12.get(0));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 assertNotNull(path13);
62 assertEquals(2, path13.size());
63 if (path13.get(0).getDst().getDpid() == 2L) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070064 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 12L)), path13.get(0));
65 assertEquals(new LinkEvent(topology.getOutgoingLink(2L, 23L)), path13.get(1));
Ray Milkey269ffb92014-04-03 14:43:30 -070066 } else {
Jonathan Harte37e4e22014-05-13 19:12:02 -070067 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 14L)), path13.get(0));
68 assertEquals(new LinkEvent(topology.getOutgoingLink(4L, 43L)), path13.get(1));
Ray Milkey269ffb92014-04-03 14:43:30 -070069 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 assertNotNull(path14);
72 assertEquals(1, path14.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -070073 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 14L)), path14.get(0));
Ray Milkey269ffb92014-04-03 14:43:30 -070074 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 @Test
77 public void testGetPathNull() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070078 MockTopology topology = new MockTopology();
79 topology.createSampleTopology1();
80 topology.removeLink(1L, 12L, 2L, 21L);
81 topology.removeLink(1L, 14L, 4L, 41L);
Ray Milkey269ffb92014-04-03 14:43:30 -070082 // now, there is no path from switch 1, but to switch1
Toshio Koidefe1d5d92014-02-26 20:09:48 -080083
Jonathan Harte37e4e22014-05-13 19:12:02 -070084 ConstrainedBFSTree tree1 = new ConstrainedBFSTree(topology.getSwitch(1L));
85 Path path12 = tree1.getPath(topology.getSwitch(2L));
86 Path path13 = tree1.getPath(topology.getSwitch(3L));
87 Path path14 = tree1.getPath(topology.getSwitch(4L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080088
Jonathan Harte37e4e22014-05-13 19:12:02 -070089 ConstrainedBFSTree tree2 = new ConstrainedBFSTree(topology.getSwitch(2L));
90 Path path21 = tree2.getPath(topology.getSwitch(1L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -080091
Ray Milkey269ffb92014-04-03 14:43:30 -070092 assertNull(path12);
93 assertNull(path13);
94 assertNull(path14);
95 assertNotNull(path21);
96 assertEquals(1, path21.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -070097 assertEquals(new LinkEvent(topology.getOutgoingLink(2L, 21L)), path21.get(0));
Ray Milkey269ffb92014-04-03 14:43:30 -070098 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 @Test
101 public void testGetConstrainedPath() {
Jonathan Harte37e4e22014-05-13 19:12:02 -0700102 MockTopology topology = new MockTopology();
103 topology.createSampleTopology1();
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 PathIntentMap intents = new PathIntentMap();
105 IntentOperationList intentOps = new IntentOperationList();
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800106
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 // create constrained shortest path intents that have the same source destination ports
108 ConstrainedShortestPathIntent intent1 = new ConstrainedShortestPathIntent(
109 "1", 1L, LOCAL_PORT, 0x111L, 2L, LOCAL_PORT, 0x222L, 600.0);
110 ConstrainedShortestPathIntent intent2 = new ConstrainedShortestPathIntent(
111 "2", 1L, LOCAL_PORT, 0x333L, 2L, LOCAL_PORT, 0x444L, 600.0);
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800112
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 // calculate path of the intent1
Jonathan Harte37e4e22014-05-13 19:12:02 -0700114 ConstrainedBFSTree tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 600.0);
115 Path path1 = tree.getPath(topology.getSwitch(2L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 assertNotNull(path1);
118 assertEquals(1, path1.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -0700119 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 12L)), path1.get(0));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800120
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 PathIntent pathIntent1 = new PathIntent("pi1", path1, 600.0, intent1);
122 intentOps.add(Operator.ADD, pathIntent1);
123 intents.executeOperations(intentOps);
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800124
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 // calculate path of the intent2
Jonathan Harte37e4e22014-05-13 19:12:02 -0700126 tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 600.0);
127 Path path2 = tree.getPath(topology.getSwitch(2L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800128
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 assertNotNull(path2);
130 assertEquals(2, path2.size());
Jonathan Harte37e4e22014-05-13 19:12:02 -0700131 assertEquals(new LinkEvent(topology.getOutgoingLink(1L, 14L)), path2.get(0));
132 assertEquals(new LinkEvent(topology.getOutgoingLink(4L, 42L)), path2.get(1));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 PathIntent pathIntent2 = new PathIntent("pi2", path2, 600.0, intent2);
135 intentOps.add(Operator.ADD, pathIntent2);
136 intents.executeOperations(intentOps);
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800137
Ray Milkey269ffb92014-04-03 14:43:30 -0700138 // calculate path of the intent3
Jonathan Harte37e4e22014-05-13 19:12:02 -0700139 tree = new ConstrainedBFSTree(topology.getSwitch(1L), intents, 600.0);
140 Path path3 = tree.getPath(topology.getSwitch(2L));
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800141
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 assertNull(path3);
143 }
Toshio Koidefe1d5d92014-02-26 20:09:48 -0800144}