Change FlowBatchHandle to be interface class.
- Users can access to the shared flow batch operation map
using the FlowBatchHandle.
- FlowBatchHandle is an interface and the instance of it
can be obtained from APIs defined by FlowManagerService.
- The implementation of the map can be accessed only from
the implementation of the FlowBatchHandle.
- This task is a part of ONOS-1692 and ONOS-1842.
Change-Id: I651a2886d166765ca5aae6abcc2b844153ffb2bc
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchIdGeneratorWithIdBlockAllocator.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchIdGeneratorWithIdBlockAllocator.java
new file mode 100644
index 0000000..7646d64
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/flowmanager/FlowBatchIdGeneratorWithIdBlockAllocator.java
@@ -0,0 +1,35 @@
+package net.onrc.onos.core.flowmanager;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import net.onrc.onos.api.flowmanager.FlowBatchId;
+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 FlowBatchId using
+ * {@link IdBlockAllocator#allocateUniqueIdBlock()}.
+ */
+public class FlowBatchIdGeneratorWithIdBlockAllocator {
+ private final IdBlockAllocator allocator;
+ private IdBlock idBlock;
+
+ /**
+ * Creates a FlowBatchId generator instance using specified ID block allocator.
+ *
+ * @param allocator the ID block allocator to be used
+ */
+ public FlowBatchIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
+ this.allocator = checkNotNull(allocator);
+ this.idBlock = allocator.allocateUniqueIdBlock();
+ }
+
+ public synchronized FlowBatchId getNewId() {
+ try {
+ return new FlowBatchId(idBlock.getNextId());
+ } catch (UnavailableIdException e) {
+ idBlock = allocator.allocateUniqueIdBlock();
+ return new FlowBatchId(idBlock.getNextId());
+ }
+ }
+}