blob: 09a939fc719fb3e1d78ad7377945968e558e5aa4 [file] [log] [blame]
Michele Santuari4b6019e2014-12-19 11:31:45 +01001package org.onlab.packet;
2
3/*
Ray Milkey34c95902015-04-15 09:47:53 -07004 * Copyright 2014-2015 Open Networking Laboratory
Michele Santuari4b6019e2014-12-19 11:31:45 +01005 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/**
20 * Representation of a MPLS label.
21 */
22public class MplsLabel {
23
24 private final int mplsLabel;
25
26 // An MPLS Label maximum 20 bits.
27 public static final int MAX_MPLS = 0xFFFFF;
28
29 protected MplsLabel(int value) {
30 this.mplsLabel = value;
31 }
32
33 public static MplsLabel mplsLabel(int value) {
34
Pavlin Radoslavov3ebe1702015-02-17 09:53:17 -080035 if (value < 0 || value > MAX_MPLS) {
36 String errorMsg = "MPLS label value " + value +
37 " is not in the interval [0, 0xFFFFF]";
38 throw new IllegalArgumentException(errorMsg);
Michele Santuari4b6019e2014-12-19 11:31:45 +010039 }
40 return new MplsLabel(value);
41 }
42
43 public int toInt() {
44 return this.mplsLabel;
45 }
46
47 @Override
48 public boolean equals(Object obj) {
49 if (this == obj) {
50 return true;
51 }
52
53 if (obj instanceof MplsLabel) {
54
55 MplsLabel other = (MplsLabel) obj;
56
57 if (this.mplsLabel == other.mplsLabel) {
58 return true;
59 }
60 }
61
62 return false;
63 }
64
65 @Override
66 public int hashCode() {
67 return this.mplsLabel;
68 }
69
70 @Override
71 public String toString() {
72 return String.valueOf(this.mplsLabel);
73 }
74}