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 | |
Sho SHIMIZU | dffe3b8 | 2016-05-13 15:44:57 -0700 | [diff] [blame] | 33 | static ContinuousResourceAllocation empty(ContinuousResource original) { |
| 34 | return new ContinuousResourceAllocation(original, ImmutableList.of()); |
| 35 | } |
| 36 | |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 37 | ContinuousResourceAllocation(ContinuousResource original, |
| 38 | ImmutableList<ResourceAllocation> allocations) { |
| 39 | this.original = original; |
| 40 | this.allocations = allocations; |
| 41 | } |
| 42 | |
Sho SHIMIZU | 8fb4ad7 | 2016-05-13 15:53:44 -0700 | [diff] [blame] | 43 | // for serializer |
| 44 | private ContinuousResourceAllocation() { |
| 45 | this.original = null; |
| 46 | this.allocations = null; |
| 47 | } |
| 48 | |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Checks if there is enough resource volume to allocated the requested resource |
| 51 | * against the specified resource. |
| 52 | * |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 53 | * @param request requested resource |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 54 | * @return true if there is enough resource volume. Otherwise, false. |
| 55 | */ |
| 56 | // computational complexity: O(n) where n is the number of allocations |
Sho SHIMIZU | 38bcfcf | 2016-05-13 15:48:54 -0700 | [diff] [blame] | 57 | boolean hasEnoughResource(ContinuousResource request) { |
Sho SHIMIZU | dffe3b8 | 2016-05-13 15:44:57 -0700 | [diff] [blame] | 58 | double allocated = allocations.stream() |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 59 | .filter(x -> x.resource() instanceof ContinuousResource) |
| 60 | .map(x -> (ContinuousResource) x.resource()) |
| 61 | .mapToDouble(ContinuousResource::value) |
| 62 | .sum(); |
| 63 | double left = original.value() - allocated; |
| 64 | return request.value() <= left; |
| 65 | } |
| 66 | |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 67 | ImmutableList<ResourceAllocation> allocations() { |
| 68 | return allocations; |
| 69 | } |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 70 | |
Sho SHIMIZU | 3120d82 | 2016-05-17 09:28:10 -0700 | [diff] [blame] | 71 | ContinuousResourceAllocation allocate(ResourceAllocation value) { |
Sho SHIMIZU | efb7511 | 2016-05-13 15:24:34 -0700 | [diff] [blame] | 72 | return new ContinuousResourceAllocation(original, ImmutableList.<ResourceAllocation>builder() |
| 73 | .addAll(allocations) |
| 74 | .add(value) |
| 75 | .build()); |
| 76 | } |
| 77 | |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 78 | ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) { |
| 79 | List<ResourceAllocation> nonMatched = allocations.stream() |
| 80 | .filter(x -> !(x.consumerId().equals(consumerId) && |
| 81 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 82 | .collect(Collectors.toList()); |
| 83 | |
| 84 | List<ResourceAllocation> matched = allocations.stream() |
| 85 | .filter(x -> (x.consumerId().equals(consumerId) && |
| 86 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 87 | .collect(Collectors.toList()); |
| 88 | |
Sho SHIMIZU | 99514fbb | 2016-07-05 14:57:09 -0700 | [diff] [blame] | 89 | if (!matched.isEmpty()) { |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 90 | matched.remove(0); |
| 91 | } |
| 92 | |
| 93 | return new ContinuousResourceAllocation(original, |
| 94 | Stream.concat(nonMatched.stream(), matched.stream()) |
| 95 | .collect(GuavaCollectors.toImmutableList())); |
| 96 | } |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 97 | } |