blob: 7646d6483aa98736990f34190c9336fb596408ea [file] [log] [blame]
Toshio Koide3c846312014-08-21 19:47:15 -07001package net.onrc.onos.core.flowmanager;
2
3import static com.google.common.base.Preconditions.checkNotNull;
4import net.onrc.onos.api.flowmanager.FlowBatchId;
5import net.onrc.onos.core.util.IdBlock;
6import net.onrc.onos.core.util.IdBlockAllocator;
7import net.onrc.onos.core.util.UnavailableIdException;
8
9/**
10 * Generates a global unique FlowBatchId using
11 * {@link IdBlockAllocator#allocateUniqueIdBlock()}.
12 */
13public class FlowBatchIdGeneratorWithIdBlockAllocator {
14 private final IdBlockAllocator allocator;
15 private IdBlock idBlock;
16
17 /**
18 * Creates a FlowBatchId generator instance using specified ID block allocator.
19 *
20 * @param allocator the ID block allocator to be used
21 */
22 public FlowBatchIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
23 this.allocator = checkNotNull(allocator);
24 this.idBlock = allocator.allocateUniqueIdBlock();
25 }
26
27 public synchronized FlowBatchId getNewId() {
28 try {
29 return new FlowBatchId(idBlock.getNextId());
30 } catch (UnavailableIdException e) {
31 idBlock = allocator.allocateUniqueIdBlock();
32 return new FlowBatchId(idBlock.getNextId());
33 }
34 }
35}