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; |
Jordan Halterman | 9eead3c | 2017-05-09 12:24:14 -0700 | [diff] [blame] | 25 | import java.util.Objects; |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 26 | import java.util.stream.Collectors; |
| 27 | import java.util.stream.Stream; |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 28 | |
| 29 | // internal use only |
| 30 | final class ContinuousResourceAllocation { |
| 31 | private final ContinuousResource original; |
| 32 | private final ImmutableList<ResourceAllocation> allocations; |
| 33 | |
Sho SHIMIZU | dffe3b8 | 2016-05-13 15:44:57 -0700 | [diff] [blame] | 34 | static ContinuousResourceAllocation empty(ContinuousResource original) { |
| 35 | return new ContinuousResourceAllocation(original, ImmutableList.of()); |
| 36 | } |
| 37 | |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 38 | ContinuousResourceAllocation(ContinuousResource original, |
| 39 | ImmutableList<ResourceAllocation> allocations) { |
| 40 | this.original = original; |
| 41 | this.allocations = allocations; |
| 42 | } |
| 43 | |
Sho SHIMIZU | 8fb4ad7 | 2016-05-13 15:53:44 -0700 | [diff] [blame] | 44 | // for serializer |
| 45 | private ContinuousResourceAllocation() { |
| 46 | this.original = null; |
| 47 | this.allocations = null; |
| 48 | } |
| 49 | |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Checks if there is enough resource volume to allocated the requested resource |
| 52 | * against the specified resource. |
| 53 | * |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 54 | * @param request requested resource |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 55 | * @return true if there is enough resource volume. Otherwise, false. |
| 56 | */ |
| 57 | // computational complexity: O(n) where n is the number of allocations |
Sho SHIMIZU | 38bcfcf | 2016-05-13 15:48:54 -0700 | [diff] [blame] | 58 | boolean hasEnoughResource(ContinuousResource request) { |
Sho SHIMIZU | dffe3b8 | 2016-05-13 15:44:57 -0700 | [diff] [blame] | 59 | double allocated = allocations.stream() |
Sho SHIMIZU | 0a07c07 | 2016-05-13 15:12:58 -0700 | [diff] [blame] | 60 | .filter(x -> x.resource() instanceof ContinuousResource) |
| 61 | .map(x -> (ContinuousResource) x.resource()) |
| 62 | .mapToDouble(ContinuousResource::value) |
| 63 | .sum(); |
| 64 | double left = original.value() - allocated; |
| 65 | return request.value() <= left; |
| 66 | } |
| 67 | |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 68 | ImmutableList<ResourceAllocation> allocations() { |
| 69 | return allocations; |
| 70 | } |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 71 | |
Sho SHIMIZU | 3120d82 | 2016-05-17 09:28:10 -0700 | [diff] [blame] | 72 | ContinuousResourceAllocation allocate(ResourceAllocation value) { |
Sho SHIMIZU | efb7511 | 2016-05-13 15:24:34 -0700 | [diff] [blame] | 73 | return new ContinuousResourceAllocation(original, ImmutableList.<ResourceAllocation>builder() |
| 74 | .addAll(allocations) |
| 75 | .add(value) |
| 76 | .build()); |
| 77 | } |
| 78 | |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 79 | ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) { |
| 80 | List<ResourceAllocation> nonMatched = allocations.stream() |
| 81 | .filter(x -> !(x.consumerId().equals(consumerId) && |
| 82 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 83 | .collect(Collectors.toList()); |
| 84 | |
| 85 | List<ResourceAllocation> matched = allocations.stream() |
| 86 | .filter(x -> (x.consumerId().equals(consumerId) && |
| 87 | ((ContinuousResource) x.resource()).value() == resource.value())) |
| 88 | .collect(Collectors.toList()); |
| 89 | |
Sho SHIMIZU | 99514fbb | 2016-07-05 14:57:09 -0700 | [diff] [blame] | 90 | if (!matched.isEmpty()) { |
Sho SHIMIZU | b218396 | 2016-05-13 15:03:50 -0700 | [diff] [blame] | 91 | matched.remove(0); |
| 92 | } |
| 93 | |
| 94 | return new ContinuousResourceAllocation(original, |
| 95 | Stream.concat(nonMatched.stream(), matched.stream()) |
| 96 | .collect(GuavaCollectors.toImmutableList())); |
| 97 | } |
Jordan Halterman | 9eead3c | 2017-05-09 12:24:14 -0700 | [diff] [blame] | 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object o) { |
| 101 | if (this == o) { |
| 102 | return true; |
| 103 | } |
| 104 | if (o == null || getClass() != o.getClass()) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | ContinuousResourceAllocation that = (ContinuousResourceAllocation) o; |
| 109 | |
| 110 | if (!original.equals(that.original)) { |
| 111 | return false; |
| 112 | } |
| 113 | return allocations.equals(that.allocations); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int hashCode() { |
| 118 | return Objects.hash(original, allocations); |
| 119 | } |
Sho SHIMIZU | deb0442 | 2016-05-06 16:10:46 -0700 | [diff] [blame] | 120 | } |