Implement Atomix AsyncAtomicCounterMap, AtomicCounterMap and state machine.
Change-Id: Ifd7f60ae8dcfe7239e034a92654b4ef30ffe46ae
diff --git a/core/api/src/main/java/org/onosproject/store/primitives/DefaultAtomicCounterMap.java b/core/api/src/main/java/org/onosproject/store/primitives/DefaultAtomicCounterMap.java
new file mode 100644
index 0000000..10b213a
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/primitives/DefaultAtomicCounterMap.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2017-present 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.primitives;
+
+import com.google.common.base.Throwables;
+import org.onosproject.store.service.AsyncAtomicCounterMap;
+import org.onosproject.store.service.AtomicCounterMap;
+import org.onosproject.store.service.ConsistentMapException;
+import org.onosproject.store.service.Synchronous;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Default implementation of {@code AtomicCounterMap}.
+ *
+ * @param <K> map key type
+ */
+public class DefaultAtomicCounterMap<K> extends Synchronous<AsyncAtomicCounterMap<K>> implements AtomicCounterMap<K> {
+
+ private final AsyncAtomicCounterMap<K> asyncCounterMap;
+ private final long operationTimeoutMillis;
+
+ public DefaultAtomicCounterMap(AsyncAtomicCounterMap<K> asyncCounterMap, long operationTimeoutMillis) {
+ super(asyncCounterMap);
+ this.asyncCounterMap = asyncCounterMap;
+ this.operationTimeoutMillis = operationTimeoutMillis;
+ }
+
+ @Override
+ public long incrementAndGet(K key) {
+ return complete(asyncCounterMap.incrementAndGet(key));
+ }
+
+ @Override
+ public long decrementAndGet(K key) {
+ return complete(asyncCounterMap.decrementAndGet(key));
+ }
+
+ @Override
+ public long getAndIncrement(K key) {
+ return complete(asyncCounterMap.getAndIncrement(key));
+ }
+
+ @Override
+ public long getAndDecrement(K key) {
+ return complete(asyncCounterMap.getAndDecrement(key));
+ }
+
+ @Override
+ public long addAndGet(K key, long delta) {
+ return complete(asyncCounterMap.addAndGet(key, delta));
+ }
+
+ @Override
+ public long getAndAdd(K key, long delta) {
+ return complete(asyncCounterMap.getAndAdd(key, delta));
+ }
+
+ @Override
+ public long get(K key) {
+ return complete(asyncCounterMap.get(key));
+ }
+
+ @Override
+ public long put(K key, long newValue) {
+ return complete(asyncCounterMap.put(key, newValue));
+ }
+
+ @Override
+ public long putIfAbsent(K key, long newValue) {
+ return complete(asyncCounterMap.putIfAbsent(key, newValue));
+ }
+
+ @Override
+ public boolean replace(K key, long expectedOldValue, long newValue) {
+ return complete(asyncCounterMap.replace(key, expectedOldValue, newValue));
+ }
+
+ @Override
+ public long remove(K key) {
+ return complete(asyncCounterMap.remove(key));
+ }
+
+ @Override
+ public boolean remove(K key, long value) {
+ return complete(asyncCounterMap.remove(key, value));
+ }
+
+ @Override
+ public int size() {
+ return complete(asyncCounterMap.size());
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return complete(asyncCounterMap.isEmpty());
+ }
+
+ @Override
+ public void clear() {
+ complete(asyncCounterMap.clear());
+ }
+
+ private <T> T complete(CompletableFuture<T> future) {
+ try {
+ return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new ConsistentMapException.Interrupted();
+ } catch (TimeoutException e) {
+ throw new ConsistentMapException.Timeout(name());
+ } catch (ExecutionException e) {
+ Throwables.propagateIfPossible(e.getCause());
+ throw new ConsistentMapException(e.getCause());
+ }
+ }
+}
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 2e986c4..d034261 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
@@ -15,9 +15,8 @@
*/
package org.onosproject.store.primitives;
-import java.util.Set;
-
import org.onosproject.store.service.AsyncAtomicCounter;
+import org.onosproject.store.service.AsyncAtomicCounterMap;
import org.onosproject.store.service.AsyncAtomicValue;
import org.onosproject.store.service.AsyncConsistentMap;
import org.onosproject.store.service.AsyncConsistentMultimap;
@@ -28,6 +27,8 @@
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.WorkQueue;
+import java.util.Set;
+
/**
* Interface for entity that can create instances of different distributed primitives.
*/
@@ -68,6 +69,17 @@
String name, Serializer serializer);
/**
+ * Creates a new {@code AsyncAtomicCounterMap}.
+ *
+ * @param name counter map name
+ * @param serializer serializer to use for serializing/deserializing keys
+ * @param <K> key type
+ * @return atomic counter map
+ */
+ <K> AsyncAtomicCounterMap<K> newAsyncAtomicCounterMap(
+ String name, Serializer serializer);
+
+ /**
* Creates a new {@code AsyncAtomicCounter}.
*
* @param name counter name
diff --git a/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounterMap.java b/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounterMap.java
index 6ac096f..e4a5408 100644
--- a/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounterMap.java
+++ b/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounterMap.java
@@ -15,6 +15,8 @@
*/
package org.onosproject.store.service;
+import org.onosproject.store.primitives.DefaultAtomicCounterMap;
+
import java.util.concurrent.CompletableFuture;
/**
@@ -139,4 +141,44 @@
* @return true if the value was removed, false otherwise
*/
CompletableFuture<Boolean> remove(K key, long value);
+
+ /**
+ * Returns the number of entries in the map.
+ *
+ * @return the number of entries in the map
+ */
+ CompletableFuture<Integer> size();
+
+ /**
+ * Returns a boolean indicating whether the map is empty.
+ *
+ * @return true if the map is empty, false otherwise
+ */
+ CompletableFuture<Boolean> isEmpty();
+
+ /**
+ * Removes all entries from the map.
+ *
+ * @return void
+ */
+ CompletableFuture<Void> clear();
+
+ /**
+ * Returns a new {@link AtomicCounterMap} that is backed by this instance.
+ *
+ * @return new {@code AtomicCounterMap} instance
+ */
+ default AtomicCounterMap<K> asAtomicCounterMap() {
+ return asAtomicCounterMap(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS);
+ }
+
+ /**
+ * Returns a new {@link AtomicCounterMap} that is backed by this instance.
+ *
+ * @param timeoutMillis timeout duration for the returned ConsistentMap operations
+ * @return new {@code AtomicCounterMap} instance
+ */
+ default AtomicCounterMap<K> asAtomicCounterMap(long timeoutMillis) {
+ return new DefaultAtomicCounterMap<>(this, timeoutMillis);
+ }
}
diff --git a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMap.java b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMap.java
index f6ab5d5..1d56946 100644
--- a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMap.java
+++ b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMap.java
@@ -93,7 +93,6 @@
*/
long put(K key, long newValue);
-
/**
* If key is not already associated with a value or if key is associated with
* zero, associate it with newValue. Returns the previous value associated with
@@ -137,4 +136,23 @@
* @return true if the value was removed, false otherwise
*/
boolean remove(K key, long value);
+
+ /**
+ * Returns the number of entries in the map.
+ *
+ * @return the number of entries in the map, including {@code 0} values
+ */
+ int size();
+
+ /**
+ * If the map is empty, returns true, otherwise false.
+ *
+ * @return true if the map is empty.
+ */
+ boolean isEmpty();
+
+ /**
+ * Clears all entries from the map.
+ */
+ void clear();
}
diff --git a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMapBuilder.java b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMapBuilder.java
index 56934f4..48c897b3 100644
--- a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMapBuilder.java
+++ b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterMapBuilder.java
@@ -20,9 +20,18 @@
/**
* Builder for AtomicCounterMap.
*/
-public abstract class AtomicCounterMapBuilder
- extends DistributedPrimitiveBuilder<AtomicCounterMapBuilder, AsyncAtomicCounterMap> {
+public abstract class AtomicCounterMapBuilder<K>
+ extends DistributedPrimitiveBuilder<AtomicCounterMapBuilder<K>, AtomicCounterMap<K>> {
public AtomicCounterMapBuilder() {
super(DistributedPrimitive.Type.COUNTER_MAP);
}
-}
\ No newline at end of file
+
+ /**
+ * Builds an async atomic counter map based on the configuration options
+ * supplied to this builder.
+ *
+ * @return new async atomic counter map
+ * @throws java.lang.RuntimeException if a mandatory parameter is missing
+ */
+ public abstract AsyncAtomicCounterMap<K> buildAsyncMap();
+}
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 d532be2..e034e57 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
@@ -69,6 +69,14 @@
<K, V> ConsistentMultimapBuilder<K, V> consistentMultimapBuilder();
/**
+ * Creates a new {@code AtomicCounterMapBuilder}.
+ *
+ * @param <K> key type
+ * @return builder for an atomic counter map
+ */
+ <K> AtomicCounterMapBuilder<K> atomicCounterMapBuilder();
+
+ /**
* Creates a new DistributedSetBuilder.
*
* @param <E> set element type