blob: 351c7a5fa97d68860a79d65c889b9f9ba97355e2 [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.
Sho SHIMIZU364cbac2015-10-29 15:47:35 -070075 *
76 * @deprecated in Emu Release
Ayaka Koshibee114f042015-05-01 11:43:00 -070077 */
Sho SHIMIZU364cbac2015-10-29 15:47:35 -070078@Deprecated
Ayaka Koshibe474ef5f2015-05-01 15:24:51 -070079@Component(immediate = true, enabled = true)
Ayaka Koshibee114f042015-05-01 11:43:00 -070080@Service
81public class ConsistentLinkResourceStore extends
82 AbstractStore<LinkResourceEvent, LinkResourceStoreDelegate> implements
83 LinkResourceStore {
84
85 private final Logger log = getLogger(getClass());
86
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070087 private static final BandwidthResource DEFAULT_BANDWIDTH = new BandwidthResource(Bandwidth.mbps(1_000));
88 private static final BandwidthResource EMPTY_BW = new BandwidthResource(Bandwidth.bps(0));
Ayaka Koshibee114f042015-05-01 11:43:00 -070089
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;
94
95 // table to store current allocations
96 /** LinkKey -> List<LinkResourceAllocations>. */
Ayaka Koshibebcb02372015-06-01 10:56:42 -070097 private static final String LINK_RESOURCE_ALLOCATIONS = "LinkAllocations";
Ayaka Koshibee114f042015-05-01 11:43:00 -070098
99 /** IntentId -> LinkResourceAllocations. */
Ayaka Koshibebcb02372015-06-01 10:56:42 -0700100 private static final String INTENT_ALLOCATIONS = "LinkIntentAllocations";
Ayaka Koshibee114f042015-05-01 11:43:00 -0700101
Sho SHIMIZU200a7392015-07-23 16:28:35 -0700102 private static final Serializer SERIALIZER = Serializer.using(KryoNamespaces.API);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700103
104 // for reading committed values.
105 private ConsistentMap<IntentId, LinkResourceAllocations> intentAllocMap;
106
107 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
108 protected StorageService storageService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700111 protected DeviceService deviceService;
112
Ayaka Koshibee114f042015-05-01 11:43:00 -0700113 @Activate
114 public void activate() {
115 intentAllocMap = storageService.<IntentId, LinkResourceAllocations>consistentMapBuilder()
116 .withName(INTENT_ALLOCATIONS)
117 .withSerializer(SERIALIZER)
118 .build();
119 log.info("Started");
120 }
121
122 @Deactivate
123 public void deactivate() {
124 log.info("Stopped");
125 }
126
127 private TransactionalMap<IntentId, LinkResourceAllocations> getIntentAllocs(TransactionContext tx) {
128 return tx.getTransactionalMap(INTENT_ALLOCATIONS, SERIALIZER);
129 }
130
131 private TransactionalMap<LinkKey, List<LinkResourceAllocations>> getLinkAllocs(TransactionContext tx) {
132 return tx.getTransactionalMap(LINK_RESOURCE_ALLOCATIONS, SERIALIZER);
133 }
134
135 private TransactionContext getTxContext() {
136 return storageService.transactionContextBuilder().build();
137 }
138
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700139 private Set<ResourceAllocation> getResourceCapacity(ResourceType type, Link link) {
Sho SHIMIZU04ae54a2015-10-05 15:46:28 -0700140 switch (type) {
141 case BANDWIDTH:
142 return ImmutableSet.of(getBandwidthResourceCapacity(link));
143 case LAMBDA:
144 return getLambdaResourceCapacity(link);
145 case MPLS_LABEL:
146 return getMplsResourceCapacity();
147 default:
148 return ImmutableSet.of();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700149 }
Ayaka Koshibee114f042015-05-01 11:43:00 -0700150 }
151
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700152 private Set<ResourceAllocation> getLambdaResourceCapacity(Link link) {
Ayaka Koshibe8117f362015-06-02 13:44:01 -0700153 Port port = deviceService.getPort(link.src().deviceId(), link.src().port());
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700154 if (!(port instanceof OmsPort)) {
155 return Collections.emptySet();
156 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700157
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700158 OmsPort omsPort = (OmsPort) port;
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700159 Set<ResourceAllocation> allocations = new HashSet<>();
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700160 // Assume fixed grid for now
161 for (int i = 0; i < omsPort.totalChannels(); i++) {
162 allocations.add(new LambdaResourceAllocation(LambdaResource.valueOf(i)));
Ayaka Koshibee114f042015-05-01 11:43:00 -0700163 }
164 return allocations;
165 }
166
167 private BandwidthResourceAllocation getBandwidthResourceCapacity(Link link) {
168
169 // if Link annotation exist, use them
170 // if all fails, use DEFAULT_BANDWIDTH
Sho SHIMIZUa890e7c2015-10-02 14:48:13 -0700171 BandwidthResource bandwidth = DEFAULT_BANDWIDTH;
Ayaka Koshibee114f042015-05-01 11:43:00 -0700172 String strBw = link.annotations().value(BANDWIDTH);
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700173 if (strBw == null) {
174 return new BandwidthResourceAllocation(bandwidth);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700175 }
176
Sho SHIMIZUa005a6e52015-10-02 15:13:27 -0700177 try {
178 bandwidth = new BandwidthResource(Bandwidth.mbps(Double.parseDouble(strBw)));
179 } catch (NumberFormatException e) {
180 // do nothings, use default bandwidth
181 bandwidth = DEFAULT_BANDWIDTH;
182 }
Ayaka Koshibee114f042015-05-01 11:43:00 -0700183 return new BandwidthResourceAllocation(bandwidth);
184 }
185
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700186 private Set<ResourceAllocation> getMplsResourceCapacity() {
187 Set<ResourceAllocation> allocations = new HashSet<>();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700188 //Ignoring reserved labels of 0 through 15
189 for (int i = MIN_UNRESERVED_LABEL; i <= MAX_UNRESERVED_LABEL; i++) {
190 allocations.add(new MplsLabelResourceAllocation(MplsLabel
191 .valueOf(i)));
192
193 }
194 return allocations;
195 }
196
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700197 private Map<ResourceType, Set<ResourceAllocation>> getResourceCapacity(Link link) {
198 Map<ResourceType, Set<ResourceAllocation>> caps = new HashMap<>();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700199 for (ResourceType type : ResourceType.values()) {
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700200 Set<ResourceAllocation> cap = getResourceCapacity(type, link);
Sho SHIMIZUf618cb02015-10-02 14:43:50 -0700201 caps.put(type, cap);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700202 }
203 return caps;
204 }
205
206 @Override
207 public Set<ResourceAllocation> getFreeResources(Link link) {
208 TransactionContext tx = getTxContext();
209
210 tx.begin();
211 try {
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700212 Map<ResourceType, Set<ResourceAllocation>> freeResources = getFreeResourcesEx(tx, link);
Sho SHIMIZUeca9f352015-10-05 15:55:24 -0700213 return freeResources.values().stream()
214 .flatMap(Collection::stream)
215 .collect(Collectors.toSet());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700216 } finally {
217 tx.abort();
218 }
219 }
220
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700221 private Map<ResourceType, Set<ResourceAllocation>> getFreeResourcesEx(TransactionContext tx, Link link) {
Ayaka Koshibee114f042015-05-01 11:43:00 -0700222 checkNotNull(tx);
223 checkNotNull(link);
224
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700225 Map<ResourceType, Set<ResourceAllocation>> free = new HashMap<>();
226 final Map<ResourceType, Set<ResourceAllocation>> caps = getResourceCapacity(link);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700227 final List<LinkResourceAllocations> allocations = ImmutableList.copyOf(getAllocations(tx, link));
Ayaka Koshibee114f042015-05-01 11:43:00 -0700228
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700229 Set<ResourceAllocation> bw = caps.get(ResourceType.BANDWIDTH);
230 Set<ResourceAllocation> value = getFreeBandwidthResources(link, bw, allocations);
231 free.put(ResourceType.BANDWIDTH, value);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700232
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700233 Set<ResourceAllocation> lmd = caps.get(ResourceType.LAMBDA);
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700234 Set<ResourceAllocation> freeL = getFreeResources(link, lmd, allocations,
235 LambdaResourceAllocation.class);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700236 free.put(ResourceType.LAMBDA, freeL);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700237
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700238 Set<ResourceAllocation> mpls = caps.get(ResourceType.MPLS_LABEL);
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700239 Set<ResourceAllocation> freeLabel = getFreeResources(link, mpls, allocations,
240 MplsLabelResourceAllocation.class);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700241 free.put(ResourceType.MPLS_LABEL, freeLabel);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700242
Ayaka Koshibee114f042015-05-01 11:43:00 -0700243 return free;
244 }
245
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700246 private Set<ResourceAllocation> getFreeBandwidthResources(Link link, Set<ResourceAllocation> bw,
247 List<LinkResourceAllocations> allocations) {
248 if (bw == null || bw.isEmpty()) {
249 bw = Sets.newHashSet(new BandwidthResourceAllocation(EMPTY_BW));
250 }
251
252 BandwidthResourceAllocation cap = (BandwidthResourceAllocation) bw.iterator().next();
253 double freeBw = cap.bandwidth().toDouble();
254
255 // enumerate current allocations, subtracting resources
256 double allocatedBw = allocations.stream()
257 .flatMap(x -> x.getResourceAllocation(link).stream())
258 .filter(x -> x instanceof BandwidthResourceAllocation)
259 .map(x -> (BandwidthResourceAllocation) x)
260 .mapToDouble(x -> x.bandwidth().toDouble())
261 .sum();
262 freeBw -= allocatedBw;
263 return Sets.newHashSet(
264 new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(freeBw))));
265 }
266
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700267 private Set<ResourceAllocation> getFreeResources(Link link,
268 Set<ResourceAllocation> resources,
269 List<LinkResourceAllocations> allocations,
270 Class<? extends ResourceAllocation> cls) {
271 if (resources == null || resources.isEmpty()) {
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700272 // nothing left
273 return Collections.emptySet();
274 }
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700275 Set<ResourceAllocation> freeL = resources.stream()
276 .filter(cls::isInstance)
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700277 .collect(Collectors.toSet());
278
279 // enumerate current allocations, removing resources
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700280 List<ResourceAllocation> allocated = allocations.stream()
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700281 .flatMap(x -> x.getResourceAllocation(link).stream())
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700282 .filter(cls::isInstance)
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700283 .collect(Collectors.toList());
Sho SHIMIZU1bd72c22015-10-06 10:52:12 -0700284 freeL.removeAll(allocated);
Sho SHIMIZU48588a72015-10-05 17:00:48 -0700285 return freeL;
286 }
287
Ayaka Koshibee114f042015-05-01 11:43:00 -0700288 @Override
289 public void allocateResources(LinkResourceAllocations allocations) {
290 checkNotNull(allocations);
291 TransactionContext tx = getTxContext();
292
293 tx.begin();
294 try {
295 TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
296 intentAllocs.put(allocations.intentId(), allocations);
297 allocations.links().forEach(link -> allocateLinkResource(tx, link, allocations));
298 tx.commit();
Sho SHIMIZU1e0a34c2015-11-02 16:52:29 -0800299 } catch (ResourceAllocationException e) {
Sho SHIMIZUe289f432015-10-06 19:33:16 -0700300 log.error("Exception thrown, rolling back", e);
301 tx.abort();
Ayaka Koshibee114f042015-05-01 11:43:00 -0700302 } catch (Exception e) {
303 log.error("Exception thrown, rolling back", e);
304 tx.abort();
305 throw e;
306 }
307 }
308
309 private void allocateLinkResource(TransactionContext tx, Link link,
310 LinkResourceAllocations allocations) {
311 // requested resources
312 Set<ResourceAllocation> reqs = allocations.getResourceAllocation(link);
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700313 Map<ResourceType, Set<ResourceAllocation>> available = getFreeResourcesEx(tx, link);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700314 for (ResourceAllocation req : reqs) {
Sho SHIMIZUe1b463b2015-10-05 15:39:19 -0700315 Set<ResourceAllocation> avail = available.get(req.type());
Ayaka Koshibee114f042015-05-01 11:43:00 -0700316 if (req instanceof BandwidthResourceAllocation) {
317 // check if allocation should be accepted
318 if (avail.isEmpty()) {
Sho SHIMIZUe289f432015-10-06 19:33:16 -0700319 throw new ResourceAllocationException(String.format("There's no Bandwidth resource on %s?", link));
Ayaka Koshibee114f042015-05-01 11:43:00 -0700320 }
321 BandwidthResourceAllocation bw = (BandwidthResourceAllocation) avail.iterator().next();
322 double bwLeft = bw.bandwidth().toDouble();
323 BandwidthResourceAllocation bwReq = ((BandwidthResourceAllocation) req);
324 bwLeft -= bwReq.bandwidth().toDouble();
325 if (bwLeft < 0) {
326 throw new ResourceAllocationException(
327 PositionalParameterStringFormatter.format(
328 "Unable to allocate bandwidth for link {} "
329 + " requested amount is {} current allocation is {}",
330 link,
331 bwReq.bandwidth().toDouble(),
332 bw));
333 }
334 } else if (req instanceof LambdaResourceAllocation) {
335 LambdaResourceAllocation lambdaAllocation = (LambdaResourceAllocation) req;
336 // check if allocation should be accepted
337 if (!avail.contains(req)) {
338 // requested lambda was not available
339 throw new ResourceAllocationException(
340 PositionalParameterStringFormatter.format(
341 "Unable to allocate lambda for link {} lambda is {}",
342 link,
343 lambdaAllocation.lambda().toInt()));
344 }
345 } else if (req instanceof MplsLabelResourceAllocation) {
346 MplsLabelResourceAllocation mplsAllocation = (MplsLabelResourceAllocation) req;
347 if (!avail.contains(req)) {
348 throw new ResourceAllocationException(
349 PositionalParameterStringFormatter
350 .format("Unable to allocate MPLS label for link "
351 + "{} MPLS label is {}",
352 link,
353 mplsAllocation
354 .mplsLabel()
355 .toString()));
356 }
357 }
358 }
359 // all requests allocatable => add allocation
360 final LinkKey linkKey = LinkKey.linkKey(link);
361 TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
362 List<LinkResourceAllocations> before = linkAllocs.get(linkKey);
363 if (before == null) {
364 List<LinkResourceAllocations> after = new ArrayList<>();
365 after.add(allocations);
Sho SHIMIZU1dcef072015-10-07 17:52:39 -0700366 linkAllocs.putIfAbsent(linkKey, after);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700367 } else {
Sho SHIMIZU0d037612015-10-07 15:36:31 -0700368 boolean overlapped = before.stream()
369 .flatMap(x -> x.getResourceAllocation(link).stream())
370 .anyMatch(x -> allocations.getResourceAllocation(link).contains(x));
371 if (overlapped) {
372 throw new ResourceAllocationException(
373 String.format("Resource allocations are overlapped between %s and %s",
374 before, allocations)
375 );
376 }
Ayaka Koshibee114f042015-05-01 11:43:00 -0700377 List<LinkResourceAllocations> after = new ArrayList<>(before.size() + 1);
378 after.addAll(before);
379 after.add(allocations);
380 linkAllocs.replace(linkKey, before, after);
381 }
382 }
383
384 @Override
385 public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
386 checkNotNull(allocations);
387
388 final IntentId intentId = allocations.intentId();
389 final Collection<Link> links = allocations.links();
390 boolean success = false;
391 do {
392 TransactionContext tx = getTxContext();
393 tx.begin();
394 try {
395 TransactionalMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
396 intentAllocs.remove(intentId);
397
398 TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
399 links.forEach(link -> {
400 final LinkKey linkId = LinkKey.linkKey(link);
401
402 List<LinkResourceAllocations> before = linkAllocs.get(linkId);
403 if (before == null || before.isEmpty()) {
404 // something is wrong, but it is already freed
405 log.warn("There was no resource left to release on {}", linkId);
406 return;
407 }
408 List<LinkResourceAllocations> after = new ArrayList<>(before);
409 after.remove(allocations);
410 linkAllocs.replace(linkId, before, after);
411 });
Sho SHIMIZU1e0a34c2015-11-02 16:52:29 -0800412 success = tx.commit();
413 } catch (Exception e) {
Ayaka Koshibee114f042015-05-01 11:43:00 -0700414 log.error("Exception thrown during releaseResource {}", allocations, e);
415 tx.abort();
416 throw e;
417 }
418 } while (!success);
419
420 // Issue events to force recompilation of intents.
421 final List<LinkResourceAllocations> releasedResources = ImmutableList.of(allocations);
422 return new LinkResourceEvent(
423 LinkResourceEvent.Type.ADDITIONAL_RESOURCES_AVAILABLE,
424 releasedResources);
425
426 }
427
428 @Override
429 public LinkResourceAllocations getAllocations(IntentId intentId) {
430 checkNotNull(intentId);
431 Versioned<LinkResourceAllocations> alloc = null;
432 try {
433 alloc = intentAllocMap.get(intentId);
434 } catch (Exception e) {
435 log.warn("Could not read resource allocation information", e);
436 }
437 return alloc == null ? null : alloc.value();
438 }
439
440 @Override
441 public Iterable<LinkResourceAllocations> getAllocations(Link link) {
442 checkNotNull(link);
443 TransactionContext tx = getTxContext();
444 Iterable<LinkResourceAllocations> res = null;
445 tx.begin();
446 try {
447 res = getAllocations(tx, link);
448 } finally {
449 tx.abort();
450 }
451 return res == null ? Collections.emptyList() : res;
452 }
453
454 @Override
455 public Iterable<LinkResourceAllocations> getAllocations() {
456 try {
457 Set<LinkResourceAllocations> allocs =
458 intentAllocMap.values().stream().map(Versioned::value).collect(Collectors.toSet());
459 return ImmutableSet.copyOf(allocs);
460 } catch (Exception e) {
461 log.warn("Could not read resource allocation information", e);
462 }
463 return ImmutableSet.of();
464 }
465
466 private Iterable<LinkResourceAllocations> getAllocations(TransactionContext tx, Link link) {
467 checkNotNull(tx);
468 checkNotNull(link);
469 final LinkKey key = LinkKey.linkKey(link);
470 TransactionalMap<LinkKey, List<LinkResourceAllocations>> linkAllocs = getLinkAllocs(tx);
Ayaka Koshibee114f042015-05-01 11:43:00 -0700471
Sho SHIMIZU88d36e22015-10-02 14:23:46 -0700472 List<LinkResourceAllocations> res = linkAllocs.get(key);
473 if (res != null) {
474 return res;
Ayaka Koshibee114f042015-05-01 11:43:00 -0700475 }
Sho SHIMIZU88d36e22015-10-02 14:23:46 -0700476
477 res = linkAllocs.putIfAbsent(key, new ArrayList<>());
478 if (res == null) {
479 return Collections.emptyList();
480 } else {
481 return res;
482 }
Ayaka Koshibee114f042015-05-01 11:43:00 -0700483 }
484
485}