blob: ce01dd126fd13321d2d4dce839d5bbb8cec7e30b [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 */
Toshio Koide5c0a7262014-10-23 14:50:21 -070016package org.onlab.onos.net.resource.impl;
Toshio Koide50df38d2014-10-23 10:36:51 -070017
Toshio Koide106d4492014-10-28 11:22:02 -070018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
Toshio Koide50df38d2014-10-23 10:36:51 -070020import static org.slf4j.LoggerFactory.getLogger;
21
Toshio Koide106d4492014-10-28 11:22:02 -070022import java.util.Collections;
Toshio Koideca0fcff2014-10-23 14:08:36 -070023import java.util.HashMap;
Toshio Koide106d4492014-10-28 11:22:02 -070024import java.util.HashSet;
Toshio Koide9be539e2014-10-23 18:43:30 -070025import java.util.Iterator;
Toshio Koideca0fcff2014-10-23 14:08:36 -070026import java.util.Map;
27import java.util.Set;
28
Toshio Koide50df38d2014-10-23 10:36:51 -070029import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
Toshio Koide106d4492014-10-28 11:22:02 -070032import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
Toshio Koide50df38d2014-10-23 10:36:51 -070034import org.apache.felix.scr.annotations.Service;
35import org.onlab.onos.net.Link;
36import org.onlab.onos.net.intent.IntentId;
Toshio Koide5c0a7262014-10-23 14:50:21 -070037import org.onlab.onos.net.resource.BandwidthResourceAllocation;
38import org.onlab.onos.net.resource.BandwidthResourceRequest;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080039import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
Toshio Koide5c0a7262014-10-23 14:50:21 -070040import org.onlab.onos.net.resource.Lambda;
41import org.onlab.onos.net.resource.LambdaResourceAllocation;
Toshio Koide106d4492014-10-28 11:22:02 -070042import org.onlab.onos.net.resource.LambdaResourceRequest;
Toshio Koide5c0a7262014-10-23 14:50:21 -070043import org.onlab.onos.net.resource.LinkResourceAllocations;
44import org.onlab.onos.net.resource.LinkResourceRequest;
45import org.onlab.onos.net.resource.LinkResourceService;
Toshio Koide106d4492014-10-28 11:22:02 -070046import org.onlab.onos.net.resource.LinkResourceStore;
Toshio Koide5c0a7262014-10-23 14:50:21 -070047import org.onlab.onos.net.resource.ResourceAllocation;
48import org.onlab.onos.net.resource.ResourceRequest;
Toshio Koide106d4492014-10-28 11:22:02 -070049import org.onlab.onos.net.resource.ResourceType;
Toshio Koide50df38d2014-10-23 10:36:51 -070050import org.slf4j.Logger;
51
52/**
53 * Provides basic implementation of link resources allocation.
54 */
55@Component(immediate = true)
56@Service
57public class LinkResourceManager implements LinkResourceService {
58
59 private final Logger log = getLogger(getClass());
60
Toshio Koide106d4492014-10-28 11:22:02 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 private LinkResourceStore store;
Ray Milkeycaa450b2014-10-29 15:54:24 -070063
Toshio Koide50df38d2014-10-23 10:36:51 -070064 @Activate
65 public void activate() {
66 log.info("Started");
67 }
68
69 @Deactivate
70 public void deactivate() {
71 log.info("Stopped");
72 }
73
Toshio Koide106d4492014-10-28 11:22:02 -070074 /**
75 * Returns available lambdas on specified link.
76 *
77 * @param link the link
78 * @return available lambdas on specified link
79 */
80 private Set<Lambda> getAvailableLambdas(Link link) {
81 checkNotNull(link);
82 Set<ResourceAllocation> resAllocs = store.getFreeResources(link);
83 if (resAllocs == null) {
84 return Collections.emptySet();
85 }
86 Set<Lambda> lambdas = new HashSet<>();
87 for (ResourceAllocation res : resAllocs) {
88 if (res.type() == ResourceType.LAMBDA) {
89 lambdas.add(((LambdaResourceAllocation) res).lambda());
90 }
91 }
92 return lambdas;
Toshio Koide9be539e2014-10-23 18:43:30 -070093 }
94
Toshio Koide106d4492014-10-28 11:22:02 -070095 /**
96 * Returns available lambdas on specified links.
97 *
98 * @param links the links
99 * @return available lambdas on specified links
100 */
101 private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) {
102 checkNotNull(links);
103 Iterator<Link> i = links.iterator();
104 checkArgument(i.hasNext());
105 Set<Lambda> lambdas = new HashSet<>(getAvailableLambdas(i.next()));
106 while (i.hasNext()) {
107 lambdas.retainAll(getAvailableLambdas(i.next()));
108 }
109 return lambdas;
110 }
Ray Milkeycaa450b2014-10-29 15:54:24 -0700111
Toshio Koide50df38d2014-10-23 10:36:51 -0700112 @Override
113 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
Toshio Koide106d4492014-10-28 11:22:02 -0700114 // TODO Concatenate multiple bandwidth requests.
115 // TODO Support multiple lambda resource requests.
116 // TODO Throw appropriate exception.
Toshio Koideca0fcff2014-10-23 14:08:36 -0700117
Toshio Koide106d4492014-10-28 11:22:02 -0700118 Set<ResourceAllocation> allocs = new HashSet<>();
Toshio Koide9be539e2014-10-23 18:43:30 -0700119 for (ResourceRequest r : req.resources()) {
Toshio Koideca0fcff2014-10-23 14:08:36 -0700120 switch (r.type()) {
121 case BANDWIDTH:
Toshio Koideca0fcff2014-10-23 14:08:36 -0700122 BandwidthResourceRequest br = (BandwidthResourceRequest) r;
Toshio Koide106d4492014-10-28 11:22:02 -0700123 allocs.add(new BandwidthResourceAllocation(br.bandwidth()));
Toshio Koideca0fcff2014-10-23 14:08:36 -0700124 break;
125 case LAMBDA:
Toshio Koide106d4492014-10-28 11:22:02 -0700126 Iterator<Lambda> lambdaIterator =
127 getAvailableLambdas(req.links()).iterator();
Toshio Koide9be539e2014-10-23 18:43:30 -0700128 if (lambdaIterator.hasNext()) {
Toshio Koide106d4492014-10-28 11:22:02 -0700129 allocs.add(new LambdaResourceAllocation(lambdaIterator.next()));
130 } else {
131 log.info("Failed to allocate lambda resource.");
132 return null;
Toshio Koide9be539e2014-10-23 18:43:30 -0700133 }
Toshio Koideca0fcff2014-10-23 14:08:36 -0700134 break;
135 default:
136 break;
137 }
138 }
139
140 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
Toshio Koide9be539e2014-10-23 18:43:30 -0700141 for (Link link : req.links()) {
Toshio Koide106d4492014-10-28 11:22:02 -0700142 allocations.put(link, allocs);
Toshio Koideca0fcff2014-10-23 14:08:36 -0700143 }
Toshio Koide106d4492014-10-28 11:22:02 -0700144 LinkResourceAllocations result =
145 new DefaultLinkResourceAllocations(req, allocations);
146 store.allocateResources(result);
147 return result;
148
Toshio Koide50df38d2014-10-23 10:36:51 -0700149 }
150
151 @Override
152 public void releaseResources(LinkResourceAllocations allocations) {
Toshio Koide106d4492014-10-28 11:22:02 -0700153 store.releaseResources(allocations);
Toshio Koide50df38d2014-10-23 10:36:51 -0700154 }
155
156 @Override
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700157 public LinkResourceAllocations updateResources(LinkResourceRequest req,
Toshio Koide106d4492014-10-28 11:22:02 -0700158 LinkResourceAllocations oldAllocations) {
159 // TODO
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700160 return null;
161 }
162
163 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700164 public Iterable<LinkResourceAllocations> getAllocations() {
Toshio Koide106d4492014-10-28 11:22:02 -0700165 return store.getAllocations();
Toshio Koide50df38d2014-10-23 10:36:51 -0700166 }
167
168 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700169 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
Toshio Koide106d4492014-10-28 11:22:02 -0700170 return store.getAllocations(link);
Toshio Koide50df38d2014-10-23 10:36:51 -0700171 }
172
173 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700174 public LinkResourceAllocations getAllocations(IntentId intentId) {
Toshio Koide106d4492014-10-28 11:22:02 -0700175 return store.getAllocations(intentId);
Toshio Koide50df38d2014-10-23 10:36:51 -0700176 }
177
178 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700179 public Iterable<ResourceRequest> getAvailableResources(Link link) {
Toshio Koide106d4492014-10-28 11:22:02 -0700180 Set<ResourceAllocation> freeRes = store.getFreeResources(link);
181 Set<ResourceRequest> result = new HashSet<>();
182 for (ResourceAllocation alloc : freeRes) {
183 switch (alloc.type()) {
184 case BANDWIDTH:
185 result.add(new BandwidthResourceRequest(
186 ((BandwidthResourceAllocation) alloc).bandwidth()));
187 break;
188 case LAMBDA:
189 result.add(new LambdaResourceRequest());
190 break;
191 default:
192 break;
193 }
194 }
Ray Milkeycaa450b2014-10-29 15:54:24 -0700195 return result;
Toshio Koide50df38d2014-10-23 10:36:51 -0700196 }
197
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700198 @Override
199 public ResourceRequest getAvailableResources(Link link,
Toshio Koide106d4492014-10-28 11:22:02 -0700200 LinkResourceAllocations allocations) {
201 // TODO
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700202 return null;
203 }
204
Toshio Koide50df38d2014-10-23 10:36:51 -0700205}