Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction; |
| 2 | |
| 3 | import net.onrc.onos.core.util.IdBlock; |
| 4 | import net.onrc.onos.core.util.IdBlockAllocator; |
| 5 | import net.onrc.onos.core.util.UnavailableIdException; |
| 6 | |
| 7 | import static com.google.common.base.Preconditions.checkNotNull; |
| 8 | |
| 9 | /** |
| 10 | * Generates a global unique MatchActionIdId. |
| 11 | */ |
| 12 | public class MatchActionIdGeneratorWithIdBlockAllocator |
| 13 | implements MatchActionIdGenerator { |
| 14 | |
| 15 | private final IdBlockAllocator allocator; |
| 16 | private IdBlock idBlock; |
| 17 | |
| 18 | /** |
| 19 | * Creates a FlowId generator instance using specified ID block allocator. |
| 20 | * |
| 21 | * @param allocator the ID block allocator to be used |
| 22 | */ |
| 23 | public MatchActionIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) { |
| 24 | this.allocator = checkNotNull(allocator); |
| 25 | this.idBlock = allocator.allocateUniqueIdBlock(); |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | public synchronized MatchActionId getNewId() { |
| 30 | try { |
| 31 | return new MatchActionId(idBlock.getNextId()); |
| 32 | } catch (UnavailableIdException e) { |
| 33 | idBlock = allocator.allocateUniqueIdBlock(); |
| 34 | return new MatchActionId(idBlock.getNextId()); |
| 35 | } |
| 36 | } |
| 37 | } |