Ayaka Koshibe

added IPAddress construct

Change-Id: Idc57a22b568b33503c78254222d52fbfc19b0b72
......@@ -8,23 +8,26 @@ import java.util.Objects;
import java.util.Set;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
/**
* A basic implementation of a Host.
*/
public class DefaultHost extends AbstractElement implements Host {
private final MACAddress mac;
private final short vlan;
private final HostLocation location;
private final Set<IPv4> ips;
private final Set<IPAddress> ips;
public DefaultHost(ProviderId providerId, ElementId id, MACAddress mac,
short vlan, HostLocation loc, Set<IPv4> ips) {
short vlan, HostLocation loc, Set<IPAddress> ips) {
super(providerId, id);
this.mac = mac;
this.vlan = vlan;
this.location = loc;
this.ips = new HashSet<IPv4>(ips);
this.ips = new HashSet<IPAddress>(ips);
}
@Override
......@@ -38,7 +41,7 @@ public class DefaultHost extends AbstractElement implements Host {
}
@Override
public Set<IPv4> ipAddresses() {
public Set<IPAddress> ipAddresses() {
return Collections.unmodifiableSet(ips);
}
......
package org.onlab.onos.net;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
import java.util.Set;
......@@ -15,6 +15,7 @@ public interface Host extends Element {
*
* @return host id
*/
@Override
HostId id();
/**
......@@ -36,7 +37,7 @@ public interface Host extends Element {
*
* @return set of IP addresses; empty if no IP address is bound
*/
Set<IPv4> ipAddresses();
Set<IPAddress> ipAddresses();
/**
* Returns the most recent host location where the host attaches to the
......
......@@ -6,7 +6,7 @@ import java.util.HashSet;
import java.util.Set;
import org.onlab.onos.net.HostLocation;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
import com.google.common.collect.ImmutableSet;
......@@ -16,14 +16,14 @@ public class DefaultHostDescription implements HostDescription {
private final MACAddress mac;
private final short vlan;
private final HostLocation location;
private final Set<IPv4> ips;
private final Set<IPAddress> ips;
public DefaultHostDescription(MACAddress mac, short vlan,
HostLocation loc, Set<IPv4> ips) {
HostLocation loc, Set<IPAddress> ips) {
this.mac = mac;
this.vlan = vlan;
this.location = loc;
this.ips = new HashSet<IPv4>(ips);
this.ips = new HashSet<IPAddress>(ips);
}
@Override
......@@ -42,7 +42,7 @@ public class DefaultHostDescription implements HostDescription {
}
@Override
public Set<IPv4> ipAddresses() {
public Set<IPAddress> ipAddresses() {
return ImmutableSet.copyOf(ips);
}
......
......@@ -4,7 +4,7 @@ import java.util.Set;
import org.onlab.onos.net.Description;
import org.onlab.onos.net.HostLocation;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
/**
......@@ -38,6 +38,6 @@ public interface HostDescription extends Description {
*
* @return a set of IP addresses.
*/
Set<IPv4> ipAddresses();
Set<IPAddress> ipAddresses();
}
......
......@@ -4,7 +4,7 @@ import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.Host;
import org.onlab.onos.net.HostId;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
import java.util.Set;
......@@ -59,7 +59,7 @@ public interface HostService {
* @param ip ip address
* @return set of hosts with the given IP
*/
Set<Host> getHostsByIp(IPv4 ip);
Set<Host> getHostsByIp(IPAddress ip);
/**
* Returns the set of hosts whose most recent location is the specified
......
......@@ -21,7 +21,7 @@ import org.onlab.onos.net.host.HostProviderService;
import org.onlab.onos.net.host.HostService;
import org.onlab.onos.net.provider.AbstractProviderRegistry;
import org.onlab.onos.net.provider.AbstractProviderService;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
import org.slf4j.Logger;
......@@ -96,7 +96,7 @@ public class SimpleHostManager
}
@Override
public Set<Host> getHostsByIp(IPv4 ip) {
public Set<Host> getHostsByIp(IPAddress ip) {
checkNotNull(ip, "IP address cannot be null");
return store.getHosts(ip);
}
......
......@@ -8,7 +8,7 @@ import org.onlab.onos.net.HostId;
import org.onlab.onos.net.host.HostDescription;
import org.onlab.onos.net.host.HostEvent;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.packet.IPv4;
import org.onlab.packet.IPAddress;
import org.onlab.packet.MACAddress;
import com.google.common.collect.HashMultimap;
......@@ -62,7 +62,7 @@ public class SimpleHostStore {
descr.vlan(),
descr.location(),
descr.ipAddresses());
synchronized(this) {
synchronized (this) {
hosts.put(hostId, newhost);
locations.put(descr.location(), newhost);
}
......@@ -104,7 +104,7 @@ public class SimpleHostStore {
* @return remove even or null if host was not found
*/
HostEvent removeHost(HostId hostId) {
synchronized(this) {
synchronized (this) {
Host host = hosts.remove(hostId);
if (host != null) {
locations.remove((host.location()), host);
......@@ -180,7 +180,7 @@ public class SimpleHostStore {
* @param ip ip address
* @return set of hosts with the given IP
*/
Set<Host> getHosts(IPv4 ip) {
Set<Host> getHosts(IPAddress ip) {
Set<Host> ipset = new HashSet<>();
for (Host h : hosts.values()) {
if (h.ipAddresses().contains(ip)) {
......@@ -216,4 +216,4 @@ public class SimpleHostStore {
return hostset;
}
}
\ No newline at end of file
}
......
package org.onlab.packet;
import java.util.Arrays;
/**
* A class representing an IPv4 address.
*/
public class IPAddress {
//IP Versions
public enum Version { INET, INET6 };
//lengths of addresses, in bytes
public static final int INET_LEN = 4;
public static final int INET6_LEN = 6;
protected Version version;
//does it make more sense to have a integral address?
protected byte[] octets;
protected IPAddress(Version ver, byte[] octets) {
this.version = ver;
this.octets = Arrays.copyOf(octets, INET_LEN);
}
/**
* Converts a byte array into an IP address.
*
* @param address a byte array
* @return an IP address
*/
public static IPAddress valueOf(byte [] address) {
return new IPAddress(Version.INET, address);
}
/**
* Converts an integer into an IPv4 address.
*
* @param address an integer representing an IP value
* @return an IP address
*/
public static IPAddress valueOf(int address) {
byte [] bytes = new byte [] {
(byte) ((address >> 24) & 0xff),
(byte) ((address >> 16) & 0xff),
(byte) ((address >> 8) & 0xff),
(byte) ((address >> 0) & 0xff)
};
return new IPAddress(Version.INET, bytes);
}
/**
* Converts a string in dotted-decimal notation (x.x.x.x) into
* an IPv4 address.
*
* @param address a string representing an IP address, e.g. "10.0.0.1"
* @return an IP address
*/
public static IPAddress valueOf(String address) {
final String [] parts = address.split(".");
if (parts.length != INET_LEN) {
throw new IllegalArgumentException("Malformed IP address string; "
+ "Addres must have four decimal values separated by dots (.)");
}
final byte [] bytes = new byte[INET_LEN];
for (int i = 0; i < INET_LEN; i++) {
bytes[i] = Byte.parseByte(parts[i], 10);
}
return new IPAddress(Version.INET, bytes);
}
/**
* Returns the IP version of this address.
*
* @return the version
*/
public Version version() {
return this.version;
}
/**
* Returns the IP address as a byte array.
*
* @return a byte array
*/
public byte [] toOctets() {
return Arrays.copyOf(this.octets, INET_LEN);
}
public int toInt() {
int address =
((octets[0] << 24) |
(octets[1] << 16) |
(octets[2] << 8) |
(octets[3] << 0));
return address;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
for (final byte b : this.octets) {
if (builder.length() > 0) {
builder.append(".");
}
builder.append(String.format("%02d", b));
}
return builder.toString();
}
@Override
public int hashCode() {
return octets.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof IPAddress) {
IPAddress other = (IPAddress) obj;
if (!(this.version.equals(other.version))) {
return false;
}
if (!(Arrays.equals(this.octets, other.octets))) {
return false;
}
}
return true;
}
}