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 | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 16 | package org.onosproject.net.resource.link; |
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; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 26 | import org.onosproject.net.resource.ResourceAllocation; |
| 27 | import org.onosproject.net.resource.ResourceRequest; |
| 28 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 29 | |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 30 | import java.util.Collection; |
| 31 | import java.util.Collections; |
| 32 | import java.util.Map; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 33 | import java.util.Objects; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 34 | import java.util.Map.Entry; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 35 | import java.util.Set; |
| 36 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Implementation of {@link LinkResourceAllocations}. |
| 39 | */ |
| 40 | public class DefaultLinkResourceAllocations implements LinkResourceAllocations { |
| 41 | private final LinkResourceRequest request; |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 42 | // TODO: probably should be using LinkKey instead |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 43 | private final Map<Link, Set<ResourceAllocation>> allocations; |
| 44 | |
| 45 | /** |
| 46 | * Creates a new link resource allocations. |
| 47 | * |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 48 | * @param request requested resources |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 49 | * @param allocations allocated resources |
| 50 | */ |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 51 | public DefaultLinkResourceAllocations(LinkResourceRequest request, |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 52 | Map<Link, Set<ResourceAllocation>> allocations) { |
Yuta HIGUCHI | adac04a | 2014-11-13 00:02:45 -0800 | [diff] [blame] | 53 | this.request = checkNotNull(request); |
| 54 | ImmutableMap.Builder<Link, Set<ResourceAllocation>> builder |
| 55 | = ImmutableMap.builder(); |
| 56 | for (Entry<Link, Set<ResourceAllocation>> e : allocations.entrySet()) { |
| 57 | builder.put(e.getKey(), ImmutableSet.copyOf(e.getValue())); |
| 58 | } |
| 59 | this.allocations = builder.build(); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 62 | public IntentId intentId() { |
| 63 | return request.intentId(); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 66 | public Collection<Link> links() { |
| 67 | return request.links(); |
| 68 | } |
| 69 | |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 70 | public Set<ResourceRequest> resources() { |
| 71 | return request.resources(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public ResourceType type() { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public Set<ResourceAllocation> getResourceAllocation(Link link) { |
| 81 | Set<ResourceAllocation> result = allocations.get(link); |
| 82 | if (result == null) { |
| 83 | result = Collections.emptySet(); |
| 84 | } |
| 85 | return result; |
| 86 | } |
| 87 | |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 88 | @Override |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 89 | public int hashCode() { |
| 90 | return Objects.hash(request, allocations); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object obj) { |
| 95 | if (this == obj) { |
| 96 | return true; |
| 97 | } |
| 98 | if (obj == null || getClass() != obj.getClass()) { |
| 99 | return false; |
| 100 | } |
| 101 | final DefaultLinkResourceAllocations other = (DefaultLinkResourceAllocations) obj; |
| 102 | return Objects.equals(this.request, other.request) |
| 103 | && Objects.equals(this.allocations, other.allocations); |
| 104 | } |
| 105 | |
| 106 | @Override |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 107 | public String toString() { |
| 108 | return MoreObjects.toStringHelper(this) |
| 109 | .add("allocations", allocations) |
| 110 | .toString(); |
| 111 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 112 | } |