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; |
| 52 | import org.onosproject.net.resource.ResourceAllocation; |
| 53 | import org.onosproject.net.resource.ResourceRequest; |
| 54 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 55 | import org.slf4j.Logger; |
| 56 | |
| 57 | /** |
| 58 | * Provides basic implementation of link resources allocation. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
| 62 | public class LinkResourceManager implements LinkResourceService { |
| 63 | |
| 64 | private final Logger log = getLogger(getClass()); |
| 65 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 66 | protected final AbstractListenerRegistry<LinkResourceEvent, LinkResourceListener> |
| 67 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 68 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | private LinkResourceStore store; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 71 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected EventDeliveryService eventDispatcher; |
| 74 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 75 | @Activate |
| 76 | public void activate() { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 77 | eventDispatcher.addSink(LinkResourceEvent.class, listenerRegistry); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 78 | log.info("Started"); |
| 79 | } |
| 80 | |
| 81 | @Deactivate |
| 82 | public void deactivate() { |
| 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Returns available lambdas on specified link. |
| 88 | * |
| 89 | * @param link the link |
| 90 | * @return available lambdas on specified link |
| 91 | */ |
| 92 | private Set<Lambda> getAvailableLambdas(Link link) { |
| 93 | checkNotNull(link); |
| 94 | Set<ResourceAllocation> resAllocs = store.getFreeResources(link); |
| 95 | if (resAllocs == null) { |
| 96 | return Collections.emptySet(); |
| 97 | } |
| 98 | Set<Lambda> lambdas = new HashSet<>(); |
| 99 | for (ResourceAllocation res : resAllocs) { |
| 100 | if (res.type() == ResourceType.LAMBDA) { |
| 101 | lambdas.add(((LambdaResourceAllocation) res).lambda()); |
| 102 | } |
| 103 | } |
| 104 | return lambdas; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 107 | /** |
| 108 | * Returns available lambdas on specified links. |
| 109 | * |
| 110 | * @param links the links |
| 111 | * @return available lambdas on specified links |
| 112 | */ |
| 113 | private Iterable<Lambda> getAvailableLambdas(Iterable<Link> links) { |
| 114 | checkNotNull(links); |
| 115 | Iterator<Link> i = links.iterator(); |
| 116 | checkArgument(i.hasNext()); |
| 117 | Set<Lambda> lambdas = new HashSet<>(getAvailableLambdas(i.next())); |
| 118 | while (i.hasNext()) { |
| 119 | lambdas.retainAll(getAvailableLambdas(i.next())); |
| 120 | } |
| 121 | return lambdas; |
| 122 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 123 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 124 | @Override |
| 125 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 126 | // TODO Concatenate multiple bandwidth requests. |
| 127 | // TODO Support multiple lambda resource requests. |
| 128 | // TODO Throw appropriate exception. |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 129 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 130 | Set<ResourceAllocation> allocs = new HashSet<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 131 | for (ResourceRequest r : req.resources()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 132 | switch (r.type()) { |
| 133 | case BANDWIDTH: |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 134 | BandwidthResourceRequest br = (BandwidthResourceRequest) r; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 135 | allocs.add(new BandwidthResourceAllocation(br.bandwidth())); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 136 | break; |
| 137 | case LAMBDA: |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 138 | Iterator<Lambda> lambdaIterator = |
| 139 | getAvailableLambdas(req.links()).iterator(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 140 | if (lambdaIterator.hasNext()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 141 | allocs.add(new LambdaResourceAllocation(lambdaIterator.next())); |
| 142 | } else { |
| 143 | log.info("Failed to allocate lambda resource."); |
| 144 | return null; |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 145 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 146 | break; |
| 147 | default: |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 153 | for (Link link : req.links()) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 154 | allocations.put(link, allocs); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 155 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 156 | LinkResourceAllocations result = |
| 157 | new DefaultLinkResourceAllocations(req, allocations); |
| 158 | store.allocateResources(result); |
| 159 | return result; |
| 160 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void releaseResources(LinkResourceAllocations allocations) { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 165 | final LinkResourceEvent event = store.releaseResources(allocations); |
| 166 | if (event != null) { |
| 167 | post(event); |
| 168 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 172 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 173 | LinkResourceAllocations oldAllocations) { |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 174 | releaseResources(oldAllocations); |
| 175 | return requestResources(req); |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 179 | public Iterable<LinkResourceAllocations> getAllocations() { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 180 | return store.getAllocations(); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 184 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 185 | return store.getAllocations(link); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 189 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 190 | return store.getAllocations(intentId); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 194 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 195 | Set<ResourceAllocation> freeRes = store.getFreeResources(link); |
| 196 | Set<ResourceRequest> result = new HashSet<>(); |
| 197 | for (ResourceAllocation alloc : freeRes) { |
| 198 | switch (alloc.type()) { |
| 199 | case BANDWIDTH: |
| 200 | result.add(new BandwidthResourceRequest( |
| 201 | ((BandwidthResourceAllocation) alloc).bandwidth())); |
| 202 | break; |
| 203 | case LAMBDA: |
| 204 | result.add(new LambdaResourceRequest()); |
| 205 | break; |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 210 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 213 | @Override |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 214 | public Iterable<ResourceRequest> getAvailableResources(Link link, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 215 | LinkResourceAllocations allocations) { |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 216 | Set<ResourceRequest> result = new HashSet<>(); |
| 217 | Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link); |
| 218 | result = (Set<ResourceRequest>) getAvailableResources(link); |
| 219 | result.addAll(allocatedRes); |
| 220 | return result; |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 223 | @Override |
| 224 | public void addListener(LinkResourceListener listener) { |
| 225 | listenerRegistry.addListener(listener); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void removeListener(LinkResourceListener listener) { |
| 230 | listenerRegistry.removeListener(listener); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Posts the specified event to the local event dispatcher. |
| 235 | */ |
| 236 | private void post(LinkResourceEvent event) { |
| 237 | if (event != null) { |
| 238 | eventDispatcher.post(event); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Store delegate to re-post events emitted from the store. |
| 244 | */ |
| 245 | private class InternalStoreDelegate implements LinkResourceStoreDelegate { |
| 246 | @Override |
| 247 | public void notify(LinkResourceEvent event) { |
| 248 | post(event); |
| 249 | } |
| 250 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 251 | } |