Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 19 | |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.intent.IntentId; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 26 | |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 27 | import java.util.Collection; |
| 28 | import java.util.Collections; |
| 29 | import java.util.Map; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 30 | import java.util.Map.Entry; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Implementation of {@link LinkResourceAllocations}. |
| 35 | */ |
| 36 | public class DefaultLinkResourceAllocations implements LinkResourceAllocations { |
| 37 | private final LinkResourceRequest request; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 38 | // TODO: probably should be using LinkKey instead |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 39 | private final Map<Link, Set<ResourceAllocation>> allocations; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new link resource allocations. |
| 43 | * |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 44 | * @param request requested resources |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 45 | * @param allocations allocated resources |
| 46 | */ |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 47 | public DefaultLinkResourceAllocations(LinkResourceRequest request, |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 48 | Map<Link, Set<ResourceAllocation>> allocations) { |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 49 | this.request = checkNotNull(request); |
| 50 | ImmutableMap.Builder<Link, Set<ResourceAllocation>> builder |
| 51 | = ImmutableMap.builder(); |
| 52 | for (Entry<Link, Set<ResourceAllocation>> e : allocations.entrySet()) { |
| 53 | builder.put(e.getKey(), ImmutableSet.copyOf(e.getValue())); |
| 54 | } |
| 55 | this.allocations = builder.build(); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public IntentId intendId() { |
| 60 | return request.intendId(); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Collection<Link> links() { |
| 65 | return request.links(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public Set<ResourceRequest> resources() { |
| 70 | return request.resources(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public ResourceType type() { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Set<ResourceAllocation> getResourceAllocation(Link link) { |
| 80 | Set<ResourceAllocation> result = allocations.get(link); |
| 81 | if (result == null) { |
| 82 | result = Collections.emptySet(); |
| 83 | } |
| 84 | return result; |
| 85 | } |
| 86 | |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public String toString() { |
| 89 | return MoreObjects.toStringHelper(this) |
| 90 | .add("allocations", allocations) |
| 91 | .toString(); |
| 92 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 93 | } |