blob: afda7aa877cb8e02c9402eb9f24d97badcd444a5 [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
Jian Li597d7b22016-02-29 14:06:55 -080018import org.onlab.util.Identifier;
19
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070020/**
21 * Representation of a VLAN ID.
22 */
Jian Li597d7b22016-02-29 14:06:55 -080023public class VlanId extends Identifier<Short> {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070024 // Based on convention used elsewhere? Check and change if needed
Ayaka Koshibe16698a32014-09-13 22:19:02 -070025 public static final short UNTAGGED = (short) 0xffff;
tom545708e2014-10-09 17:10:02 -070026
Jonathan Hart6cd2f352015-01-13 17:44:45 -080027 // In a traffic selector, this means that a VLAN ID must be present, but
28 // can have any value. We use the same value as OpenFlow, but this is not
29 // required.
30 public static final short ANY_VALUE = (short) 0x1000;
31
tom545708e2014-10-09 17:10:02 -070032 public static final VlanId NONE = VlanId.vlanId(UNTAGGED);
Jonathan Hart6cd2f352015-01-13 17:44:45 -080033 public static final VlanId ANY = VlanId.vlanId(ANY_VALUE);
tom545708e2014-10-09 17:10:02 -070034
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070035 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070036 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070037
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070038 protected VlanId() {
Jian Li597d7b22016-02-29 14:06:55 -080039 super(UNTAGGED);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070040 }
41
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070042 protected VlanId(short value) {
Jian Li597d7b22016-02-29 14:06:55 -080043 super(value);
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070044 }
45
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070046 public static VlanId vlanId() {
47 return new VlanId(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070048 }
49
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070050 public static VlanId vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070051 if (value == UNTAGGED) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070052 return new VlanId();
Ayaka Koshibe1a100982014-09-13 19:32:19 -070053 }
54
Jonathan Hart6cd2f352015-01-13 17:44:45 -080055 if (value == ANY_VALUE) {
56 return new VlanId(ANY_VALUE);
57 }
58
Ayaka Koshibe1a100982014-09-13 19:32:19 -070059 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070060 throw new IllegalArgumentException(
61 "value exceeds allowed maximum VLAN ID value (4095)");
62 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070063 return new VlanId(value);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070064 }
65
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070066 public short toShort() {
Jian Li597d7b22016-02-29 14:06:55 -080067 return this.identifier;
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070068 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070069
70 @Override
71 public String toString() {
Jian Li597d7b22016-02-29 14:06:55 -080072 if (this.identifier == ANY_VALUE) {
Jonathan Hart6cd2f352015-01-13 17:44:45 -080073 return "Any";
74 }
Jian Li597d7b22016-02-29 14:06:55 -080075 return String.valueOf(this.identifier);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070076 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070077}
78