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