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