blob: d502d931ab622b6ecb07d1af39ac850f5383bc01 [file] [log] [blame]
Toshio Koide515ba842014-08-20 11:53:37 -07001package net.onrc.onos.core.flowmanager;
2
3import static com.google.common.base.Preconditions.checkNotNull;
4import net.onrc.onos.api.flowmanager.FlowId;
5import net.onrc.onos.api.flowmanager.FlowIdGenerator;
6import net.onrc.onos.core.util.IdBlock;
7import net.onrc.onos.core.util.IdBlockAllocator;
8import net.onrc.onos.core.util.UnavailableIdException;
9
10/**
11 * Generates a global unique FlowId using
12 * {@link IdBlockAllocator#allocateUniqueIdBlock()}.
13 */
14public 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}