blob: 51515e7116d71bbf215bec2744ba5855ea8560b1 [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 */
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080016package org.onlab.onos.net.resource;
17
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
Toshio Koideca0fcff2014-10-23 14:08:36 -070024import org.onlab.onos.net.Link;
25import org.onlab.onos.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;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080030import java.util.Map.Entry;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070031import java.util.Set;
32
Toshio Koideca0fcff2014-10-23 14:08:36 -070033/**
34 * Implementation of {@link LinkResourceAllocations}.
35 */
36public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
37 private final LinkResourceRequest request;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080038 // TODO: probably should be using LinkKey instead
Toshio Koideca0fcff2014-10-23 14:08:36 -070039 private final Map<Link, Set<ResourceAllocation>> allocations;
40
41 /**
42 * Creates a new link resource allocations.
43 *
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070044 * @param request requested resources
Toshio Koideca0fcff2014-10-23 14:08:36 -070045 * @param allocations allocated resources
46 */
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080047 public DefaultLinkResourceAllocations(LinkResourceRequest request,
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070048 Map<Link, Set<ResourceAllocation>> allocations) {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080049 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 Koideca0fcff2014-10-23 14:08:36 -070056 }
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'Connorfe0f4b12014-10-30 21:19:02 -070087 @Override
88 public String toString() {
89 return MoreObjects.toStringHelper(this)
90 .add("allocations", allocations)
91 .toString();
92 }
Toshio Koideca0fcff2014-10-23 14:08:36 -070093}