mirror of
https://github.com/opennetworkinglab/onos.git
synced 2025-10-16 09:51:38 +02:00
Define valueAs() to get the enclosed value and remove volume()
volume() is replaced by valueAs() Change-Id: I3dbcbd6a0b8fcd28f0064272fe1fa6d7259e0a87
This commit is contained in:
parent
800a7a496a
commit
f08cb4c0e0
@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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 <T> type of the return value
|
||||
* @return the volume of this resource
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> 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 <T> Optional<T> valueAs(Class<T> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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 <T> type of the return value
|
||||
* @return the volume of this resource
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
// TODO: consider receiving Class<T> as an argument. Which approach is convenient?
|
||||
public <T> 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 <T> Optional<T> valueAs(Class<T> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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 <T> type of return value
|
||||
* @return the volume of this resource
|
||||
* @param type class instance specifying the type of return value
|
||||
* @param <T> 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> T volume();
|
||||
<T> Optional<T> valueAs(Class<T> type);
|
||||
|
||||
/**
|
||||
* Returns the last component of this instance.
|
||||
|
@ -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<DeviceId> 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<Double> volume = resource.valueAs(double.class);
|
||||
assertThat(volume.get(), is(BW1.bps()));
|
||||
}
|
||||
}
|
||||
|
@ -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<MplsPathIntent> {
|
||||
|
||||
private Set<MplsLabel> 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());
|
||||
}
|
||||
|
||||
|
@ -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<Optical
|
||||
Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
|
||||
))
|
||||
.map(resourceService::getAvailableResources)
|
||||
.map(x -> 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<OchSignal>) ImmutableSet.copyOf(x))
|
||||
.reduce(Sets::intersection)
|
||||
.orElse(Collections.emptySet());
|
||||
|
@ -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<PathIntent> {
|
||||
|
||||
private Set<VlanId> 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());
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user