Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
| 3 | import net.onrc.onos.api.newintent.IntentId; |
| 4 | import net.onrc.onos.api.newintent.IntentIdGenerator; |
Sho SHIMIZU | fc932d5 | 2014-08-15 11:22:37 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.util.IdBlock; |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 6 | import net.onrc.onos.core.util.UnavailableIdException; |
| 7 | import net.onrc.onos.core.util.IdBlockAllocator; |
| 8 | |
| 9 | import static com.google.common.base.Preconditions.checkNotNull; |
| 10 | |
| 11 | /** |
| 12 | * An implementation of {@link IntentIdGenerator}, |
| 13 | * which uses {@link IdBlockAllocator#allocateUniqueIdBlock()}. |
| 14 | */ |
| 15 | public class IdBlockAllocatorBasedIntentIdGenerator implements IntentIdGenerator { |
| 16 | |
| 17 | private final IdBlockAllocator allocator; |
| 18 | private IdBlock idBlock; |
| 19 | |
Sho SHIMIZU | 42b9281 | 2014-08-17 19:56:44 -0700 | [diff] [blame] | 20 | /** |
Sho SHIMIZU | a8fae01 | 2014-08-18 10:57:30 -0700 | [diff] [blame] | 21 | * Constructs an intent ID generator, which uses the specified ID block allocator |
| 22 | * to generate a global unique intent ID. |
Sho SHIMIZU | 42b9281 | 2014-08-17 19:56:44 -0700 | [diff] [blame] | 23 | * |
Sho SHIMIZU | a8fae01 | 2014-08-18 10:57:30 -0700 | [diff] [blame] | 24 | * @param allocator the ID block allocator to use for generating intent IDs |
Sho SHIMIZU | 42b9281 | 2014-08-17 19:56:44 -0700 | [diff] [blame] | 25 | */ |
Sho SHIMIZU | 9257b0c | 2014-08-13 15:00:10 -0700 | [diff] [blame] | 26 | 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 | } |