Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowprogrammer; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 2 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 3 | import static org.easymock.EasyMock.anyObject; |
| 4 | import static org.easymock.EasyMock.createMock; |
| 5 | import static org.easymock.EasyMock.eq; |
| 6 | import static org.easymock.EasyMock.expect; |
| 7 | import static org.easymock.EasyMock.expectLastCall; |
| 8 | import static org.easymock.EasyMock.getCurrentArguments; |
| 9 | import static org.easymock.EasyMock.replay; |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 10 | import static org.junit.Assert.assertEquals; |
| 11 | import static org.junit.Assert.assertTrue; |
| 12 | import static org.junit.Assert.fail; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 13 | |
| 14 | import java.io.IOException; |
| 15 | import java.util.ArrayList; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 16 | import java.util.Collections; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 17 | import java.util.List; |
| 18 | import java.util.concurrent.ExecutionException; |
| 19 | import java.util.concurrent.Future; |
| 20 | |
| 21 | import net.floodlightcontroller.core.IOFSwitch; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.flowprogrammer.IFlowPusherService.MsgPriority; |
| 23 | import net.onrc.onos.core.flowprogrammer.IFlowSyncService.SyncResult; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.intent.FlowEntry; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 25 | |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 26 | import org.easymock.IAnswer; |
| 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Ignore; |
| 30 | import org.junit.Test; |
| 31 | import org.junit.runner.RunWith; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 32 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 33 | import org.powermock.modules.junit4.PowerMockRunner; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 34 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 35 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 36 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 37 | import org.projectfloodlight.openflow.protocol.OFFlowModCommand; |
| 38 | import org.projectfloodlight.openflow.protocol.OFFlowStatsEntry; |
| 39 | import org.projectfloodlight.openflow.protocol.OFFlowStatsReply; |
| 40 | import org.projectfloodlight.openflow.protocol.OFFlowStatsRequest; |
| 41 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 42 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
| 43 | import org.projectfloodlight.openflow.protocol.OFType; |
| 44 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 45 | import org.projectfloodlight.openflow.types.U64; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 46 | |
| 47 | // Test should be fixed to fit RAMCloud basis |
| 48 | @Ignore |
| 49 | @RunWith(PowerMockRunner.class) |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 50 | @PrepareForTest({FlowSynchronizer.class }) |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 51 | public class FlowSynchronizerTest { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | private FlowPusher pusher; |
| 53 | private FlowSynchronizer sync; |
| 54 | private List<Long> idAdded; |
| 55 | private List<Long> idRemoved; |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 56 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 57 | /* |
| 58 | * OF1.0 Factory for now. Change when we move to |
| 59 | * OF 1.3. |
| 60 | */ |
| 61 | private static OFFactory factory10; |
| 62 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | @Before |
| 64 | public void setUp() throws Exception { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 65 | factory10 = OFFactories.getFactory(OFVersion.OF_10); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | idAdded = new ArrayList<Long>(); |
| 67 | idRemoved = new ArrayList<Long>(); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 68 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 69 | pusher = createMock(FlowPusher.class); |
| 70 | expect(pusher.suspend(anyObject(IOFSwitch.class))).andReturn(true).anyTimes(); |
| 71 | expect(pusher.resume(anyObject(IOFSwitch.class))).andReturn(true).anyTimes(); |
| 72 | pusher.add(anyObject(IOFSwitch.class), anyObject(OFMessage.class), |
| 73 | eq(MsgPriority.HIGH)); |
| 74 | expectLastCall().andAnswer(new IAnswer<Object>() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public Object answer() throws Throwable { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 77 | OFMessage msg = (OFMessage) getCurrentArguments()[1]; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | if (msg.getType().equals(OFType.FLOW_MOD)) { |
| 79 | OFFlowMod fm = (OFFlowMod) msg; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 80 | if (fm.getCommand() == OFFlowModCommand.DELETE_STRICT) { |
| 81 | idRemoved.add(fm.getCookie().getValue()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | }).anyTimes(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 87 | pusher.pushFlowEntry(anyObject(IOFSwitch.class), anyObject(FlowEntry.class), |
| 88 | eq(MsgPriority.HIGH)); |
| 89 | expectLastCall().andAnswer(new IAnswer<Object>() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 90 | @Override |
| 91 | public Object answer() throws Throwable { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 92 | FlowEntry flow = (FlowEntry) getCurrentArguments()[1]; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 93 | idAdded.add(flow.getFlowEntryId()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | return null; |
| 95 | } |
| 96 | }).anyTimes(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 97 | replay(pusher); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 99 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 100 | @After |
| 101 | public void tearDown() throws Exception { |
| 102 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 103 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Test that synchronization doesn't affect anything in case either DB and |
| 106 | * flow table has the same entries. |
| 107 | */ |
| 108 | @Test |
| 109 | public void testStable() { |
| 110 | // Create mock of flow table : flow 1 |
| 111 | IOFSwitch sw = createMockSwitch(new long[]{1}); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 112 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | // Create mock of flow entries : flow 1 |
| 114 | initMockGraph(new long[]{1}); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 115 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 116 | // synchronize |
| 117 | doSynchronization(sw); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 118 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 119 | // check if flow is not changed |
| 120 | assertEquals(0, idAdded.size()); |
| 121 | assertEquals(0, idRemoved.size()); |
| 122 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 123 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Test that an flow is added in case DB has an extra FlowEntry. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testSingleAdd() { |
| 129 | // Create mock of flow table : null |
| 130 | IOFSwitch sw = createMockSwitch(new long[]{}); |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | // Create mock of flow entries : flow 1 |
| 133 | initMockGraph(new long[]{1}); |
| 134 | |
| 135 | // synchronize |
| 136 | doSynchronization(sw); |
| 137 | |
| 138 | // check if single flow is installed |
| 139 | assertEquals(1, idAdded.size()); |
| 140 | assertTrue(idAdded.contains((long) 1)); |
| 141 | assertEquals(0, idRemoved.size()); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Test that an flow is deleted in case switch has an extra FlowEntry. |
| 146 | */ |
| 147 | @Test |
| 148 | public void testSingleDelete() { |
| 149 | // Create mock of flow table : flow 1 |
| 150 | IOFSwitch sw = createMockSwitch(new long[]{1}); |
| 151 | |
| 152 | // Create mock of flow entries : null |
| 153 | initMockGraph(new long[]{}); |
| 154 | |
| 155 | // synchronize |
| 156 | doSynchronization(sw); |
| 157 | |
| 158 | // check if single flow is deleted |
| 159 | assertEquals(0, idAdded.size()); |
| 160 | assertEquals(1, idRemoved.size()); |
| 161 | assertTrue(idRemoved.contains((long) 1)); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Test that appropriate flows are added and other appropriate flows are deleted |
| 166 | * in case flows in DB are overlapping flows in switch. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testMixed() { |
| 170 | // Create mock of flow table : flow 1,2,3 |
| 171 | IOFSwitch sw = createMockSwitch(new long[]{1, 2, 3}); |
| 172 | |
| 173 | // Create mock of flow entries : flow 2,3,4,5 |
| 174 | initMockGraph(new long[]{2, 3, 4, 5}); |
| 175 | |
| 176 | // synchronize |
| 177 | doSynchronization(sw); |
| 178 | |
| 179 | // check if two flows {4,5} is installed and one flow {1} is deleted |
| 180 | assertEquals(2, idAdded.size()); |
| 181 | assertTrue(idAdded.contains((long) 4)); |
| 182 | assertTrue(idAdded.contains((long) 5)); |
| 183 | assertEquals(1, idRemoved.size()); |
| 184 | assertTrue(idRemoved.contains((long) 1)); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | @Test |
| 189 | public void testMassive() { |
| 190 | // Create mock of flow table : flow 0-1999 |
| 191 | long[] swIdList = new long[2000]; |
| 192 | for (long i = 0; i < 2000; ++i) { |
| 193 | swIdList[(int) i] = i; |
| 194 | } |
| 195 | IOFSwitch sw = createMockSwitch(swIdList); |
| 196 | |
| 197 | // Create mock of flow entries : flow 1500-3499 |
| 198 | long[] dbIdList = new long[2000]; |
| 199 | for (long i = 0; i < 2000; ++i) { |
| 200 | dbIdList[(int) i] = 1500 + i; |
| 201 | } |
| 202 | initMockGraph(dbIdList); |
| 203 | |
| 204 | // synchronize |
| 205 | doSynchronization(sw); |
| 206 | |
| 207 | // check if 1500 flows {2000-3499} is installed and 1500 flows {0,...,1499} is deleted |
| 208 | assertEquals(1500, idAdded.size()); |
| 209 | for (long i = 2000; i < 3500; ++i) { |
| 210 | assertTrue(idAdded.contains(i)); |
| 211 | } |
| 212 | assertEquals(1500, idRemoved.size()); |
| 213 | for (long i = 0; i < 1500; ++i) { |
| 214 | assertTrue(idRemoved.contains(i)); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Create mock IOFSwitch with flow table which has arbitrary flows. |
| 220 | * |
| 221 | * @param cookieList List of FlowEntry IDs switch has. |
| 222 | * @return Mock object. |
| 223 | */ |
| 224 | private IOFSwitch createMockSwitch(long[] cookieList) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 225 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 226 | expect(sw.getId()).andReturn((long) 1).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 227 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 228 | List<OFStatsReply> stats = new ArrayList<OFStatsReply>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 229 | for (long cookie : cookieList) { |
| 230 | stats.add(createReply(cookie)); |
| 231 | } |
| 232 | |
| 233 | @SuppressWarnings("unchecked") |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 234 | Future<List<OFStatsReply>> future = createMock(Future.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 235 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 236 | expect(future.get()).andReturn(stats).once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 237 | } catch (InterruptedException e1) { |
| 238 | fail("Failed in Future#get()"); |
| 239 | } catch (ExecutionException e1) { |
| 240 | fail("Failed in Future#get()"); |
| 241 | } |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 242 | replay(future); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | |
| 244 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 245 | expect(sw.getStatistics(anyObject(OFFlowStatsRequest.class))) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 246 | .andReturn(future).once(); |
| 247 | } catch (IOException e) { |
| 248 | fail("Failed in IOFSwitch#getStatistics()"); |
| 249 | } |
| 250 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 251 | replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | return sw; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Create single OFFlowStatisticsReply object which is actually obtained from switch. |
| 257 | * |
| 258 | * @param cookie Cookie value, which indicates ID of FlowEntry installed to switch. |
| 259 | * @return Created object. |
| 260 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 261 | private OFFlowStatsReply createReply(long cookie) { |
| 262 | OFFlowStatsEntry entry = factory10.buildFlowStatsEntry() |
| 263 | .setCookie(U64.of(cookie)) |
| 264 | .setPriority(1) |
| 265 | .setMatch(factory10.buildMatch().build()) |
| 266 | .build(); |
| 267 | OFFlowStatsReply stat = factory10.buildFlowStatsReply() |
| 268 | .setEntries(Collections.singletonList(entry)).build(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 269 | |
| 270 | return stat; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Create mock FlowDatabaseOperation to mock DB. |
| 275 | * |
| 276 | * @param idList List of FlowEntry IDs stored in DB. |
| 277 | */ |
| 278 | private void initMockGraph(long[] idList) { |
| 279 | /* |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 280 | * TODO: The old FlowDatabaseOperation class is gone, so the method |
| 281 | * below needs to be rewritten. |
| 282 | */ |
| 283 | /* |
| 284 | List<IFlowEntry> flowEntryList = new ArrayList<IFlowEntry>(); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 285 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 286 | for (long id : idList) { |
| 287 | IFlowEntry entry = EasyMock.createMock(IFlowEntry.class); |
| 288 | EasyMock.expect(entry.getFlowEntryId()).andReturn(String.valueOf(id)).anyTimes(); |
| 289 | EasyMock.replay(entry); |
| 290 | flowEntryList.add(entry); |
| 291 | } |
| 292 | |
| 293 | ISwitchObject swObj = EasyMock.createMock(ISwitchObject.class); |
| 294 | EasyMock.expect(swObj.getFlowEntries()).andReturn(flowEntryList).once(); |
| 295 | EasyMock.replay(swObj); |
| 296 | |
| 297 | DBOperation mockOp = PowerMock.createMock(DBOperation.class); |
| 298 | EasyMock.expect(mockOp.searchSwitch(EasyMock.anyObject(String.class))).andReturn(swObj).once(); |
| 299 | |
| 300 | PowerMock.mockStatic(FlowDatabaseOperation.class); |
| 301 | for (IFlowEntry entry : flowEntryList) { |
| 302 | EasyMock.expect(FlowDatabaseOperation.extractFlowEntry(EasyMock.eq(entry))) |
| 303 | .andAnswer(new IAnswer<FlowEntry>() { |
| 304 | @Override |
| 305 | public FlowEntry answer() throws Throwable { |
| 306 | IFlowEntry iflow = (IFlowEntry)EasyMock.getCurrentArguments()[0]; |
| 307 | long flowEntryId = Long.valueOf(iflow.getFlowEntryId()); |
| 308 | |
| 309 | FlowEntry flow = EasyMock.createMock(FlowEntry.class); |
| 310 | EasyMock.expect(flow.flowEntryId()).andReturn(new FlowEntryId(flowEntryId)).anyTimes(); |
| 311 | EasyMock.replay(flow); |
| 312 | return flow; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 313 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 314 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 315 | }).anyTimes(); |
| 316 | EasyMock.expect(mockOp.searchFlowEntry(EasyMock.eq(new FlowEntryId(entry.getFlowEntryId())))) |
| 317 | .andReturn(entry); |
| 318 | } |
| 319 | PowerMock.replay(FlowDatabaseOperation.class); |
| 320 | EasyMock.replay(mockOp); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 321 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 322 | try { |
| 323 | PowerMock.expectNew(DBOperation.class).andReturn(mockOp); |
| 324 | } catch (Exception e) { |
| 325 | fail("Failed to create DBOperation"); |
| 326 | } |
| 327 | PowerMock.replay(DBOperation.class); |
| 328 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Instantiate FlowSynchronizer and sync flows. |
| 333 | * |
| 334 | * @param sw Target IOFSwitch object |
| 335 | */ |
| 336 | private void doSynchronization(IOFSwitch sw) { |
| 337 | sync = new FlowSynchronizer(); |
| 338 | sync.init(pusher); |
| 339 | Future<SyncResult> future = sync.synchronize(sw); |
| 340 | try { |
| 341 | future.get(); |
| 342 | } catch (Exception e) { |
| 343 | fail("Failed to Future#get()"); |
| 344 | } |
| 345 | } |
Naoki Shiota | 8df97bc | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 346 | } |