IDs for MatchAction objects using block allocation

Modified MatchActionId and MatchActionOperationsId to
use longs as IDs and to use ID block allocation to
create them.

Change-Id: I757b353a94a498f624df345cbc16975714db15b3
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchAction.java b/src/main/java/net/onrc/onos/core/matchaction/MatchAction.java
index 24790b8..5f7d9fb 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchAction.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchAction.java
@@ -24,8 +24,23 @@
      * @param match the Match object as match condition on the port
      * @param actions the list of Action objects as actions on the switch
      */
+    public MatchAction(MatchActionId id, SwitchPort port, Match match, List<Action> actions) {
+        this.id = id;
+        this.port = port;
+        this.match = match;
+        this.actions = actions;
+    }
+
+    /**
+     * Constructor. TEMPORARY
+     *
+     * @param id ID for this MatchAction object
+     * @param port switch port to apply changes to
+     * @param match the Match object as match condition on the port
+     * @param actions the list of Action objects as actions on the switch
+     */
     public MatchAction(String id, SwitchPort port, Match match, List<Action> actions) {
-        this.id = new MatchActionId(id);
+        this.id = null;
         this.port = port;
         this.match = match;
         this.actions = actions;
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionId.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionId.java
index b520439..3dcb64c 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionId.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionId.java
@@ -2,36 +2,38 @@
 
 import net.onrc.onos.api.batchoperation.BatchOperationTarget;
 
+import java.util.Objects;
+
 /**
  * A unique identifier for a MatchAction.  Objects of this class are immutable.
  */
 public final class MatchActionId implements BatchOperationTarget {
-    private final String value;
+    private final long value;
 
     /**
      * Creates a new Match Action Identifier based on the given id string.
      *
      * @param id unique id string
      */
-    public MatchActionId(String id) {
+    public MatchActionId(long id) {
         value = id;
     }
 
     @Override
     public String toString() {
-        return value;
+        return Long.toString(value);
     }
 
     @Override
     public int hashCode() {
-        return value.hashCode();
+        return Objects.hashCode(value);
     }
 
     @Override
     public boolean equals(Object obj) {
         if (obj instanceof MatchActionId) {
-            MatchActionId other = (MatchActionId) obj;
-            return (value.equals(other.value));
+            final MatchActionId that = (MatchActionId) obj;
+            return this.value == that.value;
         }
         return false;
     }
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGenerator.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGenerator.java
new file mode 100644
index 0000000..4f36d7c
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGenerator.java
@@ -0,0 +1,10 @@
+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
new file mode 100644
index 0000000..2c92ff7
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionIdGeneratorWithIdBlockAllocator.java
@@ -0,0 +1,37 @@
+package net.onrc.onos.core.matchaction;
+
+import net.onrc.onos.core.util.IdBlock;
+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 MatchActionIdGeneratorWithIdBlockAllocator
+        implements MatchActionIdGenerator {
+
+        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 MatchActionIdGeneratorWithIdBlockAllocator(IdBlockAllocator allocator) {
+            this.allocator = checkNotNull(allocator);
+            this.idBlock = allocator.allocateUniqueIdBlock();
+        }
+
+        @Override
+        public synchronized MatchActionId getNewId() {
+            try {
+                return new MatchActionId(idBlock.getNextId());
+            } catch (UnavailableIdException e) {
+                idBlock = allocator.allocateUniqueIdBlock();
+                return new MatchActionId(idBlock.getNextId());
+            }
+        }
+}
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperations.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperations.java
index a17d0f2..c865203 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperations.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperations.java
@@ -28,22 +28,11 @@
      *
      * @param newId match action operations identifier for this instance
      */
-    private MatchActionOperations(final MatchActionOperationsId newId) {
+    public MatchActionOperations(final MatchActionOperationsId newId) {
         id = checkNotNull(newId);
     }
 
     /**
-     * Creates a MatchActionOperations object from an id.
-     *
-     * @param newId match action operations identifier to use for the new object
-     * @return Match Action Operations object
-     */
-    public static MatchActionOperations createMatchActionsOperations(
-            final MatchActionOperationsId newId) {
-        return new MatchActionOperations(newId);
-    }
-
-    /**
      * Gets the identifier for the Match Action Operations object.
      *
      * @return identifier for the Opertions object
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsId.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsId.java
index 7c9d1db..e346732 100644
--- a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsId.java
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsId.java
@@ -1,6 +1,6 @@
 package net.onrc.onos.core.matchaction;
 
-import java.util.UUID;
+import java.util.Objects;
 
 /**
  * Identifier for a MatchActionOperations object.  This is an immutable class
@@ -9,15 +9,13 @@
  */
 public final class MatchActionOperationsId {
 
-    private static final String OPERATIONS_ID_PREFIX = "MatchActionOperationsId-";
-    private final String id;
+    private final long id;
 
     /**
-     * Constructs an Operations identifier and allocates a unique identifier
-     * for it.
+     * Constructs an Operations identifier and from a unique identifier.
      */
-    private MatchActionOperationsId() {
-        id = OPERATIONS_ID_PREFIX + UUID.randomUUID();
+    public MatchActionOperationsId(final long newId) {
+        id = newId;
     }
 
     /**
@@ -25,7 +23,7 @@
      *
      * @return Operations object identifier as a string
      */
-    public String getId() {
+    public long getId() {
         return id;
     }
 
@@ -39,24 +37,13 @@
             return false;
         }
 
-        final MatchActionOperationsId otherMatchActionOperationsId =
-                (MatchActionOperationsId) other;
+        final MatchActionOperationsId that = (MatchActionOperationsId) other;
 
-        return otherMatchActionOperationsId.getId().equals(getId());
+        return this.getId() == that.getId();
     }
 
     @Override
     public int hashCode() {
-        return id.hashCode();
+        return Objects.hashCode(id);
     }
-
-    /**
-     * Creates a new Id for a MatchActionOperation.
-     *
-     * @return new Id for a MatchActionOperation
-     */
-    public static MatchActionOperationsId createNewOperationsId() {
-        return new MatchActionOperationsId();
-    }
-
 }
diff --git a/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGenerator.java b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGenerator.java
new file mode 100644
index 0000000..3274224
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGenerator.java
@@ -0,0 +1,10 @@
+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
new file mode 100644
index 0000000..d1d5240
--- /dev/null
+++ b/src/main/java/net/onrc/onos/core/matchaction/MatchActionOperationsIdGeneratorWithIdBlockAllocator.java
@@ -0,0 +1,37 @@
+package net.onrc.onos.core.matchaction;
+
+import net.onrc.onos.core.util.IdBlock;
+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 {
+
+        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) {
+            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());
+            }
+        }
+}