blob: 2e0969828b42c3f0e8a11ce762bcd5179cda1293 [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;
Sho SHIMIZUfc932d52014-08-15 11:22:37 -07005import net.onrc.onos.core.util.IdBlock;
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -07006import 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
Sho SHIMIZU42b92812014-08-17 19:56:44 -070020 /**
Sho SHIMIZUa8fae012014-08-18 10:57:30 -070021 * Constructs an intent ID generator, which uses the specified ID block allocator
22 * to generate a global unique intent ID.
Sho SHIMIZU42b92812014-08-17 19:56:44 -070023 *
Sho SHIMIZUa8fae012014-08-18 10:57:30 -070024 * @param allocator the ID block allocator to use for generating intent IDs
Sho SHIMIZU42b92812014-08-17 19:56:44 -070025 */
Sho SHIMIZU9257b0c2014-08-13 15:00:10 -070026 public IdBlockAllocatorBasedIntentIdGenerator(IdBlockAllocator allocator) {
27 this.allocator = checkNotNull(allocator);
28 this.idBlock = allocator.allocateUniqueIdBlock();
29 }
30
31 @Override
32 public synchronized IntentId getNewId() {
33 try {
34 return new IntentId(idBlock.getNextId());
35 } catch (UnavailableIdException e) {
36 idBlock = allocator.allocateUniqueIdBlock();
37 return new IntentId(idBlock.getNextId());
38 }
39 }
40}