Sho SHIMIZU

Change hasEnoughResource() to instance method from static method

Change-Id: Ib6de1bfc2b7e65677c3685ac3c2f51391204d9cf
......@@ -39,7 +39,6 @@ import java.util.stream.Stream;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.MAX_RETRIES;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.RETRY_DELAY;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource;
class ConsistentContinuousResourceSubStore {
private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
......@@ -108,7 +107,7 @@ class ConsistentContinuousResourceSubStore {
return true;
}
return hasEnoughResource(allocation.value().original(), resource, allocation.value());
return allocation.value().hasEnoughResource(allocation.value().original(), resource);
}
<T> Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
......
......@@ -30,6 +30,10 @@ final class ContinuousResourceAllocation {
private final ContinuousResource original;
private final ImmutableList<ResourceAllocation> allocations;
static ContinuousResourceAllocation empty(ContinuousResource original) {
return new ContinuousResourceAllocation(original, ImmutableList.of());
}
ContinuousResourceAllocation(ContinuousResource original,
ImmutableList<ResourceAllocation> allocations) {
this.original = original;
......@@ -42,18 +46,11 @@ final class ContinuousResourceAllocation {
*
* @param original original resource
* @param request requested resource
* @param allocation current allocation of the resource
* @return true if there is enough resource volume. Otherwise, false.
*/
// computational complexity: O(n) where n is the number of allocations
static boolean hasEnoughResource(ContinuousResource original,
ContinuousResource request,
ContinuousResourceAllocation allocation) {
if (allocation == null) {
return request.value() <= original.value();
}
double allocated = allocation.allocations().stream()
boolean hasEnoughResource(ContinuousResource original, ContinuousResource request) {
double allocated = allocations.stream()
.filter(x -> x.resource() instanceof ContinuousResource)
.map(x -> (ContinuousResource) x.resource())
.mapToDouble(ContinuousResource::value)
......
......@@ -35,7 +35,6 @@ import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource;
class TransactionalContinuousResourceSubStore {
private final Logger log = LoggerFactory.getLogger(getClass());
......@@ -140,7 +139,9 @@ class TransactionalContinuousResourceSubStore {
// Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
ContinuousResource original = lookedUp.get();
ContinuousResourceAllocation allocations = consumers.get(request.id());
if (!hasEnoughResource(original, request, allocations)) {
if (!Optional.ofNullable(allocations)
.orElse(ContinuousResourceAllocation.empty(original))
.hasEnoughResource(original, request)) {
return false;
}
......