/**
   * Send a hello message to the switch using the handshake transactions ids.
   *
   * @throws IOException
   */
  private void sendHelloMessage() throws IOException {
    // Send initial hello message

    OFHello.Builder builder = factory.buildHello();

    /* Our highest-configured OFVersion does support version bitmaps, so include it */
    if (factory.getVersion().compareTo(OFVersion.OF_13) >= 0) {
      List<OFHelloElem> he = new ArrayList<OFHelloElem>();
      he.add(factory.buildHelloElemVersionbitmap().setBitmaps(ofBitmaps).build());
      builder.setElements(he);
    }

    OFHello m = builder.setXid(handshakeTransactionIds--).build();

    write(m);
    log.debug("Send hello: {}", m);
  }
  @Test
  public void testWrite() {
    OFBsnGentableEntryDelete.Builder builder = factory.buildBsnGentableEntryDelete();
    builder
        .setXid(0x12345678)
        .setTableId(GenTableId.of(20))
        .setKey(
            ImmutableList.<OFBsnTlv>of(
                factory.bsnTlvs().port(OFPort.of(5)),
                factory.bsnTlvs().mac(MacAddress.of("01:23:45:67:89:ab"))));
    OFBsnGentableEntryDelete bsnGentableEntryDelete = builder.build();
    ByteBuf bb = Unpooled.buffer();
    bsnGentableEntryDelete.writeTo(bb);
    byte[] written = new byte[bb.readableBytes()];
    bb.readBytes(written);

    assertThat(written, CoreMatchers.equalTo(BSN_GENTABLE_ENTRY_DELETE_SERIALIZED));
  }
示例#3
0
 /**
  * Send a hello message to the switch using the handshake transactions ids.
  *
  * @throws IOException
  */
 private void sendHelloMessage() throws IOException {
   // Send initial hello message
   // FIXME:LOJI: Haven't negotiated version yet, assume 1.3
   OFHello.Builder builder = factory.buildHello().setXid(handshakeTransactionIds--);
   // FIXME: Need to add code here to set the version bitmap hello element
   OFHello m = builder.build();
   channel.write(Collections.singletonList(m));
   log.debug("Send hello: {}", m);
 }
示例#4
0
 private OFFeaturesReply createOFFeaturesReply(DatapathId datapathId) {
   OFFeaturesReply fr =
       factory
           .buildFeaturesReply()
           .setXid(0)
           .setDatapathId(datapathId)
           .setPorts(ImmutableList.<OFPortDesc>of())
           .build();
   return fr;
 }
 @Override
 public OFOxm<?> mapSelector(OFFactory factory, ExtensionSelector extensionSelector) {
   ExtensionSelectorType type = extensionSelector.type();
   if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
     VlanId vlanId = ((OfdpaMatchVlanVid) extensionSelector).vlanId();
     // Special VLAN 0x0000/0x1FFF required by OFDPA
     if (vlanId.equals(VlanId.NONE)) {
       OFVlanVidMatch vid = OFVlanVidMatch.ofRawVid((short) 0x0000);
       OFVlanVidMatch mask = OFVlanVidMatch.ofRawVid((short) 0x1FFF);
       return factory.oxms().vlanVidMasked(vid, mask);
       // Normal case
     } else if (vlanId.equals(VlanId.ANY)) {
       return factory.oxms().vlanVidMasked(OFVlanVidMatch.PRESENT, OFVlanVidMatch.PRESENT);
     } else {
       return factory.oxms().vlanVid(OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vlanId.toShort())));
     }
   }
   throw new UnsupportedOperationException(
       "Unexpected ExtensionSelector: " + extensionSelector.toString());
 }
  @Test
  public void testRead() throws Exception {
    OFBsnGentableEntryDelete.Builder builder = factory.buildBsnGentableEntryDelete();
    builder
        .setXid(0x12345678)
        .setTableId(GenTableId.of(20))
        .setKey(
            ImmutableList.<OFBsnTlv>of(
                factory.bsnTlvs().port(OFPort.of(5)),
                factory.bsnTlvs().mac(MacAddress.of("01:23:45:67:89:ab"))));
    OFBsnGentableEntryDelete bsnGentableEntryDeleteBuilt = builder.build();

    ByteBuf input = Unpooled.copiedBuffer(BSN_GENTABLE_ENTRY_DELETE_SERIALIZED);

    // FIXME should invoke the overall reader once implemented
    OFBsnGentableEntryDelete bsnGentableEntryDeleteRead =
        OFBsnGentableEntryDeleteVer13.READER.readFrom(input);
    assertEquals(BSN_GENTABLE_ENTRY_DELETE_SERIALIZED.length, input.readerIndex());

    assertEquals(bsnGentableEntryDeleteBuilt, bsnGentableEntryDeleteRead);
  }
 private void sendEchoReply(OFEchoRequest request) {
   OFEchoReply reply =
       factory.buildEchoReply().setXid(request.getXid()).setData(request.getData()).build();
   write(reply);
 }
 private void sendEchoRequest() {
   OFEchoRequest request = factory.buildEchoRequest().setXid(handshakeTransactionIds--).build();
   /* Record for latency calculation */
   echoSendTime = System.currentTimeMillis();
   write(request);
 }
 /**
  * Send a features request message to the switch using the handshake transactions ids.
  *
  * @throws IOException
  */
 private void sendFeaturesRequest() throws IOException {
   // Send initial Features Request
   OFFeaturesRequest m = factory.buildFeaturesRequest().setXid(handshakeTransactionIds--).build();
   write(m);
 }
示例#10
0
 private void sendEchoReply(OFEchoRequest request) {
   OFEchoReply reply =
       factory.buildEchoReply().setXid(request.getXid()).setData(request.getData()).build();
   channel.write(Collections.singletonList(reply));
 }
示例#11
0
 private void sendEchoRequest() {
   OFEchoRequest request = factory.buildEchoRequest().setXid(handshakeTransactionIds--).build();
   channel.write(Collections.singletonList(request));
 }
示例#12
0
 /**
  * Send a features request message to the switch using the handshake transactions ids.
  *
  * @throws IOException
  */
 private void sendFeaturesRequest() throws IOException {
   // Send initial Features Request
   OFFeaturesRequest m = factory.buildFeaturesRequest().setXid(handshakeTransactionIds--).build();
   channel.write(Collections.singletonList(m));
 }
示例#13
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);
  }
示例#14
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);
  }
示例#15
0
  public void doSetUp(HARole role) throws Exception {
    super.setUp();
    FloodlightModuleContext fmc = new FloodlightModuleContext();
    FloodlightProvider cm = new FloodlightProvider();

    fmc.addConfigParam(cm, "role", role.toString());
    controller = (Controller) cm.getServiceImpls().get(IFloodlightProviderService.class);
    fmc.addService(IFloodlightProviderService.class, controller);

    MemoryStorageSource memstorage = new MemoryStorageSource();
    fmc.addService(IStorageSourceService.class, memstorage);

    RestApiServer restApi = new RestApiServer();
    fmc.addService(IRestApiService.class, restApi);

    ThreadPool threadPool = new ThreadPool();
    fmc.addService(IThreadPoolService.class, threadPool);

    MockSwitchManager switchService = new MockSwitchManager();
    fmc.addService(IOFSwitchService.class, switchService);

    PktInProcessingTime ppt = new PktInProcessingTime();
    fmc.addService(IPktInProcessingTimeService.class, ppt);

    // TODO: should mock IDebugCounterService and make sure
    // the expected counters are updated.
    DebugCounterServiceImpl debugCounterService = new DebugCounterServiceImpl();
    fmc.addService(IDebugCounterService.class, debugCounterService);

    DebugEventService debugEventService = new DebugEventService();
    fmc.addService(IDebugEventService.class, debugEventService);

    IShutdownService shutdownService = createMock(IShutdownService.class);
    shutdownService.registerShutdownListener(anyObject(IShutdownListener.class));
    expectLastCall().anyTimes();
    replay(shutdownService);
    fmc.addService(IShutdownService.class, shutdownService);
    verify(shutdownService);

    tp = new MockThreadPoolService();
    fmc.addService(IThreadPoolService.class, tp);

    syncService = new MockSyncService();
    fmc.addService(ISyncService.class, syncService);

    ppt.init(fmc);
    restApi.init(fmc);
    threadPool.init(fmc);
    memstorage.init(fmc);
    tp.init(fmc);
    debugCounterService.init(fmc);
    debugEventService.init(fmc);
    syncService.init(fmc);
    cm.init(fmc);

    ppt.startUp(fmc);
    restApi.startUp(fmc);
    threadPool.startUp(fmc);
    memstorage.startUp(fmc);
    tp.startUp(fmc);
    debugCounterService.startUp(fmc);
    debugEventService.startUp(fmc);
    syncService.startUp(fmc);
    cm.startUp(fmc);

    testPacket =
        new Ethernet()
            .setSourceMACAddress("00:44:33:22:11:00")
            .setDestinationMACAddress("00:11:22:33:44:55")
            .setEtherType(EthType.ARP)
            .setPayload(
                new ARP()
                    .setHardwareType(ARP.HW_TYPE_ETHERNET)
                    .setProtocolType(ARP.PROTO_TYPE_IP)
                    .setHardwareAddressLength((byte) 6)
                    .setProtocolAddressLength((byte) 4)
                    .setOpCode(ARP.OP_REPLY)
                    .setSenderHardwareAddress(Ethernet.toMACAddress("00:44:33:22:11:00"))
                    .setSenderProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.1"))
                    .setTargetHardwareAddress(Ethernet.toMACAddress("00:11:22:33:44:55"))
                    .setTargetProtocolAddress(IPv4.toIPv4AddressBytes("192.168.1.2")));
    byte[] testPacketSerialized = testPacket.serialize();

    // The specific factory can be obtained from the switch, but we don't have one
    pi =
        (OFPacketIn)
            factory
                .buildPacketIn()
                .setBufferId(OFBufferId.NO_BUFFER)
                .setInPort(OFPort.of(1))
                .setData(testPacketSerialized)
                .setReason(OFPacketInReason.NO_MATCH)
                .setTotalLen(testPacketSerialized.length)
                .build();
  }