Ayaka Koshibe

added IPAddress construct

Change-Id: Idc57a22b568b33503c78254222d52fbfc19b0b72
...@@ -8,23 +8,26 @@ import java.util.Objects; ...@@ -8,23 +8,26 @@ import java.util.Objects;
8 import java.util.Set; 8 import java.util.Set;
9 9
10 import org.onlab.onos.net.provider.ProviderId; 10 import org.onlab.onos.net.provider.ProviderId;
11 -import org.onlab.packet.IPv4; 11 +import org.onlab.packet.IPAddress;
12 import org.onlab.packet.MACAddress; 12 import org.onlab.packet.MACAddress;
13 13
14 +/**
15 + * A basic implementation of a Host.
16 + */
14 public class DefaultHost extends AbstractElement implements Host { 17 public class DefaultHost extends AbstractElement implements Host {
15 18
16 private final MACAddress mac; 19 private final MACAddress mac;
17 private final short vlan; 20 private final short vlan;
18 private final HostLocation location; 21 private final HostLocation location;
19 - private final Set<IPv4> ips; 22 + private final Set<IPAddress> ips;
20 23
21 public DefaultHost(ProviderId providerId, ElementId id, MACAddress mac, 24 public DefaultHost(ProviderId providerId, ElementId id, MACAddress mac,
22 - short vlan, HostLocation loc, Set<IPv4> ips) { 25 + short vlan, HostLocation loc, Set<IPAddress> ips) {
23 super(providerId, id); 26 super(providerId, id);
24 this.mac = mac; 27 this.mac = mac;
25 this.vlan = vlan; 28 this.vlan = vlan;
26 this.location = loc; 29 this.location = loc;
27 - this.ips = new HashSet<IPv4>(ips); 30 + this.ips = new HashSet<IPAddress>(ips);
28 } 31 }
29 32
30 @Override 33 @Override
...@@ -38,7 +41,7 @@ public class DefaultHost extends AbstractElement implements Host { ...@@ -38,7 +41,7 @@ public class DefaultHost extends AbstractElement implements Host {
38 } 41 }
39 42
40 @Override 43 @Override
41 - public Set<IPv4> ipAddresses() { 44 + public Set<IPAddress> ipAddresses() {
42 return Collections.unmodifiableSet(ips); 45 return Collections.unmodifiableSet(ips);
43 } 46 }
44 47
......
1 package org.onlab.onos.net; 1 package org.onlab.onos.net;
2 2
3 -import org.onlab.packet.IPv4; 3 +import org.onlab.packet.IPAddress;
4 import org.onlab.packet.MACAddress; 4 import org.onlab.packet.MACAddress;
5 5
6 import java.util.Set; 6 import java.util.Set;
...@@ -15,6 +15,7 @@ public interface Host extends Element { ...@@ -15,6 +15,7 @@ public interface Host extends Element {
15 * 15 *
16 * @return host id 16 * @return host id
17 */ 17 */
18 + @Override
18 HostId id(); 19 HostId id();
19 20
20 /** 21 /**
...@@ -36,7 +37,7 @@ public interface Host extends Element { ...@@ -36,7 +37,7 @@ public interface Host extends Element {
36 * 37 *
37 * @return set of IP addresses; empty if no IP address is bound 38 * @return set of IP addresses; empty if no IP address is bound
38 */ 39 */
39 - Set<IPv4> ipAddresses(); 40 + Set<IPAddress> ipAddresses();
40 41
41 /** 42 /**
42 * Returns the most recent host location where the host attaches to the 43 * Returns the most recent host location where the host attaches to the
......
...@@ -6,7 +6,7 @@ import java.util.HashSet; ...@@ -6,7 +6,7 @@ import java.util.HashSet;
6 import java.util.Set; 6 import java.util.Set;
7 7
8 import org.onlab.onos.net.HostLocation; 8 import org.onlab.onos.net.HostLocation;
9 -import org.onlab.packet.IPv4; 9 +import org.onlab.packet.IPAddress;
10 import org.onlab.packet.MACAddress; 10 import org.onlab.packet.MACAddress;
11 11
12 import com.google.common.collect.ImmutableSet; 12 import com.google.common.collect.ImmutableSet;
...@@ -16,14 +16,14 @@ public class DefaultHostDescription implements HostDescription { ...@@ -16,14 +16,14 @@ public class DefaultHostDescription implements HostDescription {
16 private final MACAddress mac; 16 private final MACAddress mac;
17 private final short vlan; 17 private final short vlan;
18 private final HostLocation location; 18 private final HostLocation location;
19 - private final Set<IPv4> ips; 19 + private final Set<IPAddress> ips;
20 20
21 public DefaultHostDescription(MACAddress mac, short vlan, 21 public DefaultHostDescription(MACAddress mac, short vlan,
22 - HostLocation loc, Set<IPv4> ips) { 22 + HostLocation loc, Set<IPAddress> ips) {
23 this.mac = mac; 23 this.mac = mac;
24 this.vlan = vlan; 24 this.vlan = vlan;
25 this.location = loc; 25 this.location = loc;
26 - this.ips = new HashSet<IPv4>(ips); 26 + this.ips = new HashSet<IPAddress>(ips);
27 } 27 }
28 28
29 @Override 29 @Override
...@@ -42,7 +42,7 @@ public class DefaultHostDescription implements HostDescription { ...@@ -42,7 +42,7 @@ public class DefaultHostDescription implements HostDescription {
42 } 42 }
43 43
44 @Override 44 @Override
45 - public Set<IPv4> ipAddresses() { 45 + public Set<IPAddress> ipAddresses() {
46 return ImmutableSet.copyOf(ips); 46 return ImmutableSet.copyOf(ips);
47 } 47 }
48 48
......
...@@ -4,7 +4,7 @@ import java.util.Set; ...@@ -4,7 +4,7 @@ import java.util.Set;
4 4
5 import org.onlab.onos.net.Description; 5 import org.onlab.onos.net.Description;
6 import org.onlab.onos.net.HostLocation; 6 import org.onlab.onos.net.HostLocation;
7 -import org.onlab.packet.IPv4; 7 +import org.onlab.packet.IPAddress;
8 import org.onlab.packet.MACAddress; 8 import org.onlab.packet.MACAddress;
9 9
10 /** 10 /**
...@@ -38,6 +38,6 @@ public interface HostDescription extends Description { ...@@ -38,6 +38,6 @@ public interface HostDescription extends Description {
38 * 38 *
39 * @return a set of IP addresses. 39 * @return a set of IP addresses.
40 */ 40 */
41 - Set<IPv4> ipAddresses(); 41 + Set<IPAddress> ipAddresses();
42 42
43 } 43 }
......
...@@ -4,7 +4,7 @@ import org.onlab.onos.net.ConnectPoint; ...@@ -4,7 +4,7 @@ import org.onlab.onos.net.ConnectPoint;
4 import org.onlab.onos.net.DeviceId; 4 import org.onlab.onos.net.DeviceId;
5 import org.onlab.onos.net.Host; 5 import org.onlab.onos.net.Host;
6 import org.onlab.onos.net.HostId; 6 import org.onlab.onos.net.HostId;
7 -import org.onlab.packet.IPv4; 7 +import org.onlab.packet.IPAddress;
8 import org.onlab.packet.MACAddress; 8 import org.onlab.packet.MACAddress;
9 9
10 import java.util.Set; 10 import java.util.Set;
...@@ -59,7 +59,7 @@ public interface HostService { ...@@ -59,7 +59,7 @@ public interface HostService {
59 * @param ip ip address 59 * @param ip ip address
60 * @return set of hosts with the given IP 60 * @return set of hosts with the given IP
61 */ 61 */
62 - Set<Host> getHostsByIp(IPv4 ip); 62 + Set<Host> getHostsByIp(IPAddress ip);
63 63
64 /** 64 /**
65 * Returns the set of hosts whose most recent location is the specified 65 * Returns the set of hosts whose most recent location is the specified
......
...@@ -21,7 +21,7 @@ import org.onlab.onos.net.host.HostProviderService; ...@@ -21,7 +21,7 @@ import org.onlab.onos.net.host.HostProviderService;
21 import org.onlab.onos.net.host.HostService; 21 import org.onlab.onos.net.host.HostService;
22 import org.onlab.onos.net.provider.AbstractProviderRegistry; 22 import org.onlab.onos.net.provider.AbstractProviderRegistry;
23 import org.onlab.onos.net.provider.AbstractProviderService; 23 import org.onlab.onos.net.provider.AbstractProviderService;
24 -import org.onlab.packet.IPv4; 24 +import org.onlab.packet.IPAddress;
25 import org.onlab.packet.MACAddress; 25 import org.onlab.packet.MACAddress;
26 import org.slf4j.Logger; 26 import org.slf4j.Logger;
27 27
...@@ -96,7 +96,7 @@ public class SimpleHostManager ...@@ -96,7 +96,7 @@ public class SimpleHostManager
96 } 96 }
97 97
98 @Override 98 @Override
99 - public Set<Host> getHostsByIp(IPv4 ip) { 99 + public Set<Host> getHostsByIp(IPAddress ip) {
100 checkNotNull(ip, "IP address cannot be null"); 100 checkNotNull(ip, "IP address cannot be null");
101 return store.getHosts(ip); 101 return store.getHosts(ip);
102 } 102 }
......
...@@ -8,7 +8,7 @@ import org.onlab.onos.net.HostId; ...@@ -8,7 +8,7 @@ import org.onlab.onos.net.HostId;
8 import org.onlab.onos.net.host.HostDescription; 8 import org.onlab.onos.net.host.HostDescription;
9 import org.onlab.onos.net.host.HostEvent; 9 import org.onlab.onos.net.host.HostEvent;
10 import org.onlab.onos.net.provider.ProviderId; 10 import org.onlab.onos.net.provider.ProviderId;
11 -import org.onlab.packet.IPv4; 11 +import org.onlab.packet.IPAddress;
12 import org.onlab.packet.MACAddress; 12 import org.onlab.packet.MACAddress;
13 13
14 import com.google.common.collect.HashMultimap; 14 import com.google.common.collect.HashMultimap;
...@@ -62,7 +62,7 @@ public class SimpleHostStore { ...@@ -62,7 +62,7 @@ public class SimpleHostStore {
62 descr.vlan(), 62 descr.vlan(),
63 descr.location(), 63 descr.location(),
64 descr.ipAddresses()); 64 descr.ipAddresses());
65 - synchronized(this) { 65 + synchronized (this) {
66 hosts.put(hostId, newhost); 66 hosts.put(hostId, newhost);
67 locations.put(descr.location(), newhost); 67 locations.put(descr.location(), newhost);
68 } 68 }
...@@ -104,7 +104,7 @@ public class SimpleHostStore { ...@@ -104,7 +104,7 @@ public class SimpleHostStore {
104 * @return remove even or null if host was not found 104 * @return remove even or null if host was not found
105 */ 105 */
106 HostEvent removeHost(HostId hostId) { 106 HostEvent removeHost(HostId hostId) {
107 - synchronized(this) { 107 + synchronized (this) {
108 Host host = hosts.remove(hostId); 108 Host host = hosts.remove(hostId);
109 if (host != null) { 109 if (host != null) {
110 locations.remove((host.location()), host); 110 locations.remove((host.location()), host);
...@@ -180,7 +180,7 @@ public class SimpleHostStore { ...@@ -180,7 +180,7 @@ public class SimpleHostStore {
180 * @param ip ip address 180 * @param ip ip address
181 * @return set of hosts with the given IP 181 * @return set of hosts with the given IP
182 */ 182 */
183 - Set<Host> getHosts(IPv4 ip) { 183 + Set<Host> getHosts(IPAddress ip) {
184 Set<Host> ipset = new HashSet<>(); 184 Set<Host> ipset = new HashSet<>();
185 for (Host h : hosts.values()) { 185 for (Host h : hosts.values()) {
186 if (h.ipAddresses().contains(ip)) { 186 if (h.ipAddresses().contains(ip)) {
...@@ -216,4 +216,4 @@ public class SimpleHostStore { ...@@ -216,4 +216,4 @@ public class SimpleHostStore {
216 return hostset; 216 return hostset;
217 } 217 }
218 218
219 -}
...\ No newline at end of file ...\ No newline at end of file
219 +}
......
1 +package org.onlab.packet;
2 +
3 +import java.util.Arrays;
4 +
5 +/**
6 + * A class representing an IPv4 address.
7 + */
8 +public class IPAddress {
9 +
10 + //IP Versions
11 + public enum Version { INET, INET6 };
12 +
13 + //lengths of addresses, in bytes
14 + public static final int INET_LEN = 4;
15 + public static final int INET6_LEN = 6;
16 +
17 + protected Version version;
18 + //does it make more sense to have a integral address?
19 + protected byte[] octets;
20 +
21 + protected IPAddress(Version ver, byte[] octets) {
22 + this.version = ver;
23 + this.octets = Arrays.copyOf(octets, INET_LEN);
24 + }
25 +
26 + /**
27 + * Converts a byte array into an IP address.
28 + *
29 + * @param address a byte array
30 + * @return an IP address
31 + */
32 + public static IPAddress valueOf(byte [] address) {
33 + return new IPAddress(Version.INET, address);
34 + }
35 +
36 + /**
37 + * Converts an integer into an IPv4 address.
38 + *
39 + * @param address an integer representing an IP value
40 + * @return an IP address
41 + */
42 + public static IPAddress valueOf(int address) {
43 + byte [] bytes = new byte [] {
44 + (byte) ((address >> 24) & 0xff),
45 + (byte) ((address >> 16) & 0xff),
46 + (byte) ((address >> 8) & 0xff),
47 + (byte) ((address >> 0) & 0xff)
48 + };
49 + return new IPAddress(Version.INET, bytes);
50 + }
51 +
52 + /**
53 + * Converts a string in dotted-decimal notation (x.x.x.x) into
54 + * an IPv4 address.
55 + *
56 + * @param address a string representing an IP address, e.g. "10.0.0.1"
57 + * @return an IP address
58 + */
59 + public static IPAddress valueOf(String address) {
60 + final String [] parts = address.split(".");
61 + if (parts.length != INET_LEN) {
62 + throw new IllegalArgumentException("Malformed IP address string; "
63 + + "Addres must have four decimal values separated by dots (.)");
64 + }
65 + final byte [] bytes = new byte[INET_LEN];
66 + for (int i = 0; i < INET_LEN; i++) {
67 + bytes[i] = Byte.parseByte(parts[i], 10);
68 + }
69 + return new IPAddress(Version.INET, bytes);
70 + }
71 +
72 + /**
73 + * Returns the IP version of this address.
74 + *
75 + * @return the version
76 + */
77 + public Version version() {
78 + return this.version;
79 + }
80 +
81 + /**
82 + * Returns the IP address as a byte array.
83 + *
84 + * @return a byte array
85 + */
86 + public byte [] toOctets() {
87 + return Arrays.copyOf(this.octets, INET_LEN);
88 + }
89 +
90 + public int toInt() {
91 + int address =
92 + ((octets[0] << 24) |
93 + (octets[1] << 16) |
94 + (octets[2] << 8) |
95 + (octets[3] << 0));
96 + return address;
97 + }
98 +
99 + @Override
100 + public String toString() {
101 + final StringBuilder builder = new StringBuilder();
102 + for (final byte b : this.octets) {
103 + if (builder.length() > 0) {
104 + builder.append(".");
105 + }
106 + builder.append(String.format("%02d", b));
107 + }
108 + return builder.toString();
109 + }
110 +
111 + @Override
112 + public int hashCode() {
113 + return octets.hashCode();
114 + }
115 +
116 + @Override
117 + public boolean equals(Object obj) {
118 + if (this == obj) {
119 + return true;
120 + }
121 + if (obj instanceof IPAddress) {
122 + IPAddress other = (IPAddress) obj;
123 + if (!(this.version.equals(other.version))) {
124 + return false;
125 + }
126 + if (!(Arrays.equals(this.octets, other.octets))) {
127 + return false;
128 + }
129 + }
130 + return true;
131 + }
132 +}