Add FlowIdGenerator's implementation class.
- Updated FlowId to use long internally
- Implemented FlowIdGeneratorWithIdBlockAllocator class as an implementation class of FlowIdGenerator.
Change-Id: Id3bcb47c63217b0ea4a2f7d5ae208532783c323a
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java
new file mode 100644
index 0000000..d502d93
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java
@@ -0,0 +1,38 @@
+package net.onrc.onos.core.flowmanager;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import net.onrc.onos.api.flowmanager.FlowId;
+import net.onrc.onos.api.flowmanager.FlowIdGenerator;
+import net.onrc.onos.core.util.IdBlock;
+import net.onrc.onos.core.util.IdBlockAllocator;
+import net.onrc.onos.core.util.UnavailableIdException;
+
+/**
+ * Generates a global unique FlowId using
+ * {@link IdBlockAllocator#allocateUniqueIdBlock()}.
+ */
+public class FlowIdGeneratorWithIdBlockAllocator implements FlowIdGenerator {
+
+ private final IdBlockAllocator allocator;
+ private IdBlock idBlock;
+
+ /**
+ * Creates a FlowId generator instance using specified ID block allocator.
+ *
+ * @param allocator the ID block allocator to be used
+ */
+ public FlowIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
+ this.allocator = checkNotNull(allocator);
+ this.idBlock = allocator.allocateUniqueIdBlock();
+ }
+
+ @Override
+ public synchronized FlowId getNewId() {
+ try {
+ return new FlowId(idBlock.getNextId());
+ } catch (UnavailableIdException e) {
+ idBlock = allocator.allocateUniqueIdBlock();
+ return new FlowId(idBlock.getNextId());
+ }
+ }
+}