Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 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, |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 36 | ContinuousResourceAllocation allocation) { |
Sho SHIMIZU | 22fb283 | 2016-05-06 11:44:03 -0700 | [diff] [blame] | 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 | } |