Support for a distributed counter
Change-Id: I346e9baa28556fac13e53771021f5f6fbcd75ac9
diff --git a/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounter.java b/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounter.java
new file mode 100644
index 0000000..8c27666
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/AsyncAtomicCounter.java
@@ -0,0 +1,38 @@
+/*
+ * 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.concurrent.CompletableFuture;
+
+/**
+ * An async atomic counter dispenses monotonically increasing values.
+ */
+public interface AsyncAtomicCounter {
+
+ /**
+ * Atomically increment by one the current value.
+ *
+ * @return updated value
+ */
+ CompletableFuture<Long> incrementAndGet();
+
+ /**
+ * Returns the current value of the counter without modifying it.
+ *
+ * @return current value
+ */
+ CompletableFuture<Long> get();
+}
diff --git a/core/api/src/main/java/org/onosproject/store/service/AtomicCounter.java b/core/api/src/main/java/org/onosproject/store/service/AtomicCounter.java
new file mode 100644
index 0000000..4385ce0
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/AtomicCounter.java
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/**
+ * An atomic counter dispenses monotonically increasing values.
+ */
+public interface AtomicCounter {
+
+ /**
+ * Atomically increment by one the current value.
+ *
+ * @return updated value
+ */
+ long incrementAndGet();
+
+ /**
+ * Returns the current value of the counter without modifying it.
+ *
+ * @return current value
+ */
+ long get();
+}
diff --git a/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java
new file mode 100644
index 0000000..ee8481d2
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/AtomicCounterBuilder.java
@@ -0,0 +1,52 @@
+package org.onosproject.store.service;
+
+/**
+ * Builder for AtomicCounter.
+ */
+public interface AtomicCounterBuilder {
+
+ /**
+ * Sets the name for the atomic counter.
+ * <p>
+ * Each atomic counter is identified by a unique name.
+ * </p>
+ * <p>
+ * Note: This is a mandatory parameter.
+ * </p>
+ *
+ * @param name name of the atomic counter
+ * @return this AtomicCounterBuilder
+ */
+ public AtomicCounterBuilder withName(String name);
+
+ /**
+ * Creates this counter on the partition that spans the entire cluster.
+ * <p>
+ * When partitioning is disabled, the counter state will be
+ * ephemeral and does not survive a full cluster restart.
+ * </p>
+ * <p>
+ * Note: By default partitions are enabled.
+ * </p>
+ * @return this AtomicCounterBuilder
+ */
+ public AtomicCounterBuilder withPartitionsDisabled();
+
+ /**
+ * Builds a AtomicCounter based on the configuration options
+ * supplied to this builder.
+ *
+ * @return new AtomicCounter
+ * @throws java.lang.RuntimeException if a mandatory parameter is missing
+ */
+ public AtomicCounter build();
+
+ /**
+ * Builds a AsyncAtomicCounter based on the configuration options
+ * supplied to this builder.
+ *
+ * @return new AsyncAtomicCounter
+ * @throws java.lang.RuntimeException if a mandatory parameter is missing
+ */
+ public AsyncAtomicCounter buildAsyncCounter();
+}
diff --git a/core/api/src/main/java/org/onosproject/store/service/ConsistentMapException.java b/core/api/src/main/java/org/onosproject/store/service/ConsistentMapException.java
index 7d6ad4d..94ed649 100644
--- a/core/api/src/main/java/org/onosproject/store/service/ConsistentMapException.java
+++ b/core/api/src/main/java/org/onosproject/store/service/ConsistentMapException.java
@@ -20,7 +20,7 @@
* Top level exception for ConsistentMap failures.
*/
@SuppressWarnings("serial")
-public class ConsistentMapException extends RuntimeException {
+public class ConsistentMapException extends StorageException {
public ConsistentMapException() {
}
diff --git a/core/api/src/main/java/org/onosproject/store/service/Serializer.java b/core/api/src/main/java/org/onosproject/store/service/Serializer.java
index 466943e..32ae585 100644
--- a/core/api/src/main/java/org/onosproject/store/service/Serializer.java
+++ b/core/api/src/main/java/org/onosproject/store/service/Serializer.java
@@ -16,6 +16,8 @@
package org.onosproject.store.service;
+import org.onlab.util.KryoNamespace;
+
/**
* Interface for serialization for store artifacts.
*/
@@ -35,4 +37,24 @@
* @param <T> decoded type
*/
<T> T decode(byte[] bytes);
+
+ /**
+ * Creates a new Serializer instance from a KryoNamespace.
+ *
+ * @param kryo kryo namespace
+ * @return Serializer instance
+ */
+ public static Serializer using(KryoNamespace kryo) {
+ return new Serializer() {
+ @Override
+ public <T> byte[] encode(T object) {
+ return kryo.serialize(object);
+ }
+
+ @Override
+ public <T> T decode(byte[] bytes) {
+ return kryo.deserialize(bytes);
+ }
+ };
+ }
}
\ No newline at end of file
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 eb129fe..204e35b 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
@@ -17,6 +17,7 @@
import java.util.Collection;
import java.util.List;
+import java.util.Map;
/**
* Service for administering storage instances.
@@ -38,6 +39,13 @@
List<MapInfo> getMapInfo();
/**
+ * Returns information about all the atomic counters in the system.
+ *
+ * @return mapping from counter name to that counter's next value
+ */
+ Map<String, Long> getCounters();
+
+ /**
* Returns all the transactions in the system.
*
* @return collection of transactions
diff --git a/core/api/src/main/java/org/onosproject/store/service/StorageException.java b/core/api/src/main/java/org/onosproject/store/service/StorageException.java
new file mode 100644
index 0000000..a66fc3e
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/store/service/StorageException.java
@@ -0,0 +1,48 @@
+/*
+ * 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;
+
+/**
+ * Top level exception for Store failures.
+ */
+@SuppressWarnings("serial")
+public class StorageException extends RuntimeException {
+ public StorageException() {
+ }
+
+ public StorageException(Throwable t) {
+ super(t);
+ }
+
+ /**
+ * Store operation timeout.
+ */
+ public static class Timeout extends StorageException {
+ }
+
+ /**
+ * Store update conflicts with an in flight transaction.
+ */
+ public static class ConcurrentModification extends StorageException {
+ }
+
+ /**
+ * Store operation interrupted.
+ */
+ public static class Interrupted extends StorageException {
+ }
+}
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 5ea0420..91cc273 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
@@ -16,6 +16,7 @@
package org.onosproject.store.service;
+
/**
* Storage service.
* <p>
@@ -55,6 +56,13 @@
<E> SetBuilder<E> setBuilder();
/**
+ * Creates a new AtomicCounterBuilder.
+ *
+ * @return atomic counter builder
+ */
+ AtomicCounterBuilder atomicCounterBuilder();
+
+ /**
* Creates a new transaction context.
*
* @return transaction context