Sho SHIMIZU
Committed by Gerrit Code Review

Add method to get available resources filtered by its type

Change-Id: I034bdcf4e9a399af6c68c7ed7f53185bba9b2e27
......@@ -163,6 +163,27 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
Set<Resource> getAvailableResources(DiscreteResourceId parent);
/**
* Returns available resources which are child resources of the specified parent and
* whose type is the specified type.
*
* @param parent parent resource ID
* @param cls class to specify a type of resource
* @param <T> type of the resource
* @return available resources of the specified type under the specified parent resource
*/
<T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls);
/**
* Returns available resource values which are the values of the child resource of
* the specified parent and whose type is the specified type.
*
* @param parent parent resource ID
* @param cls class to specify a type of resource
* @param <T> type of the resource
* @return available resource value of the specified type under the specified parent resource
*/
<T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls);
/**
* Returns resources registered under the specified resource.
*
* @param parent parent resource ID
......
......@@ -17,7 +17,6 @@ package org.onosproject.net.intent.impl.compiler;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -27,7 +26,6 @@ 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,9 +156,9 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
}
private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
.flatMap(x -> Tools.stream(x.valueAs(MplsLabel.class)))
.collect(Collectors.toSet());
return resourceService.getAvailableResourceValues(
Resources.discrete(cp.deviceId(), cp.port()).id(),
MplsLabel.class);
}
private MplsLabel getMplsLabel(Map<LinkKey, MplsLabel> labels, LinkKey link) {
......
......@@ -24,7 +24,6 @@ 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;
......@@ -217,9 +216,7 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical
Resources.discrete(x.src().deviceId(), x.src().port()).id(),
Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
))
.map(resourceService::getAvailableResources)
.map(x -> x.stream()
.flatMap(r -> Tools.stream(r.valueAs(OchSignal.class))).collect(Collectors.toList()))
.map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
.map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
.reduce(Sets::intersection)
.orElse(Collections.emptySet());
......
......@@ -23,7 +23,6 @@ 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;
......@@ -299,9 +298,9 @@ public class PathIntentCompiler implements IntentCompiler<PathIntent> {
}
private Set<VlanId> findVlanId(ConnectPoint cp) {
return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
.flatMap(x -> Tools.stream(x.valueAs(VlanId.class)))
.collect(Collectors.toSet());
return resourceService.getAvailableResourceValues(
Resources.discrete(cp.deviceId(), cp.port()).id(),
VlanId.class);
}
private boolean isLast(List<Link> links, int i) {
......
......@@ -24,6 +24,7 @@ import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.GuavaCollectors;
import org.onlab.util.Tools;
import org.onosproject.event.AbstractListenerManager;
import org.onosproject.net.newresource.DiscreteResourceId;
import org.onosproject.net.newresource.ResourceAdminService;
......@@ -150,6 +151,28 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
checkNotNull(parent);
checkNotNull(cls);
// naive implementation
return getAvailableResources(parent).stream()
.filter(resource -> resource.isTypeOf(cls))
.collect(Collectors.toSet());
}
@Override
public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
checkNotNull(parent);
checkNotNull(cls);
// naive implementation
return getAvailableResources(parent).stream()
.flatMap(resource -> Tools.stream(resource.valueAs(cls)))
.collect(Collectors.toSet());
}
@Override
public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
checkNotNull(parent);
......
......@@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.onlab.packet.MplsLabel;
import org.onlab.packet.VlanId;
import org.onlab.util.Tools;
import org.onosproject.net.newresource.ContinuousResourceId;
import org.onosproject.net.newresource.DiscreteResource;
import org.onosproject.net.newresource.DiscreteResourceId;
......@@ -111,6 +112,21 @@ class MockResourceService implements ResourceService {
}
@Override
public <T> Set<Resource> getAvailableResources(DiscreteResourceId parent, Class<T> cls) {
return getAvailableResources(parent).stream()
.filter(x -> x.isTypeOf(cls))
.collect(Collectors.toSet());
}
@Override
public <T> Set<T> getAvailableResourceValues(DiscreteResourceId parent, Class<T> cls) {
return getAvailableResources(parent).stream()
.filter(x -> x.isTypeOf(cls))
.flatMap(x -> Tools.stream(x.valueAs(cls)))
.collect(Collectors.toSet());
}
@Override
public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
return getAvailableResources(parent);
}
......