blob: a38550e4cafc58267cee4aa21185c93574fa195e [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
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 Koshibee114f042015-05-01 11:43:00 -070016package org.onosproject.store.resource.impl;
17
18import java.util.ArrayList;
19import java.util.Collection;
20import java.util.Collections;
21import java.util.HashMap;
22import java.util.HashSet;
23import java.util.List;
24import java.util.Map;
25import java.util.Set;
26import java.util.stream.Collectors;
27
28import org.apache.felix.scr.annotations.Component;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
32import org.apache.felix.scr.annotations.Activate;
33import org.apache.felix.scr.annotations.Deactivate;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070034import org.onlab.util.Bandwidth;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070035import org.onosproject.net.OmsPort;
36import org.onosproject.net.device.DeviceService;
Ayaka Koshibee114f042015-05-01 11:43:00 -070037import org.slf4j.Logger;
Ayaka Koshibee114f042015-05-01 11:43:00 -070038import org.onlab.util.PositionalParameterStringFormatter;
39import org.onosproject.net.Link;
40import org.onosproject.net.LinkKey;
Ayaka Koshibe8117f362015-06-02 13:44:01 -070041import org.onosproject.net.Port;
Ayaka Koshibee114f042015-05-01 11:43:00 -070042import org.onosproject.net.intent.IntentId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070043import org.onosproject.net.resource.link.BandwidthResource;
44import org.onosproject.net.resource.link.BandwidthResourceAllocation;
45import org.onosproject.net.resource.link.LambdaResource;
46import org.onosproject.net.resource.link.LambdaResourceAllocation;
47import org.onosproject.net.resource.link.LinkResourceAllocations;
48import org.onosproject.net.resource.link.LinkResourceEvent;
49import org.onosproject.net.resource.link.LinkResourceStore;
50import org.onosproject.net.resource.link.LinkResourceStoreDelegate;
51import org.onosproject.net.resource.link.MplsLabel;
52import org.onosproject.net.resource.link.MplsLabelResourceAllocation;
Ayaka Koshibee114f042015-05-01 11:43:00 -070053import org.onosproject.net.resource.ResourceAllocation;
54import org.onosproject.net.resource.ResourceAllocationException;
55import org.onosproject.net.resource.ResourceType;
56import org.onosproject.store.AbstractStore;
57import org.onosproject.store.serializers.KryoNamespaces;
58import org.onosproject.store.service.ConsistentMap;
59import org.onosproject.store.service.Serializer;
60import org.onosproject.store.service.StorageService;
61import org.onosproject.store.service.TransactionContext;
Ayaka Koshibee114f042015-05-01 11:43:00 -070062import org.onosproject.store.service.TransactionalMap;
63import org.onosproject.store.service.Versioned;
64
65import com.google.common.collect.ImmutableList;
66import com.google.common.collect.ImmutableSet;
67import com.google.common.collect.Sets;
68
69import static com.google.common.base.Preconditions.checkNotNull;
Ayaka Koshibee114f042015-05-01 11:43:00 -070070import static org.slf4j.LoggerFactory.getLogger;
71import static org.onosproject.net.AnnotationKeys.BANDWIDTH;
Ayaka Koshibee114f042015-05-01 11:43:00 -070072
73/**
74 * Store that manages link resources using Copycat-backed TransactionalMaps.
75 */
Ayaka Koshibe474ef5f2015-05-01 15:24:51 -070076@Component(immediate = true, enabled = true)
Ayaka Koshibee114f042015-05-01 11:43:00 -070077@Service
78public class ConsistentLinkResourceStore extends
79 AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements
80 LinkResourceStore {
81
82 private final Logger log = getLogger(getClass());
83
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070084 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 Koshibee114f042015-05-01 11:43:00 -070086
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 Koshibebcb02372015-06-01 10:56:42 -070094 private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations";
Ayaka Koshibee114f042015-05-01 11:43:00 -070095
96 /** IntentId -> LinkResourceAllocations. */
Ayaka Koshibebcb02372015-06-01 10:56:42 -070097 private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations";
Ayaka Koshibee114f042015-05-01 11:43:00 -070098
Sho SHIMIZU200a7392015-07-23 16:28:35 -070099 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700100
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 Leenheer1afa2a02015-05-13 09:18:07 -0700108 protected DeviceService deviceService;
109
Ayaka Koshibee114f042015-05-01 11:43:00 -0700110 @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 SHIMIZUe1b463b2015-10-05 15:39:19 -0700136 private Set<ResourceAllocation> getResourceCapacity(ResourceType type, Link link) {
Sho SHIMIZU04ae54a2015-10-05 15:46:28 -0700137 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 Koshibee114f042015-05-01 11:43:00 -0700146 }
Ayaka Koshibee114f042015-05-01 11:43:00 -0700147 }
148
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700149 private Set<ResourceAllocation> getLambdaResourceCapacity(Link link) {
Ayaka Koshibe8117f362015-06-02 13:44:01 -0700150 Port port = deviceService.getPort(link.src().deviceId(), link.src().port());
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700151 if (!(port instanceof OmsPort)) {
152 return Collections.emptySet();
153 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700154
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700155 OmsPort omsPort = (OmsPort) port;
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700156 Set<ResourceAllocation> allocations = new HashSet<>();
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700157 // Assume fixed grid for now
158 for (int i = 0; i < omsPort.totalChannels(); i++) {
159 allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
Ayaka Koshibee114f042015-05-01 11:43:00 -0700160 }
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 SHIMIZUa890e7c2015-10-02 14:48:13 -0700168 BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
Ayaka Koshibee114f042015-05-01 11:43:00 -0700169 String strBw = link.annotations().value(BANDWIDTH);
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700170 if (strBw == null) {
171 return new BandwidthResourceAllocation(bandwidth);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700172 }
173
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700174 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 Koshibee114f042015-05-01 11:43:00 -0700180 return new BandwidthResourceAllocation(bandwidth);
181 }
182
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700183 private Set<ResourceAllocation> getMplsResourceCapacity() {
184 Set<ResourceAllocation> allocations = new HashSet<>();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700185 //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 SHIMIZUe1b463b2015-10-05 15:39:19 -0700194 private Map<ResourceType, Set<ResourceAllocation>> getResourceCapacity(Link link) {
195 Map<ResourceType, Set<ResourceAllocation>> caps = new HashMap<>();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700196 for (ResourceType type : ResourceType.values()) {
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700197 Set<ResourceAllocation> cap = getResourceCapacity(type, link);
Sho SHIMIZUf618cb02015-10-02 14:43:50 -0700198 caps.put(type, cap);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700199 }
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 SHIMIZUe1b463b2015-10-05 15:39:19 -0700209 Map<ResourceType, Set<ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link);
Sho SHIMIZUeca9f352015-10-05 15:55:24 -0700210 return freeResources.values().stream()
211 .flatMap(Collection::stream)
212 .collect(Collectors.toSet());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700213 } finally {
214 tx.abort();
215 }
216 }
217
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700218 private Map<ResourceType, Set<ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) {
Ayaka Koshibee114f042015-05-01 11:43:00 -0700219 checkNotNull(tx);
220 checkNotNull(link);
221
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700222 Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>();
223 final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700224 final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link));
Ayaka Koshibee114f042015-05-01 11:43:00 -0700225
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700226 Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH);
227 Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations);
228 free.put(ResourceType.BANDWIDTH, value);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700229
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700230 Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700231 Set<ResourceAllocation> freeL = getFreeResources(link, lmd, allocations,
232 LambdaResourceAllocation.class);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700233 free.put(ResourceType.LAMBDA, freeL);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700234
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700235 Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700236 Set<ResourceAllocation> freeLabel = getFreeResources(link, mpls, allocations,
237 MplsLabelResourceAllocation.class);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700238 free.put(ResourceType.MPLS_LABEL, freeLabel);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700239
Ayaka Koshibee114f042015-05-01 11:43:00 -0700240 return free;
241 }
242
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700243 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 SHIMIZU1bd72c22015-10-06 10:52:12 -0700264 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 SHIMIZU48588a72015-10-05 17:00:48 -0700269 // nothing left
270 return Collections.emptySet();
271 }
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700272 Set<ResourceAllocation> freeL = resources.stream()
273 .filter(cls::isInstance)
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700274 .collect(Collectors.toSet());
275
276 // enumerate current allocations, removing resources
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700277 List<ResourceAllocation> allocated = allocations.stream()
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700278 .flatMap(x -> x.getResourceAllocation(link).stream())
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700279 .filter(cls::isInstance)
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700280 .collect(Collectors.toList());
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700281 freeL.removeAll(allocated);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700282 return freeL;
283 }
284
Ayaka Koshibee114f042015-05-01 11:43:00 -0700285 @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 SHIMIZU1e0a34c2015-11-02 16:52:29 -0800296 } catch (ResourceAllocationException e) {
Sho SHIMIZUe289f432015-10-06 19:33:16 -0700297 log.error("Exception thrown, rolling back", e);
298 tx.abort();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700299 } 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 SHIMIZUe1b463b2015-10-05 15:39:19 -0700310 Map<ResourceType, Set<ResourceAllocation>> available = getFreeResourcesEx(tx, link);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700311 for (ResourceAllocation req : reqs) {
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700312 Set<ResourceAllocation> avail = available.get(req.type());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700313 if (req instanceof BandwidthResourceAllocation) {
314 // check if allocation should be accepted
315 if (avail.isEmpty()) {
Sho SHIMIZUe289f432015-10-06 19:33:16 -0700316 throw new ResourceAllocationException(String.format("There's no Bandwidth resource on %s?", link));
Ayaka Koshibee114f042015-05-01 11:43:00 -0700317 }
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 SHIMIZU1dcef072015-10-07 17:52:39 -0700363 linkAllocs.putIfAbsent(linkKey, after);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700364 } else {
Sho SHIMIZU0d037612015-10-07 15:36:31 -0700365 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 Koshibee114f042015-05-01 11:43:00 -0700374 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 SHIMIZU1e0a34c2015-11-02 16:52:29 -0800409 success = tx.commit();
410 } catch (Exception e) {
Ayaka Koshibee114f042015-05-01 11:43:00 -0700411 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 Koshibee114f042015-05-01 11:43:00 -0700468
Sho SHIMIZU88d36e22015-10-02 14:23:46 -0700469 List<LinkResourceAllocations> res = linkAllocs.get(key);
470 if (res != null) {
471 return res;
Ayaka Koshibee114f042015-05-01 11:43:00 -0700472 }
Sho SHIMIZU88d36e22015-10-02 14:23:46 -0700473
474 res = linkAllocs.putIfAbsent(key, new ArrayList<>());
475 if (res == null) {
476 return Collections.emptyList();
477 } else {
478 return res;
479 }
Ayaka Koshibee114f042015-05-01 11:43:00 -0700480 }
481
482}