Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame^] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource.impl; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 17 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.event.AbstractListenerRegistry; |
| 25 | import org.onosproject.event.EventDeliveryService; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.intent.IntentId; |
| 28 | import org.onosproject.net.resource.BandwidthResourceAllocation; |
| 29 | import org.onosproject.net.resource.BandwidthResourceRequest; |
| 30 | import org.onosproject.net.resource.DefaultLinkResourceAllocations; |
| 31 | import org.onosproject.net.resource.Lambda; |
| 32 | import org.onosproject.net.resource.LambdaResourceAllocation; |
| 33 | import org.onosproject.net.resource.LambdaResourceRequest; |
| 34 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 35 | import org.onosproject.net.resource.LinkResourceEvent; |
| 36 | import org.onosproject.net.resource.LinkResourceListener; |
| 37 | import org.onosproject.net.resource.LinkResourceRequest; |
| 38 | import org.onosproject.net.resource.LinkResourceService; |
| 39 | import org.onosproject.net.resource.LinkResourceStore; |
| 40 | import org.onosproject.net.resource.LinkResourceStoreDelegate; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 41 | import org.onosproject.net.resource.MplsLabel; |
| 42 | import org.onosproject.net.resource.MplsLabelResourceAllocation; |
| 43 | import org.onosproject.net.resource.MplsLabelResourceRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.resource.ResourceAllocation; |
| 45 | import org.onosproject.net.resource.ResourceRequest; |
| 46 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 49 | import java.util.Collections; |
| 50 | import java.util.HashMap; |
| 51 | import java.util.HashSet; |
| 52 | import java.util.Iterator; |
| 53 | import java.util.Map; |
| 54 | import java.util.Set; |
| 55 | |
| 56 | import static com.google.common.base.Preconditions.checkArgument; |
| 57 | import static com.google.common.base.Preconditions.checkNotNull; |
| 58 | import static org.slf4j.LoggerFactory.getLogger; |
| 59 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Provides basic implementation of link resources allocation. |
| 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class LinkResourceManager implements LinkResourceService { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 69 | protected final AbstractListenerRegistry<LinkResourceEvent, LinkResourceListener> |
| 70 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 71 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | private LinkResourceStore store; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 74 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected EventDeliveryService eventDispatcher; |
| 77 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 78 | @Activate |
| 79 | public void activate() { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 80 | eventDispatcher.addSink(LinkResourceEvent.class, listenerRegistry); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 81 | log.info("Started"); |
| 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | public void deactivate() { |
| 86 | log.info("Stopped"); |
| 87 | } |
| 88 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Returns available lambdas on specified link. |
| 91 | * |
| 92 | * @param link the link |
| 93 | * @return available lambdas on specified link |
| 94 | */ |
| 95 | private Set<Lambda> getAvailableLambdas(Link link) { |
| 96 | checkNotNull(link); |
| 97 | Set<ResourceAllocation> resAllocs = store.getFreeResources(link); |
| 98 | if (resAllocs == null) { |
| 99 | return Collections.emptySet(); |
| 100 | } |
| 101 | Set<Lambda> lambdas = new HashSet<>(); |
| 102 | for (ResourceAllocation res : resAllocs) { |
| 103 | if (res.type() == ResourceType.LAMBDA) { |
| 104 | lambdas.add(((LambdaResourceAllocation) res).lambda()); |
| 105 | } |
| 106 | } |
| 107 | return lambdas; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 110 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Returns available lambdas on specified links. |
| 113 | * |
| 114 | * @param links the links |
| 115 | * @return available lambdas on specified links |
| 116 | */ |
| 117 | private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) { |
| 118 | checkNotNull(links); |
| 119 | Iterator<Link> i = links.iterator(); |
| 120 | checkArgument(i.hasNext()); |
| 121 | Set<Lambda> lambdas = new HashSet<>(getAvailableLambdas(i.next())); |
| 122 | while (i.hasNext()) { |
| 123 | lambdas.retainAll(getAvailableLambdas(i.next())); |
| 124 | } |
| 125 | return lambdas; |
| 126 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 127 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Returns available MPLS label on specified link. |
| 131 | * |
| 132 | * @param link the link |
| 133 | * @return available MPLS labels on specified link |
| 134 | */ |
| 135 | private Iterable<MplsLabel> getAvailableMplsLabels(Link link) { |
| 136 | Set<ResourceAllocation> resAllocs = store.getFreeResources(link); |
| 137 | if (resAllocs == null) { |
| 138 | return Collections.emptySet(); |
| 139 | } |
| 140 | Set<MplsLabel> mplsLabels = new HashSet<>(); |
| 141 | for (ResourceAllocation res : resAllocs) { |
| 142 | if (res.type() == ResourceType.MPLS_LABEL) { |
| 143 | |
| 144 | mplsLabels.add(((MplsLabelResourceAllocation) res).mplsLabel()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return mplsLabels; |
| 149 | } |
| 150 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 151 | @Override |
| 152 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 153 | // TODO Concatenate multiple bandwidth requests. |
| 154 | // TODO Support multiple lambda resource requests. |
| 155 | // TODO Throw appropriate exception. |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 156 | Set<ResourceAllocation> allocs = new HashSet<>(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 157 | Map<Link, Set<ResourceAllocation>> allocsPerLink = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 158 | for (ResourceRequest r : req.resources()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 159 | switch (r.type()) { |
| 160 | case BANDWIDTH: |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 161 | BandwidthResourceRequest br = (BandwidthResourceRequest) r; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 162 | allocs.add(new BandwidthResourceAllocation(br.bandwidth())); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 163 | break; |
| 164 | case LAMBDA: |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 165 | Iterator<Lambda> lambdaIterator = |
| 166 | getAvailableLambdas(req.links()).iterator(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 167 | if (lambdaIterator.hasNext()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 168 | allocs.add(new LambdaResourceAllocation(lambdaIterator.next())); |
| 169 | } else { |
| 170 | log.info("Failed to allocate lambda resource."); |
| 171 | return null; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 172 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 173 | break; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 174 | case MPLS_LABEL: |
| 175 | for (Link link : req.links()) { |
| 176 | if (allocsPerLink.get(link) == null) { |
| 177 | allocsPerLink.put(link, |
| 178 | new HashSet<ResourceAllocation>()); |
| 179 | } |
| 180 | Iterator<MplsLabel> mplsIter = getAvailableMplsLabels(link) |
| 181 | .iterator(); |
| 182 | if (mplsIter.hasNext()) { |
| 183 | allocsPerLink.get(link) |
| 184 | .add(new MplsLabelResourceAllocation(mplsIter |
| 185 | .next())); |
| 186 | } else { |
| 187 | log.info("Failed to allocate MPLS resource."); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | break; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 192 | default: |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 198 | for (Link link : req.links()) { |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 199 | allocations.put(link, new HashSet<ResourceAllocation>(allocs)); |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 200 | Set<ResourceAllocation> linkAllocs = allocsPerLink.get(link); |
| 201 | if (linkAllocs != null) { |
| 202 | allocations.get(link).addAll(linkAllocs); |
| 203 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 204 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 205 | LinkResourceAllocations result = |
| 206 | new DefaultLinkResourceAllocations(req, allocations); |
| 207 | store.allocateResources(result); |
| 208 | return result; |
| 209 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void releaseResources(LinkResourceAllocations allocations) { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 214 | final LinkResourceEvent event = store.releaseResources(allocations); |
| 215 | if (event != null) { |
| 216 | post(event); |
| 217 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 221 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 222 | LinkResourceAllocations oldAllocations) { |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 223 | releaseResources(oldAllocations); |
| 224 | return requestResources(req); |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 228 | public Iterable<LinkResourceAllocations> getAllocations() { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 229 | return store.getAllocations(); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 233 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 234 | return store.getAllocations(link); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 238 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 239 | return store.getAllocations(intentId); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 243 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 244 | Set<ResourceAllocation> freeRes = store.getFreeResources(link); |
| 245 | Set<ResourceRequest> result = new HashSet<>(); |
| 246 | for (ResourceAllocation alloc : freeRes) { |
| 247 | switch (alloc.type()) { |
| 248 | case BANDWIDTH: |
| 249 | result.add(new BandwidthResourceRequest( |
| 250 | ((BandwidthResourceAllocation) alloc).bandwidth())); |
| 251 | break; |
| 252 | case LAMBDA: |
| 253 | result.add(new LambdaResourceRequest()); |
| 254 | break; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 255 | case MPLS_LABEL: |
| 256 | result.add(new MplsLabelResourceRequest()); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 257 | default: |
| 258 | break; |
| 259 | } |
| 260 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 261 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 264 | @Override |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 265 | public Iterable<ResourceRequest> getAvailableResources(Link link, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 266 | LinkResourceAllocations allocations) { |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 267 | Set<ResourceRequest> result = new HashSet<>(); |
| 268 | Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link); |
| 269 | result = (Set<ResourceRequest>) getAvailableResources(link); |
| 270 | result.addAll(allocatedRes); |
| 271 | return result; |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 274 | @Override |
| 275 | public void addListener(LinkResourceListener listener) { |
| 276 | listenerRegistry.addListener(listener); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void removeListener(LinkResourceListener listener) { |
| 281 | listenerRegistry.removeListener(listener); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Posts the specified event to the local event dispatcher. |
| 286 | */ |
| 287 | private void post(LinkResourceEvent event) { |
| 288 | if (event != null) { |
| 289 | eventDispatcher.post(event); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Store delegate to re-post events emitted from the store. |
| 295 | */ |
| 296 | private class InternalStoreDelegate implements LinkResourceStoreDelegate { |
| 297 | @Override |
| 298 | public void notify(LinkResourceEvent event) { |
| 299 | post(event); |
| 300 | } |
| 301 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 302 | } |