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 { ...@@ -153,6 +153,14 @@ public interface ResourceService {
153 Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer); 153 Collection<ResourceAllocation> getResourceAllocations(ResourceConsumer consumer);
154 154
155 /** 155 /**
156 + * Returns resource paths that point available child resources under the specified resource path.
157 + *
158 + * @param parent parent resource path
159 + * @return available resource paths under the specified resource path
160 + */
161 + Collection<ResourcePath> getAvailableResources(ResourcePath parent);
162 +
163 + /**
156 * Returns the availability of the specified resource. 164 * Returns the availability of the specified resource.
157 * 165 *
158 * @param resource resource to check the availability 166 * @param resource resource to check the availability
......
...@@ -92,6 +92,14 @@ public interface ResourceStore { ...@@ -92,6 +92,14 @@ public interface ResourceStore {
92 Collection<ResourcePath> getResources(ResourceConsumer consumer); 92 Collection<ResourcePath> getResources(ResourceConsumer consumer);
93 93
94 /** 94 /**
95 + * Returns a collection of the child resources of the specified parent.
96 + *
97 + * @param parent parent of the resource to be returned
98 + * @return a collection of the child resources of the specified resource
99 + */
100 + Collection<ResourcePath> getChildResources(ResourcePath parent);
101 +
102 + /**
95 * Returns a collection of the resources which are children of the specified parent and 103 * Returns a collection of the resources which are children of the specified parent and
96 * whose type is the specified class. 104 * whose type is the specified class.
97 * 105 *
......
...@@ -127,6 +127,17 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ ...@@ -127,6 +127,17 @@ public final class ResourceManager implements ResourceService, ResourceAdminServ
127 } 127 }
128 128
129 @Override 129 @Override
130 + public Collection<ResourcePath> getAvailableResources(ResourcePath parent) {
131 + checkNotNull(parent);
132 +
133 + Collection<ResourcePath> children = store.getChildResources(parent);
134 + return children.stream()
135 + // We access store twice in this method, then the store may be updated by others
136 + .filter(x -> !store.getConsumer(x).isPresent())
137 + .collect(Collectors.toList());
138 + }
139 +
140 + @Override
130 public boolean isAvailable(ResourcePath resource) { 141 public boolean isAvailable(ResourcePath resource) {
131 checkNotNull(resource); 142 checkNotNull(resource);
132 143
......
...@@ -239,6 +239,18 @@ public class ConsistentResourceStore implements ResourceStore { ...@@ -239,6 +239,18 @@ public class ConsistentResourceStore implements ResourceStore {
239 } 239 }
240 240
241 @Override 241 @Override
242 + public Collection<ResourcePath> getChildResources(ResourcePath parent) {
243 + checkNotNull(parent);
244 +
245 + Versioned<List<ResourcePath>> children = childMap.get(parent);
246 + if (children == null) {
247 + return Collections.emptyList();
248 + }
249 +
250 + return children.value();
251 + }
252 +
253 + @Override
242 public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) { 254 public <T> Collection<ResourcePath> getAllocatedResources(ResourcePath parent, Class<T> cls) {
243 checkNotNull(parent); 255 checkNotNull(parent);
244 checkNotNull(cls); 256 checkNotNull(cls);
......