Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 17 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.HashSet; |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 21 | import java.util.List; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 22 | import java.util.Map; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Service; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 29 | import org.onlab.util.Bandwidth; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 30 | import org.onlab.util.PositionalParameterStringFormatter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.AnnotationKeys; |
| 32 | import org.onosproject.net.Annotations; |
| 33 | import org.onosproject.net.Link; |
| 34 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 35 | import org.onosproject.net.resource.link.BandwidthResource; |
| 36 | import org.onosproject.net.resource.link.BandwidthResourceAllocation; |
| 37 | import org.onosproject.net.resource.link.LambdaResource; |
| 38 | import org.onosproject.net.resource.link.LambdaResourceAllocation; |
| 39 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 40 | import org.onosproject.net.resource.link.LinkResourceEvent; |
| 41 | import org.onosproject.net.resource.link.LinkResourceStore; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.resource.ResourceAllocation; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 43 | import org.onosproject.net.resource.ResourceAllocationException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import org.onosproject.net.resource.ResourceType; |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 47 | import com.google.common.collect.ImmutableList; |
| 48 | |
| 49 | import static com.google.common.base.Preconditions.checkNotNull; |
| 50 | import static com.google.common.base.Preconditions.checkState; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Manages link resources using trivial in-memory structures implementation. |
Sho SHIMIZU | 364cbac | 2015-10-29 15:47:35 -0700 | [diff] [blame] | 55 | * |
| 56 | * @deprecated in Emu Release |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 57 | */ |
Sho SHIMIZU | 364cbac | 2015-10-29 15:47:35 -0700 | [diff] [blame] | 58 | @Deprecated |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 59 | @Component(immediate = true) |
| 60 | @Service |
| 61 | public class SimpleLinkResourceStore implements LinkResourceStore { |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 62 | private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000)); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 63 | private final Logger log = getLogger(getClass()); |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 64 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 65 | private Map<IntentId, LinkResourceAllocations> linkResourceAllocationsMap; |
| 66 | private Map<Link, Set<LinkResourceAllocations>> allocatedResources; |
| 67 | private Map<Link, Set<ResourceAllocation>> freeResources; |
| 68 | |
| 69 | @Activate |
| 70 | public void activate() { |
| 71 | linkResourceAllocationsMap = new HashMap<>(); |
| 72 | allocatedResources = new HashMap<>(); |
| 73 | freeResources = new HashMap<>(); |
| 74 | |
| 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | public void deactivate() { |
| 80 | log.info("Stopped"); |
| 81 | } |
| 82 | |
Toshio Koide | 69e5257 | 2014-10-30 11:14:09 -0700 | [diff] [blame] | 83 | /** |
| 84 | * Returns free resources for a given link obtaining from topology |
| 85 | * information. |
| 86 | * |
| 87 | * @param link the target link |
| 88 | * @return free resources |
| 89 | */ |
Yuta HIGUCHI | d45886c | 2014-11-07 15:15:45 -0800 | [diff] [blame] | 90 | private synchronized Set<ResourceAllocation> readOriginalFreeResources(Link link) { |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 91 | Annotations annotations = link.annotations(); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 92 | Set<ResourceAllocation> allocations = new HashSet<>(); |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 93 | |
| 94 | try { |
| 95 | int waves = Integer.parseInt(annotations.value(AnnotationKeys.OPTICAL_WAVES)); |
| 96 | for (int i = 1; i <= waves; i++) { |
Sho SHIMIZU | 94b7ff4 | 2015-05-06 17:51:49 -0700 | [diff] [blame] | 97 | allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i))); |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 98 | } |
| 99 | } catch (NumberFormatException e) { |
| 100 | log.debug("No optical.wave annotation on link %s", link); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 101 | } |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 102 | |
Sho SHIMIZU | 63feca7 | 2015-05-07 10:44:25 -0700 | [diff] [blame] | 103 | BandwidthResource bandwidth = DEFAULT_BANDWIDTH; |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 104 | try { |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 105 | bandwidth = new BandwidthResource( |
| 106 | Bandwidth.mbps((Double.parseDouble(annotations.value(AnnotationKeys.BANDWIDTH))))); |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 107 | } catch (NumberFormatException e) { |
| 108 | log.debug("No bandwidth annotation on link %s", link); |
| 109 | } |
Ray Milkey | 7325701 | 2014-11-20 12:02:27 -0800 | [diff] [blame] | 110 | allocations.add( |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 111 | new BandwidthResourceAllocation(bandwidth)); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 112 | return allocations; |
| 113 | } |
| 114 | |
Toshio Koide | 69e5257 | 2014-10-30 11:14:09 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Finds and returns {@link BandwidthResourceAllocation} object from a given |
| 117 | * set. |
| 118 | * |
| 119 | * @param freeRes a set of ResourceAllocation object. |
| 120 | * @return {@link BandwidthResourceAllocation} object if found, otherwise |
| 121 | * {@link BandwidthResourceAllocation} object with 0 bandwidth |
| 122 | * |
| 123 | */ |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 124 | private synchronized BandwidthResourceAllocation getBandwidth( |
| 125 | Set<ResourceAllocation> freeRes) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 126 | for (ResourceAllocation res : freeRes) { |
| 127 | if (res.type() == ResourceType.BANDWIDTH) { |
| 128 | return (BandwidthResourceAllocation) res; |
| 129 | } |
| 130 | } |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 131 | return new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(0))); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Toshio Koide | 69e5257 | 2014-10-30 11:14:09 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Subtracts given resources from free resources for given link. |
| 136 | * |
| 137 | * @param link the target link |
| 138 | * @param allocations the resources to be subtracted |
| 139 | */ |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 140 | private synchronized void subtractFreeResources(Link link, |
| 141 | LinkResourceAllocations allocations) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 142 | // TODO Use lock or version for updating freeResources. |
| 143 | checkNotNull(link); |
Toshio Koide | 69e5257 | 2014-10-30 11:14:09 -0700 | [diff] [blame] | 144 | Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link)); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 145 | Set<ResourceAllocation> subRes = allocations.getResourceAllocation(link); |
| 146 | for (ResourceAllocation res : subRes) { |
| 147 | switch (res.type()) { |
| 148 | case BANDWIDTH: |
| 149 | BandwidthResourceAllocation ba = getBandwidth(freeRes); |
| 150 | double requestedBandwidth = |
| 151 | ((BandwidthResourceAllocation) res).bandwidth().toDouble(); |
| 152 | double newBandwidth = ba.bandwidth().toDouble() - requestedBandwidth; |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 153 | if (newBandwidth < 0.0) { |
| 154 | throw new ResourceAllocationException( |
| 155 | PositionalParameterStringFormatter.format( |
| 156 | "Unable to allocate bandwidth for link {} " |
| 157 | + "requested amount is {} current allocation is {}", |
| 158 | link, |
| 159 | requestedBandwidth, |
| 160 | ba)); |
| 161 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 162 | freeRes.remove(ba); |
| 163 | freeRes.add(new BandwidthResourceAllocation( |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 164 | new BandwidthResource(Bandwidth.bps(newBandwidth)))); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 165 | break; |
| 166 | case LAMBDA: |
Ray Milkey | 9a39eca | 2015-01-05 09:41:01 -0800 | [diff] [blame] | 167 | final boolean lambdaAvailable = freeRes.remove(res); |
| 168 | if (!lambdaAvailable) { |
| 169 | int requestedLambda = |
| 170 | ((LambdaResourceAllocation) res).lambda().toInt(); |
| 171 | throw new ResourceAllocationException( |
| 172 | PositionalParameterStringFormatter.format( |
| 173 | "Unable to allocate lambda for link {} lambda is {}", |
| 174 | link, |
| 175 | requestedLambda)); |
| 176 | } |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | freeResources.put(link, freeRes); |
| 183 | |
| 184 | } |
| 185 | |
Toshio Koide | 69e5257 | 2014-10-30 11:14:09 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Adds given resources to free resources for given link. |
| 188 | * |
| 189 | * @param link the target link |
| 190 | * @param allocations the resources to be added |
| 191 | */ |
Toshio Koide | 8e5e91e | 2014-11-13 12:27:12 -0800 | [diff] [blame] | 192 | private synchronized void addFreeResources(Link link, |
| 193 | LinkResourceAllocations allocations) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 194 | // TODO Use lock or version for updating freeResources. |
Toshio Koide | 69e5257 | 2014-10-30 11:14:09 -0700 | [diff] [blame] | 195 | Set<ResourceAllocation> freeRes = new HashSet<>(getFreeResources(link)); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 196 | Set<ResourceAllocation> addRes = allocations.getResourceAllocation(link); |
| 197 | for (ResourceAllocation res : addRes) { |
| 198 | switch (res.type()) { |
| 199 | case BANDWIDTH: |
| 200 | BandwidthResourceAllocation ba = getBandwidth(freeRes); |
| 201 | double requestedBandwidth = |
| 202 | ((BandwidthResourceAllocation) res).bandwidth().toDouble(); |
| 203 | double newBandwidth = ba.bandwidth().toDouble() + requestedBandwidth; |
| 204 | freeRes.remove(ba); |
| 205 | freeRes.add(new BandwidthResourceAllocation( |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 206 | new BandwidthResource(Bandwidth.bps(newBandwidth)))); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 207 | break; |
| 208 | case LAMBDA: |
| 209 | checkState(freeRes.add(res)); |
| 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | freeResources.put(link, freeRes); |
| 216 | } |
| 217 | |
| 218 | @Override |
Yuta HIGUCHI | d45886c | 2014-11-07 15:15:45 -0800 | [diff] [blame] | 219 | public synchronized Set<ResourceAllocation> getFreeResources(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 220 | checkNotNull(link); |
| 221 | Set<ResourceAllocation> freeRes = freeResources.get(link); |
| 222 | if (freeRes == null) { |
| 223 | freeRes = readOriginalFreeResources(link); |
| 224 | } |
| 225 | |
| 226 | return freeRes; |
| 227 | } |
| 228 | |
| 229 | @Override |
Yuta HIGUCHI | d45886c | 2014-11-07 15:15:45 -0800 | [diff] [blame] | 230 | public synchronized void allocateResources(LinkResourceAllocations allocations) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 231 | checkNotNull(allocations); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 232 | linkResourceAllocationsMap.put(allocations.intentId(), allocations); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 233 | for (Link link : allocations.links()) { |
| 234 | subtractFreeResources(link, allocations); |
| 235 | Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link); |
| 236 | if (linkAllocs == null) { |
| 237 | linkAllocs = new HashSet<>(); |
| 238 | } |
| 239 | linkAllocs.add(allocations); |
| 240 | allocatedResources.put(link, linkAllocs); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | @Override |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 245 | public synchronized LinkResourceEvent releaseResources(LinkResourceAllocations allocations) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 246 | checkNotNull(allocations); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 247 | linkResourceAllocationsMap.remove(allocations.intentId()); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 248 | for (Link link : allocations.links()) { |
| 249 | addFreeResources(link, allocations); |
| 250 | Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link); |
| 251 | if (linkAllocs == null) { |
| 252 | log.error("Missing resource allocation."); |
| 253 | } else { |
| 254 | linkAllocs.remove(allocations); |
| 255 | } |
| 256 | allocatedResources.put(link, linkAllocs); |
| 257 | } |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 258 | |
| 259 | final List<LinkResourceAllocations> releasedResources = |
| 260 | ImmutableList.of(allocations); |
| 261 | |
| 262 | return new LinkResourceEvent( |
| 263 | LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE, |
| 264 | releasedResources); |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
Yuta HIGUCHI | d45886c | 2014-11-07 15:15:45 -0800 | [diff] [blame] | 268 | public synchronized LinkResourceAllocations getAllocations(IntentId intentId) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 269 | checkNotNull(intentId); |
| 270 | return linkResourceAllocationsMap.get(intentId); |
| 271 | } |
| 272 | |
| 273 | @Override |
Yuta HIGUCHI | d45886c | 2014-11-07 15:15:45 -0800 | [diff] [blame] | 274 | public synchronized Iterable<LinkResourceAllocations> getAllocations(Link link) { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 275 | checkNotNull(link); |
| 276 | Set<LinkResourceAllocations> result = allocatedResources.get(link); |
| 277 | if (result == null) { |
| 278 | result = Collections.emptySet(); |
| 279 | } |
| 280 | return Collections.unmodifiableSet(result); |
| 281 | } |
| 282 | |
| 283 | @Override |
Yuta HIGUCHI | d45886c | 2014-11-07 15:15:45 -0800 | [diff] [blame] | 284 | public synchronized Iterable<LinkResourceAllocations> getAllocations() { |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 285 | return Collections.unmodifiableCollection(linkResourceAllocationsMap.values()); |
| 286 | } |
| 287 | |
Ray Milkey | e97ede9 | 2014-11-20 10:43:12 -0800 | [diff] [blame] | 288 | |
Toshio Koide | 106d449 | 2014-10-28 11:22:02 -0700 | [diff] [blame] | 289 | } |