blob: a1ab4f0a730da3676fd804b4c4cf377b4cb19bc9 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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/**
Luca Prete283a9622016-03-29 16:12:20 -070021 * Representation of a VLAN identifier.
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070022 */
Luca Prete283a9622016-03-29 16:12:20 -070023public final 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
Sho SHIMIZU55caa1c2016-05-18 23:35:02 -070032 public static final short NO_VID = 0; // 0 is not used for VLAN ID
33 public static final short RESERVED = 4095; // represents all tagged traffic
34
tom545708e2014-10-09 17:10:02 -070035 public static final VlanId NONE = VlanId.vlanId(UNTAGGED);
Jonathan Hart6cd2f352015-01-13 17:44:45 -080036 public static final VlanId ANY = VlanId.vlanId(ANY_VALUE);
tom545708e2014-10-09 17:10:02 -070037
Luca Prete283a9622016-03-29 16:12:20 -070038 private static final String STRING_NONE = "None";
39 private static final String STRING_NUMERIC_NONE = "-1";
40 private static final String STRING_ANY = "Any";
41
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070042 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070043 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070044
Luca Prete283a9622016-03-29 16:12:20 -070045 // Constructor for serialization.
46 private VlanId() {
Jian Li597d7b22016-02-29 14:06:55 -080047 super(UNTAGGED);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070048 }
49
Luca Prete283a9622016-03-29 16:12:20 -070050 // Creates a VLAN identifier for the specified VLAN number.
51 private VlanId(short value) {
Jian Li597d7b22016-02-29 14:06:55 -080052 super(value);
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070053 }
54
Luca Prete283a9622016-03-29 16:12:20 -070055 /**
56 * Creates a VLAN identifier for untagged VLAN.
57 *
58 * @return VLAN identifier
59 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070060 public static VlanId vlanId() {
61 return new VlanId(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070062 }
63
Luca Prete283a9622016-03-29 16:12:20 -070064 /**
65 * Creates a VLAN identifier using the supplied VLAN identifier.
66 *
67 * @param value VLAN identifier expressed as short
68 * @return VLAN identifier
69 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070070 public static VlanId vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070071 if (value == UNTAGGED) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070072 return new VlanId();
Ayaka Koshibe1a100982014-09-13 19:32:19 -070073 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -080074 if (value == ANY_VALUE) {
75 return new VlanId(ANY_VALUE);
76 }
Ayaka Koshibe1a100982014-09-13 19:32:19 -070077 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070078 throw new IllegalArgumentException(
79 "value exceeds allowed maximum VLAN ID value (4095)");
80 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070081 return new VlanId(value);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070082 }
83
Luca Prete283a9622016-03-29 16:12:20 -070084 /**
85 * Creates a VLAN identifier Object using the supplied VLAN identifier.
86 *
87 * @param value VLAN identifier expressed as string
88 * @return VLAN identifier
89 */
90 public static VlanId vlanId(String value) {
91 if (value.equals(STRING_NONE) || value.equals(STRING_NUMERIC_NONE)) {
92 return new VlanId();
93 }
94 if (value.equals(STRING_ANY)) {
95 return new VlanId(ANY_VALUE);
96 }
97 try {
Charles Chan9b26bf32016-04-12 16:46:36 -070098 return VlanId.vlanId(Short.parseShort(value));
Luca Prete283a9622016-03-29 16:12:20 -070099 } catch (NumberFormatException e) {
100 throw new IllegalArgumentException(e);
101 }
102 }
103
104 /**
105 * Returns the backing VLAN number.
106 *
107 * @return VLAN number
108 */
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700109 public short toShort() {
Jian Li597d7b22016-02-29 14:06:55 -0800110 return this.identifier;
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700111 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -0700112
113 @Override
114 public String toString() {
Jian Li597d7b22016-02-29 14:06:55 -0800115 if (this.identifier == ANY_VALUE) {
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800116 return "Any";
117 }
Luca Prete283a9622016-03-29 16:12:20 -0700118 if (this.identifier == UNTAGGED) {
119 return "None";
120 }
Jian Li597d7b22016-02-29 14:06:55 -0800121 return String.valueOf(this.identifier);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -0700122 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700123}
124