blob: 03dc713dd508baadf35755dce1f3d38ab0a0a805 [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;
Jordan Halterman9eead3c2017-05-09 12:24:14 -070025import java.util.Objects;
Sho SHIMIZUb2183962016-05-13 15:03:50 -070026import java.util.stream.Collectors;
27import java.util.stream.Stream;
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070028
29// internal use only
30final class ContinuousResourceAllocation {
31 private final ContinuousResource original;
32 private final ImmutableList<ResourceAllocation> allocations;
33
Sho SHIMIZUdffe3b82016-05-13 15:44:57 -070034 static ContinuousResourceAllocation empty(ContinuousResource original) {
35 return new ContinuousResourceAllocation(original, ImmutableList.of());
36 }
37
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070038 ContinuousResourceAllocation(ContinuousResource original,
39 ImmutableList<ResourceAllocation> allocations) {
40 this.original = original;
41 this.allocations = allocations;
42 }
43
Sho SHIMIZU8fb4ad72016-05-13 15:53:44 -070044 // for serializer
45 private ContinuousResourceAllocation() {
46 this.original = null;
47 this.allocations = null;
48 }
49
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070050 /**
51 * Checks if there is enough resource volume to allocated the requested resource
52 * against the specified resource.
53 *
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070054 * @param request requested resource
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070055 * @return true if there is enough resource volume. Otherwise, false.
56 */
57 // computational complexity: O(n) where n is the number of allocations
Sho SHIMIZU38bcfcf2016-05-13 15:48:54 -070058 boolean hasEnoughResource(ContinuousResource request) {
Sho SHIMIZUdffe3b82016-05-13 15:44:57 -070059 double allocated = allocations.stream()
Sho SHIMIZU0a07c072016-05-13 15:12:58 -070060 .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 SHIMIZUdeb04422016-05-06 16:10:46 -070068 ImmutableList<ResourceAllocation> allocations() {
69 return allocations;
70 }
Sho SHIMIZUb2183962016-05-13 15:03:50 -070071
Sho SHIMIZU3120d822016-05-17 09:28:10 -070072 ContinuousResourceAllocation allocate(ResourceAllocation value) {
Sho SHIMIZUefb75112016-05-13 15:24:34 -070073 return new ContinuousResourceAllocation(original, ImmutableList.<ResourceAllocation>builder()
74 .addAll(allocations)
75 .add(value)
76 .build());
77 }
78
Sho SHIMIZUb2183962016-05-13 15:03:50 -070079 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 SHIMIZU99514fbb2016-07-05 14:57:09 -070090 if (!matched.isEmpty()) {
Sho SHIMIZUb2183962016-05-13 15:03:50 -070091 matched.remove(0);
92 }
93
94 return new ContinuousResourceAllocation(original,
95 Stream.concat(nonMatched.stream(), matched.stream())
96 .collect(GuavaCollectors.toImmutableList()));
97 }
Jordan Halterman9eead3c2017-05-09 12:24:14 -070098
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 SHIMIZUdeb04422016-05-06 16:10:46 -0700120}