Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent; |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 3 | import static org.easymock.EasyMock.createMock; |
| 4 | import static org.easymock.EasyMock.expect; |
| 5 | import static org.easymock.EasyMock.replay; |
| 6 | import static org.junit.Assert.assertEquals; |
| 7 | import static org.junit.Assert.assertTrue; |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 8 | |
| 9 | import java.util.Collection; |
| 10 | |
Jonathan Hart | aa38097 | 2014-04-03 10:24:46 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.topology.Link; |
| 13 | import net.onrc.onos.core.topology.LinkEvent; |
Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 14 | import net.onrc.onos.core.topology.Port; |
| 15 | import net.onrc.onos.core.topology.Switch; |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 16 | |
| 17 | import org.junit.After; |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | |
| 21 | public class PathIntentMapTest { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | Link link12, link23, link24; |
| 23 | Switch sw1, sw2, sw3, sw4; |
| 24 | Port port11, port22, port21, port23, port31, port41; |
| 25 | Path path1, path2; |
| 26 | PathIntent intent1, intent2; |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 27 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 28 | @Before |
| 29 | public void setUp() throws Exception { |
| 30 | sw1 = createMock(Switch.class); |
| 31 | sw2 = createMock(Switch.class); |
| 32 | sw3 = createMock(Switch.class); |
| 33 | sw4 = createMock(Switch.class); |
| 34 | expect(sw1.getDpid()).andReturn(1L).anyTimes(); |
| 35 | expect(sw2.getDpid()).andReturn(2L).anyTimes(); |
| 36 | expect(sw3.getDpid()).andReturn(3L).anyTimes(); |
| 37 | expect(sw4.getDpid()).andReturn(4L).anyTimes(); |
| 38 | replay(sw1); |
| 39 | replay(sw2); |
| 40 | replay(sw3); |
| 41 | replay(sw4); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 42 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 43 | port11 = createMock(Port.class); |
| 44 | port22 = createMock(Port.class); |
| 45 | port21 = createMock(Port.class); |
| 46 | port23 = createMock(Port.class); |
| 47 | port31 = createMock(Port.class); |
| 48 | port41 = createMock(Port.class); |
| 49 | expect(port11.getNumber()).andReturn(1L).anyTimes(); |
| 50 | expect(port22.getNumber()).andReturn(2L).anyTimes(); |
| 51 | expect(port21.getNumber()).andReturn(1L).anyTimes(); |
| 52 | expect(port23.getNumber()).andReturn(3L).anyTimes(); |
| 53 | expect(port31.getNumber()).andReturn(1L).anyTimes(); |
| 54 | expect(port41.getNumber()).andReturn(1L).anyTimes(); |
| 55 | replay(port11); |
| 56 | replay(port22); |
| 57 | replay(port21); |
| 58 | replay(port23); |
| 59 | replay(port31); |
| 60 | replay(port41); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | link12 = createMock(Link.class); |
| 63 | link23 = createMock(Link.class); |
| 64 | link24 = createMock(Link.class); |
| 65 | expect(link12.getCapacity()).andReturn(1000.0).anyTimes(); |
| 66 | expect(link23.getCapacity()).andReturn(1000.0).anyTimes(); |
| 67 | expect(link24.getCapacity()).andReturn(1000.0).anyTimes(); |
| 68 | expect(link12.getSrcSwitch()).andReturn(sw1).anyTimes(); |
| 69 | expect(link23.getSrcSwitch()).andReturn(sw2).anyTimes(); |
| 70 | expect(link24.getSrcSwitch()).andReturn(sw2).anyTimes(); |
| 71 | expect(link12.getSrcPort()).andReturn(port11).anyTimes(); |
| 72 | expect(link23.getSrcPort()).andReturn(port21).anyTimes(); |
| 73 | expect(link24.getSrcPort()).andReturn(port23).anyTimes(); |
| 74 | expect(link12.getDstSwitch()).andReturn(sw2).anyTimes(); |
| 75 | expect(link23.getDstSwitch()).andReturn(sw3).anyTimes(); |
| 76 | expect(link24.getDstSwitch()).andReturn(sw4).anyTimes(); |
| 77 | expect(link12.getDstPort()).andReturn(port22).anyTimes(); |
| 78 | expect(link23.getDstPort()).andReturn(port31).anyTimes(); |
| 79 | expect(link24.getDstPort()).andReturn(port41).anyTimes(); |
| 80 | replay(link12); |
| 81 | replay(link23); |
| 82 | replay(link24); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | path1 = new Path(); |
| 85 | path1.add(new LinkEvent(link12)); |
| 86 | path1.add(new LinkEvent(link23)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 87 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | path2 = new Path(); |
| 89 | path2.add(new LinkEvent(link12)); |
| 90 | path2.add(new LinkEvent(link24)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 91 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 92 | intent1 = new PathIntent("1", path1, 400.0, new Intent("_1")); |
| 93 | intent2 = new PathIntent("2", path2, 400.0, new Intent("_2")); |
| 94 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | @After |
| 97 | public void tearDown() throws Exception { |
| 98 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | @Test |
| 101 | public void testCreate() { |
| 102 | PathIntentMap intents = new PathIntentMap(); |
| 103 | assertEquals(0, intents.getAllIntents().size()); |
| 104 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 105 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | @Test |
| 107 | public void testGetIntentsByDpid() { |
| 108 | IntentOperationList operations = new IntentOperationList(); |
| 109 | operations.add(Operator.ADD, intent1); |
| 110 | operations.add(Operator.ADD, intent2); |
| 111 | assertEquals(2, operations.size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 112 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | PathIntentMap intents = new PathIntentMap(); |
| 114 | intents.executeOperations(operations); |
| 115 | assertEquals(2, intents.getAllIntents().size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | Collection<PathIntent> pathIntents = intents.getIntentsByDpid(1L); |
| 118 | assertEquals(2, pathIntents.size()); |
| 119 | assertTrue(pathIntents.contains(intent1)); |
| 120 | assertTrue(pathIntents.contains(intent2)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 121 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | pathIntents = intents.getIntentsByDpid(2L); |
| 123 | assertEquals(2, pathIntents.size()); |
| 124 | assertTrue(pathIntents.contains(intent1)); |
| 125 | assertTrue(pathIntents.contains(intent2)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 126 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | pathIntents = intents.getIntentsByDpid(3L); |
| 128 | assertEquals(1, pathIntents.size()); |
| 129 | assertTrue(pathIntents.contains(intent1)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 130 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | pathIntents = intents.getIntentsByDpid(4L); |
| 132 | assertEquals(1, pathIntents.size()); |
| 133 | assertTrue(pathIntents.contains(intent2)); |
| 134 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 135 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | @Test |
| 137 | public void testGetPathIntentsByPort() { |
| 138 | IntentOperationList operations = new IntentOperationList(); |
| 139 | operations.add(Operator.ADD, intent1); |
| 140 | operations.add(Operator.ADD, intent2); |
| 141 | assertEquals(2, operations.size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 142 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 143 | PathIntentMap intents = new PathIntentMap(); |
| 144 | intents.executeOperations(operations); |
| 145 | assertEquals(2, intents.getAllIntents().size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 146 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | Collection<PathIntent> pathIntents = intents.getIntentsByPort(1L, 1L); |
| 148 | assertEquals(2, pathIntents.size()); |
| 149 | assertTrue(pathIntents.contains(intent1)); |
| 150 | assertTrue(pathIntents.contains(intent2)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 151 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | pathIntents = intents.getIntentsByPort(2L, 1L); |
| 153 | assertEquals(1, pathIntents.size()); |
| 154 | assertTrue(pathIntents.contains(intent1)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 155 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 156 | pathIntents = intents.getIntentsByPort(2L, 3L); |
| 157 | assertEquals(1, pathIntents.size()); |
| 158 | assertTrue(pathIntents.contains(intent2)); |
| 159 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 160 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | @Test |
| 162 | public void testGetPathIntentsByLink() { |
| 163 | IntentOperationList operations = new IntentOperationList(); |
| 164 | operations.add(Operator.ADD, intent1); |
| 165 | operations.add(Operator.ADD, intent2); |
| 166 | assertEquals(2, operations.size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 167 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 168 | PathIntentMap intents = new PathIntentMap(); |
| 169 | intents.executeOperations(operations); |
| 170 | assertEquals(2, intents.getAllIntents().size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 171 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | Collection<PathIntent> pathIntents = intents.getIntentsByLink(new LinkEvent(link12)); |
| 173 | assertEquals(2, pathIntents.size()); |
| 174 | assertTrue(pathIntents.contains(intent1)); |
| 175 | assertTrue(pathIntents.contains(intent2)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 176 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | pathIntents = intents.getIntentsByLink(new LinkEvent(link23)); |
| 178 | assertEquals(1, pathIntents.size()); |
| 179 | assertTrue(pathIntents.contains(intent1)); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 180 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | pathIntents = intents.getIntentsByLink(new LinkEvent(link24)); |
| 182 | assertEquals(1, pathIntents.size()); |
| 183 | assertTrue(pathIntents.contains(intent2)); |
| 184 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 185 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 186 | @Test |
| 187 | public void testGetAvailableBandwidth() { |
| 188 | IntentOperationList operations = new IntentOperationList(); |
| 189 | operations.add(Operator.ADD, intent1); |
| 190 | operations.add(Operator.ADD, intent2); |
| 191 | assertEquals(2, operations.size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 192 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 193 | PathIntentMap intents = new PathIntentMap(); |
| 194 | intents.executeOperations(operations); |
| 195 | assertEquals(2, intents.getAllIntents().size()); |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 196 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | assertEquals(200.0, intents.getAvailableBandwidth(link12), 0.0); |
| 198 | } |
Toshio Koide | 122e570 | 2014-02-21 17:53:51 -0800 | [diff] [blame] | 199 | } |