blob: fa59a82170051f903a88d97560c131f8d434e70f [file] [log] [blame]
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -07001package org.onlab.packet;
2
3/**
4 * Representation of a VLAN ID.
5 */
tomc370ebd2014-09-16 01:25:21 -07006// FIXME: This will end-up looking like a constant; we should name it 'VlanId', 'IpAddress', 'MacAddress'.
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -07007public class VLANID {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -07008
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -07009 private final short value;
Ayaka Koshibe1a100982014-09-13 19:32:19 -070010 // Based on convention used elsewhere? Check and change if needed
Ayaka Koshibe16698a32014-09-13 22:19:02 -070011 public static final short UNTAGGED = (short) 0xffff;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070012 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070013 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070014
Ayaka Koshibe1a100982014-09-13 19:32:19 -070015 protected VLANID() {
16 this.value = UNTAGGED;
17 }
18
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070019 protected VLANID(short value) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070020 this.value = value;
21 }
22
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070023 public static VLANID vlanId() {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070024 return new VLANID(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070025 }
26
27 public static VLANID vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070028 if (value == UNTAGGED) {
29 return new VLANID();
30 }
31
32 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070033 throw new IllegalArgumentException(
34 "value exceeds allowed maximum VLAN ID value (4095)");
35 }
36 return new VLANID(value);
37 }
38
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070039 public short toShort() {
40 return this.value;
41 }
42
43 @Override
44 public boolean equals(Object obj) {
45 if (this == obj) {
46 return true;
47 }
48
49 if (obj instanceof VLANID) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070050
Ayaka Koshibe50ee9242014-09-12 16:37:46 -070051 VLANID other = (VLANID) obj;
52
53 if (this.value == other.value) {
54 return true;
55 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070056 }
57
58 return false;
59 }
60
61 @Override
62 public int hashCode() {
63 return this.value;
64 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070065
66 @Override
67 public String toString() {
68 return String.valueOf(this.value);
69 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070070}
71