Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
| 21 | import java.util.HashMap; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | import java.util.stream.Collectors; |
| 27 | |
| 28 | import org.apache.felix.scr.annotations.Component; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
| 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Deactivate; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 34 | import org.onlab.util.Bandwidth; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 35 | import org.onosproject.net.OmsPort; |
| 36 | import org.onosproject.net.device.DeviceService; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 38 | import org.onlab.util.PositionalParameterStringFormatter; |
| 39 | import org.onosproject.net.Link; |
| 40 | import org.onosproject.net.LinkKey; |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 41 | import org.onosproject.net.Port; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 43 | import org.onosproject.net.resource.link.BandwidthResource; |
| 44 | import org.onosproject.net.resource.link.BandwidthResourceAllocation; |
| 45 | import org.onosproject.net.resource.link.LambdaResource; |
| 46 | import org.onosproject.net.resource.link.LambdaResourceAllocation; |
| 47 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 48 | import org.onosproject.net.resource.link.LinkResourceEvent; |
| 49 | import org.onosproject.net.resource.link.LinkResourceStore; |
| 50 | import org.onosproject.net.resource.link.LinkResourceStoreDelegate; |
| 51 | import org.onosproject.net.resource.link.MplsLabel; |
| 52 | import org.onosproject.net.resource.link.MplsLabelResourceAllocation; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 53 | import org.onosproject.net.resource.ResourceAllocation; |
| 54 | import org.onosproject.net.resource.ResourceAllocationException; |
| 55 | import org.onosproject.net.resource.ResourceType; |
| 56 | import org.onosproject.store.AbstractStore; |
| 57 | import org.onosproject.store.serializers.KryoNamespaces; |
| 58 | import org.onosproject.store.service.ConsistentMap; |
| 59 | import org.onosproject.store.service.Serializer; |
| 60 | import org.onosproject.store.service.StorageService; |
| 61 | import org.onosproject.store.service.TransactionContext; |
| 62 | import org.onosproject.store.service.TransactionException; |
| 63 | import org.onosproject.store.service.TransactionalMap; |
| 64 | import org.onosproject.store.service.Versioned; |
| 65 | |
| 66 | import com.google.common.collect.ImmutableList; |
| 67 | import com.google.common.collect.ImmutableSet; |
| 68 | import com.google.common.collect.Sets; |
| 69 | |
| 70 | import static com.google.common.base.Preconditions.checkNotNull; |
| 71 | import static com.google.common.base.Preconditions.checkState; |
| 72 | import static org.slf4j.LoggerFactory.getLogger; |
| 73 | import static org.onosproject.net.AnnotationKeys.BANDWIDTH; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Store that manages link resources using Copycat-backed TransactionalMaps. |
| 77 | */ |
Ayaka Koshibe | 474ef5f | 2015-05-01 15:24:51 -0700 | [diff] [blame] | 78 | @Component(immediate = true, enabled = true) |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 79 | @Service |
| 80 | public class ConsistentLinkResourceStore extends |
| 81 | AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements |
| 82 | LinkResourceStore { |
| 83 | |
| 84 | private final Logger log = getLogger(getClass()); |
| 85 | |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 86 | private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000)); |
| 87 | private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0)); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 88 | |
| 89 | // Smallest non-reserved MPLS label |
| 90 | private static final int MIN_UNRESERVED_LABEL = 0x10; |
| 91 | // Max non-reserved MPLS label = 239 |
| 92 | private static final int MAX_UNRESERVED_LABEL = 0xEF; |
| 93 | |
| 94 | // table to store current allocations |
| 95 | /** LinkKey -> List<LinkResourceAllocations>. */ |
Ayaka Koshibe | bcb0237 | 2015-06-01 10:56:42 -0700 | [diff] [blame] | 96 | private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations"; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 97 | |
| 98 | /** IntentId -> LinkResourceAllocations. */ |
Ayaka Koshibe | bcb0237 | 2015-06-01 10:56:42 -0700 | [diff] [blame] | 99 | private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations"; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 100 | |
Sho SHIMIZU | 200a739 | 2015-07-23 16:28:35 -0700 | [diff] [blame] | 101 | private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 102 | |
| 103 | // for reading committed values. |
| 104 | private ConsistentMap<IntentId, LinkResourceAllocations> intentAllocMap; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected StorageService storageService; |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 110 | protected DeviceService deviceService; |
| 111 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 112 | @Activate |
| 113 | public void activate() { |
| 114 | intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder() |
| 115 | .withName(INTENT_ALLOCATIONS) |
| 116 | .withSerializer(SERIALIZER) |
| 117 | .build(); |
| 118 | log.info("Started"); |
| 119 | } |
| 120 | |
| 121 | @Deactivate |
| 122 | public void deactivate() { |
| 123 | log.info("Stopped"); |
| 124 | } |
| 125 | |
| 126 | private TransactionalMap<IntentId, LinkResourceAllocations> getIntentAllocs(TransactionContext tx) { |
| 127 | return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER); |
| 128 | } |
| 129 | |
| 130 | private TransactionalMap<LinkKey, List<LinkResourceAllocations>> getLinkAllocs(TransactionContext tx) { |
| 131 | return tx.getTransactionalMap(LINK_RESOURCE_ALLOCATIONS, SERIALIZER); |
| 132 | } |
| 133 | |
| 134 | private TransactionContext getTxContext() { |
| 135 | return storageService.transactionContextBuilder().build(); |
| 136 | } |
| 137 | |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 138 | private Set<ResourceAllocation> getResourceCapacity(ResourceType type, Link link) { |
Sho SHIMIZU | 04ae54a | 2015-10-05 15:46:28 -0700 | [diff] [blame^] | 139 | switch (type) { |
| 140 | case BANDWIDTH: |
| 141 | return ImmutableSet.of(getBandwidthResourceCapacity(link)); |
| 142 | case LAMBDA: |
| 143 | return getLambdaResourceCapacity(link); |
| 144 | case MPLS_LABEL: |
| 145 | return getMplsResourceCapacity(); |
| 146 | default: |
| 147 | return ImmutableSet.of(); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 148 | } |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 151 | private Set<ResourceAllocation> getLambdaResourceCapacity(Link link) { |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 152 | Port port = deviceService.getPort(link.src().deviceId(), link.src().port()); |
Sho SHIMIZU | a005a6e5 | 2015-10-02 15:13:27 -0700 | [diff] [blame] | 153 | if (!(port instanceof OmsPort)) { |
| 154 | return Collections.emptySet(); |
| 155 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 156 | |
Sho SHIMIZU | a005a6e5 | 2015-10-02 15:13:27 -0700 | [diff] [blame] | 157 | OmsPort omsPort = (OmsPort) port; |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 158 | Set<ResourceAllocation> allocations = new HashSet<>(); |
Sho SHIMIZU | a005a6e5 | 2015-10-02 15:13:27 -0700 | [diff] [blame] | 159 | // Assume fixed grid for now |
| 160 | for (int i = 0; i < omsPort.totalChannels(); i++) { |
| 161 | allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i))); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 162 | } |
| 163 | return allocations; |
| 164 | } |
| 165 | |
| 166 | private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) { |
| 167 | |
| 168 | // if Link annotation exist, use them |
| 169 | // if all fails, use DEFAULT_BANDWIDTH |
Sho SHIMIZU | a890e7c | 2015-10-02 14:48:13 -0700 | [diff] [blame] | 170 | BandwidthResource bandwidth = DEFAULT_BANDWIDTH; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 171 | String strBw = link.annotations().value(BANDWIDTH); |
Sho SHIMIZU | a005a6e5 | 2015-10-02 15:13:27 -0700 | [diff] [blame] | 172 | if (strBw == null) { |
| 173 | return new BandwidthResourceAllocation(bandwidth); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Sho SHIMIZU | a005a6e5 | 2015-10-02 15:13:27 -0700 | [diff] [blame] | 176 | try { |
| 177 | bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw))); |
| 178 | } catch (NumberFormatException e) { |
| 179 | // do nothings, use default bandwidth |
| 180 | bandwidth = DEFAULT_BANDWIDTH; |
| 181 | } |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 182 | return new BandwidthResourceAllocation(bandwidth); |
| 183 | } |
| 184 | |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 185 | private Set<ResourceAllocation> getMplsResourceCapacity() { |
| 186 | Set<ResourceAllocation> allocations = new HashSet<>(); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 187 | //Ignoring reserved labels of 0 through 15 |
| 188 | for (int i = MIN_UNRESERVED_LABEL; i <= MAX_UNRESERVED_LABEL; i++) { |
| 189 | allocations.add(new MplsLabelResourceAllocation(MplsLabel |
| 190 | .valueOf(i))); |
| 191 | |
| 192 | } |
| 193 | return allocations; |
| 194 | } |
| 195 | |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 196 | private Map<ResourceType, Set<ResourceAllocation>> getResourceCapacity(Link link) { |
| 197 | Map<ResourceType, Set<ResourceAllocation>> caps = new HashMap<>(); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 198 | for (ResourceType type : ResourceType.values()) { |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 199 | Set<ResourceAllocation> cap = getResourceCapacity(type, link); |
Sho SHIMIZU | f618cb0 | 2015-10-02 14:43:50 -0700 | [diff] [blame] | 200 | caps.put(type, cap); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 201 | } |
| 202 | return caps; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Set<ResourceAllocation> getFreeResources(Link link) { |
| 207 | TransactionContext tx = getTxContext(); |
| 208 | |
| 209 | tx.begin(); |
| 210 | try { |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 211 | Map<ResourceType, Set<ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 212 | Set<ResourceAllocation> allFree = new HashSet<>(); |
| 213 | freeResources.values().forEach(allFree::addAll); |
| 214 | return allFree; |
| 215 | } finally { |
| 216 | tx.abort(); |
| 217 | } |
| 218 | } |
| 219 | |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 220 | private Map<ResourceType, Set<ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) { |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 221 | checkNotNull(tx); |
| 222 | checkNotNull(link); |
| 223 | |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 224 | Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>(); |
| 225 | final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link); |
Sho SHIMIZU | 48588a7 | 2015-10-05 17:00:48 -0700 | [diff] [blame] | 226 | final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link)); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 227 | |
Sho SHIMIZU | 48588a7 | 2015-10-05 17:00:48 -0700 | [diff] [blame] | 228 | Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH); |
| 229 | Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations); |
| 230 | free.put(ResourceType.BANDWIDTH, value); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 231 | |
Sho SHIMIZU | 48588a7 | 2015-10-05 17:00:48 -0700 | [diff] [blame] | 232 | Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA); |
| 233 | Set<ResourceAllocation> freeL = getFreeLambdaResources(link, lmd, allocations); |
| 234 | free.put(ResourceType.LAMBDA, freeL); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 235 | |
Sho SHIMIZU | 48588a7 | 2015-10-05 17:00:48 -0700 | [diff] [blame] | 236 | Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL); |
| 237 | Set<ResourceAllocation> freeLabel = getFreeLabelResources(link, mpls, allocations); |
| 238 | free.put(ResourceType.MPLS_LABEL, freeLabel); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 239 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 240 | return free; |
| 241 | } |
| 242 | |
Sho SHIMIZU | 48588a7 | 2015-10-05 17:00:48 -0700 | [diff] [blame] | 243 | private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw, |
| 244 | List<LinkResourceAllocations> allocations) { |
| 245 | if (bw == null || bw.isEmpty()) { |
| 246 | bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW)); |
| 247 | } |
| 248 | |
| 249 | BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next(); |
| 250 | double freeBw = cap.bandwidth().toDouble(); |
| 251 | |
| 252 | // enumerate current allocations, subtracting resources |
| 253 | double allocatedBw = allocations.stream() |
| 254 | .flatMap(x -> x.getResourceAllocation(link).stream()) |
| 255 | .filter(x -> x instanceof BandwidthResourceAllocation) |
| 256 | .map(x -> (BandwidthResourceAllocation) x) |
| 257 | .mapToDouble(x -> x.bandwidth().toDouble()) |
| 258 | .sum(); |
| 259 | freeBw -= allocatedBw; |
| 260 | return Sets.newHashSet( |
| 261 | new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))); |
| 262 | } |
| 263 | |
| 264 | private Set<ResourceAllocation> getFreeLambdaResources(Link link, Set<ResourceAllocation> lmd, |
| 265 | List<LinkResourceAllocations> allocations) { |
| 266 | if (lmd == null || lmd.isEmpty()) { |
| 267 | // nothing left |
| 268 | return Collections.emptySet(); |
| 269 | } |
| 270 | Set<ResourceAllocation> freeL = lmd.stream() |
| 271 | .filter(x -> x instanceof LambdaResourceAllocation) |
| 272 | .collect(Collectors.toSet()); |
| 273 | |
| 274 | // enumerate current allocations, removing resources |
| 275 | List<ResourceAllocation> allocatedLambda = allocations.stream() |
| 276 | .flatMap(x -> x.getResourceAllocation(link).stream()) |
| 277 | .filter(x -> x instanceof LambdaResourceAllocation) |
| 278 | .collect(Collectors.toList()); |
| 279 | freeL.removeAll(allocatedLambda); |
| 280 | return freeL; |
| 281 | } |
| 282 | |
| 283 | private Set<ResourceAllocation> getFreeLabelResources(Link link, Set<ResourceAllocation> mpls, |
| 284 | List<LinkResourceAllocations> allocations) { |
| 285 | if (mpls == null || mpls.isEmpty()) { |
| 286 | // nothing left |
| 287 | return Collections.emptySet(); |
| 288 | } |
| 289 | Set<ResourceAllocation> freeLabel = mpls.stream() |
| 290 | .filter(x -> x instanceof MplsLabelResourceAllocation) |
| 291 | .collect(Collectors.toSet()); |
| 292 | |
| 293 | // enumerate current allocations, removing resources |
| 294 | List<ResourceAllocation> allocatedLabel = allocations.stream() |
| 295 | .flatMap(x -> x.getResourceAllocation(link).stream()) |
| 296 | .filter(x -> x instanceof MplsLabelResourceAllocation) |
| 297 | .collect(Collectors.toList()); |
| 298 | freeLabel.removeAll(allocatedLabel); |
| 299 | return freeLabel; |
| 300 | } |
| 301 | |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 302 | @Override |
| 303 | public void allocateResources(LinkResourceAllocations allocations) { |
| 304 | checkNotNull(allocations); |
| 305 | TransactionContext tx = getTxContext(); |
| 306 | |
| 307 | tx.begin(); |
| 308 | try { |
| 309 | TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx); |
| 310 | intentAllocs.put(allocations.intentId(), allocations); |
| 311 | allocations.links().forEach(link -> allocateLinkResource(tx, link, allocations)); |
| 312 | tx.commit(); |
| 313 | } catch (Exception e) { |
| 314 | log.error("Exception thrown, rolling back", e); |
| 315 | tx.abort(); |
| 316 | throw e; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private void allocateLinkResource(TransactionContext tx, Link link, |
| 321 | LinkResourceAllocations allocations) { |
| 322 | // requested resources |
| 323 | Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link); |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 324 | Map<ResourceType, Set<ResourceAllocation>> available = getFreeResourcesEx(tx, link); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 325 | for (ResourceAllocation req : reqs) { |
Sho SHIMIZU | e1b463b | 2015-10-05 15:39:19 -0700 | [diff] [blame] | 326 | Set<ResourceAllocation> avail = available.get(req.type()); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 327 | if (req instanceof BandwidthResourceAllocation) { |
| 328 | // check if allocation should be accepted |
| 329 | if (avail.isEmpty()) { |
| 330 | checkState(!avail.isEmpty(), |
| 331 | "There's no Bandwidth resource on %s?", |
| 332 | link); |
| 333 | } |
| 334 | BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next(); |
| 335 | double bwLeft = bw.bandwidth().toDouble(); |
| 336 | BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req); |
| 337 | bwLeft -= bwReq.bandwidth().toDouble(); |
| 338 | if (bwLeft < 0) { |
| 339 | throw new ResourceAllocationException( |
| 340 | PositionalParameterStringFormatter.format( |
| 341 | "Unable to allocate bandwidth for link {} " |
| 342 | + " requested amount is {} current allocation is {}", |
| 343 | link, |
| 344 | bwReq.bandwidth().toDouble(), |
| 345 | bw)); |
| 346 | } |
| 347 | } else if (req instanceof LambdaResourceAllocation) { |
| 348 | LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req; |
| 349 | // check if allocation should be accepted |
| 350 | if (!avail.contains(req)) { |
| 351 | // requested lambda was not available |
| 352 | throw new ResourceAllocationException( |
| 353 | PositionalParameterStringFormatter.format( |
| 354 | "Unable to allocate lambda for link {} lambda is {}", |
| 355 | link, |
| 356 | lambdaAllocation.lambda().toInt())); |
| 357 | } |
| 358 | } else if (req instanceof MplsLabelResourceAllocation) { |
| 359 | MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req; |
| 360 | if (!avail.contains(req)) { |
| 361 | throw new ResourceAllocationException( |
| 362 | PositionalParameterStringFormatter |
| 363 | .format("Unable to allocate MPLS label for link " |
| 364 | + "{} MPLS label is {}", |
| 365 | link, |
| 366 | mplsAllocation |
| 367 | .mplsLabel() |
| 368 | .toString())); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | // all requests allocatable => add allocation |
| 373 | final LinkKey linkKey = LinkKey.linkKey(link); |
| 374 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
| 375 | List<LinkResourceAllocations> before = linkAllocs.get(linkKey); |
| 376 | if (before == null) { |
| 377 | List<LinkResourceAllocations> after = new ArrayList<>(); |
| 378 | after.add(allocations); |
| 379 | before = linkAllocs.putIfAbsent(linkKey, after); |
| 380 | if (before != null) { |
| 381 | // concurrent allocation detected, retry transaction : is this needed? |
| 382 | log.warn("Concurrent Allocation, retrying"); |
| 383 | throw new TransactionException(); |
| 384 | } |
| 385 | } else { |
| 386 | List<LinkResourceAllocations> after = new ArrayList<>(before.size() + 1); |
| 387 | after.addAll(before); |
| 388 | after.add(allocations); |
| 389 | linkAllocs.replace(linkKey, before, after); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) { |
| 395 | checkNotNull(allocations); |
| 396 | |
| 397 | final IntentId intentId = allocations.intentId(); |
| 398 | final Collection<Link> links = allocations.links(); |
| 399 | boolean success = false; |
| 400 | do { |
| 401 | TransactionContext tx = getTxContext(); |
| 402 | tx.begin(); |
| 403 | try { |
| 404 | TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx); |
| 405 | intentAllocs.remove(intentId); |
| 406 | |
| 407 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
| 408 | links.forEach(link -> { |
| 409 | final LinkKey linkId = LinkKey.linkKey(link); |
| 410 | |
| 411 | List<LinkResourceAllocations> before = linkAllocs.get(linkId); |
| 412 | if (before == null || before.isEmpty()) { |
| 413 | // something is wrong, but it is already freed |
| 414 | log.warn("There was no resource left to release on {}", linkId); |
| 415 | return; |
| 416 | } |
| 417 | List<LinkResourceAllocations> after = new ArrayList<>(before); |
| 418 | after.remove(allocations); |
| 419 | linkAllocs.replace(linkId, before, after); |
| 420 | }); |
| 421 | tx.commit(); |
| 422 | success = true; |
| 423 | } catch (TransactionException e) { |
| 424 | log.debug("Transaction failed, retrying", e); |
| 425 | tx.abort(); |
| 426 | } catch (Exception e) { |
| 427 | log.error("Exception thrown during releaseResource {}", allocations, e); |
| 428 | tx.abort(); |
| 429 | throw e; |
| 430 | } |
| 431 | } while (!success); |
| 432 | |
| 433 | // Issue events to force recompilation of intents. |
| 434 | final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations); |
| 435 | return new LinkResourceEvent( |
| 436 | LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE, |
| 437 | releasedResources); |
| 438 | |
| 439 | } |
| 440 | |
| 441 | @Override |
| 442 | public LinkResourceAllocations getAllocations(IntentId intentId) { |
| 443 | checkNotNull(intentId); |
| 444 | Versioned<LinkResourceAllocations> alloc = null; |
| 445 | try { |
| 446 | alloc = intentAllocMap.get(intentId); |
| 447 | } catch (Exception e) { |
| 448 | log.warn("Could not read resource allocation information", e); |
| 449 | } |
| 450 | return alloc == null ? null : alloc.value(); |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public Iterable<LinkResourceAllocations> getAllocations(Link link) { |
| 455 | checkNotNull(link); |
| 456 | TransactionContext tx = getTxContext(); |
| 457 | Iterable<LinkResourceAllocations> res = null; |
| 458 | tx.begin(); |
| 459 | try { |
| 460 | res = getAllocations(tx, link); |
| 461 | } finally { |
| 462 | tx.abort(); |
| 463 | } |
| 464 | return res == null ? Collections.emptyList() : res; |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public Iterable<LinkResourceAllocations> getAllocations() { |
| 469 | try { |
| 470 | Set<LinkResourceAllocations> allocs = |
| 471 | intentAllocMap.values().stream().map(Versioned::value).collect(Collectors.toSet()); |
| 472 | return ImmutableSet.copyOf(allocs); |
| 473 | } catch (Exception e) { |
| 474 | log.warn("Could not read resource allocation information", e); |
| 475 | } |
| 476 | return ImmutableSet.of(); |
| 477 | } |
| 478 | |
| 479 | private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, Link link) { |
| 480 | checkNotNull(tx); |
| 481 | checkNotNull(link); |
| 482 | final LinkKey key = LinkKey.linkKey(link); |
| 483 | TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx); |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 484 | |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 485 | List<LinkResourceAllocations> res = linkAllocs.get(key); |
| 486 | if (res != null) { |
| 487 | return res; |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 488 | } |
Sho SHIMIZU | 88d36e2 | 2015-10-02 14:23:46 -0700 | [diff] [blame] | 489 | |
| 490 | res = linkAllocs.putIfAbsent(key, new ArrayList<>()); |
| 491 | if (res == null) { |
| 492 | return Collections.emptyList(); |
| 493 | } else { |
| 494 | return res; |
| 495 | } |
Ayaka Koshibe | e114f04 | 2015-05-01 11:43:00 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | } |