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