blob: f84c08f9ad252ada3c7b0cfbaefaeafde966644e [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.flowprogrammer;
Naoki Shiota75b7dd62013-12-03 18:09:21 -08002
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07003
Sho SHIMIZU107344e2014-08-13 16:11:53 -07004import static org.easymock.EasyMock.anyInt;
5import static org.easymock.EasyMock.anyLong;
6import static org.easymock.EasyMock.anyObject;
7import static org.easymock.EasyMock.anyShort;
8import static org.easymock.EasyMock.createMock;
9import static org.easymock.EasyMock.eq;
10import static org.easymock.EasyMock.expect;
11import static org.easymock.EasyMock.expectLastCall;
12import static org.easymock.EasyMock.replay;
13import static org.easymock.EasyMock.verify;
Naoki Shiota47993102014-07-09 14:00:54 -070014import static org.junit.Assert.assertNotNull;
15import static org.junit.Assert.assertTrue;
16import static org.junit.Assert.fail;
17
18import java.io.IOException;
19import java.util.ArrayList;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23import java.util.concurrent.ScheduledExecutorService;
24import java.util.concurrent.TimeUnit;
25
Naoki Shiota75b7dd62013-12-03 18:09:21 -080026import net.floodlightcontroller.core.FloodlightContext;
27import net.floodlightcontroller.core.IFloodlightProviderService;
28import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070029import net.floodlightcontroller.core.internal.OFMessageFuture;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080030import net.floodlightcontroller.core.module.FloodlightModuleContext;
31import net.floodlightcontroller.threadpool.IThreadPoolService;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070032import net.onrc.onos.core.intent.FlowEntry;
33import net.onrc.onos.core.intent.IntentOperation.Operator;
Ray Milkey10643572014-06-10 15:17:39 -070034import net.onrc.onos.core.util.IntegrationTest;
Naoki Shiota47993102014-07-09 14:00:54 -070035
Naoki Shiota75b7dd62013-12-03 18:09:21 -080036import org.junit.Test;
Ray Milkey10643572014-06-10 15:17:39 -070037import org.junit.experimental.categories.Category;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070038import org.projectfloodlight.openflow.protocol.OFBarrierReply;
39import org.projectfloodlight.openflow.protocol.OFBarrierRequest;
40import org.projectfloodlight.openflow.protocol.OFFactories;
41import org.projectfloodlight.openflow.protocol.OFFactory;
42import org.projectfloodlight.openflow.protocol.OFFlowModify;
43import org.projectfloodlight.openflow.protocol.OFMessage;
44import org.projectfloodlight.openflow.protocol.OFType;
45import org.projectfloodlight.openflow.protocol.OFVersion;
46import org.projectfloodlight.openflow.protocol.action.OFAction;
47import org.projectfloodlight.openflow.protocol.match.Match;
48import org.projectfloodlight.openflow.types.OFBufferId;
49import org.projectfloodlight.openflow.types.OFPort;
50import org.projectfloodlight.openflow.types.U64;
Jonathan Hartce07f322014-08-13 14:40:53 -070051import org.projectfloodlight.openflow.util.HexString;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070052
Naoki Shiota75b7dd62013-12-03 18:09:21 -080053
Ray Milkey10643572014-06-10 15:17:39 -070054@Category(IntegrationTest.class)
Naoki Shiota75b7dd62013-12-03 18:09:21 -080055public class FlowPusherTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 private FlowPusher pusher;
57 private FloodlightContext context;
58 private FloodlightModuleContext modContext;
Ray Milkey269ffb92014-04-03 14:43:30 -070059 private IFloodlightProviderService flProviderService;
60 private IThreadPoolService threadPoolService;
Naoki Shiota75b7dd62013-12-03 18:09:21 -080061
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070062 private OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10);
63
Ray Milkey269ffb92014-04-03 14:43:30 -070064 /**
Jonathan Hart51bb27c2014-08-11 16:00:31 -070065 * Test single OFMessage is correctly sent to single switch.
Ray Milkey269ffb92014-04-03 14:43:30 -070066 */
67 @Test
68 public void testAddMessage() {
69 beginInitMock();
Naoki Shiota75b7dd62013-12-03 18:09:21 -080070
Sho SHIMIZU107344e2014-08-13 16:11:53 -070071 OFMessage msg = createMock(OFMessage.class);
72 expect(msg.getXid()).andReturn((long) 1).anyTimes();
Sho SHIMIZU107344e2014-08-13 16:11:53 -070073 replay(msg);
Naoki Shiota75b7dd62013-12-03 18:09:21 -080074
Jonathan Hartce07f322014-08-13 14:40:53 -070075 IOFSwitch sw = createConnectedSwitchMock(1);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -070076
Ray Milkey269ffb92014-04-03 14:43:30 -070077 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -070078 sw.write(eq(msg), eq((FloodlightContext) null));
79 expectLastCall().once();
80 replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -070081 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070082 fail("Failed in IOFSwitch#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -070083 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 endInitMock();
86 initPusher(1);
Naoki Shiotad6ef3b32014-03-13 18:42:23 -070087
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070088 boolean addResult = pusher.add(sw, msg);
89 assertTrue(addResult);
Naoki Shiota75b7dd62013-12-03 18:09:21 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 try {
92 // wait until message is processed.
93 Thread.sleep(1000);
94 } catch (InterruptedException e) {
95 fail("Failed in Thread.sleep()");
96 }
Sho SHIMIZU107344e2014-08-13 16:11:53 -070097 verify(msg);
98 verify(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -070099 verifyAll();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 pusher.stop();
102 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800103
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 /**
Jonathan Hart51bb27c2014-08-11 16:00:31 -0700105 * Test bunch of OFMessages are correctly sent to single switch.
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 */
107 @Test
108 public void testMassiveAddMessage() {
Yuta HIGUCHI7c39c842014-06-09 16:33:03 -0700109 // Some number larger than FlowPusher.MAX_MESSAGE_SEND
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700110 final int numMsg = FlowPusher.MAX_MESSAGE_SEND * 2;
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700111
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 beginInitMock();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800113
Jonathan Hartce07f322014-08-13 14:40:53 -0700114 IOFSwitch sw = createConnectedSwitchMock(1);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700115
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 List<OFMessage> messages = new ArrayList<OFMessage>();
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800118
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700119 for (int i = 0; i < numMsg; ++i) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700120 OFMessage msg = createMock(OFMessage.class);
121 expect(msg.getXid()).andReturn((long) i).anyTimes();
122 replay(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 messages.add(msg);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800124
Ray Milkey269ffb92014-04-03 14:43:30 -0700125 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700126 sw.write(eq(msg), eq((FloodlightContext) null));
127 expectLastCall().once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700129 fail("Failed in IOFSwitch#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 }
131 }
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700132 replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700133 endInitMock();
134 initPusher(1);
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800135
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 for (OFMessage msg : messages) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700137 boolean addResult = pusher.add(sw, msg);
138 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 }
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800140
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 try {
142 // wait until message is processed.
Yuta HIGUCHI7c39c842014-06-09 16:33:03 -0700143 Thread.sleep(1000);
Ray Milkey269ffb92014-04-03 14:43:30 -0700144 } catch (InterruptedException e) {
145 fail("Failed in Thread.sleep()");
146 }
Naoki Shiotad6ef3b32014-03-13 18:42:23 -0700147
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 for (OFMessage msg : messages) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700149 verify(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 }
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700151 verify(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 verifyAll();
153
154 pusher.stop();
155 }
156
157 /**
158 * Test bunch of OFMessages are correctly sent to multiple switches with single threads.
159 */
160 @Test
161 public void testMultiSwitchAddMessage() {
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700162 final int numSwitch = 10;
163 final int numMsg = 100; // messages per thread
Ray Milkey269ffb92014-04-03 14:43:30 -0700164
165 beginInitMock();
166
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700167 Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700168 for (int i = 0; i < numSwitch; ++i) {
Jonathan Hartce07f322014-08-13 14:40:53 -0700169 IOFSwitch sw = createConnectedSwitchMock(i);
Ray Milkey269ffb92014-04-03 14:43:30 -0700170
171 List<OFMessage> messages = new ArrayList<OFMessage>();
172
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700173 for (int j = 0; j < numMsg; ++j) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700174 OFMessage msg = createMock(OFMessage.class);
175 expect(msg.getXid()).andReturn((long) j).anyTimes();
176 replay(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700177 messages.add(msg);
178
179 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700180 sw.write(eq(msg), eq((FloodlightContext) null));
181 expectLastCall().once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700182 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700183 fail("Failed in IOFWrite#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700184 }
185 }
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700186 swMap.put(sw, messages);
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700187 replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700188 }
189
190 endInitMock();
191 initPusher(1);
192
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700193 for (IOFSwitch sw : swMap.keySet()) {
194 for (OFMessage msg : swMap.get(sw)) {
195 boolean addResult = pusher.add(sw, msg);
196 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700197 }
198 }
199
200 try {
201 // wait until message is processed.
202 Thread.sleep(1000);
203 } catch (InterruptedException e) {
204 fail("Failed in Thread.sleep()");
205 }
206
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700207 for (IOFSwitch sw : swMap.keySet()) {
208 for (OFMessage msg : swMap.get(sw)) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700209 verify(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700210 }
211
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700212 verify(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700213 }
214 verifyAll();
215
216 pusher.stop();
217 }
218
219 /**
220 * Test bunch of OFMessages are correctly sent to multiple switches using multiple threads.
221 */
222 @Test
223 public void testMultiThreadedAddMessage() {
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700224 final int numThreads = 10;
225 final int numMsg = 100; // messages per thread
Ray Milkey269ffb92014-04-03 14:43:30 -0700226
227 beginInitMock();
228
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700229 Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700230 for (int i = 0; i < numThreads; ++i) {
Jonathan Hartce07f322014-08-13 14:40:53 -0700231 IOFSwitch sw = createConnectedSwitchMock(i);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700232 //EasyMock.replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700233
234 List<OFMessage> messages = new ArrayList<OFMessage>();
235
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700236 for (int j = 0; j < numMsg; ++j) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700237 OFMessage msg = createMock(OFMessage.class);
238 expect(msg.getXid()).andReturn((long) j).anyTimes();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700239
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700240 replay(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700241 messages.add(msg);
242
243 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700244 sw.write(eq(msg), eq((FloodlightContext) null));
245 expectLastCall().once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700246 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700247 fail("Failed in IOFWrite#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700248 }
249 }
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700250 swMap.put(sw, messages);
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700251 replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700252 }
253
254 endInitMock();
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700255 initPusher(numThreads);
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700256 for (IOFSwitch sw : swMap.keySet()) {
257 for (OFMessage msg : swMap.get(sw)) {
258 boolean addResult = pusher.add(sw, msg);
259 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700260 }
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
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700270 for (IOFSwitch sw : swMap.keySet()) {
271 for (OFMessage msg : swMap.get(sw)) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700272 verify(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700273 }
274
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700275 verify(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700276 }
277 verifyAll();
278
279 pusher.stop();
280 }
281
282 private long barrierTime = 0;
283
284 /**
285 * Test rate limitation of messages works correctly.
286 */
287 @Test
288 public void testRateLimitedAddMessage() {
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700289 final long limitRate = 100; // [bytes/ms]
290 final int numMsg = 1000;
Ray Milkey269ffb92014-04-03 14:43:30 -0700291
292 // Accuracy of FlowPusher's rate calculation can't be measured by unit test
293 // because switch doesn't return BARRIER_REPLY.
294 // In unit test we use approximate way to measure rate. This value is
295 // acceptable margin of measured rate.
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700296 final double acceptableRate = limitRate * 1.2;
Ray Milkey269ffb92014-04-03 14:43:30 -0700297
298 beginInitMock();
299
Jonathan Hartce07f322014-08-13 14:40:53 -0700300 IOFSwitch sw = createConnectedSwitchMock(1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700301
302 List<OFMessage> messages = new ArrayList<OFMessage>();
303
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700304 for (int i = 0; i < numMsg; ++i) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700305 OFMessage msg = createMock(OFMessage.class);
306 expect(msg.getXid()).andReturn((long) 1).anyTimes();
307 replay(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700308 messages.add(msg);
309
310 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700311 sw.write(eq(msg), eq((FloodlightContext) null));
312 expectLastCall().once();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700313 } catch (IOException e1) {
314 fail("Failed in IOFWrite#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700315 }
316 }
317
318 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700319 sw.write(anyObject(OFBarrierRequest.class), eq((FloodlightContext) null));
320 expectLastCall().once();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700321 barrierTime = System.currentTimeMillis();
Ray Milkey269ffb92014-04-03 14:43:30 -0700322 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700323 fail("Failed in IOFWrite#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700324 }
325
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700326 replay(sw);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700327
Ray Milkey269ffb92014-04-03 14:43:30 -0700328 endInitMock();
329 initPusher(1);
330
331 pusher.createQueue(sw);
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700332 pusher.setRate(sw, limitRate);
Ray Milkey269ffb92014-04-03 14:43:30 -0700333
334 long beginTime = System.currentTimeMillis();
335 for (OFMessage msg : messages) {
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700336 boolean addResult = pusher.add(sw, msg);
337 assertTrue(addResult);
Ray Milkey269ffb92014-04-03 14:43:30 -0700338 }
339
340 pusher.barrierAsync(sw);
341
342 try {
343 do {
344 Thread.sleep(1000);
345 } while (barrierTime == 0);
346 } catch (InterruptedException e) {
347 fail("Failed to sleep");
348 }
349
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700350 double measuredRate = numMsg * 100 / (barrierTime - beginTime);
351 assertTrue(measuredRate < acceptableRate);
Ray Milkey269ffb92014-04-03 14:43:30 -0700352
353 for (OFMessage msg : messages) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700354 verify(msg);
Ray Milkey269ffb92014-04-03 14:43:30 -0700355 }
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700356 verify(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700357 verifyAll();
358
359 pusher.stop();
360 }
361
362 /**
363 * Test barrier message is correctly sent to a switch.
364 */
365 @Test
366 public void testBarrierMessage() {
367 beginInitMock();
368
Jonathan Hartce07f322014-08-13 14:40:53 -0700369 IOFSwitch sw = createConnectedSwitchMock(1);
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700370 expect(sw.getOFVersion()).andReturn(OFVersion.OF_10).once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700371
372 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700373 sw.write((OFMessage) anyObject(), eq((FloodlightContext) null));
374 expectLastCall().once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700375 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700376 fail("Failed in IOFWrite#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700377 }
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700378 replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700379 endInitMock();
380 initPusher(1);
381
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700382 OFMessageFuture<OFBarrierReply> future = pusher.barrierAsync(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700383
384 assertNotNull(future);
385
386 try {
387 Thread.sleep(1000);
388 } catch (InterruptedException e) {
389 fail("Failed to sleep");
390 }
391
392 verifyAll();
393
394 pusher.stop();
395 }
396
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700397 static final long XID_TO_VERIFY = 100;
Ray Milkey269ffb92014-04-03 14:43:30 -0700398 static final long DPID_TO_VERIFY = 10;
399
400 /**
401 * Test FlowObject is correctly converted to message and is sent to a switch.
402 */
403 @SuppressWarnings("unchecked")
404 @Test
405 public void testAddFlow() {
406 // instantiate required objects
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700407 FlowEntry flowEntry1 = new FlowEntry(DPID_TO_VERIFY, 1, 11, null, null, 0, 0, Operator.ADD);
408 /*
Ray Milkey269ffb92014-04-03 14:43:30 -0700409 flowEntry1.setDpid(new Dpid(DPID_TO_VERIFY));
410 flowEntry1.setFlowId(new FlowId(1));
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700411 flowEntry1.setInPort(new PortNumber((short) 1));
412 flowEntry1.setOutPort(new PortNumber((short) 11));
Ray Milkey269ffb92014-04-03 14:43:30 -0700413 flowEntry1.setFlowEntryId(new FlowEntryId(1));
414 flowEntry1.setFlowEntryMatch(new FlowEntryMatch());
415 flowEntry1.setFlowEntryActions(new FlowEntryActions());
416 flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState());
417 flowEntry1.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700418 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700419
420 beginInitMock();
421
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700422 OFFlowModify fm = createMock(OFFlowModify.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700423
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700424 OFFlowModify.Builder bld = createMock(OFFlowModify.Builder.class);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700425
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700426 expect(bld.setIdleTimeout(anyInt())).andReturn(bld);
427 expect(bld.setHardTimeout(anyInt())).andReturn(bld);
428 expect(bld.setPriority(anyShort())).andReturn(bld);
429 expect(bld.setBufferId(OFBufferId.NO_BUFFER)).andReturn(bld);
430 expect(bld.setCookie(U64.of(anyLong()))).andReturn(bld);
431 expect(bld.setMatch(anyObject(Match.class))).andReturn(bld);
432 expect(bld.setActions((List<OFAction>) anyObject())).andReturn(bld);
433 expect(bld.setOutPort(OFPort.of(anyInt()))).andReturn(bld).atLeastOnce();
434 expect(bld.build()).andReturn(fm);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700435
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700436 expect(fm.getXid()).andReturn(XID_TO_VERIFY).anyTimes();
437 expect(fm.getType()).andReturn(OFType.FLOW_MOD).anyTimes();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700438
Jonathan Hartce07f322014-08-13 14:40:53 -0700439 IOFSwitch sw = createConnectedSwitchMock(DPID_TO_VERIFY);
Ray Milkey269ffb92014-04-03 14:43:30 -0700440
441 try {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700442 sw.write(anyObject(OFMessage.class), eq((FloodlightContext) null));
443 expectLastCall().once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700444 } catch (IOException e1) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700445 fail("Failed in IOFWrite#write()");
Ray Milkey269ffb92014-04-03 14:43:30 -0700446 }
447
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700448 replay(bld, fm);
449 replay(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700450
451 endInitMock();
452 initPusher(1);
453
454 pusher.pushFlowEntry(sw, flowEntry1);
455
456 try {
457 Thread.sleep(1000);
458 } catch (InterruptedException e) {
459 fail("Failed to sleep");
460 }
461
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700462 verify(sw);
Ray Milkey269ffb92014-04-03 14:43:30 -0700463 verifyAll();
464
465 pusher.stop();
466 }
467
468 private void beginInitMock() {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700469 context = createMock(FloodlightContext.class);
470 modContext = createMock(FloodlightModuleContext.class);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700471 // AAS: I don't think we should mock factories... the rabbit whole is too deep.
472 //factory10 = EasyMock.createMock(OFFactories.getFactory(OFVersion.OF_10).getClass());
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700473 flProviderService = createMock(IFloodlightProviderService.class);
474 threadPoolService = createMock(IThreadPoolService.class);
Ray Milkey269ffb92014-04-03 14:43:30 -0700475
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700476 expect(modContext.getServiceImpl(eq(IThreadPoolService.class)))
Ray Milkey269ffb92014-04-03 14:43:30 -0700477 .andReturn(threadPoolService).once();
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700478 expect(modContext.getServiceImpl(eq(IFloodlightProviderService.class)))
Ray Milkey269ffb92014-04-03 14:43:30 -0700479 .andReturn(flProviderService).once();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700480 // AAS: FlowPusher doesn't call the following anymore.
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700481 flProviderService.addOFMessageListener(eq(OFType.BARRIER_REPLY),
482 anyObject(FlowPusher.class));
483 expectLastCall().once();
Ray Milkey269ffb92014-04-03 14:43:30 -0700484
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700485 ScheduledExecutorService executor = createMock(ScheduledExecutorService.class);
486 expect(executor.schedule((Runnable) anyObject(), anyLong(),
487 (TimeUnit) anyObject())).andReturn(null).once();
488 replay(executor);
489 expect(threadPoolService.getScheduledExecutor()).andReturn(executor).anyTimes();
Ray Milkey269ffb92014-04-03 14:43:30 -0700490 }
491
492 private void endInitMock() {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700493 replay(threadPoolService);
494 replay(flProviderService);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700495 //EasyMock.replay(factory10);
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700496 replay(modContext);
497 replay(context);
Ray Milkey269ffb92014-04-03 14:43:30 -0700498 }
499
500 private void verifyAll() {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700501 verify(threadPoolService);
502 verify(flProviderService);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700503 //EasyMock.verify(factory10);
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700504 verify(modContext);
505 verify(context);
Ray Milkey269ffb92014-04-03 14:43:30 -0700506 }
507
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -0700508 private void initPusher(int numThread) {
509 pusher = new FlowPusher(numThread);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700510 pusher.init(modContext);
Ray Milkey269ffb92014-04-03 14:43:30 -0700511 pusher.start();
512 }
513
Jonathan Hartce07f322014-08-13 14:40:53 -0700514 private IOFSwitch createConnectedSwitchMock(long dpid) {
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700515 IOFSwitch sw = createMock(IOFSwitch.class);
516 expect(sw.isConnected()).andReturn(true).anyTimes();
517 expect(sw.getId()).andReturn(dpid).anyTimes();
Jonathan Hartce07f322014-08-13 14:40:53 -0700518 expect(sw.getStringId()).andReturn(HexString.toHexString(dpid))
519 .anyTimes();
520 expect(sw.getNextTransactionId()).andReturn(1).times(0, 1);
521 // TODO 1.3ize
522 expect(sw.getFactory()).andReturn(factory10).anyTimes();
Naoki Shiota47993102014-07-09 14:00:54 -0700523 sw.flush();
Sho SHIMIZU107344e2014-08-13 16:11:53 -0700524 expectLastCall().anyTimes();
Naoki Shiota47993102014-07-09 14:00:54 -0700525
526 return sw;
527 }
528
Naoki Shiota75b7dd62013-12-03 18:09:21 -0800529}