Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Representation of a MPLS label. |
| 20 | */ |
| 21 | public class MplsLabel { |
| 22 | |
| 23 | private final int mplsLabel; |
| 24 | |
| 25 | // An MPLS Label maximum 20 bits. |
| 26 | public static final int MAX_MPLS = 0xFFFFF; |
| 27 | |
| 28 | protected MplsLabel(int value) { |
| 29 | this.mplsLabel = value; |
| 30 | } |
| 31 | |
| 32 | public static MplsLabel mplsLabel(int value) { |
| 33 | |
Pavlin Radoslavov | 3ebe170 | 2015-02-17 09:53:17 -0800 | [diff] [blame] | 34 | if (value < 0 || value > MAX_MPLS) { |
| 35 | String errorMsg = "MPLS label value " + value + |
| 36 | " is not in the interval [0, 0xFFFFF]"; |
| 37 | throw new IllegalArgumentException(errorMsg); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 38 | } |
| 39 | return new MplsLabel(value); |
| 40 | } |
| 41 | |
| 42 | public int toInt() { |
| 43 | return this.mplsLabel; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public boolean equals(Object obj) { |
| 48 | if (this == obj) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | if (obj instanceof MplsLabel) { |
| 53 | |
| 54 | MplsLabel other = (MplsLabel) obj; |
| 55 | |
| 56 | if (this.mplsLabel == other.mplsLabel) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int hashCode() { |
| 66 | return this.mplsLabel; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String toString() { |
| 71 | return String.valueOf(this.mplsLabel); |
| 72 | } |
| 73 | } |