blob: 68e31ba763bc353900cac9408c3c4cda6149674a [file] [log] [blame]
Toshio Koidefe2625e2013-06-26 13:59:53 -07001package net.onrc.onos.ofcontroller.flowmanager;
2
3import static org.junit.Assert.*;
4import static org.easymock.EasyMock.expect;
5import static org.easymock.EasyMock.cmpEq;
6import static org.powermock.api.easymock.PowerMock.*;
7
8import java.util.*;
9import java.util.concurrent.Executors;
10import java.util.concurrent.ScheduledExecutorService;
11import java.util.concurrent.TimeUnit;
12
13import net.floodlightcontroller.core.IFloodlightProviderService;
Toshio Koideca7abe02013-06-27 17:30:17 -070014import net.floodlightcontroller.core.IOFSwitch;
Toshio Koidefe2625e2013-06-26 13:59:53 -070015import net.floodlightcontroller.core.module.FloodlightModuleContext;
16import net.floodlightcontroller.core.module.IFloodlightService;
17import net.floodlightcontroller.restserver.IRestApiService;
18import net.onrc.onos.graph.GraphDBOperation;
19import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
20import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
Toshio Koidefe2625e2013-06-26 13:59:53 -070021import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070022import net.onrc.onos.ofcontroller.topology.TopologyManager;
Toshio Koidefe2625e2013-06-26 13:59:53 -070023import net.onrc.onos.ofcontroller.util.*;
24
25import org.easymock.EasyMock;
26import org.easymock.IAnswer;
27import org.junit.After;
28import org.junit.Before;
Toshio Koideca7abe02013-06-27 17:30:17 -070029import org.junit.Ignore;
Toshio Koidefe2625e2013-06-26 13:59:53 -070030import org.junit.Test;
31import org.junit.runner.RunWith;
Toshio Koideca7abe02013-06-27 17:30:17 -070032import org.openflow.protocol.OFFlowMod;
33import org.openflow.protocol.OFType;
34import org.openflow.protocol.factory.BasicFactory;
Toshio Koidefe2625e2013-06-26 13:59:53 -070035import org.powermock.core.classloader.annotations.PrepareForTest;
36import org.powermock.modules.junit4.PowerMockRunner;
37
38/**
39 * @author Toshio Koide
40 */
41@RunWith(PowerMockRunner.class)
42@PrepareForTest({FlowManager.class, GraphDBOperation.class, System.class, Executors.class})
43public class FlowManagerTest {
44 private static FloodlightModuleContext context;
45 private static IFloodlightProviderService floodlightProvider;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070046 private static TopologyManager topologyManager;
Toshio Koidefe2625e2013-06-26 13:59:53 -070047 private static IRestApiService restApi;
48 private static GraphDBOperation op;
49
50 /**
51 * @throws java.lang.Exception
52 */
53 @Before
54 public void setUp() throws Exception {
55 }
56
57 /**
58 * @throws java.lang.Exception
59 */
60 @After
61 public void tearDown() throws Exception {
62 }
63
64 /**
65 * @throws java.lang.Exception
66 */
67 private void expectInitWithContext() throws Exception {
68 // create mock objects
69 context = createMock(FloodlightModuleContext.class);
70 floodlightProvider = createMock(IFloodlightProviderService.class);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070071 topologyManager = createMock(TopologyManager.class);
Toshio Koidefe2625e2013-06-26 13:59:53 -070072 restApi = createMock(IRestApiService.class);
73 op = createMock(GraphDBOperation.class);
74
75 // setup expectations
76 expect(context.getServiceImpl(IFloodlightProviderService.class)).andReturn(floodlightProvider);
Toshio Koidefe2625e2013-06-26 13:59:53 -070077 expect(context.getServiceImpl(IRestApiService.class)).andReturn(restApi);
78 expectNew(GraphDBOperation.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(op);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070079 expectNew(TopologyManager.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(topologyManager);
Toshio Koidefe2625e2013-06-26 13:59:53 -070080 }
81
82 private IFlowPath createIFlowPathMock(long flowId, String installerID,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070083 long flowPathFlags,
Toshio Koidefe2625e2013-06-26 13:59:53 -070084 long srcDpid, int srcPort, long dstDpid, int dstPort) {
85 IFlowPath iFlowPath = createNiceMock(IFlowPath.class);
86 expect(iFlowPath.getFlowId()).andReturn(new FlowId(flowId).toString()).anyTimes();
87 expect(iFlowPath.getInstallerId()).andReturn(installerID).anyTimes();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070088 expect(iFlowPath.getFlowPathFlags()).andReturn(new Long(flowPathFlags)).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -070089 expect(iFlowPath.getSrcSwitch()).andReturn(new Dpid(srcDpid).toString()).anyTimes();
90 expect(iFlowPath.getSrcPort()).andReturn(new Short((short)srcPort)).anyTimes();
91 expect(iFlowPath.getDstSwitch()).andReturn(new Dpid(dstDpid).toString()).anyTimes();
92 expect(iFlowPath.getDstPort()).andReturn(new Short((short)dstPort)).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -070093 return iFlowPath;
94 }
95
Toshio Koideca7abe02013-06-27 17:30:17 -070096 private FlowPath createTestFlowPath(long flowId, String installerId,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070097 final long flowPathFlags,
Toshio Koidefe2625e2013-06-26 13:59:53 -070098 final long srcDpid, final int srcPort,
99 final long dstDpid, final int dstPort
100 ) {
101 FlowPath flowPath = new FlowPath();
102 flowPath.setFlowId(new FlowId(flowId));
103 flowPath.setInstallerId(new CallerId(installerId));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700104 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700105 flowPath.setDataPath(new DataPath() {{
106 setSrcPort(new SwitchPort(new Dpid(srcDpid), new Port((short)srcPort)));
107 setDstPort(new SwitchPort(new Dpid(dstDpid), new Port((short)dstPort)));
108 }});
109 flowPath.setFlowEntryMatch(new FlowEntryMatch());
110 return flowPath;
111 }
112
113 private ArrayList<FlowPath> createTestFlowPaths() {
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700114 FlowPath flowPath1 = createTestFlowPath(1, "foo caller id", 0, 1, 1, 2, 2);
115 FlowPath flowPath2 = createTestFlowPath(2, "caller id", 0, 1, 1, 2, 2);
116 FlowPath flowPath3 = createTestFlowPath(3, "caller id", 0, 1, 5, 2, 2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700117
118 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
119 flowPaths.add(flowPath1);
120 flowPaths.add(flowPath2);
121 flowPaths.add(flowPath3);
122
123 return flowPaths;
124 }
125
126
127 // IFlowService methods
128
129
130 /**
131 * Test method for {@link FlowManager#addFlow(FlowPath, FlowId, String)}.
132 * @throws Exception
133 */
134 @Test
135 public final void testAddFlowFailGraphCreatesNoFlow() throws Exception {
136 // instantiate required objects
137 FlowId flowId = new FlowId(123);
138 FlowPath flowPath = new FlowPath();
139 flowPath.setFlowId(flowId);
Toshio Koideca7abe02013-06-27 17:30:17 -0700140 FlowManager fm = new FlowManager();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700141
142 // setup expectations
143 expectInitWithContext();
144 expect(op.searchFlowPath(flowId)).andReturn(null);
145 expect(op.newFlowPath()).andReturn(null);
146 op.rollback();
147
148 // start the test
149 replayAll();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700150
Toshio Koidefe2625e2013-06-26 13:59:53 -0700151 fm.init(context);
152 Boolean result = fm.addFlow(flowPath, flowId, "");
153
154 // verify the test
155 verifyAll();
156 assertFalse(result);
157 }
158
159 /**
160 * Test method for {@link FlowManager#addFlow(FlowPath, FlowId, String)}.
161 * @throws Exception
162 */
163 @Test
164 public final void testAddFlowSuccessNormally() throws Exception {
165 final String addFlowEntry = "addFlowEntry";
166 // create mock objects
167 IFlowPath createdFlowPath = createNiceMock(IFlowPath.class);
168 IFlowEntry createdFlowEntry1 = createNiceMock(IFlowEntry.class);
169 IFlowEntry createdFlowEntry2 = createNiceMock(IFlowEntry.class);
170 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
171
172 // instantiate required objects
173 final FlowEntry flowEntry1 = new FlowEntry();
174 final FlowEntry flowEntry2 = new FlowEntry();
175 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
176 flowEntries.add(flowEntry1);
177 flowEntries.add(flowEntry2);
178
179 DataPath dataPath = new DataPath();
180 dataPath.setSrcPort(new SwitchPort(new Dpid(0x1234), new Port((short)1)));
181 dataPath.setDstPort(new SwitchPort(new Dpid(0x5678), new Port((short)2)));
182 dataPath.setFlowEntries(flowEntries);
183
184 FlowEntryMatch match = new FlowEntryMatch();
185
186 FlowPath flowPath = new FlowPath();
187 flowPath.setFlowId(new FlowId(0x100));
188 flowPath.setInstallerId(new CallerId("installer id"));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700189 flowPath.setFlowPathFlags(new FlowPathFlags(0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700190 flowPath.setDataPath(dataPath);
191 flowPath.setFlowEntryMatch(match);
192
193 // setup expectations
194 expectInitWithContext();
195 expect(op.searchFlowPath(cmpEq(new FlowId(0x100)))).andReturn(null);
196 expect(op.newFlowPath()).andReturn(createdFlowPath);
197 createdFlowPath.setFlowId("0x100");
198 createdFlowPath.setType("flow");
199 createdFlowPath.setInstallerId("installer id");
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700200 createdFlowPath.setFlowPathFlags(new Long((long)0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700201 createdFlowPath.setSrcSwitch("00:00:00:00:00:00:12:34");
202 createdFlowPath.setSrcPort(new Short((short)1));
203 createdFlowPath.setDstSwitch("00:00:00:00:00:00:56:78");
204 createdFlowPath.setDstPort(new Short((short)2));
205 createdFlowPath.setDataPathSummary("data path summary");
206 createdFlowPath.setUserState("FE_USER_ADD");
207
208 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry1)
209 .andReturn(createdFlowEntry1);
210 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry2)
211 .andReturn(createdFlowEntry2);
212
213 op.commit();
214
215 // start the test
216 replayAll();
217
218 fm.init(context);
219 Boolean result = fm.addFlow(flowPath, new FlowId(0x100), "data path summary");
220
221 // verify the test
222 verifyAll();
223 assertTrue(result);
224 }
225
226 /**
227 * Test method for {@link FlowManager#deleteAllFlows()}.
228 * @throws Exception
229 */
230 @Test
231 public final void testDeleteAllFlowsSuccessNormally() throws Exception {
232 // create mock objects
233 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
234 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
235
236 // instantiate required objects
237 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
238 flowPaths.add(flowPath1);
239 flowPaths.add(flowPath2);
Toshio Koideca7abe02013-06-27 17:30:17 -0700240 FlowManager fm = new FlowManager();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700241
242 // setup expectations
243 expectInitWithContext();
244 expect(op.getAllFlowPaths()).andReturn(flowPaths);
245
246 expect(flowPath1.getFlowId()).andReturn("1").anyTimes();
247 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowPath1);
248 expect(flowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
249 op.removeFlowPath(flowPath1);
250
251 expect(flowPath2.getFlowId()).andReturn("2").anyTimes();
252 expect(op.searchFlowPath(cmpEq(new FlowId(2)))).andReturn(flowPath2);
253 expect(flowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
254 op.removeFlowPath(flowPath2);
255
256 op.commit();
257 expectLastCall().anyTimes();
258
259 // start the test
260 replayAll();
261
Toshio Koidefe2625e2013-06-26 13:59:53 -0700262 fm.init(context);
263 Boolean result = fm.deleteAllFlows();
264
265 // verify the test
266 verifyAll();
267 assertTrue(result);
268 }
269
270 /**
271 * Test method for {@link FlowManager#deleteFlow(FlowId)}.
272 * @throws Exception
273 */
274 @Test
275 public final void testDeleteFlowSuccessEmptyFlowPath() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700276 // instantiate required objects
277 FlowManager fm = new FlowManager();
278
Toshio Koidefe2625e2013-06-26 13:59:53 -0700279 // create mock objects
280 IFlowPath flowObj = createNiceMock(IFlowPath.class);
281
282 // setup expectations
283 expectInitWithContext();
284 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowObj);
285 expect(flowObj.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
286 op.removeFlowPath(flowObj);
287 op.commit();
288 expectLastCall().anyTimes();
289
290 // start the test
291 replayAll();
292
Toshio Koidefe2625e2013-06-26 13:59:53 -0700293 fm.init(context);
294 Boolean result = fm.deleteFlow(new FlowId(1));
295
296 // verify the test
297 verifyAll();
298 assertTrue(result);
299 }
300
301 /**
302 * Test method for {@link FlowManager#clearAllFlows()}.
303 * @throws Exception
304 */
305 @Test
306 public final void testClearAllFlowsSuccessNormally() throws Exception {
307 // create mock objects
308 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
309 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
310 IFlowPath flowPath3 = createNiceMock(IFlowPath.class);
311 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, "clearFlow");
312
313 // instantiate required objects
314 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
315 flowPaths.add(flowPath1);
316 flowPaths.add(flowPath2);
317 flowPaths.add(null);
318 flowPaths.add(flowPath3);
319
320 // setup expectations
321 expectInitWithContext();
322 expect(op.getAllFlowPaths()).andReturn(flowPaths);
323 expect(flowPath1.getFlowId()).andReturn(new FlowId(1).toString());
324 expect(flowPath2.getFlowId()).andReturn(null);
325 expect(flowPath3.getFlowId()).andReturn(new FlowId(3).toString());
326 expect(fm.clearFlow(cmpEq(new FlowId(1)))).andReturn(true);
327 expect(fm.clearFlow(cmpEq(new FlowId(3)))).andReturn(true);
328
329 // start the test
330 replayAll();
331
332 fm.init(context);
333 Boolean result = fm.clearAllFlows();
334
335 //verify the test
336 verifyAll();
337 assertTrue(result);
338 }
339
340 /**
341 * Test method for {@link FlowManager#getFlow()}.
342 * @throws Exception
343 */
344 @Test
345 public final void testGetFlowSuccessNormally() throws Exception {
346 // instantiate required objects
347 FlowManager fm = new FlowManager();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700348 IFlowPath iFlowPath = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700349
350 // setup expectations
351 expectInitWithContext();
Toshio Koideca7abe02013-06-27 17:30:17 -0700352 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(iFlowPath);
353 expect(iFlowPath.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700354 op.commit();
355
356 // start the test
357 replayAll();
358
359 fm.init(context);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700360 FlowPath flowPath = fm.getFlow(new FlowId(1));
361 String installerId = flowPath.installerId().toString();
362 long flowPathFlags = flowPath.flowPathFlags().flags();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700363
364 //verify the test
365 verifyAll();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700366 assertEquals("caller id", installerId);
367 assertEquals(0L, flowPathFlags);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700368 }
369
370 /**
371 * Test method for {@link FlowManager#getAllFlows(CallerId, DataPathEndpoints)}.
372 * @throws Exception
373 */
374 @Test
375 public final void testGetAllFlowsWithCallerIdAndDataPathEndpointsSuccessNormally() throws Exception {
376 final String getAllFlows = "getAllFlows";
377 // create mock objects
378 FlowManager fm = createPartialMock(FlowManager.class, getAllFlows,
379 new Class<?>[]{}, new Object[]{});
380
381 // instantiate required objects
382 DataPathEndpoints dataPathEndpoints = new DataPathEndpoints(
383 new SwitchPort(new Dpid(1), new Port((short)1)),
384 new SwitchPort(new Dpid(2), new Port((short)2)));
385
386 ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths();
387
388 //setup expectations
389 expectInitWithContext();
390 expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows);
391
392 //start the test
393 replayAll();
394
395 fm.init(context);
396 ArrayList<FlowPath> flows = fm.getAllFlows(new CallerId("caller id"), dataPathEndpoints);
397
398 // verify the test
399 verifyAll();
400 assertEquals(1, flows.size());
401 assertEquals(obtainedAllFlows.get(1), flows.get(0));
402 }
403
404 /**
405 * Test method for {@link FlowManager#getAllFlows(DataPathEndpoints)}.
406 * @throws Exception
407 */
408 @Test
409 public final void testGetAllFlowsWithDataPathEndpointsSuccessNormally() throws Exception {
410 final String getAllFlows = "getAllFlows";
411 // create mock objects
412 FlowManager fm = createPartialMock(FlowManager.class, getAllFlows,
413 new Class<?>[]{}, new Object[]{});
414
415 // instantiate required objects
416 DataPathEndpoints dataPathEndpoints = new DataPathEndpoints(
417 new SwitchPort(new Dpid(1), new Port((short)1)),
418 new SwitchPort(new Dpid(2), new Port((short)2)));
419
420 ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths();
421
422 //setup expectations
423 expectInitWithContext();
424 expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows);
425
426 //start the test
427 replayAll();
428
429 fm.init(context);
430 ArrayList<FlowPath> flows = fm.getAllFlows(dataPathEndpoints);
431
432 // verify the test
433 verifyAll();
434 assertEquals(2, flows.size());
435 assertEquals(obtainedAllFlows.get(0), flows.get(0));
436 assertEquals(obtainedAllFlows.get(1), flows.get(1));
437 // TODO: ignore the order of flows in the list
438 }
439
440 /**
441 * Test method for {@link FlowManager#getAllFlowsSummary(FlowId, int)}.
442 * @throws Exception
443 */
444 @Test
445 public final void testGetAllFlowsSummarySuccessNormally() throws Exception {
446 final String getAllFlowsWithoutFlowEntries = "getAllFlowsWithoutFlowEntries";
447 // create mock objects
448 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, getAllFlowsWithoutFlowEntries);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700449 IFlowPath flowPath1 = createIFlowPathMock(1, "", 0, 1, 2, 3, 4);
450 IFlowPath flowPath2 = createIFlowPathMock(5, "", 0, 2, 3, 4, 5);
451 IFlowPath flowPath3 = createIFlowPathMock(10, "", 0, 3, 4, 5, 6);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700452
453 // instantiate required objects
454 ArrayList<IFlowPath> flows = new ArrayList<IFlowPath>();
455 flows.add(flowPath3);
456 flows.add(flowPath1);
457 flows.add(flowPath2);
458
459 // setup expectations
460 expectInitWithContext();
461 expectPrivate(fm, getAllFlowsWithoutFlowEntries).andReturn(flows);
462
463 // start the test
464 replayAll();
465
466 fm.init(context);
467 ArrayList<IFlowPath> returnedFlows = fm.getAllFlowsSummary(null, 0);
468
469 // verify the test
470 verifyAll();
471 assertEquals(3, returnedFlows.size());
472 assertEquals(1, new FlowId(returnedFlows.get(0).getFlowId()).value());
473 assertEquals(5, new FlowId(returnedFlows.get(1).getFlowId()).value());
474 assertEquals(10, new FlowId(returnedFlows.get(2).getFlowId()).value());
475 }
476
477 /**
478 * Test method for {@link FlowManager#getAllFlows()}.
479 * @throws Exception
480 */
481 @Test
482 public final void testGetAllFlowsSuccessNormally() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700483 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700484 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
485 IFlowPath iFlowPath2 = createIFlowPathMock(2, "caller id", 0, 2, 5, 3, 5);
Toshio Koideca7abe02013-06-27 17:30:17 -0700486
Toshio Koidefe2625e2013-06-26 13:59:53 -0700487 // instantiate required objects
488 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
Toshio Koideca7abe02013-06-27 17:30:17 -0700489 flowPaths.add(iFlowPath1);
490 flowPaths.add(iFlowPath2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700491 FlowManager fm = new FlowManager();
492
493 // setup expectations
494 expectInitWithContext();
495 expect(op.getAllFlowPaths()).andReturn(flowPaths);
Toshio Koideca7abe02013-06-27 17:30:17 -0700496 expect(iFlowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
497 expect(iFlowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700498 op.commit();
499
500 // start the test
501 replayAll();
502
503 fm.init(context);
504 ArrayList<FlowPath> flows = fm.getAllFlows();
505
506 // verify the test
507 verifyAll();
508 assertEquals(2, flows.size());
509 assertEquals(new SwitchPort(new Dpid(1), new Port((short)1)).toString(),
510 flows.get(0).dataPath().srcPort().toString());
511 assertEquals(new SwitchPort(new Dpid(2), new Port((short)5)).toString(),
512 flows.get(1).dataPath().srcPort().toString());
513 // TODO: more asserts
514 // TODO: ignore seq. of the list
515 }
516
517 /**
518 * Test method for {@link FlowManager#addAndMaintainShortestPathFlow(FlowPath)}.
519 * @throws Exception
520 */
521 @Test
522 public final void testAddAndMaintainShortestPathFlowSuccessNormally() throws Exception {
523 final String addFlow = "addFlow";
524
525 // create mock objects
526 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow);
527
528 // instantiate required objects
529 DataPath dataPath = new DataPath();
530 dataPath.setSrcPort(new SwitchPort(new Dpid(1), new Port((short)3)));
531 dataPath.setDstPort(new SwitchPort(new Dpid(2), new Port((short)4)));
532 FlowEntryMatch match = new FlowEntryMatch();
533 FlowPath paramFlow = new FlowPath();
534 paramFlow.setFlowId(new FlowId(100));
535 paramFlow.setInstallerId(new CallerId("installer id"));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700536 paramFlow.setFlowPathFlags(new FlowPathFlags(0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700537 paramFlow.setDataPath(dataPath);
538 paramFlow.setFlowEntryMatch(match);
539
540 // setup expectations
541 expectInitWithContext();
542 expectPrivate(fm, addFlow,
543 EasyMock.anyObject(FlowPath.class),
544 EasyMock.anyObject(FlowId.class),
545 EasyMock.anyObject(String.class)
546 ).andAnswer(new IAnswer<Object>() {
547 public Object answer() throws Exception {
548 FlowPath flowPath = (FlowPath)EasyMock.getCurrentArguments()[0];
549 assertEquals(flowPath.flowId().value(), 100);
550 assertEquals(flowPath.installerId().toString(), "installer id");
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700551 assertEquals(flowPath.flowPathFlags().flags(), 0);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700552 assertEquals(flowPath.dataPath().srcPort().toString(),
553 new SwitchPort(new Dpid(1), new Port((short)3)).toString());
554
555 String dataPathSummary = (String)EasyMock.getCurrentArguments()[2];
556 assertEquals(dataPathSummary, "X");
557
558 return true;
559 }
560 });
561
562 // start the test
563 replayAll();
564
565 fm.init(context);
566 FlowPath resultFlow = fm.addAndMaintainShortestPathFlow(paramFlow);
567
568 // verify the test
569 verifyAll();
570 assertEquals(paramFlow.flowId().value(), resultFlow.flowId().value());
571 assertEquals(paramFlow.installerId().toString(), resultFlow.installerId().toString());
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700572 assertEquals(paramFlow.flowPathFlags().flags(), resultFlow.flowPathFlags().flags());
Toshio Koidefe2625e2013-06-26 13:59:53 -0700573 assertEquals(paramFlow.dataPath().toString(), resultFlow.dataPath().toString());
574 assertEquals(paramFlow.flowEntryMatch().toString(), resultFlow.flowEntryMatch().toString());
575 }
576
Toshio Koidefe2625e2013-06-26 13:59:53 -0700577 // INetMapStorage methods
578
Toshio Koidefe2625e2013-06-26 13:59:53 -0700579 /**
580 * Test method for {@link FlowManager#init(String)}.
581 * @throws Exception
582 */
583 @Test
584 public final void testInitSuccessNormally() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700585 // instantiate required objects
586 FlowManager fm = new FlowManager();
587
Toshio Koidefe2625e2013-06-26 13:59:53 -0700588 // create mock objects
589 op = createMock(GraphDBOperation.class);
590
591 // setup expectations
592 expectNew(GraphDBOperation.class, "/dummy/path").andReturn(op);
593
594 // start the test
595 replayAll();
596
Toshio Koidefe2625e2013-06-26 13:59:53 -0700597 fm.init("/dummy/path");
598
599 // verify the test
600 verifyAll();
601 }
602
603 /**
604 * Test method for {@link FlowManager#close()}.
605 * @throws Exception
606 */
607 @Test
608 public final void testCloseSuccessNormally() throws Exception {
609 // instantiate required objects
610 FlowManager fm = new FlowManager();
611
612 // setup expectations
613 expectInitWithContext();
614 op.close();
615
616 // start the test
617 replayAll();
618
619 fm.init(context);
620 fm.close();
621
622 // verify the test
623 verifyAll();
624 }
625
626
627 // IFloodlightModule methods
628
629
630 /**
631 * Test method for {@link FlowManager#getModuleServices()}.
632 * @throws Exception
633 */
634 @Test
635 public final void testGetModuleServicesSuccessNormally() throws Exception {
636 // instantiate required objects
637 FlowManager fm = new FlowManager();
638
639 // setup expectations
640 expectInitWithContext();
641
642 // start the test
643 replayAll();
644
645 fm.init(context);
646 Collection<Class<? extends IFloodlightService>> l = fm.getModuleServices();
647
648 // verify the test
649 verifyAll();
650 assertEquals(1, l.size());
651 assertEquals(IFlowService.class, l.iterator().next());
652 }
653
654 /**
655 * Test method for {@link FlowManager#getServiceImpls()}.
656 * @throws Exception
657 */
658 @Test
659 public final void testGetServiceImplsSuccessNormally() throws Exception {
660 // instantiate required objects
661 FlowManager fm = new FlowManager();
662
663 // setup expectations
664 expectInitWithContext();
665
666 // start the test
667 replayAll();
668
669 fm.init(context);
670 Map<Class<? extends IFloodlightService>, IFloodlightService> si = fm.getServiceImpls();
671
672 // verify the test
673 verifyAll();
674 assertEquals(1, si.size());
675 assertTrue(si.containsKey(IFlowService.class));
676 assertEquals(fm, si.get(IFlowService.class));
677 }
678
679 /**
680 * Test method for {@link FlowManager#getModuleDependencies()}.
681 * @throws Exception
682 */
683 @Test
684 public final void testGetModuleDependenciesSuccessNormally() throws Exception {
685 // instantiate required objects
686 FlowManager fm = new FlowManager();
687
688 // setup expectations
689 expectInitWithContext();
690
691 // start the test
692 replayAll();
693
694 fm.init(context);
695 Collection<Class<? extends IFloodlightService>> md = fm.getModuleDependencies();
696
697 // verify the test
698 verifyAll();
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -0700699 assertEquals(2, md.size());
Toshio Koidefe2625e2013-06-26 13:59:53 -0700700 assertTrue(md.contains(IFloodlightProviderService.class));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700701 assertTrue(md.contains(IRestApiService.class));
702 }
703
704 /**
705 * Test method for {@link FlowManager#init(FloodlightModuleContext)}.
706 * @throws Exception
707 */
708 @Test
709 public final void testInitWithFloodlightModuleContextSuccessNormally() throws Exception {
710 // instantiate required objects
711 FlowManager fm = new FlowManager();
712
713 // setup expectations
714 expectInitWithContext();
715
716 // start the test
717 replayAll();
718
719 fm.init(context);
720
721 // verify the test
722 verifyAll();
723 }
724
725 /**
726 * Test method for {@link FlowManager#startUp(FloodlightModuleContext)}.
727 * @throws Exception
728 */
729 @Test
730 public final void testStartupSuccessNormally() throws Exception {
731 // create mock objects
732 mockStaticPartial(Executors.class, "newScheduledThreadPool");
733 ScheduledExecutorService scheduler = createMock(ScheduledExecutorService.class);
734
Toshio Koidefe2625e2013-06-26 13:59:53 -0700735 // instantiate required objects
736 FlowManager fm = new FlowManager();
737
738 // setup expectations
739 expectInitWithContext();
740 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
741 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
742 expect(scheduler.scheduleAtFixedRate(
743 EasyMock.anyObject(Runnable.class),
744 EasyMock.anyLong(),
745 EasyMock.anyLong(),
746 EasyMock.anyObject(TimeUnit.class))).andReturn(null).times(2);
747 restApi.addRestletRoutable(EasyMock.anyObject(FlowWebRoutable.class));
748
749 // start the test
750 replayAll();
751
752 fm.init(context);
753 fm.startUp(context);
754
755 // verify the test
756 verifyAll();
757 }
Toshio Koideca7abe02013-06-27 17:30:17 -0700758
759
760 // other methods
761
762
763 /**
764 * Test method for {@link FlowManager#clearFlow(FlowId)}.
765 * @throws Exception
766 */
767 @Test
768 public final void testClearFlowSuccessNormally() throws Exception {
769 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700770 IFlowPath flowPath = createIFlowPathMock(123, "id", 0, 1, 2, 3, 4);
Toshio Koideca7abe02013-06-27 17:30:17 -0700771 IFlowEntry flowEntry1 = createMock(IFlowEntry.class);
772 IFlowEntry flowEntry2 = createMock(IFlowEntry.class);
773 IFlowEntry flowEntry3 = createMock(IFlowEntry.class);
774
775 // instantiate required objects
776 FlowManager fm = new FlowManager();
777 FlowId flowId = new FlowId(123);
778 ArrayList<IFlowEntry> flowEntries = new ArrayList<IFlowEntry>();
779 flowEntries.add(flowEntry1);
780 flowEntries.add(flowEntry2);
781 flowEntries.add(flowEntry3);
782
783 // setup expectations
784 expectInitWithContext();
785 expect(op.searchFlowPath(cmpEq(flowId))).andReturn(flowPath);
786 expect(flowPath.getFlowEntries()).andReturn(flowEntries);
787 flowPath.removeFlowEntry(flowEntry1);
788 flowPath.removeFlowEntry(flowEntry2);
789 flowPath.removeFlowEntry(flowEntry3);
790 op.removeFlowEntry(flowEntry1);
791 op.removeFlowEntry(flowEntry2);
792 op.removeFlowEntry(flowEntry3);
793 op.removeFlowPath(flowPath);
794 op.commit();
795
796 // start the test
797 replayAll();
798
799 fm.init(context);
800 fm.clearFlow(flowId);
801
802 // verify the test
803 verifyAll();
804 }
805
806 /**
807 * Test method for {@link FlowManager#getAllFlowsWithoutFlowEntries()}.
808 * @throws Exception
809 */
810 @Test
811 public final void testGetAllFlowsWithoutFlowEntriesSuccessNormally() throws Exception {
812 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700813 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
814 IFlowPath iFlowPath2 = createIFlowPathMock(2, "caller id", 0, 2, 5, 3, 5);
Toshio Koideca7abe02013-06-27 17:30:17 -0700815
816 // instantiate required objects
817 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
818 flowPaths.add(iFlowPath1);
819 flowPaths.add(iFlowPath2);
820 FlowManager fm = new FlowManager();
821
822 // setup expectations
823 expectInitWithContext();
824 op.commit();
825 expect(op.getAllFlowPaths()).andReturn(flowPaths);
826
827 // start the test
828 replayAll();
829
830 fm.init(context);
831 ArrayList<IFlowPath> result = fm.getAllFlowsWithoutFlowEntries();
832
833 // verify the test
834 verifyAll();
835 assertEquals(iFlowPath1, result.get(0));
836 assertEquals(iFlowPath2, result.get(1));
837
838 // TODO: does this method just return the replica of the flow paths?
839 }
840
841 /**
842 * Test method for {@link FlowManager#reconcileFlow(IFlowPath, DataPath)}.
843 * @throws Exception
844 */
845 @Test
846 public final void testReconcileFlowWithFlowPathAndDataPathSuccessNormally() throws Exception {
847 final String addFlowEntry = "addFlowEntry";
848
849 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700850 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
Toshio Koideca7abe02013-06-27 17:30:17 -0700851 IFlowEntry iFlowEntry1 = createMock(IFlowEntry.class);
852 IFlowEntry iFlowEntry2 = createMock(IFlowEntry.class);
853 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
854
855 // instantiate required objects
856 FlowEntry flowEntry1 = new FlowEntry();
857 flowEntry1.setDpid(new Dpid(1));
858 flowEntry1.setFlowId(new FlowId(1));
859 flowEntry1.setInPort(new Port((short) 1));
860 flowEntry1.setOutPort(new Port((short) 11));
861 flowEntry1.setFlowEntryId(new FlowEntryId(1));
862 flowEntry1.setFlowEntryMatch(new FlowEntryMatch());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700863 flowEntry1.setFlowEntryActions(new FlowEntryActions());
Toshio Koideca7abe02013-06-27 17:30:17 -0700864 flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState());
865
866 FlowEntry flowEntry2 = new FlowEntry();
867 flowEntry2.setDpid(new Dpid(2));
868 flowEntry2.setFlowId(new FlowId(2));
869 flowEntry2.setInPort(new Port((short) 22));
870 flowEntry2.setOutPort(new Port((short) 2));
871 flowEntry2.setFlowEntryId(new FlowEntryId(2));
872 flowEntry2.setFlowEntryMatch(new FlowEntryMatch());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700873 flowEntry2.setFlowEntryActions(new FlowEntryActions());
Toshio Koideca7abe02013-06-27 17:30:17 -0700874 flowEntry2.setFlowEntryErrorState(new FlowEntryErrorState());
875
876 DataPath dataPath = new DataPath();
877 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
878 flowEntries.add(flowEntry1);
879 flowEntries.add(flowEntry2);
880 dataPath.setFlowEntries(flowEntries);
881
882 ArrayList<IFlowEntry> oldFlowEntries = new ArrayList<IFlowEntry>();
883 oldFlowEntries.add(iFlowEntry1);
884 oldFlowEntries.add(iFlowEntry2);
885
886 // setup expectations
887 expectInitWithContext();
888 expect(floodlightProvider.getSwitches()).andReturn(null); // TODO: why is this needed?
889 expect(iFlowPath1.getFlowEntries()).andReturn(oldFlowEntries);
890 iFlowEntry1.setUserState("FE_USER_DELETE");
891 iFlowEntry1.setSwitchState("FE_SWITCH_NOT_UPDATED");
892 iFlowEntry2.setUserState("FE_USER_DELETE");
893 iFlowEntry2.setSwitchState("FE_SWITCH_NOT_UPDATED");
894 expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry1).andReturn(null);
895 expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry2).andReturn(null);
896
897 // start the test
898 replayAll();
899
900 fm.init(context);
901 Boolean result = fm.reconcileFlow(iFlowPath1, dataPath);
902
903 // verify the test
904 verifyAll();
905 assertTrue(result);
906 // TODO: write more asserts
907 }
908
909 /**
910 * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, IFlowPath, IFlowEntry)}.
911 * @throws Exception
912 */
913 @Test
914 public final void testInstallFlowEntryWithIFlowPathSuccessNormally() throws Exception {
915 // create mock object
916 IOFSwitch iofSwitch = createNiceMock(IOFSwitch.class);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700917 IFlowPath iFlowPath = createIFlowPathMock(1, "id", 0, 1, 2, 3, 4);
Toshio Koideca7abe02013-06-27 17:30:17 -0700918 IFlowEntry iFlowEntry = createMock(IFlowEntry.class);
919 BasicFactory basicFactory = createMock(BasicFactory.class);
920
921 // instantiate required objects
922 FlowManager fm = new FlowManager();
923
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700924 FlowEntryAction action = new FlowEntryAction();
925 action.setActionOutput(new Port((short)2));
926 FlowEntryActions actions = new FlowEntryActions();
927 actions.addAction(action);
928
Toshio Koideca7abe02013-06-27 17:30:17 -0700929 // setup expectations
930 expectInitWithContext();
931 expect(iFlowEntry.getFlowEntryId()).andReturn(new FlowEntryId(123).toString());
932 expect(iFlowEntry.getUserState()).andReturn("FE_USER_ADD");
933 iFlowEntry.setSwitchState("FE_SWITCH_UPDATED");
934 expect(iFlowEntry.getMatchInPort()).andReturn(new Short((short) 1));
Toshio Koideca7abe02013-06-27 17:30:17 -0700935 expect(iFlowEntry.getMatchSrcMac()).andReturn("01:23:45:67:89:01");
936 expect(iFlowEntry.getMatchDstMac()).andReturn("01:23:45:67:89:02");
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700937 expect(iFlowEntry.getMatchEthernetFrameType()).andReturn(new Short((short)0x0800));
938 expect(iFlowEntry.getMatchVlanId()).andReturn(new Short((short)0x1234));
939 expect(iFlowEntry.getMatchVlanPriority()).andReturn(new Byte((byte)0x10));
940 expect(iFlowEntry.getMatchSrcIPv4Net()).andReturn("192.168.0.1");
941 expect(iFlowEntry.getMatchDstIPv4Net()).andReturn("192.168.0.2");
942 expect(iFlowEntry.getMatchIpProto()).andReturn(new Byte((byte)0x20));
943 expect(iFlowEntry.getMatchIpToS()).andReturn(new Byte((byte)0x3));
944 expect(iFlowEntry.getMatchSrcTcpUdpPort()).andReturn(new Short((short)40000));
945 expect(iFlowEntry.getMatchDstTcpUdpPort()).andReturn(new Short((short)80));
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700946 expect(iFlowEntry.getActions()).andReturn(actions.toString());
Toshio Koideca7abe02013-06-27 17:30:17 -0700947 expect(floodlightProvider.getOFMessageFactory()).andReturn(basicFactory);
948 expect(basicFactory.getMessage(OFType.FLOW_MOD)).andReturn(new OFFlowMod());
949 expect(iofSwitch.getStringId()).andReturn(new Dpid(100).toString());
950
951 // start the test
952 replayAll();
953
954 fm.init(context);
955 Boolean result = fm.installFlowEntry(iofSwitch, iFlowPath, iFlowEntry);
956
957 // verify the test
958 verifyAll();
959 assertTrue(result);
960 // TODO: write more asserts
961 }
962
963 /**
964 * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}.
965 * The method seems to be not used for now.
966 */
967 @Ignore @Test
968 public final void testInstallFlowEntryWithFlowPathSuccessNormally() {
969 fail("not yet implemented");
970 }
971
972 /**
973 * Test method for {@link FlowManager#removeFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}.
974 * The method seems to be not implemented and not used for now.
975 */
976 @Ignore @Test
977 public final void testRemoveFlowEntrySuccessNormally() {
978 fail("not yet implemented");
979 }
980
981 /**
982 * Test method for {@link FlowManager#installRemoteFlowEntry(FlowPath, FlowEntry)}.
983 * The method seems to be not implemented and not used for now.
984 */
985 @Ignore @Test
986 public final void testInstallRemoteFlowEntrySuccessNormally() {
987 fail("not yet implemented");
988 }
989
990 /**
991 * Test method for {@link FlowManager#removeRemoteFlowEntry(FlowPath, FlowEntry)}.
992 * The method seems to be not implemented and not used for now.
993 */
994 @Ignore @Test
995 public final void testRemoveRemoteFlowEntrySuccessNormally() {
996 fail("not yet implemented");
997 }
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -0700998}