blob: 3a2f66d2981de29167d6af5b205c4cb520264e96 [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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.resource;
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;
Toshio Koideca0fcff2014-10-23 14:08:36 -070026
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070027import java.util.Collection;
28import java.util.Collections;
29import java.util.Map;
Ayaka Koshibee114f042015-05-01 11:43:00 -070030import java.util.Objects;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080031import java.util.Map.Entry;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070032import java.util.Set;
33
Toshio Koideca0fcff2014-10-23 14:08:36 -070034/**
35 * Implementation of {@link LinkResourceAllocations}.
36 */
37public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
38 private final LinkResourceRequest request;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080039 // TODO: probably should be using LinkKey instead
Toshio Koideca0fcff2014-10-23 14:08:36 -070040 private final Map<Link, Set<ResourceAllocation>> allocations;
41
42 /**
43 * Creates a new link resource allocations.
44 *
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070045 * @param request requested resources
Toshio Koideca0fcff2014-10-23 14:08:36 -070046 * @param allocations allocated resources
47 */
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080048 public DefaultLinkResourceAllocations(LinkResourceRequest request,
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070049 Map<Link, Set<ResourceAllocation>> allocations) {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080050 this.request = checkNotNull(request);
51 ImmutableMap.Builder<Link, Set<ResourceAllocation>> builder
52 = ImmutableMap.builder();
53 for (Entry<Link, Set<ResourceAllocation>> e : allocations.entrySet()) {
54 builder.put(e.getKey(), ImmutableSet.copyOf(e.getValue()));
55 }
56 this.allocations = builder.build();
Toshio Koideca0fcff2014-10-23 14:08:36 -070057 }
58
59 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070060 public IntentId intentId() {
61 return request.intentId();
Toshio Koideca0fcff2014-10-23 14:08:36 -070062 }
63
64 @Override
65 public Collection<Link> links() {
66 return request.links();
67 }
68
69 @Override
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'Connorfe0f4b12014-10-30 21:19:02 -070088 @Override
Ayaka Koshibee114f042015-05-01 11:43:00 -070089 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'Connorfe0f4b12014-10-30 21:19:02 -0700107 public String toString() {
108 return MoreObjects.toStringHelper(this)
109 .add("allocations", allocations)
110 .toString();
111 }
Toshio Koideca0fcff2014-10-23 14:08:36 -0700112}