blob: 26a0162e6f2c4cb1f620e34fbda095b3c7acd6d2 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070016package org.onlab.packet;
17
18/**
19 * Representation of a VLAN ID.
20 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070021public class VlanId {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070022
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070023 private final short value;
tom545708e2014-10-09 17:10:02 -070024
Ayaka Koshibe1a100982014-09-13 19:32:19 -070025 // Based on convention used elsewhere? Check and change if needed
Ayaka Koshibe16698a32014-09-13 22:19:02 -070026 public static final short UNTAGGED = (short) 0xffff;
tom545708e2014-10-09 17:10:02 -070027
28 public static final VlanId NONE = VlanId.vlanId(UNTAGGED);
29
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070030 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070031 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070032
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070033 protected VlanId() {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070034 this.value = UNTAGGED;
35 }
36
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070037 protected VlanId(short value) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070038 this.value = value;
39 }
40
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070041 public static VlanId vlanId() {
42 return new VlanId(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070043 }
44
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070045 public static VlanId vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070046 if (value == UNTAGGED) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070047 return new VlanId();
Ayaka Koshibe1a100982014-09-13 19:32:19 -070048 }
49
50 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070051 throw new IllegalArgumentException(
52 "value exceeds allowed maximum VLAN ID value (4095)");
53 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070054 return new VlanId(value);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070055 }
56
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070057 public short toShort() {
58 return this.value;
59 }
60
61 @Override
62 public boolean equals(Object obj) {
63 if (this == obj) {
64 return true;
65 }
66
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 if (obj instanceof VlanId) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070068
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 VlanId other = (VlanId) obj;
Ayaka Koshibe50ee9242014-09-12 16:37:46 -070070
71 if (this.value == other.value) {
72 return true;
73 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070074 }
75
76 return false;
77 }
78
79 @Override
80 public int hashCode() {
81 return this.value;
82 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070083
84 @Override
85 public String toString() {
86 return String.valueOf(this.value);
87 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070088}
89