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 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 3 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 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; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 19 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 20 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 21 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.intent.FlowEntry; |
| 23 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.util.IntegrationTest; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 25 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 26 | import org.easymock.EasyMock; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 27 | import org.junit.Test; |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 28 | import org.junit.experimental.categories.Category; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 29 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 30 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 31 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 32 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFlowModify; |
| 34 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 35 | import org.projectfloodlight.openflow.protocol.OFType; |
| 36 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 37 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 38 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 39 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 40 | import org.projectfloodlight.openflow.types.OFPort; |
| 41 | import org.projectfloodlight.openflow.types.U64; |
| 42 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 43 | |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 44 | @Category(IntegrationTest.class) |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 45 | public class FlowPusherTest { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 46 | private FlowPusher pusher; |
| 47 | private FloodlightContext context; |
| 48 | private FloodlightModuleContext modContext; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | private IFloodlightProviderService flProviderService; |
| 50 | private IThreadPoolService threadPoolService; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 51 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 52 | private OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10); |
| 53 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Test single OFMessage is correctly sent to single switch via MessageDamper. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testAddMessage() { |
| 59 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 60 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 62 | EasyMock.expect(msg.getXid()).andReturn((long) 1).anyTimes(); |
| 63 | //EasyMock.expect(msg.()).andReturn((short) 100).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | EasyMock.replay(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 65 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 66 | IOFSwitch sw = createConnectedSwitchMock(1, false); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 67 | |
| 68 | |
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 { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 71 | sw.write(EasyMock.eq(msg), EasyMock.eq((FloodlightContext) null)); |
| 72 | EasyMock.expectLastCall().once(); |
| 73 | EasyMock.replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 74 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 75 | fail("Failed in IOFSwitch#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 76 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 77 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | endInitMock(); |
| 79 | initPusher(1); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 80 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 81 | boolean addResult = pusher.add(sw, msg); |
| 82 | assertTrue(addResult); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 83 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 84 | try { |
| 85 | // wait until message is processed. |
| 86 | Thread.sleep(1000); |
| 87 | } catch (InterruptedException e) { |
| 88 | fail("Failed in Thread.sleep()"); |
| 89 | } |
| 90 | EasyMock.verify(msg); |
| 91 | EasyMock.verify(sw); |
| 92 | verifyAll(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 93 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | pusher.stop(); |
| 95 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 96 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Test bunch of OFMessages are correctly sent to single switch via MessageDamper. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testMassiveAddMessage() { |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 102 | // Some number larger than FlowPusher.MAX_MESSAGE_SEND |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 103 | final int numMsg = FlowPusher.MAX_MESSAGE_SEND * 2; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 104 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 105 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 106 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 107 | IOFSwitch sw = createConnectedSwitchMock(1, false); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 108 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 109 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 110 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 111 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 112 | for (int i = 0; i < numMsg; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 113 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 114 | EasyMock.expect(msg.getXid()).andReturn((long) i).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 119 | sw.write(EasyMock.eq(msg), EasyMock.eq((FloodlightContext) null)); |
| 120 | EasyMock.expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 122 | fail("Failed in IOFSwitch#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 125 | EasyMock.replay(sw); |
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 | |
| 164 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 165 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 166 | for (int j = 0; j < numMsg; ++j) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 168 | EasyMock.expect(msg.getXid()).andReturn((long) j).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | EasyMock.replay(msg); |
| 170 | messages.add(msg); |
| 171 | |
| 172 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 173 | sw.write(EasyMock.eq(msg), EasyMock.eq((FloodlightContext) null)); |
| 174 | EasyMock.expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 175 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 176 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 179 | swMap.put(sw, messages); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 180 | EasyMock.replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | endInitMock(); |
| 184 | initPusher(1); |
| 185 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 186 | for (IOFSwitch sw : swMap.keySet()) { |
| 187 | for (OFMessage msg : swMap.get(sw)) { |
| 188 | boolean addResult = pusher.add(sw, msg); |
| 189 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | try { |
| 194 | // wait until message is processed. |
| 195 | Thread.sleep(1000); |
| 196 | } catch (InterruptedException e) { |
| 197 | fail("Failed in Thread.sleep()"); |
| 198 | } |
| 199 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 200 | for (IOFSwitch sw : swMap.keySet()) { |
| 201 | for (OFMessage msg : swMap.get(sw)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 202 | EasyMock.verify(msg); |
| 203 | } |
| 204 | |
| 205 | EasyMock.verify(sw); |
| 206 | } |
| 207 | verifyAll(); |
| 208 | |
| 209 | pusher.stop(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Test bunch of OFMessages are correctly sent to multiple switches using multiple threads. |
| 214 | */ |
| 215 | @Test |
| 216 | public void testMultiThreadedAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 217 | final int numThreads = 10; |
| 218 | final int numMsg = 100; // messages per thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 219 | |
| 220 | beginInitMock(); |
| 221 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 222 | Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 223 | for (int i = 0; i < numThreads; ++i) { |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 224 | IOFSwitch sw = createConnectedSwitchMock(i, false); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 225 | //EasyMock.replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 226 | |
| 227 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 228 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 229 | for (int j = 0; j < numMsg; ++j) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 230 | OFMessage msg = EasyMock.createMock(OFMessage.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 231 | EasyMock.expect(msg.getXid()).andReturn((long) j).anyTimes(); |
| 232 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | EasyMock.replay(msg); |
| 234 | messages.add(msg); |
| 235 | |
| 236 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 237 | sw.write(EasyMock.eq(msg), EasyMock.eq((FloodlightContext) null)); |
| 238 | EasyMock.expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 239 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 240 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 243 | swMap.put(sw, messages); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 244 | EasyMock.replay(sw); |
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); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 249 | for (IOFSwitch sw : swMap.keySet()) { |
| 250 | for (OFMessage msg : swMap.get(sw)) { |
| 251 | boolean addResult = pusher.add(sw, msg); |
| 252 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | try { |
| 257 | // wait until message is processed. |
| 258 | Thread.sleep(1000); |
| 259 | } catch (InterruptedException e) { |
| 260 | fail("Failed in Thread.sleep()"); |
| 261 | } |
| 262 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 263 | for (IOFSwitch sw : swMap.keySet()) { |
| 264 | for (OFMessage msg : swMap.get(sw)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 265 | EasyMock.verify(msg); |
| 266 | } |
| 267 | |
| 268 | EasyMock.verify(sw); |
| 269 | } |
| 270 | verifyAll(); |
| 271 | |
| 272 | pusher.stop(); |
| 273 | } |
| 274 | |
| 275 | private long barrierTime = 0; |
| 276 | |
| 277 | /** |
| 278 | * Test rate limitation of messages works correctly. |
| 279 | */ |
| 280 | @Test |
| 281 | public void testRateLimitedAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 282 | final long limitRate = 100; // [bytes/ms] |
| 283 | final int numMsg = 1000; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 284 | |
| 285 | // Accuracy of FlowPusher's rate calculation can't be measured by unit test |
| 286 | // because switch doesn't return BARRIER_REPLY. |
| 287 | // In unit test we use approximate way to measure rate. This value is |
| 288 | // acceptable margin of measured rate. |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 289 | final double acceptableRate = limitRate * 1.2; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 290 | |
| 291 | beginInitMock(); |
| 292 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 293 | IOFSwitch sw = createConnectedSwitchMock(1, true); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 294 | EasyMock.expect(sw.getOFVersion()).andReturn(OFVersion.OF_10).once(); |
| 295 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 301 | EasyMock.expect(msg.getXid()).andReturn((long) 1).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 302 | EasyMock.replay(msg); |
| 303 | messages.add(msg); |
| 304 | |
| 305 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 306 | sw.write(EasyMock.eq(msg), EasyMock.eq((FloodlightContext) null)); |
| 307 | EasyMock.expectLastCall().once(); |
| 308 | } catch (IOException e1) { |
| 309 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
| 313 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 314 | sw.write(EasyMock.anyObject(OFBarrierRequest.class), EasyMock.eq((FloodlightContext) null)); |
| 315 | EasyMock.expectLastCall().once(); |
| 316 | barrierTime = System.currentTimeMillis(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 318 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 321 | EasyMock.replay(sw); |
| 322 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 323 | endInitMock(); |
| 324 | initPusher(1); |
| 325 | |
| 326 | pusher.createQueue(sw); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 327 | pusher.setRate(sw, limitRate); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 328 | |
| 329 | long beginTime = System.currentTimeMillis(); |
| 330 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 331 | boolean addResult = pusher.add(sw, msg); |
| 332 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | pusher.barrierAsync(sw); |
| 336 | |
| 337 | try { |
| 338 | do { |
| 339 | Thread.sleep(1000); |
| 340 | } while (barrierTime == 0); |
| 341 | } catch (InterruptedException e) { |
| 342 | fail("Failed to sleep"); |
| 343 | } |
| 344 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 345 | double measuredRate = numMsg * 100 / (barrierTime - beginTime); |
| 346 | assertTrue(measuredRate < acceptableRate); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 347 | |
| 348 | for (OFMessage msg : messages) { |
| 349 | EasyMock.verify(msg); |
| 350 | } |
| 351 | EasyMock.verify(sw); |
| 352 | verifyAll(); |
| 353 | |
| 354 | pusher.stop(); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Test barrier message is correctly sent to a switch. |
| 359 | */ |
| 360 | @Test |
| 361 | public void testBarrierMessage() { |
| 362 | beginInitMock(); |
| 363 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 364 | IOFSwitch sw = createConnectedSwitchMock(1, true); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 365 | EasyMock.expect(sw.getOFVersion()).andReturn(OFVersion.OF_10).once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 366 | |
| 367 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 368 | sw.write((OFMessage) EasyMock.anyObject(), EasyMock.eq((FloodlightContext) null)); |
| 369 | EasyMock.expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 370 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 371 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 372 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 373 | EasyMock.replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 374 | endInitMock(); |
| 375 | initPusher(1); |
| 376 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 377 | OFMessageFuture<OFBarrierReply> future = pusher.barrierAsync(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 378 | |
| 379 | assertNotNull(future); |
| 380 | |
| 381 | try { |
| 382 | Thread.sleep(1000); |
| 383 | } catch (InterruptedException e) { |
| 384 | fail("Failed to sleep"); |
| 385 | } |
| 386 | |
| 387 | verifyAll(); |
| 388 | |
| 389 | pusher.stop(); |
| 390 | } |
| 391 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 392 | static final long XID_TO_VERIFY = 100; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 393 | static final long DPID_TO_VERIFY = 10; |
| 394 | |
| 395 | /** |
| 396 | * Test FlowObject is correctly converted to message and is sent to a switch. |
| 397 | */ |
| 398 | @SuppressWarnings("unchecked") |
| 399 | @Test |
| 400 | public void testAddFlow() { |
| 401 | // instantiate required objects |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 402 | FlowEntry flowEntry1 = new FlowEntry(DPID_TO_VERIFY, 1, 11, null, null, 0, 0, Operator.ADD); |
| 403 | /* |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 404 | flowEntry1.setDpid(new Dpid(DPID_TO_VERIFY)); |
| 405 | flowEntry1.setFlowId(new FlowId(1)); |
Yuta HIGUCHI | fb56450 | 2014-06-16 21:29:00 -0700 | [diff] [blame] | 406 | flowEntry1.setInPort(new PortNumber((short) 1)); |
| 407 | flowEntry1.setOutPort(new PortNumber((short) 11)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 408 | flowEntry1.setFlowEntryId(new FlowEntryId(1)); |
| 409 | flowEntry1.setFlowEntryMatch(new FlowEntryMatch()); |
| 410 | flowEntry1.setFlowEntryActions(new FlowEntryActions()); |
| 411 | flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState()); |
| 412 | flowEntry1.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 413 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 414 | |
| 415 | beginInitMock(); |
| 416 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 417 | OFFlowModify fm = EasyMock.createMock(OFFlowModify.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 418 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 419 | OFFlowModify.Builder bld = EasyMock.createMock(OFFlowModify.Builder.class); |
| 420 | |
| 421 | EasyMock.expect(bld.setIdleTimeout(EasyMock.anyInt())).andReturn(bld); |
| 422 | EasyMock.expect(bld.setHardTimeout(EasyMock.anyInt())).andReturn(bld); |
| 423 | EasyMock.expect(bld.setPriority(EasyMock.anyShort())).andReturn(bld); |
| 424 | EasyMock.expect(bld.setBufferId(OFBufferId.NO_BUFFER)).andReturn(bld); |
| 425 | EasyMock.expect(bld.setCookie(U64.of(EasyMock.anyLong()))).andReturn(bld); |
| 426 | EasyMock.expect(bld.setMatch(EasyMock.anyObject(Match.class))).andReturn(bld); |
| 427 | EasyMock.expect(bld.setActions((List<OFAction>) EasyMock.anyObject())).andReturn(bld); |
| 428 | EasyMock.expect(bld.setOutPort(OFPort.of(EasyMock.anyInt()))).andReturn(bld).atLeastOnce(); |
| 429 | EasyMock.expect(bld.build()).andReturn(fm); |
| 430 | |
| 431 | EasyMock.expect(fm.getXid()).andReturn(XID_TO_VERIFY).anyTimes(); |
| 432 | EasyMock.expect(fm.getType()).andReturn(OFType.FLOW_MOD).anyTimes(); |
| 433 | |
| 434 | |
| 435 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 436 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 437 | IOFSwitch sw = createConnectedSwitchMock(DPID_TO_VERIFY, false); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 438 | EasyMock.expect(sw.getStringId()).andReturn("1").anyTimes(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 439 | EasyMock.expect(sw.getOFVersion()).andReturn(OFVersion.OF_10).once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 440 | |
| 441 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 442 | sw.write(EasyMock.anyObject(OFMessage.class), EasyMock.eq((FloodlightContext) null)); |
| 443 | EasyMock.expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 444 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 445 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 448 | EasyMock.replay(bld, fm); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 449 | EasyMock.replay(sw); |
| 450 | |
| 451 | endInitMock(); |
| 452 | initPusher(1); |
| 453 | |
| 454 | pusher.pushFlowEntry(sw, flowEntry1); |
| 455 | |
| 456 | try { |
| 457 | Thread.sleep(1000); |
| 458 | } catch (InterruptedException e) { |
| 459 | fail("Failed to sleep"); |
| 460 | } |
| 461 | |
| 462 | EasyMock.verify(sw); |
| 463 | verifyAll(); |
| 464 | |
| 465 | pusher.stop(); |
| 466 | } |
| 467 | |
| 468 | private void beginInitMock() { |
| 469 | context = EasyMock.createMock(FloodlightContext.class); |
| 470 | modContext = EasyMock.createMock(FloodlightModuleContext.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 471 | // AAS: I don't think we should mock factories... the rabbit whole is too deep. |
| 472 | //factory10 = EasyMock.createMock(OFFactories.getFactory(OFVersion.OF_10).getClass()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 473 | flProviderService = EasyMock.createMock(IFloodlightProviderService.class); |
| 474 | threadPoolService = EasyMock.createMock(IThreadPoolService.class); |
| 475 | |
| 476 | EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IThreadPoolService.class))) |
| 477 | .andReturn(threadPoolService).once(); |
| 478 | EasyMock.expect(modContext.getServiceImpl(EasyMock.eq(IFloodlightProviderService.class))) |
| 479 | .andReturn(flProviderService).once(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 480 | // AAS: FlowPusher doesn't call the following anymore. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 481 | flProviderService.addOFMessageListener(EasyMock.eq(OFType.BARRIER_REPLY), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 482 | EasyMock.anyObject(FlowPusher.class)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 483 | EasyMock.expectLastCall().once(); |
| 484 | |
| 485 | ScheduledExecutorService executor = EasyMock.createMock(ScheduledExecutorService.class); |
| 486 | EasyMock.expect(executor.schedule((Runnable) EasyMock.anyObject(), EasyMock.anyLong(), |
| 487 | (TimeUnit) EasyMock.anyObject())).andReturn(null).once(); |
| 488 | EasyMock.replay(executor); |
| 489 | EasyMock.expect(threadPoolService.getScheduledExecutor()).andReturn(executor).anyTimes(); |
| 490 | } |
| 491 | |
| 492 | private void endInitMock() { |
| 493 | EasyMock.replay(threadPoolService); |
| 494 | EasyMock.replay(flProviderService); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 495 | //EasyMock.replay(factory10); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 496 | EasyMock.replay(modContext); |
| 497 | EasyMock.replay(context); |
| 498 | } |
| 499 | |
| 500 | private void verifyAll() { |
| 501 | EasyMock.verify(threadPoolService); |
| 502 | EasyMock.verify(flProviderService); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 503 | //EasyMock.verify(factory10); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 504 | EasyMock.verify(modContext); |
| 505 | EasyMock.verify(context); |
| 506 | } |
| 507 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 508 | private void initPusher(int numThread) { |
| 509 | pusher = new FlowPusher(numThread); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 510 | pusher.init(modContext); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 511 | pusher.start(); |
| 512 | } |
| 513 | |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 514 | private IOFSwitch createConnectedSwitchMock(long dpid, boolean useBarrier) { |
| 515 | IOFSwitch sw = EasyMock.createMock(IOFSwitch.class); |
| 516 | EasyMock.expect(sw.isConnected()).andReturn(true).anyTimes(); |
| 517 | EasyMock.expect(sw.getId()).andReturn(dpid).anyTimes(); |
| 518 | sw.flush(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 519 | EasyMock.expectLastCall().anyTimes(); |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 520 | if (useBarrier) { |
| 521 | prepareBarrier(sw); |
| 522 | } |
| 523 | |
| 524 | return sw; |
| 525 | } |
| 526 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 527 | private void prepareBarrier(IOFSwitch sw) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 528 | OFBarrierRequest.Builder bld = EasyMock.createMock(factory10.buildBarrierRequest().getClass()); |
| 529 | EasyMock.expect(bld.setXid(EasyMock.anyInt())).andReturn(bld); |
| 530 | EasyMock.expect(bld.getXid()).andReturn((long) 1).anyTimes(); |
| 531 | EasyMock.expect(bld.getType()).andReturn(OFType.BARRIER_REQUEST).anyTimes(); |
| 532 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 533 | OFBarrierRequest req = EasyMock.createMock(OFBarrierRequest.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 534 | EasyMock.expect(bld.build()).andReturn(req).anyTimes(); |
| 535 | EasyMock.replay(bld); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 536 | EasyMock.expect(sw.getNextTransactionId()).andReturn(1); |
| 537 | } |
| 538 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 539 | } |