blob: 8187d9402738542cb3c5aa25fd7e34795710f9d9 [file] [log] [blame]
Sho SHIMIZUf01fe1c2016-05-02 14:41:18 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.net.resource;
17
18import com.google.common.annotations.Beta;
19import org.onlab.packet.MplsLabel;
20
21import java.util.Optional;
22
23import static com.google.common.base.Preconditions.checkArgument;
24
25/**
26 * Codec for MplsLabel.
27 */
28@Beta
29public final class MplsCodec implements DiscreteResourceCodec {
30 @Override
31 public int encode(DiscreteResource resource) {
32 Optional<MplsLabel> mpls = resource.valueAs(MplsLabel.class);
33 checkArgument(mpls.isPresent());
34 return mpls.map(MplsLabel::toInt).get();
35 }
36
37 @Override
38 public DiscreteResource decode(DiscreteResourceId parent, int value) {
39 return Resources.discrete(parent, MplsLabel.mplsLabel(value)).resource();
40 }
41
42 @Override
43 public boolean equals(Object obj) {
44 if (obj == this) {
45 return true;
46 }
47
48 if (obj == null || getClass() != obj.getClass()) {
49 return false;
50 }
51
52 return true;
53 }
54
55 @Override
56 public int hashCode() {
57 return MplsCodec.class.hashCode();
58 }
59}