Sho SHIMIZU

Move the resource release details into ContinuousResourceAllocation

Change-Id: I3abba6021d458424ff9961eafb3cc293c033aeef
......@@ -16,8 +16,14 @@
package org.onosproject.store.resource.impl;
import com.google.common.collect.ImmutableList;
import org.onlab.util.GuavaCollectors;
import org.onosproject.net.resource.ContinuousResource;
import org.onosproject.net.resource.ResourceAllocation;
import org.onosproject.net.resource.ResourceConsumerId;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
// internal use only
final class ContinuousResourceAllocation {
......@@ -37,4 +43,24 @@ final class ContinuousResourceAllocation {
ImmutableList<ResourceAllocation> allocations() {
return allocations;
}
ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) {
List<ResourceAllocation> nonMatched = allocations.stream()
.filter(x -> !(x.consumerId().equals(consumerId) &&
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
List<ResourceAllocation> matched = allocations.stream()
.filter(x -> (x.consumerId().equals(consumerId) &&
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
if (matched.size() > 1) {
matched.remove(0);
}
return new ContinuousResourceAllocation(original,
Stream.concat(nonMatched.stream(), matched.stream())
.collect(GuavaCollectors.toImmutableList()));
}
}
......
......@@ -17,7 +17,6 @@ package org.onosproject.store.resource.impl;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import org.onlab.util.GuavaCollectors;
import org.onosproject.net.resource.ContinuousResource;
import org.onosproject.net.resource.ContinuousResourceId;
import org.onosproject.net.resource.DiscreteResourceId;
......@@ -33,7 +32,6 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.google.common.base.Preconditions.checkArgument;
import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
......@@ -168,25 +166,8 @@ class TransactionalContinuousResourceStore {
boolean release(ContinuousResource resource, ResourceConsumerId consumerId) {
ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
ContinuousResourceAllocation newAllocation = oldAllocation.release(resource, consumerId);
List<ResourceAllocation> nonMatched = oldAllocation.allocations().stream()
.filter(x -> !(x.consumerId().equals(consumerId) &&
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
List<ResourceAllocation> matched = oldAllocation.allocations().stream()
.filter(x -> (x.consumerId().equals(consumerId) &&
((ContinuousResource) x.resource()).value() == resource.value()))
.collect(Collectors.toList());
if (matched.size() > 1) {
matched.remove(0);
}
ImmutableList<ResourceAllocation> finalAllocations = Stream.concat(nonMatched.stream(),
matched.stream()).collect(GuavaCollectors.toImmutableList());
return consumers.replace(resource.id(), oldAllocation,
new ContinuousResourceAllocation(oldAllocation.original(), finalAllocations));
return consumers.replace(resource.id(), oldAllocation, newAllocation);
}
}
......