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