sdn
Committed by Gerrit Code Review

Fix for ONOS-5033 hosts - dynamic or static

Change-Id: I3791370db0037968003abc23c918c63119d2dba2
......@@ -38,7 +38,7 @@ import static com.google.common.collect.Lists.newArrayList;
public class HostsListCommand extends AbstractShellCommand {
private static final String FMT =
"id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s";
"id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s, configured=%s";
private static final String FMT_SHORT =
"id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
......@@ -93,7 +93,9 @@ public class HostsListCommand extends AbstractShellCommand {
} else {
print(FMT, host.id(), host.mac(),
host.location().deviceId(), host.location().port(),
host.vlan(), host.ipAddresses(), annotations(host.annotations()));
host.vlan(), host.ipAddresses(), annotations(host.annotations()),
host.configured());
}
}
}
......
......@@ -36,6 +36,7 @@ public class DefaultHost extends AbstractElement implements Host {
private final VlanId vlan;
private final HostLocation location;
private final Set<IpAddress> ips;
private final boolean configured;
/**
* Creates an end-station host using the supplied information.
......@@ -51,11 +52,30 @@ public class DefaultHost extends AbstractElement implements Host {
public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
VlanId vlan, HostLocation location, Set<IpAddress> ips,
Annotations... annotations) {
this(providerId, id, mac, vlan, location, ips, false, annotations);
}
/**
* Creates an end-station host using the supplied information.
*
* @param providerId provider identity
* @param id host identifier
* @param mac host MAC address
* @param vlan host VLAN identifier
* @param location host location
* @param ips host IP addresses
* @param configured true if configured via NetworkConfiguration
* @param annotations optional key/value annotations
*/
public DefaultHost(ProviderId providerId, HostId id, MacAddress mac,
VlanId vlan, HostLocation location, Set<IpAddress> ips,
boolean configured, Annotations... annotations) {
super(providerId, id, annotations);
this.mac = mac;
this.vlan = vlan;
this.location = location;
this.ips = new HashSet<>(ips);
this.configured = configured;
}
@Override
......@@ -84,6 +104,11 @@ public class DefaultHost extends AbstractElement implements Host {
}
@Override
public boolean configured() {
return configured;
}
@Override
public int hashCode() {
return Objects.hash(id, mac, vlan, location);
}
......@@ -114,6 +139,7 @@ public class DefaultHost extends AbstractElement implements Host {
.add("location", location())
.add("ipAddresses", ipAddresses())
.add("annotations", annotations())
.add("configured", configured())
.toString();
}
......
......@@ -63,6 +63,14 @@ public interface Host extends Element {
*/
HostLocation location();
/**
* Returns true if configured by NetworkConfiguration.
* @return configured/learnt dynamically
*/
default boolean configured() {
return false;
}
// TODO: explore capturing list of recent locations to aid in mobility
}
......
......@@ -40,6 +40,7 @@ public class DefaultHostDescription extends AbstractDescription
private final VlanId vlan;
private final HostLocation location;
private final Set<IpAddress> ip;
private final boolean configured;
/**
* Creates a host description using the supplied information.
......@@ -83,11 +84,46 @@ public class DefaultHostDescription extends AbstractDescription
public DefaultHostDescription(MacAddress mac, VlanId vlan,
HostLocation location, Set<IpAddress> ip,
SparseAnnotations... annotations) {
this(mac, vlan, location, ip, false, annotations);
}
/**
* Creates a host description using the supplied information.
*
* @param mac host MAC address
* @param vlan host VLAN identifier
* @param location host location
* @param configured true if configured via NetworkConfiguration
* @param annotations optional key/value annotations map
*/
public DefaultHostDescription(MacAddress mac, VlanId vlan,
HostLocation location,
boolean configured,
SparseAnnotations... annotations) {
this(mac, vlan, location, Collections.<IpAddress>emptySet(),
configured, annotations);
}
/**
* Creates a host description using the supplied information.
*
* @param mac host MAC address
* @param vlan host VLAN identifier
* @param location host location
* @param ip host IP address
* @param configured true if configured via NetworkConfiguration
* @param annotations optional key/value annotations map
*/
public DefaultHostDescription(MacAddress mac, VlanId vlan,
HostLocation location, Set<IpAddress> ip,
boolean configured,
SparseAnnotations... annotations) {
super(annotations);
this.mac = mac;
this.vlan = vlan;
this.location = location;
this.ip = ImmutableSet.copyOf(ip);
this.configured = configured;
}
@Override
......@@ -111,12 +147,18 @@ public class DefaultHostDescription extends AbstractDescription
}
@Override
public boolean configured() {
return configured;
}
@Override
public String toString() {
return toStringHelper(this)
.add("mac", mac)
.add("vlan", vlan)
.add("location", location)
.add("ipAddress", ip)
.add("configured", configured)
.toString();
}
......@@ -139,5 +181,4 @@ public class DefaultHostDescription extends AbstractDescription
}
return false;
}
}
......
......@@ -55,4 +55,12 @@ public interface HostDescription extends Description {
* @return host IP address
*/
Set<IpAddress> ipAddress();
/**
* Returns true if configured by NetworkConfiguration.
* @return configured/learnt dynamically
*/
default boolean configured() {
return false;
}
}
......
......@@ -39,7 +39,8 @@ public final class HostCodec extends AnnotatedCodec<Host> {
final ObjectNode result = context.mapper().createObjectNode()
.put("id", host.id().toString())
.put("mac", host.mac().toString())
.put("vlan", host.vlan().toString());
.put("vlan", host.vlan().toString())
.put("configured", host.configured());
final ArrayNode jsonIpAddresses = result.putArray("ipAddresses");
for (final IpAddress ipAddress : host.ipAddresses()) {
......
......@@ -64,7 +64,7 @@ public final class BasicHostOperator implements ConfigOperator {
SparseAnnotations sa = combine(cfg, descr.annotations());
return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
location, ipAddresses, sa);
location, ipAddresses, descr.configured(), sa);
}
/**
......
......@@ -167,11 +167,14 @@ public class DistributedHostStore
}
final Annotations annotations;
final boolean configured;
if (existingHost != null) {
annotations = merge((DefaultAnnotations) existingHost.annotations(),
hostDescription.annotations());
configured = existingHost.configured();
} else {
annotations = hostDescription.annotations();
configured = hostDescription.configured();
}
return new DefaultHost(providerId,
......@@ -180,6 +183,7 @@ public class DistributedHostStore
hostDescription.vlan(),
location,
addresses,
configured,
annotations);
});
return null;
......
......@@ -112,8 +112,8 @@ public class NetworkConfigHostProvider extends AbstractProvider implements HostP
protected void addHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
HostId hid = HostId.hostId(mac, vlan);
HostDescription desc = (ips != null) ?
new DefaultHostDescription(mac, vlan, hloc, ips) :
new DefaultHostDescription(mac, vlan, hloc);
new DefaultHostDescription(mac, vlan, hloc, ips, true) :
new DefaultHostDescription(mac, vlan, hloc, true);
providerService.hostDetected(hid, desc, false);
}
......@@ -128,7 +128,7 @@ public class NetworkConfigHostProvider extends AbstractProvider implements HostP
*/
protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
HostId hid = HostId.hostId(mac, vlan);
HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips, true);
providerService.hostDetected(hid, desc, true);
}
......
......@@ -214,7 +214,7 @@ public class HostsWebResource extends AbstractWebResource {
// Update host inventory
HostId hostId = HostId.hostId(mac, vlanId);
DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, annotations);
DefaultHostDescription desc = new DefaultHostDescription(mac, vlanId, hostLocation, ips, true, annotations);
hostProviderService.hostDetected(hostId, desc, false);
return hostId;
}
......
......@@ -198,7 +198,7 @@ public class HostResourceTest extends ResourceTest {
@Override
public boolean matchesSafely(JsonArray json) {
boolean hostFound = false;
final int expectedAttributes = 5;
final int expectedAttributes = 6;
for (int jsonHostIndex = 0; jsonHostIndex < json.size();
jsonHostIndex++) {
......