Sho SHIMIZU
Committed by Ray Milkey

Use more concrete type for return value

Same resources are not allowed for a parent and Set is more suitable than
Collection

Change-Id: Ib45179819d81376678bf8949864b12b5bd721085
......@@ -22,7 +22,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.apache.karaf.shell.commands.Argument;
......@@ -102,7 +101,7 @@ public class ResourcesCommand extends AbstractShellCommand {
private void printResource(Resource resource, int level) {
// TODO add an option to show only available resource
Collection<Resource> children = resourceService.getRegisteredResources(resource);
Set<Resource> children = resourceService.getRegisteredResources(resource);
if (resource.equals(Resource.ROOT)) {
print("ROOT");
......
......@@ -23,6 +23,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -162,7 +163,7 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
* @return available resources under the specified resource
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Collection<Resource> getAvailableResources(Resource parent);
Set<Resource> getAvailableResources(Resource parent);
/**
* Returns resources registered under the specified resource.
......@@ -171,7 +172,7 @@ public interface ResourceService extends ListenerService<ResourceEvent, Resource
* @return registered resources under the specified resource
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Collection<Resource> getRegisteredResources(Resource parent);
Set<Resource> getRegisteredResources(Resource parent);
/**
......
......@@ -20,6 +20,7 @@ import org.onosproject.store.Store;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* Service for storing resource and consumer information.
......@@ -104,13 +105,13 @@ public interface ResourceStore extends Store<ResourceEvent, ResourceStoreDelegat
Collection<Resource> getResources(ResourceConsumer consumer);
/**
* Returns a collection of the child resources of the specified parent.
* Returns a set of the child resources of the specified parent.
*
* @param parent parent of the resource to be returned
* @return a collection of the child resources of the specified resource
* @return a set of the child resources of the specified resource
*/
// TODO: need to change the argument type to ResourceId or ResourceId.Discrete
Collection<Resource> getChildResources(Resource parent);
Set<Resource> getChildResources(Resource parent);
/**
* Returns a collection of the resources which are children of the specified parent and
......
......@@ -38,6 +38,7 @@ import org.slf4j.Logger;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -147,18 +148,18 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public Collection<Resource> getAvailableResources(Resource parent) {
public Set<Resource> getAvailableResources(Resource parent) {
checkNotNull(parent);
Collection<Resource> children = store.getChildResources(parent);
Set<Resource> children = store.getChildResources(parent);
return children.stream()
// We access store twice in this method, then the store may be updated by others
.filter(store::isAvailable)
.collect(Collectors.toList());
.collect(Collectors.toSet());
}
@Override
public Collection<Resource> getRegisteredResources(Resource parent) {
public Set<Resource> getRegisteredResources(Resource parent) {
checkNotNull(parent);
return store.getChildResources(parent);
......
......@@ -16,6 +16,7 @@
package org.onosproject.net.intent.impl.compiler;
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.ResourceAllocation;
......@@ -30,6 +31,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
class MockResourceService implements ResourceService {
......@@ -92,16 +94,16 @@ class MockResourceService implements ResourceService {
}
@Override
public Collection<Resource> getAvailableResources(Resource parent) {
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)));
return ImmutableList.copyOf(resources);
return ImmutableSet.copyOf(resources);
}
@Override
public Collection<Resource> getRegisteredResources(Resource parent) {
public Set<Resource> getRegisteredResources(Resource parent) {
return getAvailableResources(parent);
}
......
......@@ -17,6 +17,7 @@ package org.onosproject.store.newresource.impl;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
......@@ -389,16 +390,16 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
}
@Override
public Collection<Resource> getChildResources(Resource parent) {
public Set<Resource> getChildResources(Resource parent) {
checkNotNull(parent);
if (!(parent instanceof DiscreteResource)) {
// only Discrete resource can have child resource
return ImmutableList.of();
return ImmutableSet.of();
}
Versioned<Set<Resource>> children = childMap.get((DiscreteResource) parent);
if (children == null) {
return ImmutableList.of();
return ImmutableSet.of();
}
return children.value();
......