blob: 0227d3f856c0e4e76b8a3c05e00a7210304620f6 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
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'Connor6de2e202015-05-21 14:30:41 -070016package org.onosproject.net.resource.link;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080017
18import static com.google.common.base.Preconditions.checkNotNull;
Toshio Koideca0fcff2014-10-23 14:08:36 -070019
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070020import com.google.common.base.MoreObjects;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080021import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.ImmutableSet;
23
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.Link;
25import org.onosproject.net.intent.IntentId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070026import org.onosproject.net.resource.ResourceAllocation;
27import org.onosproject.net.resource.ResourceRequest;
28import org.onosproject.net.resource.ResourceType;
Toshio Koideca0fcff2014-10-23 14:08:36 -070029
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070030import java.util.Collection;
31import java.util.Collections;
32import java.util.Map;
Ayaka Koshibee114f042015-05-01 11:43:00 -070033import java.util.Objects;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080034import java.util.Map.Entry;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070035import java.util.Set;
36
Toshio Koideca0fcff2014-10-23 14:08:36 -070037/**
38 * Implementation of {@link LinkResourceAllocations}.
39 */
40public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
41 private final LinkResourceRequest request;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080042 // TODO: probably should be using LinkKey instead
Toshio Koideca0fcff2014-10-23 14:08:36 -070043 private final Map<Link, Set<ResourceAllocation>> allocations;
44
45 /**
46 * Creates a new link resource allocations.
47 *
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070048 * @param request requested resources
Toshio Koideca0fcff2014-10-23 14:08:36 -070049 * @param allocations allocated resources
50 */
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080051 public DefaultLinkResourceAllocations(LinkResourceRequest request,
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070052 Map<Link, Set<ResourceAllocation>> allocations) {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080053 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 Koideca0fcff2014-10-23 14:08:36 -070060 }
61
62 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070063 public IntentId intentId() {
64 return request.intentId();
Toshio Koideca0fcff2014-10-23 14:08:36 -070065 }
66
67 @Override
68 public Collection<Link> links() {
69 return request.links();
70 }
71
72 @Override
73 public Set<ResourceRequest> resources() {
74 return request.resources();
75 }
76
77 @Override
78 public ResourceType type() {
79 return null;
80 }
81
82 @Override
83 public Set<ResourceAllocation> getResourceAllocation(Link link) {
84 Set<ResourceAllocation> result = allocations.get(link);
85 if (result == null) {
86 result = Collections.emptySet();
87 }
88 return result;
89 }
90
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070091 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070092 public int hashCode() {
93 return Objects.hash(request, allocations);
94 }
95
96 @Override
97 public boolean equals(Object obj) {
98 if (this == obj) {
99 return true;
100 }
101 if (obj == null || getClass() != obj.getClass()) {
102 return false;
103 }
104 final DefaultLinkResourceAllocations other = (DefaultLinkResourceAllocations) obj;
105 return Objects.equals(this.request, other.request)
106 && Objects.equals(this.allocations, other.allocations);
107 }
108
109 @Override
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700110 public String toString() {
111 return MoreObjects.toStringHelper(this)
112 .add("allocations", allocations)
113 .toString();
114 }
Toshio Koideca0fcff2014-10-23 14:08:36 -0700115}