blob: 89f188d015571a6c9314acc62d877a85a28442bc [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 Shiota47993102014-07-09 14:00:54 -07003import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertTrue;
6import static org.junit.Assert.fail;
7
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.HashMap;
11import java.util.List;
12import java.util.Map;
13import java.util.concurrent.ScheduledExecutorService;
14import java.util.concurrent.TimeUnit;
15
Naoki Shiota75b7dd62013-12-03 18:09:21 -080016import net.floodlightcontroller.core.FloodlightContext;
17import net.floodlightcontroller.core.IFloodlightProviderService;
18import net.floodlightcontroller.core.IOFSwitch;
19import net.floodlightcontroller.core.module.FloodlightModuleContext;
20import net.floodlightcontroller.threadpool.IThreadPoolService;
21import net.floodlightcontroller.util.OFMessageDamper;
Jonathan Hart23701d12014-04-03 10:45:48 -070022import net.onrc.onos.core.util.Dpid;
23import net.onrc.onos.core.util.FlowEntry;
24import net.onrc.onos.core.util.FlowEntryActions;
25import net.onrc.onos.core.util.FlowEntryErrorState;
26import net.onrc.onos.core.util.FlowEntryId;
27import net.onrc.onos.core.util.FlowEntryMatch;
28import net.onrc.onos.core.util.FlowEntryUserState;
29import net.onrc.onos.core.util.FlowId;
Ray Milkey10643572014-06-10 15:17:39 -070030import net.onrc.onos.core.util.IntegrationTest;
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070031import net.onrc.onos.core.util.PortNumber;
Naoki Shiota47993102014-07-09 14:00:54 -070032
Naoki Shiota75b7dd62013-12-03 18:09:21 -080033import org.easymock.EasyMock;
34import org.easymock.IAnswer;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080035import org.junit.Test;
Ray Milkey10643572014-06-10 15:17:39 -070036import org.junit.experimental.categories.Category;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080037import org.openflow.protocol.OFBarrierRequest;
38import org.openflow.protocol.OFFlowMod;
39import org.openflow.protocol.OFMatch;
40import org.openflow.protocol.OFMessage;
41import org.openflow.protocol.OFType;
42import org.openflow.protocol.action.OFAction;
43import org.openflow.protocol.factory.BasicFactory;
44
Ray Milkey10643572014-06-10 15:17:39 -070045@Category(IntegrationTest.class)
Naoki Shiota75b7dd62013-12-03 18:09:21 -080046public class FlowPusherTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070047 private FlowPusher pusher;
48 private FloodlightContext context;
49 private FloodlightModuleContext modContext;
50 private BasicFactory factory;
51 private OFMessageDamper damper;
52 private IFloodlightProviderService flProviderService;
53 private IThreadPoolService threadPoolService;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 /**
56 * Test single OFMessage is correctly sent to single switch via MessageDamper.
57 */
58 @Test
59 public void testAddMessage() {
60 beginInitMock();
Naoki Shiota75b7dd62013-12-03 18:09:21 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 OFMessage msg = EasyMock.createMock(OFMessage.class);
63 EasyMock.expect(msg.getXid()).andReturn(1).anyTimes();
64 EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes();
65 EasyMock.replay(msg);
Naoki Shiota75b7dd62013-12-03 18:09:21 -080066
Naoki Shiota47993102014-07-09 14:00:54 -070067 IOFSwitch sw = createConnectedSwitchMock(1, false);
Ray Milkey269ffb92014-04-03 14:43:30 -070068 EasyMock.replay(sw);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -070069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 try {
71 EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context)))
72 .andReturn(true).once();
73 } catch (IOException e1) {
74 fail("Failed in OFMessageDamper#write()");
75 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -080076
Ray Milkey269ffb92014-04-03 14:43:30 -070077 endInitMock();
78 initPusher(1);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -070079
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070080 boolean addResult = pusher.add(sw, msg);
81 assertTrue(addResult);
Naoki Shiota75b7dd62013-12-03 18:09:21 -080082
Ray Milkey269ffb92014-04-03 14:43:30 -070083 try {
84 // wait until message is processed.
85 Thread.sleep(1000);
86 } catch (InterruptedException e) {
87 fail("Failed in Thread.sleep()");
88 }
89 EasyMock.verify(msg);
90 EasyMock.verify(sw);
91 verifyAll();
Naoki Shiota75b7dd62013-12-03 18:09:21 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 pusher.stop();
94 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 /**
97 * Test bunch of OFMessages are correctly sent to single switch via MessageDamper.
98 */
99 @Test
100 public void testMassiveAddMessage() {
Yuta HIGUCHI7c39c842014-06-09 16:33:03 -0700101 // Some number larger than FlowPusher.MAX_MESSAGE_SEND
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700102 final int numMsg = FlowPusher.MAX_MESSAGE_SEND * 2;
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700103
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 beginInitMock();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800105
Naoki Shiota47993102014-07-09 14:00:54 -0700106 IOFSwitch sw = createConnectedSwitchMock(1, false);
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 EasyMock.replay(sw);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 List<OFMessage> messages = new ArrayList<OFMessage>();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800110
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700111 for (int i = 0; i < numMsg; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 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);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 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 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700125
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 endInitMock();
127 initPusher(1);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800128
Ray Milkey269ffb92014-04-03 14:43:30 -0700129 for (OFMessage msg : messages) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700130 boolean addResult = pusher.add(sw, msg);
131 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 try {
135 // wait until message is processed.
Yuta HIGUCHI7c39c842014-06-09 16:33:03 -0700136 Thread.sleep(1000);
Ray Milkey269ffb92014-04-03 14:43:30 -0700137 } catch (InterruptedException e) {
138 fail("Failed in Thread.sleep()");
139 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700140
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 for (OFMessage msg : messages) {
142 EasyMock.verify(msg);
143 }
144 EasyMock.verify(sw);
145 verifyAll();
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() {
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700155 final int numSwitch = 10;
156 final int numMsg = 100; // messages per thread
Ray Milkey269ffb92014-04-03 14:43:30 -0700157
158 beginInitMock();
159
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700160 Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700161 for (int i = 0; i < numSwitch; ++i) {
Naoki Shiota47993102014-07-09 14:00:54 -0700162 IOFSwitch sw = createConnectedSwitchMock(i, false);
Ray Milkey269ffb92014-04-03 14:43:30 -0700163 EasyMock.replay(sw);
164
165 List<OFMessage> messages = new ArrayList<OFMessage>();
166
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700167 for (int j = 0; j < numMsg; ++j) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700168 OFMessage msg = EasyMock.createMock(OFMessage.class);
169 EasyMock.expect(msg.getXid()).andReturn(j).anyTimes();
170 EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes();
171 EasyMock.replay(msg);
172 messages.add(msg);
173
174 try {
175 EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context)))
176 .andReturn(true).once();
177 } catch (IOException e1) {
178 fail("Failed in OFMessageDamper#write()");
179 }
180 }
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700181 swMap.put(sw, messages);
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 }
183
184 endInitMock();
185 initPusher(1);
186
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700187 for (IOFSwitch sw : swMap.keySet()) {
188 for (OFMessage msg : swMap.get(sw)) {
189 boolean addResult = pusher.add(sw, msg);
190 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700191 }
192 }
193
194 try {
195 // wait until message is processed.
196 Thread.sleep(1000);
197 } catch (InterruptedException e) {
198 fail("Failed in Thread.sleep()");
199 }
200
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700201 for (IOFSwitch sw : swMap.keySet()) {
202 for (OFMessage msg : swMap.get(sw)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700203 EasyMock.verify(msg);
204 }
205
206 EasyMock.verify(sw);
207 }
208 verifyAll();
209
210 pusher.stop();
211 }
212
213 /**
214 * Test bunch of OFMessages are correctly sent to multiple switches using multiple threads.
215 */
216 @Test
217 public void testMultiThreadedAddMessage() {
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700218 final int numThreads = 10;
219 final int numMsg = 100; // messages per thread
Ray Milkey269ffb92014-04-03 14:43:30 -0700220
221 beginInitMock();
222
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700223 Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700224 for (int i = 0; i < numThreads; ++i) {
Naoki Shiota47993102014-07-09 14:00:54 -0700225 IOFSwitch sw = createConnectedSwitchMock(i, false);
Ray Milkey269ffb92014-04-03 14:43:30 -0700226 EasyMock.replay(sw);
227
228 List<OFMessage> messages = new ArrayList<OFMessage>();
229
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700230 for (int j = 0; j < numMsg; ++j) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700231 OFMessage msg = EasyMock.createMock(OFMessage.class);
232 EasyMock.expect(msg.getXid()).andReturn(j).anyTimes();
233 EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes();
234 EasyMock.replay(msg);
235 messages.add(msg);
236
237 try {
238 EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context)))
239 .andReturn(true).once();
240 } catch (IOException e1) {
241 fail("Failed in OFMessageDamper#write()");
242 }
243 }
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700244 swMap.put(sw, messages);
Ray Milkey269ffb92014-04-03 14:43:30 -0700245 }
246
247 endInitMock();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700248 initPusher(numThreads);
Ray Milkey269ffb92014-04-03 14:43:30 -0700249
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700250 for (IOFSwitch sw : swMap.keySet()) {
251 for (OFMessage msg : swMap.get(sw)) {
252 boolean addResult = pusher.add(sw, msg);
253 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700254 }
255 }
256
257 try {
258 // wait until message is processed.
259 Thread.sleep(1000);
260 } catch (InterruptedException e) {
261 fail("Failed in Thread.sleep()");
262 }
263
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700264 for (IOFSwitch sw : swMap.keySet()) {
265 for (OFMessage msg : swMap.get(sw)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700266 EasyMock.verify(msg);
267 }
268
269 EasyMock.verify(sw);
270 }
271 verifyAll();
272
273 pusher.stop();
274 }
275
276 private long barrierTime = 0;
277
278 /**
279 * Test rate limitation of messages works correctly.
280 */
281 @Test
282 public void testRateLimitedAddMessage() {
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700283 final long limitRate = 100; // [bytes/ms]
284 final int numMsg = 1000;
Ray Milkey269ffb92014-04-03 14:43:30 -0700285
286 // Accuracy of FlowPusher's rate calculation can't be measured by unit test
287 // because switch doesn't return BARRIER_REPLY.
288 // In unit test we use approximate way to measure rate. This value is
289 // acceptable margin of measured rate.
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700290 final double acceptableRate = limitRate * 1.2;
Ray Milkey269ffb92014-04-03 14:43:30 -0700291
292 beginInitMock();
293
Naoki Shiota47993102014-07-09 14:00:54 -0700294 IOFSwitch sw = createConnectedSwitchMock(1, true);
Ray Milkey269ffb92014-04-03 14:43:30 -0700295 EasyMock.replay(sw);
296
297 List<OFMessage> messages = new ArrayList<OFMessage>();
298
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700299 for (int i = 0; i < numMsg; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700300 OFMessage msg = EasyMock.createMock(OFMessage.class);
301 EasyMock.expect(msg.getXid()).andReturn(1).anyTimes();
302 EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes();
303 EasyMock.expect(msg.getLengthU()).andReturn(100).anyTimes();
304 EasyMock.replay(msg);
305 messages.add(msg);
306
307 try {
308 EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context)))
309 .andReturn(true).once();
310 } catch (IOException e) {
311 fail("Failed in OFMessageDamper#write()");
312 }
313 }
314
315 try {
316 EasyMock.expect(damper.write(EasyMock.eq(sw), (OFMessage) EasyMock.anyObject(), EasyMock.eq(context)))
317 .andAnswer(new IAnswer<Boolean>() {
318 @Override
319 public Boolean answer() throws Throwable {
320 OFMessage msg = (OFMessage) EasyMock.getCurrentArguments()[1];
321 if (msg.getType() == OFType.BARRIER_REQUEST) {
322 barrierTime = System.currentTimeMillis();
323 }
324 return true;
325 }
326 }).once();
327 } catch (IOException e1) {
328 fail("Failed in OFMessageDamper#write()");
329 }
330
331 endInitMock();
332 initPusher(1);
333
334 pusher.createQueue(sw);
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700335 pusher.setRate(sw, limitRate);
Ray Milkey269ffb92014-04-03 14:43:30 -0700336
337 long beginTime = System.currentTimeMillis();
338 for (OFMessage msg : messages) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700339 boolean addResult = pusher.add(sw, msg);
340 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700341 }
342
343 pusher.barrierAsync(sw);
344
345 try {
346 do {
347 Thread.sleep(1000);
348 } while (barrierTime == 0);
349 } catch (InterruptedException e) {
350 fail("Failed to sleep");
351 }
352
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700353 double measuredRate = numMsg * 100 / (barrierTime - beginTime);
354 assertTrue(measuredRate < acceptableRate);
Ray Milkey269ffb92014-04-03 14:43:30 -0700355
356 for (OFMessage msg : messages) {
357 EasyMock.verify(msg);
358 }
359 EasyMock.verify(sw);
360 verifyAll();
361
362 pusher.stop();
363 }
364
365 /**
366 * Test barrier message is correctly sent to a switch.
367 */
368 @Test
369 public void testBarrierMessage() {
370 beginInitMock();
371
Naoki Shiota47993102014-07-09 14:00:54 -0700372 IOFSwitch sw = createConnectedSwitchMock(1, true);
Ray Milkey269ffb92014-04-03 14:43:30 -0700373 EasyMock.replay(sw);
374
375 try {
376 EasyMock.expect(damper.write(EasyMock.eq(sw), (OFMessage) EasyMock.anyObject(), EasyMock.eq(context)))
377 .andReturn(true).once();
378 } catch (IOException e1) {
379 fail("Failed in OFMessageDamper#write()");
380 }
381
382 endInitMock();
383 initPusher(1);
384
385 OFBarrierReplyFuture future = pusher.barrierAsync(sw);
386
387 assertNotNull(future);
388
389 try {
390 Thread.sleep(1000);
391 } catch (InterruptedException e) {
392 fail("Failed to sleep");
393 }
394
395 verifyAll();
396
397 pusher.stop();
398 }
399
400 static final int XID_TO_VERIFY = 100;
401 static final long DPID_TO_VERIFY = 10;
402
403 /**
404 * Test FlowObject is correctly converted to message and is sent to a switch.
405 */
406 @SuppressWarnings("unchecked")
407 @Test
408 public void testAddFlow() {
409 // instantiate required objects
410 FlowEntry flowEntry1 = new FlowEntry();
411 flowEntry1.setDpid(new Dpid(DPID_TO_VERIFY));
412 flowEntry1.setFlowId(new FlowId(1));
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700413 flowEntry1.setInPort(new PortNumber((short) 1));
414 flowEntry1.setOutPort(new PortNumber((short) 11));
Ray Milkey269ffb92014-04-03 14:43:30 -0700415 flowEntry1.setFlowEntryId(new FlowEntryId(1));
416 flowEntry1.setFlowEntryMatch(new FlowEntryMatch());
417 flowEntry1.setFlowEntryActions(new FlowEntryActions());
418 flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState());
419 flowEntry1.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
420
421 beginInitMock();
422
423 OFFlowMod msg = EasyMock.createMock(OFFlowMod.class);
424 EasyMock.expect(msg.setIdleTimeout(EasyMock.anyShort())).andReturn(msg);
425 EasyMock.expect(msg.setHardTimeout(EasyMock.anyShort())).andReturn(msg);
426 EasyMock.expect(msg.setPriority(EasyMock.anyShort())).andReturn(msg);
427 EasyMock.expect(msg.setBufferId(EasyMock.anyInt())).andReturn(msg);
428 EasyMock.expect(msg.setCookie(EasyMock.anyLong())).andReturn(msg);
429 EasyMock.expect(msg.setCommand(EasyMock.anyShort())).andReturn(msg);
430 EasyMock.expect(msg.setMatch(EasyMock.anyObject(OFMatch.class))).andReturn(msg);
431 EasyMock.expect(msg.setActions((List<OFAction>) EasyMock.anyObject())).andReturn(msg);
432 EasyMock.expect(msg.setLengthU(EasyMock.anyShort())).andReturn(msg);
433 EasyMock.expect(msg.setOutPort(EasyMock.anyShort())).andReturn(msg).atLeastOnce();
434 EasyMock.expect(msg.getXid()).andReturn(XID_TO_VERIFY).anyTimes();
435 EasyMock.expect(msg.getType()).andReturn(OFType.FLOW_MOD).anyTimes();
436 EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes();
437 EasyMock.replay(msg);
438
439 EasyMock.expect(factory.getMessage(EasyMock.eq(OFType.FLOW_MOD))).andReturn(msg);
440
Naoki Shiota47993102014-07-09 14:00:54 -0700441 IOFSwitch sw = createConnectedSwitchMock(DPID_TO_VERIFY, false);
Ray Milkey269ffb92014-04-03 14:43:30 -0700442 EasyMock.expect(sw.getStringId()).andReturn("1").anyTimes();
Ray Milkey269ffb92014-04-03 14:43:30 -0700443
444 try {
445 EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.anyObject(OFMessage.class), EasyMock.eq(context)))
446 .andAnswer(new IAnswer<Boolean>() {
447 @Override
448 public Boolean answer() throws Throwable {
449 OFMessage msg = (OFMessage) EasyMock.getCurrentArguments()[1];
450 if (msg.getType() == OFType.FLOW_MOD) {
451 assertEquals(msg.getXid(), XID_TO_VERIFY);
452 }
453 return true;
454 }
455 }).atLeastOnce();
456 } catch (IOException e1) {
457 fail("Failed in OFMessageDamper#write()");
458 }
459
460 EasyMock.replay(sw);
461
462 endInitMock();
463 initPusher(1);
464
465 pusher.pushFlowEntry(sw, flowEntry1);
466
467 try {
468 Thread.sleep(1000);
469 } catch (InterruptedException e) {
470 fail("Failed to sleep");
471 }
472
473 EasyMock.verify(sw);
474 verifyAll();
475
476 pusher.stop();
477 }
478
479 private void beginInitMock() {
480 context = EasyMock.createMock(FloodlightContext.class);
481 modContext = EasyMock.createMock(FloodlightModuleContext.class);
482 factory = EasyMock.createMock(BasicFactory.class);
483 damper = EasyMock.createMock(OFMessageDamper.class);
484 flProviderService = EasyMock.createMock(IFloodlightProviderService.class);
485 threadPoolService = EasyMock.createMock(IThreadPoolService.class);
486
487 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IThreadPoolService.class)))
488 .andReturn(threadPoolService).once();
489 EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IFloodlightProviderService.class)))
490 .andReturn(flProviderService).once();
491 flProviderService.addOFMessageListener(EasyMock.eq(OFType.BARRIER_REPLY),
492 (FlowPusher) EasyMock.anyObject());
493 EasyMock.expectLastCall().once();
494
495 ScheduledExecutorService executor = EasyMock.createMock(ScheduledExecutorService.class);
496 EasyMock.expect(executor.schedule((Runnable) EasyMock.anyObject(), EasyMock.anyLong(),
497 (TimeUnit) EasyMock.anyObject())).andReturn(null).once();
498 EasyMock.replay(executor);
499 EasyMock.expect(threadPoolService.getScheduledExecutor()).andReturn(executor).anyTimes();
500 }
501
502 private void endInitMock() {
503 EasyMock.replay(threadPoolService);
504 EasyMock.replay(flProviderService);
505 EasyMock.replay(damper);
506 EasyMock.replay(factory);
507 EasyMock.replay(modContext);
508 EasyMock.replay(context);
509 }
510
511 private void verifyAll() {
512 EasyMock.verify(threadPoolService);
513 EasyMock.verify(flProviderService);
514 EasyMock.verify(damper);
515 EasyMock.verify(factory);
516 EasyMock.verify(modContext);
517 EasyMock.verify(context);
518 }
519
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700520 private void initPusher(int numThread) {
521 pusher = new FlowPusher(numThread);
Ray Milkey269ffb92014-04-03 14:43:30 -0700522 pusher.init(context, modContext, factory, damper);
523 pusher.start();
524 }
525
Naoki Shiota47993102014-07-09 14:00:54 -0700526 private IOFSwitch createConnectedSwitchMock(long dpid, boolean useBarrier) {
527 IOFSwitch sw = EasyMock.createMock(IOFSwitch.class);
528 EasyMock.expect(sw.isConnected()).andReturn(true).anyTimes();
529 EasyMock.expect(sw.getId()).andReturn(dpid).anyTimes();
530 sw.flush();
531 EasyMock.expectLastCall().atLeastOnce();
532 if (useBarrier) {
533 prepareBarrier(sw);
534 }
535
536 return sw;
537 }
538
Ray Milkey269ffb92014-04-03 14:43:30 -0700539 private void prepareBarrier(IOFSwitch sw) {
540 OFBarrierRequest req = EasyMock.createMock(OFBarrierRequest.class);
541 req.setXid(EasyMock.anyInt());
542 EasyMock.expectLastCall().once();
543 EasyMock.expect(req.getXid()).andReturn(1).anyTimes();
544 EasyMock.expect(req.getType()).andReturn(OFType.BARRIER_REQUEST).anyTimes();
545 EasyMock.expect(req.getLength()).andReturn((short) 100).anyTimes();
546 EasyMock.replay(req);
547 EasyMock.expect(factory.getMessage(EasyMock.eq(OFType.BARRIER_REQUEST))).andReturn(req).anyTimes();
548 EasyMock.expect(sw.getNextTransactionId()).andReturn(1);
549 }
550
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800551}