2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.resource.impl;
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;
26 import java.util.stream.Collectors;
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;
34 import org.onlab.util.Bandwidth;
35 import org.onosproject.net.OmsPort;
36 import org.onosproject.net.device.DeviceService;
37 import org.slf4j.Logger;
38 import org.onlab.util.PositionalParameterStringFormatter;
39 import org.onosproject.net.Link;
40 import org.onosproject.net.LinkKey;
41 import org.onosproject.net.Port;
42 import org.onosproject.net.intent.IntentId;
43 import org.onosproject.net.link.LinkService;
44 import org.onosproject.net.resource.link.BandwidthResource;
45 import org.onosproject.net.resource.link.BandwidthResourceAllocation;
46 import org.onosproject.net.resource.link.LambdaResource;
47 import org.onosproject.net.resource.link.LambdaResourceAllocation;
48 import org.onosproject.net.resource.link.LinkResourceAllocations;
49 import org.onosproject.net.resource.link.LinkResourceEvent;
50 import org.onosproject.net.resource.link.LinkResourceStore;
51 import org.onosproject.net.resource.link.LinkResourceStoreDelegate;
52 import org.onosproject.net.resource.link.MplsLabel;
53 import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
54 import org.onosproject.net.resource.ResourceAllocation;
55 import org.onosproject.net.resource.ResourceAllocationException;
56 import org.onosproject.net.resource.ResourceType;
57 import org.onosproject.store.AbstractStore;
58 import org.onosproject.store.serializers.KryoNamespaces;
59 import org.onosproject.store.service.ConsistentMap;
60 import org.onosproject.store.service.Serializer;
61 import org.onosproject.store.service.StorageService;
62 import org.onosproject.store.service.TransactionContext;
63 import org.onosproject.store.service.TransactionException;
64 import org.onosproject.store.service.TransactionalMap;
65 import org.onosproject.store.service.Versioned;
67 import com.google.common.collect.ImmutableList;
68 import com.google.common.collect.ImmutableSet;
69 import com.google.common.collect.Sets;
71 import static com.google.common.base.Preconditions.checkNotNull;
72 import static com.google.common.base.Preconditions.checkState;
73 import static org.slf4j.LoggerFactory.getLogger;
74 import static org.onosproject.net.AnnotationKeys.BANDWIDTH;
77 * Store that manages link resources using Copycat-backed TransactionalMaps.
79 @Component(immediate = true, enabled = true)
81 public class ConsistentLinkResourceStore extends
82 AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements
85 private final Logger log = getLogger(getClass());
87 private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
88 private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0));
90 // Smallest non-reserved MPLS label
91 private static final int MIN_UNRESERVED_LABEL = 0x10;
92 // Max non-reserved MPLS label = 239
93 private static final int MAX_UNRESERVED_LABEL = 0xEF;
95 // table to store current allocations
96 /** LinkKey -> List<LinkResourceAllocations>. */
97 private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations";
99 /** IntentId -> LinkResourceAllocations. */
100 private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations";
102 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
104 // for reading committed values.
105 private ConsistentMap<IntentId, LinkResourceAllocations> intentAllocMap;
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected StorageService storageService;
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected LinkService linkService;
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
114 protected DeviceService deviceService;
117 public void activate() {
118 intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder()
119 .withName(INTENT_ALLOCATIONS)
120 .withSerializer(SERIALIZER)
126 public void deactivate() {
130 private TransactionalMap<IntentId, LinkResourceAllocations> getIntentAllocs(TransactionContext tx) {
131 return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER);
134 private TransactionalMap<LinkKey, List<LinkResourceAllocations>> getLinkAllocs(TransactionContext tx) {
135 return tx.getTransactionalMap(LINK_RESOURCE_ALLOCATIONS, SERIALIZER);
138 private TransactionContext getTxContext() {
139 return storageService.transactionContextBuilder().build();
142 private Set<? extends ResourceAllocation> getResourceCapacity(ResourceType type, Link link) {
143 if (type == ResourceType.BANDWIDTH) {
144 return ImmutableSet.of(getBandwidthResourceCapacity(link));
146 if (type == ResourceType.LAMBDA) {
147 return getLambdaResourceCapacity(link);
149 if (type == ResourceType.MPLS_LABEL) {
150 return getMplsResourceCapacity();
152 return ImmutableSet.of();
155 private Set<LambdaResourceAllocation> getLambdaResourceCapacity(Link link) {
156 Set<LambdaResourceAllocation> allocations = new HashSet<>();
157 Port port = deviceService.getPort(link.src().deviceId(), link.src().port());
158 if (port instanceof OmsPort) {
159 OmsPort omsPort = (OmsPort) port;
161 // Assume fixed grid for now
162 for (int i = 0; i < omsPort.totalChannels(); i++) {
163 allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
169 private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) {
171 // if Link annotation exist, use them
172 // if all fails, use DEFAULT_BANDWIDTH
173 BandwidthResource bandwidth = null;
174 String strBw = link.annotations().value(BANDWIDTH);
177 bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw)));
178 } catch (NumberFormatException e) {
184 if (bandwidth == null) {
185 // fall back, use fixed default
186 bandwidth = DEFAULT_BANDWIDTH;
188 return new BandwidthResourceAllocation(bandwidth);
191 private Set<MplsLabelResourceAllocation> getMplsResourceCapacity() {
192 Set<MplsLabelResourceAllocation> allocations = new HashSet<>();
193 //Ignoring reserved labels of 0 through 15
194 for (int i = MIN_UNRESERVED_LABEL; i <= MAX_UNRESERVED_LABEL; i++) {
195 allocations.add(new MplsLabelResourceAllocation(MplsLabel
202 private Map<ResourceType, Set<? extends ResourceAllocation>> getResourceCapacity(Link link) {
203 Map<ResourceType, Set<? extends ResourceAllocation>> caps = new HashMap<>();
204 for (ResourceType type : ResourceType.values()) {
205 Set<? extends ResourceAllocation> cap = getResourceCapacity(type, link);
214 public Set<ResourceAllocation> getFreeResources(Link link) {
215 TransactionContext tx = getTxContext();
219 Map<ResourceType, Set<? extends ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link);
220 Set<ResourceAllocation> allFree = new HashSet<>();
221 freeResources.values().forEach(allFree::addAll);
228 private Map<ResourceType, Set<? extends ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) {
232 Map<ResourceType, Set<? extends ResourceAllocation>> free = new HashMap<>();
233 final Map<ResourceType, Set<? extends ResourceAllocation>> caps = getResourceCapacity(link);
234 final Iterable<LinkResourceAllocations> allocations = getAllocations(tx, link);
236 for (ResourceType type : ResourceType.values()) {
237 // there should be class/category of resources
241 Set<? extends ResourceAllocation> bw = caps.get(type);
242 if (bw == null || bw.isEmpty()) {
243 bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW));
246 BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next();
247 double freeBw = cap.bandwidth().toDouble();
249 // enumerate current allocations, subtracting resources
250 for (LinkResourceAllocations alloc : allocations) {
251 Set<ResourceAllocation> types = alloc.getResourceAllocation(link);
252 for (ResourceAllocation a : types) {
253 if (a instanceof BandwidthResourceAllocation) {
254 BandwidthResourceAllocation bwA = (BandwidthResourceAllocation) a;
255 freeBw -= bwA.bandwidth().toDouble();
260 free.put(type, Sets.newHashSet(
261 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw)))));
264 Set<? extends ResourceAllocation> lmd = caps.get(type);
265 if (lmd == null || lmd.isEmpty()) {
269 Set<LambdaResourceAllocation> freeL = new HashSet<>();
270 for (ResourceAllocation r : lmd) {
271 if (r instanceof LambdaResourceAllocation) {
272 freeL.add((LambdaResourceAllocation) r);
276 // enumerate current allocations, removing resources
277 for (LinkResourceAllocations alloc : allocations) {
278 Set<ResourceAllocation> types = alloc.getResourceAllocation(link);
279 for (ResourceAllocation a : types) {
280 if (a instanceof LambdaResourceAllocation) {
286 free.put(type, freeL);
289 Set<? extends ResourceAllocation> mpls = caps.get(type);
290 if (mpls == null || mpls.isEmpty()) {
294 Set<MplsLabelResourceAllocation> freeLabel = new HashSet<>();
295 for (ResourceAllocation r : mpls) {
296 if (r instanceof MplsLabelResourceAllocation) {
297 freeLabel.add((MplsLabelResourceAllocation) r);
301 // enumerate current allocations, removing resources
302 for (LinkResourceAllocations alloc : allocations) {
303 Set<ResourceAllocation> types = alloc.getResourceAllocation(link);
304 for (ResourceAllocation a : types) {
305 if (a instanceof MplsLabelResourceAllocation) {
311 free.put(type, freeLabel);
314 log.debug("unsupported ResourceType {}", type);
322 public void allocateResources(LinkResourceAllocations allocations) {
323 checkNotNull(allocations);
324 TransactionContext tx = getTxContext();
328 TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
329 intentAllocs.put(allocations.intentId(), allocations);
330 allocations.links().forEach(link -> allocateLinkResource(tx, link, allocations));
332 } catch (Exception e) {
333 log.error("Exception thrown, rolling back", e);
339 private void allocateLinkResource(TransactionContext tx, Link link,
340 LinkResourceAllocations allocations) {
341 // requested resources
342 Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link);
343 Map<ResourceType, Set<? extends ResourceAllocation>> available = getFreeResourcesEx(tx, link);
344 for (ResourceAllocation req : reqs) {
345 Set<? extends ResourceAllocation> avail = available.get(req.type());
346 if (req instanceof BandwidthResourceAllocation) {
347 // check if allocation should be accepted
348 if (avail.isEmpty()) {
349 checkState(!avail.isEmpty(),
350 "There's no Bandwidth resource on %s?",
353 BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next();
354 double bwLeft = bw.bandwidth().toDouble();
355 BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req);
356 bwLeft -= bwReq.bandwidth().toDouble();
358 throw new ResourceAllocationException(
359 PositionalParameterStringFormatter.format(
360 "Unable to allocate bandwidth for link {} "
361 + " requested amount is {} current allocation is {}",
363 bwReq.bandwidth().toDouble(),
366 } else if (req instanceof LambdaResourceAllocation) {
367 LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req;
368 // check if allocation should be accepted
369 if (!avail.contains(req)) {
370 // requested lambda was not available
371 throw new ResourceAllocationException(
372 PositionalParameterStringFormatter.format(
373 "Unable to allocate lambda for link {} lambda is {}",
375 lambdaAllocation.lambda().toInt()));
377 } else if (req instanceof MplsLabelResourceAllocation) {
378 MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req;
379 if (!avail.contains(req)) {
380 throw new ResourceAllocationException(
381 PositionalParameterStringFormatter
382 .format("Unable to allocate MPLS label for link "
383 + "{} MPLS label is {}",
391 // all requests allocatable => add allocation
392 final LinkKey linkKey = LinkKey.linkKey(link);
393 TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
394 List<LinkResourceAllocations> before = linkAllocs.get(linkKey);
395 if (before == null) {
396 List<LinkResourceAllocations> after = new ArrayList<>();
397 after.add(allocations);
398 before = linkAllocs.putIfAbsent(linkKey, after);
399 if (before != null) {
400 // concurrent allocation detected, retry transaction : is this needed?
401 log.warn("Concurrent Allocation, retrying");
402 throw new TransactionException();
405 List<LinkResourceAllocations> after = new ArrayList<>(before.size() + 1);
406 after.addAll(before);
407 after.add(allocations);
408 linkAllocs.replace(linkKey, before, after);
413 public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
414 checkNotNull(allocations);
416 final IntentId intentId = allocations.intentId();
417 final Collection<Link> links = allocations.links();
418 boolean success = false;
420 TransactionContext tx = getTxContext();
423 TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
424 intentAllocs.remove(intentId);
426 TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
427 links.forEach(link -> {
428 final LinkKey linkId = LinkKey.linkKey(link);
430 List<LinkResourceAllocations> before = linkAllocs.get(linkId);
431 if (before == null || before.isEmpty()) {
432 // something is wrong, but it is already freed
433 log.warn("There was no resource left to release on {}", linkId);
436 List<LinkResourceAllocations> after = new ArrayList<>(before);
437 after.remove(allocations);
438 linkAllocs.replace(linkId, before, after);
442 } catch (TransactionException e) {
443 log.debug("Transaction failed, retrying", e);
445 } catch (Exception e) {
446 log.error("Exception thrown during releaseResource {}", allocations, e);
452 // Issue events to force recompilation of intents.
453 final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations);
454 return new LinkResourceEvent(
455 LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
461 public LinkResourceAllocations getAllocations(IntentId intentId) {
462 checkNotNull(intentId);
463 Versioned<LinkResourceAllocations> alloc = null;
465 alloc = intentAllocMap.get(intentId);
466 } catch (Exception e) {
467 log.warn("Could not read resource allocation information", e);
469 return alloc == null ? null : alloc.value();
473 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
475 TransactionContext tx = getTxContext();
476 Iterable<LinkResourceAllocations> res = null;
479 res = getAllocations(tx, link);
483 return res == null ? Collections.emptyList() : res;
487 public Iterable<LinkResourceAllocations> getAllocations() {
489 Set<LinkResourceAllocations> allocs =
490 intentAllocMap.values().stream().map(Versioned::value).collect(Collectors.toSet());
491 return ImmutableSet.copyOf(allocs);
492 } catch (Exception e) {
493 log.warn("Could not read resource allocation information", e);
495 return ImmutableSet.of();
498 private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, Link link) {
501 final LinkKey key = LinkKey.linkKey(link);
502 TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
503 List<LinkResourceAllocations> res = null;
505 res = linkAllocs.get(key);
507 res = linkAllocs.putIfAbsent(key, new ArrayList<>());
510 return Collections.emptyList();