Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | package net.floodlightcontroller.core.internal; |
| 19 | |
| 20 | import static org.easymock.EasyMock.*; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Collection; |
| 24 | import java.util.Date; |
| 25 | import java.util.HashMap; |
| 26 | import java.util.HashSet; |
| 27 | import java.util.List; |
| 28 | import java.util.Map; |
| 29 | import java.util.concurrent.ConcurrentHashMap; |
| 30 | import java.util.concurrent.Future; |
| 31 | import java.util.concurrent.TimeUnit; |
| 32 | |
| 33 | import net.floodlightcontroller.core.FloodlightProvider; |
| 34 | import net.floodlightcontroller.core.FloodlightContext; |
| 35 | import net.floodlightcontroller.core.IFloodlightProviderService; |
| 36 | import net.floodlightcontroller.core.IHAListener; |
| 37 | import net.floodlightcontroller.core.IFloodlightProviderService.Role; |
| 38 | import net.floodlightcontroller.core.IOFMessageFilterManagerService; |
| 39 | import net.floodlightcontroller.core.IOFMessageListener; |
| 40 | import net.floodlightcontroller.core.IListener.Command; |
| 41 | import net.floodlightcontroller.core.IOFSwitch; |
| 42 | import net.floodlightcontroller.core.IOFSwitchListener; |
| 43 | import net.floodlightcontroller.core.OFMessageFilterManager; |
| 44 | import net.floodlightcontroller.core.internal.Controller.IUpdate; |
| 45 | import net.floodlightcontroller.core.internal.Controller.SwitchUpdate; |
| 46 | import net.floodlightcontroller.core.internal.Controller.SwitchUpdateType; |
| 47 | import net.floodlightcontroller.core.internal.OFChannelState.HandshakeState; |
| 48 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 49 | import net.floodlightcontroller.core.test.MockFloodlightProvider; |
| 50 | import net.floodlightcontroller.core.test.MockThreadPoolService; |
| 51 | import net.floodlightcontroller.counter.CounterStore; |
| 52 | import net.floodlightcontroller.counter.ICounterStoreService; |
| 53 | import net.floodlightcontroller.packet.ARP; |
| 54 | import net.floodlightcontroller.packet.Ethernet; |
| 55 | import net.floodlightcontroller.packet.IPacket; |
| 56 | import net.floodlightcontroller.packet.IPv4; |
| 57 | import net.floodlightcontroller.perfmon.IPktInProcessingTimeService; |
| 58 | import net.floodlightcontroller.perfmon.PktInProcessingTime; |
| 59 | import net.floodlightcontroller.restserver.IRestApiService; |
| 60 | import net.floodlightcontroller.restserver.RestApiServer; |
| 61 | import net.floodlightcontroller.storage.IStorageSourceService; |
| 62 | import net.floodlightcontroller.storage.memory.MemoryStorageSource; |
| 63 | import net.floodlightcontroller.test.FloodlightTestCase; |
| 64 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
| 65 | |
| 66 | import org.easymock.Capture; |
| 67 | import org.easymock.EasyMock; |
| 68 | import org.jboss.netty.channel.Channel; |
| 69 | import org.junit.Test; |
| 70 | import org.openflow.protocol.OFError; |
| 71 | import org.openflow.protocol.OFError.OFBadRequestCode; |
| 72 | import org.openflow.protocol.OFError.OFErrorType; |
| 73 | import org.openflow.protocol.OFFeaturesReply; |
| 74 | import org.openflow.protocol.OFPacketIn; |
| 75 | import org.openflow.protocol.OFPacketOut; |
| 76 | import org.openflow.protocol.OFPhysicalPort; |
| 77 | import org.openflow.protocol.OFPort; |
| 78 | import org.openflow.protocol.OFPortStatus; |
| 79 | import org.openflow.protocol.OFStatisticsReply; |
| 80 | import org.openflow.protocol.OFType; |
| 81 | import org.openflow.protocol.OFPacketIn.OFPacketInReason; |
| 82 | import org.openflow.protocol.OFPortStatus.OFPortReason; |
| 83 | import org.openflow.protocol.OFVendor; |
| 84 | import org.openflow.protocol.action.OFAction; |
| 85 | import org.openflow.protocol.action.OFActionOutput; |
| 86 | import org.openflow.protocol.factory.BasicFactory; |
| 87 | import org.openflow.protocol.statistics.OFFlowStatisticsReply; |
| 88 | import org.openflow.protocol.statistics.OFStatistics; |
| 89 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 90 | import org.openflow.util.HexString; |
| 91 | import org.openflow.vendor.nicira.OFNiciraVendorData; |
| 92 | import org.openflow.vendor.nicira.OFRoleReplyVendorData; |
| 93 | |
| 94 | /** |
| 95 | * |
| 96 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 97 | */ |
| 98 | public class ControllerTest extends FloodlightTestCase { |
| 99 | |
| 100 | private Controller controller; |
| 101 | private MockThreadPoolService tp; |
| 102 | |
| 103 | @Override |
| 104 | public void setUp() throws Exception { |
| 105 | super.setUp(); |
| 106 | FloodlightModuleContext fmc = new FloodlightModuleContext(); |
| 107 | |
| 108 | FloodlightProvider cm = new FloodlightProvider(); |
| 109 | controller = (Controller)cm.getServiceImpls().get(IFloodlightProviderService.class); |
| 110 | fmc.addService(IFloodlightProviderService.class, controller); |
| 111 | |
| 112 | MemoryStorageSource memstorage = new MemoryStorageSource(); |
| 113 | fmc.addService(IStorageSourceService.class, memstorage); |
| 114 | |
| 115 | RestApiServer restApi = new RestApiServer(); |
| 116 | fmc.addService(IRestApiService.class, restApi); |
| 117 | |
| 118 | CounterStore cs = new CounterStore(); |
| 119 | fmc.addService(ICounterStoreService.class, cs); |
| 120 | |
| 121 | PktInProcessingTime ppt = new PktInProcessingTime(); |
| 122 | fmc.addService(IPktInProcessingTimeService.class, ppt); |
| 123 | |
| 124 | tp = new MockThreadPoolService(); |
| 125 | fmc.addService(IThreadPoolService.class, tp); |
| 126 | |
| 127 | ppt.init(fmc); |
| 128 | restApi.init(fmc); |
| 129 | memstorage.init(fmc); |
| 130 | cm.init(fmc); |
| 131 | tp.init(fmc); |
| 132 | ppt.startUp(fmc); |
| 133 | restApi.startUp(fmc); |
| 134 | memstorage.startUp(fmc); |
| 135 | cm.startUp(fmc); |
| 136 | tp.startUp(fmc); |
| 137 | } |
| 138 | |
| 139 | public Controller getController() { |
| 140 | return controller; |
| 141 | } |
| 142 | |
| 143 | protected OFStatisticsReply getStatisticsReply(int transactionId, |
| 144 | int count, boolean moreReplies) { |
| 145 | OFStatisticsReply sr = new OFStatisticsReply(); |
| 146 | sr.setXid(transactionId); |
| 147 | sr.setStatisticType(OFStatisticsType.FLOW); |
| 148 | List<OFStatistics> statistics = new ArrayList<OFStatistics>(); |
| 149 | for (int i = 0; i < count; ++i) { |
| 150 | statistics.add(new OFFlowStatisticsReply()); |
| 151 | } |
| 152 | sr.setStatistics(statistics); |
| 153 | if (moreReplies) |
| 154 | sr.setFlags((short) 1); |
| 155 | return sr; |
| 156 | } |
| 157 | |
| 158 | /* Set the mock expectations for sw when sw is passed to addSwitch */ |
| 159 | protected void setupSwitchForAddSwitch(IOFSwitch sw, long dpid) { |
| 160 | String dpidString = HexString.toHexString(dpid); |
| 161 | |
| 162 | expect(sw.getId()).andReturn(dpid).anyTimes(); |
| 163 | expect(sw.getStringId()).andReturn(dpidString).anyTimes(); |
| 164 | expect(sw.getConnectedSince()).andReturn(new Date()); |
| 165 | Channel channel = createMock(Channel.class); |
| 166 | expect(sw.getChannel()).andReturn(channel); |
| 167 | expect(channel.getRemoteAddress()).andReturn(null); |
| 168 | |
| 169 | expect(sw.getCapabilities()).andReturn(0).anyTimes(); |
| 170 | expect(sw.getBuffers()).andReturn(0).anyTimes(); |
| 171 | expect(sw.getTables()).andReturn((byte)0).anyTimes(); |
| 172 | expect(sw.getActions()).andReturn(0).anyTimes(); |
| 173 | expect(sw.getPorts()).andReturn(new ArrayList<OFPhysicalPort>()).anyTimes(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Run the controller's main loop so that updates are processed |
| 178 | */ |
| 179 | protected class ControllerRunThread extends Thread { |
| 180 | public void run() { |
| 181 | controller.openFlowPort = 0; // Don't listen |
| 182 | controller.run(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Verify that a listener that throws an exception halts further |
| 188 | * execution, and verify that the Commands STOP and CONTINUE are honored. |
| 189 | * @throws Exception |
| 190 | */ |
| 191 | @Test |
| 192 | public void testHandleMessages() throws Exception { |
| 193 | Controller controller = getController(); |
| 194 | controller.removeOFMessageListeners(OFType.PACKET_IN); |
| 195 | |
| 196 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 197 | expect(sw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes(); |
| 198 | |
| 199 | // Build our test packet |
| 200 | IPacket testPacket = new Ethernet() |
| 201 | .setSourceMACAddress("00:44:33:22:11:00") |
| 202 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 203 | .setEtherType(Ethernet.TYPE_ARP) |
| 204 | .setPayload( |
| 205 | new ARP() |
| 206 | .setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 207 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 208 | .setHardwareAddressLength((byte) 6) |
| 209 | .setProtocolAddressLength((byte) 4) |
| 210 | .setOpCode(ARP.OP_REPLY) |
| 211 | .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:00")) |
| 212 | .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1")) |
| 213 | .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55")) |
| 214 | .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2"))); |
| 215 | byte[] testPacketSerialized = testPacket.serialize(); |
| 216 | |
| 217 | // Build the PacketIn |
| 218 | OFPacketIn pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN)) |
| 219 | .setBufferId(-1) |
| 220 | .setInPort((short) 1) |
| 221 | .setPacketData(testPacketSerialized) |
| 222 | .setReason(OFPacketInReason.NO_MATCH) |
| 223 | .setTotalLength((short) testPacketSerialized.length); |
| 224 | |
| 225 | IOFMessageListener test1 = createMock(IOFMessageListener.class); |
| 226 | expect(test1.getName()).andReturn("test1").anyTimes(); |
| 227 | expect(test1.isCallbackOrderingPrereq((OFType)anyObject(), (String)anyObject())).andReturn(false).anyTimes(); |
| 228 | expect(test1.isCallbackOrderingPostreq((OFType)anyObject(), (String)anyObject())).andReturn(false).anyTimes(); |
| 229 | expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andThrow(new RuntimeException("This is NOT an error! We are testing exception catching.")); |
| 230 | IOFMessageListener test2 = createMock(IOFMessageListener.class); |
| 231 | expect(test2.getName()).andReturn("test2").anyTimes(); |
| 232 | expect(test2.isCallbackOrderingPrereq((OFType)anyObject(), (String)anyObject())).andReturn(false).anyTimes(); |
| 233 | expect(test2.isCallbackOrderingPostreq((OFType)anyObject(), (String)anyObject())).andReturn(false).anyTimes(); |
| 234 | // expect no calls to test2.receive() since test1.receive() threw an exception |
| 235 | |
| 236 | replay(test1, test2, sw); |
| 237 | controller.addOFMessageListener(OFType.PACKET_IN, test1); |
| 238 | controller.addOFMessageListener(OFType.PACKET_IN, test2); |
| 239 | try { |
| 240 | controller.handleMessage(sw, pi, null); |
| 241 | } catch (RuntimeException e) { |
| 242 | assertEquals(e.getMessage().startsWith("This is NOT an error!"), true); |
| 243 | } |
| 244 | verify(test1, test2, sw); |
| 245 | |
| 246 | // verify STOP works |
| 247 | reset(test1, test2, sw); |
| 248 | expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.STOP); |
| 249 | //expect(test1.getId()).andReturn(0).anyTimes(); |
| 250 | expect(sw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes(); |
| 251 | replay(test1, test2, sw); |
| 252 | controller.handleMessage(sw, pi, null); |
| 253 | verify(test1, test2, sw); |
| 254 | } |
| 255 | |
| 256 | public class FutureFetcher<E> implements Runnable { |
| 257 | public E value; |
| 258 | public Future<E> future; |
| 259 | |
| 260 | public FutureFetcher(Future<E> future) { |
| 261 | this.future = future; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void run() { |
| 266 | try { |
| 267 | value = future.get(); |
| 268 | } catch (Exception e) { |
| 269 | throw new RuntimeException(e); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @return the value |
| 275 | */ |
| 276 | public E getValue() { |
| 277 | return value; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @return the future |
| 282 | */ |
| 283 | public Future<E> getFuture() { |
| 284 | return future; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * |
| 290 | * @throws Exception |
| 291 | */ |
| 292 | @Test |
| 293 | public void testOFStatisticsFuture() throws Exception { |
| 294 | // Test for a single stats reply |
| 295 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 296 | sw.cancelStatisticsReply(1); |
| 297 | OFStatisticsFuture sf = new OFStatisticsFuture(tp, sw, 1); |
| 298 | |
| 299 | replay(sw); |
| 300 | List<OFStatistics> stats; |
| 301 | FutureFetcher<List<OFStatistics>> ff = new FutureFetcher<List<OFStatistics>>(sf); |
| 302 | Thread t = new Thread(ff); |
| 303 | t.start(); |
| 304 | sf.deliverFuture(sw, getStatisticsReply(1, 10, false)); |
| 305 | |
| 306 | t.join(); |
| 307 | stats = ff.getValue(); |
| 308 | verify(sw); |
| 309 | assertEquals(10, stats.size()); |
| 310 | |
| 311 | // Test multiple stats replies |
| 312 | reset(sw); |
| 313 | sw.cancelStatisticsReply(1); |
| 314 | |
| 315 | sf = new OFStatisticsFuture(tp, sw, 1); |
| 316 | |
| 317 | replay(sw); |
| 318 | ff = new FutureFetcher<List<OFStatistics>>(sf); |
| 319 | t = new Thread(ff); |
| 320 | t.start(); |
| 321 | sf.deliverFuture(sw, getStatisticsReply(1, 10, true)); |
| 322 | sf.deliverFuture(sw, getStatisticsReply(1, 5, false)); |
| 323 | t.join(); |
| 324 | |
| 325 | stats = sf.get(); |
| 326 | verify(sw); |
| 327 | assertEquals(15, stats.size()); |
| 328 | |
| 329 | // Test cancellation |
| 330 | reset(sw); |
| 331 | sw.cancelStatisticsReply(1); |
| 332 | sf = new OFStatisticsFuture(tp, sw, 1); |
| 333 | |
| 334 | replay(sw); |
| 335 | ff = new FutureFetcher<List<OFStatistics>>(sf); |
| 336 | t = new Thread(ff); |
| 337 | t.start(); |
| 338 | sf.cancel(true); |
| 339 | t.join(); |
| 340 | |
| 341 | stats = sf.get(); |
| 342 | verify(sw); |
| 343 | assertEquals(0, stats.size()); |
| 344 | |
| 345 | // Test self timeout |
| 346 | reset(sw); |
| 347 | sw.cancelStatisticsReply(1); |
| 348 | sf = new OFStatisticsFuture(tp, sw, 1, 75, TimeUnit.MILLISECONDS); |
| 349 | |
| 350 | replay(sw); |
| 351 | ff = new FutureFetcher<List<OFStatistics>>(sf); |
| 352 | t = new Thread(ff); |
| 353 | t.start(); |
| 354 | t.join(2000); |
| 355 | |
| 356 | stats = sf.get(); |
| 357 | verify(sw); |
| 358 | assertEquals(0, stats.size()); |
| 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void testMessageFilterManager() throws Exception { |
| 363 | class MyOFMessageFilterManager extends OFMessageFilterManager { |
| 364 | public MyOFMessageFilterManager(int timer_interval) { |
| 365 | super(); |
| 366 | TIMER_INTERVAL = timer_interval; |
| 367 | } |
| 368 | } |
| 369 | FloodlightModuleContext fmCntx = new FloodlightModuleContext(); |
| 370 | MockFloodlightProvider mfp = new MockFloodlightProvider(); |
| 371 | OFMessageFilterManager mfm = new MyOFMessageFilterManager(100); |
| 372 | MockThreadPoolService mtp = new MockThreadPoolService(); |
| 373 | fmCntx.addService(IOFMessageFilterManagerService.class, mfm); |
| 374 | fmCntx.addService(IFloodlightProviderService.class, mfp); |
| 375 | fmCntx.addService(IThreadPoolService.class, mtp); |
| 376 | String sid = null; |
| 377 | |
| 378 | mfm.init(fmCntx); |
| 379 | mfm.startUp(fmCntx); |
| 380 | |
| 381 | ConcurrentHashMap <String, String> filter; |
| 382 | int i; |
| 383 | |
| 384 | //Adding the filter works -- adds up to the maximum filter size. |
| 385 | for(i=mfm.getMaxFilterSize(); i > 0; --i) { |
| 386 | filter = new ConcurrentHashMap<String,String>(); |
| 387 | filter.put("mac", String.format("00:11:22:33:44:%d%d", i,i)); |
| 388 | sid = mfm.setupFilter(null, filter, 60); |
| 389 | assertTrue(mfm.getNumberOfFilters() == mfm.getMaxFilterSize() - i +1); |
| 390 | } |
| 391 | |
| 392 | // Add one more to see if you can't |
| 393 | filter = new ConcurrentHashMap<String,String>(); |
| 394 | filter.put("mac", "mac2"); |
| 395 | mfm.setupFilter(null, filter, 10*1000); |
| 396 | |
| 397 | assertTrue(mfm.getNumberOfFilters() == mfm.getMaxFilterSize()); |
| 398 | |
| 399 | // Deleting the filter works. |
| 400 | mfm.setupFilter(sid, null, -1); |
| 401 | assertTrue(mfm.getNumberOfFilters() == mfm.getMaxFilterSize()-1); |
| 402 | |
| 403 | // Creating mock switch to which we will send packet out and |
| 404 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 405 | expect(sw.getId()).andReturn(new Long(0)); |
| 406 | |
| 407 | // Mock Packet-in |
| 408 | IPacket testPacket = new Ethernet() |
| 409 | .setSourceMACAddress("00:44:33:22:11:00") |
| 410 | .setDestinationMACAddress("00:11:22:33:44:55") |
| 411 | .setEtherType(Ethernet.TYPE_ARP) |
| 412 | .setPayload( |
| 413 | new ARP() |
| 414 | .setHardwareType(ARP.HW_TYPE_ETHERNET) |
| 415 | .setProtocolType(ARP.PROTO_TYPE_IP) |
| 416 | .setHardwareAddressLength((byte) 6) |
| 417 | .setProtocolAddressLength((byte) 4) |
| 418 | .setOpCode(ARP.OP_REPLY) |
| 419 | .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:00")) |
| 420 | .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1")) |
| 421 | .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55")) |
| 422 | .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2"))); |
| 423 | byte[] testPacketSerialized = testPacket.serialize(); |
| 424 | |
| 425 | // Build the PacketIn |
| 426 | OFPacketIn pi = ((OFPacketIn) new BasicFactory().getMessage(OFType.PACKET_IN)) |
| 427 | .setBufferId(-1) |
| 428 | .setInPort((short) 1) |
| 429 | .setPacketData(testPacketSerialized) |
| 430 | .setReason(OFPacketInReason.NO_MATCH) |
| 431 | .setTotalLength((short) testPacketSerialized.length); |
| 432 | |
| 433 | // Mock Packet-out |
| 434 | OFPacketOut packetOut = |
| 435 | (OFPacketOut) mockFloodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT); |
| 436 | packetOut.setBufferId(pi.getBufferId()) |
| 437 | .setInPort(pi.getInPort()); |
| 438 | List<OFAction> poactions = new ArrayList<OFAction>(); |
| 439 | poactions.add(new OFActionOutput(OFPort.OFPP_TABLE.getValue(), (short) 0)); |
| 440 | packetOut.setActions(poactions) |
| 441 | .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH) |
| 442 | .setPacketData(testPacketSerialized) |
| 443 | .setLengthU(OFPacketOut.MINIMUM_LENGTH+packetOut.getActionsLength()+testPacketSerialized.length); |
| 444 | |
| 445 | FloodlightContext cntx = new FloodlightContext(); |
| 446 | IFloodlightProviderService.bcStore.put(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD, (Ethernet) testPacket); |
| 447 | |
| 448 | |
| 449 | // Let's check the listeners. |
| 450 | List <IOFMessageListener> lm; |
| 451 | |
| 452 | // Check to see if all the listeners are active. |
| 453 | lm = mfp.getListeners().get(OFType.PACKET_OUT); |
| 454 | assertTrue(lm.size() == 1); |
| 455 | assertTrue(lm.get(0).equals(mfm)); |
| 456 | |
| 457 | lm = mfp.getListeners().get(OFType.FLOW_MOD); |
| 458 | assertTrue(lm.size() == 1); |
| 459 | assertTrue(lm.get(0).equals(mfm)); |
| 460 | |
| 461 | lm = mfp.getListeners().get(OFType.PACKET_IN); |
| 462 | assertTrue(lm.size() == 1); |
| 463 | assertTrue(lm.get(0).equals(mfm)); |
| 464 | |
| 465 | HashSet<String> matchedFilters; |
| 466 | |
| 467 | // Send a packet in and check if it matches a filter. |
| 468 | matchedFilters = mfm.getMatchedFilters(pi, cntx); |
| 469 | assertTrue(matchedFilters.size() == 1); |
| 470 | |
| 471 | // Send a packet out and check if it matches a filter |
| 472 | matchedFilters = mfm.getMatchedFilters(packetOut, cntx); |
| 473 | assertTrue(matchedFilters.size() == 1); |
| 474 | |
| 475 | // Wait for all filters to be timed out. |
| 476 | Thread.sleep(150); |
| 477 | assertEquals(0, mfm.getNumberOfFilters()); |
| 478 | } |
| 479 | |
| 480 | @Test |
| 481 | public void testAddSwitch() throws Exception { |
| 482 | controller.activeSwitches = new ConcurrentHashMap<Long, IOFSwitch>(); |
| 483 | |
| 484 | //OFSwitchImpl oldsw = createMock(OFSwitchImpl.class); |
| 485 | OFSwitchImpl oldsw = new OFSwitchImpl(); |
| 486 | OFFeaturesReply featuresReply = new OFFeaturesReply(); |
| 487 | featuresReply.setDatapathId(0L); |
| 488 | featuresReply.setPorts(new ArrayList<OFPhysicalPort>()); |
| 489 | oldsw.setFeaturesReply(featuresReply); |
| 490 | //expect(oldsw.getId()).andReturn(0L).anyTimes(); |
| 491 | //expect(oldsw.asyncRemoveSwitchLock()).andReturn(rwlock.writeLock()).anyTimes(); |
| 492 | //oldsw.setConnected(false); |
| 493 | //expect(oldsw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes(); |
| 494 | |
| 495 | Channel channel = createNiceMock(Channel.class); |
| 496 | //expect(oldsw.getChannel()).andReturn(channel); |
| 497 | oldsw.setChannel(channel); |
| 498 | expect(channel.close()).andReturn(null); |
| 499 | |
| 500 | IOFSwitch newsw = createMock(IOFSwitch.class); |
| 501 | expect(newsw.getId()).andReturn(0L).anyTimes(); |
| 502 | expect(newsw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes(); |
| 503 | expect(newsw.getConnectedSince()).andReturn(new Date()); |
| 504 | Channel channel2 = createMock(Channel.class); |
| 505 | expect(newsw.getChannel()).andReturn(channel2); |
| 506 | expect(channel2.getRemoteAddress()).andReturn(null); |
| 507 | expect(newsw.getPorts()).andReturn(new ArrayList<OFPhysicalPort>()); |
| 508 | expect(newsw.getCapabilities()).andReturn(0).anyTimes(); |
| 509 | expect(newsw.getBuffers()).andReturn(0).anyTimes(); |
| 510 | expect(newsw.getTables()).andReturn((byte)0).anyTimes(); |
| 511 | expect(newsw.getActions()).andReturn(0).anyTimes(); |
Jonathan Hart | fe99038 | 2013-02-06 15:22:32 -0800 | [diff] [blame^] | 512 | expect(newsw.getPorts()).andReturn(new ArrayList<OFPhysicalPort>()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 513 | controller.activeSwitches.put(0L, oldsw); |
| 514 | replay(newsw, channel, channel2); |
| 515 | |
| 516 | controller.addSwitch(newsw); |
| 517 | |
| 518 | verify(newsw, channel, channel2); |
| 519 | } |
| 520 | |
| 521 | @Test |
| 522 | public void testUpdateQueue() throws Exception { |
| 523 | class DummySwitchListener implements IOFSwitchListener { |
| 524 | public int nAdded; |
| 525 | public int nRemoved; |
| 526 | public int nPortChanged; |
| 527 | public DummySwitchListener() { |
| 528 | nAdded = 0; |
| 529 | nRemoved = 0; |
| 530 | nPortChanged = 0; |
| 531 | } |
| 532 | public synchronized void addedSwitch(IOFSwitch sw) { |
| 533 | nAdded++; |
| 534 | notifyAll(); |
| 535 | } |
| 536 | public synchronized void removedSwitch(IOFSwitch sw) { |
| 537 | nRemoved++; |
| 538 | notifyAll(); |
| 539 | } |
| 540 | public String getName() { |
| 541 | return "dummy"; |
| 542 | } |
| 543 | @Override |
| 544 | public void switchPortChanged(Long switchId) { |
| 545 | nPortChanged++; |
| 546 | notifyAll(); |
| 547 | } |
| 548 | } |
| 549 | DummySwitchListener switchListener = new DummySwitchListener(); |
| 550 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 551 | ControllerRunThread t = new ControllerRunThread(); |
| 552 | t.start(); |
| 553 | |
| 554 | controller.addOFSwitchListener(switchListener); |
| 555 | synchronized(switchListener) { |
| 556 | controller.updates.put(controller.new SwitchUpdate(sw, |
| 557 | Controller.SwitchUpdateType.ADDED)); |
| 558 | switchListener.wait(500); |
| 559 | assertTrue("IOFSwitchListener.addedSwitch() was not called", |
| 560 | switchListener.nAdded == 1); |
| 561 | controller.updates.put(controller.new SwitchUpdate(sw, |
| 562 | Controller.SwitchUpdateType.REMOVED)); |
| 563 | switchListener.wait(500); |
| 564 | assertTrue("IOFSwitchListener.removedSwitch() was not called", |
| 565 | switchListener.nRemoved == 1); |
| 566 | controller.updates.put(controller.new SwitchUpdate(sw, |
| 567 | Controller.SwitchUpdateType.PORTCHANGED)); |
| 568 | switchListener.wait(500); |
| 569 | assertTrue("IOFSwitchListener.switchPortChanged() was not called", |
| 570 | switchListener.nPortChanged == 1); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | |
| 575 | private Map<String,Object> getFakeControllerIPRow(String id, String controllerId, |
| 576 | String type, int number, String discoveredIP ) { |
| 577 | HashMap<String, Object> row = new HashMap<String,Object>(); |
| 578 | row.put(Controller.CONTROLLER_INTERFACE_ID, id); |
| 579 | row.put(Controller.CONTROLLER_INTERFACE_CONTROLLER_ID, controllerId); |
| 580 | row.put(Controller.CONTROLLER_INTERFACE_TYPE, type); |
| 581 | row.put(Controller.CONTROLLER_INTERFACE_NUMBER, number); |
| 582 | row.put(Controller.CONTROLLER_INTERFACE_DISCOVERED_IP, discoveredIP); |
| 583 | return row; |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Test notifications for controller node IP changes. This requires |
| 588 | * synchronization between the main test thread and another thread |
| 589 | * that runs Controller's main loop and takes / handles updates. We |
| 590 | * synchronize with wait(timeout) / notifyAll(). We check for the |
| 591 | * expected condition after the wait returns. However, if wait returns |
| 592 | * due to the timeout (or due to spurious awaking) and the check fails we |
| 593 | * might just not have waited long enough. Using a long enough timeout |
| 594 | * mitigates this but we cannot get rid of the fundamental "issue". |
| 595 | * |
| 596 | * @throws Exception |
| 597 | */ |
| 598 | @Test |
| 599 | public void testControllerNodeIPChanges() throws Exception { |
| 600 | class DummyHAListener implements IHAListener { |
| 601 | public Map<String, String> curControllerNodeIPs; |
| 602 | public Map<String, String> addedControllerNodeIPs; |
| 603 | public Map<String, String> removedControllerNodeIPs; |
| 604 | public int nCalled; |
| 605 | |
| 606 | public DummyHAListener() { |
| 607 | this.nCalled = 0; |
| 608 | } |
| 609 | |
| 610 | @Override |
| 611 | public void roleChanged(Role oldRole, Role newRole) { |
| 612 | // ignore |
| 613 | } |
| 614 | |
| 615 | @Override |
| 616 | public synchronized void controllerNodeIPsChanged( |
| 617 | Map<String, String> curControllerNodeIPs, |
| 618 | Map<String, String> addedControllerNodeIPs, |
| 619 | Map<String, String> removedControllerNodeIPs) { |
| 620 | this.curControllerNodeIPs = curControllerNodeIPs; |
| 621 | this.addedControllerNodeIPs = addedControllerNodeIPs; |
| 622 | this.removedControllerNodeIPs = removedControllerNodeIPs; |
| 623 | this.nCalled++; |
| 624 | notifyAll(); |
| 625 | } |
| 626 | |
| 627 | public void do_assert(int nCalled, |
| 628 | Map<String, String> curControllerNodeIPs, |
| 629 | Map<String, String> addedControllerNodeIPs, |
| 630 | Map<String, String> removedControllerNodeIPs) { |
| 631 | assertEquals("nCalled is not as expected", nCalled, this.nCalled); |
| 632 | assertEquals("curControllerNodeIPs is not as expected", |
| 633 | curControllerNodeIPs, this.curControllerNodeIPs); |
| 634 | assertEquals("addedControllerNodeIPs is not as expected", |
| 635 | addedControllerNodeIPs, this.addedControllerNodeIPs); |
| 636 | assertEquals("removedControllerNodeIPs is not as expected", |
| 637 | removedControllerNodeIPs, this.removedControllerNodeIPs); |
| 638 | |
| 639 | } |
| 640 | } |
| 641 | long waitTimeout = 250; // ms |
| 642 | DummyHAListener listener = new DummyHAListener(); |
| 643 | HashMap<String,String> expectedCurMap = new HashMap<String, String>(); |
| 644 | HashMap<String,String> expectedAddedMap = new HashMap<String, String>(); |
| 645 | HashMap<String,String> expectedRemovedMap = new HashMap<String, String>(); |
| 646 | |
| 647 | controller.addHAListener(listener); |
| 648 | ControllerRunThread t = new ControllerRunThread(); |
| 649 | t.start(); |
| 650 | |
| 651 | synchronized(listener) { |
| 652 | // Insert a first entry |
| 653 | controller.storageSource.insertRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 654 | getFakeControllerIPRow("row1", "c1", "Ethernet", 0, "1.1.1.1")); |
| 655 | expectedCurMap.clear(); |
| 656 | expectedAddedMap.clear(); |
| 657 | expectedRemovedMap.clear(); |
| 658 | expectedCurMap.put("c1", "1.1.1.1"); |
| 659 | expectedAddedMap.put("c1", "1.1.1.1"); |
| 660 | listener.wait(waitTimeout); |
| 661 | listener.do_assert(1, expectedCurMap, expectedAddedMap, expectedRemovedMap); |
| 662 | |
| 663 | // Add an interface that we want to ignore. |
| 664 | controller.storageSource.insertRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 665 | getFakeControllerIPRow("row2", "c1", "Ethernet", 1, "1.1.1.2")); |
| 666 | listener.wait(waitTimeout); // TODO: do a different check. This call will have to wait for the timeout |
| 667 | assertTrue("controllerNodeIPsChanged() should not have been called here", |
| 668 | listener.nCalled == 1); |
| 669 | |
| 670 | // Add another entry |
| 671 | controller.storageSource.insertRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 672 | getFakeControllerIPRow("row3", "c2", "Ethernet", 0, "2.2.2.2")); |
| 673 | expectedCurMap.clear(); |
| 674 | expectedAddedMap.clear(); |
| 675 | expectedRemovedMap.clear(); |
| 676 | expectedCurMap.put("c1", "1.1.1.1"); |
| 677 | expectedCurMap.put("c2", "2.2.2.2"); |
| 678 | expectedAddedMap.put("c2", "2.2.2.2"); |
| 679 | listener.wait(waitTimeout); |
| 680 | listener.do_assert(2, expectedCurMap, expectedAddedMap, expectedRemovedMap); |
| 681 | |
| 682 | |
| 683 | // Update an entry |
| 684 | controller.storageSource.updateRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 685 | "row3", getFakeControllerIPRow("row3", "c2", "Ethernet", 0, "2.2.2.3")); |
| 686 | expectedCurMap.clear(); |
| 687 | expectedAddedMap.clear(); |
| 688 | expectedRemovedMap.clear(); |
| 689 | expectedCurMap.put("c1", "1.1.1.1"); |
| 690 | expectedCurMap.put("c2", "2.2.2.3"); |
| 691 | expectedAddedMap.put("c2", "2.2.2.3"); |
| 692 | expectedRemovedMap.put("c2", "2.2.2.2"); |
| 693 | listener.wait(waitTimeout); |
| 694 | listener.do_assert(3, expectedCurMap, expectedAddedMap, expectedRemovedMap); |
| 695 | |
| 696 | // Delete an entry |
| 697 | controller.storageSource.deleteRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 698 | "row3"); |
| 699 | expectedCurMap.clear(); |
| 700 | expectedAddedMap.clear(); |
| 701 | expectedRemovedMap.clear(); |
| 702 | expectedCurMap.put("c1", "1.1.1.1"); |
| 703 | expectedRemovedMap.put("c2", "2.2.2.3"); |
| 704 | listener.wait(waitTimeout); |
| 705 | listener.do_assert(4, expectedCurMap, expectedAddedMap, expectedRemovedMap); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | @Test |
| 710 | public void testGetControllerNodeIPs() { |
| 711 | HashMap<String,String> expectedCurMap = new HashMap<String, String>(); |
| 712 | |
| 713 | controller.storageSource.insertRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 714 | getFakeControllerIPRow("row1", "c1", "Ethernet", 0, "1.1.1.1")); |
| 715 | controller.storageSource.insertRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 716 | getFakeControllerIPRow("row2", "c1", "Ethernet", 1, "1.1.1.2")); |
| 717 | controller.storageSource.insertRow(Controller.CONTROLLER_INTERFACE_TABLE_NAME, |
| 718 | getFakeControllerIPRow("row3", "c2", "Ethernet", 0, "2.2.2.2")); |
| 719 | expectedCurMap.put("c1", "1.1.1.1"); |
| 720 | expectedCurMap.put("c2", "2.2.2.2"); |
| 721 | assertEquals("expectedControllerNodeIPs is not as expected", |
| 722 | expectedCurMap, controller.getControllerNodeIPs()); |
| 723 | } |
| 724 | |
| 725 | @Test |
| 726 | public void testSetRoleNull() { |
| 727 | try { |
| 728 | controller.setRole(null); |
| 729 | fail("Should have thrown an Exception"); |
| 730 | } |
| 731 | catch (NullPointerException e) { |
| 732 | //exptected |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | @Test |
| 737 | public void testSetRole() { |
| 738 | controller.connectedSwitches.add(new OFSwitchImpl()); |
| 739 | RoleChanger roleChanger = createMock(RoleChanger.class); |
| 740 | roleChanger.submitRequest(controller.connectedSwitches, Role.SLAVE); |
| 741 | controller.roleChanger = roleChanger; |
| 742 | |
| 743 | assertEquals("Check that update queue is empty", 0, |
| 744 | controller.updates.size()); |
| 745 | |
| 746 | replay(roleChanger); |
| 747 | controller.setRole(Role.SLAVE); |
| 748 | verify(roleChanger); |
| 749 | |
| 750 | Controller.IUpdate upd = controller.updates.poll(); |
| 751 | assertNotNull("Check that update queue has an update", upd); |
| 752 | assertTrue("Check that update is HARoleUpdate", |
| 753 | upd instanceof Controller.HARoleUpdate); |
| 754 | Controller.HARoleUpdate roleUpd = (Controller.HARoleUpdate)upd; |
| 755 | assertSame(null, roleUpd.oldRole); |
| 756 | assertSame(Role.SLAVE, roleUpd.newRole); |
| 757 | } |
| 758 | |
| 759 | @Test |
| 760 | public void testCheckSwitchReady() { |
| 761 | OFChannelState state = new OFChannelState(); |
| 762 | Controller.OFChannelHandler chdlr = controller.new OFChannelHandler(state); |
| 763 | chdlr.sw = createMock(OFSwitchImpl.class); |
| 764 | |
| 765 | // Wrong current state |
| 766 | // Should not go to READY |
| 767 | state.hsState = OFChannelState.HandshakeState.HELLO; |
| 768 | state.hasDescription = true; |
| 769 | state.hasGetConfigReply = true; |
| 770 | replay(chdlr.sw); // nothing called on sw |
| 771 | chdlr.checkSwitchReady(); |
| 772 | verify(chdlr.sw); |
| 773 | assertSame(OFChannelState.HandshakeState.HELLO, state.hsState); |
| 774 | reset(chdlr.sw); |
| 775 | |
| 776 | // Have only config reply |
| 777 | state.hsState = OFChannelState.HandshakeState.FEATURES_REPLY; |
| 778 | state.hasDescription = false; |
| 779 | state.hasGetConfigReply = true; |
| 780 | replay(chdlr.sw); |
| 781 | chdlr.checkSwitchReady(); |
| 782 | verify(chdlr.sw); |
| 783 | assertSame(OFChannelState.HandshakeState.FEATURES_REPLY, state.hsState); |
| 784 | assertTrue(controller.connectedSwitches.isEmpty()); |
| 785 | assertTrue(controller.activeSwitches.isEmpty()); |
| 786 | reset(chdlr.sw); |
| 787 | |
| 788 | // Have only desc reply |
| 789 | state.hsState = OFChannelState.HandshakeState.FEATURES_REPLY; |
| 790 | state.hasDescription = true; |
| 791 | state.hasGetConfigReply = false; |
| 792 | replay(chdlr.sw); |
| 793 | chdlr.checkSwitchReady(); |
| 794 | verify(chdlr.sw); |
| 795 | assertSame(OFChannelState.HandshakeState.FEATURES_REPLY, state.hsState); |
| 796 | assertTrue(controller.connectedSwitches.isEmpty()); |
| 797 | assertTrue(controller.activeSwitches.isEmpty()); |
| 798 | reset(chdlr.sw); |
| 799 | |
| 800 | ////////////////////////////////////////// |
| 801 | // Finally, everything is right. Should advance to READY |
| 802 | ////////////////////////////////////////// |
| 803 | controller.roleChanger = createMock(RoleChanger.class); |
| 804 | state.hsState = OFChannelState.HandshakeState.FEATURES_REPLY; |
| 805 | state.hasDescription = true; |
| 806 | state.hasGetConfigReply = true; |
| 807 | // Role support disabled. Switch should be promoted to active switch |
| 808 | // list. |
| 809 | setupSwitchForAddSwitch(chdlr.sw, 0L); |
| 810 | chdlr.sw.clearAllFlowMods(); |
| 811 | replay(controller.roleChanger, chdlr.sw); |
| 812 | chdlr.checkSwitchReady(); |
| 813 | verify(controller.roleChanger, chdlr.sw); |
| 814 | assertSame(OFChannelState.HandshakeState.READY, state.hsState); |
| 815 | assertSame(chdlr.sw, controller.activeSwitches.get(0L)); |
| 816 | assertTrue(controller.connectedSwitches.contains(chdlr.sw)); |
| 817 | assertTrue(state.firstRoleReplyReceived); |
| 818 | reset(chdlr.sw); |
| 819 | reset(controller.roleChanger); |
| 820 | controller.connectedSwitches.clear(); |
| 821 | controller.activeSwitches.clear(); |
| 822 | |
| 823 | |
| 824 | // Role support enabled. |
| 825 | state.hsState = OFChannelState.HandshakeState.FEATURES_REPLY; |
| 826 | controller.role = Role.MASTER; |
| 827 | Capture<Collection<OFSwitchImpl>> swListCapture = |
| 828 | new Capture<Collection<OFSwitchImpl>>(); |
| 829 | controller.roleChanger.submitRequest(capture(swListCapture), |
| 830 | same(Role.MASTER)); |
| 831 | replay(controller.roleChanger, chdlr.sw); |
| 832 | chdlr.checkSwitchReady(); |
| 833 | verify(controller.roleChanger, chdlr.sw); |
| 834 | assertSame(OFChannelState.HandshakeState.READY, state.hsState); |
| 835 | assertTrue(controller.activeSwitches.isEmpty()); |
| 836 | assertTrue(controller.connectedSwitches.contains(chdlr.sw)); |
| 837 | assertTrue(state.firstRoleReplyReceived); |
| 838 | Collection<OFSwitchImpl> swList = swListCapture.getValue(); |
| 839 | assertEquals(1, swList.size()); |
| 840 | assertTrue("swList must contain this switch", swList.contains(chdlr.sw)); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | @Test |
| 845 | public void testChannelDisconnected() throws Exception { |
| 846 | OFChannelState state = new OFChannelState(); |
| 847 | state.hsState = OFChannelState.HandshakeState.READY; |
| 848 | Controller.OFChannelHandler chdlr = controller.new OFChannelHandler(state); |
| 849 | chdlr.sw = createMock(OFSwitchImpl.class); |
| 850 | |
| 851 | // Switch is active |
| 852 | expect(chdlr.sw.getId()).andReturn(0L).anyTimes(); |
| 853 | expect(chdlr.sw.getStringId()).andReturn("00:00:00:00:00:00:00:00") |
| 854 | .anyTimes(); |
| 855 | chdlr.sw.cancelAllStatisticsReplies(); |
| 856 | chdlr.sw.setConnected(false); |
| 857 | expect(chdlr.sw.isConnected()).andReturn(true); |
| 858 | |
| 859 | controller.connectedSwitches.add(chdlr.sw); |
| 860 | controller.activeSwitches.put(0L, chdlr.sw); |
| 861 | |
| 862 | replay(chdlr.sw); |
| 863 | chdlr.channelDisconnected(null, null); |
| 864 | verify(chdlr.sw); |
| 865 | |
| 866 | // Switch is connected but not active |
| 867 | reset(chdlr.sw); |
| 868 | expect(chdlr.sw.getId()).andReturn(0L).anyTimes(); |
| 869 | chdlr.sw.setConnected(false); |
| 870 | replay(chdlr.sw); |
| 871 | chdlr.channelDisconnected(null, null); |
| 872 | verify(chdlr.sw); |
| 873 | |
| 874 | // Not in ready state |
| 875 | state.hsState = HandshakeState.START; |
| 876 | reset(chdlr.sw); |
| 877 | replay(chdlr.sw); |
| 878 | chdlr.channelDisconnected(null, null); |
| 879 | verify(chdlr.sw); |
| 880 | |
| 881 | // Switch is null |
| 882 | state.hsState = HandshakeState.READY; |
| 883 | chdlr.sw = null; |
| 884 | chdlr.channelDisconnected(null, null); |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | @Test |
| 889 | public void testRoleChangeForSerialFailoverSwitch() throws Exception { |
| 890 | OFSwitchImpl newsw = createMock(OFSwitchImpl.class); |
| 891 | expect(newsw.getId()).andReturn(0L).anyTimes(); |
| 892 | expect(newsw.getStringId()).andReturn("00:00:00:00:00:00:00").anyTimes(); |
| 893 | Channel channel2 = createMock(Channel.class); |
| 894 | expect(newsw.getChannel()).andReturn(channel2); |
| 895 | |
| 896 | // newsw.role is null because the switch does not support |
| 897 | // role request messages |
| 898 | expect(newsw.getAttribute(IOFSwitch.SWITCH_SUPPORTS_NX_ROLE)) |
| 899 | .andReturn(false); |
| 900 | // switch is connected |
| 901 | controller.connectedSwitches.add(newsw); |
| 902 | |
| 903 | // the switch should get disconnected when role is changed to SLAVE |
| 904 | expect(channel2.close()).andReturn(null); |
| 905 | |
| 906 | replay(newsw, channel2); |
| 907 | controller.setRole(Role.SLAVE); |
| 908 | verify(newsw, channel2); |
| 909 | } |
| 910 | */ |
| 911 | |
| 912 | @Test |
| 913 | public void testRoleNotSupportedError() throws Exception { |
| 914 | int xid = 424242; |
| 915 | OFChannelState state = new OFChannelState(); |
| 916 | state.hsState = HandshakeState.READY; |
| 917 | Controller.OFChannelHandler chdlr = controller.new OFChannelHandler(state); |
| 918 | chdlr.sw = createMock(OFSwitchImpl.class); |
| 919 | Channel ch = createMock(Channel.class); |
| 920 | |
| 921 | // the error returned when role request message is not supported by sw |
| 922 | OFError msg = new OFError(); |
| 923 | msg.setType(OFType.ERROR); |
| 924 | msg.setXid(xid); |
| 925 | msg.setErrorType(OFErrorType.OFPET_BAD_REQUEST); |
| 926 | msg.setErrorCode(OFBadRequestCode.OFPBRC_BAD_VENDOR); |
| 927 | |
| 928 | // the switch connection should get disconnected when the controller is |
| 929 | // in SLAVE mode and the switch does not support role-request messages |
| 930 | state.firstRoleReplyReceived = false; |
| 931 | controller.role = Role.SLAVE; |
| 932 | expect(chdlr.sw.checkFirstPendingRoleRequestXid(xid)).andReturn(true); |
| 933 | chdlr.sw.deliverRoleRequestNotSupported(xid); |
| 934 | expect(chdlr.sw.getChannel()).andReturn(ch).anyTimes(); |
| 935 | expect(ch.close()).andReturn(null); |
| 936 | |
| 937 | replay(ch, chdlr.sw); |
| 938 | chdlr.processOFMessage(msg); |
| 939 | verify(ch, chdlr.sw); |
| 940 | assertTrue("state.firstRoleReplyReceived must be true", |
| 941 | state.firstRoleReplyReceived); |
| 942 | assertTrue("activeSwitches must be empty", |
| 943 | controller.activeSwitches.isEmpty()); |
| 944 | reset(ch, chdlr.sw); |
| 945 | |
| 946 | |
| 947 | // a different error message - should also reject role request |
| 948 | msg.setErrorType(OFErrorType.OFPET_BAD_REQUEST); |
| 949 | msg.setErrorCode(OFBadRequestCode.OFPBRC_EPERM); |
| 950 | state.firstRoleReplyReceived = false; |
| 951 | controller.role = Role.SLAVE; |
| 952 | expect(chdlr.sw.checkFirstPendingRoleRequestXid(xid)).andReturn(true); |
| 953 | chdlr.sw.deliverRoleRequestNotSupported(xid); |
| 954 | expect(chdlr.sw.getChannel()).andReturn(ch).anyTimes(); |
| 955 | expect(ch.close()).andReturn(null); |
| 956 | replay(ch, chdlr.sw); |
| 957 | |
| 958 | chdlr.processOFMessage(msg); |
| 959 | verify(ch, chdlr.sw); |
| 960 | assertTrue("state.firstRoleReplyReceived must be True even with EPERM", |
| 961 | state.firstRoleReplyReceived); |
| 962 | assertTrue("activeSwitches must be empty", |
| 963 | controller.activeSwitches.isEmpty()); |
| 964 | reset(ch, chdlr.sw); |
| 965 | |
| 966 | |
| 967 | // We are MASTER, the switch should be added to the list of active |
| 968 | // switches. |
| 969 | state.firstRoleReplyReceived = false; |
| 970 | controller.role = Role.MASTER; |
| 971 | expect(chdlr.sw.checkFirstPendingRoleRequestXid(xid)).andReturn(true); |
| 972 | chdlr.sw.deliverRoleRequestNotSupported(xid); |
| 973 | setupSwitchForAddSwitch(chdlr.sw, 0L); |
| 974 | chdlr.sw.clearAllFlowMods(); |
| 975 | replay(ch, chdlr.sw); |
| 976 | |
| 977 | chdlr.processOFMessage(msg); |
| 978 | verify(ch, chdlr.sw); |
| 979 | assertTrue("state.firstRoleReplyReceived must be true", |
| 980 | state.firstRoleReplyReceived); |
| 981 | assertSame("activeSwitches must contain this switch", |
| 982 | chdlr.sw, controller.activeSwitches.get(0L)); |
| 983 | reset(ch, chdlr.sw); |
| 984 | |
| 985 | } |
| 986 | |
| 987 | |
| 988 | @Test |
| 989 | public void testVendorMessageUnknown() throws Exception { |
| 990 | // Check behavior with an unknown vendor id |
| 991 | OFChannelState state = new OFChannelState(); |
| 992 | state.hsState = HandshakeState.READY; |
| 993 | Controller.OFChannelHandler chdlr = controller.new OFChannelHandler(state); |
| 994 | OFVendor msg = new OFVendor(); |
| 995 | msg.setVendor(0); |
| 996 | chdlr.processOFMessage(msg); |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | // Helper function. |
| 1001 | protected Controller.OFChannelHandler getChannelHandlerForRoleReplyTest() { |
| 1002 | OFChannelState state = new OFChannelState(); |
| 1003 | state.hsState = HandshakeState.READY; |
| 1004 | Controller.OFChannelHandler chdlr = controller.new OFChannelHandler(state); |
| 1005 | chdlr.sw = createMock(OFSwitchImpl.class); |
| 1006 | return chdlr; |
| 1007 | } |
| 1008 | |
| 1009 | // Helper function |
| 1010 | protected OFVendor getRoleReplyMsgForRoleReplyTest(int xid, int nicira_role) { |
| 1011 | OFVendor msg = new OFVendor(); |
| 1012 | msg.setXid(xid); |
| 1013 | msg.setVendor(OFNiciraVendorData.NX_VENDOR_ID); |
| 1014 | OFRoleReplyVendorData roleReplyVendorData = |
| 1015 | new OFRoleReplyVendorData(OFRoleReplyVendorData.NXT_ROLE_REPLY); |
| 1016 | msg.setVendorData(roleReplyVendorData); |
| 1017 | roleReplyVendorData.setRole(nicira_role); |
| 1018 | return msg; |
| 1019 | } |
| 1020 | |
| 1021 | /** invalid role in role reply */ |
| 1022 | @Test |
| 1023 | public void testNiciraRoleReplyInvalidRole() |
| 1024 | throws Exception { |
| 1025 | int xid = 424242; |
| 1026 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1027 | Channel ch = createMock(Channel.class); |
| 1028 | expect(chdlr.sw.getChannel()).andReturn(ch); |
| 1029 | expect(ch.close()).andReturn(null); |
| 1030 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, 232323); |
| 1031 | replay(chdlr.sw, ch); |
| 1032 | chdlr.processOFMessage(msg); |
| 1033 | verify(chdlr.sw, ch); |
| 1034 | } |
| 1035 | |
| 1036 | /** First role reply message received: transition from slave to master */ |
| 1037 | @Test |
| 1038 | public void testNiciraRoleReplySlave2MasterFristTime() |
| 1039 | throws Exception { |
| 1040 | int xid = 424242; |
| 1041 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1042 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, |
| 1043 | OFRoleReplyVendorData.NX_ROLE_MASTER); |
| 1044 | |
| 1045 | chdlr.sw.deliverRoleReply(xid, Role.MASTER); |
| 1046 | expect(chdlr.sw.isActive()).andReturn(true); |
| 1047 | setupSwitchForAddSwitch(chdlr.sw, 1L); |
| 1048 | chdlr.sw.clearAllFlowMods(); |
| 1049 | chdlr.state.firstRoleReplyReceived = false; |
| 1050 | replay(chdlr.sw); |
| 1051 | chdlr.processOFMessage(msg); |
| 1052 | verify(chdlr.sw); |
| 1053 | assertTrue("state.firstRoleReplyReceived must be true", |
| 1054 | chdlr.state.firstRoleReplyReceived); |
| 1055 | assertSame("activeSwitches must contain this switch", |
| 1056 | chdlr.sw, controller.activeSwitches.get(1L)); |
| 1057 | } |
| 1058 | |
| 1059 | |
| 1060 | /** Not first role reply message received: transition from slave to master */ |
| 1061 | @Test |
| 1062 | public void testNiciraRoleReplySlave2MasterNotFristTime() |
| 1063 | throws Exception { |
| 1064 | int xid = 424242; |
| 1065 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1066 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, |
| 1067 | OFRoleReplyVendorData.NX_ROLE_MASTER); |
| 1068 | |
| 1069 | chdlr.sw.deliverRoleReply(xid, Role.MASTER); |
| 1070 | expect(chdlr.sw.isActive()).andReturn(true); |
| 1071 | setupSwitchForAddSwitch(chdlr.sw, 1L); |
| 1072 | chdlr.state.firstRoleReplyReceived = true; |
| 1073 | // Flow table shouldn't be wipe |
| 1074 | replay(chdlr.sw); |
| 1075 | chdlr.processOFMessage(msg); |
| 1076 | verify(chdlr.sw); |
| 1077 | assertTrue("state.firstRoleReplyReceived must be true", |
| 1078 | chdlr.state.firstRoleReplyReceived); |
| 1079 | assertSame("activeSwitches must contain this switch", |
| 1080 | chdlr.sw, controller.activeSwitches.get(1L)); |
| 1081 | } |
| 1082 | |
| 1083 | /** transition from slave to equal */ |
| 1084 | @Test |
| 1085 | public void testNiciraRoleReplySlave2Equal() |
| 1086 | throws Exception { |
| 1087 | int xid = 424242; |
| 1088 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1089 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, |
| 1090 | OFRoleReplyVendorData.NX_ROLE_OTHER); |
| 1091 | |
| 1092 | chdlr.sw.deliverRoleReply(xid, Role.EQUAL); |
| 1093 | expect(chdlr.sw.isActive()).andReturn(true); |
| 1094 | setupSwitchForAddSwitch(chdlr.sw, 1L); |
| 1095 | chdlr.sw.clearAllFlowMods(); |
| 1096 | chdlr.state.firstRoleReplyReceived = false; |
| 1097 | replay(chdlr.sw); |
| 1098 | chdlr.processOFMessage(msg); |
| 1099 | verify(chdlr.sw); |
| 1100 | assertTrue("state.firstRoleReplyReceived must be true", |
| 1101 | chdlr.state.firstRoleReplyReceived); |
| 1102 | assertSame("activeSwitches must contain this switch", |
| 1103 | chdlr.sw, controller.activeSwitches.get(1L)); |
| 1104 | }; |
| 1105 | |
| 1106 | @Test |
| 1107 | /** Slave2Slave transition ==> no change */ |
| 1108 | public void testNiciraRoleReplySlave2Slave() throws Exception{ |
| 1109 | int xid = 424242; |
| 1110 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1111 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, |
| 1112 | OFRoleReplyVendorData.NX_ROLE_SLAVE); |
| 1113 | |
| 1114 | chdlr.sw.deliverRoleReply(xid, Role.SLAVE); |
| 1115 | expect(chdlr.sw.getId()).andReturn(1L).anyTimes(); |
| 1116 | expect(chdlr.sw.getStringId()).andReturn("00:00:00:00:00:00:00:01") |
| 1117 | .anyTimes(); |
| 1118 | expect(chdlr.sw.isActive()).andReturn(false); |
| 1119 | // don't add switch to activeSwitches ==> slave2slave |
| 1120 | chdlr.state.firstRoleReplyReceived = false; |
| 1121 | replay(chdlr.sw); |
| 1122 | chdlr.processOFMessage(msg); |
| 1123 | verify(chdlr.sw); |
| 1124 | assertTrue("state.firstRoleReplyReceived must be true", |
| 1125 | chdlr.state.firstRoleReplyReceived); |
| 1126 | assertTrue("activeSwitches must be empty", |
| 1127 | controller.activeSwitches.isEmpty()); |
| 1128 | } |
| 1129 | |
| 1130 | @Test |
| 1131 | /** Equal2Master transition ==> no change */ |
| 1132 | public void testNiciraRoleReplyEqual2Master() throws Exception{ |
| 1133 | int xid = 424242; |
| 1134 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1135 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, |
| 1136 | OFRoleReplyVendorData.NX_ROLE_MASTER); |
| 1137 | |
| 1138 | chdlr.sw.deliverRoleReply(xid, Role.MASTER); |
| 1139 | expect(chdlr.sw.getId()).andReturn(1L).anyTimes(); |
| 1140 | expect(chdlr.sw.getStringId()).andReturn("00:00:00:00:00:00:00:01") |
| 1141 | .anyTimes(); |
| 1142 | expect(chdlr.sw.isActive()).andReturn(true); |
| 1143 | controller.activeSwitches.put(1L, chdlr.sw); |
| 1144 | chdlr.state.firstRoleReplyReceived = false; |
| 1145 | replay(chdlr.sw); |
| 1146 | chdlr.processOFMessage(msg); |
| 1147 | verify(chdlr.sw); |
| 1148 | assertTrue("state.firstRoleReplyReceived must be true", |
| 1149 | chdlr.state.firstRoleReplyReceived); |
| 1150 | assertSame("activeSwitches must contain this switch", |
| 1151 | chdlr.sw, controller.activeSwitches.get(1L)); |
| 1152 | } |
| 1153 | |
| 1154 | @Test |
| 1155 | public void testNiciraRoleReplyMaster2Slave() |
| 1156 | throws Exception { |
| 1157 | int xid = 424242; |
| 1158 | Controller.OFChannelHandler chdlr = getChannelHandlerForRoleReplyTest(); |
| 1159 | OFVendor msg = getRoleReplyMsgForRoleReplyTest(xid, |
| 1160 | OFRoleReplyVendorData.NX_ROLE_SLAVE); |
| 1161 | |
| 1162 | chdlr.sw.deliverRoleReply(xid, Role.SLAVE); |
| 1163 | expect(chdlr.sw.getId()).andReturn(1L).anyTimes(); |
| 1164 | expect(chdlr.sw.getStringId()).andReturn("00:00:00:00:00:00:00:01") |
| 1165 | .anyTimes(); |
| 1166 | controller.activeSwitches.put(1L, chdlr.sw); |
| 1167 | expect(chdlr.sw.isActive()).andReturn(false); |
| 1168 | expect(chdlr.sw.isConnected()).andReturn(true); |
| 1169 | chdlr.sw.cancelAllStatisticsReplies(); |
| 1170 | chdlr.state.firstRoleReplyReceived = false; |
| 1171 | replay(chdlr.sw); |
| 1172 | chdlr.processOFMessage(msg); |
| 1173 | verify(chdlr.sw); |
| 1174 | assertTrue("state.firstRoleReplyReceived must be true", |
| 1175 | chdlr.state.firstRoleReplyReceived); |
| 1176 | assertTrue("activeSwitches must be empty", |
| 1177 | controller.activeSwitches.isEmpty()); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Tests that you can't remove a switch from the active |
| 1182 | * switch list. |
| 1183 | * @throws Exception |
| 1184 | */ |
| 1185 | @Test |
| 1186 | public void testRemoveActiveSwitch() { |
| 1187 | IOFSwitch sw = EasyMock.createNiceMock(IOFSwitch.class); |
| 1188 | boolean exceptionThrown = false; |
| 1189 | expect(sw.getId()).andReturn(1L).anyTimes(); |
| 1190 | replay(sw); |
| 1191 | getController().activeSwitches.put(sw.getId(), sw); |
| 1192 | try { |
| 1193 | getController().getSwitches().remove(1L); |
| 1194 | } catch (UnsupportedOperationException e) { |
| 1195 | exceptionThrown = true; |
| 1196 | } |
| 1197 | assertTrue(exceptionThrown); |
| 1198 | verify(sw); |
| 1199 | } |
| 1200 | |
| 1201 | public void verifyPortChangedUpdateInQueue(IOFSwitch sw) throws Exception { |
| 1202 | assertEquals(1, controller.updates.size()); |
| 1203 | IUpdate update = controller.updates.take(); |
| 1204 | assertEquals(true, update instanceof SwitchUpdate); |
| 1205 | SwitchUpdate swUpdate = (SwitchUpdate)update; |
| 1206 | assertEquals(sw, swUpdate.sw); |
| 1207 | assertEquals(SwitchUpdateType.PORTCHANGED, swUpdate.switchUpdateType); |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * Test handlePortStatus() |
| 1212 | * TODO: test correct updateStorage behavior! |
| 1213 | */ |
| 1214 | @Test |
| 1215 | public void testHandlePortStatus() throws Exception { |
| 1216 | IOFSwitch sw = createMock(IOFSwitch.class); |
| 1217 | OFPhysicalPort port = new OFPhysicalPort(); |
| 1218 | port.setName("myPortName1"); |
| 1219 | port.setPortNumber((short)42); |
| 1220 | |
| 1221 | OFPortStatus ofps = new OFPortStatus(); |
| 1222 | ofps.setDesc(port); |
| 1223 | |
| 1224 | ofps.setReason((byte)OFPortReason.OFPPR_ADD.ordinal()); |
| 1225 | sw.setPort(port); |
| 1226 | expectLastCall().once(); |
| 1227 | replay(sw); |
| 1228 | controller.handlePortStatusMessage(sw, ofps, false); |
| 1229 | verify(sw); |
| 1230 | verifyPortChangedUpdateInQueue(sw); |
| 1231 | reset(sw); |
| 1232 | |
| 1233 | ofps.setReason((byte)OFPortReason.OFPPR_MODIFY.ordinal()); |
| 1234 | sw.setPort(port); |
| 1235 | expectLastCall().once(); |
| 1236 | replay(sw); |
| 1237 | controller.handlePortStatusMessage(sw, ofps, false); |
| 1238 | verify(sw); |
| 1239 | verifyPortChangedUpdateInQueue(sw); |
| 1240 | reset(sw); |
| 1241 | |
| 1242 | ofps.setReason((byte)OFPortReason.OFPPR_DELETE.ordinal()); |
| 1243 | sw.deletePort(port.getPortNumber()); |
| 1244 | expectLastCall().once(); |
| 1245 | replay(sw); |
| 1246 | controller.handlePortStatusMessage(sw, ofps, false); |
| 1247 | verify(sw); |
| 1248 | verifyPortChangedUpdateInQueue(sw); |
| 1249 | reset(sw); |
| 1250 | } |
| 1251 | } |