blob: 9f2eceb2e5f03433a90c56fc1ec059b0901c3c4c [file] [log] [blame]
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07001package net.onrc.onos.core.newintent;
2
3import net.onrc.onos.api.newintent.IntentId;
4import net.onrc.onos.api.newintent.IntentIdGenerator;
5import net.onrc.onos.core.registry.IdBlock;
6import net.onrc.onos.core.util.UnavailableIdException;
7import net.onrc.onos.core.util.IdBlockAllocator;
8
9import static com.google.common.base.Preconditions.checkNotNull;
10
11/**
12 * An implementation of {@link IntentIdGenerator},
13 * which uses {@link IdBlockAllocator#allocateUniqueIdBlock()}.
14 */
15public class IdBlockAllocatorBasedIntentIdGenerator implements IntentIdGenerator {
16
17 private final IdBlockAllocator allocator;
18 private IdBlock idBlock;
19
20 public IdBlockAllocatorBasedIntentIdGenerator(IdBlockAllocator allocator) {
21 this.allocator = checkNotNull(allocator);
22 this.idBlock = allocator.allocateUniqueIdBlock();
23 }
24
25 @Override
26 public synchronized IntentId getNewId() {
27 try {
28 return new IntentId(idBlock.getNextId());
29 } catch (UnavailableIdException e) {
30 idBlock = allocator.allocateUniqueIdBlock();
31 return new IntentId(idBlock.getNextId());
32 }
33 }
34}