Java Code Examples for org.onosproject.net.host.HostService#getHostsByMac()

The following examples show how to use org.onosproject.net.host.HostService#getHostsByMac() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VplsNeighbourHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Handles reply messages between VLAN tagged interfaces.
 *
 * @param context the message context
 * @param hostService the host service
 */
protected void handleReply(NeighbourMessageContext context,
                           HostService hostService) {
    // Find target VPLS, then reply to the host
    VplsData vplsData = findVpls(context);
    if (vplsData != null) {
        MacAddress dstMac = context.dstMac();
        Set<Host> hosts = hostService.getHostsByMac(dstMac);
        hosts = hosts.stream()
                .filter(host -> vplsData.interfaces().contains(getHostInterface(host)))
                .collect(Collectors.toSet());

        // reply to all host in same VPLS
        hosts.stream()
                .map(this::getHostInterface)
                .filter(Objects::nonNull)
                .forEach(context::forward);
    } else {
        // this might be happened when we remove an interface from VPLS
        // just ignore this message
        log.warn(CAN_NOT_FIND_VPLS, context.inPort(), context.vlan());
        context.drop();
    }
}
 
Example 2
Source File: BgpSpeakerNeighbourHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void handleMessage(NeighbourMessageContext context, HostService hostService) {
    switch (context.type()) {
    case REQUEST:
        // Reply to requests that target our configured interface IP
        // address on this port. Drop all other requests.
        interfaceService.getInterfacesByPort(context.inPort())
                .stream()
                .filter(intf -> intf.ipAddressesList()
                        .stream()
                        .anyMatch(ia -> ia.ipAddress().equals(context.target()) &&
                                ia.subnetAddress().contains(context.sender())))
                .forEach(intf -> context.reply(intf.mac()));

        break;
    case REPLY:
        // Proxy replies over to our internal BGP speaker if the host
        // is known to us
        Set<Host> hosts = hostService.getHostsByMac(context.dstMac());

        if (hosts.isEmpty()) {
            context.drop();
        } else {
            Host h = hosts.stream().findFirst().get();
            VlanId bgpSpeakerVlanId = h.vlan();

            if (!bgpSpeakerVlanId.equals(VlanId.NONE)) {
                context.packet().setVlanID(bgpSpeakerVlanId.toShort());
            } else {
                context.packet().setVlanID(Ethernet.VLAN_UNTAGGED);
            }
            context.forward(h.location());
        }
        break;
    default:
        break;
    }
}
 
Example 3
Source File: VirtualNetworkHostManagerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
 * on an empty virtual network.
 */
@Test
public void testGetHostsOnEmptyVnet() {
    VirtualNetwork virtualNetwork = setupEmptyVnet();
    HostService hostService = manager.get(virtualNetwork.id(), HostService.class);

    // test the getHosts() and getHostCount() methods
    Iterator<Host> itHosts = hostService.getHosts().iterator();
    assertEquals("The host set size did not match.", 0, Iterators.size(itHosts));
    assertEquals("The host count did not match.", 0, hostService.getHostCount());

    // test the getHost() method
    Host testHost = hostService.getHost(HID2);
    assertNull("The host should be null.", testHost);

    // test the getHostsByVlan(...) method
    Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
    assertEquals("The host set size did not match.", 0, collHost.size());

    // test the getHostsByMac(...) method
    collHost = hostService.getHostsByMac(MAC2);
    assertEquals("The host set size did not match.", 0, collHost.size());

    // test the getHostsByIp(...) method
    collHost = hostService.getHostsByIp(IP1);
    assertEquals("The host set size did not match.", 0, collHost.size());

    // test the getConnectedHosts(ConnectPoint) method
    collHost = hostService.getConnectedHosts(LOC1);
    assertEquals("The host set size did not match.", 0, collHost.size());

    // test the getConnectedHosts(DeviceId) method
    collHost = hostService.getConnectedHosts(DID2);
    assertEquals("The host set size did not match.", 0, collHost.size());
}
 
Example 4
Source File: VirtualNetworkHostManagerTest.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Tests querying for hosts with null mac.
 */
@Test(expected = NullPointerException.class)
public void testGetHostsByNullMac() {
    VirtualNetwork vnet = setupEmptyVnet();
    HostService hostService = manager.get(vnet.id(), HostService.class);

    hostService.getHostsByMac(null);
}
 
Example 5
Source File: SimpleFabricNeighbour.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Handles reply messages between VLAN tagged interfaces.
 *
 * @param context the message context
 * @param hostService the host service
 */
protected void handleReply(NeighbourMessageContext context,
                           HostService hostService) {
    // Find target L2 Network, then reply to the host
    FabricNetwork fabricNetwork = simpleFabric.fabricNetwork(context.inPort(), context.vlan());
    if (fabricNetwork != null) {
        // TODO: need to check and update simpleFabric.DefaultFabricNetwork
        MacAddress mac = simpleFabric.vMacForIp(context.target());
        if (mac != null) {
            log.trace("simple fabric neightbour response message to virtual gateway; drop: {} {} target={}",
                      context.inPort(), context.vlan(), context.target());
            context.drop();
        } else {
            // forward reply to the hosts of the dstMac
            Set<Host> hosts = hostService.getHostsByMac(context.dstMac());
            log.trace("simple fabric neightbour response message forward: {} {} target={} -> {}",
                      context.inPort(), context.vlan(), context.target(), hosts);
            hosts.stream()
                    .map(host -> simpleFabric.hostInterface(host))
                    .filter(Objects::nonNull)
                    .forEach(context::forward);
        }
    } else {
        // this might be happened when we remove an interface from L2 Network
        // just ignore this message
        log.warn("simple fabric neightbour response message drop for unknown fabricNetwork: {} {}",
                 context.inPort(), context.vlan());
        context.drop();
    }
}
 
Example 6
Source File: VirtualNetworkHostManagerTest.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
 * on a non-empty virtual network.
 */
@Test
public void testGetHostsOnNonEmptyVnet() {
    VirtualNetwork virtualNetwork = setupEmptyVnet();
    VirtualHost vhost1 = manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
    VirtualHost vhost2 = manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
    HostService hostService = manager.get(virtualNetwork.id(), HostService.class);

    // test the getHosts() and getHostCount() methods
    Iterator<Host> itHosts = hostService.getHosts().iterator();
    assertEquals("The host set size did not match.", 2, Iterators.size(itHosts));
    assertEquals("The host count did not match.", 2, hostService.getHostCount());

    // test the getHost() method
    Host testHost = hostService.getHost(HID2);
    assertEquals("The expected host did not match.", vhost2, testHost);

    // test the getHostsByVlan(...) method
    Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
    assertEquals("The host set size did not match.", 1, collHost.size());
    assertTrue("The host did not match.", collHost.contains(vhost1));

    // test the getHostsByMac(...) method
    collHost = hostService.getHostsByMac(MAC2);
    assertEquals("The host set size did not match.", 1, collHost.size());
    assertTrue("The host did not match.", collHost.contains(vhost2));

    // test the getHostsByIp(...) method
    collHost = hostService.getHostsByIp(IP1);
    assertEquals("The host set size did not match.", 2, collHost.size());
    collHost = hostService.getHostsByIp(IP2);
    assertEquals("The host set size did not match.", 1, collHost.size());
    assertTrue("The host did not match.", collHost.contains(vhost1));

    // test the getConnectedHosts(ConnectPoint) method
    collHost = hostService.getConnectedHosts(LOC1);
    assertEquals("The host set size did not match.", 1, collHost.size());
    assertTrue("The host did not match.", collHost.contains(vhost1));

    // test the getConnectedHosts(DeviceId) method
    collHost = hostService.getConnectedHosts(DID2);
    assertEquals("The host set size did not match.", 1, collHost.size());
    assertTrue("The host did not match.", collHost.contains(vhost2));
}