Sho SHIMIZU

Change hasEnoughResource() to instance method from static method

Change-Id: Ib6de1bfc2b7e65677c3685ac3c2f51391204d9cf
...@@ -39,7 +39,6 @@ import java.util.stream.Stream; ...@@ -39,7 +39,6 @@ import java.util.stream.Stream;
39 import static org.onosproject.store.resource.impl.ConsistentResourceStore.MAX_RETRIES; 39 import static org.onosproject.store.resource.impl.ConsistentResourceStore.MAX_RETRIES;
40 import static org.onosproject.store.resource.impl.ConsistentResourceStore.RETRY_DELAY; 40 import static org.onosproject.store.resource.impl.ConsistentResourceStore.RETRY_DELAY;
41 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; 41 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
42 -import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource;
43 42
44 class ConsistentContinuousResourceSubStore { 43 class ConsistentContinuousResourceSubStore {
45 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers; 44 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
...@@ -108,7 +107,7 @@ class ConsistentContinuousResourceSubStore { ...@@ -108,7 +107,7 @@ class ConsistentContinuousResourceSubStore {
108 return true; 107 return true;
109 } 108 }
110 109
111 - return hasEnoughResource(allocation.value().original(), resource, allocation.value()); 110 + return allocation.value().hasEnoughResource(allocation.value().original(), resource);
112 } 111 }
113 112
114 <T> Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) { 113 <T> Stream<ContinuousResource> getAllocatedResources(DiscreteResourceId parent, Class<T> cls) {
......
...@@ -30,6 +30,10 @@ final class ContinuousResourceAllocation { ...@@ -30,6 +30,10 @@ final class ContinuousResourceAllocation {
30 private final ContinuousResource original; 30 private final ContinuousResource original;
31 private final ImmutableList<ResourceAllocation> allocations; 31 private final ImmutableList<ResourceAllocation> allocations;
32 32
33 + static ContinuousResourceAllocation empty(ContinuousResource original) {
34 + return new ContinuousResourceAllocation(original, ImmutableList.of());
35 + }
36 +
33 ContinuousResourceAllocation(ContinuousResource original, 37 ContinuousResourceAllocation(ContinuousResource original,
34 ImmutableList<ResourceAllocation> allocations) { 38 ImmutableList<ResourceAllocation> allocations) {
35 this.original = original; 39 this.original = original;
...@@ -42,18 +46,11 @@ final class ContinuousResourceAllocation { ...@@ -42,18 +46,11 @@ final class ContinuousResourceAllocation {
42 * 46 *
43 * @param original original resource 47 * @param original original resource
44 * @param request requested resource 48 * @param request requested resource
45 - * @param allocation current allocation of the resource
46 * @return true if there is enough resource volume. Otherwise, false. 49 * @return true if there is enough resource volume. Otherwise, false.
47 */ 50 */
48 // computational complexity: O(n) where n is the number of allocations 51 // computational complexity: O(n) where n is the number of allocations
49 - static boolean hasEnoughResource(ContinuousResource original, 52 + boolean hasEnoughResource(ContinuousResource original, ContinuousResource request) {
50 - ContinuousResource request, 53 + double allocated = allocations.stream()
51 - ContinuousResourceAllocation allocation) {
52 - if (allocation == null) {
53 - return request.value() <= original.value();
54 - }
55 -
56 - double allocated = allocation.allocations().stream()
57 .filter(x -> x.resource() instanceof ContinuousResource) 54 .filter(x -> x.resource() instanceof ContinuousResource)
58 .map(x -> (ContinuousResource) x.resource()) 55 .map(x -> (ContinuousResource) x.resource())
59 .mapToDouble(ContinuousResource::value) 56 .mapToDouble(ContinuousResource::value)
......
...@@ -35,7 +35,6 @@ import java.util.stream.Collectors; ...@@ -35,7 +35,6 @@ import java.util.stream.Collectors;
35 35
36 import static com.google.common.base.Preconditions.checkArgument; 36 import static com.google.common.base.Preconditions.checkArgument;
37 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER; 37 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
38 -import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource;
39 38
40 class TransactionalContinuousResourceSubStore { 39 class TransactionalContinuousResourceSubStore {
41 private final Logger log = LoggerFactory.getLogger(getClass()); 40 private final Logger log = LoggerFactory.getLogger(getClass());
...@@ -140,7 +139,9 @@ class TransactionalContinuousResourceSubStore { ...@@ -140,7 +139,9 @@ class TransactionalContinuousResourceSubStore {
140 // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId 139 // Down cast: this must be safe as ContinuousResource is associated with ContinuousResourceId
141 ContinuousResource original = lookedUp.get(); 140 ContinuousResource original = lookedUp.get();
142 ContinuousResourceAllocation allocations = consumers.get(request.id()); 141 ContinuousResourceAllocation allocations = consumers.get(request.id());
143 - if (!hasEnoughResource(original, request, allocations)) { 142 + if (!Optional.ofNullable(allocations)
143 + .orElse(ContinuousResourceAllocation.empty(original))
144 + .hasEnoughResource(original, request)) {
144 return false; 145 return false;
145 } 146 }
146 147
......