Sho SHIMIZU
Committed by Gerrit Code Review

Use ResourceId or DiscreteResourceId when specifying a resource

Change-Id: I4e29558ec649510c8d08bb5e5f8ed10c189252e5
......@@ -30,8 +30,8 @@ import org.onosproject.net.OchSignal;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.newresource.DiscreteResourceId;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.Resource;
import org.onosproject.net.newresource.ResourceService;
import com.google.common.base.Strings;
......@@ -107,7 +107,7 @@ public class AllocationsCommand extends AbstractShellCommand {
// TODO: Current design cannot deal with sub-resources
// (e.g., TX/RX under Port)
Resource resource = Resources.discrete(did, num).resource();
DiscreteResourceId resource = Resources.discrete(did, num).id();
if (lambda) {
//print("Lambda resources:");
Collection<ResourceAllocation> allocations
......
......@@ -34,6 +34,7 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.TributarySlot;
import org.onosproject.net.newresource.ContinuousResource;
import org.onosproject.net.newresource.DiscreteResource;
import org.onosproject.net.newresource.Resource;
import org.onosproject.net.newresource.ResourceService;
......@@ -101,7 +102,13 @@ public class ResourcesCommand extends AbstractShellCommand {
private void printResource(Resource resource, int level) {
// TODO add an option to show only available resource
Set<Resource> children = resourceService.getRegisteredResources(resource);
// workaround to preserve the original behavior of ResourceService#getRegisteredResources
Set<Resource> children;
if (resource instanceof DiscreteResource) {
children = resourceService.getRegisteredResources(((DiscreteResource) resource).id());
} else {
children = Collections.emptySet();
}
if (resource.equals(Resource.ROOT)) {
print("ROOT");
......
......@@ -129,24 +129,22 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
/**
* Returns resource allocations of the specified resource.
*
* @param resource resource to check the allocation
* @param id ID of the resource to check the allocation
* @return list of allocation information.
* If the resource is not allocated, the return value is an empty list.
*/
// TODO: need to change the argument type to ResourceId
List<ResourceAllocation> getResourceAllocations(Resource resource);
List<ResourceAllocation> getResourceAllocations(ResourceId id);
/**
* Returns allocated resources being as children of the specified parent and being the specified resource type.
*
* @param parent parent resource
* @param parent parent resource ID
* @param cls class to specify a type of resource
* @param <T> type of the resource
* @return non-empty collection of resource allocations if resources are allocated with the subject and type,
* empty collection if no resource is allocated with the subject and type
*/
// TODO: might need to change the first argument type to ResourceId or ResourceId.Discrete
<T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls);
<T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls);
/**
* Returns resources allocated to the specified consumer.
......@@ -159,20 +157,18 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
/**
* Returns resources that point available child resources under the specified resource.
*
* @param parent parent resource
* @param parent parent resource ID
* @return available resources under the specified resource
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Set<Resource> getAvailableResources(Resource parent);
Set<Resource> getAvailableResources(DiscreteResourceId parent);
/**
* Returns resources registered under the specified resource.
*
* @param parent parent resource
* @param parent parent resource ID
* @return registered resources under the specified resource
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Set<Resource> getRegisteredResources(Resource parent);
Set<Resource> getRegisteredResources(DiscreteResourceId parent);
/**
......
......@@ -81,12 +81,11 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
* The return value is a list having only one element when the given resource is discrete type.
* The return value may have multiple elements when the given resource is continuous type.
*
* @param resource resource whose allocated consumer to be returned
* @param id ID of the resource whose allocated consumer to be returned
* @return resource consumers who are allocated the resource.
* Returns empty list if there is no such consumer.
*/
// TODO: need to change the argument type to ResourceId
List<ResourceConsumer> getConsumers(Resource resource);
List<ResourceAllocation> getResourceAllocations(ResourceId id);
/**
* Returns the availability of the specified resource.
......@@ -107,22 +106,20 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
/**
* Returns a set of the child resources of the specified parent.
*
* @param parent parent of the resource to be returned
* @param parent ID of the parent of the resource to be returned
* @return a set of the child resources of the specified resource
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Set<Resource> getChildResources(Resource parent);
Set<Resource> getChildResources(DiscreteResourceId parent);
/**
* Returns a collection of the resources which are children of the specified parent and
* whose type is the specified class.
*
* @param parent parent of the resources to be returned
* @param parent ID of the parent of the resources to be returned
* @param cls class instance of the children
* @param <T> type of the resource
* @return a collection of the resources which belongs to the specified subject and
* whose type is the specified class.
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
<T> Collection<Resource> getAllocatedResources(Resource parent, Class<T> cls);
<T> Collection<Resource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls);
}
......
......@@ -157,7 +157,7 @@ public class MplsPathIntentCompiler implements IntentCompiler<MplsPathIntent> {
}
private Set<MplsLabel> findMplsLabel(ConnectPoint cp) {
return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).resource()).stream()
return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
.filter(x -> x.last() instanceof MplsLabel)
.map(x -> (MplsLabel) x.last())
.collect(Collectors.toSet());
......
......@@ -313,8 +313,7 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu
if (ochCP != null) {
OchPort ochPort = (OchPort) deviceService.getPort(ochCP.deviceId(), ochCP.port());
Optional<IntentId> intentId =
resourceService.getResourceAllocations(
Resources.discrete(ochCP.deviceId(), ochCP.port()).resource())
resourceService.getResourceAllocations(Resources.discrete(ochCP.deviceId(), ochCP.port()).id())
.stream()
.map(ResourceAllocation::consumer)
.filter(x -> x instanceof IntentId)
......@@ -335,8 +334,7 @@ public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircu
}
Optional<IntentId> intentId =
resourceService.getResourceAllocations(
Resources.discrete(oduPort.deviceId(), port.number()).resource())
resourceService.getResourceAllocations(Resources.discrete(oduPort.deviceId(), port.number()).id())
.stream()
.map(ResourceAllocation::consumer)
.filter(x -> x instanceof IntentId)
......
......@@ -214,8 +214,8 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler<Optical
private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) {
return links.stream()
.flatMap(x -> Stream.of(
Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
Resources.discrete(x.src().deviceId(), x.src().port()).id(),
Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
))
.map(resourceService::getAvailableResources)
.map(x -> Iterables.filter(x, r -> r.last() instanceof OchSignal))
......
......@@ -285,7 +285,7 @@ public class PathIntentCompiler implements IntentCompiler<PathIntent> {
}
private Set<VlanId> findVlanId(ConnectPoint cp) {
return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).resource()).stream()
return resourceService.getAvailableResources(Resources.discrete(cp.deviceId(), cp.port()).id()).stream()
.filter(x -> x.last() instanceof VlanId)
.map(x -> (VlanId) x.last())
.collect(Collectors.toSet());
......
......@@ -25,10 +25,12 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.util.GuavaCollectors;
import org.onosproject.event.AbstractListenerManager;
import org.onosproject.net.newresource.DiscreteResourceId;
import org.onosproject.net.newresource.ResourceAdminService;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.ResourceConsumer;
import org.onosproject.net.newresource.ResourceEvent;
import org.onosproject.net.newresource.ResourceId;
import org.onosproject.net.newresource.ResourceListener;
import org.onosproject.net.newresource.ResourceService;
import org.onosproject.net.newresource.Resource;
......@@ -115,25 +117,21 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public List<ResourceAllocation> getResourceAllocations(Resource resource) {
checkNotNull(resource);
public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
checkNotNull(id);
List<ResourceConsumer> consumers = store.getConsumers(resource);
return consumers.stream()
.map(x -> new ResourceAllocation(resource, x))
.collect(GuavaCollectors.toImmutableList());
return store.getResourceAllocations(id);
}
@Override
public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) {
public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
checkNotNull(parent);
checkNotNull(cls);
// We access store twice in this method, then the store may be updated by others
Collection<Resource> resources = store.getAllocatedResources(parent, cls);
return resources.stream()
.flatMap(resource -> store.getConsumers(resource).stream()
.map(consumer -> new ResourceAllocation(resource, consumer)))
.flatMap(resource -> store.getResourceAllocations(resource.id()).stream())
.collect(GuavaCollectors.toImmutableList());
}
......@@ -148,7 +146,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public Set<Resource> getAvailableResources(Resource parent) {
public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
checkNotNull(parent);
Set<Resource> children = store.getChildResources(parent);
......@@ -159,7 +157,7 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public Set<Resource> getRegisteredResources(Resource parent) {
public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
checkNotNull(parent);
return store.getChildResources(parent);
......
......@@ -19,11 +19,16 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.onlab.packet.MplsLabel;
import org.onlab.packet.VlanId;
import org.onosproject.net.newresource.ContinuousResourceId;
import org.onosproject.net.newresource.DiscreteResource;
import org.onosproject.net.newresource.DiscreteResourceId;
import org.onosproject.net.newresource.ResourceAllocation;
import org.onosproject.net.newresource.ResourceConsumer;
import org.onosproject.net.newresource.ResourceId;
import org.onosproject.net.newresource.ResourceListener;
import org.onosproject.net.newresource.Resource;
import org.onosproject.net.newresource.ResourceService;
import org.onosproject.net.newresource.Resources;
import java.util.Collection;
import java.util.HashMap;
......@@ -70,17 +75,21 @@ class MockResourceService implements ResourceService {
}
@Override
public List<ResourceAllocation> getResourceAllocations(Resource resource) {
return Optional.ofNullable(assignment.get(resource))
.map(x -> ImmutableList.of(new ResourceAllocation(resource, x)))
public List<ResourceAllocation> getResourceAllocations(ResourceId id) {
if (id instanceof ContinuousResourceId) {
return ImmutableList.of();
}
DiscreteResource discrete = Resources.discrete((DiscreteResourceId) id).resource();
return Optional.ofNullable(assignment.get(discrete))
.map(x -> ImmutableList.of(new ResourceAllocation(discrete, x)))
.orElse(ImmutableList.of());
}
@Override
public <T> Collection<ResourceAllocation> getResourceAllocations(Resource parent, Class<T> cls) {
public <T> Collection<ResourceAllocation> getResourceAllocations(DiscreteResourceId parent, Class<T> cls) {
return assignment.entrySet().stream()
.filter(x -> x.getKey().parent().isPresent())
.filter(x -> x.getKey().parent().get().equals(parent))
.filter(x -> x.getKey().parent().get().id().equals(parent))
.map(x -> new ResourceAllocation(x.getKey(), x.getValue()))
.collect(Collectors.toList());
}
......@@ -94,16 +103,15 @@ class MockResourceService implements ResourceService {
}
@Override
public Set<Resource> getAvailableResources(Resource parent) {
Collection<Resource> resources = new HashSet<Resource>();
resources.add(parent.child(VlanId.vlanId((short) 10)));
resources.add(parent.child(MplsLabel.mplsLabel(10)));
public Set<Resource> getAvailableResources(DiscreteResourceId parent) {
Collection<Resource> resources = new HashSet<>();
resources.add(Resources.discrete(parent).resource().child(VlanId.vlanId((short) 10)));
resources.add(Resources.discrete(parent).resource().child(MplsLabel.mplsLabel(10)));
return ImmutableSet.copyOf(resources);
}
@Override
public Set<Resource> getRegisteredResources(Resource parent) {
public Set<Resource> getRegisteredResources(DiscreteResourceId parent) {
return getAvailableResources(parent);
}
......