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