blob: ea09c17067445797ae25d337a11ad84ea7613102 [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
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -07008import java.lang.reflect.Method;
Toshio Koidefe2625e2013-06-26 13:59:53 -07009import java.util.*;
10import java.util.concurrent.Executors;
11import java.util.concurrent.ScheduledExecutorService;
12import java.util.concurrent.TimeUnit;
13
14import net.floodlightcontroller.core.IFloodlightProviderService;
Toshio Koideca7abe02013-06-27 17:30:17 -070015import net.floodlightcontroller.core.IOFSwitch;
Toshio Koidefe2625e2013-06-26 13:59:53 -070016import net.floodlightcontroller.core.module.FloodlightModuleContext;
17import net.floodlightcontroller.core.module.IFloodlightService;
18import net.floodlightcontroller.restserver.IRestApiService;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070019import net.onrc.onos.datagrid.IDatagridService;
Toshio Koidefe2625e2013-06-26 13:59:53 -070020import net.onrc.onos.graph.GraphDBOperation;
21import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry;
22import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath;
Toshio Koidefe2625e2013-06-26 13:59:53 -070023import net.onrc.onos.ofcontroller.flowmanager.web.FlowWebRoutable;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070024import net.onrc.onos.ofcontroller.topology.ITopologyNetService;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070025import net.onrc.onos.ofcontroller.topology.TopologyManager;
Toshio Koidefe2625e2013-06-26 13:59:53 -070026import net.onrc.onos.ofcontroller.util.*;
27
28import org.easymock.EasyMock;
29import org.easymock.IAnswer;
30import org.junit.After;
31import org.junit.Before;
Toshio Koideca7abe02013-06-27 17:30:17 -070032import org.junit.Ignore;
Toshio Koidefe2625e2013-06-26 13:59:53 -070033import org.junit.Test;
34import org.junit.runner.RunWith;
Toshio Koideca7abe02013-06-27 17:30:17 -070035import org.openflow.protocol.OFFlowMod;
36import org.openflow.protocol.OFType;
37import org.openflow.protocol.factory.BasicFactory;
Toshio Koidefe2625e2013-06-26 13:59:53 -070038import org.powermock.core.classloader.annotations.PrepareForTest;
39import org.powermock.modules.junit4.PowerMockRunner;
40
41/**
42 * @author Toshio Koide
43 */
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070044@Ignore
Toshio Koidefe2625e2013-06-26 13:59:53 -070045@RunWith(PowerMockRunner.class)
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070046@PrepareForTest({FlowManager.class, FlowDatabaseOperation.class, GraphDBOperation.class, System.class, Executors.class})
Toshio Koidefe2625e2013-06-26 13:59:53 -070047public class FlowManagerTest {
48 private static FloodlightModuleContext context;
49 private static IFloodlightProviderService floodlightProvider;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070050 private static TopologyManager topologyManager;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070051 private static IDatagridService datagridService;
Toshio Koidefe2625e2013-06-26 13:59:53 -070052 private static IRestApiService restApi;
53 private static GraphDBOperation op;
54
55 /**
56 * @throws java.lang.Exception
57 */
58 @Before
59 public void setUp() throws Exception {
60 }
61
62 /**
63 * @throws java.lang.Exception
64 */
65 @After
66 public void tearDown() throws Exception {
67 }
68
69 /**
70 * @throws java.lang.Exception
71 */
72 private void expectInitWithContext() throws Exception {
73 // create mock objects
74 context = createMock(FloodlightModuleContext.class);
75 floodlightProvider = createMock(IFloodlightProviderService.class);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070076 topologyManager = createMock(TopologyManager.class);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070077 datagridService = createMock(IDatagridService.class);
Toshio Koidefe2625e2013-06-26 13:59:53 -070078 restApi = createMock(IRestApiService.class);
79 op = createMock(GraphDBOperation.class);
80
81 // setup expectations
82 expect(context.getServiceImpl(IFloodlightProviderService.class)).andReturn(floodlightProvider);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070083 expect(context.getServiceImpl(ITopologyNetService.class)).andReturn(topologyManager);
84 expect(context.getServiceImpl(IDatagridService.class)).andReturn(datagridService);
Toshio Koidefe2625e2013-06-26 13:59:53 -070085 expect(context.getServiceImpl(IRestApiService.class)).andReturn(restApi);
86 expectNew(GraphDBOperation.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(op);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070087 expectNew(TopologyManager.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(topologyManager);
Toshio Koidefe2625e2013-06-26 13:59:53 -070088 }
89
90 private IFlowPath createIFlowPathMock(long flowId, String installerID,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070091 long flowPathFlags,
Toshio Koidefe2625e2013-06-26 13:59:53 -070092 long srcDpid, int srcPort, long dstDpid, int dstPort) {
93 IFlowPath iFlowPath = createNiceMock(IFlowPath.class);
94 expect(iFlowPath.getFlowId()).andReturn(new FlowId(flowId).toString()).anyTimes();
95 expect(iFlowPath.getInstallerId()).andReturn(installerID).anyTimes();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070096 expect(iFlowPath.getFlowPathFlags()).andReturn(new Long(flowPathFlags)).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -070097 expect(iFlowPath.getSrcSwitch()).andReturn(new Dpid(srcDpid).toString()).anyTimes();
98 expect(iFlowPath.getSrcPort()).andReturn(new Short((short)srcPort)).anyTimes();
99 expect(iFlowPath.getDstSwitch()).andReturn(new Dpid(dstDpid).toString()).anyTimes();
100 expect(iFlowPath.getDstPort()).andReturn(new Short((short)dstPort)).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700101 return iFlowPath;
102 }
103
Toshio Koideca7abe02013-06-27 17:30:17 -0700104 private FlowPath createTestFlowPath(long flowId, String installerId,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700105 final long flowPathFlags,
Toshio Koidefe2625e2013-06-26 13:59:53 -0700106 final long srcDpid, final int srcPort,
107 final long dstDpid, final int dstPort
108 ) {
109 FlowPath flowPath = new FlowPath();
110 flowPath.setFlowId(new FlowId(flowId));
111 flowPath.setInstallerId(new CallerId(installerId));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700112 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700113 flowPath.setDataPath(new DataPath() {{
114 setSrcPort(new SwitchPort(new Dpid(srcDpid), new Port((short)srcPort)));
115 setDstPort(new SwitchPort(new Dpid(dstDpid), new Port((short)dstPort)));
116 }});
117 flowPath.setFlowEntryMatch(new FlowEntryMatch());
118 return flowPath;
119 }
120
121 private ArrayList<FlowPath> createTestFlowPaths() {
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700122 FlowPath flowPath1 = createTestFlowPath(1, "foo caller id", 0, 1, 1, 2, 2);
123 FlowPath flowPath2 = createTestFlowPath(2, "caller id", 0, 1, 1, 2, 2);
124 FlowPath flowPath3 = createTestFlowPath(3, "caller id", 0, 1, 5, 2, 2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700125
126 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
127 flowPaths.add(flowPath1);
128 flowPaths.add(flowPath2);
129 flowPaths.add(flowPath3);
130
131 return flowPaths;
132 }
133
134
135 // IFlowService methods
136
137
138 /**
139 * Test method for {@link FlowManager#addFlow(FlowPath, FlowId, String)}.
140 * @throws Exception
141 */
142 @Test
143 public final void testAddFlowFailGraphCreatesNoFlow() throws Exception {
144 // instantiate required objects
145 FlowId flowId = new FlowId(123);
146 FlowPath flowPath = new FlowPath();
147 flowPath.setFlowId(flowId);
Toshio Koideca7abe02013-06-27 17:30:17 -0700148 FlowManager fm = new FlowManager();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700149
150 // setup expectations
151 expectInitWithContext();
152 expect(op.searchFlowPath(flowId)).andReturn(null);
153 expect(op.newFlowPath()).andReturn(null);
154 op.rollback();
155
156 // start the test
157 replayAll();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700158
Toshio Koidefe2625e2013-06-26 13:59:53 -0700159 fm.init(context);
160 Boolean result = fm.addFlow(flowPath, flowId, "");
161
162 // verify the test
163 verifyAll();
164 assertFalse(result);
165 }
166
167 /**
168 * Test method for {@link FlowManager#addFlow(FlowPath, FlowId, String)}.
169 * @throws Exception
170 */
171 @Test
172 public final void testAddFlowSuccessNormally() throws Exception {
173 final String addFlowEntry = "addFlowEntry";
174 // create mock objects
175 IFlowPath createdFlowPath = createNiceMock(IFlowPath.class);
176 IFlowEntry createdFlowEntry1 = createNiceMock(IFlowEntry.class);
177 IFlowEntry createdFlowEntry2 = createNiceMock(IFlowEntry.class);
178 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
179
180 // instantiate required objects
181 final FlowEntry flowEntry1 = new FlowEntry();
182 final FlowEntry flowEntry2 = new FlowEntry();
183 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
184 flowEntries.add(flowEntry1);
185 flowEntries.add(flowEntry2);
186
187 DataPath dataPath = new DataPath();
188 dataPath.setSrcPort(new SwitchPort(new Dpid(0x1234), new Port((short)1)));
189 dataPath.setDstPort(new SwitchPort(new Dpid(0x5678), new Port((short)2)));
190 dataPath.setFlowEntries(flowEntries);
191
192 FlowEntryMatch match = new FlowEntryMatch();
193
194 FlowPath flowPath = new FlowPath();
195 flowPath.setFlowId(new FlowId(0x100));
196 flowPath.setInstallerId(new CallerId("installer id"));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700197 flowPath.setFlowPathFlags(new FlowPathFlags(0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700198 flowPath.setDataPath(dataPath);
199 flowPath.setFlowEntryMatch(match);
200
201 // setup expectations
202 expectInitWithContext();
203 expect(op.searchFlowPath(cmpEq(new FlowId(0x100)))).andReturn(null);
204 expect(op.newFlowPath()).andReturn(createdFlowPath);
205 createdFlowPath.setFlowId("0x100");
206 createdFlowPath.setType("flow");
207 createdFlowPath.setInstallerId("installer id");
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700208 createdFlowPath.setFlowPathFlags(new Long((long)0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700209 createdFlowPath.setSrcSwitch("00:00:00:00:00:00:12:34");
210 createdFlowPath.setSrcPort(new Short((short)1));
211 createdFlowPath.setDstSwitch("00:00:00:00:00:00:56:78");
212 createdFlowPath.setDstPort(new Short((short)2));
213 createdFlowPath.setDataPathSummary("data path summary");
214 createdFlowPath.setUserState("FE_USER_ADD");
215
216 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry1)
217 .andReturn(createdFlowEntry1);
218 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry2)
219 .andReturn(createdFlowEntry2);
220
221 op.commit();
222
223 // start the test
224 replayAll();
225
226 fm.init(context);
227 Boolean result = fm.addFlow(flowPath, new FlowId(0x100), "data path summary");
228
229 // verify the test
230 verifyAll();
231 assertTrue(result);
232 }
233
234 /**
235 * Test method for {@link FlowManager#deleteAllFlows()}.
236 * @throws Exception
237 */
238 @Test
239 public final void testDeleteAllFlowsSuccessNormally() throws Exception {
240 // create mock objects
241 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
242 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
243
244 // instantiate required objects
245 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
246 flowPaths.add(flowPath1);
247 flowPaths.add(flowPath2);
Toshio Koideca7abe02013-06-27 17:30:17 -0700248 FlowManager fm = new FlowManager();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700249
250 // setup expectations
251 expectInitWithContext();
252 expect(op.getAllFlowPaths()).andReturn(flowPaths);
253
254 expect(flowPath1.getFlowId()).andReturn("1").anyTimes();
255 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowPath1);
256 expect(flowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
257 op.removeFlowPath(flowPath1);
258
259 expect(flowPath2.getFlowId()).andReturn("2").anyTimes();
260 expect(op.searchFlowPath(cmpEq(new FlowId(2)))).andReturn(flowPath2);
261 expect(flowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
262 op.removeFlowPath(flowPath2);
263
264 op.commit();
265 expectLastCall().anyTimes();
266
267 // start the test
268 replayAll();
269
Toshio Koidefe2625e2013-06-26 13:59:53 -0700270 fm.init(context);
271 Boolean result = fm.deleteAllFlows();
272
273 // verify the test
274 verifyAll();
275 assertTrue(result);
276 }
277
278 /**
279 * Test method for {@link FlowManager#deleteFlow(FlowId)}.
280 * @throws Exception
281 */
282 @Test
283 public final void testDeleteFlowSuccessEmptyFlowPath() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700284 // instantiate required objects
285 FlowManager fm = new FlowManager();
286
Toshio Koidefe2625e2013-06-26 13:59:53 -0700287 // create mock objects
288 IFlowPath flowObj = createNiceMock(IFlowPath.class);
289
290 // setup expectations
291 expectInitWithContext();
292 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowObj);
293 expect(flowObj.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
294 op.removeFlowPath(flowObj);
295 op.commit();
296 expectLastCall().anyTimes();
297
298 // start the test
299 replayAll();
300
Toshio Koidefe2625e2013-06-26 13:59:53 -0700301 fm.init(context);
302 Boolean result = fm.deleteFlow(new FlowId(1));
303
304 // verify the test
305 verifyAll();
306 assertTrue(result);
307 }
308
309 /**
310 * Test method for {@link FlowManager#clearAllFlows()}.
311 * @throws Exception
312 */
313 @Test
314 public final void testClearAllFlowsSuccessNormally() throws Exception {
315 // create mock objects
316 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
317 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
318 IFlowPath flowPath3 = createNiceMock(IFlowPath.class);
319 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, "clearFlow");
320
321 // instantiate required objects
322 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
323 flowPaths.add(flowPath1);
324 flowPaths.add(flowPath2);
325 flowPaths.add(null);
326 flowPaths.add(flowPath3);
327
328 // setup expectations
329 expectInitWithContext();
330 expect(op.getAllFlowPaths()).andReturn(flowPaths);
331 expect(flowPath1.getFlowId()).andReturn(new FlowId(1).toString());
332 expect(flowPath2.getFlowId()).andReturn(null);
333 expect(flowPath3.getFlowId()).andReturn(new FlowId(3).toString());
334 expect(fm.clearFlow(cmpEq(new FlowId(1)))).andReturn(true);
335 expect(fm.clearFlow(cmpEq(new FlowId(3)))).andReturn(true);
336
337 // start the test
338 replayAll();
339
340 fm.init(context);
341 Boolean result = fm.clearAllFlows();
342
343 //verify the test
344 verifyAll();
345 assertTrue(result);
346 }
347
348 /**
349 * Test method for {@link FlowManager#getFlow()}.
350 * @throws Exception
351 */
352 @Test
353 public final void testGetFlowSuccessNormally() throws Exception {
354 // instantiate required objects
355 FlowManager fm = new FlowManager();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700356 IFlowPath iFlowPath = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700357
358 // setup expectations
359 expectInitWithContext();
Toshio Koideca7abe02013-06-27 17:30:17 -0700360 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(iFlowPath);
361 expect(iFlowPath.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700362 op.commit();
363
364 // start the test
365 replayAll();
366
367 fm.init(context);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700368 FlowPath flowPath = fm.getFlow(new FlowId(1));
369 String installerId = flowPath.installerId().toString();
370 long flowPathFlags = flowPath.flowPathFlags().flags();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700371
372 //verify the test
373 verifyAll();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700374 assertEquals("caller id", installerId);
375 assertEquals(0L, flowPathFlags);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700376 }
377
378 /**
379 * Test method for {@link FlowManager#getAllFlows(CallerId, DataPathEndpoints)}.
380 * @throws Exception
381 */
382 @Test
383 public final void testGetAllFlowsWithCallerIdAndDataPathEndpointsSuccessNormally() throws Exception {
384 final String getAllFlows = "getAllFlows";
385 // create mock objects
386 FlowManager fm = createPartialMock(FlowManager.class, getAllFlows,
387 new Class<?>[]{}, new Object[]{});
388
389 // instantiate required objects
390 DataPathEndpoints dataPathEndpoints = new DataPathEndpoints(
391 new SwitchPort(new Dpid(1), new Port((short)1)),
392 new SwitchPort(new Dpid(2), new Port((short)2)));
393
394 ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths();
395
396 //setup expectations
397 expectInitWithContext();
398 expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows);
399
400 //start the test
401 replayAll();
402
403 fm.init(context);
404 ArrayList<FlowPath> flows = fm.getAllFlows(new CallerId("caller id"), dataPathEndpoints);
405
406 // verify the test
407 verifyAll();
408 assertEquals(1, flows.size());
409 assertEquals(obtainedAllFlows.get(1), flows.get(0));
410 }
411
412 /**
413 * Test method for {@link FlowManager#getAllFlows(DataPathEndpoints)}.
414 * @throws Exception
415 */
416 @Test
417 public final void testGetAllFlowsWithDataPathEndpointsSuccessNormally() throws Exception {
418 final String getAllFlows = "getAllFlows";
419 // create mock objects
420 FlowManager fm = createPartialMock(FlowManager.class, getAllFlows,
421 new Class<?>[]{}, new Object[]{});
422
423 // instantiate required objects
424 DataPathEndpoints dataPathEndpoints = new DataPathEndpoints(
425 new SwitchPort(new Dpid(1), new Port((short)1)),
426 new SwitchPort(new Dpid(2), new Port((short)2)));
427
428 ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths();
429
430 //setup expectations
431 expectInitWithContext();
432 expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows);
433
434 //start the test
435 replayAll();
436
437 fm.init(context);
438 ArrayList<FlowPath> flows = fm.getAllFlows(dataPathEndpoints);
439
440 // verify the test
441 verifyAll();
442 assertEquals(2, flows.size());
443 assertEquals(obtainedAllFlows.get(0), flows.get(0));
444 assertEquals(obtainedAllFlows.get(1), flows.get(1));
445 // TODO: ignore the order of flows in the list
446 }
447
448 /**
449 * Test method for {@link FlowManager#getAllFlowsSummary(FlowId, int)}.
450 * @throws Exception
451 */
452 @Test
453 public final void testGetAllFlowsSummarySuccessNormally() throws Exception {
454 final String getAllFlowsWithoutFlowEntries = "getAllFlowsWithoutFlowEntries";
455 // create mock objects
456 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, getAllFlowsWithoutFlowEntries);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700457 IFlowPath flowPath1 = createIFlowPathMock(1, "", 0, 1, 2, 3, 4);
458 IFlowPath flowPath2 = createIFlowPathMock(5, "", 0, 2, 3, 4, 5);
459 IFlowPath flowPath3 = createIFlowPathMock(10, "", 0, 3, 4, 5, 6);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700460
461 // instantiate required objects
462 ArrayList<IFlowPath> flows = new ArrayList<IFlowPath>();
463 flows.add(flowPath3);
464 flows.add(flowPath1);
465 flows.add(flowPath2);
466
467 // setup expectations
468 expectInitWithContext();
469 expectPrivate(fm, getAllFlowsWithoutFlowEntries).andReturn(flows);
470
471 // start the test
472 replayAll();
473
474 fm.init(context);
475 ArrayList<IFlowPath> returnedFlows = fm.getAllFlowsSummary(null, 0);
476
477 // verify the test
478 verifyAll();
479 assertEquals(3, returnedFlows.size());
480 assertEquals(1, new FlowId(returnedFlows.get(0).getFlowId()).value());
481 assertEquals(5, new FlowId(returnedFlows.get(1).getFlowId()).value());
482 assertEquals(10, new FlowId(returnedFlows.get(2).getFlowId()).value());
483 }
484
485 /**
486 * Test method for {@link FlowManager#getAllFlows()}.
487 * @throws Exception
488 */
489 @Test
490 public final void testGetAllFlowsSuccessNormally() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700491 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700492 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
493 IFlowPath iFlowPath2 = createIFlowPathMock(2, "caller id", 0, 2, 5, 3, 5);
Toshio Koideca7abe02013-06-27 17:30:17 -0700494
Toshio Koidefe2625e2013-06-26 13:59:53 -0700495 // instantiate required objects
496 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
Toshio Koideca7abe02013-06-27 17:30:17 -0700497 flowPaths.add(iFlowPath1);
498 flowPaths.add(iFlowPath2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700499 FlowManager fm = new FlowManager();
500
501 // setup expectations
502 expectInitWithContext();
503 expect(op.getAllFlowPaths()).andReturn(flowPaths);
Toshio Koideca7abe02013-06-27 17:30:17 -0700504 expect(iFlowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
505 expect(iFlowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700506 op.commit();
507
508 // start the test
509 replayAll();
510
511 fm.init(context);
512 ArrayList<FlowPath> flows = fm.getAllFlows();
513
514 // verify the test
515 verifyAll();
516 assertEquals(2, flows.size());
517 assertEquals(new SwitchPort(new Dpid(1), new Port((short)1)).toString(),
518 flows.get(0).dataPath().srcPort().toString());
519 assertEquals(new SwitchPort(new Dpid(2), new Port((short)5)).toString(),
520 flows.get(1).dataPath().srcPort().toString());
521 // TODO: more asserts
522 // TODO: ignore seq. of the list
523 }
524
525 /**
526 * Test method for {@link FlowManager#addAndMaintainShortestPathFlow(FlowPath)}.
527 * @throws Exception
528 */
529 @Test
530 public final void testAddAndMaintainShortestPathFlowSuccessNormally() throws Exception {
531 final String addFlow = "addFlow";
532
533 // create mock objects
534 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow);
535
536 // instantiate required objects
537 DataPath dataPath = new DataPath();
538 dataPath.setSrcPort(new SwitchPort(new Dpid(1), new Port((short)3)));
539 dataPath.setDstPort(new SwitchPort(new Dpid(2), new Port((short)4)));
540 FlowEntryMatch match = new FlowEntryMatch();
541 FlowPath paramFlow = new FlowPath();
542 paramFlow.setFlowId(new FlowId(100));
543 paramFlow.setInstallerId(new CallerId("installer id"));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700544 paramFlow.setFlowPathFlags(new FlowPathFlags(0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700545 paramFlow.setDataPath(dataPath);
546 paramFlow.setFlowEntryMatch(match);
547
548 // setup expectations
549 expectInitWithContext();
550 expectPrivate(fm, addFlow,
551 EasyMock.anyObject(FlowPath.class),
552 EasyMock.anyObject(FlowId.class),
553 EasyMock.anyObject(String.class)
554 ).andAnswer(new IAnswer<Object>() {
555 public Object answer() throws Exception {
556 FlowPath flowPath = (FlowPath)EasyMock.getCurrentArguments()[0];
557 assertEquals(flowPath.flowId().value(), 100);
558 assertEquals(flowPath.installerId().toString(), "installer id");
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700559 assertEquals(flowPath.flowPathFlags().flags(), 0);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700560 assertEquals(flowPath.dataPath().srcPort().toString(),
561 new SwitchPort(new Dpid(1), new Port((short)3)).toString());
562
563 String dataPathSummary = (String)EasyMock.getCurrentArguments()[2];
564 assertEquals(dataPathSummary, "X");
565
566 return true;
567 }
568 });
569
570 // start the test
571 replayAll();
572
573 fm.init(context);
574 FlowPath resultFlow = fm.addAndMaintainShortestPathFlow(paramFlow);
575
576 // verify the test
577 verifyAll();
578 assertEquals(paramFlow.flowId().value(), resultFlow.flowId().value());
579 assertEquals(paramFlow.installerId().toString(), resultFlow.installerId().toString());
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700580 assertEquals(paramFlow.flowPathFlags().flags(), resultFlow.flowPathFlags().flags());
Toshio Koidefe2625e2013-06-26 13:59:53 -0700581 assertEquals(paramFlow.dataPath().toString(), resultFlow.dataPath().toString());
582 assertEquals(paramFlow.flowEntryMatch().toString(), resultFlow.flowEntryMatch().toString());
583 }
584
Toshio Koidefe2625e2013-06-26 13:59:53 -0700585 // INetMapStorage methods
586
Toshio Koidefe2625e2013-06-26 13:59:53 -0700587 /**
588 * Test method for {@link FlowManager#init(String)}.
589 * @throws Exception
590 */
591 @Test
592 public final void testInitSuccessNormally() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700593 // instantiate required objects
594 FlowManager fm = new FlowManager();
595
Toshio Koidefe2625e2013-06-26 13:59:53 -0700596 // create mock objects
597 op = createMock(GraphDBOperation.class);
598
599 // setup expectations
600 expectNew(GraphDBOperation.class, "/dummy/path").andReturn(op);
601
602 // start the test
603 replayAll();
604
Toshio Koidefe2625e2013-06-26 13:59:53 -0700605 fm.init("/dummy/path");
606
607 // verify the test
608 verifyAll();
609 }
610
611 /**
612 * Test method for {@link FlowManager#close()}.
613 * @throws Exception
614 */
615 @Test
616 public final void testCloseSuccessNormally() throws Exception {
617 // instantiate required objects
618 FlowManager fm = new FlowManager();
619
620 // setup expectations
621 expectInitWithContext();
622 op.close();
623
624 // start the test
625 replayAll();
626
627 fm.init(context);
628 fm.close();
629
630 // verify the test
631 verifyAll();
632 }
633
634
635 // IFloodlightModule methods
636
637
638 /**
639 * Test method for {@link FlowManager#getModuleServices()}.
640 * @throws Exception
641 */
642 @Test
643 public final void testGetModuleServicesSuccessNormally() throws Exception {
644 // instantiate required objects
645 FlowManager fm = new FlowManager();
646
647 // setup expectations
648 expectInitWithContext();
649
650 // start the test
651 replayAll();
652
653 fm.init(context);
654 Collection<Class<? extends IFloodlightService>> l = fm.getModuleServices();
655
656 // verify the test
657 verifyAll();
658 assertEquals(1, l.size());
659 assertEquals(IFlowService.class, l.iterator().next());
660 }
661
662 /**
663 * Test method for {@link FlowManager#getServiceImpls()}.
664 * @throws Exception
665 */
666 @Test
667 public final void testGetServiceImplsSuccessNormally() throws Exception {
668 // instantiate required objects
669 FlowManager fm = new FlowManager();
670
671 // setup expectations
672 expectInitWithContext();
673
674 // start the test
675 replayAll();
676
677 fm.init(context);
678 Map<Class<? extends IFloodlightService>, IFloodlightService> si = fm.getServiceImpls();
679
680 // verify the test
681 verifyAll();
682 assertEquals(1, si.size());
683 assertTrue(si.containsKey(IFlowService.class));
684 assertEquals(fm, si.get(IFlowService.class));
685 }
686
687 /**
688 * Test method for {@link FlowManager#getModuleDependencies()}.
689 * @throws Exception
690 */
691 @Test
692 public final void testGetModuleDependenciesSuccessNormally() throws Exception {
693 // instantiate required objects
694 FlowManager fm = new FlowManager();
695
696 // setup expectations
697 expectInitWithContext();
698
699 // start the test
700 replayAll();
701
702 fm.init(context);
703 Collection<Class<? extends IFloodlightService>> md = fm.getModuleDependencies();
704
705 // verify the test
706 verifyAll();
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700707 assertEquals(4, md.size());
Toshio Koidefe2625e2013-06-26 13:59:53 -0700708 assertTrue(md.contains(IFloodlightProviderService.class));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700709 assertTrue(md.contains(IRestApiService.class));
710 }
711
712 /**
713 * Test method for {@link FlowManager#init(FloodlightModuleContext)}.
714 * @throws Exception
715 */
716 @Test
717 public final void testInitWithFloodlightModuleContextSuccessNormally() throws Exception {
718 // instantiate required objects
719 FlowManager fm = new FlowManager();
720
721 // setup expectations
722 expectInitWithContext();
723
724 // start the test
725 replayAll();
726
727 fm.init(context);
728
729 // verify the test
730 verifyAll();
731 }
732
733 /**
734 * Test method for {@link FlowManager#startUp(FloodlightModuleContext)}.
735 * @throws Exception
736 */
737 @Test
738 public final void testStartupSuccessNormally() throws Exception {
739 // create mock objects
740 mockStaticPartial(Executors.class, "newScheduledThreadPool");
741 ScheduledExecutorService scheduler = createMock(ScheduledExecutorService.class);
742
Toshio Koidefe2625e2013-06-26 13:59:53 -0700743 // instantiate required objects
744 FlowManager fm = new FlowManager();
745
746 // setup expectations
747 expectInitWithContext();
748 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
749 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
750 expect(scheduler.scheduleAtFixedRate(
751 EasyMock.anyObject(Runnable.class),
752 EasyMock.anyLong(),
753 EasyMock.anyLong(),
754 EasyMock.anyObject(TimeUnit.class))).andReturn(null).times(2);
755 restApi.addRestletRoutable(EasyMock.anyObject(FlowWebRoutable.class));
756
757 // start the test
758 replayAll();
759
760 fm.init(context);
761 fm.startUp(context);
762
763 // verify the test
764 verifyAll();
765 }
Toshio Koideca7abe02013-06-27 17:30:17 -0700766
767
768 // other methods
769
770
771 /**
772 * Test method for {@link FlowManager#clearFlow(FlowId)}.
773 * @throws Exception
774 */
775 @Test
776 public final void testClearFlowSuccessNormally() throws Exception {
777 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700778 IFlowPath flowPath = createIFlowPathMock(123, "id", 0, 1, 2, 3, 4);
Toshio Koideca7abe02013-06-27 17:30:17 -0700779 IFlowEntry flowEntry1 = createMock(IFlowEntry.class);
780 IFlowEntry flowEntry2 = createMock(IFlowEntry.class);
781 IFlowEntry flowEntry3 = createMock(IFlowEntry.class);
782
783 // instantiate required objects
784 FlowManager fm = new FlowManager();
785 FlowId flowId = new FlowId(123);
786 ArrayList<IFlowEntry> flowEntries = new ArrayList<IFlowEntry>();
787 flowEntries.add(flowEntry1);
788 flowEntries.add(flowEntry2);
789 flowEntries.add(flowEntry3);
790
791 // setup expectations
792 expectInitWithContext();
793 expect(op.searchFlowPath(cmpEq(flowId))).andReturn(flowPath);
794 expect(flowPath.getFlowEntries()).andReturn(flowEntries);
795 flowPath.removeFlowEntry(flowEntry1);
796 flowPath.removeFlowEntry(flowEntry2);
797 flowPath.removeFlowEntry(flowEntry3);
798 op.removeFlowEntry(flowEntry1);
799 op.removeFlowEntry(flowEntry2);
800 op.removeFlowEntry(flowEntry3);
801 op.removeFlowPath(flowPath);
802 op.commit();
803
804 // start the test
805 replayAll();
806
807 fm.init(context);
808 fm.clearFlow(flowId);
809
810 // verify the test
811 verifyAll();
812 }
813
814 /**
815 * Test method for {@link FlowManager#getAllFlowsWithoutFlowEntries()}.
816 * @throws Exception
817 */
818 @Test
819 public final void testGetAllFlowsWithoutFlowEntriesSuccessNormally() throws Exception {
820 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700821 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
822 IFlowPath iFlowPath2 = createIFlowPathMock(2, "caller id", 0, 2, 5, 3, 5);
Toshio Koideca7abe02013-06-27 17:30:17 -0700823
824 // instantiate required objects
825 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
826 flowPaths.add(iFlowPath1);
827 flowPaths.add(iFlowPath2);
828 FlowManager fm = new FlowManager();
829
830 // setup expectations
831 expectInitWithContext();
832 op.commit();
833 expect(op.getAllFlowPaths()).andReturn(flowPaths);
834
835 // start the test
836 replayAll();
837
838 fm.init(context);
839 ArrayList<IFlowPath> result = fm.getAllFlowsWithoutFlowEntries();
840
841 // verify the test
842 verifyAll();
843 assertEquals(iFlowPath1, result.get(0));
844 assertEquals(iFlowPath2, result.get(1));
845
846 // TODO: does this method just return the replica of the flow paths?
847 }
848
849 /**
850 * Test method for {@link FlowManager#reconcileFlow(IFlowPath, DataPath)}.
851 * @throws Exception
852 */
853 @Test
854 public final void testReconcileFlowWithFlowPathAndDataPathSuccessNormally() throws Exception {
855 final String addFlowEntry = "addFlowEntry";
856
857 // create mock objects
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700858 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", 0, 1, 1, 2, 2);
Toshio Koideca7abe02013-06-27 17:30:17 -0700859 IFlowEntry iFlowEntry1 = createMock(IFlowEntry.class);
860 IFlowEntry iFlowEntry2 = createMock(IFlowEntry.class);
861 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
862
863 // instantiate required objects
864 FlowEntry flowEntry1 = new FlowEntry();
865 flowEntry1.setDpid(new Dpid(1));
866 flowEntry1.setFlowId(new FlowId(1));
867 flowEntry1.setInPort(new Port((short) 1));
868 flowEntry1.setOutPort(new Port((short) 11));
869 flowEntry1.setFlowEntryId(new FlowEntryId(1));
870 flowEntry1.setFlowEntryMatch(new FlowEntryMatch());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700871 flowEntry1.setFlowEntryActions(new FlowEntryActions());
Toshio Koideca7abe02013-06-27 17:30:17 -0700872 flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState());
873
874 FlowEntry flowEntry2 = new FlowEntry();
875 flowEntry2.setDpid(new Dpid(2));
876 flowEntry2.setFlowId(new FlowId(2));
877 flowEntry2.setInPort(new Port((short) 22));
878 flowEntry2.setOutPort(new Port((short) 2));
879 flowEntry2.setFlowEntryId(new FlowEntryId(2));
880 flowEntry2.setFlowEntryMatch(new FlowEntryMatch());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700881 flowEntry2.setFlowEntryActions(new FlowEntryActions());
Toshio Koideca7abe02013-06-27 17:30:17 -0700882 flowEntry2.setFlowEntryErrorState(new FlowEntryErrorState());
883
884 DataPath dataPath = new DataPath();
885 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
886 flowEntries.add(flowEntry1);
887 flowEntries.add(flowEntry2);
888 dataPath.setFlowEntries(flowEntries);
889
890 ArrayList<IFlowEntry> oldFlowEntries = new ArrayList<IFlowEntry>();
891 oldFlowEntries.add(iFlowEntry1);
892 oldFlowEntries.add(iFlowEntry2);
893
894 // setup expectations
895 expectInitWithContext();
Toshio Koideca7abe02013-06-27 17:30:17 -0700896 expect(iFlowPath1.getFlowEntries()).andReturn(oldFlowEntries);
897 iFlowEntry1.setUserState("FE_USER_DELETE");
898 iFlowEntry1.setSwitchState("FE_SWITCH_NOT_UPDATED");
899 iFlowEntry2.setUserState("FE_USER_DELETE");
900 iFlowEntry2.setSwitchState("FE_SWITCH_NOT_UPDATED");
901 expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry1).andReturn(null);
902 expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry2).andReturn(null);
903
904 // start the test
905 replayAll();
906
907 fm.init(context);
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700908 // Use reflection to test the private method
909 // Boolean result = fm.reconcileFlow(iFlowPath1, dataPath);
910 Class fmClass = FlowManager.class;
911 Method method = fmClass.getDeclaredMethod(
912 "reconcileFlow",
913 new Class[] { IFlowPath.class, DataPath.class });
914 method.setAccessible(true);
915 Boolean result = (Boolean)method.invoke(fm,
916 new Object[] { iFlowPath1, dataPath });
Toshio Koideca7abe02013-06-27 17:30:17 -0700917
918 // verify the test
919 verifyAll();
920 assertTrue(result);
921 // TODO: write more asserts
922 }
923
924 /**
925 * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, IFlowPath, IFlowEntry)}.
926 * @throws Exception
927 */
928 @Test
929 public final void testInstallFlowEntryWithIFlowPathSuccessNormally() throws Exception {
930 // create mock object
931 IOFSwitch iofSwitch = createNiceMock(IOFSwitch.class);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700932 IFlowPath iFlowPath = createIFlowPathMock(1, "id", 0, 1, 2, 3, 4);
Toshio Koideca7abe02013-06-27 17:30:17 -0700933 IFlowEntry iFlowEntry = createMock(IFlowEntry.class);
934 BasicFactory basicFactory = createMock(BasicFactory.class);
935
936 // instantiate required objects
937 FlowManager fm = new FlowManager();
938
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700939 FlowEntryAction action = new FlowEntryAction();
940 action.setActionOutput(new Port((short)2));
941 FlowEntryActions actions = new FlowEntryActions();
942 actions.addAction(action);
943
Toshio Koideca7abe02013-06-27 17:30:17 -0700944 // setup expectations
945 expectInitWithContext();
946 expect(iFlowEntry.getFlowEntryId()).andReturn(new FlowEntryId(123).toString());
947 expect(iFlowEntry.getUserState()).andReturn("FE_USER_ADD");
948 iFlowEntry.setSwitchState("FE_SWITCH_UPDATED");
949 expect(iFlowEntry.getMatchInPort()).andReturn(new Short((short) 1));
Toshio Koideca7abe02013-06-27 17:30:17 -0700950 expect(iFlowEntry.getMatchSrcMac()).andReturn("01:23:45:67:89:01");
951 expect(iFlowEntry.getMatchDstMac()).andReturn("01:23:45:67:89:02");
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700952 expect(iFlowEntry.getMatchEthernetFrameType()).andReturn(new Short((short)0x0800));
953 expect(iFlowEntry.getMatchVlanId()).andReturn(new Short((short)0x1234));
954 expect(iFlowEntry.getMatchVlanPriority()).andReturn(new Byte((byte)0x10));
955 expect(iFlowEntry.getMatchSrcIPv4Net()).andReturn("192.168.0.1");
956 expect(iFlowEntry.getMatchDstIPv4Net()).andReturn("192.168.0.2");
957 expect(iFlowEntry.getMatchIpProto()).andReturn(new Byte((byte)0x20));
958 expect(iFlowEntry.getMatchIpToS()).andReturn(new Byte((byte)0x3));
959 expect(iFlowEntry.getMatchSrcTcpUdpPort()).andReturn(new Short((short)40000));
960 expect(iFlowEntry.getMatchDstTcpUdpPort()).andReturn(new Short((short)80));
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700961 expect(iFlowEntry.getActions()).andReturn(actions.toString());
Toshio Koideca7abe02013-06-27 17:30:17 -0700962 expect(floodlightProvider.getOFMessageFactory()).andReturn(basicFactory);
963 expect(basicFactory.getMessage(OFType.FLOW_MOD)).andReturn(new OFFlowMod());
964 expect(iofSwitch.getStringId()).andReturn(new Dpid(100).toString());
965
966 // start the test
967 replayAll();
968
969 fm.init(context);
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700970 // Use reflection to test the private method
971 // Boolean result = fm.installFlowEntry(iofSwitch, iFlowPath, iFlowEntry);
972 Class fmClass = FlowManager.class;
973 Method method = fmClass.getDeclaredMethod(
974 "installFlowEntry",
975 new Class[] { IOFSwitch.class, IFlowPath.class, IFlowEntry.class });
976 method.setAccessible(true);
977 Boolean result = (Boolean)method.invoke(fm,
978 new Object[] { iofSwitch, iFlowPath, iFlowEntry });
979
Toshio Koideca7abe02013-06-27 17:30:17 -0700980
981 // verify the test
982 verifyAll();
983 assertTrue(result);
984 // TODO: write more asserts
985 }
986
987 /**
988 * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}.
989 * The method seems to be not used for now.
990 */
991 @Ignore @Test
992 public final void testInstallFlowEntryWithFlowPathSuccessNormally() {
993 fail("not yet implemented");
994 }
995
996 /**
997 * Test method for {@link FlowManager#removeFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}.
998 * The method seems to be not implemented and not used for now.
999 */
1000 @Ignore @Test
1001 public final void testRemoveFlowEntrySuccessNormally() {
1002 fail("not yet implemented");
1003 }
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -07001004}