Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowprogrammer; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 2 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 3 | import net.floodlightcontroller.core.FloodlightContext; |
| 4 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 5 | import net.floodlightcontroller.core.IOFSwitch; |
| 6 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 7 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
| 8 | import net.floodlightcontroller.util.OFMessageDamper; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.Dpid; |
| 10 | import net.onrc.onos.core.util.FlowEntry; |
| 11 | import net.onrc.onos.core.util.FlowEntryActions; |
| 12 | import net.onrc.onos.core.util.FlowEntryErrorState; |
| 13 | import net.onrc.onos.core.util.FlowEntryId; |
| 14 | import net.onrc.onos.core.util.FlowEntryMatch; |
| 15 | import net.onrc.onos.core.util.FlowEntryUserState; |
| 16 | import net.onrc.onos.core.util.FlowId; |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.util.IntegrationTest; |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 18 | import net.onrc.onos.core.util.PortNumber; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 19 | import org.easymock.EasyMock; |
| 20 | import org.easymock.IAnswer; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 21 | import org.junit.Test; |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 22 | import org.junit.experimental.categories.Category; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 23 | import org.openflow.protocol.OFBarrierRequest; |
| 24 | import org.openflow.protocol.OFFlowMod; |
| 25 | import org.openflow.protocol.OFMatch; |
| 26 | import org.openflow.protocol.OFMessage; |
| 27 | import org.openflow.protocol.OFType; |
| 28 | import org.openflow.protocol.action.OFAction; |
| 29 | import org.openflow.protocol.factory.BasicFactory; |
| 30 | |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.HashMap; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.concurrent.ScheduledExecutorService; |
| 37 | import java.util.concurrent.TimeUnit; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | import static org.junit.Assert.assertNotNull; |
| 41 | import static org.junit.Assert.assertTrue; |
| 42 | import static org.junit.Assert.fail; |
| 43 | |
| 44 | @Category(IntegrationTest.class) |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 45 | public class FlowPusherTest { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | private FlowPusher pusher; |
| 47 | private FloodlightContext context; |
| 48 | private FloodlightModuleContext modContext; |
| 49 | private BasicFactory factory; |
| 50 | private OFMessageDamper damper; |
| 51 | private IFloodlightProviderService flProviderService; |
| 52 | private IThreadPoolService threadPoolService; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 53 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Test single OFMessage is correctly sent to single switch via MessageDamper. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testAddMessage() { |
| 59 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 62 | EasyMock.expect(msg.getXid()).andReturn(1).anyTimes(); |
| 63 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 64 | EasyMock.replay(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 65 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 67 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 68 | sw.flush(); |
| 69 | EasyMock.expectLastCall().once(); |
| 70 | EasyMock.replay(sw); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 71 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 72 | try { |
| 73 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 74 | .andReturn(true).once(); |
| 75 | } catch (IOException e1) { |
| 76 | fail("Failed in OFMessageDamper#write()"); |
| 77 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 78 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | endInitMock(); |
| 80 | initPusher(1); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 81 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 82 | boolean addResult = pusher.add(sw, msg); |
| 83 | assertTrue(addResult); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | try { |
| 86 | // wait until message is processed. |
| 87 | Thread.sleep(1000); |
| 88 | } catch (InterruptedException e) { |
| 89 | fail("Failed in Thread.sleep()"); |
| 90 | } |
| 91 | EasyMock.verify(msg); |
| 92 | EasyMock.verify(sw); |
| 93 | verifyAll(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 94 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 95 | pusher.stop(); |
| 96 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 97 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Test bunch of OFMessages are correctly sent to single switch via MessageDamper. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testMassiveAddMessage() { |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 103 | // Some number larger than FlowPusher.MAX_MESSAGE_SEND |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 104 | final int numMsg = FlowPusher.MAX_MESSAGE_SEND * 2; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 105 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 107 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 108 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 109 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 110 | sw.flush(); |
| 111 | EasyMock.expectLastCall().atLeastOnce(); |
| 112 | EasyMock.replay(sw); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 113 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 115 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 116 | for (int i = 0; i < numMsg; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 118 | EasyMock.expect(msg.getXid()).andReturn(i).anyTimes(); |
| 119 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 120 | EasyMock.replay(msg); |
| 121 | messages.add(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 122 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | try { |
| 124 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 125 | .andReturn(true).once(); |
| 126 | } catch (IOException e1) { |
| 127 | fail("Failed in OFMessageDamper#write()"); |
| 128 | } |
| 129 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 130 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 131 | endInitMock(); |
| 132 | initPusher(1); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 133 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 134 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 135 | boolean addResult = pusher.add(sw, msg); |
| 136 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 138 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | try { |
| 140 | // wait until message is processed. |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 141 | Thread.sleep(1000); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | } catch (InterruptedException e) { |
| 143 | fail("Failed in Thread.sleep()"); |
| 144 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 145 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 146 | for (OFMessage msg : messages) { |
| 147 | EasyMock.verify(msg); |
| 148 | } |
| 149 | EasyMock.verify(sw); |
| 150 | verifyAll(); |
| 151 | |
| 152 | pusher.stop(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Test bunch of OFMessages are correctly sent to multiple switches with single threads. |
| 157 | */ |
| 158 | @Test |
| 159 | public void testMultiSwitchAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 160 | final int numSwitch = 10; |
| 161 | final int numMsg = 100; // messages per thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 162 | |
| 163 | beginInitMock(); |
| 164 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 165 | Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 166 | for (int i = 0; i < numSwitch; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 168 | EasyMock.expect(sw.getId()).andReturn((long) i).anyTimes(); |
| 169 | sw.flush(); |
| 170 | EasyMock.expectLastCall().atLeastOnce(); |
| 171 | EasyMock.replay(sw); |
| 172 | |
| 173 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 174 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 175 | for (int j = 0; j < numMsg; ++j) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 176 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 177 | EasyMock.expect(msg.getXid()).andReturn(j).anyTimes(); |
| 178 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 179 | EasyMock.replay(msg); |
| 180 | messages.add(msg); |
| 181 | |
| 182 | try { |
| 183 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 184 | .andReturn(true).once(); |
| 185 | } catch (IOException e1) { |
| 186 | fail("Failed in OFMessageDamper#write()"); |
| 187 | } |
| 188 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 189 | swMap.put(sw, messages); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | endInitMock(); |
| 193 | initPusher(1); |
| 194 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 195 | for (IOFSwitch sw : swMap.keySet()) { |
| 196 | for (OFMessage msg : swMap.get(sw)) { |
| 197 | boolean addResult = pusher.add(sw, msg); |
| 198 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | try { |
| 203 | // wait until message is processed. |
| 204 | Thread.sleep(1000); |
| 205 | } catch (InterruptedException e) { |
| 206 | fail("Failed in Thread.sleep()"); |
| 207 | } |
| 208 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 209 | for (IOFSwitch sw : swMap.keySet()) { |
| 210 | for (OFMessage msg : swMap.get(sw)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 211 | EasyMock.verify(msg); |
| 212 | } |
| 213 | |
| 214 | EasyMock.verify(sw); |
| 215 | } |
| 216 | verifyAll(); |
| 217 | |
| 218 | pusher.stop(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Test bunch of OFMessages are correctly sent to multiple switches using multiple threads. |
| 223 | */ |
| 224 | @Test |
| 225 | public void testMultiThreadedAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 226 | final int numThreads = 10; |
| 227 | final int numMsg = 100; // messages per thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 228 | |
| 229 | beginInitMock(); |
| 230 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 231 | Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 232 | for (int i = 0; i < numThreads; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 234 | EasyMock.expect(sw.getId()).andReturn((long) i).anyTimes(); |
| 235 | sw.flush(); |
| 236 | EasyMock.expectLastCall().atLeastOnce(); |
| 237 | EasyMock.replay(sw); |
| 238 | |
| 239 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 240 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 241 | for (int j = 0; j < numMsg; ++j) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 243 | EasyMock.expect(msg.getXid()).andReturn(j).anyTimes(); |
| 244 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 245 | EasyMock.replay(msg); |
| 246 | messages.add(msg); |
| 247 | |
| 248 | try { |
| 249 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 250 | .andReturn(true).once(); |
| 251 | } catch (IOException e1) { |
| 252 | fail("Failed in OFMessageDamper#write()"); |
| 253 | } |
| 254 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 255 | swMap.put(sw, messages); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | endInitMock(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 259 | initPusher(numThreads); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 261 | for (IOFSwitch sw : swMap.keySet()) { |
| 262 | for (OFMessage msg : swMap.get(sw)) { |
| 263 | boolean addResult = pusher.add(sw, msg); |
| 264 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | try { |
| 269 | // wait until message is processed. |
| 270 | Thread.sleep(1000); |
| 271 | } catch (InterruptedException e) { |
| 272 | fail("Failed in Thread.sleep()"); |
| 273 | } |
| 274 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 275 | for (IOFSwitch sw : swMap.keySet()) { |
| 276 | for (OFMessage msg : swMap.get(sw)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 277 | EasyMock.verify(msg); |
| 278 | } |
| 279 | |
| 280 | EasyMock.verify(sw); |
| 281 | } |
| 282 | verifyAll(); |
| 283 | |
| 284 | pusher.stop(); |
| 285 | } |
| 286 | |
| 287 | private long barrierTime = 0; |
| 288 | |
| 289 | /** |
| 290 | * Test rate limitation of messages works correctly. |
| 291 | */ |
| 292 | @Test |
| 293 | public void testRateLimitedAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 294 | final long limitRate = 100; // [bytes/ms] |
| 295 | final int numMsg = 1000; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 296 | |
| 297 | // Accuracy of FlowPusher's rate calculation can't be measured by unit test |
| 298 | // because switch doesn't return BARRIER_REPLY. |
| 299 | // In unit test we use approximate way to measure rate. This value is |
| 300 | // acceptable margin of measured rate. |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 301 | final double acceptableRate = limitRate * 1.2; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 302 | |
| 303 | beginInitMock(); |
| 304 | |
| 305 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 306 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 307 | sw.flush(); |
| 308 | EasyMock.expectLastCall().atLeastOnce(); |
| 309 | prepareBarrier(sw); |
| 310 | EasyMock.replay(sw); |
| 311 | |
| 312 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 313 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 314 | for (int i = 0; i < numMsg; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 315 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 316 | EasyMock.expect(msg.getXid()).andReturn(1).anyTimes(); |
| 317 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 318 | EasyMock.expect(msg.getLengthU()).andReturn(100).anyTimes(); |
| 319 | EasyMock.replay(msg); |
| 320 | messages.add(msg); |
| 321 | |
| 322 | try { |
| 323 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 324 | .andReturn(true).once(); |
| 325 | } catch (IOException e) { |
| 326 | fail("Failed in OFMessageDamper#write()"); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | try { |
| 331 | EasyMock.expect(damper.write(EasyMock.eq(sw), (OFMessage) EasyMock.anyObject(), EasyMock.eq(context))) |
| 332 | .andAnswer(new IAnswer<Boolean>() { |
| 333 | @Override |
| 334 | public Boolean answer() throws Throwable { |
| 335 | OFMessage msg = (OFMessage) EasyMock.getCurrentArguments()[1]; |
| 336 | if (msg.getType() == OFType.BARRIER_REQUEST) { |
| 337 | barrierTime = System.currentTimeMillis(); |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 | }).once(); |
| 342 | } catch (IOException e1) { |
| 343 | fail("Failed in OFMessageDamper#write()"); |
| 344 | } |
| 345 | |
| 346 | endInitMock(); |
| 347 | initPusher(1); |
| 348 | |
| 349 | pusher.createQueue(sw); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 350 | pusher.setRate(sw, limitRate); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 351 | |
| 352 | long beginTime = System.currentTimeMillis(); |
| 353 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 354 | boolean addResult = pusher.add(sw, msg); |
| 355 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | pusher.barrierAsync(sw); |
| 359 | |
| 360 | try { |
| 361 | do { |
| 362 | Thread.sleep(1000); |
| 363 | } while (barrierTime == 0); |
| 364 | } catch (InterruptedException e) { |
| 365 | fail("Failed to sleep"); |
| 366 | } |
| 367 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 368 | double measuredRate = numMsg * 100 / (barrierTime - beginTime); |
| 369 | assertTrue(measuredRate < acceptableRate); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 370 | |
| 371 | for (OFMessage msg : messages) { |
| 372 | EasyMock.verify(msg); |
| 373 | } |
| 374 | EasyMock.verify(sw); |
| 375 | verifyAll(); |
| 376 | |
| 377 | pusher.stop(); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Test barrier message is correctly sent to a switch. |
| 382 | */ |
| 383 | @Test |
| 384 | public void testBarrierMessage() { |
| 385 | beginInitMock(); |
| 386 | |
| 387 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 388 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 389 | sw.flush(); |
| 390 | EasyMock.expectLastCall().atLeastOnce(); |
| 391 | prepareBarrier(sw); |
| 392 | EasyMock.replay(sw); |
| 393 | |
| 394 | try { |
| 395 | EasyMock.expect(damper.write(EasyMock.eq(sw), (OFMessage) EasyMock.anyObject(), EasyMock.eq(context))) |
| 396 | .andReturn(true).once(); |
| 397 | } catch (IOException e1) { |
| 398 | fail("Failed in OFMessageDamper#write()"); |
| 399 | } |
| 400 | |
| 401 | endInitMock(); |
| 402 | initPusher(1); |
| 403 | |
| 404 | OFBarrierReplyFuture future = pusher.barrierAsync(sw); |
| 405 | |
| 406 | assertNotNull(future); |
| 407 | |
| 408 | try { |
| 409 | Thread.sleep(1000); |
| 410 | } catch (InterruptedException e) { |
| 411 | fail("Failed to sleep"); |
| 412 | } |
| 413 | |
| 414 | verifyAll(); |
| 415 | |
| 416 | pusher.stop(); |
| 417 | } |
| 418 | |
| 419 | static final int XID_TO_VERIFY = 100; |
| 420 | static final long DPID_TO_VERIFY = 10; |
| 421 | |
| 422 | /** |
| 423 | * Test FlowObject is correctly converted to message and is sent to a switch. |
| 424 | */ |
| 425 | @SuppressWarnings("unchecked") |
| 426 | @Test |
| 427 | public void testAddFlow() { |
| 428 | // instantiate required objects |
| 429 | FlowEntry flowEntry1 = new FlowEntry(); |
| 430 | flowEntry1.setDpid(new Dpid(DPID_TO_VERIFY)); |
| 431 | flowEntry1.setFlowId(new FlowId(1)); |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 432 | flowEntry1.setInPort(new PortNumber((short) 1)); |
| 433 | flowEntry1.setOutPort(new PortNumber((short) 11)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 434 | flowEntry1.setFlowEntryId(new FlowEntryId(1)); |
| 435 | flowEntry1.setFlowEntryMatch(new FlowEntryMatch()); |
| 436 | flowEntry1.setFlowEntryActions(new FlowEntryActions()); |
| 437 | flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState()); |
| 438 | flowEntry1.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
| 439 | |
| 440 | beginInitMock(); |
| 441 | |
| 442 | OFFlowMod msg = EasyMock.createMock(OFFlowMod.class); |
| 443 | EasyMock.expect(msg.setIdleTimeout(EasyMock.anyShort())).andReturn(msg); |
| 444 | EasyMock.expect(msg.setHardTimeout(EasyMock.anyShort())).andReturn(msg); |
| 445 | EasyMock.expect(msg.setPriority(EasyMock.anyShort())).andReturn(msg); |
| 446 | EasyMock.expect(msg.setBufferId(EasyMock.anyInt())).andReturn(msg); |
| 447 | EasyMock.expect(msg.setCookie(EasyMock.anyLong())).andReturn(msg); |
| 448 | EasyMock.expect(msg.setCommand(EasyMock.anyShort())).andReturn(msg); |
| 449 | EasyMock.expect(msg.setMatch(EasyMock.anyObject(OFMatch.class))).andReturn(msg); |
| 450 | EasyMock.expect(msg.setActions((List<OFAction>) EasyMock.anyObject())).andReturn(msg); |
| 451 | EasyMock.expect(msg.setLengthU(EasyMock.anyShort())).andReturn(msg); |
| 452 | EasyMock.expect(msg.setOutPort(EasyMock.anyShort())).andReturn(msg).atLeastOnce(); |
| 453 | EasyMock.expect(msg.getXid()).andReturn(XID_TO_VERIFY).anyTimes(); |
| 454 | EasyMock.expect(msg.getType()).andReturn(OFType.FLOW_MOD).anyTimes(); |
| 455 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 456 | EasyMock.replay(msg); |
| 457 | |
| 458 | EasyMock.expect(factory.getMessage(EasyMock.eq(OFType.FLOW_MOD))).andReturn(msg); |
| 459 | |
| 460 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 461 | EasyMock.expect(sw.getId()).andReturn(DPID_TO_VERIFY).anyTimes(); |
| 462 | EasyMock.expect(sw.getStringId()).andReturn("1").anyTimes(); |
| 463 | sw.flush(); |
| 464 | EasyMock.expectLastCall().once(); |
| 465 | |
| 466 | try { |
| 467 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.anyObject(OFMessage.class), EasyMock.eq(context))) |
| 468 | .andAnswer(new IAnswer<Boolean>() { |
| 469 | @Override |
| 470 | public Boolean answer() throws Throwable { |
| 471 | OFMessage msg = (OFMessage) EasyMock.getCurrentArguments()[1]; |
| 472 | if (msg.getType() == OFType.FLOW_MOD) { |
| 473 | assertEquals(msg.getXid(), XID_TO_VERIFY); |
| 474 | } |
| 475 | return true; |
| 476 | } |
| 477 | }).atLeastOnce(); |
| 478 | } catch (IOException e1) { |
| 479 | fail("Failed in OFMessageDamper#write()"); |
| 480 | } |
| 481 | |
| 482 | EasyMock.replay(sw); |
| 483 | |
| 484 | endInitMock(); |
| 485 | initPusher(1); |
| 486 | |
| 487 | pusher.pushFlowEntry(sw, flowEntry1); |
| 488 | |
| 489 | try { |
| 490 | Thread.sleep(1000); |
| 491 | } catch (InterruptedException e) { |
| 492 | fail("Failed to sleep"); |
| 493 | } |
| 494 | |
| 495 | EasyMock.verify(sw); |
| 496 | verifyAll(); |
| 497 | |
| 498 | pusher.stop(); |
| 499 | } |
| 500 | |
| 501 | private void beginInitMock() { |
| 502 | context = EasyMock.createMock(FloodlightContext.class); |
| 503 | modContext = EasyMock.createMock(FloodlightModuleContext.class); |
| 504 | factory = EasyMock.createMock(BasicFactory.class); |
| 505 | damper = EasyMock.createMock(OFMessageDamper.class); |
| 506 | flProviderService = EasyMock.createMock(IFloodlightProviderService.class); |
| 507 | threadPoolService = EasyMock.createMock(IThreadPoolService.class); |
| 508 | |
| 509 | EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IThreadPoolService.class))) |
| 510 | .andReturn(threadPoolService).once(); |
| 511 | EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IFloodlightProviderService.class))) |
| 512 | .andReturn(flProviderService).once(); |
| 513 | flProviderService.addOFMessageListener(EasyMock.eq(OFType.BARRIER_REPLY), |
| 514 | (FlowPusher) EasyMock.anyObject()); |
| 515 | EasyMock.expectLastCall().once(); |
| 516 | |
| 517 | ScheduledExecutorService executor = EasyMock.createMock(ScheduledExecutorService.class); |
| 518 | EasyMock.expect(executor.schedule((Runnable) EasyMock.anyObject(), EasyMock.anyLong(), |
| 519 | (TimeUnit) EasyMock.anyObject())).andReturn(null).once(); |
| 520 | EasyMock.replay(executor); |
| 521 | EasyMock.expect(threadPoolService.getScheduledExecutor()).andReturn(executor).anyTimes(); |
| 522 | } |
| 523 | |
| 524 | private void endInitMock() { |
| 525 | EasyMock.replay(threadPoolService); |
| 526 | EasyMock.replay(flProviderService); |
| 527 | EasyMock.replay(damper); |
| 528 | EasyMock.replay(factory); |
| 529 | EasyMock.replay(modContext); |
| 530 | EasyMock.replay(context); |
| 531 | } |
| 532 | |
| 533 | private void verifyAll() { |
| 534 | EasyMock.verify(threadPoolService); |
| 535 | EasyMock.verify(flProviderService); |
| 536 | EasyMock.verify(damper); |
| 537 | EasyMock.verify(factory); |
| 538 | EasyMock.verify(modContext); |
| 539 | EasyMock.verify(context); |
| 540 | } |
| 541 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 542 | private void initPusher(int numThread) { |
| 543 | pusher = new FlowPusher(numThread); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 544 | pusher.init(context, modContext, factory, damper); |
| 545 | pusher.start(); |
| 546 | } |
| 547 | |
| 548 | private void prepareBarrier(IOFSwitch sw) { |
| 549 | OFBarrierRequest req = EasyMock.createMock(OFBarrierRequest.class); |
| 550 | req.setXid(EasyMock.anyInt()); |
| 551 | EasyMock.expectLastCall().once(); |
| 552 | EasyMock.expect(req.getXid()).andReturn(1).anyTimes(); |
| 553 | EasyMock.expect(req.getType()).andReturn(OFType.BARRIER_REQUEST).anyTimes(); |
| 554 | EasyMock.expect(req.getLength()).andReturn((short) 100).anyTimes(); |
| 555 | EasyMock.replay(req); |
| 556 | EasyMock.expect(factory.getMessage(EasyMock.eq(OFType.BARRIER_REQUEST))).andReturn(req).anyTimes(); |
| 557 | EasyMock.expect(sw.getNextTransactionId()).andReturn(1); |
| 558 | } |
| 559 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 560 | } |