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 | |
Sho SHIMIZU | 716c8e9 | 2015-07-02 11:35:34 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 25 | import org.onosproject.event.AbstractListenerManager; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.intent.IntentId; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 28 | import org.onosproject.net.resource.ResourceAllocation; |
| 29 | import org.onosproject.net.resource.ResourceRequest; |
| 30 | import org.onosproject.net.resource.ResourceType; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 31 | import org.onosproject.net.resource.link.BandwidthResourceAllocation; |
| 32 | import org.onosproject.net.resource.link.BandwidthResourceRequest; |
| 33 | import org.onosproject.net.resource.link.DefaultLinkResourceAllocations; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 34 | import org.onosproject.net.resource.link.LambdaResourceAllocation; |
| 35 | import org.onosproject.net.resource.link.LambdaResourceRequest; |
| 36 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 37 | import org.onosproject.net.resource.link.LinkResourceEvent; |
| 38 | import org.onosproject.net.resource.link.LinkResourceListener; |
| 39 | import org.onosproject.net.resource.link.LinkResourceRequest; |
| 40 | import org.onosproject.net.resource.link.LinkResourceService; |
| 41 | import org.onosproject.net.resource.link.LinkResourceStore; |
| 42 | import org.onosproject.net.resource.link.LinkResourceStoreDelegate; |
| 43 | import org.onosproject.net.resource.link.MplsLabel; |
| 44 | import org.onosproject.net.resource.link.MplsLabelResourceAllocation; |
| 45 | import org.onosproject.net.resource.link.MplsLabelResourceRequest; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 48 | import java.util.HashMap; |
| 49 | import java.util.HashSet; |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 50 | import java.util.Map; |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 51 | import java.util.Optional; |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 52 | import java.util.Set; |
| 53 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 54 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 55 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 56 | import static org.onosproject.security.AppPermission.Type.*; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 57 | |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 58 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Provides basic implementation of link resources allocation. |
| 61 | */ |
| 62 | @Component(immediate = true) |
| 63 | @Service |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 64 | public class LinkResourceManager |
| 65 | extends AbstractListenerManager<LinkResourceEvent, LinkResourceListener> |
| 66 | implements LinkResourceService { |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 67 | |
| 68 | private final Logger log = getLogger(getClass()); |
| 69 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | private LinkResourceStore store; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 72 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 73 | @Activate |
| 74 | public void activate() { |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 75 | eventDispatcher.addSink(LinkResourceEvent.class, listenerRegistry); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 81 | eventDispatcher.removeSink(LinkResourceEvent.class); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 82 | log.info("Stopped"); |
| 83 | } |
| 84 | |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 85 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public LinkResourceAllocations requestResources(LinkResourceRequest req) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 88 | checkPermission(LINK_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 89 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 90 | // TODO Concatenate multiple bandwidth requests. |
| 91 | // TODO Support multiple lambda resource requests. |
| 92 | // TODO Throw appropriate exception. |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 93 | Set<ResourceAllocation> allocs = new HashSet<>(); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 94 | Map<Link, Set<ResourceAllocation>> allocsPerLink = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 95 | for (ResourceRequest r : req.resources()) { |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 96 | switch (r.type()) { |
| 97 | case BANDWIDTH: |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 98 | BandwidthResourceRequest br = (BandwidthResourceRequest) r; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 99 | allocs.add(new BandwidthResourceAllocation(br.bandwidth())); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 100 | break; |
| 101 | case LAMBDA: |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 102 | LambdaResourceRequest lr = (LambdaResourceRequest) r; |
| 103 | allocs.add(new LambdaResourceAllocation(lr.lambda())); |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 104 | break; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 105 | case MPLS_LABEL: |
| 106 | for (Link link : req.links()) { |
| 107 | if (allocsPerLink.get(link) == null) { |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 108 | allocsPerLink.put(link, new HashSet<>()); |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 109 | } |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 110 | |
| 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 Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 119 | } else { |
| 120 | log.info("Failed to allocate MPLS resource."); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | break; |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>(); |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 131 | for (Link link : req.links()) { |
Sho SHIMIZU | d88db6f | 2015-09-09 14:22:06 -0700 | [diff] [blame] | 132 | allocations.put(link, new HashSet<>(allocs)); |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 133 | Set<ResourceAllocation> linkAllocs = allocsPerLink.get(link); |
| 134 | if (linkAllocs != null) { |
| 135 | allocations.get(link).addAll(linkAllocs); |
| 136 | } |
Toshio Koide | ca0fcff | 2014-10-23 14:08:36 -0700 | [diff] [blame] | 137 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 138 | LinkResourceAllocations result = |
| 139 | new DefaultLinkResourceAllocations(req, allocations); |
| 140 | store.allocateResources(result); |
| 141 | return result; |
| 142 | |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void releaseResources(LinkResourceAllocations allocations) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 147 | checkPermission(LINK_WRITE); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 148 | final LinkResourceEvent event = store.releaseResources(allocations); |
| 149 | if (event != null) { |
| 150 | post(event); |
| 151 | } |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 155 | public LinkResourceAllocations updateResources(LinkResourceRequest req, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 156 | LinkResourceAllocations oldAllocations) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 157 | checkPermission(LINK_WRITE); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 158 | releaseResources(oldAllocations); |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 159 | return requestResources(req); |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 163 | public Iterable<LinkResourceAllocations> getAllocations() { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 164 | checkPermission(LINK_READ); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 165 | return store.getAllocations(); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 169 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 170 | checkPermission(LINK_READ); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 171 | return store.getAllocations(link); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 175 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 176 | checkPermission(LINK_READ); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 177 | return store.getAllocations(intentId); |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Toshio Koide | 9be539e | 2014-10-23 18:43:30 -0700 | [diff] [blame] | 181 | public Iterable<ResourceRequest> getAvailableResources(Link link) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 182 | checkPermission(LINK_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 183 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 184 | 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 SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 193 | result.add(new LambdaResourceRequest( |
| 194 | ((LambdaResourceAllocation) alloc).lambda())); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 195 | break; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 196 | case MPLS_LABEL: |
Sho SHIMIZU | 81cdc39 | 2015-10-27 15:18:49 -0700 | [diff] [blame] | 197 | result.add(new MplsLabelResourceRequest( |
| 198 | ((MplsLabelResourceAllocation) alloc).mplsLabel())); |
Sho SHIMIZU | 3fa9e8d | 2015-05-05 11:40:04 -0700 | [diff] [blame] | 199 | break; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | } |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 204 | return result; |
Toshio Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 207 | @Override |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 208 | public Iterable<ResourceRequest> getAvailableResources(Link link, |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 209 | LinkResourceAllocations allocations) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 210 | checkPermission(LINK_READ); |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 211 | |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 212 | Set<ResourceAllocation> allocatedRes = allocations.getResourceAllocation(link); |
Sho SHIMIZU | 716c8e9 | 2015-07-02 11:35:34 -0700 | [diff] [blame] | 213 | Set<ResourceRequest> result = Sets.newHashSet(getAvailableResources(link)); |
Sho SHIMIZU | c615485 | 2015-07-02 11:53:10 -0700 | [diff] [blame] | 214 | result.removeAll(allocatedRes); |
weibit | 00c94f5 | 2014-11-16 07:09:05 -0800 | [diff] [blame] | 215 | return result; |
Thomas Vachuska | f997695 | 2014-10-24 11:55:05 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 218 | /** |
| 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 Koide | 50df38d | 2014-10-23 10:36:51 -0700 | [diff] [blame] | 227 | } |