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