Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -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 com.google.common.collect.ImmutableList; |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 19 | import org.onlab.util.GuavaCollectors; |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 20 | import org.onosproject.net.resource.ContinuousResource; |
| 21 | import org.onosproject.net.resource.ResourceAllocation; |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 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; |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 27 | |
| 28 | // internal use only |
| 29 | final class ContinuousResourceAllocation { |
| 30 | private final ContinuousResource original; |
| 31 | private final ImmutableList<ResourceAllocation> allocations; |
| 32 | |
| 33 | ContinuousResourceAllocation(ContinuousResource original, |
| 34 | ImmutableList<ResourceAllocation> allocations) { |
| 35 | this.original = original; |
| 36 | this.allocations = allocations; |
| 37 | } |
| 38 | |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame^] | 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 | |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 65 | ContinuousResource original() { |
| 66 | return original; |
| 67 | } |
| 68 | |
| 69 | ImmutableList<ResourceAllocation> allocations() { |
| 70 | return allocations; |
| 71 | } |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 72 | |
| 73 | ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) { |
| 74 | List<ResourceAllocation> nonMatched = allocations.stream() |
| 75 | .filter(x -> !(x.consumerId().equals(consumerId) && |
| 76 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 77 | .collect(Collectors.toList()); |
| 78 | |
| 79 | List<ResourceAllocation> matched = allocations.stream() |
| 80 | .filter(x -> (x.consumerId().equals(consumerId) && |
| 81 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 82 | .collect(Collectors.toList()); |
| 83 | |
| 84 | if (matched.size() > 1) { |
| 85 | matched.remove(0); |
| 86 | } |
| 87 | |
| 88 | return new ContinuousResourceAllocation(original, |
| 89 | Stream.concat(nonMatched.stream(), matched.stream()) |
| 90 | .collect(GuavaCollectors.toImmutableList())); |
| 91 | } |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 92 | } |