blob: 13446b059d0b94b8ed1fca6b9aee4f9e971dcb0b [file] [log] [blame]
Sho SHIMIZUdeb04422016-05-06 16:10:46 -07001/*
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 */
16package org.onosproject.store.resource.impl;
17
18import com.google.common.collect.ImmutableList;
Sho SHIMIZUb2183962016-05-13 15:03:50 -070019import org.onlab.util.GuavaCollectors;
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070020import org.onosproject.net.resource.ContinuousResource;
21import org.onosproject.net.resource.ResourceAllocation;
Sho SHIMIZUb2183962016-05-13 15:03:50 -070022import org.onosproject.net.resource.ResourceConsumerId;
23
24import java.util.List;
25import java.util.stream.Collectors;
26import java.util.stream.Stream;
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070027
28// internal use only
29final class ContinuousResourceAllocation {
30 private final ContinuousResource original;
31 private final ImmutableList<ResourceAllocation> allocations;
32
Sho SHIMIZUdffe3b82016-05-13 15:44:57 -070033 static ContinuousResourceAllocation empty(ContinuousResource original) {
34 return new ContinuousResourceAllocation(original, ImmutableList.of());
35 }
36
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070037 ContinuousResourceAllocation(ContinuousResource original,
38 ImmutableList<ResourceAllocation> allocations) {
39 this.original = original;
40 this.allocations = allocations;
41 }
42
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070043 /**
44 * Checks if there is enough resource volume to allocated the requested resource
45 * against the specified resource.
46 *
47 * @param original original resource
48 * @param request requested resource
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070049 * @return true if there is enough resource volume. Otherwise, false.
50 */
51 // computational complexity: O(n) where n is the number of allocations
Sho SHIMIZUdffe3b82016-05-13 15:44:57 -070052 boolean hasEnoughResource(ContinuousResource original, ContinuousResource request) {
53 double allocated = allocations.stream()
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070054 .filter(x -> x.resource() instanceof ContinuousResource)
55 .map(x -> (ContinuousResource) x.resource())
56 .mapToDouble(ContinuousResource::value)
57 .sum();
58 double left = original.value() - allocated;
59 return request.value() <= left;
60 }
61
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070062 ContinuousResource original() {
63 return original;
64 }
65
66 ImmutableList<ResourceAllocation> allocations() {
67 return allocations;
68 }
Sho SHIMIZUb2183962016-05-13 15:03:50 -070069
Sho SHIMIZUefb75112016-05-13 15:24:34 -070070 ContinuousResourceAllocation allocate(ContinuousResource original, ResourceAllocation value) {
71 return new ContinuousResourceAllocation(original, ImmutableList.<ResourceAllocation>builder()
72 .addAll(allocations)
73 .add(value)
74 .build());
75 }
76
Sho SHIMIZUb2183962016-05-13 15:03:50 -070077 ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) {
78 List<ResourceAllocation> nonMatched = allocations.stream()
79 .filter(x -> !(x.consumerId().equals(consumerId) &&
80 ((ContinuousResource) x.resource()).value() == resource.value()))
81 .collect(Collectors.toList());
82
83 List<ResourceAllocation> matched = allocations.stream()
84 .filter(x -> (x.consumerId().equals(consumerId) &&
85 ((ContinuousResource) x.resource()).value() == resource.value()))
86 .collect(Collectors.toList());
87
88 if (matched.size() > 1) {
89 matched.remove(0);
90 }
91
92 return new ContinuousResourceAllocation(original,
93 Stream.concat(nonMatched.stream(), matched.stream())
94 .collect(GuavaCollectors.toImmutableList()));
95 }
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070096}