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