@Test public void testHandleMessagesNoListeners() throws Exception { IOFSwitch sw = createMock(IOFSwitch.class); expect(sw.getId()).andReturn(DatapathId.NONE).anyTimes(); replay(sw); controller.handleMessage(sw, pi, null); verify(sw); }
@Test public void testHandleMessagesSlave() throws Exception { doSetUp(HARole.STANDBY); IOFSwitch sw = createMock(IOFSwitch.class); expect(sw.getId()).andReturn(DatapathId.NONE).anyTimes(); IOFMessageListener test1 = createMock(IOFMessageListener.class); expect(test1.getName()).andReturn("test1").atLeastOnce(); expect(test1.isCallbackOrderingPrereq((OFType) anyObject(), (String) anyObject())) .andReturn(false) .atLeastOnce(); expect(test1.isCallbackOrderingPostreq((OFType) anyObject(), (String) anyObject())) .andReturn(false) .atLeastOnce(); replay(test1, sw); controller.addOFMessageListener(OFType.PACKET_IN, test1); // message should not be dispatched controller.handleMessage(sw, pi, null); verify(test1); // --------------------------------- // transition to Master // -------------------------------- controller.setRole(HARole.ACTIVE, "FooBar"); // transitioned but HA listeneres not yet notified. // message should not be dispatched reset(test1); replay(test1); controller.handleMessage(sw, pi, null); verify(test1); // notify HA listeners controller.processUpdateQueueForTesting(); // no message should be dispatched reset(test1); expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.STOP); replay(test1); controller.handleMessage(sw, pi, null); verify(test1); verify(sw); }
@Test public void testHandleMessageWithContext() throws Exception { IOFSwitch sw = createMock(IOFSwitch.class); expect(sw.getId()).andReturn(DatapathId.NONE).anyTimes(); IOFMessageListener test1 = createMock(IOFMessageListener.class); expect(test1.getName()).andReturn("test1").anyTimes(); expect(test1.isCallbackOrderingPrereq((OFType) anyObject(), (String) anyObject())) .andReturn(false) .anyTimes(); expect(test1.isCallbackOrderingPostreq((OFType) anyObject(), (String) anyObject())) .andReturn(false) .anyTimes(); FloodlightContext cntx = new FloodlightContext(); expect(test1.receive(same(sw), same(pi), same(cntx))).andReturn(Command.CONTINUE); IOFMessageListener test2 = createMock(IOFMessageListener.class); expect(test2.getName()).andReturn("test2").anyTimes(); expect(test2.isCallbackOrderingPrereq((OFType) anyObject(), (String) anyObject())) .andReturn(false) .anyTimes(); expect(test2.isCallbackOrderingPostreq((OFType) anyObject(), (String) anyObject())) .andReturn(false) .anyTimes(); // test2 will not receive any message! replay(test1, test2, sw); controller.addOFMessageListener(OFType.PACKET_IN, test1); controller.addOFMessageListener(OFType.ERROR, test2); controller.handleMessage(sw, pi, cntx); verify(test1, test2, sw); Ethernet eth = IFloodlightProviderService.bcStore.get(cntx, IFloodlightProviderService.CONTEXT_PI_PAYLOAD); assertArrayEquals(testPacket.serialize(), eth.serialize()); }
/** * Test message dispatching to OFMessageListeners. Test ordering of listeners for different types * (we do this implicitly by using STOP and CONTINUE and making sure the processing stops at the * right place) Verify that a listener that throws an exception halts further execution, and * verify that the Commands STOP and CONTINUE are honored. * * @throws Exception */ @Test public void testHandleMessages() throws Exception { controller.removeOFMessageListeners(OFType.PACKET_IN); IOFSwitch sw = createMock(IOFSwitch.class); expect(sw.getId()).andReturn(DatapathId.NONE).anyTimes(); // Setup listener orderings IOFMessageListener test1 = createMock(IOFMessageListener.class); expect(test1.getName()).andReturn("test1").anyTimes(); setupListenerOrdering(test1); IOFMessageListener test2 = createMock(IOFMessageListener.class); expect(test2.getName()).andReturn("test2").anyTimes(); // using a postreq and a prereq ordering here expect(test2.isCallbackOrderingPrereq(OFType.PACKET_IN, "test1")).andReturn(true).atLeastOnce(); expect(test2.isCallbackOrderingPostreq(OFType.FLOW_MOD, "test1")).andReturn(true).atLeastOnce(); setupListenerOrdering(test2); IOFMessageListener test3 = createMock(IOFMessageListener.class); expect(test3.getName()).andReturn("test3").anyTimes(); expect(test3.isCallbackOrderingPrereq((OFType) anyObject(), eq("test1"))) .andReturn(true) .atLeastOnce(); expect(test3.isCallbackOrderingPrereq((OFType) anyObject(), eq("test2"))) .andReturn(true) .atLeastOnce(); setupListenerOrdering(test3); // Ordering: PacketIn: test1 -> test2 -> test3 // FlowMod: test2 -> test1 replay(test1, test2, test3); controller.addOFMessageListener(OFType.PACKET_IN, test1); controller.addOFMessageListener(OFType.PACKET_IN, test3); controller.addOFMessageListener(OFType.PACKET_IN, test2); controller.addOFMessageListener(OFType.FLOW_MOD, test1); controller.addOFMessageListener(OFType.FLOW_MOD, test2); verify(test1); verify(test2); verify(test3); replay(sw); // ------------------ // Test PacketIn handling: all listeners return CONTINUE reset(test1, test2, test3); expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.CONTINUE); expect(test2.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.CONTINUE); expect(test3.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.CONTINUE); replay(test1, test2, test3); controller.handleMessage(sw, pi, null); verify(test1); verify(test2); verify(test3); // ------------------ // Test PacketIn handling: with a thrown exception. reset(test1, test2, test3); expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.CONTINUE); expect(test2.receive(eq(sw), eq(pi), isA(FloodlightContext.class))) .andThrow( new RuntimeException("This is NOT an error! We " + "are testing exception catching.")); // expect no calls to test3.receive() since test2.receive throws // an exception replay(test1, test2, test3); try { controller.handleMessage(sw, pi, null); fail("Expected exception was not thrown!"); } catch (RuntimeException e) { assertTrue( "The caught exception was not the expected one", e.getMessage().startsWith("This is NOT an error!")); } verify(test1); verify(test2); verify(test3); // ------------------ // Test PacketIn handling: test1 return Command.STOP reset(test1, test2, test3); expect(test1.receive(eq(sw), eq(pi), isA(FloodlightContext.class))).andReturn(Command.STOP); // expect no calls to test3.receive() and test2.receive since // test1.receive returns STOP replay(test1, test2, test3); controller.handleMessage(sw, pi, null); verify(test1); verify(test2); verify(test3); OFFlowMod fm = (OFFlowMod) factory.buildFlowModify().build(); // ------------------ // Test FlowMod handling: all listeners return CONTINUE reset(test1, test2, test3); expect(test1.receive(eq(sw), eq(fm), isA(FloodlightContext.class))).andReturn(Command.CONTINUE); expect(test2.receive(eq(sw), eq(fm), isA(FloodlightContext.class))).andReturn(Command.CONTINUE); // test3 is not a listener for FlowMod replay(test1, test2, test3); controller.handleMessage(sw, fm, null); verify(test1); verify(test2); verify(test3); // ------------------ // Test FlowMod handling: test2 (first listener) return STOP reset(test1, test2, test3); expect(test2.receive(eq(sw), eq(fm), isA(FloodlightContext.class))).andReturn(Command.STOP); // test2 will not be called // test3 is not a listener for FlowMod replay(test1, test2, test3); controller.handleMessage(sw, fm, null); verify(test1); verify(test2); verify(test3); verify(sw); }