New and direct StorageService method for creating an AtomicCounter

Change-Id: I8c189584dde590842075bea7e03c4c8ecf8d72c2
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java
index 632d447..03db803 100644
--- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java
@@ -39,10 +39,6 @@
 
     private final Logger log = getLogger(getClass());
 
-    @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
-            required = false, multiValued = false)
-    private boolean inMemory = false;
-
     @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding",
             required = false, multiValued = false)
     private boolean getFirst = false;
@@ -63,16 +59,7 @@
     @Override
     protected void execute() {
         StorageService storageService = get(StorageService.class);
-        if (inMemory) {
-            atomicCounter = storageService.atomicCounterBuilder()
-                    .withName(counter)
-                    .withPartitionsDisabled()
-                    .build();
-        } else {
-            atomicCounter = storageService.atomicCounterBuilder()
-                    .withName(counter)
-                    .build();
-        }
+        atomicCounter = storageService.getAsyncAtomicCounter(counter);
         CompletableFuture<Long> result;
         if (delta != null) {
             if (getFirst) {
diff --git a/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java b/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java
index d3a860f..9287f32 100644
--- a/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java
+++ b/apps/test/loadtest/src/main/java/org/onosproject/loadtest/DistributedConsensusLoadTest.java
@@ -95,9 +95,8 @@
         appId = coreService.registerApplication("org.onosproject.loadtest");
         log.info("Started with {}", appId);
         for (int i = 0; i < TOTAL_COUNTERS; ++i) {
-            AsyncAtomicCounter counter = storageService.atomicCounterBuilder()
-                                .withName(String.format("onos-app-loadtest-counter-%d", i))
-                                .build();
+            AsyncAtomicCounter counter =
+                storageService.getAsyncAtomicCounter(String.format("onos-app-loadtest-counter-%d", i));
             counters.add(counter);
         }
         reporter.scheduleWithFixedDelay(() -> {
diff --git a/core/api/src/main/java/org/onosproject/store/service/StorageService.java b/core/api/src/main/java/org/onosproject/store/service/StorageService.java
index cb81cac..e10c3d0 100644
--- a/core/api/src/main/java/org/onosproject/store/service/StorageService.java
+++ b/core/api/src/main/java/org/onosproject/store/service/StorageService.java
@@ -87,4 +87,24 @@
      * @return a builder for a transaction context.
      */
     TransactionContextBuilder transactionContextBuilder();
+
+    /**
+     * Returns an instance of {@code AsyncAtomicCounter} with specified name.
+     * @param name counter name
+     *
+     * @return AsyncAtomicCounter instance
+     */
+    default AsyncAtomicCounter getAsyncAtomicCounter(String name) {
+        return atomicCounterBuilder().withName(name).build();
+    }
+
+    /**
+     * Returns an instance of {@code AtomicCounter} with specified name.
+     * @param name counter name
+     *
+     * @return AtomicCounter instance
+     */
+    default AtomicCounter getAtomicCounter(String name) {
+        return getAsyncAtomicCounter(name).asAtomicCounter();
+    }
 }
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 1046bba..6b0f3e4 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
@@ -67,10 +67,7 @@
 
     @Activate
     public void activate() {
-        appIdCounter = storageService.atomicCounterBuilder()
-                                      .withName("onos-app-id-counter")
-                                      .build()
-                                      .asAtomicCounter();
+        appIdCounter = storageService.getAtomicCounter("onos-app-id-counter");
 
         registeredIds = storageService.<String, ApplicationId>consistentMapBuilder()
                 .withName("onos-app-ids")
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 b3d53c4..a68dee8 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
@@ -23,11 +23,9 @@
 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;
-import org.onosproject.store.service.StorageException;
 import org.onosproject.store.service.StorageService;
 import org.slf4j.Logger;
 
@@ -42,9 +40,6 @@
 @Service
 public class ConsistentIdBlockStore implements IdBlockStore {
 
-    private static final int MAX_TRIES = 5;
-    private static final int RETRY_DELAY_MS = 2_000;
-
     private final Logger log = getLogger(getClass());
     private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap();
 
@@ -65,16 +60,7 @@
 
     @Override
     public IdBlock getIdBlock(String topic) {
-        AtomicCounter counter = topicCounters
-                .computeIfAbsent(topic,
-                                 name -> storageService.atomicCounterBuilder()
-                                         .withName(name)
-                                         .build()
-                                         .asAtomicCounter());
-        Long blockBase = Tools.retryable(counter::getAndAdd,
-                StorageException.class,
-                MAX_TRIES,
-                RETRY_DELAY_MS).apply(DEFAULT_BLOCK_SIZE);
-        return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
+        AtomicCounter counter = topicCounters.computeIfAbsent(topic, storageService::getAtomicCounter);
+        return new IdBlock(counter.getAndAdd(DEFAULT_BLOCK_SIZE), DEFAULT_BLOCK_SIZE);
     }
 }
diff --git a/core/store/dist/src/main/java/org/onosproject/store/core/impl/LogicalClockManager.java b/core/store/dist/src/main/java/org/onosproject/store/core/impl/LogicalClockManager.java
index b6ff961..5c00782 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/core/impl/LogicalClockManager.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/core/impl/LogicalClockManager.java
@@ -50,11 +50,7 @@
 
     @Activate
     public void activate() {
-        atomicCounter = storageService.atomicCounterBuilder()
-                                      .withName(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME)
-                                      .withPartitionsDisabled()
-                                      .build()
-                                      .asAtomicCounter();
+        atomicCounter = storageService.getAtomicCounter(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME);
         log.info("Started");
     }
 
diff --git a/core/store/dist/src/main/java/org/onosproject/store/flowobjective/impl/DistributedFlowObjectiveStore.java b/core/store/dist/src/main/java/org/onosproject/store/flowobjective/impl/DistributedFlowObjectiveStore.java
index 9bd6383..5794f45 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/flowobjective/impl/DistributedFlowObjectiveStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/flowobjective/impl/DistributedFlowObjectiveStore.java
@@ -69,11 +69,7 @@
                                 .build()))
                 .build();
 
-        nextIds = storageService.atomicCounterBuilder()
-                .withName("next-objective-counter")
-                .build()
-                .asAtomicCounter();
-
+        nextIds = storageService.getAtomicCounter("next-objective-counter");
         log.info("Started");
     }
 
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java
index 2d3022d..f21ae4c 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/Ofdpa2GroupHandler.java
@@ -132,10 +132,7 @@
         this.serviceDirectory = context.directory();
         this.groupService = serviceDirectory.get(GroupService.class);
         this.storageService = serviceDirectory.get(StorageService.class);
-        this.nextIndex = storageService.atomicCounterBuilder()
-                            .withName("group-id-index-counter")
-                            .build()
-                            .asAtomicCounter();
+        this.nextIndex = storageService.getAtomicCounter("group-id-index-counter");
 
         pendingNextObjectives = CacheBuilder.newBuilder()
                 .expireAfterWrite(20, TimeUnit.SECONDS)
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java
index 7866a80..ea4af1a 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/meter/impl/MeterManager.java
@@ -182,10 +182,7 @@
     }
 
     private AtomicCounter allocateCounter(DeviceId deviceId) {
-        return storageService.atomicCounterBuilder()
-                .withName(String.format(METERCOUNTERIDENTIFIER, deviceId))
-                .build()
-                .asAtomicCounter();
+        return storageService.getAtomicCounter(String.format(METERCOUNTERIDENTIFIER, deviceId));
     }
 
     private class InternalMeterProviderService