示例#1
0
 @Test
 public void testGetRoleInfoDefault() {
   RoleInfo info = controller.getRoleInfo();
   assertEquals(HARole.ACTIVE, info.getRole());
   assertNotNull(info.getRoleChangeDescription());
   assertEquals(HARole.ACTIVE, controller.getRole());
   // FIXME: RoleInfo's date. but the format is kinda broken
 }
示例#2
0
 @Test
 public void testSetRoleNPE() {
   try {
     controller.setRole(null, "");
     fail("Should have thrown an Exception");
   } catch (NullPointerException e) {
     // exptected
   }
   try {
     controller.setRole(HARole.ACTIVE, null);
     fail("Should have thrown an Exception");
   } catch (NullPointerException e) {
     // exptected
   }
 }
示例#3
0
 @After
 public void tearDown() {
   tp.getScheduledExecutor().shutdownNow();
   // Make sure thare are not left over updates in the queue
   assertTrue(
       "Updates left in controller update queue", controller.isUpdateQueueEmptyForTesting());
 }
示例#4
0
  /** Test interaction with OFChannelHandler when the current role is master. */
  @Test
  public void testChannelHandlerMaster() {
    OFSwitchHandshakeHandler h = createMock(OFSwitchHandshakeHandler.class);

    // Reassert the role.
    reset(h);
    h.sendRoleRequestIfNotPending(OFControllerRole.ROLE_MASTER);
    replay(h);
    controller.reassertRole(h, HARole.ACTIVE);
    verify(h);

    // reassert a different role: no-op
    reset(h);
    replay(h);
    controller.reassertRole(h, HARole.STANDBY);
    verify(h);
  }
示例#5
0
 @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);
 }
示例#6
0
  /** Test other setRole cases: re-setting role to the current role, setting role to equal, etc. */
  @Test
  public void testSetRoleOthercases() throws Exception {
    doSetUp(HARole.STANDBY);

    // Create and add the HA listener
    IHAListener listener = createMock(IHAListener.class);
    expect(listener.getName()).andReturn("foo").anyTimes();
    setupListenerOrdering(listener);
    replay(listener);
    controller.addHAListener(listener);

    // Set role to slave again. Nothing should happen
    controller.setRole(HARole.STANDBY, "FooBar");
    controller.processUpdateQueueForTesting();
    verify(listener);

    reset(listener);
    expect(listener.getName()).andReturn("foo").anyTimes();
    listener.transitionToActive();
    expectLastCall().once();
    replay(listener);
  }
示例#7
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());
  }
示例#8
0
  /**
   * Start as SLAVE then set role to MASTER Tests normal role change transition. Check that
   * connected channels receive a setRole request
   */
  @Test
  public void testSetRole() throws Exception {
    doSetUp(HARole.STANDBY);
    RoleInfo info = controller.getRoleInfo();
    assertEquals(HARole.STANDBY, info.getRole());
    assertEquals(HARole.STANDBY, controller.getRole());

    OFSwitchHandshakeHandler h = createMock(OFSwitchHandshakeHandler.class);

    // Reassert the role.
    reset(h);
    h.sendRoleRequestIfNotPending(OFControllerRole.ROLE_SLAVE);
    replay(h);
    controller.reassertRole(h, HARole.STANDBY);
    verify(h);

    // reassert a different role: no-op
    reset(h);
    replay(h);
    controller.reassertRole(h, HARole.ACTIVE);
    verify(h);

    IHAListener listener = createMock(IHAListener.class);
    expect(listener.getName()).andReturn("foo").anyTimes();
    setupListenerOrdering(listener);
    listener.transitionToActive();
    expectLastCall().once();
    replay(listener);
    controller.addHAListener(listener);
    controller.setRole(HARole.ACTIVE, "FooBar");
    controller.processUpdateQueueForTesting();
    verify(listener);
    info = controller.getRoleInfo();
    assertEquals(HARole.ACTIVE, info.getRole());
    assertEquals("FooBar", info.getRoleChangeDescription());
    assertEquals(HARole.ACTIVE, controller.getRole());
  }
示例#9
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);
  }
示例#10
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);
  }
示例#11
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);
  }