Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 18 | import org.onlab.util.Identifier; |
| 19 | |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 20 | /** |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 21 | * Representation of a VLAN identifier. |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 22 | */ |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 23 | public final class VlanId extends Identifier<Short> { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 24 | // Based on convention used elsewhere? Check and change if needed |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 25 | public static final short UNTAGGED = (short) 0xffff; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 26 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 27 | // 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 SHIMIZU | 55caa1c | 2016-05-18 23:35:02 -0700 | [diff] [blame] | 32 | 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 | |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 35 | public static final VlanId NONE = VlanId.vlanId(UNTAGGED); |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 36 | public static final VlanId ANY = VlanId.vlanId(ANY_VALUE); |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 37 | |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 38 | 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 Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 42 | // A VLAN ID is actually 12 bits of a VLAN tag. |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 43 | public static final short MAX_VLAN = 4095; |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 44 | |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 45 | // Constructor for serialization. |
| 46 | private VlanId() { |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 47 | super(UNTAGGED); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 50 | // Creates a VLAN identifier for the specified VLAN number. |
| 51 | private VlanId(short value) { |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 52 | super(value); |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Creates a VLAN identifier for untagged VLAN. |
| 57 | * |
| 58 | * @return VLAN identifier |
| 59 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 60 | public static VlanId vlanId() { |
| 61 | return new VlanId(UNTAGGED); |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 64 | /** |
| 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 70 | public static VlanId vlanId(short value) { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 71 | if (value == UNTAGGED) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 72 | return new VlanId(); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 73 | } |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 74 | if (value == ANY_VALUE) { |
| 75 | return new VlanId(ANY_VALUE); |
| 76 | } |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 77 | if (value > MAX_VLAN) { |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 78 | throw new IllegalArgumentException( |
| 79 | "value exceeds allowed maximum VLAN ID value (4095)"); |
| 80 | } |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 81 | return new VlanId(value); |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 84 | /** |
| 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 Chan | 9b26bf3 | 2016-04-12 16:46:36 -0700 | [diff] [blame] | 98 | return VlanId.vlanId(Short.parseShort(value)); |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 99 | } 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 Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 109 | public short toShort() { |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 110 | return this.identifier; |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 111 | } |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 112 | |
| 113 | @Override |
| 114 | public String toString() { |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 115 | if (this.identifier == ANY_VALUE) { |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 116 | return "Any"; |
| 117 | } |
Luca Prete | 283a962 | 2016-03-29 16:12:20 -0700 | [diff] [blame] | 118 | if (this.identifier == UNTAGGED) { |
| 119 | return "None"; |
| 120 | } |
Jian Li | 597d7b2 | 2016-02-29 14:06:55 -0800 | [diff] [blame] | 121 | return String.valueOf(this.identifier); |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 122 | } |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 123 | } |
| 124 | |