Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.flowprogrammer; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | a88fd24 | 2014-04-03 11:24:54 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertNotNull; |
| 5 | import static org.junit.Assert.assertTrue; |
| 6 | import static org.junit.Assert.fail; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 7 | |
| 8 | import java.io.IOException; |
| 9 | import java.util.ArrayList; |
| 10 | import java.util.HashMap; |
| 11 | import java.util.List; |
| 12 | import java.util.Map; |
| 13 | import java.util.concurrent.ScheduledExecutorService; |
| 14 | import java.util.concurrent.TimeUnit; |
| 15 | |
| 16 | import net.floodlightcontroller.core.FloodlightContext; |
| 17 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 18 | import net.floodlightcontroller.core.IOFSwitch; |
| 19 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 20 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
| 21 | import net.floodlightcontroller.util.OFMessageDamper; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.util.Dpid; |
| 23 | import net.onrc.onos.core.util.FlowEntry; |
| 24 | import net.onrc.onos.core.util.FlowEntryActions; |
| 25 | import net.onrc.onos.core.util.FlowEntryErrorState; |
| 26 | import net.onrc.onos.core.util.FlowEntryId; |
| 27 | import net.onrc.onos.core.util.FlowEntryMatch; |
| 28 | import net.onrc.onos.core.util.FlowEntryUserState; |
| 29 | import net.onrc.onos.core.util.FlowId; |
| 30 | import net.onrc.onos.core.util.Port; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 31 | |
| 32 | import org.easymock.EasyMock; |
| 33 | import org.easymock.IAnswer; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 34 | import org.junit.Test; |
| 35 | import org.openflow.protocol.OFBarrierRequest; |
| 36 | import org.openflow.protocol.OFFlowMod; |
| 37 | import org.openflow.protocol.OFMatch; |
| 38 | import org.openflow.protocol.OFMessage; |
| 39 | import org.openflow.protocol.OFType; |
| 40 | import org.openflow.protocol.action.OFAction; |
| 41 | import org.openflow.protocol.factory.BasicFactory; |
| 42 | |
| 43 | public class FlowPusherTest { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 44 | private FlowPusher pusher; |
| 45 | private FloodlightContext context; |
| 46 | private FloodlightModuleContext modContext; |
| 47 | private BasicFactory factory; |
| 48 | private OFMessageDamper damper; |
| 49 | private IFloodlightProviderService flProviderService; |
| 50 | private IThreadPoolService threadPoolService; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Test single OFMessage is correctly sent to single switch via MessageDamper. |
| 54 | */ |
| 55 | @Test |
| 56 | public void testAddMessage() { |
| 57 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 58 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 60 | EasyMock.expect(msg.getXid()).andReturn(1).anyTimes(); |
| 61 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 62 | EasyMock.replay(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 65 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 66 | sw.flush(); |
| 67 | EasyMock.expectLastCall().once(); |
| 68 | EasyMock.replay(sw); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 69 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | try { |
| 71 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 72 | .andReturn(true).once(); |
| 73 | } catch (IOException e1) { |
| 74 | fail("Failed in OFMessageDamper#write()"); |
| 75 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 76 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | endInitMock(); |
| 78 | initPusher(1); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 79 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 80 | boolean addResult = pusher.add(sw, msg); |
| 81 | assertTrue(addResult); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 82 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | try { |
| 84 | // wait until message is processed. |
| 85 | Thread.sleep(1000); |
| 86 | } catch (InterruptedException e) { |
| 87 | fail("Failed in Thread.sleep()"); |
| 88 | } |
| 89 | EasyMock.verify(msg); |
| 90 | EasyMock.verify(sw); |
| 91 | verifyAll(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 92 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 93 | pusher.stop(); |
| 94 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 95 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Test bunch of OFMessages are correctly sent to single switch via MessageDamper. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testMassiveAddMessage() { |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 101 | // Some number larger than FlowPusher.MAX_MESSAGE_SEND |
| 102 | final int NUM_MSG = FlowPusher.MAX_MESSAGE_SEND * 2; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 103 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 105 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 107 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 108 | sw.flush(); |
| 109 | EasyMock.expectLastCall().atLeastOnce(); |
| 110 | EasyMock.replay(sw); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 111 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 113 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 114 | for (int i = 0; i < NUM_MSG; ++i) { |
| 115 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 116 | EasyMock.expect(msg.getXid()).andReturn(i).anyTimes(); |
| 117 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 118 | EasyMock.replay(msg); |
| 119 | messages.add(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 120 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | try { |
| 122 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 123 | .andReturn(true).once(); |
| 124 | } catch (IOException e1) { |
| 125 | fail("Failed in OFMessageDamper#write()"); |
| 126 | } |
| 127 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 128 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | endInitMock(); |
| 130 | initPusher(1); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 131 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 133 | boolean addResult = pusher.add(sw, msg); |
| 134 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 136 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | try { |
| 138 | // wait until message is processed. |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 139 | Thread.sleep(1000); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | } catch (InterruptedException e) { |
| 141 | fail("Failed in Thread.sleep()"); |
| 142 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 143 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | for (OFMessage msg : messages) { |
| 145 | EasyMock.verify(msg); |
| 146 | } |
| 147 | EasyMock.verify(sw); |
| 148 | verifyAll(); |
| 149 | |
| 150 | pusher.stop(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Test bunch of OFMessages are correctly sent to multiple switches with single threads. |
| 155 | */ |
| 156 | @Test |
| 157 | public void testMultiSwitchAddMessage() { |
| 158 | final int NUM_SWITCH = 10; |
| 159 | final int NUM_MSG = 100; // messages per thread |
| 160 | |
| 161 | beginInitMock(); |
| 162 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 163 | Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | for (int i = 0; i < NUM_SWITCH; ++i) { |
| 165 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 166 | EasyMock.expect(sw.getId()).andReturn((long) i).anyTimes(); |
| 167 | sw.flush(); |
| 168 | EasyMock.expectLastCall().atLeastOnce(); |
| 169 | EasyMock.replay(sw); |
| 170 | |
| 171 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 172 | |
| 173 | for (int j = 0; j < NUM_MSG; ++j) { |
| 174 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 175 | EasyMock.expect(msg.getXid()).andReturn(j).anyTimes(); |
| 176 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 177 | EasyMock.replay(msg); |
| 178 | messages.add(msg); |
| 179 | |
| 180 | try { |
| 181 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 182 | .andReturn(true).once(); |
| 183 | } catch (IOException e1) { |
| 184 | fail("Failed in OFMessageDamper#write()"); |
| 185 | } |
| 186 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 187 | swMap.put(sw, messages); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | endInitMock(); |
| 191 | initPusher(1); |
| 192 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 193 | for (IOFSwitch sw : swMap.keySet()) { |
| 194 | for (OFMessage msg : swMap.get(sw)) { |
| 195 | boolean addResult = pusher.add(sw, msg); |
| 196 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | try { |
| 201 | // wait until message is processed. |
| 202 | Thread.sleep(1000); |
| 203 | } catch (InterruptedException e) { |
| 204 | fail("Failed in Thread.sleep()"); |
| 205 | } |
| 206 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 207 | for (IOFSwitch sw : swMap.keySet()) { |
| 208 | for (OFMessage msg : swMap.get(sw)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 209 | EasyMock.verify(msg); |
| 210 | } |
| 211 | |
| 212 | EasyMock.verify(sw); |
| 213 | } |
| 214 | verifyAll(); |
| 215 | |
| 216 | pusher.stop(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Test bunch of OFMessages are correctly sent to multiple switches using multiple threads. |
| 221 | */ |
| 222 | @Test |
| 223 | public void testMultiThreadedAddMessage() { |
| 224 | final int NUM_THREAD = 10; |
| 225 | final int NUM_MSG = 100; // messages per thread |
| 226 | |
| 227 | beginInitMock(); |
| 228 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 229 | Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | for (int i = 0; i < NUM_THREAD; ++i) { |
| 231 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 232 | EasyMock.expect(sw.getId()).andReturn((long) i).anyTimes(); |
| 233 | sw.flush(); |
| 234 | EasyMock.expectLastCall().atLeastOnce(); |
| 235 | EasyMock.replay(sw); |
| 236 | |
| 237 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 238 | |
| 239 | for (int j = 0; j < NUM_MSG; ++j) { |
| 240 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 241 | EasyMock.expect(msg.getXid()).andReturn(j).anyTimes(); |
| 242 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 243 | EasyMock.replay(msg); |
| 244 | messages.add(msg); |
| 245 | |
| 246 | try { |
| 247 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 248 | .andReturn(true).once(); |
| 249 | } catch (IOException e1) { |
| 250 | fail("Failed in OFMessageDamper#write()"); |
| 251 | } |
| 252 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 253 | swMap.put(sw, messages); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | endInitMock(); |
| 257 | initPusher(NUM_THREAD); |
| 258 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 259 | for (IOFSwitch sw : swMap.keySet()) { |
| 260 | for (OFMessage msg : swMap.get(sw)) { |
| 261 | boolean addResult = pusher.add(sw, msg); |
| 262 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | try { |
| 267 | // wait until message is processed. |
| 268 | Thread.sleep(1000); |
| 269 | } catch (InterruptedException e) { |
| 270 | fail("Failed in Thread.sleep()"); |
| 271 | } |
| 272 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 273 | for (IOFSwitch sw : swMap.keySet()) { |
| 274 | for (OFMessage msg : swMap.get(sw)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 275 | EasyMock.verify(msg); |
| 276 | } |
| 277 | |
| 278 | EasyMock.verify(sw); |
| 279 | } |
| 280 | verifyAll(); |
| 281 | |
| 282 | pusher.stop(); |
| 283 | } |
| 284 | |
| 285 | private long barrierTime = 0; |
| 286 | |
| 287 | /** |
| 288 | * Test rate limitation of messages works correctly. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testRateLimitedAddMessage() { |
| 292 | final long LIMIT_RATE = 100; // [bytes/ms] |
| 293 | final int NUM_MSG = 1000; |
| 294 | |
| 295 | // Accuracy of FlowPusher's rate calculation can't be measured by unit test |
| 296 | // because switch doesn't return BARRIER_REPLY. |
| 297 | // In unit test we use approximate way to measure rate. This value is |
| 298 | // acceptable margin of measured rate. |
| 299 | final double ACCEPTABLE_RATE = LIMIT_RATE * 1.2; |
| 300 | |
| 301 | beginInitMock(); |
| 302 | |
| 303 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 304 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 305 | sw.flush(); |
| 306 | EasyMock.expectLastCall().atLeastOnce(); |
| 307 | prepareBarrier(sw); |
| 308 | EasyMock.replay(sw); |
| 309 | |
| 310 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 311 | |
| 312 | for (int i = 0; i < NUM_MSG; ++i) { |
| 313 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
| 314 | EasyMock.expect(msg.getXid()).andReturn(1).anyTimes(); |
| 315 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 316 | EasyMock.expect(msg.getLengthU()).andReturn(100).anyTimes(); |
| 317 | EasyMock.replay(msg); |
| 318 | messages.add(msg); |
| 319 | |
| 320 | try { |
| 321 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.eq(msg), EasyMock.eq(context))) |
| 322 | .andReturn(true).once(); |
| 323 | } catch (IOException e) { |
| 324 | fail("Failed in OFMessageDamper#write()"); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | try { |
| 329 | EasyMock.expect(damper.write(EasyMock.eq(sw), (OFMessage) EasyMock.anyObject(), EasyMock.eq(context))) |
| 330 | .andAnswer(new IAnswer<Boolean>() { |
| 331 | @Override |
| 332 | public Boolean answer() throws Throwable { |
| 333 | OFMessage msg = (OFMessage) EasyMock.getCurrentArguments()[1]; |
| 334 | if (msg.getType() == OFType.BARRIER_REQUEST) { |
| 335 | barrierTime = System.currentTimeMillis(); |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | }).once(); |
| 340 | } catch (IOException e1) { |
| 341 | fail("Failed in OFMessageDamper#write()"); |
| 342 | } |
| 343 | |
| 344 | endInitMock(); |
| 345 | initPusher(1); |
| 346 | |
| 347 | pusher.createQueue(sw); |
| 348 | pusher.setRate(sw, LIMIT_RATE); |
| 349 | |
| 350 | long beginTime = System.currentTimeMillis(); |
| 351 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 352 | boolean addResult = pusher.add(sw, msg); |
| 353 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | pusher.barrierAsync(sw); |
| 357 | |
| 358 | try { |
| 359 | do { |
| 360 | Thread.sleep(1000); |
| 361 | } while (barrierTime == 0); |
| 362 | } catch (InterruptedException e) { |
| 363 | fail("Failed to sleep"); |
| 364 | } |
| 365 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 366 | double measuredRate = NUM_MSG * 100 / (barrierTime - beginTime); |
| 367 | assertTrue(measuredRate < ACCEPTABLE_RATE); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 368 | |
| 369 | for (OFMessage msg : messages) { |
| 370 | EasyMock.verify(msg); |
| 371 | } |
| 372 | EasyMock.verify(sw); |
| 373 | verifyAll(); |
| 374 | |
| 375 | pusher.stop(); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Test barrier message is correctly sent to a switch. |
| 380 | */ |
| 381 | @Test |
| 382 | public void testBarrierMessage() { |
| 383 | beginInitMock(); |
| 384 | |
| 385 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 386 | EasyMock.expect(sw.getId()).andReturn((long) 1).anyTimes(); |
| 387 | sw.flush(); |
| 388 | EasyMock.expectLastCall().atLeastOnce(); |
| 389 | prepareBarrier(sw); |
| 390 | EasyMock.replay(sw); |
| 391 | |
| 392 | try { |
| 393 | EasyMock.expect(damper.write(EasyMock.eq(sw), (OFMessage) EasyMock.anyObject(), EasyMock.eq(context))) |
| 394 | .andReturn(true).once(); |
| 395 | } catch (IOException e1) { |
| 396 | fail("Failed in OFMessageDamper#write()"); |
| 397 | } |
| 398 | |
| 399 | endInitMock(); |
| 400 | initPusher(1); |
| 401 | |
| 402 | OFBarrierReplyFuture future = pusher.barrierAsync(sw); |
| 403 | |
| 404 | assertNotNull(future); |
| 405 | |
| 406 | try { |
| 407 | Thread.sleep(1000); |
| 408 | } catch (InterruptedException e) { |
| 409 | fail("Failed to sleep"); |
| 410 | } |
| 411 | |
| 412 | verifyAll(); |
| 413 | |
| 414 | pusher.stop(); |
| 415 | } |
| 416 | |
| 417 | static final int XID_TO_VERIFY = 100; |
| 418 | static final long DPID_TO_VERIFY = 10; |
| 419 | |
| 420 | /** |
| 421 | * Test FlowObject is correctly converted to message and is sent to a switch. |
| 422 | */ |
| 423 | @SuppressWarnings("unchecked") |
| 424 | @Test |
| 425 | public void testAddFlow() { |
| 426 | // instantiate required objects |
| 427 | FlowEntry flowEntry1 = new FlowEntry(); |
| 428 | flowEntry1.setDpid(new Dpid(DPID_TO_VERIFY)); |
| 429 | flowEntry1.setFlowId(new FlowId(1)); |
| 430 | flowEntry1.setInPort(new Port((short) 1)); |
| 431 | flowEntry1.setOutPort(new Port((short) 11)); |
| 432 | flowEntry1.setFlowEntryId(new FlowEntryId(1)); |
| 433 | flowEntry1.setFlowEntryMatch(new FlowEntryMatch()); |
| 434 | flowEntry1.setFlowEntryActions(new FlowEntryActions()); |
| 435 | flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState()); |
| 436 | flowEntry1.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
| 437 | |
| 438 | beginInitMock(); |
| 439 | |
| 440 | OFFlowMod msg = EasyMock.createMock(OFFlowMod.class); |
| 441 | EasyMock.expect(msg.setIdleTimeout(EasyMock.anyShort())).andReturn(msg); |
| 442 | EasyMock.expect(msg.setHardTimeout(EasyMock.anyShort())).andReturn(msg); |
| 443 | EasyMock.expect(msg.setPriority(EasyMock.anyShort())).andReturn(msg); |
| 444 | EasyMock.expect(msg.setBufferId(EasyMock.anyInt())).andReturn(msg); |
| 445 | EasyMock.expect(msg.setCookie(EasyMock.anyLong())).andReturn(msg); |
| 446 | EasyMock.expect(msg.setCommand(EasyMock.anyShort())).andReturn(msg); |
| 447 | EasyMock.expect(msg.setMatch(EasyMock.anyObject(OFMatch.class))).andReturn(msg); |
| 448 | EasyMock.expect(msg.setActions((List<OFAction>) EasyMock.anyObject())).andReturn(msg); |
| 449 | EasyMock.expect(msg.setLengthU(EasyMock.anyShort())).andReturn(msg); |
| 450 | EasyMock.expect(msg.setOutPort(EasyMock.anyShort())).andReturn(msg).atLeastOnce(); |
| 451 | EasyMock.expect(msg.getXid()).andReturn(XID_TO_VERIFY).anyTimes(); |
| 452 | EasyMock.expect(msg.getType()).andReturn(OFType.FLOW_MOD).anyTimes(); |
| 453 | EasyMock.expect(msg.getLength()).andReturn((short) 100).anyTimes(); |
| 454 | EasyMock.replay(msg); |
| 455 | |
| 456 | EasyMock.expect(factory.getMessage(EasyMock.eq(OFType.FLOW_MOD))).andReturn(msg); |
| 457 | |
| 458 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 459 | EasyMock.expect(sw.getId()).andReturn(DPID_TO_VERIFY).anyTimes(); |
| 460 | EasyMock.expect(sw.getStringId()).andReturn("1").anyTimes(); |
| 461 | sw.flush(); |
| 462 | EasyMock.expectLastCall().once(); |
| 463 | |
| 464 | try { |
| 465 | EasyMock.expect(damper.write(EasyMock.eq(sw), EasyMock.anyObject(OFMessage.class), EasyMock.eq(context))) |
| 466 | .andAnswer(new IAnswer<Boolean>() { |
| 467 | @Override |
| 468 | public Boolean answer() throws Throwable { |
| 469 | OFMessage msg = (OFMessage) EasyMock.getCurrentArguments()[1]; |
| 470 | if (msg.getType() == OFType.FLOW_MOD) { |
| 471 | assertEquals(msg.getXid(), XID_TO_VERIFY); |
| 472 | } |
| 473 | return true; |
| 474 | } |
| 475 | }).atLeastOnce(); |
| 476 | } catch (IOException e1) { |
| 477 | fail("Failed in OFMessageDamper#write()"); |
| 478 | } |
| 479 | |
| 480 | EasyMock.replay(sw); |
| 481 | |
| 482 | endInitMock(); |
| 483 | initPusher(1); |
| 484 | |
| 485 | pusher.pushFlowEntry(sw, flowEntry1); |
| 486 | |
| 487 | try { |
| 488 | Thread.sleep(1000); |
| 489 | } catch (InterruptedException e) { |
| 490 | fail("Failed to sleep"); |
| 491 | } |
| 492 | |
| 493 | EasyMock.verify(sw); |
| 494 | verifyAll(); |
| 495 | |
| 496 | pusher.stop(); |
| 497 | } |
| 498 | |
| 499 | private void beginInitMock() { |
| 500 | context = EasyMock.createMock(FloodlightContext.class); |
| 501 | modContext = EasyMock.createMock(FloodlightModuleContext.class); |
| 502 | factory = EasyMock.createMock(BasicFactory.class); |
| 503 | damper = EasyMock.createMock(OFMessageDamper.class); |
| 504 | flProviderService = EasyMock.createMock(IFloodlightProviderService.class); |
| 505 | threadPoolService = EasyMock.createMock(IThreadPoolService.class); |
| 506 | |
| 507 | EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IThreadPoolService.class))) |
| 508 | .andReturn(threadPoolService).once(); |
| 509 | EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IFloodlightProviderService.class))) |
| 510 | .andReturn(flProviderService).once(); |
| 511 | flProviderService.addOFMessageListener(EasyMock.eq(OFType.BARRIER_REPLY), |
| 512 | (FlowPusher) EasyMock.anyObject()); |
| 513 | EasyMock.expectLastCall().once(); |
| 514 | |
| 515 | ScheduledExecutorService executor = EasyMock.createMock(ScheduledExecutorService.class); |
| 516 | EasyMock.expect(executor.schedule((Runnable) EasyMock.anyObject(), EasyMock.anyLong(), |
| 517 | (TimeUnit) EasyMock.anyObject())).andReturn(null).once(); |
| 518 | EasyMock.replay(executor); |
| 519 | EasyMock.expect(threadPoolService.getScheduledExecutor()).andReturn(executor).anyTimes(); |
| 520 | } |
| 521 | |
| 522 | private void endInitMock() { |
| 523 | EasyMock.replay(threadPoolService); |
| 524 | EasyMock.replay(flProviderService); |
| 525 | EasyMock.replay(damper); |
| 526 | EasyMock.replay(factory); |
| 527 | EasyMock.replay(modContext); |
| 528 | EasyMock.replay(context); |
| 529 | } |
| 530 | |
| 531 | private void verifyAll() { |
| 532 | EasyMock.verify(threadPoolService); |
| 533 | EasyMock.verify(flProviderService); |
| 534 | EasyMock.verify(damper); |
| 535 | EasyMock.verify(factory); |
| 536 | EasyMock.verify(modContext); |
| 537 | EasyMock.verify(context); |
| 538 | } |
| 539 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 540 | private void initPusher(int numThread) { |
| 541 | pusher = new FlowPusher(numThread); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 542 | pusher.init(context, modContext, factory, damper); |
| 543 | pusher.start(); |
| 544 | } |
| 545 | |
| 546 | private void prepareBarrier(IOFSwitch sw) { |
| 547 | OFBarrierRequest req = EasyMock.createMock(OFBarrierRequest.class); |
| 548 | req.setXid(EasyMock.anyInt()); |
| 549 | EasyMock.expectLastCall().once(); |
| 550 | EasyMock.expect(req.getXid()).andReturn(1).anyTimes(); |
| 551 | EasyMock.expect(req.getType()).andReturn(OFType.BARRIER_REQUEST).anyTimes(); |
| 552 | EasyMock.expect(req.getLength()).andReturn((short) 100).anyTimes(); |
| 553 | EasyMock.replay(req); |
| 554 | EasyMock.expect(factory.getMessage(EasyMock.eq(OFType.BARRIER_REQUEST))).andReturn(req).anyTimes(); |
| 555 | EasyMock.expect(sw.getNextTransactionId()).andReturn(1); |
| 556 | } |
| 557 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 558 | } |