Sho SHIMIZU
Committed by Ray Milkey

Define and implement a method to query available resources

Change-Id: I49fbcdf215e402603ea15f469d41e572f1cce1c6
......@@ -153,6 +153,14 @@ public interface ResourceService {
Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
/**
* Returns resource paths that point available child resources under the specified resource path.
*
* @param parent parent resource path
* @return available resource paths under the specified resource path
*/
Collection<ResourcePath> getAvailableResources(ResourcePath parent);
/**
* Returns the availability of the specified resource.
*
* @param resource resource to check the availability
......
......@@ -92,6 +92,14 @@ public interface ResourceStore {
Collection<ResourcePath> getResources(ResourceConsumer consumer);
/**
* Returns a collection 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
*/
Collection<ResourcePath> getChildResources(ResourcePath parent);
/**
* Returns a collection of the resources which are children of the specified parent and
* whose type is the specified class.
*
......
......@@ -127,6 +127,17 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
}
@Override
public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
checkNotNull(parent);
Collection<ResourcePath> children = store.getChildResources(parent);
return children.stream()
// We access store twice in this method, then the store may be updated by others
.filter(x -> !store.getConsumer(x).isPresent())
.collect(Collectors.toList());
}
@Override
public boolean isAvailable(ResourcePath resource) {
checkNotNull(resource);
......
......@@ -239,6 +239,18 @@ public class ConsistentResourceStore implements ResourceStore {
}
@Override
public Collection<ResourcePath> getChildResources(ResourcePath parent) {
checkNotNull(parent);
Versioned<List<ResourcePath>> children = childMap.get(parent);
if (children == null) {
return Collections.emptyList();
}
return children.value();
}
@Override
public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) {
checkNotNull(parent);
checkNotNull(cls);
......