blob: 5add3cdc94b11fa5629421787d27ecbefc4f8fac [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;
14import net.floodlightcontroller.core.module.FloodlightModuleContext;
15import net.floodlightcontroller.core.module.IFloodlightService;
16import net.floodlightcontroller.restserver.IRestApiService;
17import net.onrc.onos.graph.GraphDBOperation;
18import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
19import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
20import net.onrc.onos.ofcontroller.core.INetMapTopologyService.ITopoRouteService;
21import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
22import net.onrc.onos.ofcontroller.util.*;
23
24import org.easymock.EasyMock;
25import org.easymock.IAnswer;
26import org.junit.After;
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.powermock.core.classloader.annotations.PrepareForTest;
31import org.powermock.modules.junit4.PowerMockRunner;
32
33/**
34 * @author Toshio Koide
35 */
36@RunWith(PowerMockRunner.class)
37@PrepareForTest({FlowManager.class, GraphDBOperation.class, System.class, Executors.class})
38public class FlowManagerTest {
39 private static FloodlightModuleContext context;
40 private static IFloodlightProviderService floodlightProvider;
41 private static ITopoRouteService topoRouteService;
42 private static IRestApiService restApi;
43 private static GraphDBOperation op;
44
45 /**
46 * @throws java.lang.Exception
47 */
48 @Before
49 public void setUp() throws Exception {
50 }
51
52 /**
53 * @throws java.lang.Exception
54 */
55 @After
56 public void tearDown() throws Exception {
57 }
58
59 /**
60 * @throws java.lang.Exception
61 */
62 private void expectInitWithContext() throws Exception {
63 // create mock objects
64 context = createMock(FloodlightModuleContext.class);
65 floodlightProvider = createMock(IFloodlightProviderService.class);
66 topoRouteService = createMock(ITopoRouteService.class);
67 restApi = createMock(IRestApiService.class);
68 op = createMock(GraphDBOperation.class);
69
70 // setup expectations
71 expect(context.getServiceImpl(IFloodlightProviderService.class)).andReturn(floodlightProvider);
72 expect(context.getServiceImpl(ITopoRouteService.class)).andReturn(topoRouteService);
73 expect(context.getServiceImpl(IRestApiService.class)).andReturn(restApi);
74 expectNew(GraphDBOperation.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(op);
75 }
76
77 private IFlowPath createIFlowPathMock(long flowId, String installerID,
78 long srcDpid, int srcPort, long dstDpid, int dstPort) {
79 IFlowPath iFlowPath = createNiceMock(IFlowPath.class);
80 expect(iFlowPath.getFlowId()).andReturn(new FlowId(flowId).toString()).anyTimes();
81 expect(iFlowPath.getInstallerId()).andReturn(installerID).anyTimes();
82 expect(iFlowPath.getSrcSwitch()).andReturn(new Dpid(srcDpid).toString()).anyTimes();
83 expect(iFlowPath.getSrcPort()).andReturn(new Short((short)srcPort)).anyTimes();
84 expect(iFlowPath.getDstSwitch()).andReturn(new Dpid(dstDpid).toString()).anyTimes();
85 expect(iFlowPath.getDstPort()).andReturn(new Short((short)dstPort)).anyTimes();
86 expect(iFlowPath.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
87 return iFlowPath;
88 }
89
90 private FlowPath createTestFlowPath(
91 long flowId,
92 String installerId,
93 final long srcDpid, final int srcPort,
94 final long dstDpid, final int dstPort
95 ) {
96 FlowPath flowPath = new FlowPath();
97 flowPath.setFlowId(new FlowId(flowId));
98 flowPath.setInstallerId(new CallerId(installerId));
99 flowPath.setDataPath(new DataPath() {{
100 setSrcPort(new SwitchPort(new Dpid(srcDpid), new Port((short)srcPort)));
101 setDstPort(new SwitchPort(new Dpid(dstDpid), new Port((short)dstPort)));
102 }});
103 flowPath.setFlowEntryMatch(new FlowEntryMatch());
104 return flowPath;
105 }
106
107 private ArrayList<FlowPath> createTestFlowPaths() {
108 FlowPath flowPath1 = createTestFlowPath(1, "foo caller id", 1, 1, 2, 2);
109 FlowPath flowPath2 = createTestFlowPath(2, "caller id", 1, 1, 2, 2);
110 FlowPath flowPath3 = createTestFlowPath(3, "caller id", 1, 5, 2, 2);
111
112 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
113 flowPaths.add(flowPath1);
114 flowPaths.add(flowPath2);
115 flowPaths.add(flowPath3);
116
117 return flowPaths;
118 }
119
120
121 // IFlowService methods
122
123
124 /**
125 * Test method for {@link FlowManager#addFlow(FlowPath, FlowId, String)}.
126 * @throws Exception
127 */
128 @Test
129 public final void testAddFlowFailGraphCreatesNoFlow() throws Exception {
130 // instantiate required objects
131 FlowId flowId = new FlowId(123);
132 FlowPath flowPath = new FlowPath();
133 flowPath.setFlowId(flowId);
134
135 // setup expectations
136 expectInitWithContext();
137 expect(op.searchFlowPath(flowId)).andReturn(null);
138 expect(op.newFlowPath()).andReturn(null);
139 op.rollback();
140
141 // start the test
142 replayAll();
143// replay(GraphDBOperation.class);
144
145 FlowManager fm = new FlowManager();
146 fm.init(context);
147 Boolean result = fm.addFlow(flowPath, flowId, "");
148
149 // verify the test
150 verifyAll();
151 assertFalse(result);
152 }
153
154 /**
155 * Test method for {@link FlowManager#addFlow(FlowPath, FlowId, String)}.
156 * @throws Exception
157 */
158 @Test
159 public final void testAddFlowSuccessNormally() throws Exception {
160 final String addFlowEntry = "addFlowEntry";
161 // create mock objects
162 IFlowPath createdFlowPath = createNiceMock(IFlowPath.class);
163 IFlowEntry createdFlowEntry1 = createNiceMock(IFlowEntry.class);
164 IFlowEntry createdFlowEntry2 = createNiceMock(IFlowEntry.class);
165 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
166
167 // instantiate required objects
168 final FlowEntry flowEntry1 = new FlowEntry();
169 final FlowEntry flowEntry2 = new FlowEntry();
170 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
171 flowEntries.add(flowEntry1);
172 flowEntries.add(flowEntry2);
173
174 DataPath dataPath = new DataPath();
175 dataPath.setSrcPort(new SwitchPort(new Dpid(0x1234), new Port((short)1)));
176 dataPath.setDstPort(new SwitchPort(new Dpid(0x5678), new Port((short)2)));
177 dataPath.setFlowEntries(flowEntries);
178
179 FlowEntryMatch match = new FlowEntryMatch();
180
181 FlowPath flowPath = new FlowPath();
182 flowPath.setFlowId(new FlowId(0x100));
183 flowPath.setInstallerId(new CallerId("installer id"));
184 flowPath.setDataPath(dataPath);
185 flowPath.setFlowEntryMatch(match);
186
187 // setup expectations
188 expectInitWithContext();
189 expect(op.searchFlowPath(cmpEq(new FlowId(0x100)))).andReturn(null);
190 expect(op.newFlowPath()).andReturn(createdFlowPath);
191 createdFlowPath.setFlowId("0x100");
192 createdFlowPath.setType("flow");
193 createdFlowPath.setInstallerId("installer id");
194 createdFlowPath.setSrcSwitch("00:00:00:00:00:00:12:34");
195 createdFlowPath.setSrcPort(new Short((short)1));
196 createdFlowPath.setDstSwitch("00:00:00:00:00:00:56:78");
197 createdFlowPath.setDstPort(new Short((short)2));
198 createdFlowPath.setDataPathSummary("data path summary");
199 createdFlowPath.setUserState("FE_USER_ADD");
200
201 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry1)
202 .andReturn(createdFlowEntry1);
203 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry2)
204 .andReturn(createdFlowEntry2);
205
206 op.commit();
207
208 // start the test
209 replayAll();
210
211 fm.init(context);
212 Boolean result = fm.addFlow(flowPath, new FlowId(0x100), "data path summary");
213
214 // verify the test
215 verifyAll();
216 assertTrue(result);
217 }
218
219 /**
220 * Test method for {@link FlowManager#deleteAllFlows()}.
221 * @throws Exception
222 */
223 @Test
224 public final void testDeleteAllFlowsSuccessNormally() throws Exception {
225 // create mock objects
226 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
227 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
228
229 // instantiate required objects
230 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
231 flowPaths.add(flowPath1);
232 flowPaths.add(flowPath2);
233
234 // setup expectations
235 expectInitWithContext();
236 expect(op.getAllFlowPaths()).andReturn(flowPaths);
237
238 expect(flowPath1.getFlowId()).andReturn("1").anyTimes();
239 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowPath1);
240 expect(flowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
241 op.removeFlowPath(flowPath1);
242
243 expect(flowPath2.getFlowId()).andReturn("2").anyTimes();
244 expect(op.searchFlowPath(cmpEq(new FlowId(2)))).andReturn(flowPath2);
245 expect(flowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
246 op.removeFlowPath(flowPath2);
247
248 op.commit();
249 expectLastCall().anyTimes();
250
251 // start the test
252 replayAll();
253
254 FlowManager fm = new FlowManager();
255 fm.init(context);
256 Boolean result = fm.deleteAllFlows();
257
258 // verify the test
259 verifyAll();
260 assertTrue(result);
261 }
262
263 /**
264 * Test method for {@link FlowManager#deleteFlow(FlowId)}.
265 * @throws Exception
266 */
267 @Test
268 public final void testDeleteFlowSuccessEmptyFlowPath() throws Exception {
269 // create mock objects
270 IFlowPath flowObj = createNiceMock(IFlowPath.class);
271
272 // setup expectations
273 expectInitWithContext();
274 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowObj);
275 expect(flowObj.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
276 op.removeFlowPath(flowObj);
277 op.commit();
278 expectLastCall().anyTimes();
279
280 // start the test
281 replayAll();
282
283 FlowManager fm = new FlowManager();
284 fm.init(context);
285 Boolean result = fm.deleteFlow(new FlowId(1));
286
287 // verify the test
288 verifyAll();
289 assertTrue(result);
290 }
291
292 /**
293 * Test method for {@link FlowManager#clearAllFlows()}.
294 * @throws Exception
295 */
296 @Test
297 public final void testClearAllFlowsSuccessNormally() throws Exception {
298 // create mock objects
299 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
300 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
301 IFlowPath flowPath3 = createNiceMock(IFlowPath.class);
302 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, "clearFlow");
303
304 // instantiate required objects
305 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
306 flowPaths.add(flowPath1);
307 flowPaths.add(flowPath2);
308 flowPaths.add(null);
309 flowPaths.add(flowPath3);
310
311 // setup expectations
312 expectInitWithContext();
313 expect(op.getAllFlowPaths()).andReturn(flowPaths);
314 expect(flowPath1.getFlowId()).andReturn(new FlowId(1).toString());
315 expect(flowPath2.getFlowId()).andReturn(null);
316 expect(flowPath3.getFlowId()).andReturn(new FlowId(3).toString());
317 expect(fm.clearFlow(cmpEq(new FlowId(1)))).andReturn(true);
318 expect(fm.clearFlow(cmpEq(new FlowId(3)))).andReturn(true);
319
320 // start the test
321 replayAll();
322
323 fm.init(context);
324 Boolean result = fm.clearAllFlows();
325
326 //verify the test
327 verifyAll();
328 assertTrue(result);
329 }
330
331 /**
332 * Test method for {@link FlowManager#getFlow()}.
333 * @throws Exception
334 */
335 @Test
336 public final void testGetFlowSuccessNormally() throws Exception {
337 // instantiate required objects
338 FlowManager fm = new FlowManager();
339
340 // setup expectations
341 expectInitWithContext();
342 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(
343 createIFlowPathMock(1, "caller id", 1, 1, 2, 2));
344 op.commit();
345
346 // start the test
347 replayAll();
348
349 fm.init(context);
350 String result = fm.getFlow(new FlowId(1)).installerId().toString();
351
352 //verify the test
353 verifyAll();
354 assertEquals("caller id", result);
355 }
356
357 /**
358 * Test method for {@link FlowManager#getAllFlows(CallerId, DataPathEndpoints)}.
359 * @throws Exception
360 */
361 @Test
362 public final void testGetAllFlowsWithCallerIdAndDataPathEndpointsSuccessNormally() throws Exception {
363 final String getAllFlows = "getAllFlows";
364 // create mock objects
365 FlowManager fm = createPartialMock(FlowManager.class, getAllFlows,
366 new Class<?>[]{}, new Object[]{});
367
368 // instantiate required objects
369 DataPathEndpoints dataPathEndpoints = new DataPathEndpoints(
370 new SwitchPort(new Dpid(1), new Port((short)1)),
371 new SwitchPort(new Dpid(2), new Port((short)2)));
372
373 ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths();
374
375 //setup expectations
376 expectInitWithContext();
377 expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows);
378
379 //start the test
380 replayAll();
381
382 fm.init(context);
383 ArrayList<FlowPath> flows = fm.getAllFlows(new CallerId("caller id"), dataPathEndpoints);
384
385 // verify the test
386 verifyAll();
387 assertEquals(1, flows.size());
388 assertEquals(obtainedAllFlows.get(1), flows.get(0));
389 }
390
391 /**
392 * Test method for {@link FlowManager#getAllFlows(DataPathEndpoints)}.
393 * @throws Exception
394 */
395 @Test
396 public final void testGetAllFlowsWithDataPathEndpointsSuccessNormally() throws Exception {
397 final String getAllFlows = "getAllFlows";
398 // create mock objects
399 FlowManager fm = createPartialMock(FlowManager.class, getAllFlows,
400 new Class<?>[]{}, new Object[]{});
401
402 // instantiate required objects
403 DataPathEndpoints dataPathEndpoints = new DataPathEndpoints(
404 new SwitchPort(new Dpid(1), new Port((short)1)),
405 new SwitchPort(new Dpid(2), new Port((short)2)));
406
407 ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths();
408
409 //setup expectations
410 expectInitWithContext();
411 expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows);
412
413 //start the test
414 replayAll();
415
416 fm.init(context);
417 ArrayList<FlowPath> flows = fm.getAllFlows(dataPathEndpoints);
418
419 // verify the test
420 verifyAll();
421 assertEquals(2, flows.size());
422 assertEquals(obtainedAllFlows.get(0), flows.get(0));
423 assertEquals(obtainedAllFlows.get(1), flows.get(1));
424 // TODO: ignore the order of flows in the list
425 }
426
427 /**
428 * Test method for {@link FlowManager#getAllFlowsSummary(FlowId, int)}.
429 * @throws Exception
430 */
431 @Test
432 public final void testGetAllFlowsSummarySuccessNormally() throws Exception {
433 final String getAllFlowsWithoutFlowEntries = "getAllFlowsWithoutFlowEntries";
434 // create mock objects
435 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, getAllFlowsWithoutFlowEntries);
436 IFlowPath flowPath1 = createIFlowPathMock(1, "", 1, 2, 3, 4);
437 IFlowPath flowPath2 = createIFlowPathMock(5, "", 2, 3, 4, 5);
438 IFlowPath flowPath3 = createIFlowPathMock(10, "", 3, 4, 5, 6);
439
440 // instantiate required objects
441 ArrayList<IFlowPath> flows = new ArrayList<IFlowPath>();
442 flows.add(flowPath3);
443 flows.add(flowPath1);
444 flows.add(flowPath2);
445
446 // setup expectations
447 expectInitWithContext();
448 expectPrivate(fm, getAllFlowsWithoutFlowEntries).andReturn(flows);
449
450 // start the test
451 replayAll();
452
453 fm.init(context);
454 ArrayList<IFlowPath> returnedFlows = fm.getAllFlowsSummary(null, 0);
455
456 // verify the test
457 verifyAll();
458 assertEquals(3, returnedFlows.size());
459 assertEquals(1, new FlowId(returnedFlows.get(0).getFlowId()).value());
460 assertEquals(5, new FlowId(returnedFlows.get(1).getFlowId()).value());
461 assertEquals(10, new FlowId(returnedFlows.get(2).getFlowId()).value());
462 }
463
464 /**
465 * Test method for {@link FlowManager#getAllFlows()}.
466 * @throws Exception
467 */
468 @Test
469 public final void testGetAllFlowsSuccessNormally() throws Exception {
470 // instantiate required objects
471 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
472 flowPaths.add(createIFlowPathMock(1, "caller id", 1, 1, 2, 2));
473 flowPaths.add(createIFlowPathMock(1, "caller id", 2, 5, 3, 5));
474 FlowManager fm = new FlowManager();
475
476 // setup expectations
477 expectInitWithContext();
478 expect(op.getAllFlowPaths()).andReturn(flowPaths);
479 op.commit();
480
481 // start the test
482 replayAll();
483
484 fm.init(context);
485 ArrayList<FlowPath> flows = fm.getAllFlows();
486
487 // verify the test
488 verifyAll();
489 assertEquals(2, flows.size());
490 assertEquals(new SwitchPort(new Dpid(1), new Port((short)1)).toString(),
491 flows.get(0).dataPath().srcPort().toString());
492 assertEquals(new SwitchPort(new Dpid(2), new Port((short)5)).toString(),
493 flows.get(1).dataPath().srcPort().toString());
494 // TODO: more asserts
495 // TODO: ignore seq. of the list
496 }
497
498 /**
499 * Test method for {@link FlowManager#addAndMaintainShortestPathFlow(FlowPath)}.
500 * @throws Exception
501 */
502 @Test
503 public final void testAddAndMaintainShortestPathFlowSuccessNormally() throws Exception {
504 final String addFlow = "addFlow";
505
506 // create mock objects
507 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow);
508
509 // instantiate required objects
510 DataPath dataPath = new DataPath();
511 dataPath.setSrcPort(new SwitchPort(new Dpid(1), new Port((short)3)));
512 dataPath.setDstPort(new SwitchPort(new Dpid(2), new Port((short)4)));
513 FlowEntryMatch match = new FlowEntryMatch();
514 FlowPath paramFlow = new FlowPath();
515 paramFlow.setFlowId(new FlowId(100));
516 paramFlow.setInstallerId(new CallerId("installer id"));
517 paramFlow.setDataPath(dataPath);
518 paramFlow.setFlowEntryMatch(match);
519
520 // setup expectations
521 expectInitWithContext();
522 expectPrivate(fm, addFlow,
523 EasyMock.anyObject(FlowPath.class),
524 EasyMock.anyObject(FlowId.class),
525 EasyMock.anyObject(String.class)
526 ).andAnswer(new IAnswer<Object>() {
527 public Object answer() throws Exception {
528 FlowPath flowPath = (FlowPath)EasyMock.getCurrentArguments()[0];
529 assertEquals(flowPath.flowId().value(), 100);
530 assertEquals(flowPath.installerId().toString(), "installer id");
531 assertEquals(flowPath.dataPath().srcPort().toString(),
532 new SwitchPort(new Dpid(1), new Port((short)3)).toString());
533
534 String dataPathSummary = (String)EasyMock.getCurrentArguments()[2];
535 assertEquals(dataPathSummary, "X");
536
537 return true;
538 }
539 });
540
541 // start the test
542 replayAll();
543
544 fm.init(context);
545 FlowPath resultFlow = fm.addAndMaintainShortestPathFlow(paramFlow);
546
547 // verify the test
548 verifyAll();
549 assertEquals(paramFlow.flowId().value(), resultFlow.flowId().value());
550 assertEquals(paramFlow.installerId().toString(), resultFlow.installerId().toString());
551 assertEquals(paramFlow.dataPath().toString(), resultFlow.dataPath().toString());
552 assertEquals(paramFlow.flowEntryMatch().toString(), resultFlow.flowEntryMatch().toString());
553 }
554
555 /**
556 * Test method for {@link FlowManager#measurementStorePathFlow(FlowPath)}.
557 * @throws Exception
558 */
559 @Test
560 public final void testMeasurementStorePathFlowSuccessNormally() throws Exception {
561 // instantiate required objects
562 FlowPath paramFlow = createTestFlowPath(100, "installer id", 1, 3, 2, 4);
563 Map<Long, Object> shortestPathMap = new HashMap<Long, Object>();
564 FlowManager fm = new FlowManager();
565
566 // setup expectations
567 expectInitWithContext();
568 expect((Map<Long,Object>)topoRouteService.prepareShortestPathTopo()
569 ).andReturn(shortestPathMap);
570 expect(topoRouteService.getTopoShortestPath(
571 shortestPathMap,
572 paramFlow.dataPath().srcPort(),
573 paramFlow.dataPath().dstPort())).andReturn(null);
574
575 // start the test
576 replayAll();
577
578 fm.init(context);
579 FlowPath resultFlowPath = fm.measurementStorePathFlow(paramFlow);
580
581 // verify the test
582 verifyAll();
583 assertEquals(paramFlow.flowId().value(), resultFlowPath.flowId().value());
584 assertEquals(paramFlow.installerId().toString(), resultFlowPath.installerId().toString());
585 assertEquals(paramFlow.dataPath().toString(), resultFlowPath.dataPath().toString());
586 assertEquals(paramFlow.flowEntryMatch().toString(), resultFlowPath.flowEntryMatch().toString());
587 }
588
589 /**
590 * Test method for {@link FlowManager#measurementInstallPaths(Integer)}.
591 * @throws Exception
592 */
593 @Test
594 public final void testMeasurementInstallPathsSuccessNormally() throws Exception {
595 final String addFlow = "addFlow";
596
597 // create mock objects
598 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow);
599
600 // instantiate required objects
601 FlowPath flow1 = createTestFlowPath(1, "installer id", 1, 2, 3, 4);
602 FlowPath flow2 = createTestFlowPath(2, "installer id", 2, 3, 4, 5);
603 FlowPath flow3 = createTestFlowPath(3, "installer id", 3, 4, 5, 6);
604 Map<Long, Object> shortestPathMap = new HashMap<Long, Object>();
605
606 // setup expectations
607 expectInitWithContext();
608 expect((Map<Long,Object>)topoRouteService.prepareShortestPathTopo()
609 ).andReturn(shortestPathMap);
610
611 expect(topoRouteService.getTopoShortestPath(
612 shortestPathMap,
613 flow1.dataPath().srcPort(),
614 flow1.dataPath().dstPort())).andReturn(null);
615
616 expect(topoRouteService.getTopoShortestPath(
617 shortestPathMap,
618 flow2.dataPath().srcPort(),
619 flow2.dataPath().dstPort())).andReturn(null);
620
621 expect(topoRouteService.getTopoShortestPath(
622 shortestPathMap,
623 flow3.dataPath().srcPort(),
624 flow3.dataPath().dstPort())).andReturn(null);
625
626 expectPrivate(fm, addFlow,
627 EasyMock.cmpEq(flow1),
628 EasyMock.anyObject(FlowId.class),
629 EasyMock.anyObject(String.class)).andReturn(true);
630
631 expectPrivate(fm, addFlow,
632 EasyMock.cmpEq(flow2),
633 EasyMock.anyObject(FlowId.class),
634 EasyMock.anyObject(String.class)).andReturn(true);
635
636 expectPrivate(fm, addFlow,
637 EasyMock.cmpEq(flow3),
638 EasyMock.anyObject(FlowId.class),
639 EasyMock.anyObject(String.class)).andReturn(true);
640
641 // start the test
642 replayAll();
643
644 fm.init(context);
645 fm.measurementStorePathFlow(flow1);
646 fm.measurementStorePathFlow(flow2);
647 fm.measurementStorePathFlow(flow3);
648 Boolean result = fm.measurementInstallPaths(3);
649
650 // verify the test
651 verifyAll();
652 assertTrue(result);
653 }
654
655 /**
656 * Test method for {@link FlowManager#measurementGetInstallPathsTimeNsec()}.
657 * @throws Exception
658 */
659 @Test
660 public final void testMeasurementGetInstallPathsTimeNsecSuccessNormally() throws Exception {
661 final String addFlow = "addFlow";
662
663 // create mock objects
664 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow);
665 mockStaticPartial(System.class, "nanoTime");
666
667 // instantiate required objects
668 FlowPath flow1 = createTestFlowPath(1, "installer id", 1, 2, 3, 4);
669 Map<Long, Object> shortestPathMap = new HashMap<Long, Object>();
670
671 // setup expectations
672 expectInitWithContext();
673 expect(System.nanoTime()).andReturn(new Long(100000));
674 expect(System.nanoTime()).andReturn(new Long(110000));
675 expect((Map<Long,Object>)topoRouteService.prepareShortestPathTopo()
676 ).andReturn(shortestPathMap);
677 expect(topoRouteService.getTopoShortestPath(
678 shortestPathMap,
679 flow1.dataPath().srcPort(),
680 flow1.dataPath().dstPort())).andReturn(null);
681 expectPrivate(fm, addFlow,
682 EasyMock.cmpEq(flow1),
683 EasyMock.anyObject(FlowId.class),
684 EasyMock.anyObject(String.class)).andReturn(true);
685
686 // start the test
687 replayAll();
688
689 fm.init(context);
690 fm.measurementStorePathFlow(flow1).toString();
691 fm.measurementInstallPaths(1);
692 Long result = fm.measurementGetInstallPathsTimeNsec();
693
694 // verify the test
695 verifyAll();
696 assertEquals(new Long(10000), result);
697 }
698
699 /**
700 * Test method for {@link FlowManager#measurementGetPerFlowInstallTime()}.
701 * @throws Exception
702 */
703 @Test
704 public final void testMeasurementGetPerFlowInstallTimeSuccessNormally() throws Exception {
705 final String addFlow = "addFlow";
706
707 // create mock objects
708 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow);
709
710 // instantiate required objects
711 FlowPath flow1 = createTestFlowPath(1, "installer id", 1, 2, 3, 4);
712 Map<Long, Object> shortestPathMap = new HashMap<Long, Object>();
713
714 // setup expectations
715 expectInitWithContext();
716 expect((Map<Long,Object>)topoRouteService.prepareShortestPathTopo()
717 ).andReturn(shortestPathMap);
718
719 expect(topoRouteService.getTopoShortestPath(
720 shortestPathMap,
721 flow1.dataPath().srcPort(),
722 flow1.dataPath().dstPort())).andReturn(null);
723
724 expectPrivate(fm, addFlow,
725 EasyMock.cmpEq(flow1),
726 EasyMock.anyObject(FlowId.class),
727 EasyMock.anyObject(String.class)).andReturn(true);
728
729
730 // start the test
731 replayAll();
732
733 fm.init(context);
734 fm.measurementStorePathFlow(flow1);
735 fm.measurementInstallPaths(10);
736 String result = fm.measurementGetPerFlowInstallTime();
737
738 // verify the test
739 verifyAll();
740 assertTrue(result.startsWith("ThreadAndTimePerFlow"));
741 }
742
743 /**
744 * Test method for {@link FlowManager#measurementClearAllPaths()}.
745 * @throws Exception
746 */
747 @Test
748 public final void testMeasurementClearAllPathsSuccessNormally() throws Exception {
749 // instantiate required objects
750 FlowPath paramFlow = createTestFlowPath(100, "installer id", 1, 3, 2, 4);
751 Map<Long, Object> shortestPathMap = new HashMap<Long, Object>();
752
753 // setup expectations
754 expectInitWithContext();
755 expect((Map<Long,Object>)topoRouteService.prepareShortestPathTopo()
756 ).andReturn(shortestPathMap);
757 expect(topoRouteService.getTopoShortestPath(
758 shortestPathMap,
759 paramFlow.dataPath().srcPort(),
760 paramFlow.dataPath().dstPort())).andReturn(null);
761 topoRouteService.dropShortestPathTopo(shortestPathMap);
762
763 // start the test
764 replayAll();
765
766 FlowManager fm = new FlowManager();
767 fm.init(context);
768 fm.measurementStorePathFlow(paramFlow);
769 Boolean result = fm.measurementClearAllPaths();
770
771 // verify the test
772 verifyAll();
773 assertTrue(result);
774 assertEquals(new Long(0), fm.measurementGetInstallPathsTimeNsec());
775 assertEquals("", fm.measurementGetPerFlowInstallTime());
776 }
777
778
779 // INetMapStorage methods
780
781
782 /**
783 * Test method for {@link FlowManager#init(String)}.
784 * @throws Exception
785 */
786 @Test
787 public final void testInitSuccessNormally() throws Exception {
788 // create mock objects
789 op = createMock(GraphDBOperation.class);
790
791 // setup expectations
792 expectNew(GraphDBOperation.class, "/dummy/path").andReturn(op);
793
794 // start the test
795 replayAll();
796
797 FlowManager fm = new FlowManager();
798 fm.init("/dummy/path");
799
800 // verify the test
801 verifyAll();
802 }
803
804 /**
805 * Test method for {@link FlowManager#close()}.
806 * @throws Exception
807 */
808 @Test
809 public final void testCloseSuccessNormally() throws Exception {
810 // instantiate required objects
811 FlowManager fm = new FlowManager();
812
813 // setup expectations
814 expectInitWithContext();
815 op.close();
816
817 // start the test
818 replayAll();
819
820 fm.init(context);
821 fm.close();
822
823 // verify the test
824 verifyAll();
825 }
826
827
828 // IFloodlightModule methods
829
830
831 /**
832 * Test method for {@link FlowManager#getModuleServices()}.
833 * @throws Exception
834 */
835 @Test
836 public final void testGetModuleServicesSuccessNormally() throws Exception {
837 // instantiate required objects
838 FlowManager fm = new FlowManager();
839
840 // setup expectations
841 expectInitWithContext();
842
843 // start the test
844 replayAll();
845
846 fm.init(context);
847 Collection<Class<? extends IFloodlightService>> l = fm.getModuleServices();
848
849 // verify the test
850 verifyAll();
851 assertEquals(1, l.size());
852 assertEquals(IFlowService.class, l.iterator().next());
853 }
854
855 /**
856 * Test method for {@link FlowManager#getServiceImpls()}.
857 * @throws Exception
858 */
859 @Test
860 public final void testGetServiceImplsSuccessNormally() throws Exception {
861 // instantiate required objects
862 FlowManager fm = new FlowManager();
863
864 // setup expectations
865 expectInitWithContext();
866
867 // start the test
868 replayAll();
869
870 fm.init(context);
871 Map<Class<? extends IFloodlightService>, IFloodlightService> si = fm.getServiceImpls();
872
873 // verify the test
874 verifyAll();
875 assertEquals(1, si.size());
876 assertTrue(si.containsKey(IFlowService.class));
877 assertEquals(fm, si.get(IFlowService.class));
878 }
879
880 /**
881 * Test method for {@link FlowManager#getModuleDependencies()}.
882 * @throws Exception
883 */
884 @Test
885 public final void testGetModuleDependenciesSuccessNormally() throws Exception {
886 // instantiate required objects
887 FlowManager fm = new FlowManager();
888
889 // setup expectations
890 expectInitWithContext();
891
892 // start the test
893 replayAll();
894
895 fm.init(context);
896 Collection<Class<? extends IFloodlightService>> md = fm.getModuleDependencies();
897
898 // verify the test
899 verifyAll();
900 assertEquals(3, md.size());
901 assertTrue(md.contains(IFloodlightProviderService.class));
902 assertTrue(md.contains(ITopoRouteService.class));
903 assertTrue(md.contains(IRestApiService.class));
904 }
905
906 /**
907 * Test method for {@link FlowManager#init(FloodlightModuleContext)}.
908 * @throws Exception
909 */
910 @Test
911 public final void testInitWithFloodlightModuleContextSuccessNormally() throws Exception {
912 // instantiate required objects
913 FlowManager fm = new FlowManager();
914
915 // setup expectations
916 expectInitWithContext();
917
918 // start the test
919 replayAll();
920
921 fm.init(context);
922
923 // verify the test
924 verifyAll();
925 }
926
927 /**
928 * Test method for {@link FlowManager#startUp(FloodlightModuleContext)}.
929 * @throws Exception
930 */
931 @Test
932 public final void testStartupSuccessNormally() throws Exception {
933 // create mock objects
934 mockStaticPartial(Executors.class, "newScheduledThreadPool");
935 ScheduledExecutorService scheduler = createMock(ScheduledExecutorService.class);
936
937
938 // instantiate required objects
939 FlowManager fm = new FlowManager();
940
941 // setup expectations
942 expectInitWithContext();
943 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
944 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
945 expect(scheduler.scheduleAtFixedRate(
946 EasyMock.anyObject(Runnable.class),
947 EasyMock.anyLong(),
948 EasyMock.anyLong(),
949 EasyMock.anyObject(TimeUnit.class))).andReturn(null).times(2);
950 restApi.addRestletRoutable(EasyMock.anyObject(FlowWebRoutable.class));
951
952 // start the test
953 replayAll();
954
955 fm.init(context);
956 fm.startUp(context);
957
958 // verify the test
959 verifyAll();
960 }
961}