blob: ffe558f95d87991206c1bce415cdb506a0650128 [file] [log] [blame]
tom80c0e5e2014-09-08 18:08:58 -07001package org.onlab.onos.net;
2
Ayaka Koshibea9c199f2014-09-16 16:21:40 -07003import org.onlab.packet.MacAddress;
4import org.onlab.packet.VlanId;
tom7869ad92014-09-09 14:32:08 -07005
tom545708e2014-10-09 17:10:02 -07006import java.util.Objects;
7
8import static com.google.common.base.Preconditions.checkArgument;
tom80c0e5e2014-09-08 18:08:58 -07009
10/**
11 * Immutable representation of a host identity.
12 */
13public final class HostId extends ElementId {
14
toma1d16b62014-10-02 23:45:11 -070015 /**
16 * Represents either no host, or an unspecified host; used for creating
17 * open ingress/egress edge links.
18 */
tom545708e2014-10-09 17:10:02 -070019 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;
toma1d16b62014-10-02 23:45:11 -070026
tom80c0e5e2014-09-08 18:08:58 -070027 // Public construction is prohibited
tom545708e2014-10-09 17:10:02 -070028 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;
tom80c0e5e2014-09-08 18:08:58 -070037 }
38
39 /**
tom545708e2014-10-09 17:10:02 -070040 * Returns the host MAC address.
tom80c0e5e2014-09-08 18:08:58 -070041 *
tom545708e2014-10-09 17:10:02 -070042 * @return MAC address
tom80c0e5e2014-09-08 18:08:58 -070043 */
tom545708e2014-10-09 17:10:02 -070044 public MacAddress mac() {
45 return mac;
tom80c0e5e2014-09-08 18:08:58 -070046 }
47
48 /**
tom545708e2014-10-09 17:10:02 -070049 * 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.
tom80c0e5e2014-09-08 18:08:58 -070059 *
60 * @param string device URI string
tom7869ad92014-09-09 14:32:08 -070061 * @return host identifier
tom80c0e5e2014-09-08 18:08:58 -070062 */
63 public static HostId hostId(String string) {
tom545708e2014-10-09 17:10:02 -070064 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);
tom80c0e5e2014-09-08 18:08:58 -070069 }
70
tom7869ad92014-09-09 14:32:08 -070071 /**
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 Koshibea9c199f2014-09-16 16:21:40 -070078 public static HostId hostId(MacAddress mac, VlanId vlanId) {
tom545708e2014-10-09 17:10:02 -070079 return new HostId(mac, vlanId);
tom7869ad92014-09-09 14:32:08 -070080 }
81
tomc370ebd2014-09-16 01:25:21 -070082 /**
83 * Creates a device id using the supplied MAC and default VLAN.
84 *
85 * @param mac mac address
86 * @return host identifier
87 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070088 public static HostId hostId(MacAddress mac) {
89 return hostId(mac, VlanId.vlanId(VlanId.UNTAGGED));
tomc370ebd2014-09-16 01:25:21 -070090 }
91
tom545708e2014-10-09 17:10:02 -070092 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
tom80c0e5e2014-09-08 18:08:58 -0700114}