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