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/api/flowmanager/Flow.java b/src/main/java/net/onrc/onos/api/flowmanager/Flow.java
index 8608adb..65dc00e 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/Flow.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/Flow.java
@@ -5,10 +5,11 @@
import java.util.List;
import net.onrc.onos.api.batchoperation.BatchOperationTarget;
-import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionId;
import net.onrc.onos.core.matchaction.MatchActionOperations;
-import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionOperationsId;
import net.onrc.onos.core.matchaction.match.Match;
+import net.onrc.onos.core.util.IdGenerator;
/**
* An abstract class to define flow object which is managed by
@@ -67,8 +68,8 @@
*/
public abstract List<MatchActionOperations> compile(
FlowBatchOperation.Operator op,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator);
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator);
/**
* Generates a hash code using the FlowId.
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java b/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java
index 3e94c7b..3bf9a30 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/FlowId.java
@@ -26,7 +26,8 @@
/**
* Creates new instance with string ID.
* <p>
- * This FlowId instance should be generated with {@link FlowIdGenerator}.
+ * This FlowId instance should be generated with
+ * {@link net.onrc.onos.core.util.IdGenerator} of flow ID.
*
* @param id String representation of the ID.
*/
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/FlowIdGenerator.java b/src/main/java/net/onrc/onos/api/flowmanager/FlowIdGenerator.java
deleted file mode 100644
index 9b85e22..0000000
--- a/src/main/java/net/onrc/onos/api/flowmanager/FlowIdGenerator.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package net.onrc.onos.api.flowmanager;
-
-/**
- * An generator of {@link FlowId}.
- */
-public interface FlowIdGenerator {
- /**
- * Generates a global unique {@link FlowId} instance.
- *
- * @return a global unique {@link FlowId} instance.
- */
- FlowId getNewId();
-}
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/FlowManagerService.java b/src/main/java/net/onrc/onos/api/flowmanager/FlowManagerService.java
index 93a4e6c..4c19cbd 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/FlowManagerService.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/FlowManagerService.java
@@ -1,5 +1,7 @@
package net.onrc.onos.api.flowmanager;
+import net.onrc.onos.core.util.IdGenerator;
+
import java.util.Collection;
/**
@@ -65,7 +67,7 @@
* Gets ID generator for Flow objects.
* @return the ID generator for Flow objects
*/
- FlowIdGenerator getFlowIdGenerator();
+ IdGenerator<FlowId> getFlowIdGenerator();
/**
* Sets a conflict detection policy.
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/OpticalPathFlow.java b/src/main/java/net/onrc/onos/api/flowmanager/OpticalPathFlow.java
index 51dce43..a352f05 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/OpticalPathFlow.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/OpticalPathFlow.java
@@ -3,12 +3,13 @@
import java.util.List;
import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
-import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionId;
import net.onrc.onos.core.matchaction.MatchActionOperations;
-import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionOperationsId;
import net.onrc.onos.core.matchaction.action.Action;
import net.onrc.onos.core.matchaction.match.PacketMatch;
import net.onrc.onos.core.matchaction.match.PacketMatchBuilder;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.PortNumber;
/**
@@ -67,8 +68,8 @@
@Override
public List<MatchActionOperations> compile(Operator op,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
// TODO Auto-generated method stub
return null;
}
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/PacketPathFlow.java b/src/main/java/net/onrc/onos/api/flowmanager/PacketPathFlow.java
index 37820fb..34d6776 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/PacketPathFlow.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/PacketPathFlow.java
@@ -10,13 +10,14 @@
import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
import net.onrc.onos.core.matchaction.MatchAction;
-import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionId;
import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
import net.onrc.onos.core.matchaction.MatchActionOperations;
-import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionOperationsId;
import net.onrc.onos.core.matchaction.action.Action;
import net.onrc.onos.core.matchaction.action.OutputAction;
import net.onrc.onos.core.matchaction.match.PacketMatch;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.PortNumber;
import net.onrc.onos.core.util.SwitchPort;
@@ -86,8 +87,8 @@
@Override
public List<MatchActionOperations> compile(Operator op,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
switch (op) {
case ADD:
return compileAddOperation(maIdGenerator, maoIdGenerator);
@@ -108,7 +109,7 @@
*/
private MatchAction createNextMatchAction(Iterator<SwitchPort> portIterator,
Iterator<List<Action>> actionsIterator,
- MatchActionIdGenerator maIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator) {
if (portIterator == null || actionsIterator == null ||
!portIterator.hasNext() || !actionsIterator.hasNext()) {
return null;
@@ -128,8 +129,8 @@
* @return the list of {@link MatchActionOperations} objects
*/
private List<MatchActionOperations> compileAddOperation(
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
Path path = checkNotNull(getPath());
checkState(path.size() > 0, "Path object has no link.");
@@ -183,8 +184,8 @@
* @return the list of {@link MatchActionOperations} objects
*/
private List<MatchActionOperations> compileRemoveOperation(
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
// TODO implement it
throw new UnsupportedOperationException(
"REMOVE operation is not implemented yet.");
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/SingleDstTreeFlow.java b/src/main/java/net/onrc/onos/api/flowmanager/SingleDstTreeFlow.java
index 0caef51..d02351b 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/SingleDstTreeFlow.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/SingleDstTreeFlow.java
@@ -18,14 +18,15 @@
import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
import net.onrc.onos.core.matchaction.MatchAction;
-import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionId;
import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
import net.onrc.onos.core.matchaction.MatchActionOperations;
-import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionOperationsId;
import net.onrc.onos.core.matchaction.action.Action;
import net.onrc.onos.core.matchaction.action.OutputAction;
import net.onrc.onos.core.matchaction.match.PacketMatch;
import net.onrc.onos.core.util.Dpid;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.PortNumber;
import net.onrc.onos.core.util.SwitchPort;
@@ -110,8 +111,8 @@
@Override
public List<MatchActionOperations> compile(Operator op,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
switch (op) {
case ADD:
return compileAddOperation(maIdGenerator, maoIdGenerator);
@@ -123,7 +124,7 @@
}
private MatchAction createMatchAction(SwitchPort port, List<Action> actions,
- MatchActionIdGenerator maIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator) {
checkNotNull(port);
checkNotNull(actions);
@@ -145,8 +146,8 @@
Dpid egressSwitch,
Map<Dpid, Set<PortNumber>> inPorts,
Map<Dpid, PortNumber> outPorts,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
MatchActionOperations firstOps =
new MatchActionOperations(maoIdGenerator.getNewId());
for (Entry<Dpid, Set<PortNumber>> innerSw : inPorts.entrySet()) {
@@ -183,8 +184,8 @@
private MatchActionOperations generateSecondAddOperations(
Dpid egressSwitch,
Map<Dpid, PortNumber> outPorts,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
MatchActionOperations secondOps =
new MatchActionOperations(maoIdGenerator.getNewId());
for (SwitchPort port : getIngressPorts()) {
@@ -213,8 +214,8 @@
}
private List<MatchActionOperations> compileAddOperation(
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
checkNotNull(tree);
checkState(tree.size() > 0, "Tree object has no link.");
diff --git a/src/main/java/net/onrc/onos/api/flowmanager/SingleSrcTreeFlow.java b/src/main/java/net/onrc/onos/api/flowmanager/SingleSrcTreeFlow.java
index 3e70f4b..89f1efd 100644
--- a/src/main/java/net/onrc/onos/api/flowmanager/SingleSrcTreeFlow.java
+++ b/src/main/java/net/onrc/onos/api/flowmanager/SingleSrcTreeFlow.java
@@ -6,12 +6,13 @@
import java.util.Set;
import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
-import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionId;
import net.onrc.onos.core.matchaction.MatchActionOperations;
-import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionOperationsId;
import net.onrc.onos.core.matchaction.action.OutputAction;
import net.onrc.onos.core.matchaction.match.PacketMatch;
import net.onrc.onos.core.util.Dpid;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.SwitchPort;
import org.apache.commons.lang3.tuple.Pair;
@@ -84,8 +85,8 @@
@Override
public List<MatchActionOperations> compile(Operator op,
- MatchActionIdGenerator maIdGenerator,
- MatchActionOperationsIdGenerator maoIdGenerator) {
+ IdGenerator<MatchActionId> maIdGenerator,
+ IdGenerator<MatchActionOperationsId> maoIdGenerator) {
// TODO Auto-generated method stub
return null;
}
diff --git a/src/main/java/net/onrc/onos/api/newintent/IntentIdGenerator.java b/src/main/java/net/onrc/onos/api/newintent/IntentIdGenerator.java
deleted file mode 100644
index ec75c7b..0000000
--- a/src/main/java/net/onrc/onos/api/newintent/IntentIdGenerator.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package net.onrc.onos.api.newintent;
-
-/**
- * This interface is for generator of IntentId.
- *
- * <p>
- * {@link #getNewId()} generates a globally unique {@link IntentId} instance
- * on each invocation. Application developers should not generate IntentId
- * by themselves. Instead use an implementation of this interface.
- * </p>
- */
-public interface IntentIdGenerator {
- /**
- * Generates a globally unique {@link IntentId} instance.
- *
- * @return a globally unique {@link IntentId} instance.
- */
- public IntentId getNewId();
-}
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);
}
}
diff --git a/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java b/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java
index 39499f4..14c63df 100644
--- a/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java
+++ b/src/main/java/net/onrc/onos/core/flowmanager/FlowManagerModule.java
@@ -22,18 +22,18 @@
import net.onrc.onos.api.flowmanager.FlowBatchOperation;
import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator;
import net.onrc.onos.api.flowmanager.FlowId;
-import net.onrc.onos.api.flowmanager.FlowIdGenerator;
import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService;
import net.onrc.onos.api.flowmanager.FlowManagerListener;
import net.onrc.onos.core.datagrid.ISharedCollectionsService;
import net.onrc.onos.core.matchaction.MatchActionFloodlightService;
-import net.onrc.onos.core.matchaction.MatchActionIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionId;
import net.onrc.onos.core.matchaction.MatchActionOperationEntry;
import net.onrc.onos.core.matchaction.MatchActionOperations;
-import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator;
+import net.onrc.onos.core.matchaction.MatchActionOperationsId;
import net.onrc.onos.core.matchaction.MatchActionService;
import net.onrc.onos.core.registry.IControllerRegistryService;
import net.onrc.onos.core.util.IdBlockAllocator;
+import net.onrc.onos.core.util.IdGenerator;
/**
* Manages a set of Flow objects, computes and maintains a set of Match-Action
@@ -45,8 +45,8 @@
private ConflictDetectionPolicy conflictDetectionPolicy;
private FlowIdGeneratorWithIdBlockAllocator flowIdGenerator;
private FlowBatchIdGeneratorWithIdBlockAllocator flowBatchIdGenerator;
- private MatchActionIdGenerator maIdGenerator;
- private MatchActionOperationsIdGenerator maoIdGenerator;
+ private IdGenerator<MatchActionId> maIdGenerator;
+ private IdGenerator<MatchActionOperationsId> maoIdGenerator;
private MatchActionService matchActionService;
private IControllerRegistryService registryService;
private ISharedCollectionsService sharedCollectionService;
@@ -155,7 +155,7 @@
}
@Override
- public FlowIdGenerator getFlowIdGenerator() {
+ public IdGenerator<FlowId> getFlowIdGenerator() {
return flowIdGenerator;
}
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGenerator.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGenerator.java
deleted file mode 100644
index 4f36d7c..0000000
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGenerator.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package net.onrc.onos.core.matchaction;
-/**
- * A generator of MatchActionId.
- */
-public interface MatchActionIdGenerator {
- /**
- * Generates a globally unique MatchActionId instance.
- */
- MatchActionId getNewId();
-}
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGeneratorWithIdBlockAllocator.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGeneratorWithIdBlockAllocator.java
index 14a09c2..ef255d0 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGeneratorWithIdBlockAllocator.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGeneratorWithIdBlockAllocator.java
@@ -1,19 +1,13 @@
package net.onrc.onos.core.matchaction;
-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;
-
-import static com.google.common.base.Preconditions.checkNotNull;
/**
* Generates a global unique MatchActionId.
*/
public class MatchActionIdGeneratorWithIdBlockAllocator
- implements MatchActionIdGenerator {
-
- private final IdBlockAllocator allocator;
- private IdBlock idBlock;
+ extends AbstractBlockAllocatorBasedIdGenerator<MatchActionId> {
/**
* Creates a FlowId generator instance using specified ID block allocator.
@@ -21,17 +15,11 @@
* @param allocator the ID block allocator to be used
*/
public MatchActionIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
- this.allocator = checkNotNull(allocator);
- this.idBlock = allocator.allocateUniqueIdBlock();
+ super(allocator);
}
@Override
- public synchronized MatchActionId getNewId() {
- try {
- return new MatchActionId(idBlock.getNextId());
- } catch (UnavailableIdException e) {
- idBlock = allocator.allocateUniqueIdBlock();
- return new MatchActionId(idBlock.getNextId());
- }
+ protected MatchActionId convertFrom(long value) {
+ return new MatchActionId(value);
}
}
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionModule.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionModule.java
index 78e528c..6ae5d5e 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionModule.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionModule.java
@@ -7,6 +7,7 @@
import java.util.Set;
import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy;
+import net.onrc.onos.core.util.IdGenerator;
/**
* Manages Match-Action entries.
@@ -56,14 +57,14 @@
}
@Override
- public MatchActionIdGenerator getMatchActionIdGenerator() {
+ public IdGenerator<MatchActionId> getMatchActionIdGenerator() {
// TODO Auto-generated method stub
// use MatchActionIdGeneratorWithIdBlockAllocator.
return null;
}
@Override
- public MatchActionOperationsIdGenerator getMatchActionOperationsIdGenerator() {
+ public IdGenerator<MatchActionOperationsId> getMatchActionOperationsIdGenerator() {
// TODO Auto-generated method stub
// use MatchActionOperationsIdGeneratorWithIdBlockAllocator.
return null;
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGenerator.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGenerator.java
deleted file mode 100644
index 3274224..0000000
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGenerator.java
+++ /dev/null
@@ -1,10 +0,0 @@
-package net.onrc.onos.core.matchaction;
-/**
- * A generator of MatchActionId.
- */
-public interface MatchActionOperationsIdGenerator {
- /**
- * Generates a globally unique MatchActionId instance.
- */
- MatchActionOperationsId getNewId();
-}
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGeneratorWithIdBlockAllocator.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGeneratorWithIdBlockAllocator.java
index d1d5240..fd9efdc 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGeneratorWithIdBlockAllocator.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGeneratorWithIdBlockAllocator.java
@@ -1,37 +1,26 @@
package net.onrc.onos.core.matchaction;
-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;
-import static com.google.common.base.Preconditions.checkNotNull;
/**
* Generates a global unique MatchActionIdId.
*/
public class MatchActionOperationsIdGeneratorWithIdBlockAllocator
- implements MatchActionOperationsIdGenerator {
+ extends AbstractBlockAllocatorBasedIdGenerator<MatchActionOperationsId> {
- 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 MatchActionOperationsIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
+ super(allocator);
+ }
- /**
- * Creates a FlowId generator instance using specified ID block allocator.
- *
- * @param allocator the ID block allocator to be used
- */
- public MatchActionOperationsIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
- this.allocator = checkNotNull(allocator);
- this.idBlock = allocator.allocateUniqueIdBlock();
- }
-
- @Override
- public synchronized MatchActionOperationsId getNewId() {
- try {
- return new MatchActionOperationsId(idBlock.getNextId());
- } catch (UnavailableIdException e) {
- idBlock = allocator.allocateUniqueIdBlock();
- return new MatchActionOperationsId(idBlock.getNextId());
- }
- }
+ @Override
+ protected MatchActionOperationsId convertFrom(long value) {
+ return new MatchActionOperationsId(value);
+ }
}
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionService.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionService.java
index 161ed91..fa6b34f 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionService.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionService.java
@@ -4,6 +4,7 @@
import java.util.Set;
import net.onrc.onos.api.flowmanager.ConflictDetectionPolicy;
+import net.onrc.onos.core.util.IdGenerator;
/**
* An interface for the match-action service.
@@ -51,14 +52,14 @@
*
* @return the ID generator for MatchActionId
*/
- MatchActionIdGenerator getMatchActionIdGenerator();
+ IdGenerator<MatchActionId> getMatchActionIdGenerator();
/**
* Gets the ID generator for MatchActionOperationsId.
*
* @return the ID generator for MatchActionOperationsId
*/
- MatchActionOperationsIdGenerator getMatchActionOperationsIdGenerator();
+ IdGenerator<MatchActionOperationsId> getMatchActionOperationsIdGenerator();
/**
* Adds event listener to this service.
diff --git a/src/main/java/net/onrc/onos/core/newintent/AbstractFlowGeneratingIntentCompiler.java b/src/main/java/net/onrc/onos/core/newintent/AbstractFlowGeneratingIntentCompiler.java
index 07041e9..159caf5 100644
--- a/src/main/java/net/onrc/onos/core/newintent/AbstractFlowGeneratingIntentCompiler.java
+++ b/src/main/java/net/onrc/onos/core/newintent/AbstractFlowGeneratingIntentCompiler.java
@@ -1,9 +1,9 @@
package net.onrc.onos.core.newintent;
import net.onrc.onos.api.flowmanager.FlowId;
-import net.onrc.onos.api.flowmanager.FlowIdGenerator;
import net.onrc.onos.api.newintent.Intent;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
+import net.onrc.onos.api.newintent.IntentId;
+import net.onrc.onos.core.util.IdGenerator;
import static com.google.common.base.Preconditions.checkNotNull;
@@ -15,17 +15,17 @@
public abstract class AbstractFlowGeneratingIntentCompiler<T extends Intent>
extends AbstractIntentCompiler<T> {
- private final FlowIdGenerator flowIdGenerator;
+ private final IdGenerator<FlowId> flowIdGenerator;
/**
- * Constructs an object with the specified {@link IntentIdGenerator}
- * and {@link FlowIdGenerator}.
+ * Constructs an object with the specified {@link IdGenerator IdGenerators} of intent ID
+ * and flow ID.
*
* @param intentIdGenerator
* @param flowIdGenerator
*/
- protected AbstractFlowGeneratingIntentCompiler(IntentIdGenerator intentIdGenerator,
- FlowIdGenerator flowIdGenerator) {
+ protected AbstractFlowGeneratingIntentCompiler(IdGenerator<IntentId> intentIdGenerator,
+ IdGenerator<FlowId> flowIdGenerator) {
super(intentIdGenerator);
this.flowIdGenerator = checkNotNull(flowIdGenerator);
}
diff --git a/src/main/java/net/onrc/onos/core/newintent/AbstractIntentCompiler.java b/src/main/java/net/onrc/onos/core/newintent/AbstractIntentCompiler.java
index 0b16f10..46f4cf9 100644
--- a/src/main/java/net/onrc/onos/core/newintent/AbstractIntentCompiler.java
+++ b/src/main/java/net/onrc/onos/core/newintent/AbstractIntentCompiler.java
@@ -4,10 +4,10 @@
import net.onrc.onos.api.newintent.Intent;
import net.onrc.onos.api.newintent.IntentCompiler;
import net.onrc.onos.api.newintent.IntentId;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
import net.onrc.onos.core.matchaction.action.Action;
import net.onrc.onos.core.matchaction.action.Actions;
import net.onrc.onos.core.matchaction.action.OutputAction;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.SwitchPort;
import java.util.ArrayList;
@@ -20,7 +20,7 @@
* @param <T> the type of intent
*/
public abstract class AbstractIntentCompiler<T extends Intent> implements IntentCompiler<T> {
- private final IntentIdGenerator idGenerator;
+ private final IdGenerator<IntentId> idGenerator;
/**
* Constructs an instance with the specified Intent ID generator.
@@ -32,7 +32,7 @@
* </p>
* @param idGenerator intent ID generator
*/
- protected AbstractIntentCompiler(IntentIdGenerator idGenerator) {
+ protected AbstractIntentCompiler(IdGenerator<IntentId> idGenerator) {
this.idGenerator = checkNotNull(idGenerator);
}
diff --git a/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java b/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java
index 9137b29..a89879a 100644
--- a/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java
+++ b/src/main/java/net/onrc/onos/core/newintent/IdBlockAllocatorBasedIntentIdGenerator.java
@@ -1,21 +1,14 @@
package net.onrc.onos.core.newintent;
import net.onrc.onos.api.newintent.IntentId;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
-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;
-
-import static com.google.common.base.Preconditions.checkNotNull;
/**
- * An implementation of {@link IntentIdGenerator},
- * which uses {@link IdBlockAllocator#allocateUniqueIdBlock()}.
+ * An implementation of {@link net.onrc.onos.core.util.IdGenerator} of intent ID,
+ * which uses {@link IdBlockAllocator}.
*/
-public class IdBlockAllocatorBasedIntentIdGenerator implements IntentIdGenerator {
-
- private final IdBlockAllocator allocator;
- private IdBlock idBlock;
+public class IdBlockAllocatorBasedIntentIdGenerator extends AbstractBlockAllocatorBasedIdGenerator<IntentId> {
/**
* Constructs an intent ID generator, which uses the specified ID block allocator
@@ -24,17 +17,11 @@
* @param allocator the ID block allocator to use for generating intent IDs
*/
public IdBlockAllocatorBasedIntentIdGenerator(IdBlockAllocator allocator) {
- this.allocator = checkNotNull(allocator);
- this.idBlock = allocator.allocateUniqueIdBlock();
+ super(allocator);
}
@Override
- public synchronized IntentId getNewId() {
- try {
- return new IntentId(idBlock.getNextId());
- } catch (UnavailableIdException e) {
- idBlock = allocator.allocateUniqueIdBlock();
- return new IntentId(idBlock.getNextId());
- }
+ protected IntentId convertFrom(long value) {
+ return new IntentId(value);
}
}
diff --git a/src/main/java/net/onrc/onos/core/newintent/IntentFloodlightModule.java b/src/main/java/net/onrc/onos/core/newintent/IntentFloodlightModule.java
index fee5983..cf0e5e5 100644
--- a/src/main/java/net/onrc/onos/core/newintent/IntentFloodlightModule.java
+++ b/src/main/java/net/onrc/onos/core/newintent/IntentFloodlightModule.java
@@ -4,7 +4,7 @@
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
-import net.onrc.onos.api.flowmanager.FlowIdGenerator;
+import net.onrc.onos.api.flowmanager.FlowId;
import net.onrc.onos.api.flowmanager.FlowManagerFloodlightService;
import net.onrc.onos.api.flowmanager.FlowManagerService;
import net.onrc.onos.api.newintent.InstallableIntent;
@@ -13,7 +13,6 @@
import net.onrc.onos.api.newintent.IntentEventListener;
import net.onrc.onos.api.newintent.IntentFloodlightService;
import net.onrc.onos.api.newintent.IntentId;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
import net.onrc.onos.api.newintent.IntentInstaller;
import net.onrc.onos.api.newintent.IntentManager;
import net.onrc.onos.api.newintent.IntentOperations;
@@ -25,6 +24,7 @@
import net.onrc.onos.core.registry.IControllerRegistryService;
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.util.IdBlockAllocator;
+import net.onrc.onos.core.util.IdGenerator;
import java.util.ArrayList;
import java.util.Arrays;
@@ -76,11 +76,11 @@
IdBlockAllocator idBlockAllocator =
context.getServiceImpl(IControllerRegistryService.class);
- IntentIdGenerator intentIdGenerator =
+ IdGenerator<IntentId> intentIdGenerator =
new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
FlowManagerService flowManagerService =
context.getServiceImpl(FlowManagerFloodlightService.class);
- FlowIdGenerator flowIdGenerator =
+ IdGenerator<FlowId> flowIdGenerator =
flowManagerService.getFlowIdGenerator();
ITopologyService topologyService =
@@ -90,8 +90,8 @@
registerDefaultInstallers(flowManagerService);
}
- private void registerDefaultCompilers(IntentIdGenerator intentIdGenerator,
- FlowIdGenerator flowIdGenerator,
+ private void registerDefaultCompilers(IdGenerator<IntentId> intentIdGenerator,
+ IdGenerator<FlowId> flowIdGenerator,
ITopologyService topologyService) {
intentManager.registerCompiler(PointToPointIntent.class,
new PointToPointIntentCompiler(intentIdGenerator,
diff --git a/src/main/java/net/onrc/onos/core/newintent/MultiPointToSinglePointIntentCompiler.java b/src/main/java/net/onrc/onos/core/newintent/MultiPointToSinglePointIntentCompiler.java
index eb31457..e5a9d25 100644
--- a/src/main/java/net/onrc/onos/core/newintent/MultiPointToSinglePointIntentCompiler.java
+++ b/src/main/java/net/onrc/onos/core/newintent/MultiPointToSinglePointIntentCompiler.java
@@ -1,10 +1,10 @@
package net.onrc.onos.core.newintent;
-import net.onrc.onos.api.flowmanager.FlowIdGenerator;
+import net.onrc.onos.api.flowmanager.FlowId;
import net.onrc.onos.api.flowmanager.SingleDstTreeFlow;
import net.onrc.onos.api.flowmanager.Tree;
import net.onrc.onos.api.newintent.Intent;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
+import net.onrc.onos.api.newintent.IntentId;
import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent;
import net.onrc.onos.core.intent.ConstrainedBFSTree;
import net.onrc.onos.core.intent.Path;
@@ -13,6 +13,7 @@
import net.onrc.onos.core.topology.BaseTopology;
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.topology.Switch;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.SwitchPort;
import java.util.Arrays;
@@ -37,8 +38,8 @@
* @param flowIdGenerator flow ID generator
* @param topologyService topology service
*/
- public MultiPointToSinglePointIntentCompiler(IntentIdGenerator intentIdGenerator,
- FlowIdGenerator flowIdGenerator,
+ public MultiPointToSinglePointIntentCompiler(IdGenerator<IntentId> intentIdGenerator,
+ IdGenerator<FlowId> flowIdGenerator,
ITopologyService topologyService) {
super(intentIdGenerator, flowIdGenerator);
this.topologyService = checkNotNull(topologyService);
diff --git a/src/main/java/net/onrc/onos/core/newintent/PathIntentCompiler.java b/src/main/java/net/onrc/onos/core/newintent/PathIntentCompiler.java
index e7e2f87..0277050 100644
--- a/src/main/java/net/onrc/onos/core/newintent/PathIntentCompiler.java
+++ b/src/main/java/net/onrc/onos/core/newintent/PathIntentCompiler.java
@@ -3,15 +3,16 @@
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
-import net.onrc.onos.api.flowmanager.FlowIdGenerator;
+import net.onrc.onos.api.flowmanager.FlowId;
import net.onrc.onos.api.flowmanager.FlowLink;
import net.onrc.onos.api.flowmanager.PacketPathFlow;
import net.onrc.onos.api.flowmanager.Path;
import net.onrc.onos.api.newintent.Intent;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
+import net.onrc.onos.api.newintent.IntentId;
import net.onrc.onos.api.newintent.PathIntent;
import net.onrc.onos.core.matchaction.match.Match;
import net.onrc.onos.core.matchaction.match.PacketMatch;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.LinkTuple;
import java.util.Arrays;
@@ -30,8 +31,8 @@
* @param intentIdGenerator intent ID generator
* @param flowIdGenerator flow ID generator
*/
- public PathIntentCompiler(IntentIdGenerator intentIdGenerator,
- FlowIdGenerator flowIdGenerator) {
+ public PathIntentCompiler(IdGenerator<IntentId> intentIdGenerator,
+ IdGenerator<FlowId> flowIdGenerator) {
super(intentIdGenerator, flowIdGenerator);
}
diff --git a/src/main/java/net/onrc/onos/core/newintent/PointToPointIntentCompiler.java b/src/main/java/net/onrc/onos/core/newintent/PointToPointIntentCompiler.java
index fcf718d..3c04e0d 100644
--- a/src/main/java/net/onrc/onos/core/newintent/PointToPointIntentCompiler.java
+++ b/src/main/java/net/onrc/onos/core/newintent/PointToPointIntentCompiler.java
@@ -4,12 +4,11 @@
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import net.onrc.onos.api.flowmanager.FlowId;
-import net.onrc.onos.api.flowmanager.FlowIdGenerator;
import net.onrc.onos.api.flowmanager.FlowLink;
import net.onrc.onos.api.flowmanager.PacketPathFlow;
import net.onrc.onos.api.flowmanager.Path;
import net.onrc.onos.api.newintent.Intent;
-import net.onrc.onos.api.newintent.IntentIdGenerator;
+import net.onrc.onos.api.newintent.IntentId;
import net.onrc.onos.api.newintent.PointToPointIntent;
import net.onrc.onos.core.intent.ConstrainedBFSTree;
import net.onrc.onos.core.matchaction.action.Action;
@@ -19,6 +18,7 @@
import net.onrc.onos.core.topology.ITopologyService;
import net.onrc.onos.core.topology.LinkEvent;
import net.onrc.onos.core.topology.Switch;
+import net.onrc.onos.core.util.IdGenerator;
import net.onrc.onos.core.util.SwitchPort;
import java.util.Arrays;
@@ -41,8 +41,8 @@
* @param intentIdGenerator intent ID generator
* @param topologyService topology service
*/
- public PointToPointIntentCompiler(IntentIdGenerator intentIdGenerator,
- FlowIdGenerator flowIdGenerator,
+ public PointToPointIntentCompiler(IdGenerator<IntentId> intentIdGenerator,
+ IdGenerator<FlowId> flowIdGenerator,
ITopologyService topologyService) {
super(intentIdGenerator, flowIdGenerator);
this.topologyService = checkNotNull(topologyService);
diff --git a/src/main/java/net/onrc/onos/core/util/AbstractBlockAllocatorBasedIdGenerator.java b/src/main/java/net/onrc/onos/core/util/AbstractBlockAllocatorBasedIdGenerator.java
new file mode 100644
index 0000000..851bb91
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/util/AbstractBlockAllocatorBasedIdGenerator.java
@@ -0,0 +1,40 @@
+package net.onrc.onos.core.util;
+
+/**
+ * Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
+ * backend.
+ *
+ * @param <T> the type of ID
+ */
+public abstract class AbstractBlockAllocatorBasedIdGenerator<T> implements IdGenerator<T> {
+ protected final IdBlockAllocator allocator;
+ protected IdBlock idBlock;
+
+ /**
+ * Constructs an ID generator which use {@link IdBlockAllocator} as backend.
+ *
+ * @param allocator
+ */
+ protected AbstractBlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
+ this.allocator = allocator;
+ this.idBlock = allocator.allocateUniqueIdBlock();
+ }
+
+ @Override
+ public synchronized T getNewId() {
+ try {
+ return convertFrom(idBlock.getNextId());
+ } catch (UnavailableIdException e) {
+ idBlock = allocator.allocateUniqueIdBlock();
+ return convertFrom(idBlock.getNextId());
+ }
+ }
+
+ /**
+ * Returns an ID instance of {@code T} type from the long value.
+ *
+ * @param value original long value
+ * @return ID instance
+ */
+ protected abstract T convertFrom(long value);
+}
diff --git a/src/main/java/net/onrc/onos/core/util/IdGenerator.java b/src/main/java/net/onrc/onos/core/util/IdGenerator.java
new file mode 100644
index 0000000..b735954
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/util/IdGenerator.java
@@ -0,0 +1,21 @@
+package net.onrc.onos.core.util;
+
+/**
+ * A generalized interface for ID generation
+ *
+ * {@link #getNewId()} generates a globally unique ID instance on
+ * each invocation.
+ *
+ * @param <T> the type of ID
+ */
+// TODO: do we need to define a base marker interface for ID,
+// then changed the type parameter to <T extends BaseId> something
+// like that?
+public interface IdGenerator<T> {
+ /**
+ * Returns a globally unique ID instance.
+ *
+ * @return globally unique ID instance
+ */
+ T getNewId();
+}
diff --git a/src/test/java/net/onrc/onos/api/flowmanager/FakeFlowManagerService.java b/src/test/java/net/onrc/onos/api/flowmanager/FakeFlowManagerService.java
index c231bca..50e68ce 100644
--- a/src/test/java/net/onrc/onos/api/flowmanager/FakeFlowManagerService.java
+++ b/src/test/java/net/onrc/onos/api/flowmanager/FakeFlowManagerService.java
@@ -2,6 +2,7 @@
import com.google.common.collect.ImmutableList;
import net.onrc.onos.core.flowmanager.FlowBatchHandleImpl;
+import net.onrc.onos.core.util.IdGenerator;
import java.util.ArrayList;
import java.util.Arrays;
@@ -87,7 +88,7 @@
}
@Override
- public FlowIdGenerator getFlowIdGenerator() {
+ public IdGenerator<FlowId> getFlowIdGenerator() {
throw new UnsupportedOperationException();
}
diff --git a/src/test/java/net/onrc/onos/api/flowmanager/FlowIdGenerator.java b/src/test/java/net/onrc/onos/api/flowmanager/FlowIdGenerator.java
new file mode 100644
index 0000000..95087af
--- /dev/null
+++ b/src/test/java/net/onrc/onos/api/flowmanager/FlowIdGenerator.java
@@ -0,0 +1,10 @@
+package net.onrc.onos.api.flowmanager;
+
+import net.onrc.onos.core.util.IdGenerator;
+
+/**
+ * An generator of {@link FlowId}. It is defined only for
+ * testing purpose to keep type safety on mock creation.
+ */
+public interface FlowIdGenerator extends IdGenerator<FlowId> {
+}
diff --git a/src/test/java/net/onrc/onos/api/newintent/IntentIdGenerator.java b/src/test/java/net/onrc/onos/api/newintent/IntentIdGenerator.java
new file mode 100644
index 0000000..2f5e013
--- /dev/null
+++ b/src/test/java/net/onrc/onos/api/newintent/IntentIdGenerator.java
@@ -0,0 +1,16 @@
+package net.onrc.onos.api.newintent;
+
+import net.onrc.onos.core.util.IdGenerator;
+
+/**
+ * This interface is for generator of IntentId. It is defined only for
+ * testing purpose to keep type safety on mock creation.
+ *
+ * <p>
+ * {@link #getNewId()} generates a globally unique {@link IntentId} instance
+ * on each invocation. Application developers should not generate IntentId
+ * by themselves. Instead use an implementation of this interface.
+ * </p>
+ */
+public interface IntentIdGenerator extends IdGenerator<IntentId> {
+}