blob: 6a1418d8d5c663581b69d57a298e5909dcc3a90f [file] [log] [blame]
Michele Santuari4b6019e2014-12-19 11:31:45 +01001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Michele Santuari4b6019e2014-12-19 11:31:45 +01003 *
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'Connor7cbbbb72016-04-09 02:13:23 -070016package org.onlab.packet;
Michele Santuari4b6019e2014-12-19 11:31:45 +010017
Pier Ventref8543d82016-09-28 19:49:33 -070018import org.onlab.util.Identifier;
19
Michele Santuari4b6019e2014-12-19 11:31:45 +010020/**
21 * Representation of a MPLS label.
22 */
Pier Ventref8543d82016-09-28 19:49:33 -070023public final class MplsLabel extends Identifier<Integer> {
Michele Santuari4b6019e2014-12-19 11:31:45 +010024
25 // An MPLS Label maximum 20 bits.
26 public static final int MAX_MPLS = 0xFFFFF;
27
28 protected MplsLabel(int value) {
Pier Ventref8543d82016-09-28 19:49:33 -070029 super(value);
Michele Santuari4b6019e2014-12-19 11:31:45 +010030 }
31
32 public static MplsLabel mplsLabel(int value) {
33
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -080034 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 Santuari4b6019e2014-12-19 11:31:45 +010038 }
39 return new MplsLabel(value);
40 }
41
Pier Ventref8543d82016-09-28 19:49:33 -070042 /**
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 Santuari4b6019e2014-12-19 11:31:45 +010056 public int toInt() {
Pier Ventref8543d82016-09-28 19:49:33 -070057 return this.id();
Michele Santuari4b6019e2014-12-19 11:31:45 +010058 }
59
60 @Override
61 public String toString() {
Pier Ventref8543d82016-09-28 19:49:33 -070062 return String.valueOf(this.identifier);
Michele Santuari4b6019e2014-12-19 11:31:45 +010063 }
Pier Ventref8543d82016-09-28 19:49:33 -070064
Michele Santuari4b6019e2014-12-19 11:31:45 +010065}