Sho SHIMIZU
Committed by Sho SHIMIZU

Modify DiscreteResourceCodec interface

This is for ONOS-4281

Change-Id: Idaa4e3666946ebf1a76abfeb54c626dfb50082c4
...@@ -18,26 +18,25 @@ package org.onosproject.net.resource; ...@@ -18,26 +18,25 @@ package org.onosproject.net.resource;
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 19
20 /** 20 /**
21 - * Represents the common interface to encode a discrete resource to an integer, 21 + * Represents the common interface to encode an object of the specified type to an integer,
22 - * and to decode an integer to a discrete resource. 22 + * and to decode an integer to an object of the specified type.
23 * This class is intended to be used only by the ResourceService implementation. 23 * This class is intended to be used only by the ResourceService implementation.
24 */ 24 */
25 @Beta 25 @Beta
26 -public interface DiscreteResourceCodec { 26 +public interface DiscreteResourceCodec<T> {
27 /** 27 /**
28 - * Encodes the specified discrete resource to an integer. 28 + * Encodes the specified object to an integer.
29 * 29 *
30 * @param resource resource 30 * @param resource resource
31 * @return encoded integer 31 * @return encoded integer
32 */ 32 */
33 - int encode(DiscreteResource resource); 33 + int encode(T resource);
34 34
35 /** 35 /**
36 - * Decodes the specified integer to a discrete resource. 36 + * Decodes the specified integer to an object.
37 * 37 *
38 - * @param parent parent of the returned resource
39 * @param value encoded integer 38 * @param value encoded integer
40 * @return decoded discrete resource 39 * @return decoded discrete resource
41 */ 40 */
42 - DiscreteResource decode(DiscreteResourceId parent, int value); 41 + T decode(int value);
43 } 42 }
......
...@@ -24,6 +24,7 @@ import com.google.common.collect.DiscreteDomain; ...@@ -24,6 +24,7 @@ import com.google.common.collect.DiscreteDomain;
24 import com.google.common.collect.Range; 24 import com.google.common.collect.Range;
25 import com.google.common.collect.TreeRangeSet; 25 import com.google.common.collect.TreeRangeSet;
26 import org.onlab.util.ClosedOpenRange; 26 import org.onlab.util.ClosedOpenRange;
27 +import org.onlab.util.Tools;
27 28
28 import java.util.ArrayList; 29 import java.util.ArrayList;
29 import java.util.List; 30 import java.util.List;
...@@ -45,6 +46,8 @@ public final class DiscreteResourceSetSerializer extends Serializer<DiscreteReso ...@@ -45,6 +46,8 @@ public final class DiscreteResourceSetSerializer extends Serializer<DiscreteReso
45 public void write(Kryo kryo, Output output, DiscreteResourceSet object) { 46 public void write(Kryo kryo, Output output, DiscreteResourceSet object) {
46 TreeRangeSet<Integer> rangeSet = TreeRangeSet.create(); 47 TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
47 object.values().stream() 48 object.values().stream()
49 + .map(x -> x.valueAs(Object.class))
50 + .flatMap(Tools::stream)
48 .map(x -> object.codec().encode(x)) 51 .map(x -> object.codec().encode(x))
49 .map(Range::singleton) 52 .map(Range::singleton)
50 .map(x -> x.canonical(DiscreteDomain.integers())) 53 .map(x -> x.canonical(DiscreteDomain.integers()))
...@@ -70,7 +73,8 @@ public final class DiscreteResourceSetSerializer extends Serializer<DiscreteReso ...@@ -70,7 +73,8 @@ public final class DiscreteResourceSetSerializer extends Serializer<DiscreteReso
70 73
71 Set<DiscreteResource> resources = ranges.stream() 74 Set<DiscreteResource> resources = ranges.stream()
72 .flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound())) 75 .flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound()))
73 - .mapToObj(x -> codec.decode(parent, x)) 76 + .mapToObj(x -> codec.decode(x))
77 + .map(x -> Resources.discrete(parent, x).resource())
74 .collect(Collectors.toSet()); 78 .collect(Collectors.toSet());
75 79
76 return DiscreteResourceSet.of(resources, codec); 80 return DiscreteResourceSet.of(resources, codec);
......
...@@ -18,25 +18,19 @@ package org.onosproject.net.resource; ...@@ -18,25 +18,19 @@ package org.onosproject.net.resource;
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 import org.onlab.packet.MplsLabel; 19 import org.onlab.packet.MplsLabel;
20 20
21 -import java.util.Optional;
22 -
23 -import static com.google.common.base.Preconditions.checkArgument;
24 -
25 /** 21 /**
26 * Codec for MplsLabel. 22 * Codec for MplsLabel.
27 */ 23 */
28 @Beta 24 @Beta
29 -public final class MplsCodec implements DiscreteResourceCodec { 25 +public final class MplsCodec implements DiscreteResourceCodec<MplsLabel> {
30 @Override 26 @Override
31 - public int encode(DiscreteResource resource) { 27 + public int encode(MplsLabel resource) {
32 - Optional<MplsLabel> mpls = resource.valueAs(MplsLabel.class); 28 + return resource.toInt();
33 - checkArgument(mpls.isPresent());
34 - return mpls.map(MplsLabel::toInt).get();
35 } 29 }
36 30
37 @Override 31 @Override
38 - public DiscreteResource decode(DiscreteResourceId parent, int value) { 32 + public MplsLabel decode(int value) {
39 - return Resources.discrete(parent, MplsLabel.mplsLabel(value)).resource(); 33 + return MplsLabel.mplsLabel(value);
40 } 34 }
41 35
42 @Override 36 @Override
......
...@@ -19,16 +19,16 @@ package org.onosproject.net.resource; ...@@ -19,16 +19,16 @@ package org.onosproject.net.resource;
19 * Represents no-op codec intended to used in an empty discrete resource set only. 19 * Represents no-op codec intended to used in an empty discrete resource set only.
20 * It's not supposed to be used by other classes. 20 * It's not supposed to be used by other classes.
21 */ 21 */
22 -public class NoOpCodec implements DiscreteResourceCodec { 22 +public class NoOpCodec implements DiscreteResourceCodec<Object> {
23 public static final DiscreteResourceCodec INSTANCE = new NoOpCodec(); 23 public static final DiscreteResourceCodec INSTANCE = new NoOpCodec();
24 24
25 @Override 25 @Override
26 - public int encode(DiscreteResource resource) { 26 + public int encode(Object resource) {
27 return 0; 27 return 0;
28 } 28 }
29 29
30 @Override 30 @Override
31 - public DiscreteResource decode(DiscreteResourceId parent, int value) { 31 + public Object decode(int value) {
32 return Resource.ROOT; 32 return Resource.ROOT;
33 } 33 }
34 34
......
...@@ -18,25 +18,19 @@ package org.onosproject.net.resource; ...@@ -18,25 +18,19 @@ package org.onosproject.net.resource;
18 import com.google.common.annotations.Beta; 18 import com.google.common.annotations.Beta;
19 import org.onlab.packet.VlanId; 19 import org.onlab.packet.VlanId;
20 20
21 -import java.util.Optional;
22 -
23 -import static com.google.common.base.Preconditions.checkArgument;
24 -
25 /** 21 /**
26 * Codec for Vlan. 22 * Codec for Vlan.
27 */ 23 */
28 @Beta 24 @Beta
29 -public final class VlanCodec implements DiscreteResourceCodec { 25 +public final class VlanCodec implements DiscreteResourceCodec<VlanId> {
30 @Override 26 @Override
31 - public int encode(DiscreteResource resource) { 27 + public int encode(VlanId resource) {
32 - Optional<VlanId> vlan = resource.valueAs(VlanId.class); 28 + return resource.toShort();
33 - checkArgument(vlan.isPresent());
34 - return vlan.map(x -> (int) x.toShort()).get();
35 } 29 }
36 30
37 @Override 31 @Override
38 - public DiscreteResource decode(DiscreteResourceId parent, int value) { 32 + public VlanId decode(int value) {
39 - return Resources.discrete(parent, VlanId.vlanId((short) value)).resource(); 33 + return VlanId.vlanId((short) value);
40 } 34 }
41 35
42 @Override 36 @Override
......