tom

Merge branch 'master' of ssh://gerrit.onlab.us:29418/onos-next

...@@ -8,23 +8,27 @@ import java.util.Objects; ...@@ -8,23 +8,27 @@ 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 +import org.onlab.packet.VLANID;
13 14
15 +/**
16 + * A basic implementation of a Host.
17 + */
14 public class DefaultHost extends AbstractElement implements Host { 18 public class DefaultHost extends AbstractElement implements Host {
15 19
16 private final MACAddress mac; 20 private final MACAddress mac;
17 - private final short vlan; 21 + private final VLANID vlan;
18 private final HostLocation location; 22 private final HostLocation location;
19 - private final Set<IPv4> ips; 23 + private final Set<IPAddress> ips;
20 24
21 - public DefaultHost(ProviderId providerId, ElementId id, MACAddress mac, 25 + public DefaultHost(ProviderId providerId, HostId id, MACAddress mac,
22 - short vlan, HostLocation loc, Set<IPv4> ips) { 26 + VLANID vlan, HostLocation loc, Set<IPAddress> ips) {
23 super(providerId, id); 27 super(providerId, id);
24 this.mac = mac; 28 this.mac = mac;
25 this.vlan = vlan; 29 this.vlan = vlan;
26 this.location = loc; 30 this.location = loc;
27 - this.ips = new HashSet<IPv4>(ips); 31 + this.ips = new HashSet<IPAddress>(ips);
28 } 32 }
29 33
30 @Override 34 @Override
...@@ -38,7 +42,7 @@ public class DefaultHost extends AbstractElement implements Host { ...@@ -38,7 +42,7 @@ public class DefaultHost extends AbstractElement implements Host {
38 } 42 }
39 43
40 @Override 44 @Override
41 - public Set<IPv4> ipAddresses() { 45 + public Set<IPAddress> ipAddresses() {
42 return Collections.unmodifiableSet(ips); 46 return Collections.unmodifiableSet(ips);
43 } 47 }
44 48
...@@ -48,7 +52,7 @@ public class DefaultHost extends AbstractElement implements Host { ...@@ -48,7 +52,7 @@ public class DefaultHost extends AbstractElement implements Host {
48 } 52 }
49 53
50 @Override 54 @Override
51 - public short vlan() { 55 + public VLANID vlan() {
52 return vlan; 56 return vlan;
53 } 57 }
54 58
......
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 +import org.onlab.packet.VLANID;
5 6
6 import java.util.Set; 7 import java.util.Set;
7 8
...@@ -15,6 +16,7 @@ public interface Host extends Element { ...@@ -15,6 +16,7 @@ public interface Host extends Element {
15 * 16 *
16 * @return host id 17 * @return host id
17 */ 18 */
19 + @Override
18 HostId id(); 20 HostId id();
19 21
20 /** 22 /**
...@@ -29,14 +31,14 @@ public interface Host extends Element { ...@@ -29,14 +31,14 @@ public interface Host extends Element {
29 * 31 *
30 * @return VLAN ID value 32 * @return VLAN ID value
31 */ 33 */
32 - short vlan(); 34 + VLANID vlan();
33 35
34 /** 36 /**
35 * Returns set of IP addresses currently bound to the host MAC address. 37 * Returns set of IP addresses currently bound to the host MAC address.
36 * 38 *
37 * @return set of IP addresses; empty if no IP address is bound 39 * @return set of IP addresses; empty if no IP address is bound
38 */ 40 */
39 - Set<IPv4> ipAddresses(); 41 + Set<IPAddress> ipAddresses();
40 42
41 /** 43 /**
42 * Returns the most recent host location where the host attaches to the 44 * Returns the most recent host location where the host attaches to the
......
...@@ -6,24 +6,25 @@ import java.util.HashSet; ...@@ -6,24 +6,25 @@ 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 +import org.onlab.packet.VLANID;
11 12
12 import com.google.common.collect.ImmutableSet; 13 import com.google.common.collect.ImmutableSet;
13 14
14 public class DefaultHostDescription implements HostDescription { 15 public class DefaultHostDescription implements HostDescription {
15 16
16 private final MACAddress mac; 17 private final MACAddress mac;
17 - private final short vlan; 18 + private final VLANID vlan;
18 private final HostLocation location; 19 private final HostLocation location;
19 - private final Set<IPv4> ips; 20 + private final Set<IPAddress> ips;
20 21
21 - public DefaultHostDescription(MACAddress mac, short vlan, 22 + public DefaultHostDescription(MACAddress mac, VLANID vlan,
22 - HostLocation loc, Set<IPv4> ips) { 23 + HostLocation loc, Set<IPAddress> ips) {
23 this.mac = mac; 24 this.mac = mac;
24 this.vlan = vlan; 25 this.vlan = vlan;
25 this.location = loc; 26 this.location = loc;
26 - this.ips = new HashSet<IPv4>(ips); 27 + this.ips = new HashSet<IPAddress>(ips);
27 } 28 }
28 29
29 @Override 30 @Override
...@@ -32,7 +33,7 @@ public class DefaultHostDescription implements HostDescription { ...@@ -32,7 +33,7 @@ public class DefaultHostDescription implements HostDescription {
32 } 33 }
33 34
34 @Override 35 @Override
35 - public short vlan() { 36 + public VLANID vlan() {
36 return vlan; 37 return vlan;
37 } 38 }
38 39
...@@ -42,7 +43,7 @@ public class DefaultHostDescription implements HostDescription { ...@@ -42,7 +43,7 @@ public class DefaultHostDescription implements HostDescription {
42 } 43 }
43 44
44 @Override 45 @Override
45 - public Set<IPv4> ipAddresses() { 46 + public Set<IPAddress> ipAddresses() {
46 return ImmutableSet.copyOf(ips); 47 return ImmutableSet.copyOf(ips);
47 } 48 }
48 49
......
...@@ -4,8 +4,9 @@ import java.util.Set; ...@@ -4,8 +4,9 @@ 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 +import org.onlab.packet.VLANID;
9 10
10 /** 11 /**
11 * Information describing host and its location. 12 * Information describing host and its location.
...@@ -24,7 +25,7 @@ public interface HostDescription extends Description { ...@@ -24,7 +25,7 @@ public interface HostDescription extends Description {
24 * 25 *
25 * @return the VLAN ID value 26 * @return the VLAN ID value
26 */ 27 */
27 - short vlan(); 28 + VLANID vlan();
28 29
29 /** 30 /**
30 * Returns the location of the host on the network edge. 31 * Returns the location of the host on the network edge.
...@@ -38,6 +39,6 @@ public interface HostDescription extends Description { ...@@ -38,6 +39,6 @@ public interface HostDescription extends Description {
38 * 39 *
39 * @return a set of IP addresses. 40 * @return a set of IP addresses.
40 */ 41 */
41 - Set<IPv4> ipAddresses(); 42 + Set<IPAddress> ipAddresses();
42 43
43 } 44 }
......
...@@ -4,8 +4,9 @@ import org.onlab.onos.net.ConnectPoint; ...@@ -4,8 +4,9 @@ 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 +import org.onlab.packet.VLANID;
9 10
10 import java.util.Set; 11 import java.util.Set;
11 12
...@@ -43,7 +44,7 @@ public interface HostService { ...@@ -43,7 +44,7 @@ public interface HostService {
43 * @return set of hosts in the given vlan id 44 * @return set of hosts in the given vlan id
44 */ 45 */
45 // FIXME: change long to VLanId 46 // FIXME: change long to VLanId
46 - Set<Host> getHostsByVlan(long vlanId); 47 + Set<Host> getHostsByVlan(VLANID vlanId);
47 48
48 /** 49 /**
49 * Returns the set of hosts that have the specified MAC address. 50 * Returns the set of hosts that have the specified MAC address.
...@@ -59,7 +60,7 @@ public interface HostService { ...@@ -59,7 +60,7 @@ public interface HostService {
59 * @param ip ip address 60 * @param ip ip address
60 * @return set of hosts with the given IP 61 * @return set of hosts with the given IP
61 */ 62 */
62 - Set<Host> getHostsByIp(IPv4 ip); 63 + Set<Host> getHostsByIp(IPAddress ip);
63 64
64 /** 65 /**
65 * Returns the set of hosts whose most recent location is the specified 66 * Returns the set of hosts whose most recent location is the specified
......
...@@ -21,8 +21,9 @@ import org.onlab.onos.net.host.HostProviderService; ...@@ -21,8 +21,9 @@ 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.onlab.packet.VLANID;
26 import org.slf4j.Logger; 27 import org.slf4j.Logger;
27 28
28 import java.util.Set; 29 import java.util.Set;
...@@ -85,7 +86,7 @@ public class SimpleHostManager ...@@ -85,7 +86,7 @@ public class SimpleHostManager
85 } 86 }
86 87
87 @Override 88 @Override
88 - public Set<Host> getHostsByVlan(long vlanId) { 89 + public Set<Host> getHostsByVlan(VLANID vlanId) {
89 return store.getHosts(vlanId); 90 return store.getHosts(vlanId);
90 } 91 }
91 92
...@@ -96,7 +97,7 @@ public class SimpleHostManager ...@@ -96,7 +97,7 @@ public class SimpleHostManager
96 } 97 }
97 98
98 @Override 99 @Override
99 - public Set<Host> getHostsByIp(IPv4 ip) { 100 + public Set<Host> getHostsByIp(IPAddress ip) {
100 checkNotNull(ip, "IP address cannot be null"); 101 checkNotNull(ip, "IP address cannot be null");
101 return store.getHosts(ip); 102 return store.getHosts(ip);
102 } 103 }
......
...@@ -19,8 +19,9 @@ import org.onlab.onos.net.HostId; ...@@ -19,8 +19,9 @@ import org.onlab.onos.net.HostId;
19 import org.onlab.onos.net.host.HostDescription; 19 import org.onlab.onos.net.host.HostDescription;
20 import org.onlab.onos.net.host.HostEvent; 20 import org.onlab.onos.net.host.HostEvent;
21 import org.onlab.onos.net.provider.ProviderId; 21 import org.onlab.onos.net.provider.ProviderId;
22 -import org.onlab.packet.IPv4; 22 +import org.onlab.packet.IPAddress;
23 import org.onlab.packet.MACAddress; 23 import org.onlab.packet.MACAddress;
24 +import org.onlab.packet.VLANID;
24 25
25 import com.google.common.collect.HashMultimap; 26 import com.google.common.collect.HashMultimap;
26 import com.google.common.collect.ImmutableSet; 27 import com.google.common.collect.ImmutableSet;
...@@ -149,10 +150,10 @@ public class SimpleHostStore { ...@@ -149,10 +150,10 @@ public class SimpleHostStore {
149 * @param vlanId vlan id 150 * @param vlanId vlan id
150 * @return set of hosts in the vlan 151 * @return set of hosts in the vlan
151 */ 152 */
152 - Set<Host> getHosts(long vlanId) { 153 + Set<Host> getHosts(VLANID vlanId) {
153 Set<Host> vlanset = new HashSet<Host>(); 154 Set<Host> vlanset = new HashSet<Host>();
154 for (Host h : hosts.values()) { 155 for (Host h : hosts.values()) {
155 - if (h.vlan() == vlanId) { 156 + if (h.vlan().equals(vlanId)) {
156 vlanset.add(h); 157 vlanset.add(h);
157 } 158 }
158 } 159 }
...@@ -181,7 +182,7 @@ public class SimpleHostStore { ...@@ -181,7 +182,7 @@ public class SimpleHostStore {
181 * @param ip ip address 182 * @param ip ip address
182 * @return set of hosts with the given IP 183 * @return set of hosts with the given IP
183 */ 184 */
184 - Set<Host> getHosts(IPv4 ip) { 185 + Set<Host> getHosts(IPAddress ip) {
185 Set<Host> ipset = new HashSet<>(); 186 Set<Host> ipset = new HashSet<>();
186 for (Host h : hosts.values()) { 187 for (Host h : hosts.values()) {
187 if (h.ipAddresses().contains(ip)) { 188 if (h.ipAddresses().contains(ip)) {
......
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 address, 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 +}
1 +package org.onlab.packet;
2 +
3 +/**
4 + * Representation of a VLAN ID.
5 + */
6 +public class VLANID {
7 + // A VLAN ID is 12 bits, short is close
8 + private final short value;
9 +
10 + public VLANID(short value) {
11 + this.value = value;
12 + }
13 +
14 + public short toShort() {
15 + return this.value;
16 + }
17 +
18 + @Override
19 + public boolean equals(Object obj) {
20 + if (this == obj) {
21 + return true;
22 + }
23 +
24 + if (obj instanceof VLANID) {
25 + return true;
26 + }
27 +
28 + VLANID other = (VLANID) obj;
29 + if (this.value == other.value) {
30 + return true;
31 + }
32 +
33 + return false;
34 + }
35 +
36 + @Override
37 + public int hashCode() {
38 + return this.value;
39 + }
40 +}
41 +