Added RetryingFunction for simplified retry support.
Moved retry logic out of primitives (AtomicCounter) to the caller site.

Change-Id: I319d61f153f98d421baf32a1b5cd69d20dc63427
diff --git a/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentApplicationIdStore.java b/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentApplicationIdStore.java
index ecfc3dc..e54b0ee 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentApplicationIdStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentApplicationIdStore.java
@@ -15,12 +15,10 @@
  */
 package org.onosproject.store.core.impl;
 
-import static org.onlab.util.Tools.groupedThreads;
 import static org.slf4j.LoggerFactory.getLogger;
 
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 
 import org.apache.felix.scr.annotations.Activate;
@@ -30,20 +28,21 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.KryoNamespace;
+import org.onlab.util.Tools;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.ApplicationIdStore;
 import org.onosproject.core.DefaultApplicationId;
 import org.onosproject.store.serializers.KryoNamespaces;
-import org.onosproject.store.service.AsyncAtomicCounter;
+import org.onosproject.store.service.AtomicCounter;
 import org.onosproject.store.service.ConsistentMap;
 import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageException;
 import org.onosproject.store.service.StorageService;
 import org.onosproject.store.service.Versioned;
 import org.slf4j.Logger;
 
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
-import com.google.common.util.concurrent.Futures;
 
 /**
  * ApplicationIdStore implementation on top of {@code AtomicCounter}
@@ -58,7 +57,7 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected StorageService storageService;
 
-    private AsyncAtomicCounter appIdCounter;
+    private AtomicCounter appIdCounter;
     private ConsistentMap<String, ApplicationId> registeredIds;
     private Map<String, ApplicationId> nameToAppIdCache = Maps.newConcurrentMap();
     private Map<Short, ApplicationId> idToAppIdCache = Maps.newConcurrentMap();
@@ -71,13 +70,10 @@
 
     @Activate
     public void activate() {
-        executor = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/store/appId", "retry-handler"));
         appIdCounter = storageService.atomicCounterBuilder()
                                       .withName("onos-app-id-counter")
                                       .withPartitionsDisabled()
-                                      .withRetryOnFailure()
-                                      .withRetryExecutor(executor)
-                                      .buildAsyncCounter();
+                                      .build();
 
         registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
                 .withName("onos-app-ids")
@@ -128,7 +124,9 @@
         ApplicationId appId = nameToAppIdCache.computeIfAbsent(name, key -> {
             Versioned<ApplicationId> existingAppId = registeredIds.get(name);
             if (existingAppId == null) {
-                int id = Futures.getUnchecked(appIdCounter.incrementAndGet()).intValue();
+                int id = Tools.retryable(appIdCounter::incrementAndGet, StorageException.class, 1, 2000)
+                              .get()
+                              .intValue();
                 DefaultApplicationId newAppId = new DefaultApplicationId(id, name);
                 existingAppId = registeredIds.putIfAbsent(name, newAppId);
                 if (existingAppId != null) {
diff --git a/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentIdBlockStore.java b/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentIdBlockStore.java
index 8924423..8913742 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentIdBlockStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/core/impl/ConsistentIdBlockStore.java
@@ -1,12 +1,14 @@
 package org.onosproject.store.core.impl;
 
 import com.google.common.collect.Maps;
+
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.Tools;
 import org.onosproject.core.IdBlock;
 import org.onosproject.core.IdBlockStore;
 import org.onosproject.store.service.AtomicCounter;
@@ -16,7 +18,6 @@
 
 import java.util.Map;
 
-import static org.onlab.util.Tools.randomDelay;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
@@ -54,19 +55,10 @@
                                  name -> storageService.atomicCounterBuilder()
                                          .withName(name)
                                          .build());
-        Throwable exc = null;
-        for (int i = 0; i < MAX_TRIES; i++) {
-            try {
-                Long blockBase = counter.getAndAdd(DEFAULT_BLOCK_SIZE);
-                return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
-            } catch (StorageException e) {
-                log.warn("Unable to allocate ID block due to {}; retrying...",
-                         e.getMessage());
-                exc = e;
-                randomDelay(RETRY_DELAY_MS); // FIXME: This is a deliberate hack; fix in Drake
-            }
-        }
-        throw new IllegalStateException("Unable to allocate ID block", exc);
+        Long blockBase = Tools.retryable(counter::getAndAdd,
+                StorageException.class,
+                MAX_TRIES,
+                RETRY_DELAY_MS).apply(DEFAULT_BLOCK_SIZE);
+        return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
     }
-
 }