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