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