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, |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 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 | } |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 242 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 243 | /** |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 244 | * Test method for {@link FlowManager#deleteFlow(FlowId)}. |
| 245 | * @throws Exception |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 246 | */ |
| 247 | @Test |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 248 | public final void testDeleteFlowSuccessNormally() throws Exception { |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 249 | // create mock objects |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 250 | IFlowPath flowPath = createIFlowPathMock(123, "id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 2, 3, 4); |
| 251 | IFlowEntry flowEntry1 = createMock(IFlowEntry.class); |
| 252 | IFlowEntry flowEntry2 = createMock(IFlowEntry.class); |
| 253 | IFlowEntry flowEntry3 = createMock(IFlowEntry.class); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 254 | |
| 255 | // instantiate required objects |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 256 | FlowManager fm = new FlowManager(); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 257 | FlowId flowId = new FlowId(123); |
| 258 | ArrayList<IFlowEntry> flowEntries = new ArrayList<IFlowEntry>(); |
| 259 | flowEntries.add(flowEntry1); |
| 260 | flowEntries.add(flowEntry2); |
| 261 | flowEntries.add(flowEntry3); |
| 262 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 263 | // setup expectations |
| 264 | expectInitWithContext(); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 265 | expect(op.searchFlowPath(cmpEq(flowId))).andReturn(flowPath); |
| 266 | expect(flowPath.getFlowEntries()).andReturn(flowEntries); |
| 267 | flowPath.removeFlowEntry(flowEntry1); |
| 268 | flowPath.removeFlowEntry(flowEntry2); |
| 269 | flowPath.removeFlowEntry(flowEntry3); |
| 270 | op.removeFlowEntry(flowEntry1); |
| 271 | op.removeFlowEntry(flowEntry2); |
| 272 | op.removeFlowEntry(flowEntry3); |
| 273 | op.removeFlowPath(flowPath); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 274 | op.commit(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 275 | |
| 276 | // start the test |
| 277 | replayAll(); |
| 278 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 279 | fm.init(context); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 280 | fm.deleteFlow(flowId); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 281 | |
| 282 | // verify the test |
| 283 | verifyAll(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Test method for {@link FlowManager#deleteFlow(FlowId)}. |
| 288 | * @throws Exception |
| 289 | */ |
| 290 | @Test |
| 291 | public final void testDeleteFlowSuccessEmptyFlowPath() throws Exception { |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 292 | // instantiate required objects |
| 293 | FlowManager fm = new FlowManager(); |
| 294 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 295 | // create mock objects |
| 296 | IFlowPath flowObj = createNiceMock(IFlowPath.class); |
| 297 | |
| 298 | // setup expectations |
| 299 | expectInitWithContext(); |
| 300 | expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(flowObj); |
| 301 | expect(flowObj.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()); |
| 302 | op.removeFlowPath(flowObj); |
| 303 | op.commit(); |
| 304 | expectLastCall().anyTimes(); |
| 305 | |
| 306 | // start the test |
| 307 | replayAll(); |
| 308 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 309 | fm.init(context); |
| 310 | Boolean result = fm.deleteFlow(new FlowId(1)); |
| 311 | |
| 312 | // verify the test |
| 313 | verifyAll(); |
| 314 | assertTrue(result); |
| 315 | } |
| 316 | |
| 317 | /** |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 318 | * Test method for {@link FlowManager#deleteAllFlows()}. |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 319 | * @throws Exception |
| 320 | */ |
| 321 | @Test |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 322 | public final void testDeleteAllFlowsSuccessNormally() throws Exception { |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 323 | // create mock objects |
| 324 | IFlowPath flowPath1 = createNiceMock(IFlowPath.class); |
| 325 | IFlowPath flowPath2 = createNiceMock(IFlowPath.class); |
| 326 | IFlowPath flowPath3 = createNiceMock(IFlowPath.class); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 327 | FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, "deleteFlow"); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 328 | |
| 329 | // instantiate required objects |
| 330 | ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>(); |
| 331 | flowPaths.add(flowPath1); |
| 332 | flowPaths.add(flowPath2); |
| 333 | flowPaths.add(null); |
| 334 | flowPaths.add(flowPath3); |
| 335 | |
| 336 | // setup expectations |
| 337 | expectInitWithContext(); |
| 338 | expect(op.getAllFlowPaths()).andReturn(flowPaths); |
| 339 | expect(flowPath1.getFlowId()).andReturn(new FlowId(1).toString()); |
| 340 | expect(flowPath2.getFlowId()).andReturn(null); |
| 341 | expect(flowPath3.getFlowId()).andReturn(new FlowId(3).toString()); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 342 | expect(fm.deleteFlow(cmpEq(new FlowId(1)))).andReturn(true); |
| 343 | expect(fm.deleteFlow(cmpEq(new FlowId(3)))).andReturn(true); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 344 | |
| 345 | // start the test |
| 346 | replayAll(); |
| 347 | |
| 348 | fm.init(context); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 349 | Boolean result = fm.deleteAllFlows(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 350 | |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 351 | // verify the test |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 352 | verifyAll(); |
| 353 | assertTrue(result); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Test method for {@link FlowManager#getFlow()}. |
| 358 | * @throws Exception |
| 359 | */ |
| 360 | @Test |
| 361 | public final void testGetFlowSuccessNormally() throws Exception { |
| 362 | // instantiate required objects |
| 363 | FlowManager fm = new FlowManager(); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 364 | 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] | 365 | |
| 366 | // setup expectations |
| 367 | expectInitWithContext(); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 368 | expect(op.searchFlowPath(cmpEq(new FlowId(1)))).andReturn(iFlowPath); |
| 369 | expect(iFlowPath.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 370 | op.commit(); |
| 371 | |
| 372 | // start the test |
| 373 | replayAll(); |
| 374 | |
| 375 | fm.init(context); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 376 | FlowPath flowPath = fm.getFlow(new FlowId(1)); |
| 377 | String installerId = flowPath.installerId().toString(); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 378 | String flowPathType = flowPath.flowPathType().toString(); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 379 | String flowPathUserState = flowPath.flowPathUserState().toString(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 380 | long flowPathFlags = flowPath.flowPathFlags().flags(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 381 | |
| 382 | //verify the test |
| 383 | verifyAll(); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 384 | assertEquals("caller id", installerId); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 385 | assertEquals("FP_TYPE_SHORTEST_PATH", flowPathType); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 386 | assertEquals("FP_USER_ADD", flowPathUserState); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 387 | assertEquals(0L, flowPathFlags); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Test method for {@link FlowManager#getAllFlows(CallerId, DataPathEndpoints)}. |
| 392 | * @throws Exception |
| 393 | */ |
| 394 | @Test |
| 395 | public final void testGetAllFlowsWithCallerIdAndDataPathEndpointsSuccessNormally() throws Exception { |
| 396 | final String getAllFlows = "getAllFlows"; |
| 397 | // create mock objects |
| 398 | FlowManager fm = createPartialMock(FlowManager.class, getAllFlows, |
| 399 | new Class<?>[]{}, new Object[]{}); |
| 400 | |
| 401 | // instantiate required objects |
| 402 | DataPathEndpoints dataPathEndpoints = new DataPathEndpoints( |
| 403 | new SwitchPort(new Dpid(1), new Port((short)1)), |
| 404 | new SwitchPort(new Dpid(2), new Port((short)2))); |
| 405 | |
| 406 | ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths(); |
| 407 | |
| 408 | //setup expectations |
| 409 | expectInitWithContext(); |
| 410 | expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows); |
| 411 | |
| 412 | //start the test |
| 413 | replayAll(); |
| 414 | |
| 415 | fm.init(context); |
| 416 | ArrayList<FlowPath> flows = fm.getAllFlows(new CallerId("caller id"), dataPathEndpoints); |
| 417 | |
| 418 | // verify the test |
| 419 | verifyAll(); |
| 420 | assertEquals(1, flows.size()); |
| 421 | assertEquals(obtainedAllFlows.get(1), flows.get(0)); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Test method for {@link FlowManager#getAllFlows(DataPathEndpoints)}. |
| 426 | * @throws Exception |
| 427 | */ |
| 428 | @Test |
| 429 | public final void testGetAllFlowsWithDataPathEndpointsSuccessNormally() throws Exception { |
| 430 | final String getAllFlows = "getAllFlows"; |
| 431 | // create mock objects |
| 432 | FlowManager fm = createPartialMock(FlowManager.class, getAllFlows, |
| 433 | new Class<?>[]{}, new Object[]{}); |
| 434 | |
| 435 | // instantiate required objects |
| 436 | DataPathEndpoints dataPathEndpoints = new DataPathEndpoints( |
| 437 | new SwitchPort(new Dpid(1), new Port((short)1)), |
| 438 | new SwitchPort(new Dpid(2), new Port((short)2))); |
| 439 | |
| 440 | ArrayList<FlowPath> obtainedAllFlows = createTestFlowPaths(); |
| 441 | |
| 442 | //setup expectations |
| 443 | expectInitWithContext(); |
| 444 | expectPrivate(fm, getAllFlows).andReturn(obtainedAllFlows); |
| 445 | |
| 446 | //start the test |
| 447 | replayAll(); |
| 448 | |
| 449 | fm.init(context); |
| 450 | ArrayList<FlowPath> flows = fm.getAllFlows(dataPathEndpoints); |
| 451 | |
| 452 | // verify the test |
| 453 | verifyAll(); |
| 454 | assertEquals(2, flows.size()); |
| 455 | assertEquals(obtainedAllFlows.get(0), flows.get(0)); |
| 456 | assertEquals(obtainedAllFlows.get(1), flows.get(1)); |
| 457 | // TODO: ignore the order of flows in the list |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Test method for {@link FlowManager#getAllFlowsSummary(FlowId, int)}. |
| 462 | * @throws Exception |
| 463 | */ |
| 464 | @Test |
| 465 | public final void testGetAllFlowsSummarySuccessNormally() throws Exception { |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 466 | final String getAllFlowsWithDataPathSummary = "getAllFlowsWithDataPathSummary"; |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 467 | // create mock objects |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 468 | FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, getAllFlowsWithDataPathSummary); |
| 469 | FlowPath flowPath1 = createTestFlowPath(1, "", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 2, 3, 4); |
| 470 | FlowPath flowPath2 = createTestFlowPath(5, "", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 2, 3, 4, 5); |
| 471 | FlowPath flowPath3 = createTestFlowPath(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] | 472 | |
| 473 | // instantiate required objects |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 474 | ArrayList<FlowPath> flows = new ArrayList<FlowPath>(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 475 | flows.add(flowPath3); |
| 476 | flows.add(flowPath1); |
| 477 | flows.add(flowPath2); |
| 478 | |
| 479 | // setup expectations |
| 480 | expectInitWithContext(); |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 481 | expectPrivate(fm, getAllFlowsWithDataPathSummary).andReturn(flows); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 482 | |
| 483 | // start the test |
| 484 | replayAll(); |
| 485 | |
| 486 | fm.init(context); |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 487 | ArrayList<FlowPath> returnedFlows = fm.getAllFlowsSummary(null, 0); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 488 | |
| 489 | // verify the test |
| 490 | verifyAll(); |
| 491 | assertEquals(3, returnedFlows.size()); |
Pavlin Radoslavov | 4ef6ba2 | 2013-11-22 19:32:58 -0800 | [diff] [blame] | 492 | assertEquals(1, new FlowId(returnedFlows.get(0).flowId().value()).value()); |
| 493 | assertEquals(5, new FlowId(returnedFlows.get(1).flowId().value()).value()); |
| 494 | assertEquals(10, new FlowId(returnedFlows.get(2).flowId().value()).value()); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Test method for {@link FlowManager#getAllFlows()}. |
| 499 | * @throws Exception |
| 500 | */ |
| 501 | @Test |
| 502 | public final void testGetAllFlowsSuccessNormally() throws Exception { |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 503 | // create mock objects |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 504 | IFlowPath iFlowPath1 = createIFlowPathMock(1, "caller id", "FP_TYPE_SHORTEST_PATH", "FP_USER_ADD", 0, 1, 1, 2, 2); |
| 505 | 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] | 506 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 507 | // instantiate required objects |
| 508 | ArrayList<IFlowPath> flowPaths = new ArrayList<IFlowPath>(); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 509 | flowPaths.add(iFlowPath1); |
| 510 | flowPaths.add(iFlowPath2); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 511 | FlowManager fm = new FlowManager(); |
| 512 | |
| 513 | // setup expectations |
| 514 | expectInitWithContext(); |
| 515 | expect(op.getAllFlowPaths()).andReturn(flowPaths); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 516 | expect(iFlowPath1.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes(); |
| 517 | expect(iFlowPath2.getFlowEntries()).andReturn(new ArrayList<IFlowEntry>()).anyTimes(); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 518 | op.commit(); |
| 519 | |
| 520 | // start the test |
| 521 | replayAll(); |
| 522 | |
| 523 | fm.init(context); |
| 524 | ArrayList<FlowPath> flows = fm.getAllFlows(); |
| 525 | |
| 526 | // verify the test |
| 527 | verifyAll(); |
| 528 | assertEquals(2, flows.size()); |
| 529 | assertEquals(new SwitchPort(new Dpid(1), new Port((short)1)).toString(), |
| 530 | flows.get(0).dataPath().srcPort().toString()); |
| 531 | assertEquals(new SwitchPort(new Dpid(2), new Port((short)5)).toString(), |
| 532 | flows.get(1).dataPath().srcPort().toString()); |
| 533 | // TODO: more asserts |
| 534 | // TODO: ignore seq. of the list |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Test method for {@link FlowManager#addAndMaintainShortestPathFlow(FlowPath)}. |
| 539 | * @throws Exception |
| 540 | */ |
| 541 | @Test |
| 542 | public final void testAddAndMaintainShortestPathFlowSuccessNormally() throws Exception { |
| 543 | final String addFlow = "addFlow"; |
| 544 | |
| 545 | // create mock objects |
| 546 | FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlow); |
| 547 | |
| 548 | // instantiate required objects |
| 549 | DataPath dataPath = new DataPath(); |
| 550 | dataPath.setSrcPort(new SwitchPort(new Dpid(1), new Port((short)3))); |
| 551 | dataPath.setDstPort(new SwitchPort(new Dpid(2), new Port((short)4))); |
| 552 | FlowEntryMatch match = new FlowEntryMatch(); |
| 553 | FlowPath paramFlow = new FlowPath(); |
| 554 | paramFlow.setFlowId(new FlowId(100)); |
| 555 | paramFlow.setInstallerId(new CallerId("installer id")); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 556 | paramFlow.setFlowPathType(FlowPathType.valueOf("FP_TYPE_SHORTEST_PATH")); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 557 | paramFlow.setFlowPathUserState(FlowPathUserState.valueOf("FP_USER_ADD")); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 558 | paramFlow.setFlowPathFlags(new FlowPathFlags(0)); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 559 | paramFlow.setDataPath(dataPath); |
| 560 | paramFlow.setFlowEntryMatch(match); |
| 561 | |
| 562 | // setup expectations |
| 563 | expectInitWithContext(); |
| 564 | expectPrivate(fm, addFlow, |
| 565 | EasyMock.anyObject(FlowPath.class), |
| 566 | EasyMock.anyObject(FlowId.class), |
| 567 | EasyMock.anyObject(String.class) |
| 568 | ).andAnswer(new IAnswer<Object>() { |
| 569 | public Object answer() throws Exception { |
| 570 | FlowPath flowPath = (FlowPath)EasyMock.getCurrentArguments()[0]; |
| 571 | assertEquals(flowPath.flowId().value(), 100); |
| 572 | assertEquals(flowPath.installerId().toString(), "installer id"); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 573 | assertEquals(flowPath.flowPathType().toString(), "PF_TYPE_SHORTEST_PATH"); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 574 | assertEquals(flowPath.flowPathUserState().toString(), "PF_USER_STATE"); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 575 | assertEquals(flowPath.flowPathFlags().flags(), 0); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 576 | assertEquals(flowPath.dataPath().srcPort().toString(), |
| 577 | new SwitchPort(new Dpid(1), new Port((short)3)).toString()); |
| 578 | |
| 579 | String dataPathSummary = (String)EasyMock.getCurrentArguments()[2]; |
| 580 | assertEquals(dataPathSummary, "X"); |
| 581 | |
| 582 | return true; |
| 583 | } |
| 584 | }); |
| 585 | |
| 586 | // start the test |
| 587 | replayAll(); |
| 588 | |
| 589 | fm.init(context); |
| 590 | FlowPath resultFlow = fm.addAndMaintainShortestPathFlow(paramFlow); |
| 591 | |
| 592 | // verify the test |
| 593 | verifyAll(); |
| 594 | assertEquals(paramFlow.flowId().value(), resultFlow.flowId().value()); |
| 595 | assertEquals(paramFlow.installerId().toString(), resultFlow.installerId().toString()); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 596 | assertEquals(paramFlow.flowPathType().toString(), resultFlow.flowPathType().toString()); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 597 | assertEquals(paramFlow.flowPathUserState().toString(), resultFlow.flowPathUserState().toString()); |
Pavlin Radoslavov | 204b286 | 2013-07-12 14:15:36 -0700 | [diff] [blame] | 598 | assertEquals(paramFlow.flowPathFlags().flags(), resultFlow.flowPathFlags().flags()); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 599 | assertEquals(paramFlow.dataPath().toString(), resultFlow.dataPath().toString()); |
| 600 | assertEquals(paramFlow.flowEntryMatch().toString(), resultFlow.flowEntryMatch().toString()); |
| 601 | } |
| 602 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 603 | // INetMapStorage methods |
| 604 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 605 | /** |
| 606 | * Test method for {@link FlowManager#init(String)}. |
| 607 | * @throws Exception |
| 608 | */ |
| 609 | @Test |
| 610 | public final void testInitSuccessNormally() throws Exception { |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 611 | // instantiate required objects |
| 612 | FlowManager fm = new FlowManager(); |
| 613 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 614 | // create mock objects |
| 615 | op = createMock(GraphDBOperation.class); |
| 616 | |
| 617 | // setup expectations |
| 618 | expectNew(GraphDBOperation.class, "/dummy/path").andReturn(op); |
| 619 | |
| 620 | // start the test |
| 621 | replayAll(); |
| 622 | |
yoshi | 2fd4c7e | 2013-11-22 15:47:55 -0800 | [diff] [blame] | 623 | fm.init("dummy_store", "/dummy/path"); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 624 | |
| 625 | // verify the test |
| 626 | verifyAll(); |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Test method for {@link FlowManager#close()}. |
| 631 | * @throws Exception |
| 632 | */ |
| 633 | @Test |
| 634 | public final void testCloseSuccessNormally() throws Exception { |
| 635 | // instantiate required objects |
| 636 | FlowManager fm = new FlowManager(); |
| 637 | |
| 638 | // setup expectations |
| 639 | expectInitWithContext(); |
| 640 | op.close(); |
| 641 | |
| 642 | // start the test |
| 643 | replayAll(); |
| 644 | |
| 645 | fm.init(context); |
| 646 | fm.close(); |
| 647 | |
| 648 | // verify the test |
| 649 | verifyAll(); |
| 650 | } |
| 651 | |
| 652 | |
| 653 | // IFloodlightModule methods |
| 654 | |
| 655 | |
| 656 | /** |
| 657 | * Test method for {@link FlowManager#getModuleServices()}. |
| 658 | * @throws Exception |
| 659 | */ |
| 660 | @Test |
| 661 | public final void testGetModuleServicesSuccessNormally() throws Exception { |
| 662 | // instantiate required objects |
| 663 | FlowManager fm = new FlowManager(); |
| 664 | |
| 665 | // setup expectations |
| 666 | expectInitWithContext(); |
| 667 | |
| 668 | // start the test |
| 669 | replayAll(); |
| 670 | |
| 671 | fm.init(context); |
| 672 | Collection<Class<? extends IFloodlightService>> l = fm.getModuleServices(); |
| 673 | |
| 674 | // verify the test |
| 675 | verifyAll(); |
| 676 | assertEquals(1, l.size()); |
| 677 | assertEquals(IFlowService.class, l.iterator().next()); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Test method for {@link FlowManager#getServiceImpls()}. |
| 682 | * @throws Exception |
| 683 | */ |
| 684 | @Test |
| 685 | public final void testGetServiceImplsSuccessNormally() throws Exception { |
| 686 | // instantiate required objects |
| 687 | FlowManager fm = new FlowManager(); |
| 688 | |
| 689 | // setup expectations |
| 690 | expectInitWithContext(); |
| 691 | |
| 692 | // start the test |
| 693 | replayAll(); |
| 694 | |
| 695 | fm.init(context); |
| 696 | Map<Class<? extends IFloodlightService>, IFloodlightService> si = fm.getServiceImpls(); |
| 697 | |
| 698 | // verify the test |
| 699 | verifyAll(); |
| 700 | assertEquals(1, si.size()); |
| 701 | assertTrue(si.containsKey(IFlowService.class)); |
| 702 | assertEquals(fm, si.get(IFlowService.class)); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Test method for {@link FlowManager#getModuleDependencies()}. |
| 707 | * @throws Exception |
| 708 | */ |
| 709 | @Test |
| 710 | public final void testGetModuleDependenciesSuccessNormally() throws Exception { |
| 711 | // instantiate required objects |
| 712 | FlowManager fm = new FlowManager(); |
| 713 | |
| 714 | // setup expectations |
| 715 | expectInitWithContext(); |
| 716 | |
| 717 | // start the test |
| 718 | replayAll(); |
| 719 | |
| 720 | fm.init(context); |
| 721 | Collection<Class<? extends IFloodlightService>> md = fm.getModuleDependencies(); |
| 722 | |
| 723 | // verify the test |
| 724 | verifyAll(); |
Pavlin Radoslavov | 0537827 | 2013-10-19 23:23:05 -0700 | [diff] [blame] | 725 | assertEquals(4, md.size()); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 726 | assertTrue(md.contains(IFloodlightProviderService.class)); |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 727 | assertTrue(md.contains(IRestApiService.class)); |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Test method for {@link FlowManager#init(FloodlightModuleContext)}. |
| 732 | * @throws Exception |
| 733 | */ |
| 734 | @Test |
| 735 | public final void testInitWithFloodlightModuleContextSuccessNormally() throws Exception { |
| 736 | // instantiate required objects |
| 737 | FlowManager fm = new FlowManager(); |
| 738 | |
| 739 | // setup expectations |
| 740 | expectInitWithContext(); |
| 741 | |
| 742 | // start the test |
| 743 | replayAll(); |
| 744 | |
| 745 | fm.init(context); |
| 746 | |
| 747 | // verify the test |
| 748 | verifyAll(); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Test method for {@link FlowManager#startUp(FloodlightModuleContext)}. |
| 753 | * @throws Exception |
| 754 | */ |
| 755 | @Test |
| 756 | public final void testStartupSuccessNormally() throws Exception { |
| 757 | // create mock objects |
| 758 | mockStaticPartial(Executors.class, "newScheduledThreadPool"); |
| 759 | ScheduledExecutorService scheduler = createMock(ScheduledExecutorService.class); |
| 760 | |
Toshio Koide | fe2625e | 2013-06-26 13:59:53 -0700 | [diff] [blame] | 761 | // instantiate required objects |
| 762 | FlowManager fm = new FlowManager(); |
| 763 | |
| 764 | // setup expectations |
| 765 | expectInitWithContext(); |
| 766 | expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler); |
| 767 | expect(Executors.newScheduledThreadPool(1)).andReturn(scheduler); |
| 768 | expect(scheduler.scheduleAtFixedRate( |
| 769 | EasyMock.anyObject(Runnable.class), |
| 770 | EasyMock.anyLong(), |
| 771 | EasyMock.anyLong(), |
| 772 | EasyMock.anyObject(TimeUnit.class))).andReturn(null).times(2); |
| 773 | restApi.addRestletRoutable(EasyMock.anyObject(FlowWebRoutable.class)); |
| 774 | |
| 775 | // start the test |
| 776 | replayAll(); |
| 777 | |
| 778 | fm.init(context); |
| 779 | fm.startUp(context); |
| 780 | |
| 781 | // verify the test |
| 782 | verifyAll(); |
| 783 | } |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 784 | |
| 785 | |
| 786 | // other methods |
| 787 | |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 788 | /** |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 789 | * Test method for {@link FlowManager#reconcileFlow(IFlowPath, DataPath)}. |
| 790 | * @throws Exception |
| 791 | */ |
| 792 | @Test |
| 793 | public final void testReconcileFlowWithFlowPathAndDataPathSuccessNormally() throws Exception { |
| 794 | final String addFlowEntry = "addFlowEntry"; |
| 795 | |
| 796 | // create mock objects |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 797 | 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] | 798 | IFlowEntry iFlowEntry1 = createMock(IFlowEntry.class); |
| 799 | IFlowEntry iFlowEntry2 = createMock(IFlowEntry.class); |
| 800 | FlowManager fm = createPartialMockAndInvokeDefaultConstructor(FlowManager.class, addFlowEntry); |
| 801 | |
| 802 | // instantiate required objects |
| 803 | FlowEntry flowEntry1 = new FlowEntry(); |
| 804 | flowEntry1.setDpid(new Dpid(1)); |
| 805 | flowEntry1.setFlowId(new FlowId(1)); |
| 806 | flowEntry1.setInPort(new Port((short) 1)); |
| 807 | flowEntry1.setOutPort(new Port((short) 11)); |
| 808 | flowEntry1.setFlowEntryId(new FlowEntryId(1)); |
| 809 | flowEntry1.setFlowEntryMatch(new FlowEntryMatch()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 810 | flowEntry1.setFlowEntryActions(new FlowEntryActions()); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 811 | flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState()); |
| 812 | |
| 813 | FlowEntry flowEntry2 = new FlowEntry(); |
| 814 | flowEntry2.setDpid(new Dpid(2)); |
| 815 | flowEntry2.setFlowId(new FlowId(2)); |
| 816 | flowEntry2.setInPort(new Port((short) 22)); |
| 817 | flowEntry2.setOutPort(new Port((short) 2)); |
| 818 | flowEntry2.setFlowEntryId(new FlowEntryId(2)); |
| 819 | flowEntry2.setFlowEntryMatch(new FlowEntryMatch()); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 820 | flowEntry2.setFlowEntryActions(new FlowEntryActions()); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 821 | flowEntry2.setFlowEntryErrorState(new FlowEntryErrorState()); |
| 822 | |
| 823 | DataPath dataPath = new DataPath(); |
| 824 | ArrayList<FlowEntry> flowEntries = new ArrayList<FlowEntry>(); |
| 825 | flowEntries.add(flowEntry1); |
| 826 | flowEntries.add(flowEntry2); |
| 827 | dataPath.setFlowEntries(flowEntries); |
| 828 | |
| 829 | ArrayList<IFlowEntry> oldFlowEntries = new ArrayList<IFlowEntry>(); |
| 830 | oldFlowEntries.add(iFlowEntry1); |
| 831 | oldFlowEntries.add(iFlowEntry2); |
| 832 | |
| 833 | // setup expectations |
| 834 | expectInitWithContext(); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 835 | expect(iFlowPath1.getFlowEntries()).andReturn(oldFlowEntries); |
| 836 | iFlowEntry1.setUserState("FE_USER_DELETE"); |
| 837 | iFlowEntry1.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 838 | iFlowEntry2.setUserState("FE_USER_DELETE"); |
| 839 | iFlowEntry2.setSwitchState("FE_SWITCH_NOT_UPDATED"); |
| 840 | expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry1).andReturn(null); |
| 841 | expectPrivate(fm, addFlowEntry, iFlowPath1, flowEntry2).andReturn(null); |
| 842 | |
| 843 | // start the test |
| 844 | replayAll(); |
| 845 | |
| 846 | fm.init(context); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 847 | // Use reflection to test the private method |
| 848 | // Boolean result = fm.reconcileFlow(iFlowPath1, dataPath); |
| 849 | Class fmClass = FlowManager.class; |
| 850 | Method method = fmClass.getDeclaredMethod( |
| 851 | "reconcileFlow", |
| 852 | new Class[] { IFlowPath.class, DataPath.class }); |
| 853 | method.setAccessible(true); |
| 854 | Boolean result = (Boolean)method.invoke(fm, |
| 855 | new Object[] { iFlowPath1, dataPath }); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 856 | |
| 857 | // verify the test |
| 858 | verifyAll(); |
| 859 | assertTrue(result); |
| 860 | // TODO: write more asserts |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, IFlowPath, IFlowEntry)}. |
| 865 | * @throws Exception |
| 866 | */ |
| 867 | @Test |
| 868 | public final void testInstallFlowEntryWithIFlowPathSuccessNormally() throws Exception { |
| 869 | // create mock object |
| 870 | IOFSwitch iofSwitch = createNiceMock(IOFSwitch.class); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 871 | 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] | 872 | IFlowEntry iFlowEntry = createMock(IFlowEntry.class); |
| 873 | BasicFactory basicFactory = createMock(BasicFactory.class); |
| 874 | |
| 875 | // instantiate required objects |
| 876 | FlowManager fm = new FlowManager(); |
| 877 | |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 878 | FlowEntryAction action = new FlowEntryAction(); |
| 879 | action.setActionOutput(new Port((short)2)); |
| 880 | FlowEntryActions actions = new FlowEntryActions(); |
| 881 | actions.addAction(action); |
| 882 | |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 883 | // setup expectations |
| 884 | expectInitWithContext(); |
| 885 | expect(iFlowEntry.getFlowEntryId()).andReturn(new FlowEntryId(123).toString()); |
| 886 | expect(iFlowEntry.getUserState()).andReturn("FE_USER_ADD"); |
| 887 | iFlowEntry.setSwitchState("FE_SWITCH_UPDATED"); |
| 888 | expect(iFlowEntry.getMatchInPort()).andReturn(new Short((short) 1)); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 889 | expect(iFlowEntry.getMatchSrcMac()).andReturn("01:23:45:67:89:01"); |
| 890 | expect(iFlowEntry.getMatchDstMac()).andReturn("01:23:45:67:89:02"); |
Pavlin Radoslavov | ad3a1e6 | 2013-07-09 13:30:16 -0700 | [diff] [blame] | 891 | expect(iFlowEntry.getMatchEthernetFrameType()).andReturn(new Short((short)0x0800)); |
| 892 | expect(iFlowEntry.getMatchVlanId()).andReturn(new Short((short)0x1234)); |
| 893 | expect(iFlowEntry.getMatchVlanPriority()).andReturn(new Byte((byte)0x10)); |
| 894 | expect(iFlowEntry.getMatchSrcIPv4Net()).andReturn("192.168.0.1"); |
| 895 | expect(iFlowEntry.getMatchDstIPv4Net()).andReturn("192.168.0.2"); |
| 896 | expect(iFlowEntry.getMatchIpProto()).andReturn(new Byte((byte)0x20)); |
| 897 | expect(iFlowEntry.getMatchIpToS()).andReturn(new Byte((byte)0x3)); |
| 898 | expect(iFlowEntry.getMatchSrcTcpUdpPort()).andReturn(new Short((short)40000)); |
| 899 | expect(iFlowEntry.getMatchDstTcpUdpPort()).andReturn(new Short((short)80)); |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 900 | expect(iFlowEntry.getActions()).andReturn(actions.toString()); |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 901 | expect(floodlightProvider.getOFMessageFactory()).andReturn(basicFactory); |
| 902 | expect(basicFactory.getMessage(OFType.FLOW_MOD)).andReturn(new OFFlowMod()); |
| 903 | expect(iofSwitch.getStringId()).andReturn(new Dpid(100).toString()); |
| 904 | |
| 905 | // start the test |
| 906 | replayAll(); |
| 907 | |
| 908 | fm.init(context); |
Pavlin Radoslavov | 1308dc6 | 2013-10-25 15:54:31 -0700 | [diff] [blame] | 909 | // Use reflection to test the private method |
| 910 | // Boolean result = fm.installFlowEntry(iofSwitch, iFlowPath, iFlowEntry); |
| 911 | Class fmClass = FlowManager.class; |
| 912 | Method method = fmClass.getDeclaredMethod( |
| 913 | "installFlowEntry", |
| 914 | new Class[] { IOFSwitch.class, IFlowPath.class, IFlowEntry.class }); |
| 915 | method.setAccessible(true); |
| 916 | Boolean result = (Boolean)method.invoke(fm, |
| 917 | new Object[] { iofSwitch, iFlowPath, iFlowEntry }); |
| 918 | |
Toshio Koide | ca7abe0 | 2013-06-27 17:30:17 -0700 | [diff] [blame] | 919 | |
| 920 | // verify the test |
| 921 | verifyAll(); |
| 922 | assertTrue(result); |
| 923 | // TODO: write more asserts |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Test method for {@link FlowManager#installFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}. |
| 928 | * The method seems to be not used for now. |
| 929 | */ |
| 930 | @Ignore @Test |
| 931 | public final void testInstallFlowEntryWithFlowPathSuccessNormally() { |
| 932 | fail("not yet implemented"); |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * Test method for {@link FlowManager#removeFlowEntry(net.floodlightcontroller.core.IOFSwitch, FlowPath, FlowEntry)}. |
| 937 | * The method seems to be not implemented and not used for now. |
| 938 | */ |
| 939 | @Ignore @Test |
| 940 | public final void testRemoveFlowEntrySuccessNormally() { |
| 941 | fail("not yet implemented"); |
| 942 | } |
Pavlin Radoslavov | ddd01ba | 2013-07-03 15:40:44 -0700 | [diff] [blame] | 943 | } |