Sho SHIMIZU

Move hasEnoughResource() to ContinuousResourceAllocation

Change-Id: I3104d115a57876a75c699f4f1a46aa7bdf66d484
...@@ -39,7 +39,7 @@ import java.util.stream.Stream; ...@@ -39,7 +39,7 @@ 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.ResourceStoreUtil.hasEnoughResource; 42 +import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource;
43 43
44 class ConsistentContinuousResourceStore { 44 class ConsistentContinuousResourceStore {
45 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers; 45 private ConsistentMap<ContinuousResourceId, ContinuousResourceAllocation> consumers;
......
...@@ -36,6 +36,32 @@ final class ContinuousResourceAllocation { ...@@ -36,6 +36,32 @@ final class ContinuousResourceAllocation {
36 this.allocations = allocations; 36 this.allocations = allocations;
37 } 37 }
38 38
39 + /**
40 + * Checks if there is enough resource volume to allocated the requested resource
41 + * against the specified resource.
42 + *
43 + * @param original original resource
44 + * @param request requested resource
45 + * @param allocation current allocation of the resource
46 + * @return true if there is enough resource volume. Otherwise, false.
47 + */
48 + // computational complexity: O(n) where n is the number of allocations
49 + static boolean hasEnoughResource(ContinuousResource original,
50 + ContinuousResource request,
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)
58 + .map(x -> (ContinuousResource) x.resource())
59 + .mapToDouble(ContinuousResource::value)
60 + .sum();
61 + double left = original.value() - allocated;
62 + return request.value() <= left;
63 + }
64 +
39 ContinuousResource original() { 65 ContinuousResource original() {
40 return original; 66 return original;
41 } 67 }
......
1 -/*
2 - * Copyright 2016-present Open Networking Laboratory
3 - *
4 - * Licensed under the Apache License, Version 2.0 (the "License");
5 - * you may not use this file except in compliance with the License.
6 - * You may obtain a copy of the License at
7 - *
8 - * http://www.apache.org/licenses/LICENSE-2.0
9 - *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS,
12 - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 - * See the License for the specific language governing permissions and
14 - * limitations under the License.
15 - */
16 -package org.onosproject.store.resource.impl;
17 -
18 -import org.onosproject.net.resource.ContinuousResource;
19 -
20 -final class ResourceStoreUtil {
21 - // prohibit construction
22 - private ResourceStoreUtil() {}
23 -
24 - /**
25 - * Checks if there is enough resource volume to allocated the requested resource
26 - * against the specified resource.
27 - *
28 - * @param original original resource
29 - * @param request requested resource
30 - * @param allocation current allocation of the resource
31 - * @return true if there is enough resource volume. Otherwise, false.
32 - */
33 - // computational complexity: O(n) where n is the number of allocations
34 - static boolean hasEnoughResource(ContinuousResource original,
35 - ContinuousResource request,
36 - ContinuousResourceAllocation allocation) {
37 - if (allocation == null) {
38 - return request.value() <= original.value();
39 - }
40 -
41 - double allocated = allocation.allocations().stream()
42 - .filter(x -> x.resource() instanceof ContinuousResource)
43 - .map(x -> (ContinuousResource) x.resource())
44 - .mapToDouble(ContinuousResource::value)
45 - .sum();
46 - double left = original.value() - allocated;
47 - return request.value() <= left;
48 - }
49 -}
...@@ -35,7 +35,7 @@ import java.util.stream.Collectors; ...@@ -35,7 +35,7 @@ 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.ResourceStoreUtil.hasEnoughResource; 38 +import static org.onosproject.store.resource.impl.ContinuousResourceAllocation.hasEnoughResource;
39 39
40 class TransactionalContinuousResourceStore { 40 class TransactionalContinuousResourceStore {
41 private final Logger log = LoggerFactory.getLogger(getClass()); 41 private final Logger log = LoggerFactory.getLogger(getClass());
......