blob: 16488f1835c29b15a08b3eb125b136b1bf30203d [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
33 ContinuousResourceAllocation(ContinuousResource original,
34 ImmutableList<ResourceAllocation> allocations) {
35 this.original = original;
36 this.allocations = allocations;
37 }
38
39 ContinuousResource original() {
40 return original;
41 }
42
43 ImmutableList<ResourceAllocation> allocations() {
44 return allocations;
45 }
Sho SHIMIZUb2183962016-05-13 15:03:50 -070046
47 ContinuousResourceAllocation release(ContinuousResource resource, ResourceConsumerId consumerId) {
48 List<ResourceAllocation> nonMatched = allocations.stream()
49 .filter(x -> !(x.consumerId().equals(consumerId) &&
50 ((ContinuousResource) x.resource()).value() == resource.value()))
51 .collect(Collectors.toList());
52
53 List<ResourceAllocation> matched = allocations.stream()
54 .filter(x -> (x.consumerId().equals(consumerId) &&
55 ((ContinuousResource) x.resource()).value() == resource.value()))
56 .collect(Collectors.toList());
57
58 if (matched.size() > 1) {
59 matched.remove(0);
60 }
61
62 return new ContinuousResourceAllocation(original,
63 Stream.concat(nonMatched.stream(), matched.stream())
64 .collect(GuavaCollectors.toImmutableList()));
65 }
Sho SHIMIZUdeb04422016-05-06 16:10:46 -070066}