Introduced a new API to match on auxLocations in getConnectedHosts

Change-Id: I3df5493898bd389a5dfe631053f5ce51c076c106
This commit is contained in:
Charles Chan 2020-03-08 17:41:11 -07:00 committed by Charles Chan
parent 80cb854485
commit b51e8442d1
5 changed files with 81 additions and 2 deletions

View File

@ -88,6 +88,17 @@ public interface HostService
*/
Set<Host> getConnectedHosts(ConnectPoint connectPoint);
/**
* Returns the set of host that attach to the specified connect point.
*
* @param connectPoint connect point
* @param matchAuxLocations true to match on the auxLocations, false to match on locations of the hosts
* @return set of hosts connected to the connection point
*/
default Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
return getConnectedHosts(connectPoint);
}
/**
* Returns the set of hosts that attach to the specified device.
*

View File

@ -134,6 +134,17 @@ public interface HostStore extends Store<HostEvent, HostStoreDelegate> {
*/
Set<Host> getConnectedHosts(ConnectPoint connectPoint);
/**
* Returns the set of host that attach to the specified connect point.
*
* @param connectPoint connect point
* @param matchAuxLocations true to match on the auxLocations, false to match on locations of the hosts
* @return set of hosts connected to the connection point
*/
default Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
return getConnectedHosts(connectPoint);
}
/**
* Returns the set of hosts that attach to the specified device.
*

View File

@ -419,9 +419,14 @@ public class HostManager
@Override
public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
return getConnectedHosts(connectPoint, false);
}
@Override
public Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
checkPermission(HOST_READ);
checkNotNull(connectPoint, "Connection point cannot be null");
return store.getConnectedHosts(connectPoint);
return store.getConnectedHosts(connectPoint, matchAuxLocations);
}
@Override

View File

@ -350,8 +350,23 @@ public class DistributedHostStore
@Override
public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
return getConnectedHosts(connectPoint, false);
}
@Override
public Set<Host> getConnectedHosts(ConnectPoint connectPoint, boolean matchAuxLocations) {
Predicate<Map.Entry<HostId, DefaultHost>> predicate;
if (matchAuxLocations) {
predicate = entry -> {
Set<HostLocation> auxLocations = entry.getValue().auxLocations();
return auxLocations != null && entry.getValue().auxLocations().contains(connectPoint);
};
} else {
predicate = entry -> entry.getValue().locations().contains(connectPoint);
}
Set<Host> filtered = hosts.entrySet().stream()
.filter(entry -> entry.getValue().locations().contains(connectPoint))
.filter(predicate)
.map(Map.Entry::getValue)
.collect(Collectors.toSet());
return ImmutableSet.copyOf(filtered);

View File

@ -100,6 +100,30 @@ public class DistributedHostStoreTest {
HOST_ADDRESS,
HOST_LEARNT_WITH_ADDRESSES.configured(),
HOST_LEARNT_WITH_ADDRESSES.annotations());
private static final HostDescription HOST_DESC_WITHOUT_AUX =
new DefaultHostDescription(HOSTID.mac(), HOSTID.vlanId(),
Sets.newHashSet(HOST_LOC11), null,
Sets.newHashSet(IP1, IP2), VlanId.NONE, EthType.EtherType.UNKNOWN.ethType(),
false);
private static final HostDescription HOST_DESC_WITH_AUX =
new DefaultHostDescription(HOSTID1.mac(), HOSTID1.vlanId(),
Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_LOC12),
Sets.newHashSet(IP1, IP2), VlanId.NONE, EthType.EtherType.UNKNOWN.ethType(),
false);
private static final Host HOST_WITHOUT_AUX =
new DefaultHost(PID, HOSTID,
HOST_DESC_WITHOUT_AUX.hwAddress(), HOST_DESC_WITHOUT_AUX.vlan(),
HOST_DESC_WITHOUT_AUX.locations(), HOST_DESC_WITHOUT_AUX.auxLocations(),
HOST_DESC_WITHOUT_AUX.ipAddress(), HOST_DESC_WITHOUT_AUX.innerVlan(), HOST_DESC_WITHOUT_AUX.tpid(),
HOST_DESC_WITHOUT_AUX.configured(), false);
private static final Host HOST_WITH_AUX =
new DefaultHost(PID, HOSTID1,
HOST_DESC_WITH_AUX.hwAddress(), HOST_DESC_WITH_AUX.vlan(),
HOST_DESC_WITH_AUX.locations(), HOST_DESC_WITH_AUX.auxLocations(),
HOST_DESC_WITH_AUX.ipAddress(), HOST_DESC_WITH_AUX.innerVlan(), HOST_DESC_WITH_AUX.tpid(),
HOST_DESC_WITH_AUX.configured(), false);
private static final MapEvent<HostId, DefaultHost> HOST_EVENT =
new MapEvent<>("foobar", HOSTID, new Versioned<>(NEW_HOST, 0), new Versioned<>(OLD_HOST, 0));
private static final DefaultHost HOST1 = new DefaultHost(PID, HOSTID, HOSTID.mac(), HOSTID.vlanId(),
@ -383,6 +407,19 @@ public class DistributedHostStoreTest {
assertNull(delegate.lastEvent.prevSubject());
}
@Test
public void testGetConnectedHost() {
ecXHostStore.createOrUpdateHost(PID, HOSTID, HOST_DESC_WITHOUT_AUX, false);
ecXHostStore.createOrUpdateHost(PID, HOSTID1, HOST_DESC_WITH_AUX, false);
assertEquals(Sets.newHashSet(HOST_WITHOUT_AUX, HOST_WITH_AUX),
ecXHostStore.getConnectedHosts(HOST_LOC11, false));
assertEquals(Sets.newHashSet(),
ecXHostStore.getConnectedHosts(HOST_LOC11, true));
assertEquals(Sets.newHashSet(HOST_WITH_AUX),
ecXHostStore.getConnectedHosts(HOST_LOC12, true));
}
private class TestStoreDelegate implements HostStoreDelegate {
public HostEvent lastEvent;