blob: 4b38308b32939cd2237411a8502649af97d4a1e1 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Jonathan Hart6cd2f352015-01-13 17:44:45 -080028 // In a traffic selector, this means that a VLAN ID must be present, but
29 // can have any value. We use the same value as OpenFlow, but this is not
30 // required.
31 public static final short ANY_VALUE = (short) 0x1000;
32
tom545708e2014-10-09 17:10:02 -070033 public static final VlanId NONE = VlanId.vlanId(UNTAGGED);
Jonathan Hart6cd2f352015-01-13 17:44:45 -080034 public static final VlanId ANY = VlanId.vlanId(ANY_VALUE);
tom545708e2014-10-09 17:10:02 -070035
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070036 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070037 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070038
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070039 protected VlanId() {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070040 this.value = UNTAGGED;
41 }
42
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070043 protected VlanId(short value) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070044 this.value = value;
45 }
46
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070047 public static VlanId vlanId() {
48 return new VlanId(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070049 }
50
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070051 public static VlanId vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070052 if (value == UNTAGGED) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070053 return new VlanId();
Ayaka Koshibe1a100982014-09-13 19:32:19 -070054 }
55
Jonathan Hart6cd2f352015-01-13 17:44:45 -080056 if (value == ANY_VALUE) {
57 return new VlanId(ANY_VALUE);
58 }
59
Ayaka Koshibe1a100982014-09-13 19:32:19 -070060 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070061 throw new IllegalArgumentException(
62 "value exceeds allowed maximum VLAN ID value (4095)");
63 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070064 return new VlanId(value);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070065 }
66
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070067 public short toShort() {
68 return this.value;
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (this == obj) {
74 return true;
75 }
76
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070077 if (obj instanceof VlanId) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070078
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070079 VlanId other = (VlanId) obj;
Ayaka Koshibe50ee9242014-09-12 16:37:46 -070080
81 if (this.value == other.value) {
82 return true;
83 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070084 }
85
86 return false;
87 }
88
89 @Override
90 public int hashCode() {
91 return this.value;
92 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070093
94 @Override
95 public String toString() {
Jonathan Hart6cd2f352015-01-13 17:44:45 -080096 if (this.value == ANY_VALUE) {
97 return "Any";
98 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070099 return String.valueOf(this.value);
100 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700101}
102