blob: 82f951f462dd438206f0de9113ecc0beba1ae587 [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
18/**
19 * Representation of a MPLS label.
20 */
21public 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 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
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}