Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 | |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 18 | import org.onlab.util.Identifier; |
| 19 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 20 | /** |
| 21 | * Representation of a MPLS label. |
| 22 | */ |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 23 | public final class MplsLabel extends Identifier<Integer> { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 24 | |
| 25 | // An MPLS Label maximum 20 bits. |
| 26 | public static final int MAX_MPLS = 0xFFFFF; |
| 27 | |
| 28 | protected MplsLabel(int value) { |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 29 | super(value); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 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 | |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Creates a MplsLabel object using the supplied decimal string. |
| 44 | * |
| 45 | * @param value the MPLS identifier expressed as string |
| 46 | * @return Mplslabel object created from the string |
| 47 | */ |
| 48 | public static MplsLabel mplsLabel(String value) { |
| 49 | try { |
| 50 | return MplsLabel.mplsLabel(Integer.parseInt(value)); |
| 51 | } catch (NumberFormatException e) { |
| 52 | throw new IllegalArgumentException(e); |
| 53 | } |
| 54 | } |
| 55 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 56 | public int toInt() { |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 57 | return this.id(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String toString() { |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 62 | return String.valueOf(this.identifier); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 63 | } |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 64 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 65 | } |