[ONOS-7117] Support configuring LeaderElector primitive session timeouts for faster leader elections

Change-Id: I81aa71cbb08ee31c145addbf00a6446ff4a5f9ed
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java b/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java
index 6b2a54c..5fad35a 100644
--- a/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java
+++ b/core/api/src/main/java/org/onosproject/store/primitives/DistributedPrimitiveCreator.java
@@ -16,6 +16,7 @@
 package org.onosproject.store.primitives;
 
 import java.util.Set;
+import java.util.concurrent.TimeUnit;
 
 import org.onosproject.store.service.AsyncAtomicCounter;
 import org.onosproject.store.service.AsyncAtomicCounterMap;
@@ -31,6 +32,8 @@
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.WorkQueue;
 
+import static org.onosproject.store.service.DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS;
+
 /**
  * Interface for entity that can create instances of different distributed primitives.
  */
@@ -120,7 +123,19 @@
      * @param name leader elector name
      * @return leader elector
      */
-    AsyncLeaderElector newAsyncLeaderElector(String name);
+    default AsyncLeaderElector newAsyncLeaderElector(String name) {
+        return newAsyncLeaderElector(name, DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
+    }
+
+    /**
+     * Creates a new {@code AsyncLeaderElector}.
+     *
+     * @param name leader elector name
+     * @param electionTimeout leader election timeout
+     * @param timeUnit leader election timeout time unit
+     * @return leader elector
+     */
+    AsyncLeaderElector newAsyncLeaderElector(String name, long electionTimeout, TimeUnit timeUnit);
 
     /**
      * Creates a new {@code WorkQueue}.
diff --git a/core/api/src/main/java/org/onosproject/store/service/LeaderElectorBuilder.java b/core/api/src/main/java/org/onosproject/store/service/LeaderElectorBuilder.java
index d395cc5..d3d6951 100644
--- a/core/api/src/main/java/org/onosproject/store/service/LeaderElectorBuilder.java
+++ b/core/api/src/main/java/org/onosproject/store/service/LeaderElectorBuilder.java
@@ -15,6 +15,8 @@
  */
 package org.onosproject.store.service;
 
+import java.util.concurrent.TimeUnit;
+
 import org.onosproject.store.primitives.DistributedPrimitiveBuilder;
 
 /**
@@ -22,7 +24,41 @@
  */
 public abstract class LeaderElectorBuilder
     extends DistributedPrimitiveBuilder<LeaderElectorBuilder, AsyncLeaderElector> {
+
+    private long electionTimeoutMillis = DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS;
+
     public LeaderElectorBuilder() {
         super(DistributedPrimitive.Type.LEADER_ELECTOR);
     }
+
+    /**
+     * Sets the election timeout in milliseconds.
+     *
+     * @param electionTimeoutMillis the election timeout in milliseconds
+     * @return leader elector builder
+     */
+    public LeaderElectorBuilder withElectionTimeout(long electionTimeoutMillis) {
+        this.electionTimeoutMillis = electionTimeoutMillis;
+        return this;
+    }
+
+    /**
+     * Sets the election timeout.
+     *
+     * @param electionTimeout the election timeout
+     * @param timeUnit the timeout time unit
+     * @return leader elector builder
+     */
+    public LeaderElectorBuilder withElectionTimeout(long electionTimeout, TimeUnit timeUnit) {
+        return withElectionTimeout(timeUnit.toMillis(electionTimeout));
+    }
+
+    /**
+     * Returns the election timeout in milliseconds.
+     *
+     * @return the election timeout in milliseconds
+     */
+    public final long electionTimeoutMillis() {
+        return electionTimeoutMillis;
+    }
 }