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;
import com.google.common.annotations.Beta;
/**
* Represents the common interface to encode a discrete resource to an integer,
* and to decode an integer to a discrete resource.
* Represents the common interface to encode an object of the specified type to an integer,
* and to decode an integer to an object of the specified type.
* This class is intended to be used only by the ResourceService implementation.
*/
@Beta
public interface DiscreteResourceCodec {
public interface DiscreteResourceCodec<T> {
/**
* Encodes the specified discrete resource to an integer.
* Encodes the specified object to an integer.
*
* @param resource resource
* @return encoded integer
*/
int encode(DiscreteResource resource);
int encode(T resource);
/**
* Decodes the specified integer to a discrete resource.
* Decodes the specified integer to an object.
*
* @param parent parent of the returned resource
* @param value encoded integer
* @return decoded discrete resource
*/
DiscreteResource decode(DiscreteResourceId parent, int value);
T decode(int value);
}
......
......@@ -24,6 +24,7 @@ import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.Range;
import com.google.common.collect.TreeRangeSet;
import org.onlab.util.ClosedOpenRange;
import org.onlab.util.Tools;
import java.util.ArrayList;
import java.util.List;
......@@ -45,6 +46,8 @@ public final class DiscreteResourceSetSerializer extends Serializer<DiscreteReso
public void write(Kryo kryo, Output output, DiscreteResourceSet object) {
TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
object.values().stream()
.map(x -> x.valueAs(Object.class))
.flatMap(Tools::stream)
.map(x -> object.codec().encode(x))
.map(Range::singleton)
.map(x -> x.canonical(DiscreteDomain.integers()))
......@@ -70,7 +73,8 @@ public final class DiscreteResourceSetSerializer extends Serializer<DiscreteReso
Set<DiscreteResource> resources = ranges.stream()
.flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound()))
.mapToObj(x -> codec.decode(parent, x))
.mapToObj(x -> codec.decode(x))
.map(x -> Resources.discrete(parent, x).resource())
.collect(Collectors.toSet());
return DiscreteResourceSet.of(resources, codec);
......
......@@ -18,25 +18,19 @@ package org.onosproject.net.resource;
import com.google.common.annotations.Beta;
import org.onlab.packet.MplsLabel;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Codec for MplsLabel.
*/
@Beta
public final class MplsCodec implements DiscreteResourceCodec {
public final class MplsCodec implements DiscreteResourceCodec<MplsLabel> {
@Override
public int encode(DiscreteResource resource) {
Optional<MplsLabel> mpls = resource.valueAs(MplsLabel.class);
checkArgument(mpls.isPresent());
return mpls.map(MplsLabel::toInt).get();
public int encode(MplsLabel resource) {
return resource.toInt();
}
@Override
public DiscreteResource decode(DiscreteResourceId parent, int value) {
return Resources.discrete(parent, MplsLabel.mplsLabel(value)).resource();
public MplsLabel decode(int value) {
return MplsLabel.mplsLabel(value);
}
@Override
......
......@@ -19,16 +19,16 @@ package org.onosproject.net.resource;
* Represents no-op codec intended to used in an empty discrete resource set only.
* It's not supposed to be used by other classes.
*/
public class NoOpCodec implements DiscreteResourceCodec {
public class NoOpCodec implements DiscreteResourceCodec<Object> {
public static final DiscreteResourceCodec INSTANCE = new NoOpCodec();
@Override
public int encode(DiscreteResource resource) {
public int encode(Object resource) {
return 0;
}
@Override
public DiscreteResource decode(DiscreteResourceId parent, int value) {
public Object decode(int value) {
return Resource.ROOT;
}
......
......@@ -18,25 +18,19 @@ package org.onosproject.net.resource;
import com.google.common.annotations.Beta;
import org.onlab.packet.VlanId;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Codec for Vlan.
*/
@Beta
public final class VlanCodec implements DiscreteResourceCodec {
public final class VlanCodec implements DiscreteResourceCodec<VlanId> {
@Override
public int encode(DiscreteResource resource) {
Optional<VlanId> vlan = resource.valueAs(VlanId.class);
checkArgument(vlan.isPresent());
return vlan.map(x -> (int) x.toShort()).get();
public int encode(VlanId resource) {
return resource.toShort();
}
@Override
public DiscreteResource decode(DiscreteResourceId parent, int value) {
return Resources.discrete(parent, VlanId.vlanId((short) value)).resource();
public VlanId decode(int value) {
return VlanId.vlanId((short) value);
}
@Override
......