Example #1
0
  @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);
  }
Example #2
0
  @Test
  public void testFloodNoBufferId() throws Exception {
    // build our expected flooded packetOut
    OFPacketOut po =
        ((OFPacketOut) mockFloodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT))
            .setActions(
                Arrays.asList(
                    new OFAction[] {new OFActionOutput().setPort(OFPort.OFPP_FLOOD.getValue())}))
            .setActionsLength((short) OFActionOutput.MINIMUM_LENGTH)
            .setBufferId(-1)
            .setInPort((short) 1)
            .setPacketData(this.testPacketSerialized);
    po.setLengthU(
        OFPacketOut.MINIMUM_LENGTH + po.getActionsLengthU() + this.testPacketSerialized.length);

    // Mock up our expected behavior
    IOFSwitch mockSwitch = createMock(IOFSwitch.class);

    Capture<OFMessage> wc1 = new Capture<OFMessage>(CaptureType.ALL);
    Capture<FloodlightContext> bc1 = new Capture<FloodlightContext>(CaptureType.ALL);

    mockSwitch.write(capture(wc1), capture(bc1));

    // Start recording the replay on the mocks
    replay(mockSwitch);
    // Get the listener and trigger the packet in
    IOFMessageListener listener =
        mockFloodlightProvider.getListeners().get(OFType.PACKET_IN).get(0);
    listener.receive(mockSwitch, this.packetIn, parseAndAnnotate(this.packetIn));

    // Verify the replay matched our expectations
    verify(mockSwitch);

    assertTrue(wc1.hasCaptured());
    OFMessage m = wc1.getValue();
    assertEquals(po, m);
  }
Example #3
0
  @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());
  }
Example #4
0
  /**
   * Test handleOutgoingMessage and also test listener ordering
   *
   * @throws Exception
   */
  @Test
  public void testHandleOutgoingMessage() throws Exception {
    OFMessage m = factory.buildEchoRequest().build();
    IOFSwitchBackend sw = createMock(IOFSwitchBackend.class);
    expect(sw.getId()).andReturn(DATAPATH_ID_0).anyTimes();

    // Add listeners
    IOFMessageListener test1 = createMock(IOFMessageListener.class);
    expect(test1.getName()).andReturn("test1").anyTimes();
    setupListenerOrdering(test1);

    IOFMessageListener test2 = createMock(IOFMessageListener.class);
    expect(test2.getName()).andReturn("test2").anyTimes();
    test2.isCallbackOrderingPostreq(OFType.ECHO_REQUEST, "test1");
    expectLastCall().andReturn(true).atLeastOnce();
    setupListenerOrdering(test2);

    IOFMessageListener test3 = createMock(IOFMessageListener.class);
    expect(test3.getName()).andReturn("test3").anyTimes();
    test3.isCallbackOrderingPostreq(OFType.ECHO_REQUEST, "test2");
    expectLastCall().andReturn(true).atLeastOnce();
    setupListenerOrdering(test3);

    // expected ordering is test3, test2, test1

    replay(test1, test2, test3);
    controller.addOFMessageListener(OFType.ECHO_REQUEST, test1);
    controller.addOFMessageListener(OFType.ECHO_REQUEST, test3);
    controller.addOFMessageListener(OFType.ECHO_REQUEST, test2);
    verify(test1);
    verify(test2);
    verify(test3);

    // Test inject with null switch and no message. Should not work.
    reset(test1, test2, test3);
    replay(test1, test2, test3, sw);
    try {
      controller.handleOutgoingMessage(null, pi);
      fail("handleOutgoindMessage should have thrown a NPE");
    } catch (NullPointerException e) {
      // expected
    }
    try {
      controller.handleOutgoingMessage(sw, null);
      fail("handleOutgoingMessage should have thrown a NPE");
    } catch (NullPointerException e) {
      // expected
    }
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);

    // Test the handleOutgoingMessage
    reset(test1, test2, test3, sw);
    expect(sw.getId()).andReturn(DATAPATH_ID_0).anyTimes();
    expect(test2.receive(same(sw), same(m), isA(FloodlightContext.class))).andReturn(Command.STOP);
    expect(test3.receive(same(sw), same(m), isA(FloodlightContext.class)))
        .andReturn(Command.CONTINUE);
    // test1 will not receive any message!
    replay(test1, test2, test3, sw);
    controller.handleOutgoingMessage(sw, m);
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);

    // Test the handleOutgoingMessage with null context
    reset(test1, test2, test3, sw);
    expect(sw.getId()).andReturn(DATAPATH_ID_0).anyTimes();
    expect(test2.receive(same(sw), same(m), isA(FloodlightContext.class))).andReturn(Command.STOP);
    expect(test3.receive(same(sw), same(m), isA(FloodlightContext.class)))
        .andReturn(Command.CONTINUE);
    // test1 will not receive any message!
    replay(test1, test2, test3, sw);
    controller.handleOutgoingMessage(sw, m);
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);

    // Test for message without listeners
    reset(test1, test2, test3, sw);
    replay(test1, test2, test3, sw);
    m = factory.buildEchoReply().build();
    controller.handleOutgoingMessage(sw, m);
    verify(test1);
    verify(test2);
    verify(test3);
    verify(sw);
  }
Example #5
0
  /**
   * 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);
  }