Move the resource release details into ContinuousResourceAllocation
Change-Id: I3abba6021d458424ff9961eafb3cc293c033aeef
Showing
2 changed files
with
28 additions
and
21 deletions
... | @@ -16,8 +16,14 @@ | ... | @@ -16,8 +16,14 @@ |
16 | package org.onosproject.store.resource.impl; | 16 | package org.onosproject.store.resource.impl; |
17 | 17 | ||
18 | import com.google.common.collect.ImmutableList; | 18 | import com.google.common.collect.ImmutableList; |
19 | +import org.onlab.util.GuavaCollectors; | ||
19 | import org.onosproject.net.resource.ContinuousResource; | 20 | import org.onosproject.net.resource.ContinuousResource; |
20 | import org.onosproject.net.resource.ResourceAllocation; | 21 | import org.onosproject.net.resource.ResourceAllocation; |
22 | +import org.onosproject.net.resource.ResourceConsumerId; | ||
23 | + | ||
24 | +import java.util.List; | ||
25 | +import java.util.stream.Collectors; | ||
26 | +import java.util.stream.Stream; | ||
21 | 27 | ||
22 | // internal use only | 28 | // internal use only |
23 | final class ContinuousResourceAllocation { | 29 | final class ContinuousResourceAllocation { |
... | @@ -37,4 +43,24 @@ final class ContinuousResourceAllocation { | ... | @@ -37,4 +43,24 @@ final class ContinuousResourceAllocation { |
37 | ImmutableList<ResourceAllocation> allocations() { | 43 | ImmutableList<ResourceAllocation> allocations() { |
38 | return allocations; | 44 | return allocations; |
39 | } | 45 | } |
46 | + | ||
47 | + ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) { | ||
48 | + List<ResourceAllocation> nonMatched = allocations.stream() | ||
49 | + .filter(x -> !(x.consumerId().equals(consumerId) && | ||
50 | + ((ContinuousResource) x.resource()).value() == resource.value())) | ||
51 | + .collect(Collectors.toList()); | ||
52 | + | ||
53 | + List<ResourceAllocation> matched = allocations.stream() | ||
54 | + .filter(x -> (x.consumerId().equals(consumerId) && | ||
55 | + ((ContinuousResource) x.resource()).value() == resource.value())) | ||
56 | + .collect(Collectors.toList()); | ||
57 | + | ||
58 | + if (matched.size() > 1) { | ||
59 | + matched.remove(0); | ||
60 | + } | ||
61 | + | ||
62 | + return new ContinuousResourceAllocation(original, | ||
63 | + Stream.concat(nonMatched.stream(), matched.stream()) | ||
64 | + .collect(GuavaCollectors.toImmutableList())); | ||
65 | + } | ||
40 | } | 66 | } | ... | ... |
... | @@ -17,7 +17,6 @@ package org.onosproject.store.resource.impl; | ... | @@ -17,7 +17,6 @@ package org.onosproject.store.resource.impl; |
17 | 17 | ||
18 | import com.google.common.collect.ImmutableList; | 18 | import com.google.common.collect.ImmutableList; |
19 | import com.google.common.collect.Sets; | 19 | import com.google.common.collect.Sets; |
20 | -import org.onlab.util.GuavaCollectors; | ||
21 | import org.onosproject.net.resource.ContinuousResource; | 20 | import org.onosproject.net.resource.ContinuousResource; |
22 | import org.onosproject.net.resource.ContinuousResourceId; | 21 | import org.onosproject.net.resource.ContinuousResourceId; |
23 | import org.onosproject.net.resource.DiscreteResourceId; | 22 | import org.onosproject.net.resource.DiscreteResourceId; |
... | @@ -33,7 +32,6 @@ import java.util.List; | ... | @@ -33,7 +32,6 @@ import java.util.List; |
33 | import java.util.Optional; | 32 | import java.util.Optional; |
34 | import java.util.Set; | 33 | import java.util.Set; |
35 | import java.util.stream.Collectors; | 34 | import java.util.stream.Collectors; |
36 | -import java.util.stream.Stream; | ||
37 | 35 | ||
38 | import static com.google.common.base.Preconditions.checkArgument; | 36 | import static com.google.common.base.Preconditions.checkArgument; |
39 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; | 37 | import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; |
... | @@ -168,25 +166,8 @@ class TransactionalContinuousResourceStore { | ... | @@ -168,25 +166,8 @@ class TransactionalContinuousResourceStore { |
168 | 166 | ||
169 | boolean release(ContinuousResource resource, ResourceConsumerId consumerId) { | 167 | boolean release(ContinuousResource resource, ResourceConsumerId consumerId) { |
170 | ContinuousResourceAllocation oldAllocation = consumers.get(resource.id()); | 168 | ContinuousResourceAllocation oldAllocation = consumers.get(resource.id()); |
169 | + ContinuousResourceAllocation newAllocation = oldAllocation.release(resource, consumerId); | ||
171 | 170 | ||
172 | - List<ResourceAllocation> nonMatched = oldAllocation.allocations().stream() | 171 | + return consumers.replace(resource.id(), oldAllocation, newAllocation); |
173 | - .filter(x -> !(x.consumerId().equals(consumerId) && | ||
174 | - ((ContinuousResource) x.resource()).value() == resource.value())) | ||
175 | - .collect(Collectors.toList()); | ||
176 | - | ||
177 | - List<ResourceAllocation> matched = oldAllocation.allocations().stream() | ||
178 | - .filter(x -> (x.consumerId().equals(consumerId) && | ||
179 | - ((ContinuousResource) x.resource()).value() == resource.value())) | ||
180 | - .collect(Collectors.toList()); | ||
181 | - | ||
182 | - if (matched.size() > 1) { | ||
183 | - matched.remove(0); | ||
184 | - } | ||
185 | - | ||
186 | - ImmutableList<ResourceAllocation> finalAllocations = Stream.concat(nonMatched.stream(), | ||
187 | - matched.stream()).collect(GuavaCollectors.toImmutableList()); | ||
188 | - | ||
189 | - return consumers.replace(resource.id(), oldAllocation, | ||
190 | - new ContinuousResourceAllocation(oldAllocation.original(), finalAllocations)); | ||
191 | } | 172 | } |
192 | } | 173 | } | ... | ... |
-
Please register or login to post a comment