Sho SHIMIZU
Committed by Gerrit Code Review

Define codec interface for discrete resources

The codec defines how to encode the given resource into an integer and
to decode the given interger to a resource.

This is for ONOS-4281.

Change-Id: I5e9143c9f0285f588274f6649a146bbc1e13dcdb
1 +/*
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 + */
16 +package org.onosproject.net.resource;
17 +
18 +import com.google.common.annotations.Beta;
19 +
20 +/**
21 + * Represents the common interface to encode a discrete resource to an integer,
22 + * and to decode an integer to a discrete resource.
23 + * This class is intended to be used only by the ResourceService implementation.
24 + */
25 +@Beta
26 +public interface DiscreteResourceCodec {
27 + /**
28 + * Encodes the specified discrete resource to an integer.
29 + *
30 + * @param resource resource
31 + * @return encoded integer
32 + */
33 + int encode(DiscreteResource resource);
34 +
35 + /**
36 + * Decodes the specified integer to a discrete resource.
37 + *
38 + * @param parent parent of the returned resource
39 + * @param value encoded integer
40 + * @return decoded discrete resource
41 + */
42 + DiscreteResource decode(DiscreteResourceId parent, int value);
43 +}
1 +/*
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 + */
16 +package org.onosproject.net.resource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onlab.packet.MplsLabel;
20 +
21 +import java.util.Optional;
22 +
23 +import static com.google.common.base.Preconditions.checkArgument;
24 +
25 +/**
26 + * Codec for MplsLabel.
27 + */
28 +@Beta
29 +public 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 +}
1 +/*
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 + */
16 +package org.onosproject.net.resource;
17 +
18 +import com.google.common.annotations.Beta;
19 +import org.onlab.packet.VlanId;
20 +
21 +import java.util.Optional;
22 +
23 +import static com.google.common.base.Preconditions.checkArgument;
24 +
25 +/**
26 + * Codec for Vlan.
27 + */
28 +@Beta
29 +public 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 +}