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 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 4 | import static org.easymock.EasyMock.anyInt; |
| 5 | import static org.easymock.EasyMock.anyLong; |
| 6 | import static org.easymock.EasyMock.anyObject; |
| 7 | import static org.easymock.EasyMock.anyShort; |
| 8 | import static org.easymock.EasyMock.createMock; |
| 9 | import static org.easymock.EasyMock.eq; |
| 10 | import static org.easymock.EasyMock.expect; |
| 11 | import static org.easymock.EasyMock.expectLastCall; |
| 12 | import static org.easymock.EasyMock.replay; |
| 13 | import static org.easymock.EasyMock.verify; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 14 | import static org.junit.Assert.assertNotNull; |
| 15 | import static org.junit.Assert.assertTrue; |
| 16 | import static org.junit.Assert.fail; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ScheduledExecutorService; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 26 | import net.floodlightcontroller.core.FloodlightContext; |
| 27 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 28 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 29 | import net.floodlightcontroller.core.internal.OFMessageFuture; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 30 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 31 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 32 | import net.onrc.onos.core.intent.FlowEntry; |
| 33 | import net.onrc.onos.core.intent.IntentOperation.Operator; |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 34 | import net.onrc.onos.core.util.IntegrationTest; |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 35 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 36 | import org.junit.Test; |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 37 | import org.junit.experimental.categories.Category; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFBarrierReply; |
| 39 | import org.projectfloodlight.openflow.protocol.OFBarrierRequest; |
| 40 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 41 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 42 | import org.projectfloodlight.openflow.protocol.OFFlowModify; |
| 43 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 44 | import org.projectfloodlight.openflow.protocol.OFType; |
| 45 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 46 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 47 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 48 | import org.projectfloodlight.openflow.types.OFBufferId; |
| 49 | import org.projectfloodlight.openflow.types.OFPort; |
| 50 | import org.projectfloodlight.openflow.types.U64; |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 51 | import org.projectfloodlight.openflow.util.HexString; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 52 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 53 | |
Ray Milkey | 1064357 | 2014-06-10 15:17:39 -0700 | [diff] [blame] | 54 | @Category(IntegrationTest.class) |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 55 | public class FlowPusherTest { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | private FlowPusher pusher; |
| 57 | private FloodlightContext context; |
| 58 | private FloodlightModuleContext modContext; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | private IFloodlightProviderService flProviderService; |
| 60 | private IThreadPoolService threadPoolService; |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 61 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 62 | private OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10); |
| 63 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 64 | /** |
Jonathan Hart | 51bb27c | 2014-08-11 16:00:31 -0700 | [diff] [blame] | 65 | * Test single OFMessage is correctly sent to single switch. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | */ |
| 67 | @Test |
| 68 | public void testAddMessage() { |
| 69 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 70 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 71 | OFMessage msg = createMock(OFMessage.class); |
| 72 | expect(msg.getXid()).andReturn((long) 1).anyTimes(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 73 | replay(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 74 | |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 75 | IOFSwitch sw = createConnectedSwitchMock(1); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 76 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 78 | sw.write(eq(msg), eq((FloodlightContext) null)); |
| 79 | expectLastCall().once(); |
| 80 | replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 82 | fail("Failed in IOFSwitch#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | endInitMock(); |
| 86 | initPusher(1); |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 87 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 88 | boolean addResult = pusher.add(sw, msg); |
| 89 | assertTrue(addResult); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 90 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 91 | try { |
| 92 | // wait until message is processed. |
| 93 | Thread.sleep(1000); |
| 94 | } catch (InterruptedException e) { |
| 95 | fail("Failed in Thread.sleep()"); |
| 96 | } |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 97 | verify(msg); |
| 98 | verify(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 99 | verifyAll(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 100 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | pusher.stop(); |
| 102 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 103 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | /** |
Jonathan Hart | 51bb27c | 2014-08-11 16:00:31 -0700 | [diff] [blame] | 105 | * Test bunch of OFMessages are correctly sent to single switch. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | */ |
| 107 | @Test |
| 108 | public void testMassiveAddMessage() { |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 109 | // Some number larger than FlowPusher.MAX_MESSAGE_SEND |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 110 | final int numMsg = FlowPusher.MAX_MESSAGE_SEND * 2; |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 111 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | beginInitMock(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 113 | |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 114 | IOFSwitch sw = createConnectedSwitchMock(1); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 115 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 116 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 117 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 118 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 119 | for (int i = 0; i < numMsg; ++i) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 120 | OFMessage msg = createMock(OFMessage.class); |
| 121 | expect(msg.getXid()).andReturn((long) i).anyTimes(); |
| 122 | replay(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | messages.add(msg); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 124 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 125 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 126 | sw.write(eq(msg), eq((FloodlightContext) null)); |
| 127 | expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 128 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 129 | fail("Failed in IOFSwitch#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 132 | replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 133 | endInitMock(); |
| 134 | initPusher(1); |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 135 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 136 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 137 | boolean addResult = pusher.add(sw, msg); |
| 138 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | } |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 140 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 141 | try { |
| 142 | // wait until message is processed. |
Yuta HIGUCHI | 7c39c84 | 2014-06-09 16:33:03 -0700 | [diff] [blame] | 143 | Thread.sleep(1000); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 144 | } catch (InterruptedException e) { |
| 145 | fail("Failed in Thread.sleep()"); |
| 146 | } |
Naoki Shiota | d6ef3b3 | 2014-03-13 18:42:23 -0700 | [diff] [blame] | 147 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 148 | for (OFMessage msg : messages) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 149 | verify(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 150 | } |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 151 | verify(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | verifyAll(); |
| 153 | |
| 154 | pusher.stop(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Test bunch of OFMessages are correctly sent to multiple switches with single threads. |
| 159 | */ |
| 160 | @Test |
| 161 | public void testMultiSwitchAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 162 | final int numSwitch = 10; |
| 163 | final int numMsg = 100; // messages per thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | |
| 165 | beginInitMock(); |
| 166 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 167 | Map<IOFSwitch, List<OFMessage>> swMap = new HashMap<IOFSwitch, List<OFMessage>>(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 168 | for (int i = 0; i < numSwitch; ++i) { |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 169 | IOFSwitch sw = createConnectedSwitchMock(i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 170 | |
| 171 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 172 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 173 | for (int j = 0; j < numMsg; ++j) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 174 | OFMessage msg = createMock(OFMessage.class); |
| 175 | expect(msg.getXid()).andReturn((long) j).anyTimes(); |
| 176 | replay(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 177 | messages.add(msg); |
| 178 | |
| 179 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 180 | sw.write(eq(msg), eq((FloodlightContext) null)); |
| 181 | expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 182 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 183 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 186 | swMap.put(sw, messages); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 187 | replay(sw); |
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)) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 209 | verify(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 212 | verify(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 224 | final int numThreads = 10; |
| 225 | final int numMsg = 100; // messages per thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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>>(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 230 | for (int i = 0; i < numThreads; ++i) { |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 231 | IOFSwitch sw = createConnectedSwitchMock(i); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 232 | //EasyMock.replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 233 | |
| 234 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 235 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 236 | for (int j = 0; j < numMsg; ++j) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 237 | OFMessage msg = createMock(OFMessage.class); |
| 238 | expect(msg.getXid()).andReturn((long) j).anyTimes(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 239 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 240 | replay(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 241 | messages.add(msg); |
| 242 | |
| 243 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 244 | sw.write(eq(msg), eq((FloodlightContext) null)); |
| 245 | expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 246 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 247 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 250 | swMap.put(sw, messages); |
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 | } |
| 253 | |
| 254 | endInitMock(); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 255 | initPusher(numThreads); |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 256 | for (IOFSwitch sw : swMap.keySet()) { |
| 257 | for (OFMessage msg : swMap.get(sw)) { |
| 258 | boolean addResult = pusher.add(sw, msg); |
| 259 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | try { |
| 264 | // wait until message is processed. |
| 265 | Thread.sleep(1000); |
| 266 | } catch (InterruptedException e) { |
| 267 | fail("Failed in Thread.sleep()"); |
| 268 | } |
| 269 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 270 | for (IOFSwitch sw : swMap.keySet()) { |
| 271 | for (OFMessage msg : swMap.get(sw)) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 272 | verify(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 275 | verify(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | } |
| 277 | verifyAll(); |
| 278 | |
| 279 | pusher.stop(); |
| 280 | } |
| 281 | |
| 282 | private long barrierTime = 0; |
| 283 | |
| 284 | /** |
| 285 | * Test rate limitation of messages works correctly. |
| 286 | */ |
| 287 | @Test |
| 288 | public void testRateLimitedAddMessage() { |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 289 | final long limitRate = 100; // [bytes/ms] |
| 290 | final int numMsg = 1000; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 291 | |
| 292 | // Accuracy of FlowPusher's rate calculation can't be measured by unit test |
| 293 | // because switch doesn't return BARRIER_REPLY. |
| 294 | // In unit test we use approximate way to measure rate. This value is |
| 295 | // acceptable margin of measured rate. |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 296 | final double acceptableRate = limitRate * 1.2; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 297 | |
| 298 | beginInitMock(); |
| 299 | |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 300 | IOFSwitch sw = createConnectedSwitchMock(1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 301 | |
| 302 | List<OFMessage> messages = new ArrayList<OFMessage>(); |
| 303 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 304 | for (int i = 0; i < numMsg; ++i) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 305 | OFMessage msg = createMock(OFMessage.class); |
| 306 | expect(msg.getXid()).andReturn((long) 1).anyTimes(); |
| 307 | replay(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 308 | messages.add(msg); |
| 309 | |
| 310 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 311 | sw.write(eq(msg), eq((FloodlightContext) null)); |
| 312 | expectLastCall().once(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 313 | } catch (IOException e1) { |
| 314 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 319 | sw.write(anyObject(OFBarrierRequest.class), eq((FloodlightContext) null)); |
| 320 | expectLastCall().once(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 321 | barrierTime = System.currentTimeMillis(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 322 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 323 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 326 | replay(sw); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 327 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 328 | endInitMock(); |
| 329 | initPusher(1); |
| 330 | |
| 331 | pusher.createQueue(sw); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 332 | pusher.setRate(sw, limitRate); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 333 | |
| 334 | long beginTime = System.currentTimeMillis(); |
| 335 | for (OFMessage msg : messages) { |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 336 | boolean addResult = pusher.add(sw, msg); |
| 337 | assertTrue(addResult); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | pusher.barrierAsync(sw); |
| 341 | |
| 342 | try { |
| 343 | do { |
| 344 | Thread.sleep(1000); |
| 345 | } while (barrierTime == 0); |
| 346 | } catch (InterruptedException e) { |
| 347 | fail("Failed to sleep"); |
| 348 | } |
| 349 | |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 350 | double measuredRate = numMsg * 100 / (barrierTime - beginTime); |
| 351 | assertTrue(measuredRate < acceptableRate); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 352 | |
| 353 | for (OFMessage msg : messages) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 354 | verify(msg); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 355 | } |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 356 | verify(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 357 | verifyAll(); |
| 358 | |
| 359 | pusher.stop(); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Test barrier message is correctly sent to a switch. |
| 364 | */ |
| 365 | @Test |
| 366 | public void testBarrierMessage() { |
| 367 | beginInitMock(); |
| 368 | |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 369 | IOFSwitch sw = createConnectedSwitchMock(1); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 370 | expect(sw.getOFVersion()).andReturn(OFVersion.OF_10).once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 371 | |
| 372 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 373 | sw.write((OFMessage) anyObject(), eq((FloodlightContext) null)); |
| 374 | expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 375 | } catch (IOException e1) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 376 | fail("Failed in IOFWrite#write()"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 377 | } |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 378 | replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 379 | endInitMock(); |
| 380 | initPusher(1); |
| 381 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 382 | OFMessageFuture<OFBarrierReply> future = pusher.barrierAsync(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 383 | |
| 384 | assertNotNull(future); |
| 385 | |
| 386 | try { |
| 387 | Thread.sleep(1000); |
| 388 | } catch (InterruptedException e) { |
| 389 | fail("Failed to sleep"); |
| 390 | } |
| 391 | |
| 392 | verifyAll(); |
| 393 | |
| 394 | pusher.stop(); |
| 395 | } |
| 396 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 397 | static final long XID_TO_VERIFY = 100; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 398 | static final long DPID_TO_VERIFY = 10; |
| 399 | |
| 400 | /** |
| 401 | * Test FlowObject is correctly converted to message and is sent to a switch. |
| 402 | */ |
| 403 | @SuppressWarnings("unchecked") |
| 404 | @Test |
| 405 | public void testAddFlow() { |
| 406 | // instantiate required objects |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 407 | FlowEntry flowEntry1 = new FlowEntry(DPID_TO_VERIFY, 1, 11, null, null, 0, 0, Operator.ADD); |
| 408 | /* |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 409 | flowEntry1.setDpid(new Dpid(DPID_TO_VERIFY)); |
| 410 | flowEntry1.setFlowId(new FlowId(1)); |
Yuta HIGUCHI | a507baf | 2014-08-22 13:42:40 -0700 | [diff] [blame^] | 411 | flowEntry1.setInPort(PortNumber.uint16((short) 1)); |
| 412 | flowEntry1.setOutPort(PortNumber.uint16((short) 11)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 413 | flowEntry1.setFlowEntryId(new FlowEntryId(1)); |
| 414 | flowEntry1.setFlowEntryMatch(new FlowEntryMatch()); |
| 415 | flowEntry1.setFlowEntryActions(new FlowEntryActions()); |
| 416 | flowEntry1.setFlowEntryErrorState(new FlowEntryErrorState()); |
| 417 | flowEntry1.setFlowEntryUserState(FlowEntryUserState.FE_USER_ADD); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 418 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 419 | |
| 420 | beginInitMock(); |
| 421 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 422 | OFFlowModify fm = createMock(OFFlowModify.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 423 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 424 | OFFlowModify.Builder bld = createMock(OFFlowModify.Builder.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 425 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 426 | expect(bld.setIdleTimeout(anyInt())).andReturn(bld); |
| 427 | expect(bld.setHardTimeout(anyInt())).andReturn(bld); |
| 428 | expect(bld.setPriority(anyShort())).andReturn(bld); |
| 429 | expect(bld.setBufferId(OFBufferId.NO_BUFFER)).andReturn(bld); |
| 430 | expect(bld.setCookie(U64.of(anyLong()))).andReturn(bld); |
| 431 | expect(bld.setMatch(anyObject(Match.class))).andReturn(bld); |
| 432 | expect(bld.setActions((List<OFAction>) anyObject())).andReturn(bld); |
| 433 | expect(bld.setOutPort(OFPort.of(anyInt()))).andReturn(bld).atLeastOnce(); |
| 434 | expect(bld.build()).andReturn(fm); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 435 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 436 | expect(fm.getXid()).andReturn(XID_TO_VERIFY).anyTimes(); |
| 437 | expect(fm.getType()).andReturn(OFType.FLOW_MOD).anyTimes(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 438 | |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 439 | IOFSwitch sw = createConnectedSwitchMock(DPID_TO_VERIFY); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 440 | |
| 441 | try { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 442 | sw.write(anyObject(OFMessage.class), eq((FloodlightContext) null)); |
| 443 | 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 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 448 | replay(bld, fm); |
| 449 | replay(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 462 | verify(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 463 | verifyAll(); |
| 464 | |
| 465 | pusher.stop(); |
| 466 | } |
| 467 | |
| 468 | private void beginInitMock() { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 469 | context = createMock(FloodlightContext.class); |
| 470 | modContext = 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()); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 473 | flProviderService = createMock(IFloodlightProviderService.class); |
| 474 | threadPoolService = createMock(IThreadPoolService.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 475 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 476 | expect(modContext.getServiceImpl(eq(IThreadPoolService.class))) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 477 | .andReturn(threadPoolService).once(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 478 | expect(modContext.getServiceImpl(eq(IFloodlightProviderService.class))) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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. |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 481 | flProviderService.addOFMessageListener(eq(OFType.BARRIER_REPLY), |
| 482 | anyObject(FlowPusher.class)); |
| 483 | expectLastCall().once(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 484 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 485 | ScheduledExecutorService executor = createMock(ScheduledExecutorService.class); |
| 486 | expect(executor.schedule((Runnable) anyObject(), anyLong(), |
| 487 | (TimeUnit) anyObject())).andReturn(null).once(); |
| 488 | replay(executor); |
| 489 | expect(threadPoolService.getScheduledExecutor()).andReturn(executor).anyTimes(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | private void endInitMock() { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 493 | replay(threadPoolService); |
| 494 | replay(flProviderService); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 495 | //EasyMock.replay(factory10); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 496 | replay(modContext); |
| 497 | replay(context); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | private void verifyAll() { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 501 | verify(threadPoolService); |
| 502 | verify(flProviderService); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 503 | //EasyMock.verify(factory10); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 504 | verify(modContext); |
| 505 | verify(context); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 514 | private IOFSwitch createConnectedSwitchMock(long dpid) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 515 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 516 | expect(sw.isConnected()).andReturn(true).anyTimes(); |
| 517 | expect(sw.getId()).andReturn(dpid).anyTimes(); |
Jonathan Hart | ce07f32 | 2014-08-13 14:40:53 -0700 | [diff] [blame] | 518 | expect(sw.getStringId()).andReturn(HexString.toHexString(dpid)) |
| 519 | .anyTimes(); |
| 520 | expect(sw.getNextTransactionId()).andReturn(1).times(0, 1); |
| 521 | // TODO 1.3ize |
| 522 | expect(sw.getFactory()).andReturn(factory10).anyTimes(); |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 523 | sw.flush(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame] | 524 | expectLastCall().anyTimes(); |
Naoki Shiota | 4799310 | 2014-07-09 14:00:54 -0700 | [diff] [blame] | 525 | |
| 526 | return sw; |
| 527 | } |
| 528 | |
Naoki Shiota | 75b7dd6 | 2013-12-03 18:09:21 -0800 | [diff] [blame] | 529 | } |