blob: 5b30577417a9fbdacac52387eb9eafe3993a91f5 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070019package org.onlab.packet;
20
21/**
22 * Representation of a VLAN ID.
23 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070024public class VlanId {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070025
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070026 private final short value;
tom545708e2014-10-09 17:10:02 -070027
Ayaka Koshibe1a100982014-09-13 19:32:19 -070028 // Based on convention used elsewhere? Check and change if needed
Ayaka Koshibe16698a32014-09-13 22:19:02 -070029 public static final short UNTAGGED = (short) 0xffff;
tom545708e2014-10-09 17:10:02 -070030
31 public static final VlanId NONE = VlanId.vlanId(UNTAGGED);
32
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070033 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070034 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070035
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070036 protected VlanId() {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070037 this.value = UNTAGGED;
38 }
39
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070040 protected VlanId(short value) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070041 this.value = value;
42 }
43
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070044 public static VlanId vlanId() {
45 return new VlanId(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070046 }
47
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070048 public static VlanId vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070049 if (value == UNTAGGED) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070050 return new VlanId();
Ayaka Koshibe1a100982014-09-13 19:32:19 -070051 }
52
53 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070054 throw new IllegalArgumentException(
55 "value exceeds allowed maximum VLAN ID value (4095)");
56 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 return new VlanId(value);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070058 }
59
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070060 public short toShort() {
61 return this.value;
62 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj) {
67 return true;
68 }
69
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070070 if (obj instanceof VlanId) {
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070071
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070072 VlanId other = (VlanId) obj;
Ayaka Koshibe50ee9242014-09-12 16:37:46 -070073
74 if (this.value == other.value) {
75 return true;
76 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070077 }
78
79 return false;
80 }
81
82 @Override
83 public int hashCode() {
84 return this.value;
85 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070086
87 @Override
88 public String toString() {
89 return String.valueOf(this.value);
90 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070091}
92