Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
| 20 | |
| 21 | /** |
| 22 | * Representation of a VLAN ID. |
| 23 | */ |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 24 | public class VlanId { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 25 | |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 26 | private final short value; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 27 | |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 28 | // Based on convention used elsewhere? Check and change if needed |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 29 | public static final short UNTAGGED = (short) 0xffff; |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 30 | |
| 31 | public static final VlanId NONE = VlanId.vlanId(UNTAGGED); |
| 32 | |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 33 | // A VLAN ID is actually 12 bits of a VLAN tag. |
Ayaka Koshibe | 16698a3 | 2014-09-13 22:19:02 -0700 | [diff] [blame] | 34 | public static final short MAX_VLAN = 4095; |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 35 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 36 | protected VlanId() { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 37 | this.value = UNTAGGED; |
| 38 | } |
| 39 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 40 | protected VlanId(short value) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 41 | this.value = value; |
| 42 | } |
| 43 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 44 | public static VlanId vlanId() { |
| 45 | return new VlanId(UNTAGGED); |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 48 | public static VlanId vlanId(short value) { |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 49 | if (value == UNTAGGED) { |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 50 | return new VlanId(); |
Ayaka Koshibe | 1a10098 | 2014-09-13 19:32:19 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if (value > MAX_VLAN) { |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 54 | throw new IllegalArgumentException( |
| 55 | "value exceeds allowed maximum VLAN ID value (4095)"); |
| 56 | } |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 57 | return new VlanId(value); |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 60 | 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 Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 70 | if (obj instanceof VlanId) { |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 71 | |
Ayaka Koshibe | a9c199f | 2014-09-16 16:21:40 -0700 | [diff] [blame] | 72 | VlanId other = (VlanId) obj; |
Ayaka Koshibe | 50ee924 | 2014-09-12 16:37:46 -0700 | [diff] [blame] | 73 | |
| 74 | if (this.value == other.value) { |
| 75 | return true; |
| 76 | } |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public int hashCode() { |
| 84 | return this.value; |
| 85 | } |
Ayaka Koshibe | 3a25aec | 2014-09-12 11:52:53 -0700 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return String.valueOf(this.value); |
| 90 | } |
Ayaka Koshibe | 04a1a4e | 2014-09-11 14:31:29 -0700 | [diff] [blame] | 91 | } |
| 92 | |