tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net; |
| 2 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 3 | import org.onlab.packet.MacAddress; |
| 4 | import org.onlab.packet.VlanId; |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 5 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 6 | import java.util.Objects; |
| 7 | |
| 8 | import static com.google.common.base.Preconditions.checkArgument; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Immutable representation of a host identity. |
| 12 | */ |
| 13 | public final class HostId extends ElementId { |
| 14 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 15 | /** |
| 16 | * Represents either no host, or an unspecified host; used for creating |
| 17 | * open ingress/egress edge links. |
| 18 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 19 | public static final HostId NONE = new HostId(MacAddress.ZERO, VlanId.NONE); |
| 20 | |
| 21 | private static final int MAC_LENGTH = 17; |
| 22 | private static final int MIN_ID_LENGTH = 19; |
| 23 | |
| 24 | private final MacAddress mac; |
| 25 | private final VlanId vlanId; |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 26 | |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 27 | // Public construction is prohibited |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 28 | private HostId(MacAddress mac, VlanId vlanId) { |
| 29 | this.mac = mac; |
| 30 | this.vlanId = vlanId; |
| 31 | } |
| 32 | |
| 33 | // Default constructor for serialization |
| 34 | private HostId() { |
| 35 | this.mac = null; |
| 36 | this.vlanId = null; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /** |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 40 | * Returns the host MAC address. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 41 | * |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 42 | * @return MAC address |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 43 | */ |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 44 | public MacAddress mac() { |
| 45 | return mac; |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 49 | * Returns the host MAC address. |
| 50 | * |
| 51 | * @return MAC address |
| 52 | */ |
| 53 | public VlanId vlanId() { |
| 54 | return vlanId; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a device id using the supplied ID string. |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 59 | * |
| 60 | * @param string device URI string |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 61 | * @return host identifier |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 62 | */ |
| 63 | public static HostId hostId(String string) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 64 | checkArgument(string.length() >= MIN_ID_LENGTH, |
| 65 | "Host ID must be at least %s characters", MIN_ID_LENGTH); |
| 66 | MacAddress mac = MacAddress.valueOf(string.substring(0, MAC_LENGTH)); |
| 67 | VlanId vlanId = VlanId.vlanId(Short.parseShort(string.substring(MAC_LENGTH + 1))); |
| 68 | return new HostId(mac, vlanId); |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 69 | } |
| 70 | |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Creates a device id using the supplied MAC & VLAN ID. |
| 73 | * |
| 74 | * @param mac mac address |
| 75 | * @param vlanId vlan identifier |
| 76 | * @return host identifier |
| 77 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 78 | public static HostId hostId(MacAddress mac, VlanId vlanId) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 79 | return new HostId(mac, vlanId); |
tom | 7869ad9 | 2014-09-09 14:32:08 -0700 | [diff] [blame] | 80 | } |
| 81 | |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Creates a device id using the supplied MAC and default VLAN. |
| 84 | * |
| 85 | * @param mac mac address |
| 86 | * @return host identifier |
| 87 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 88 | public static HostId hostId(MacAddress mac) { |
| 89 | return hostId(mac, VlanId.vlanId(VlanId.UNTAGGED)); |
tom | c370ebd | 2014-09-16 01:25:21 -0700 | [diff] [blame] | 90 | } |
| 91 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 92 | public String toString() { |
| 93 | return mac + "/" + vlanId; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int hashCode() { |
| 98 | return Objects.hash(mac, vlanId); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean equals(Object obj) { |
| 103 | if (this == obj) { |
| 104 | return true; |
| 105 | } |
| 106 | if (obj instanceof HostId) { |
| 107 | final HostId other = (HostId) obj; |
| 108 | return Objects.equals(this.mac, other.mac) && |
| 109 | Objects.equals(this.vlanId, other.vlanId); |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
tom | 80c0e5e | 2014-09-08 18:08:58 -0700 | [diff] [blame] | 114 | } |