blob: 46754afd121a1dd7a8b02123e485dbb62b001145 [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.VlanId;
20
21import java.util.Optional;
22
23import static com.google.common.base.Preconditions.checkArgument;
24
25/**
26 * Codec for Vlan.
27 */
28@Beta
29public final class VlanCodec implements DiscreteResourceCodec {
30 @Override
31 public int encode(DiscreteResource resource) {
32 Optional<VlanId> vlan = resource.valueAs(VlanId.class);
33 checkArgument(vlan.isPresent());
34 return vlan.map(x -> (int) x.toShort()).get();
35 }
36
37 @Override
38 public DiscreteResource decode(DiscreteResourceId parent, int value) {
39 return Resources.discrete(parent, VlanId.vlanId((short) 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 VlanCodec.class.hashCode();
58 }
59}