Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'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 | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 20 | import static org.slf4j.LoggerFactory.getLogger; |
| 21 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 23 | import java.util.HashMap; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 24 | import java.util.HashSet; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 25 | import java.util.Iterator; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.event.AbstractListenerRegistry; |
| 36 | import org.onosproject.event.EventDeliveryService; |
| 37 | import org.onosproject.net.Link; |
| 38 | import org.onosproject.net.intent.IntentId; |
| 39 | import org.onosproject.net.resource.BandwidthResourceAllocation; |
| 40 | import org.onosproject.net.resource.BandwidthResourceRequest; |
| 41 | import org.onosproject.net.resource.DefaultLinkResourceAllocations; |
| 42 | import org.onosproject.net.resource.Lambda; |
| 43 | import org.onosproject.net.resource.LambdaResourceAllocation; |
| 44 | import org.onosproject.net.resource.LambdaResourceRequest; |
| 45 | import org.onosproject.net.resource.LinkResourceAllocations; |
| 46 | import org.onosproject.net.resource.LinkResourceEvent; |
| 47 | import org.onosproject.net.resource.LinkResourceListener; |
| 48 | import org.onosproject.net.resource.LinkResourceRequest; |
| 49 | import org.onosproject.net.resource.LinkResourceService; |
| 50 | import org.onosproject.net.resource.LinkResourceStore; |
| 51 | import org.onosproject.net.resource.LinkResourceStoreDelegate; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 52 | import org.onosproject.net.resource.MplsLabel; |
| 53 | import org.onosproject.net.resource.MplsLabelResourceAllocation; |
| 54 | import org.onosproject.net.resource.MplsLabelResourceRequest; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 55 | import org.onosproject.net.resource.ResourceAllocation; |
| 56 | import org.onosproject.net.resource.ResourceRequest; |
| 57 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 58 | import org.slf4j.Logger; |
| 59 | |
| 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)); |
| 200 | allocations.get(link).addAll(allocsPerLink.get(link)); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 201 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 202 | LinkResourceAllocations result = |
| 203 | new DefaultLinkResourceAllocations(req, allocations); |
| 204 | store.allocateResources(result); |
| 205 | return result; |
| 206 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void releaseResources(LinkResourceAllocations allocations) { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 211 | final LinkResourceEvent event = store.releaseResources(allocations); |
| 212 | if (event != null) { |
| 213 | post(event); |
| 214 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 218 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 219 | LinkResourceAllocations oldAllocations) { |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 220 | releaseResources(oldAllocations); |
| 221 | return requestResources(req); |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 225 | public Iterable<LinkResourceAllocations> getAllocations() { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 226 | return store.getAllocations(); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 230 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 231 | return store.getAllocations(link); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 235 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 236 | return store.getAllocations(intentId); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 240 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 241 | Set<ResourceAllocation> freeRes = store.getFreeResources(link); |
| 242 | Set<ResourceRequest> result = new HashSet<>(); |
| 243 | for (ResourceAllocation alloc : freeRes) { |
| 244 | switch (alloc.type()) { |
| 245 | case BANDWIDTH: |
| 246 | result.add(new BandwidthResourceRequest( |
| 247 | ((BandwidthResourceAllocation) alloc).bandwidth())); |
| 248 | break; |
| 249 | case LAMBDA: |
| 250 | result.add(new LambdaResourceRequest()); |
| 251 | break; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 252 | case MPLS_LABEL: |
| 253 | result.add(new MplsLabelResourceRequest()); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 258 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 261 | @Override |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 262 | public Iterable<ResourceRequest> getAvailableResources(Link link, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 263 | LinkResourceAllocations allocations) { |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 264 | Set<ResourceRequest> result = new HashSet<>(); |
| 265 | Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link); |
| 266 | result = (Set<ResourceRequest>) getAvailableResources(link); |
| 267 | result.addAll(allocatedRes); |
| 268 | return result; |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 271 | @Override |
| 272 | public void addListener(LinkResourceListener listener) { |
| 273 | listenerRegistry.addListener(listener); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public void removeListener(LinkResourceListener listener) { |
| 278 | listenerRegistry.removeListener(listener); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Posts the specified event to the local event dispatcher. |
| 283 | */ |
| 284 | private void post(LinkResourceEvent event) { |
| 285 | if (event != null) { |
| 286 | eventDispatcher.post(event); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Store delegate to re-post events emitted from the store. |
| 292 | */ |
| 293 | private class InternalStoreDelegate implements LinkResourceStoreDelegate { |
| 294 | @Override |
| 295 | public void notify(LinkResourceEvent event) { |
| 296 | post(event); |
| 297 | } |
| 298 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 299 | } |