Extract common ID generation logic using IdBlockAllocator
- Define IdGenerator<T> interface
- Implement AbstractIdBlockAllocatorBasedIdGenerator<T>, which
has the common logic for the existing IntentId, FlowId, MatchActionId,
and MatchActionOperationsId generator implementations.
Change-Id: I7aeea249df1710719760ed477bffe62853577e0f
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java
index d502d93..e818932 100644
--- a/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java
+++ b/src/main/java/net/onrc/onos/core/flowmanager/FlowIdGeneratorWithIdBlockAllocator.java
@@ -1,20 +1,15 @@
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.AbstractBlockAllocatorBasedIdGenerator;
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;
+public class FlowIdGeneratorWithIdBlockAllocator
+ extends AbstractBlockAllocatorBasedIdGenerator<FlowId> {
/**
* Creates a FlowId generator instance using specified ID block allocator.
@@ -22,17 +17,11 @@
* @param allocator the ID block allocator to be used
*/
public FlowIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
- this.allocator = checkNotNull(allocator);
- this.idBlock = allocator.allocateUniqueIdBlock();
+ super(allocator);
}
@Override
- public synchronized FlowId getNewId() {
- try {
- return new FlowId(idBlock.getNextId());
- } catch (UnavailableIdException e) {
- idBlock = allocator.allocateUniqueIdBlock();
- return new FlowId(idBlock.getNextId());
- }
+ protected FlowId convertFrom(long value) {
+ return new FlowId(value);
}
}