blob: 73577c1ddc08b8c4a900c88bf4e82661f75bc018 [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;
Toshio Koidefe2625e2013-06-26 13:59:53 -070029import org.junit.After;
30import org.junit.Before;
Toshio Koideca7abe02013-06-27 17:30:17 -070031import org.junit.Ignore;
Toshio Koidefe2625e2013-06-26 13:59:53 -070032import org.junit.Test;
33import org.junit.runner.RunWith;
Toshio Koideca7abe02013-06-27 17:30:17 -070034import org.openflow.protocol.OFFlowMod;
35import org.openflow.protocol.OFType;
36import org.openflow.protocol.factory.BasicFactory;
Toshio Koidefe2625e2013-06-26 13:59:53 -070037import org.powermock.core.classloader.annotations.PrepareForTest;
38import org.powermock.modules.junit4.PowerMockRunner;
39
40/**
41 * @author Toshio Koide
42 */
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070043@Ignore
Toshio Koidefe2625e2013-06-26 13:59:53 -070044@RunWith(PowerMockRunner.class)
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070045@PrepareForTest({FlowManager.class, FlowDatabaseOperation.class, GraphDBOperation.class, System.class, Executors.class})
Toshio Koidefe2625e2013-06-26 13:59:53 -070046public class FlowManagerTest {
47 private static FloodlightModuleContext context;
48 private static IFloodlightProviderService floodlightProvider;
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070049 private static TopologyManager topologyManager;
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070050 private static IDatagridService datagridService;
Toshio Koidefe2625e2013-06-26 13:59:53 -070051 private static IRestApiService restApi;
52 private static GraphDBOperation op;
53
54 /**
55 * @throws java.lang.Exception
56 */
57 @Before
58 public void setUp() throws Exception {
59 }
60
61 /**
62 * @throws java.lang.Exception
63 */
64 @After
65 public void tearDown() throws Exception {
66 }
67
68 /**
69 * @throws java.lang.Exception
70 */
71 private void expectInitWithContext() throws Exception {
72 // create mock objects
73 context = createMock(FloodlightModuleContext.class);
74 floodlightProvider = createMock(IFloodlightProviderService.class);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070075 topologyManager = createMock(TopologyManager.class);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070076 datagridService = createMock(IDatagridService.class);
Toshio Koidefe2625e2013-06-26 13:59:53 -070077 restApi = createMock(IRestApiService.class);
78 op = createMock(GraphDBOperation.class);
79
80 // setup expectations
81 expect(context.getServiceImpl(IFloodlightProviderService.class)).andReturn(floodlightProvider);
Pavlin Radoslavov661c86f2013-10-21 12:40:40 -070082 expect(context.getServiceImpl(ITopologyNetService.class)).andReturn(topologyManager);
83 expect(context.getServiceImpl(IDatagridService.class)).andReturn(datagridService);
Toshio Koidefe2625e2013-06-26 13:59:53 -070084 expect(context.getServiceImpl(IRestApiService.class)).andReturn(restApi);
85 expectNew(GraphDBOperation.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(op);
Pavlin Radoslavove1b37bc2013-10-16 03:57:06 -070086 expectNew(TopologyManager.class, new Class<?>[] {String.class}, EasyMock.isA(String.class)).andReturn(topologyManager);
Toshio Koidefe2625e2013-06-26 13:59:53 -070087 }
88
89 private IFlowPath createIFlowPathMock(long flowId, String installerID,
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -070090 String flowPathType, String flowPathUserState,
91 long flowPathFlags, long srcDpid, int srcPort,
92 long dstDpid, int dstPort) {
Toshio Koidefe2625e2013-06-26 13:59:53 -070093 IFlowPath iFlowPath = createNiceMock(IFlowPath.class);
94 expect(iFlowPath.getFlowId()).andReturn(new FlowId(flowId).toString()).anyTimes();
95 expect(iFlowPath.getInstallerId()).andReturn(installerID).anyTimes();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -070096 expect(iFlowPath.getFlowPathType()).andReturn(flowPathType).anyTimes();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -070097 expect(iFlowPath.getFlowPathUserState()).andReturn(flowPathUserState).anyTimes();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -070098 expect(iFlowPath.getFlowPathFlags()).andReturn(new Long(flowPathFlags)).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -070099 expect(iFlowPath.getSrcSwitch()).andReturn(new Dpid(srcDpid).toString()).anyTimes();
100 expect(iFlowPath.getSrcPort()).andReturn(new Short((short)srcPort)).anyTimes();
101 expect(iFlowPath.getDstSwitch()).andReturn(new Dpid(dstDpid).toString()).anyTimes();
102 expect(iFlowPath.getDstPort()).andReturn(new Short((short)dstPort)).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700103 return iFlowPath;
104 }
105
Toshio Koideca7abe02013-06-27 17:30:17 -0700106 private FlowPath createTestFlowPath(long flowId, String installerId,
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700107 String flowPathType, String flowPathUserState,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700108 final long flowPathFlags,
Toshio Koidefe2625e2013-06-26 13:59:53 -0700109 final long srcDpid, final int srcPort,
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700110 final long dstDpid, final int dstPort
Toshio Koidefe2625e2013-06-26 13:59:53 -0700111 ) {
112 FlowPath flowPath = new FlowPath();
113 flowPath.setFlowId(new FlowId(flowId));
114 flowPath.setInstallerId(new CallerId(installerId));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700115 flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700116 flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700117 flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700118 flowPath.setDataPath(new DataPath() {{
119 setSrcPort(new SwitchPort(new Dpid(srcDpid), new Port((short)srcPort)));
120 setDstPort(new SwitchPort(new Dpid(dstDpid), new Port((short)dstPort)));
121 }});
122 flowPath.setFlowEntryMatch(new FlowEntryMatch());
123 return flowPath;
124 }
125
Naoki Shiota991093a2013-12-10 14:47:18 -0800126 /*
Toshio Koidefe2625e2013-06-26 13:59:53 -0700127 private ArrayList<FlowPath> createTestFlowPaths() {
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700128 FlowPath flowPath1 = createTestFlowPath(1, "foo caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 1, 2, 2);
129 FlowPath flowPath2 = createTestFlowPath(2, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 1, 2, 2);
130 FlowPath flowPath3 = createTestFlowPath(3, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 5, 2, 2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700131
132 ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>();
133 flowPaths.add(flowPath1);
134 flowPaths.add(flowPath2);
135 flowPaths.add(flowPath3);
136
137 return flowPaths;
138 }
Naoki Shiota991093a2013-12-10 14:47:18 -0800139 */
Toshio Koidefe2625e2013-06-26 13:59:53 -0700140
141
142 // IFlowService methods
143
144
145 /**
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800146 * Test method for {@link FlowManager#addFlow(FlowPath)}.
Toshio Koidefe2625e2013-06-26 13:59:53 -0700147 * @throws Exception
148 */
149 @Test
150 public final void testAddFlowFailGraphCreatesNoFlow() throws Exception {
151 // instantiate required objects
152 FlowId flowId = new FlowId(123);
153 FlowPath flowPath = new FlowPath();
154 flowPath.setFlowId(flowId);
Toshio Koideca7abe02013-06-27 17:30:17 -0700155 FlowManager fm = new FlowManager();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700156
157 // setup expectations
158 expectInitWithContext();
159 expect(op.searchFlowPath(flowId)).andReturn(null);
160 expect(op.newFlowPath()).andReturn(null);
161 op.rollback();
162
163 // start the test
164 replayAll();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700165
Toshio Koidefe2625e2013-06-26 13:59:53 -0700166 fm.init(context);
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800167 FlowId result = fm.addFlow(flowPath);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700168
169 // verify the test
170 verifyAll();
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800171 assertNotNull(result);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700172 }
173
174 /**
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800175 * Test method for {@link FlowManager#addFlow(FlowPath)}.
Toshio Koidefe2625e2013-06-26 13:59:53 -0700176 * @throws Exception
177 */
178 @Test
179 public final void testAddFlowSuccessNormally() throws Exception {
180 final String addFlowEntry = "addFlowEntry";
181 // create mock objects
182 IFlowPath createdFlowPath = createNiceMock(IFlowPath.class);
183 IFlowEntry createdFlowEntry1 = createNiceMock(IFlowEntry.class);
184 IFlowEntry createdFlowEntry2 = createNiceMock(IFlowEntry.class);
185 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
186
187 // instantiate required objects
188 final FlowEntry flowEntry1 = new FlowEntry();
189 final FlowEntry flowEntry2 = new FlowEntry();
190 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
191 flowEntries.add(flowEntry1);
192 flowEntries.add(flowEntry2);
193
194 DataPath dataPath = new DataPath();
195 dataPath.setSrcPort(new SwitchPort(new Dpid(0x1234), new Port((short)1)));
196 dataPath.setDstPort(new SwitchPort(new Dpid(0x5678), new Port((short)2)));
197 dataPath.setFlowEntries(flowEntries);
198
199 FlowEntryMatch match = new FlowEntryMatch();
200
201 FlowPath flowPath = new FlowPath();
202 flowPath.setFlowId(new FlowId(0x100));
203 flowPath.setInstallerId(new CallerId("installer id"));
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700204 flowPath.setFlowPathType(FlowPathType.valueOf("FP_TYPE_SHORTEST_PATH"));
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700205 flowPath.setFlowPathUserState(FlowPathUserState.valueOf("FP_USER_ADD"));
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700206 flowPath.setFlowPathFlags(new FlowPathFlags(0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700207 flowPath.setDataPath(dataPath);
208 flowPath.setFlowEntryMatch(match);
209
210 // setup expectations
211 expectInitWithContext();
212 expect(op.searchFlowPath(cmpEq(new FlowId(0x100)))).andReturn(null);
213 expect(op.newFlowPath()).andReturn(createdFlowPath);
214 createdFlowPath.setFlowId("0x100");
215 createdFlowPath.setType("flow");
216 createdFlowPath.setInstallerId("installer id");
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700217 createdFlowPath.setFlowPathType("FP_TYPE_SHORTEST_PATH");
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700218 createdFlowPath.setFlowPathUserState("FP_USER_ADD");
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700219 createdFlowPath.setFlowPathFlags(new Long((long)0));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700220 createdFlowPath.setSrcSwitch("00:00:00:00:00:00:12:34");
221 createdFlowPath.setSrcPort(new Short((short)1));
222 createdFlowPath.setDstSwitch("00:00:00:00:00:00:56:78");
223 createdFlowPath.setDstPort(new Short((short)2));
224 createdFlowPath.setDataPathSummary("data path summary");
Toshio Koidefe2625e2013-06-26 13:59:53 -0700225
226 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry1)
227 .andReturn(createdFlowEntry1);
228 expectPrivate(fm, addFlowEntry, createdFlowPath, flowEntry2)
229 .andReturn(createdFlowEntry2);
230
231 op.commit();
232
233 // start the test
234 replayAll();
235
236 fm.init(context);
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800237 FlowId result = fm.addFlow(flowPath);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700238
239 // verify the test
240 verifyAll();
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800241 assertNotNull(result);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700242 }
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800243
Toshio Koidefe2625e2013-06-26 13:59:53 -0700244 /**
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800245 * Test method for {@link FlowManager#deleteFlow(FlowId)}.
246 * @throws Exception
Toshio Koidefe2625e2013-06-26 13:59:53 -0700247 */
248 @Test
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800249 public final void testDeleteFlowSuccessNormally() throws Exception {
Toshio Koidefe2625e2013-06-26 13:59:53 -0700250 // create mock objects
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800251 IFlowPath flowPath = createIFlowPathMock(123, "id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 2, 3, 4);
252 IFlowEntry flowEntry1 = createMock(IFlowEntry.class);
253 IFlowEntry flowEntry2 = createMock(IFlowEntry.class);
254 IFlowEntry flowEntry3 = createMock(IFlowEntry.class);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700255
256 // instantiate required objects
Toshio Koideca7abe02013-06-27 17:30:17 -0700257 FlowManager fm = new FlowManager();
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800258 FlowId flowId = new FlowId(123);
259 ArrayList<IFlowEntry> flowEntries = new ArrayList<IFlowEntry>();
260 flowEntries.add(flowEntry1);
261 flowEntries.add(flowEntry2);
262 flowEntries.add(flowEntry3);
263
Toshio Koidefe2625e2013-06-26 13:59:53 -0700264 // setup expectations
265 expectInitWithContext();
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800266 expect(op.searchFlowPath(cmpEq(flowId))).andReturn(flowPath);
267 expect(flowPath.getFlowEntries()).andReturn(flowEntries);
268 flowPath.removeFlowEntry(flowEntry1);
269 flowPath.removeFlowEntry(flowEntry2);
270 flowPath.removeFlowEntry(flowEntry3);
271 op.removeFlowEntry(flowEntry1);
272 op.removeFlowEntry(flowEntry2);
273 op.removeFlowEntry(flowEntry3);
274 op.removeFlowPath(flowPath);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700275 op.commit();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700276
277 // start the test
278 replayAll();
279
Toshio Koidefe2625e2013-06-26 13:59:53 -0700280 fm.init(context);
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800281 fm.deleteFlow(flowId);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700282
283 // verify the test
284 verifyAll();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700285 }
286
287 /**
288 * Test method for {@link FlowManager#deleteFlow(FlowId)}.
289 * @throws Exception
290 */
291 @Test
292 public final void testDeleteFlowSuccessEmptyFlowPath() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700293 // instantiate required objects
294 FlowManager fm = new FlowManager();
295
Toshio Koidefe2625e2013-06-26 13:59:53 -0700296 // create mock objects
297 IFlowPath flowObj = createNiceMock(IFlowPath.class);
298
299 // setup expectations
300 expectInitWithContext();
301 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowObj);
302 expect(flowObj.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>());
303 op.removeFlowPath(flowObj);
304 op.commit();
305 expectLastCall().anyTimes();
306
307 // start the test
308 replayAll();
309
Toshio Koidefe2625e2013-06-26 13:59:53 -0700310 fm.init(context);
311 Boolean result = fm.deleteFlow(new FlowId(1));
312
313 // verify the test
314 verifyAll();
315 assertTrue(result);
316 }
317
318 /**
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800319 * Test method for {@link FlowManager#deleteAllFlows()}.
Toshio Koidefe2625e2013-06-26 13:59:53 -0700320 * @throws Exception
321 */
322 @Test
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800323 public final void testDeleteAllFlowsSuccessNormally() throws Exception {
Toshio Koidefe2625e2013-06-26 13:59:53 -0700324 // create mock objects
325 IFlowPath flowPath1 = createNiceMock(IFlowPath.class);
326 IFlowPath flowPath2 = createNiceMock(IFlowPath.class);
327 IFlowPath flowPath3 = createNiceMock(IFlowPath.class);
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800328 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, "deleteFlow");
Toshio Koidefe2625e2013-06-26 13:59:53 -0700329
330 // instantiate required objects
331 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
332 flowPaths.add(flowPath1);
333 flowPaths.add(flowPath2);
334 flowPaths.add(null);
335 flowPaths.add(flowPath3);
336
337 // setup expectations
338 expectInitWithContext();
339 expect(op.getAllFlowPaths()).andReturn(flowPaths);
340 expect(flowPath1.getFlowId()).andReturn(new FlowId(1).toString());
341 expect(flowPath2.getFlowId()).andReturn(null);
342 expect(flowPath3.getFlowId()).andReturn(new FlowId(3).toString());
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800343 expect(fm.deleteFlow(cmpEq(new FlowId(1)))).andReturn(true);
344 expect(fm.deleteFlow(cmpEq(new FlowId(3)))).andReturn(true);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700345
346 // start the test
347 replayAll();
348
349 fm.init(context);
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800350 Boolean result = fm.deleteAllFlows();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700351
Pavlin Radoslavovf2a52652013-11-22 12:35:42 -0800352 // verify the test
Toshio Koidefe2625e2013-06-26 13:59:53 -0700353 verifyAll();
354 assertTrue(result);
355 }
356
357 /**
358 * Test method for {@link FlowManager#getFlow()}.
359 * @throws Exception
360 */
361 @Test
362 public final void testGetFlowSuccessNormally() throws Exception {
363 // instantiate required objects
364 FlowManager fm = new FlowManager();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700365 IFlowPath iFlowPath = createIFlowPathMock(1, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 1, 2, 2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700366
367 // setup expectations
368 expectInitWithContext();
Toshio Koideca7abe02013-06-27 17:30:17 -0700369 expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(iFlowPath);
370 expect(iFlowPath.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700371 op.commit();
372
373 // start the test
374 replayAll();
375
376 fm.init(context);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700377 FlowPath flowPath = fm.getFlow(new FlowId(1));
378 String installerId = flowPath.installerId().toString();
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700379 String flowPathType = flowPath.flowPathType().toString();
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700380 String flowPathUserState = flowPath.flowPathUserState().toString();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700381 long flowPathFlags = flowPath.flowPathFlags().flags();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700382
383 //verify the test
384 verifyAll();
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700385 assertEquals("caller id", installerId);
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700386 assertEquals("FP_TYPE_SHORTEST_PATH", flowPathType);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700387 assertEquals("FP_USER_ADD", flowPathUserState);
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700388 assertEquals(0L, flowPathFlags);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700389 }
390
391 /**
Toshio Koidefe2625e2013-06-26 13:59:53 -0700392 * Test method for {@link FlowManager#getAllFlowsSummary(FlowId, int)}.
393 * @throws Exception
394 */
395 @Test
396 public final void testGetAllFlowsSummarySuccessNormally() throws Exception {
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800397 final String getAllFlowsWithDataPathSummary = "getAllFlowsWithDataPathSummary";
Toshio Koidefe2625e2013-06-26 13:59:53 -0700398 // create mock objects
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800399 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, getAllFlowsWithDataPathSummary);
400 FlowPath flowPath1 = createTestFlowPath(1, "", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 2, 3, 4);
401 FlowPath flowPath2 = createTestFlowPath(5, "", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 2, 3, 4, 5);
402 FlowPath flowPath3 = createTestFlowPath(10, "", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 3, 4, 5, 6);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700403
404 // instantiate required objects
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800405 ArrayList<FlowPath> flows = new ArrayList<FlowPath>();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700406 flows.add(flowPath3);
407 flows.add(flowPath1);
408 flows.add(flowPath2);
409
410 // setup expectations
411 expectInitWithContext();
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800412 expectPrivate(fm, getAllFlowsWithDataPathSummary).andReturn(flows);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700413
414 // start the test
415 replayAll();
416
417 fm.init(context);
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800418 ArrayList<FlowPath> returnedFlows = fm.getAllFlowsSummary(null, 0);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700419
420 // verify the test
421 verifyAll();
422 assertEquals(3, returnedFlows.size());
Pavlin Radoslavov4ef6ba22013-11-22 19:32:58 -0800423 assertEquals(1, new FlowId(returnedFlows.get(0).flowId().value()).value());
424 assertEquals(5, new FlowId(returnedFlows.get(1).flowId().value()).value());
425 assertEquals(10, new FlowId(returnedFlows.get(2).flowId().value()).value());
Toshio Koidefe2625e2013-06-26 13:59:53 -0700426 }
427
428 /**
429 * Test method for {@link FlowManager#getAllFlows()}.
430 * @throws Exception
431 */
432 @Test
433 public final void testGetAllFlowsSuccessNormally() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700434 // create mock objects
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700435 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 1, 2, 2);
436 IFlowPath iFlowPath2 = createIFlowPathMock(2, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 2, 5, 3, 5);
Toshio Koideca7abe02013-06-27 17:30:17 -0700437
Toshio Koidefe2625e2013-06-26 13:59:53 -0700438 // instantiate required objects
439 ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>();
Toshio Koideca7abe02013-06-27 17:30:17 -0700440 flowPaths.add(iFlowPath1);
441 flowPaths.add(iFlowPath2);
Toshio Koidefe2625e2013-06-26 13:59:53 -0700442 FlowManager fm = new FlowManager();
443
444 // setup expectations
445 expectInitWithContext();
446 expect(op.getAllFlowPaths()).andReturn(flowPaths);
Toshio Koideca7abe02013-06-27 17:30:17 -0700447 expect(iFlowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
448 expect(iFlowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes();
Toshio Koidefe2625e2013-06-26 13:59:53 -0700449 op.commit();
450
451 // start the test
452 replayAll();
453
454 fm.init(context);
455 ArrayList<FlowPath> flows = fm.getAllFlows();
456
457 // verify the test
458 verifyAll();
459 assertEquals(2, flows.size());
460 assertEquals(new SwitchPort(new Dpid(1), new Port((short)1)).toString(),
461 flows.get(0).dataPath().srcPort().toString());
462 assertEquals(new SwitchPort(new Dpid(2), new Port((short)5)).toString(),
463 flows.get(1).dataPath().srcPort().toString());
464 // TODO: more asserts
465 // TODO: ignore seq. of the list
466 }
Toshio Koidefe2625e2013-06-26 13:59:53 -0700467
Toshio Koidefe2625e2013-06-26 13:59:53 -0700468 // INetMapStorage methods
469
Toshio Koidefe2625e2013-06-26 13:59:53 -0700470 /**
471 * Test method for {@link FlowManager#init(String)}.
472 * @throws Exception
473 */
474 @Test
475 public final void testInitSuccessNormally() throws Exception {
Toshio Koideca7abe02013-06-27 17:30:17 -0700476 // instantiate required objects
477 FlowManager fm = new FlowManager();
478
Toshio Koidefe2625e2013-06-26 13:59:53 -0700479 // create mock objects
480 op = createMock(GraphDBOperation.class);
481
482 // setup expectations
483 expectNew(GraphDBOperation.class, "/dummy/path").andReturn(op);
484
485 // start the test
486 replayAll();
487
yoshi2fd4c7e2013-11-22 15:47:55 -0800488 fm.init("dummy_store", "/dummy/path");
Toshio Koidefe2625e2013-06-26 13:59:53 -0700489
490 // verify the test
491 verifyAll();
492 }
493
494 /**
495 * Test method for {@link FlowManager#close()}.
496 * @throws Exception
497 */
498 @Test
499 public final void testCloseSuccessNormally() throws Exception {
500 // instantiate required objects
501 FlowManager fm = new FlowManager();
502
503 // setup expectations
504 expectInitWithContext();
505 op.close();
506
507 // start the test
508 replayAll();
509
510 fm.init(context);
511 fm.close();
512
513 // verify the test
514 verifyAll();
515 }
516
517
518 // IFloodlightModule methods
519
520
521 /**
522 * Test method for {@link FlowManager#getModuleServices()}.
523 * @throws Exception
524 */
525 @Test
526 public final void testGetModuleServicesSuccessNormally() throws Exception {
527 // instantiate required objects
528 FlowManager fm = new FlowManager();
529
530 // setup expectations
531 expectInitWithContext();
532
533 // start the test
534 replayAll();
535
536 fm.init(context);
537 Collection<Class<? extends IFloodlightService>> l = fm.getModuleServices();
538
539 // verify the test
540 verifyAll();
541 assertEquals(1, l.size());
542 assertEquals(IFlowService.class, l.iterator().next());
543 }
544
545 /**
546 * Test method for {@link FlowManager#getServiceImpls()}.
547 * @throws Exception
548 */
549 @Test
550 public final void testGetServiceImplsSuccessNormally() throws Exception {
551 // instantiate required objects
552 FlowManager fm = new FlowManager();
553
554 // setup expectations
555 expectInitWithContext();
556
557 // start the test
558 replayAll();
559
560 fm.init(context);
561 Map<Class<? extends IFloodlightService>, IFloodlightService> si = fm.getServiceImpls();
562
563 // verify the test
564 verifyAll();
565 assertEquals(1, si.size());
566 assertTrue(si.containsKey(IFlowService.class));
567 assertEquals(fm, si.get(IFlowService.class));
568 }
569
570 /**
571 * Test method for {@link FlowManager#getModuleDependencies()}.
572 * @throws Exception
573 */
574 @Test
575 public final void testGetModuleDependenciesSuccessNormally() throws Exception {
576 // instantiate required objects
577 FlowManager fm = new FlowManager();
578
579 // setup expectations
580 expectInitWithContext();
581
582 // start the test
583 replayAll();
584
585 fm.init(context);
586 Collection<Class<? extends IFloodlightService>> md = fm.getModuleDependencies();
587
588 // verify the test
589 verifyAll();
Pavlin Radoslavov05378272013-10-19 23:23:05 -0700590 assertEquals(4, md.size());
Toshio Koidefe2625e2013-06-26 13:59:53 -0700591 assertTrue(md.contains(IFloodlightProviderService.class));
Toshio Koidefe2625e2013-06-26 13:59:53 -0700592 assertTrue(md.contains(IRestApiService.class));
593 }
594
595 /**
596 * Test method for {@link FlowManager#init(FloodlightModuleContext)}.
597 * @throws Exception
598 */
599 @Test
600 public final void testInitWithFloodlightModuleContextSuccessNormally() throws Exception {
601 // instantiate required objects
602 FlowManager fm = new FlowManager();
603
604 // setup expectations
605 expectInitWithContext();
606
607 // start the test
608 replayAll();
609
610 fm.init(context);
611
612 // verify the test
613 verifyAll();
614 }
615
616 /**
617 * Test method for {@link FlowManager#startUp(FloodlightModuleContext)}.
618 * @throws Exception
619 */
620 @Test
621 public final void testStartupSuccessNormally() throws Exception {
622 // create mock objects
623 mockStaticPartial(Executors.class, "newScheduledThreadPool");
624 ScheduledExecutorService scheduler = createMock(ScheduledExecutorService.class);
625
Toshio Koidefe2625e2013-06-26 13:59:53 -0700626 // instantiate required objects
627 FlowManager fm = new FlowManager();
628
629 // setup expectations
630 expectInitWithContext();
631 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
632 expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler);
633 expect(scheduler.scheduleAtFixedRate(
634 EasyMock.anyObject(Runnable.class),
635 EasyMock.anyLong(),
636 EasyMock.anyLong(),
637 EasyMock.anyObject(TimeUnit.class))).andReturn(null).times(2);
638 restApi.addRestletRoutable(EasyMock.anyObject(FlowWebRoutable.class));
639
640 // start the test
641 replayAll();
642
643 fm.init(context);
644 fm.startUp(context);
645
646 // verify the test
647 verifyAll();
648 }
Toshio Koideca7abe02013-06-27 17:30:17 -0700649
650
651 // other methods
652
Toshio Koideca7abe02013-06-27 17:30:17 -0700653 /**
Toshio Koideca7abe02013-06-27 17:30:17 -0700654 * Test method for {@link FlowManager#reconcileFlow(IFlowPath, DataPath)}.
655 * @throws Exception
656 */
657 @Test
658 public final void testReconcileFlowWithFlowPathAndDataPathSuccessNormally() throws Exception {
659 final String addFlowEntry = "addFlowEntry";
660
661 // create mock objects
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700662 IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 1, 2, 2);
Toshio Koideca7abe02013-06-27 17:30:17 -0700663 IFlowEntry iFlowEntry1 = createMock(IFlowEntry.class);
664 IFlowEntry iFlowEntry2 = createMock(IFlowEntry.class);
665 FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry);
666
667 // instantiate required objects
668 FlowEntry flowEntry1 = new FlowEntry();
669 flowEntry1.setDpid(new Dpid(1));
670 flowEntry1.setFlowId(new FlowId(1));
671 flowEntry1.setInPort(new Port((short) 1));
672 flowEntry1.setOutPort(new Port((short) 11));
673 flowEntry1.setFlowEntryId(new FlowEntryId(1));
674 flowEntry1.setFlowEntryMatch(new FlowEntryMatch());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700675 flowEntry1.setFlowEntryActions(new FlowEntryActions());
Toshio Koideca7abe02013-06-27 17:30:17 -0700676 flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState());
677
678 FlowEntry flowEntry2 = new FlowEntry();
679 flowEntry2.setDpid(new Dpid(2));
680 flowEntry2.setFlowId(new FlowId(2));
681 flowEntry2.setInPort(new Port((short) 22));
682 flowEntry2.setOutPort(new Port((short) 2));
683 flowEntry2.setFlowEntryId(new FlowEntryId(2));
684 flowEntry2.setFlowEntryMatch(new FlowEntryMatch());
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700685 flowEntry2.setFlowEntryActions(new FlowEntryActions());
Toshio Koideca7abe02013-06-27 17:30:17 -0700686 flowEntry2.setFlowEntryErrorState(new FlowEntryErrorState());
687
688 DataPath dataPath = new DataPath();
689 ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>();
690 flowEntries.add(flowEntry1);
691 flowEntries.add(flowEntry2);
692 dataPath.setFlowEntries(flowEntries);
693
694 ArrayList<IFlowEntry> oldFlowEntries = new ArrayList<IFlowEntry>();
695 oldFlowEntries.add(iFlowEntry1);
696 oldFlowEntries.add(iFlowEntry2);
697
698 // setup expectations
699 expectInitWithContext();
Toshio Koideca7abe02013-06-27 17:30:17 -0700700 expect(iFlowPath1.getFlowEntries()).andReturn(oldFlowEntries);
701 iFlowEntry1.setUserState("FE_USER_DELETE");
702 iFlowEntry1.setSwitchState("FE_SWITCH_NOT_UPDATED");
703 iFlowEntry2.setUserState("FE_USER_DELETE");
704 iFlowEntry2.setSwitchState("FE_SWITCH_NOT_UPDATED");
705 expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry1).andReturn(null);
706 expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry2).andReturn(null);
707
708 // start the test
709 replayAll();
710
711 fm.init(context);
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700712 // Use reflection to test the private method
713 // Boolean result = fm.reconcileFlow(iFlowPath1, dataPath);
Naoki Shiota991093a2013-12-10 14:47:18 -0800714 Class<?> fmClass = FlowManager.class;
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700715 Method method = fmClass.getDeclaredMethod(
716 "reconcileFlow",
717 new Class[] { IFlowPath.class, DataPath.class });
718 method.setAccessible(true);
719 Boolean result = (Boolean)method.invoke(fm,
720 new Object[] { iFlowPath1, dataPath });
Toshio Koideca7abe02013-06-27 17:30:17 -0700721
722 // verify the test
723 verifyAll();
724 assertTrue(result);
725 // TODO: write more asserts
726 }
727
728 /**
729 * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, IFlowPath, IFlowEntry)}.
730 * @throws Exception
731 */
732 @Test
733 public final void testInstallFlowEntryWithIFlowPathSuccessNormally() throws Exception {
734 // create mock object
735 IOFSwitch iofSwitch = createNiceMock(IOFSwitch.class);
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700736 IFlowPath iFlowPath = createIFlowPathMock(1, "id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 2, 3, 4);
Toshio Koideca7abe02013-06-27 17:30:17 -0700737 IFlowEntry iFlowEntry = createMock(IFlowEntry.class);
738 BasicFactory basicFactory = createMock(BasicFactory.class);
739
740 // instantiate required objects
741 FlowManager fm = new FlowManager();
742
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700743 FlowEntryAction action = new FlowEntryAction();
744 action.setActionOutput(new Port((short)2));
745 FlowEntryActions actions = new FlowEntryActions();
746 actions.addAction(action);
747
Toshio Koideca7abe02013-06-27 17:30:17 -0700748 // setup expectations
749 expectInitWithContext();
750 expect(iFlowEntry.getFlowEntryId()).andReturn(new FlowEntryId(123).toString());
751 expect(iFlowEntry.getUserState()).andReturn("FE_USER_ADD");
752 iFlowEntry.setSwitchState("FE_SWITCH_UPDATED");
753 expect(iFlowEntry.getMatchInPort()).andReturn(new Short((short) 1));
Toshio Koideca7abe02013-06-27 17:30:17 -0700754 expect(iFlowEntry.getMatchSrcMac()).andReturn("01:23:45:67:89:01");
755 expect(iFlowEntry.getMatchDstMac()).andReturn("01:23:45:67:89:02");
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700756 expect(iFlowEntry.getMatchEthernetFrameType()).andReturn(new Short((short)0x0800));
757 expect(iFlowEntry.getMatchVlanId()).andReturn(new Short((short)0x1234));
758 expect(iFlowEntry.getMatchVlanPriority()).andReturn(new Byte((byte)0x10));
759 expect(iFlowEntry.getMatchSrcIPv4Net()).andReturn("192.168.0.1");
760 expect(iFlowEntry.getMatchDstIPv4Net()).andReturn("192.168.0.2");
761 expect(iFlowEntry.getMatchIpProto()).andReturn(new Byte((byte)0x20));
762 expect(iFlowEntry.getMatchIpToS()).andReturn(new Byte((byte)0x3));
763 expect(iFlowEntry.getMatchSrcTcpUdpPort()).andReturn(new Short((short)40000));
764 expect(iFlowEntry.getMatchDstTcpUdpPort()).andReturn(new Short((short)80));
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700765 expect(iFlowEntry.getActions()).andReturn(actions.toString());
Toshio Koideca7abe02013-06-27 17:30:17 -0700766 expect(floodlightProvider.getOFMessageFactory()).andReturn(basicFactory);
767 expect(basicFactory.getMessage(OFType.FLOW_MOD)).andReturn(new OFFlowMod());
768 expect(iofSwitch.getStringId()).andReturn(new Dpid(100).toString());
769
770 // start the test
771 replayAll();
772
773 fm.init(context);
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700774 // Use reflection to test the private method
775 // Boolean result = fm.installFlowEntry(iofSwitch, iFlowPath, iFlowEntry);
Naoki Shiota991093a2013-12-10 14:47:18 -0800776 Class<?> fmClass = FlowManager.class;
Pavlin Radoslavov1308dc62013-10-25 15:54:31 -0700777 Method method = fmClass.getDeclaredMethod(
778 "installFlowEntry",
779 new Class[] { IOFSwitch.class, IFlowPath.class, IFlowEntry.class });
780 method.setAccessible(true);
781 Boolean result = (Boolean)method.invoke(fm,
782 new Object[] { iofSwitch, iFlowPath, iFlowEntry });
783
Toshio Koideca7abe02013-06-27 17:30:17 -0700784
785 // verify the test
786 verifyAll();
787 assertTrue(result);
788 // TODO: write more asserts
789 }
790
791 /**
792 * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}.
793 * The method seems to be not used for now.
794 */
795 @Ignore @Test
796 public final void testInstallFlowEntryWithFlowPathSuccessNormally() {
797 fail("not yet implemented");
798 }
799
800 /**
801 * Test method for {@link FlowManager#removeFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}.
802 * The method seems to be not implemented and not used for now.
803 */
804 @Ignore @Test
805 public final void testRemoveFlowEntrySuccessNormally() {
806 fail("not yet implemented");
807 }
Pavlin Radoslavovddd01ba2013-07-03 15:40:44 -0700808}