blob: 81938f7f3422369e0bee2e7f63f4d379d5dbd011 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.flowprogrammer;
Naoki Shiota75b7dd62013-12-03 18:09:21 -08002
Naoki Shiota75b7dd62013-12-03 18:09:21 -08003import net.floodlightcontroller.core.FloodlightContext;
4import net.floodlightcontroller.core.IFloodlightProviderService;
5import net.floodlightcontroller.core.IOFSwitch;
6import net.floodlightcontroller.core.module.FloodlightModuleContext;
7import net.floodlightcontroller.threadpool.IThreadPoolService;
8import net.floodlightcontroller.util.OFMessageDamper;
Jonathan Hart23701d12014-04-03 10:45:48 -07009import net.onrc.onos.core.util.Dpid;
10import net.onrc.onos.core.util.FlowEntry;
11import net.onrc.onos.core.util.FlowEntryActions;
12import net.onrc.onos.core.util.FlowEntryErrorState;
13import net.onrc.onos.core.util.FlowEntryId;
14import net.onrc.onos.core.util.FlowEntryMatch;
15import net.onrc.onos.core.util.FlowEntryUserState;
16import net.onrc.onos.core.util.FlowId;
Ray Milkey10643572014-06-10 15:17:39 -070017import net.onrc.onos.core.util.IntegrationTest;
Jonathan Hart23701d12014-04-03 10:45:48 -070018import net.onrc.onos.core.util.Port;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080019import org.easymock.EasyMock;
20import org.easymock.IAnswer;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080021import org.junit.Test;
Ray Milkey10643572014-06-10 15:17:39 -070022import org.junit.experimental.categories.Category;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080023import org.openflow.protocol.OFBarrierRequest;
24import org.openflow.protocol.OFFlowMod;
25import org.openflow.protocol.OFMatch;
26import org.openflow.protocol.OFMessage;
27import org.openflow.protocol.OFType;
28import org.openflow.protocol.action.OFAction;
29import org.openflow.protocol.factory.BasicFactory;
30
Ray Milkey10643572014-06-10 15:17:39 -070031import java.io.IOException;
32import java.util.ArrayList;
33import java.util.HashMap;
34import java.util.List;
35import java.util.Map;
36import java.util.concurrent.ScheduledExecutorService;
37import java.util.concurrent.TimeUnit;
38
39import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertNotNull;
41import static org.junit.Assert.assertTrue;
42import static org.junit.Assert.fail;
43
44@Category(IntegrationTest.class)
Naoki Shiota75b7dd62013-12-03 18:09:21 -080045public class FlowPusherTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070046 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 Shiota75b7dd62013-12-03 18:09:21 -080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 /**
55 * Test single OFMessage is correctly sent to single switch via MessageDamper.
56 */
57 @Test
58 public void testAddMessage() {
59 beginInitMock();
Naoki Shiota75b7dd62013-12-03 18:09:21 -080060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 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 Shiota75b7dd62013-12-03 18:09:21 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 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 Shiotad6ef3b32014-03-13 18:42:23 -070071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 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 Shiota75b7dd62013-12-03 18:09:21 -080078
Ray Milkey269ffb92014-04-03 14:43:30 -070079 endInitMock();
80 initPusher(1);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -070081
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070082 boolean addResult = pusher.add(sw, msg);
83 assertTrue(addResult);
Naoki Shiota75b7dd62013-12-03 18:09:21 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 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 Shiota75b7dd62013-12-03 18:09:21 -080094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 pusher.stop();
96 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 /**
99 * Test bunch of OFMessages are correctly sent to single switch via MessageDamper.
100 */
101 @Test
102 public void testMassiveAddMessage() {
Yuta HIGUCHI7c39c842014-06-09 16:33:03 -0700103 // Some number larger than FlowPusher.MAX_MESSAGE_SEND
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700104 final int numMsg = FlowPusher.MAX_MESSAGE_SEND * 2;
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700105
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 beginInitMock();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800107
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 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 Shiota75b7dd62013-12-03 18:09:21 -0800113
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 List<OFMessage> messages = new ArrayList<OFMessage>();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800115
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700116 for (int i = 0; i < numMsg; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 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 Shiota75b7dd62013-12-03 18:09:21 -0800122
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 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 Shiotad6ef3b32014-03-13 18:42:23 -0700130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 endInitMock();
132 initPusher(1);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 for (OFMessage msg : messages) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700135 boolean addResult = pusher.add(sw, msg);
136 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800138
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 try {
140 // wait until message is processed.
Yuta HIGUCHI7c39c842014-06-09 16:33:03 -0700141 Thread.sleep(1000);
Ray Milkey269ffb92014-04-03 14:43:30 -0700142 } catch (InterruptedException e) {
143 fail("Failed in Thread.sleep()");
144 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700145
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 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 HIGUCHI91a8f502014-06-17 10:15:29 -0700160 final int numSwitch = 10;
161 final int numMsg = 100; // messages per thread
Ray Milkey269ffb92014-04-03 14:43:30 -0700162
163 beginInitMock();
164
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700165 Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700166 for (int i = 0; i < numSwitch; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 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 HIGUCHI91a8f502014-06-17 10:15:29 -0700175 for (int j = 0; j < numMsg; ++j) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700176 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 HIGUCHI44a0b352014-05-14 21:32:48 -0700189 swMap.put(sw, messages);
Ray Milkey269ffb92014-04-03 14:43:30 -0700190 }
191
192 endInitMock();
193 initPusher(1);
194
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700195 for (IOFSwitch sw : swMap.keySet()) {
196 for (OFMessage msg : swMap.get(sw)) {
197 boolean addResult = pusher.add(sw, msg);
198 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700199 }
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 HIGUCHI44a0b352014-05-14 21:32:48 -0700209 for (IOFSwitch sw : swMap.keySet()) {
210 for (OFMessage msg : swMap.get(sw)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700211 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 HIGUCHI91a8f502014-06-17 10:15:29 -0700226 final int numThreads = 10;
227 final int numMsg = 100; // messages per thread
Ray Milkey269ffb92014-04-03 14:43:30 -0700228
229 beginInitMock();
230
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700231 Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700232 for (int i = 0; i < numThreads; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700233 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 HIGUCHI91a8f502014-06-17 10:15:29 -0700241 for (int j = 0; j < numMsg; ++j) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700242 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 HIGUCHI44a0b352014-05-14 21:32:48 -0700255 swMap.put(sw, messages);
Ray Milkey269ffb92014-04-03 14:43:30 -0700256 }
257
258 endInitMock();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700259 initPusher(numThreads);
Ray Milkey269ffb92014-04-03 14:43:30 -0700260
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700261 for (IOFSwitch sw : swMap.keySet()) {
262 for (OFMessage msg : swMap.get(sw)) {
263 boolean addResult = pusher.add(sw, msg);
264 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700265 }
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 HIGUCHI44a0b352014-05-14 21:32:48 -0700275 for (IOFSwitch sw : swMap.keySet()) {
276 for (OFMessage msg : swMap.get(sw)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700277 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 HIGUCHI91a8f502014-06-17 10:15:29 -0700294 final long limitRate = 100; // [bytes/ms]
295 final int numMsg = 1000;
Ray Milkey269ffb92014-04-03 14:43:30 -0700296
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 HIGUCHI91a8f502014-06-17 10:15:29 -0700301 final double acceptableRate = limitRate * 1.2;
Ray Milkey269ffb92014-04-03 14:43:30 -0700302
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 HIGUCHI91a8f502014-06-17 10:15:29 -0700314 for (int i = 0; i < numMsg; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700315 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 HIGUCHI91a8f502014-06-17 10:15:29 -0700350 pusher.setRate(sw, limitRate);
Ray Milkey269ffb92014-04-03 14:43:30 -0700351
352 long beginTime = System.currentTimeMillis();
353 for (OFMessage msg : messages) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700354 boolean addResult = pusher.add(sw, msg);
355 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700356 }
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 HIGUCHI91a8f502014-06-17 10:15:29 -0700368 double measuredRate = numMsg * 100 / (barrierTime - beginTime);
369 assertTrue(measuredRate < acceptableRate);
Ray Milkey269ffb92014-04-03 14:43:30 -0700370
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));
432 flowEntry1.setInPort(new Port((short) 1));
433 flowEntry1.setOutPort(new Port((short) 11));
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 HIGUCHI44a0b352014-05-14 21:32:48 -0700542 private void initPusher(int numThread) {
543 pusher = new FlowPusher(numThread);
Ray Milkey269ffb92014-04-03 14:43:30 -0700544 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 Shiota75b7dd62013-12-03 18:09:21 -0800560}