From f08cb4c0e025087222d1f80a1fd99eb1e00b6115 Mon Sep 17 00:00:00 2001 From: Sho SHIMIZU Date: Thu, 11 Feb 2016 18:35:59 -0800 Subject: [PATCH] Define valueAs() to get the enclosed value and remove volume() volume() is replaced by valueAs() Change-Id: I3dbcbd6a0b8fcd28f0064272fe1fa6d7259e0a87 --- .../cli/net/AllocationsCommand.java | 2 +- .../onosproject/cli/net/ResourcesCommand.java | 15 ++++---- .../net/newresource/ContinuousResource.java | 34 +++++++++++-------- .../net/newresource/DiscreteResource.java | 28 +++++++-------- .../onosproject/net/newresource/Resource.java | 12 ++++--- .../net/newresource/ResourceTest.java | 19 +++++++---- .../impl/compiler/MplsPathIntentCompiler.java | 4 +-- .../OpticalConnectivityIntentCompiler.java | 6 ++-- .../impl/compiler/PathIntentCompiler.java | 4 +-- 9 files changed, 67 insertions(+), 57 deletions(-) diff --git a/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java b/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java index ccaebd4df2..52d9bb844b 100644 --- a/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java +++ b/cli/src/main/java/org/onosproject/cli/net/AllocationsCommand.java @@ -115,7 +115,7 @@ public class AllocationsCommand extends AbstractShellCommand { for (ResourceAllocation a : allocations) { print("%s%s allocated by %s", Strings.repeat(" ", level + 1), - a.resource().last(), asVerboseString(a.consumer())); + a.resource().valueAs(Object.class).orElse(""), asVerboseString(a.consumer())); } } } diff --git a/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java b/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java index 3b8ca89a8d..49a56c588b 100644 --- a/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java +++ b/cli/src/main/java/org/onosproject/cli/net/ResourcesCommand.java @@ -128,7 +128,7 @@ public class ResourcesCommand extends AbstractShellCommand { } else { String resourceName = resource.last().getClass().getSimpleName(); - String toString = String.valueOf(resource.last()); + String toString = String.valueOf(resource.valueAs(Object.class).orElse("")); if (toString.startsWith(resourceName)) { print("%s%s", Strings.repeat(" ", level), toString); @@ -178,14 +178,13 @@ public class ResourcesCommand extends AbstractShellCommand { // aggregate into RangeSet e.getValue().stream() - .map(Resource::last) .map(res -> { - if (res instanceof VlanId) { - return (long) ((VlanId) res).toShort(); - } else if (res instanceof MplsLabel) { - return (long) ((MplsLabel) res).toInt(); - } else if (res instanceof TributarySlot) { - return ((TributarySlot) res).index(); + if (res.isTypeOf(VlanId.class)) { + return (long) res.valueAs(VlanId.class).get().toShort(); + } else if (res.isTypeOf(MplsLabel.class)) { + return (long) res.valueAs(MplsLabel.class).get().toInt(); + } else if (res.isTypeOf(TributarySlot.class)) { + return res.valueAs(TributarySlot.class).get().index(); } // TODO support Lambda (OchSignal types) return 0L; diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java index a55124e2b1..00221aeb0d 100644 --- a/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java +++ b/core/api/src/main/java/org/onosproject/net/newresource/ContinuousResource.java @@ -56,25 +56,11 @@ public final class ContinuousResource implements Resource { return typeName.equals(type.getCanonicalName()); } - /** - * The user of this methods must receive the return value as Double or double. - * Otherwise, this methods throws an exception. - * - * @param type of the return value - * @return the volume of this resource - */ - @SuppressWarnings("unchecked") - @Override - public T volume() { - return (T) Double.valueOf(value); - } - /** * Returns the value of the resource amount. * * @return the value of the resource amount */ - // FIXME: overlapping a purpose with volume() public double value() { return value; } @@ -93,6 +79,24 @@ public final class ContinuousResource implements Resource { return foundInAncestor || foundInLeaf; } + /** + * {@inheritDoc} + * + * A user must specify Double.class or double.class to avoid an empty value. + */ + @Override + public Optional valueAs(Class type) { + checkNotNull(type); + + if (type == Object.class || type == double.class || type == Double.class) { + @SuppressWarnings("unchecked") + T value = (T) Double.valueOf(this.value); + return Optional.of(value); + } + + return Optional.empty(); + } + @Override public Object last() { if (id.components().isEmpty()) { @@ -138,7 +142,7 @@ public final class ContinuousResource implements Resource { public String toString() { return MoreObjects.toStringHelper(this) .add("id", id) - .add("volume", value) + .add("value", value) .toString(); } } diff --git a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java index 6ce51d3429..beb564aadd 100644 --- a/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java +++ b/core/api/src/main/java/org/onosproject/net/newresource/DiscreteResource.java @@ -56,20 +56,6 @@ public final class DiscreteResource implements Resource { return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass()); } - /** - * The user of this methods must receive the return value as the correct type. - * Otherwise, this methods throws an exception. - * - * @param type of the return value - * @return the volume of this resource - */ - @SuppressWarnings("unchecked") - @Override - // TODO: consider receiving Class as an argument. Which approach is convenient? - public T volume() { - return (T) last(); - } - @Override public boolean isSubTypeOf(Class ancestor) { checkNotNull(ancestor); @@ -81,6 +67,19 @@ public final class DiscreteResource implements Resource { .isPresent(); } + @Override + public Optional valueAs(Class type) { + checkNotNull(type); + + if (!isTypeOf(type)) { + return Optional.empty(); + } + + @SuppressWarnings("unchecked") + T value = (T) id.components().get(id.components().size() - 1); + return Optional.of(value); + } + @Override public Object last() { if (id.components().isEmpty()) { @@ -133,7 +132,6 @@ public final class DiscreteResource implements Resource { public String toString() { return MoreObjects.toStringHelper(this) .add("id", id) - .add("volume", volume()) .toString(); } } diff --git a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java index f2a0c73434..5572592a79 100644 --- a/core/api/src/main/java/org/onosproject/net/newresource/Resource.java +++ b/core/api/src/main/java/org/onosproject/net/newresource/Resource.java @@ -64,13 +64,15 @@ public interface Resource { boolean isSubTypeOf(Class ancestor); /** - * Returns the volume of this resource. + * Returns value interpreted as the specified type. If the specified type is + * incompatible with the underlying value, an empty instance is returned. * - * @param type of return value - * @return the volume of this resource + * @param type class instance specifying the type of return value + * @param type of the return value + * @return the value of this resource as the specified type. If type mismatches, + * returns an empty instance. */ - // TODO: think about other naming possibilities. amount? quantity? - T volume(); + Optional valueAs(Class type); /** * Returns the last component of this instance. diff --git a/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java b/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java index bc6fd2972e..b404d10972 100644 --- a/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java +++ b/core/api/src/test/java/org/onosproject/net/newresource/ResourceTest.java @@ -126,18 +126,25 @@ public class ResourceTest { } @Test - public void testVolumeOfDiscrete() { + public void testValueOfDiscrete() { Resource resource = Resources.discrete(D1).resource(); - DeviceId volume = resource.volume(); - assertThat(volume, is(D1)); + Optional volume = resource.valueAs(DeviceId.class); + assertThat(volume.get(), is(D1)); } @Test - public void testVolumeOfContinuous() { + public void testValueOfRoot() { + Resource resource = Resource.ROOT; + + assertThat(resource.valueAs(Object.class), is(Optional.empty())); + } + + @Test + public void testValueOfContinuous() { Resource resource = Resources.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()); - double volume = resource.volume(); - assertThat(volume, is(BW1.bps())); + Optional volume = resource.valueAs(double.class); + assertThat(volume.get(), is(BW1.bps())); } } diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java index 94b178e641..71ae9258a5 100644 --- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java +++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java @@ -27,6 +27,7 @@ import org.onlab.packet.EthType; import org.onlab.packet.Ethernet; import org.onlab.packet.MplsLabel; import org.onlab.packet.VlanId; +import org.onlab.util.Tools; import org.onosproject.core.ApplicationId; import org.onosproject.core.CoreService; import org.onosproject.net.ConnectPoint; @@ -158,8 +159,7 @@ public class MplsPathIntentCompiler implements IntentCompiler { private Set findMplsLabel(ConnectPoint cp) { return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() - .filter(x -> x.last() instanceof MplsLabel) - .map(x -> (MplsLabel) x.last()) + .flatMap(x -> Tools.stream(x.valueAs(MplsLabel.class))) .collect(Collectors.toSet()); } diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java index 5b741bcfe3..b113b07af3 100644 --- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java +++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java @@ -17,7 +17,6 @@ package org.onosproject.net.intent.impl.compiler; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; @@ -25,6 +24,7 @@ import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.ReferenceCardinality; import org.onlab.util.Frequency; +import org.onlab.util.Tools; import org.onosproject.net.AnnotationKeys; import org.onosproject.net.ChannelSpacing; import org.onosproject.net.ConnectPoint; @@ -218,8 +218,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler Iterables.filter(x, r -> r.last() instanceof OchSignal)) - .map(x -> Iterables.transform(x, r -> (OchSignal) r.last())) + .map(x -> x.stream() + .flatMap(r -> Tools.stream(r.valueAs(OchSignal.class))).collect(Collectors.toList())) .map(x -> (Set) ImmutableSet.copyOf(x)) .reduce(Sets::intersection) .orElse(Collections.emptySet()); diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java index 7309a0993b..569f095fcb 100644 --- a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java +++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentCompiler.java @@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.ReferenceCardinality; import org.onlab.packet.VlanId; +import org.onlab.util.Tools; import org.onosproject.core.ApplicationId; import org.onosproject.core.CoreService; import org.onosproject.net.ConnectPoint; @@ -286,8 +287,7 @@ public class PathIntentCompiler implements IntentCompiler { private Set findVlanId(ConnectPoint cp) { return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream() - .filter(x -> x.last() instanceof VlanId) - .map(x -> (VlanId) x.last()) + .flatMap(x -> Tools.stream(x.valueAs(VlanId.class))) .collect(Collectors.toSet()); }