blob: 04b87f152be8afb6fd85e7127fce9c87092dc89a [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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/**
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
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
Luca Prete283a9622016-03-29 16:12:20 -070035 private static final String STRING_NONE = "None";
36 private static final String STRING_NUMERIC_NONE = "-1";
37 private static final String STRING_ANY = "Any";
38
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070039 // A VLAN ID is actually 12 bits of a VLAN tag.
Ayaka Koshibe16698a32014-09-13 22:19:02 -070040 public static final short MAX_VLAN = 4095;
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070041
Luca Prete283a9622016-03-29 16:12:20 -070042 // Constructor for serialization.
43 private VlanId() {
Jian Li597d7b22016-02-29 14:06:55 -080044 super(UNTAGGED);
Ayaka Koshibe1a100982014-09-13 19:32:19 -070045 }
46
Luca Prete283a9622016-03-29 16:12:20 -070047 // Creates a VLAN identifier for the specified VLAN number.
48 private VlanId(short value) {
Jian Li597d7b22016-02-29 14:06:55 -080049 super(value);
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -070050 }
51
Luca Prete283a9622016-03-29 16:12:20 -070052 /**
53 * Creates a VLAN identifier for untagged VLAN.
54 *
55 * @return VLAN identifier
56 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070057 public static VlanId vlanId() {
58 return new VlanId(UNTAGGED);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070059 }
60
Luca Prete283a9622016-03-29 16:12:20 -070061 /**
62 * Creates a VLAN identifier using the supplied VLAN identifier.
63 *
64 * @param value VLAN identifier expressed as short
65 * @return VLAN identifier
66 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070067 public static VlanId vlanId(short value) {
Ayaka Koshibe1a100982014-09-13 19:32:19 -070068 if (value == UNTAGGED) {
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070069 return new VlanId();
Ayaka Koshibe1a100982014-09-13 19:32:19 -070070 }
Jonathan Hart6cd2f352015-01-13 17:44:45 -080071 if (value == ANY_VALUE) {
72 return new VlanId(ANY_VALUE);
73 }
Ayaka Koshibe1a100982014-09-13 19:32:19 -070074 if (value > MAX_VLAN) {
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070075 throw new IllegalArgumentException(
76 "value exceeds allowed maximum VLAN ID value (4095)");
77 }
Ayaka Koshibea9c199f2014-09-16 16:21:40 -070078 return new VlanId(value);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -070079 }
80
Luca Prete283a9622016-03-29 16:12:20 -070081 /**
82 * Creates a VLAN identifier Object using the supplied VLAN identifier.
83 *
84 * @param value VLAN identifier expressed as string
85 * @return VLAN identifier
86 */
87 public static VlanId vlanId(String value) {
88 if (value.equals(STRING_NONE) || value.equals(STRING_NUMERIC_NONE)) {
89 return new VlanId();
90 }
91 if (value.equals(STRING_ANY)) {
92 return new VlanId(ANY_VALUE);
93 }
94 try {
95 return new VlanId(Short.parseShort(value));
96 } catch (NumberFormatException e) {
97 throw new IllegalArgumentException(e);
98 }
99 }
100
101 /**
102 * Returns the backing VLAN number.
103 *
104 * @return VLAN number
105 */
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700106 public short toShort() {
Jian Li597d7b22016-02-29 14:06:55 -0800107 return this.identifier;
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700108 }
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -0700109
110 @Override
111 public String toString() {
Jian Li597d7b22016-02-29 14:06:55 -0800112 if (this.identifier == ANY_VALUE) {
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800113 return "Any";
114 }
Luca Prete283a9622016-03-29 16:12:20 -0700115 if (this.identifier == UNTAGGED) {
116 return "None";
117 }
Jian Li597d7b22016-02-29 14:06:55 -0800118 return String.valueOf(this.identifier);
Ayaka Koshibe3a25aec2014-09-12 11:52:53 -0700119 }
Ayaka Koshibe04a1a4e2014-09-11 14:31:29 -0700120}
121