Refactor transaction support in preparation for migration to latest APIs
 - Added a explicit transaction id type
 - cli command now just returns the identifiers of in-progress transactions
 - Removed redriveTransactions until a better alternative is provided
 - Removed DatabaseUpdate and replaced its usage with MapUpdate

Change-Id: Ic4a14967072068834510cd8459fd2a6790e456ef
diff --git a/core/api/src/main/java/org/onosproject/store/service/DatabaseUpdate.java b/core/api/src/main/java/org/onosproject/store/service/DatabaseUpdate.java
deleted file mode 100644
index a62d382..0000000
--- a/core/api/src/main/java/org/onosproject/store/service/DatabaseUpdate.java
+++ /dev/null
@@ -1,220 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.onosproject.store.service;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-import static com.google.common.base.Preconditions.checkState;
-
-import com.google.common.base.MoreObjects;
-
-/**
- * Database update operation.
- *
- */
-public final class DatabaseUpdate {
-
-    /**
-     * Type of database update operation.
-     */
-    public enum Type {
-        /**
-         * Insert/Update entry without any checks.
-         */
-        PUT,
-        /**
-         * Insert an entry iff there is no existing entry for that key.
-         */
-        PUT_IF_ABSENT,
-
-        /**
-         * Update entry if the current version matches specified version.
-         */
-        PUT_IF_VERSION_MATCH,
-
-        /**
-         * Update entry if the current value matches specified value.
-         */
-        PUT_IF_VALUE_MATCH,
-
-        /**
-         * Remove entry without any checks.
-         */
-        REMOVE,
-
-        /**
-         * Remove entry if the current version matches specified version.
-         */
-        REMOVE_IF_VERSION_MATCH,
-
-        /**
-         * Remove entry if the current value matches specified value.
-         */
-        REMOVE_IF_VALUE_MATCH,
-    }
-
-    private Type type;
-    private String mapName;
-    private String key;
-    private byte[] value;
-    private byte[] currentValue;
-    private long currentVersion = -1;
-
-    /**
-     * Returns the type of update operation.
-     * @return type of update.
-     */
-    public Type type() {
-        return type;
-    }
-
-    /**
-     * Returns the name of map being updated.
-     * @return map name.
-     */
-    public String mapName() {
-        return mapName;
-    }
-
-    /**
-     * Returns the item key being updated.
-     * @return item key
-     */
-    public String key() {
-        return key;
-    }
-
-    /**
-     * Returns the new value.
-     * @return item's target value.
-     */
-    public byte[] value() {
-        return value;
-    }
-
-    /**
-     * Returns the expected current value in the database value for the key.
-     * @return current value in database.
-     */
-    public byte[] currentValue() {
-        return currentValue;
-    }
-
-    /**
-     * Returns the expected current version in the database for the key.
-     * @return expected version.
-     */
-    public long currentVersion() {
-        return currentVersion;
-    }
-
-    @Override
-    public String toString() {
-        return MoreObjects.toStringHelper(this)
-            .add("type", type)
-            .add("mapName", mapName)
-            .add("key", key)
-            .add("value", value)
-            .add("currentValue", currentValue)
-            .add("currentVersion", currentVersion)
-            .toString();
-    }
-
-    /**
-     * Creates a new builder instance.
-     *
-     * @return builder.
-     */
-    public static Builder newBuilder() {
-        return new Builder();
-    }
-
-    /**
-     * DatabaseUpdate builder.
-     *
-     */
-    public static final class Builder {
-
-        private DatabaseUpdate update = new DatabaseUpdate();
-
-        public DatabaseUpdate build() {
-            validateInputs();
-            return update;
-        }
-
-        public Builder withType(Type type) {
-            update.type = checkNotNull(type, "type cannot be null");
-            return this;
-        }
-
-        public Builder withMapName(String mapName) {
-            update.mapName = checkNotNull(mapName, "mapName cannot be null");
-            return this;
-        }
-
-        public Builder withKey(String key) {
-            update.key = checkNotNull(key, "key cannot be null");
-            return this;
-        }
-
-        public Builder withCurrentValue(byte[] value) {
-            update.currentValue = checkNotNull(value, "currentValue cannot be null");
-            return this;
-        }
-
-        public Builder withValue(byte[] value) {
-            update.value = checkNotNull(value, "value cannot be null");
-            return this;
-        }
-
-        public Builder withCurrentVersion(long version) {
-            checkArgument(version >= 0, "version cannot be negative");
-            update.currentVersion = version;
-            return this;
-        }
-
-        private void validateInputs() {
-            checkNotNull(update.type, "type must be specified");
-            checkNotNull(update.mapName, "map name must be specified");
-            checkNotNull(update.key, "key must be specified");
-            switch (update.type) {
-            case PUT:
-            case PUT_IF_ABSENT:
-                checkNotNull(update.value, "value must be specified.");
-                break;
-            case PUT_IF_VERSION_MATCH:
-                checkNotNull(update.value, "value must be specified.");
-                checkState(update.currentVersion >= 0, "current version must be specified");
-                break;
-            case PUT_IF_VALUE_MATCH:
-                checkNotNull(update.value, "value must be specified.");
-                checkNotNull(update.currentValue, "currentValue must be specified.");
-                break;
-            case REMOVE:
-                break;
-            case REMOVE_IF_VERSION_MATCH:
-                checkState(update.currentVersion >= 0, "current version must be specified");
-                break;
-            case REMOVE_IF_VALUE_MATCH:
-                checkNotNull(update.currentValue, "currentValue must be specified.");
-                break;
-            default:
-                throw new IllegalStateException("Unknown operation type");
-            }
-        }
-    }
-}
diff --git a/core/api/src/main/java/org/onosproject/store/service/DistributedPrimitive.java b/core/api/src/main/java/org/onosproject/store/service/DistributedPrimitive.java
index abdb14d..09d2979 100644
--- a/core/api/src/main/java/org/onosproject/store/service/DistributedPrimitive.java
+++ b/core/api/src/main/java/org/onosproject/store/service/DistributedPrimitive.java
@@ -61,7 +61,12 @@
         /**
          * Leader elector.
          */
-        LEADER_ELECTOR
+        LEADER_ELECTOR,
+
+        /**
+         * Transaction Context.
+         */
+        TRANSACTION_CONTEXT
     }
 
     static final long DEFAULT_OPERTATION_TIMEOUT_MILLIS = 5000L;
diff --git a/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java b/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java
index 2259104..1e9a36e 100644
--- a/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java
+++ b/core/api/src/main/java/org/onosproject/store/service/StorageAdminService.java
@@ -19,6 +19,8 @@
 import java.util.List;
 import java.util.Map;
 
+import org.onosproject.store.primitives.TransactionId;
+
 /**
  * Service for administering storage instances.
  */
@@ -62,14 +64,9 @@
     Map<String, Long> getInMemoryDatabaseCounters();
 
     /**
-     * Returns all the transactions in the system.
+     * Returns all pending transactions.
      *
-     * @return collection of transactions
+     * @return collection of pending transaction identifiers.
      */
-    Collection<Transaction> getTransactions();
-
-    /**
-     * Redrives stuck transactions while removing those that are done.
-     */
-    void redriveTransactions();
+    Collection<TransactionId> getPendingTransactions();
 }
diff --git a/core/api/src/main/java/org/onosproject/store/service/Transaction.java b/core/api/src/main/java/org/onosproject/store/service/Transaction.java
deleted file mode 100644
index 330d846..0000000
--- a/core/api/src/main/java/org/onosproject/store/service/Transaction.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Copyright 2015 Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onosproject.store.service;
-
-import java.util.List;
-
-/**
- * An immutable transaction object.
- */
-public interface Transaction {
-
-    enum State {
-        /**
-         * Indicates a new transaction that is about to be prepared. All transactions
-         * start their life in this state.
-         */
-        PREPARING,
-
-        /**
-         * Indicates a transaction that is successfully prepared i.e. all participants voted to commit
-         */
-        PREPARED,
-
-        /**
-         * Indicates a transaction that is about to be committed.
-         */
-        COMMITTING,
-
-        /**
-         * Indicates a transaction that has successfully committed.
-         */
-        COMMITTED,
-
-        /**
-         * Indicates a transaction that is about to be rolled back.
-         */
-        ROLLINGBACK,
-
-        /**
-         * Indicates a transaction that has been rolled back and all locks are released.
-         */
-        ROLLEDBACK
-    }
-
-    /**
-     * Returns the transaction Id.
-     *
-     * @return transaction id
-     */
-    long id();
-
-    /**
-     * Returns the list of updates that are part of this transaction.
-     *
-     * @return list of database updates
-     */
-    List<DatabaseUpdate> updates();
-
-    /**
-     * Returns the current state of this transaction.
-     *
-     * @return transaction state
-     */
-    State state();
-
-    /**
-     * Returns true if this transaction has completed execution.
-     *
-     * @return true is yes, false otherwise
-     */
-    default boolean isDone() {
-        return state() == State.COMMITTED || state() == State.ROLLEDBACK;
-    }
-
-    /**
-     * Returns a new transaction that is created by transitioning this one to the specified state.
-     *
-     * @param newState destination state
-     * @return a new transaction instance similar to the current one but its state set to specified state
-     */
-    Transaction transition(State newState);
-
-    /**
-     * Returns the system time when the transaction was last updated.
-     *
-     * @return last update time
-     */
-    long lastUpdated();
-}
diff --git a/core/api/src/main/java/org/onosproject/store/service/TransactionContext.java b/core/api/src/main/java/org/onosproject/store/service/TransactionContext.java
index ef97253..0ac490b 100644
--- a/core/api/src/main/java/org/onosproject/store/service/TransactionContext.java
+++ b/core/api/src/main/java/org/onosproject/store/service/TransactionContext.java
@@ -16,6 +16,8 @@
 
 package org.onosproject.store.service;
 
+import org.onosproject.store.primitives.TransactionId;
+
 /**
  * Provides a context for transactional operations.
  * <p>
@@ -31,14 +33,19 @@
  * context isolation level is REPEATABLE_READS i.e. only data that is committed can be read.
  * The only uncommitted data that can be read is the data modified by the current transaction.
  */
-public interface TransactionContext {
+public interface TransactionContext extends DistributedPrimitive {
+
+    @Override
+    default DistributedPrimitive.Type type() {
+        return DistributedPrimitive.Type.TRANSACTION_CONTEXT;
+    }
 
     /**
-     * Returns the unique transactionId.
+     * Returns the transaction identifier.
      *
      * @return transaction id
      */
-    long transactionId();
+    TransactionId transactionId();
 
     /**
      * Returns if this transaction context is open.
diff --git a/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java b/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java
index e9f3a02..8a43995 100644
--- a/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java
+++ b/core/api/src/main/java/org/onosproject/store/service/TransactionContextBuilder.java
@@ -15,33 +15,14 @@
  */
 package org.onosproject.store.service;
 
+import org.onosproject.store.primitives.DistributedPrimitiveBuilder;
+
 /**
- * Interface definition for a transaction context builder.
+ * Abstract base class for a transaction context builder.
  */
-public interface TransactionContextBuilder {
+public abstract class TransactionContextBuilder extends DistributedPrimitiveBuilder<TransactionContext> {
 
-    /**
-     * Disables distribution of map entries across multiple database partitions.
-     * <p>
-     * When partitioning is disabled, the returned map will have a single
-     * partition that spans the entire cluster. Furthermore, the changes made to
-     * the map are ephemeral and do not survive a full cluster restart.
-     * </p>
-     * <p>
-     * Note: By default, partitions are enabled. This feature is intended to
-     * simplify debugging.
-     * </p>
-     *
-     * @return this TransactionalContextBuilder
-     */
-    TransactionContextBuilder withPartitionsDisabled();
-
-    /**
-     * Builds a TransactionContext based on configuration options supplied to this
-     * builder.
-     *
-     * @return a new TransactionalContext
-     * @throws java.lang.RuntimeException if a mandatory parameter is missing
-     */
-    TransactionContext build();
+    public TransactionContextBuilder() {
+        super(DistributedPrimitive.Type.TRANSACTION_CONTEXT);
+    }
 }