Modify DiscreteResourceCodec interface
This is for ONOS-4281
Change-Id: Idaa4e3666946ebf1a76abfeb54c626dfb50082c4
(cherry picked from commit 81b75a9e00061d655b2cfbdb8c734753b2bb8c09)
diff --git a/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceCodec.java b/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceCodec.java
index 3ea27c6..7dc1c5e 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceCodec.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceCodec.java
@@ -18,26 +18,25 @@
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);
}
diff --git a/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceSetSerializer.java b/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceSetSerializer.java
index ee81211..2d6446a 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceSetSerializer.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/DiscreteResourceSetSerializer.java
@@ -24,6 +24,7 @@
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 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 @@
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);
diff --git a/core/api/src/main/java/org/onosproject/net/resource/MplsCodec.java b/core/api/src/main/java/org/onosproject/net/resource/MplsCodec.java
index 8187d94..4e651b5 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/MplsCodec.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/MplsCodec.java
@@ -18,25 +18,19 @@
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
diff --git a/core/api/src/main/java/org/onosproject/net/resource/NoOpCodec.java b/core/api/src/main/java/org/onosproject/net/resource/NoOpCodec.java
index 942848b..12f0c79 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/NoOpCodec.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/NoOpCodec.java
@@ -19,16 +19,16 @@
* 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;
}
diff --git a/core/api/src/main/java/org/onosproject/net/resource/VlanCodec.java b/core/api/src/main/java/org/onosproject/net/resource/VlanCodec.java
index 46754af..df4a2a2 100644
--- a/core/api/src/main/java/org/onosproject/net/resource/VlanCodec.java
+++ b/core/api/src/main/java/org/onosproject/net/resource/VlanCodec.java
@@ -18,25 +18,19 @@
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