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