blob: 7eb189e57bdd841e5487965ea70714fde99006da [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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.impl;
Toshio Koide50df38d2014-10-23 10:36:51 -070017
Sho SHIMIZU716c8e92015-07-02 11:35:34 -070018import com.google.common.collect.Sets;
Toshio Koide50df38d2014-10-23 10:36:51 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Toshio Koide106d4492014-10-28 11:22:02 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Toshio Koide50df38d2014-10-23 10:36:51 -070024import org.apache.felix.scr.annotations.Service;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070025import org.onosproject.event.AbstractListenerManager;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.Link;
27import org.onosproject.net.intent.IntentId;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070028import org.onosproject.net.resource.ResourceAllocation;
29import org.onosproject.net.resource.ResourceRequest;
30import org.onosproject.net.resource.ResourceType;
Brian O'Connor6de2e202015-05-21 14:30:41 -070031import org.onosproject.net.resource.link.BandwidthResourceAllocation;
32import org.onosproject.net.resource.link.BandwidthResourceRequest;
33import org.onosproject.net.resource.link.DefaultLinkResourceAllocations;
Brian O'Connor6de2e202015-05-21 14:30:41 -070034import org.onosproject.net.resource.link.LambdaResourceAllocation;
35import org.onosproject.net.resource.link.LambdaResourceRequest;
36import org.onosproject.net.resource.link.LinkResourceAllocations;
37import org.onosproject.net.resource.link.LinkResourceEvent;
38import org.onosproject.net.resource.link.LinkResourceListener;
39import org.onosproject.net.resource.link.LinkResourceRequest;
40import org.onosproject.net.resource.link.LinkResourceService;
41import org.onosproject.net.resource.link.LinkResourceStore;
42import org.onosproject.net.resource.link.LinkResourceStoreDelegate;
43import org.onosproject.net.resource.link.MplsLabel;
44import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
45import org.onosproject.net.resource.link.MplsLabelResourceRequest;
Toshio Koide50df38d2014-10-23 10:36:51 -070046import org.slf4j.Logger;
47
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080048import java.util.HashMap;
49import java.util.HashSet;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080050import java.util.Map;
Sho SHIMIZU81cdc392015-10-27 15:18:49 -070051import java.util.Optional;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080052import java.util.Set;
53
Changhoon Yoon541ef712015-05-23 17:18:34 +090054import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070055import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090056import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090057
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -080058
Toshio Koide50df38d2014-10-23 10:36:51 -070059/**
60 * Provides basic implementation of link resources allocation.
61 */
62@Component(immediate = true)
63@Service
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070064public class LinkResourceManager
65 extends AbstractListenerManager<LinkResourceEvent, LinkResourceListener>
66 implements LinkResourceService {
Toshio Koide50df38d2014-10-23 10:36:51 -070067
68 private final Logger log = getLogger(getClass());
69
Toshio Koide106d4492014-10-28 11:22:02 -070070 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 private LinkResourceStore store;
Ray Milkeycaa450b2014-10-29 15:54:24 -070072
Toshio Koide50df38d2014-10-23 10:36:51 -070073 @Activate
74 public void activate() {
Ray Milkeye97ede92014-11-20 10:43:12 -080075 eventDispatcher.addSink(LinkResourceEvent.class, listenerRegistry);
Toshio Koide50df38d2014-10-23 10:36:51 -070076 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070081 eventDispatcher.removeSink(LinkResourceEvent.class);
Toshio Koide50df38d2014-10-23 10:36:51 -070082 log.info("Stopped");
83 }
84
Michele Santuari4b6019e2014-12-19 11:31:45 +010085
Toshio Koide50df38d2014-10-23 10:36:51 -070086 @Override
87 public LinkResourceAllocations requestResources(LinkResourceRequest req) {
Changhoon Yoonb856b812015-08-10 03:47:19 +090088 checkPermission(LINK_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +090089
Toshio Koide106d4492014-10-28 11:22:02 -070090 // TODO Concatenate multiple bandwidth requests.
91 // TODO Support multiple lambda resource requests.
92 // TODO Throw appropriate exception.
Toshio Koide106d4492014-10-28 11:22:02 -070093 Set<ResourceAllocation> allocs = new HashSet<>();
Michele Santuari4b6019e2014-12-19 11:31:45 +010094 Map<Link, Set<ResourceAllocation>> allocsPerLink = new HashMap<>();
Toshio Koide9be539e2014-10-23 18:43:30 -070095 for (ResourceRequest r : req.resources()) {
Toshio Koideca0fcff2014-10-23 14:08:36 -070096 switch (r.type()) {
97 case BANDWIDTH:
Toshio Koideca0fcff2014-10-23 14:08:36 -070098 BandwidthResourceRequest br = (BandwidthResourceRequest) r;
Toshio Koide106d4492014-10-28 11:22:02 -070099 allocs.add(new BandwidthResourceAllocation(br.bandwidth()));
Toshio Koideca0fcff2014-10-23 14:08:36 -0700100 break;
101 case LAMBDA:
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700102 LambdaResourceRequest lr = (LambdaResourceRequest) r;
103 allocs.add(new LambdaResourceAllocation(lr.lambda()));
Toshio Koideca0fcff2014-10-23 14:08:36 -0700104 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100105 case MPLS_LABEL:
106 for (Link link : req.links()) {
107 if (allocsPerLink.get(link) == null) {
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700108 allocsPerLink.put(link, new HashSet<>());
Michele Santuari4b6019e2014-12-19 11:31:45 +0100109 }
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700110
111 Optional<MplsLabel> label = req.resources(link).stream()
112 .filter(x -> x.type() == ResourceType.MPLS_LABEL)
113 .map(x -> (MplsLabelResourceRequest) x)
114 .map(MplsLabelResourceRequest::mplsLabel)
115 .findFirst();
116
117 if (label.isPresent()) {
118 allocsPerLink.get(link).add(new MplsLabelResourceAllocation(label.get()));
Michele Santuari4b6019e2014-12-19 11:31:45 +0100119 } else {
120 log.info("Failed to allocate MPLS resource.");
121 break;
122 }
123 }
124 break;
Toshio Koideca0fcff2014-10-23 14:08:36 -0700125 default:
126 break;
127 }
128 }
129
130 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
Toshio Koide9be539e2014-10-23 18:43:30 -0700131 for (Link link : req.links()) {
Sho SHIMIZUd88db6f2015-09-09 14:22:06 -0700132 allocations.put(link, new HashSet<>(allocs));
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800133 Set<ResourceAllocation> linkAllocs = allocsPerLink.get(link);
134 if (linkAllocs != null) {
135 allocations.get(link).addAll(linkAllocs);
136 }
Toshio Koideca0fcff2014-10-23 14:08:36 -0700137 }
Toshio Koide106d4492014-10-28 11:22:02 -0700138 LinkResourceAllocations result =
139 new DefaultLinkResourceAllocations(req, allocations);
140 store.allocateResources(result);
141 return result;
142
Toshio Koide50df38d2014-10-23 10:36:51 -0700143 }
144
145 @Override
146 public void releaseResources(LinkResourceAllocations allocations) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900147 checkPermission(LINK_WRITE);
Ray Milkeye97ede92014-11-20 10:43:12 -0800148 final LinkResourceEvent event = store.releaseResources(allocations);
149 if (event != null) {
150 post(event);
151 }
Toshio Koide50df38d2014-10-23 10:36:51 -0700152 }
153
154 @Override
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700155 public LinkResourceAllocations updateResources(LinkResourceRequest req,
Toshio Koide106d4492014-10-28 11:22:02 -0700156 LinkResourceAllocations oldAllocations) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900157 checkPermission(LINK_WRITE);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900158 releaseResources(oldAllocations);
weibit00c94f52014-11-16 07:09:05 -0800159 return requestResources(req);
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700160 }
161
162 @Override
Toshio Koide50df38d2014-10-23 10:36:51 -0700163 public Iterable<LinkResourceAllocations> getAllocations() {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900164 checkPermission(LINK_READ);
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) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900170 checkPermission(LINK_READ);
Toshio Koide106d4492014-10-28 11:22:02 -0700171 return store.getAllocations(link);
Toshio Koide50df38d2014-10-23 10:36:51 -0700172 }
173
174 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700175 public LinkResourceAllocations getAllocations(IntentId intentId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900176 checkPermission(LINK_READ);
Toshio Koide106d4492014-10-28 11:22:02 -0700177 return store.getAllocations(intentId);
Toshio Koide50df38d2014-10-23 10:36:51 -0700178 }
179
180 @Override
Toshio Koide9be539e2014-10-23 18:43:30 -0700181 public Iterable<ResourceRequest> getAvailableResources(Link link) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900182 checkPermission(LINK_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900183
Toshio Koide106d4492014-10-28 11:22:02 -0700184 Set<ResourceAllocation> freeRes = store.getFreeResources(link);
185 Set<ResourceRequest> result = new HashSet<>();
186 for (ResourceAllocation alloc : freeRes) {
187 switch (alloc.type()) {
188 case BANDWIDTH:
189 result.add(new BandwidthResourceRequest(
190 ((BandwidthResourceAllocation) alloc).bandwidth()));
191 break;
192 case LAMBDA:
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700193 result.add(new LambdaResourceRequest(
194 ((LambdaResourceAllocation) alloc).lambda()));
Toshio Koide106d4492014-10-28 11:22:02 -0700195 break;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100196 case MPLS_LABEL:
Sho SHIMIZU81cdc392015-10-27 15:18:49 -0700197 result.add(new MplsLabelResourceRequest(
198 ((MplsLabelResourceAllocation) alloc).mplsLabel()));
Sho SHIMIZU3fa9e8d2015-05-05 11:40:04 -0700199 break;
Toshio Koide106d4492014-10-28 11:22:02 -0700200 default:
201 break;
202 }
203 }
Ray Milkeycaa450b2014-10-29 15:54:24 -0700204 return result;
Toshio Koide50df38d2014-10-23 10:36:51 -0700205 }
206
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700207 @Override
weibit00c94f52014-11-16 07:09:05 -0800208 public Iterable<ResourceRequest> getAvailableResources(Link link,
Toshio Koide106d4492014-10-28 11:22:02 -0700209 LinkResourceAllocations allocations) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900210 checkPermission(LINK_READ);
Changhoon Yoon541ef712015-05-23 17:18:34 +0900211
weibit00c94f52014-11-16 07:09:05 -0800212 Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link);
Sho SHIMIZU716c8e92015-07-02 11:35:34 -0700213 Set<ResourceRequest> result = Sets.newHashSet(getAvailableResources(link));
Sho SHIMIZUc6154852015-07-02 11:53:10 -0700214 result.removeAll(allocatedRes);
weibit00c94f52014-11-16 07:09:05 -0800215 return result;
Thomas Vachuskaf9976952014-10-24 11:55:05 -0700216 }
217
Ray Milkeye97ede92014-11-20 10:43:12 -0800218 /**
219 * Store delegate to re-post events emitted from the store.
220 */
221 private class InternalStoreDelegate implements LinkResourceStoreDelegate {
222 @Override
223 public void notify(LinkResourceEvent event) {
224 post(event);
225 }
226 }
Toshio Koide50df38d2014-10-23 10:36:51 -0700227}