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