Timestamp, OnosTimestamp and it's serializer
Change-Id: I02e3563229ddae8fc4b705c708f4066d82e24cf4
diff --git a/core/api/src/main/java/org/onlab/onos/store/Timestamp.java b/core/api/src/main/java/org/onlab/onos/store/Timestamp.java
new file mode 100644
index 0000000..b9d3648
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/store/Timestamp.java
@@ -0,0 +1,8 @@
+package org.onlab.onos.store;
+
+/**
+ * Opaque version structure.
+ */
+public interface Timestamp extends Comparable<Timestamp> {
+
+}
diff --git a/core/store/src/main/java/org/onlab/onos/store/impl/OnosTimestamp.java b/core/store/src/main/java/org/onlab/onos/store/impl/OnosTimestamp.java
new file mode 100644
index 0000000..f994e02
--- /dev/null
+++ b/core/store/src/main/java/org/onlab/onos/store/impl/OnosTimestamp.java
@@ -0,0 +1,105 @@
+package org.onlab.onos.store.impl;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.Objects;
+
+import org.onlab.onos.net.ElementId;
+import org.onlab.onos.store.Timestamp;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.collect.ComparisonChain;
+
+// If it is store specific, implement serializable interfaces?
+/**
+ * Default implementation of Timestamp.
+ */
+public final class OnosTimestamp implements Timestamp {
+
+ private final ElementId id;
+ private final int termNumber;
+ private final int sequenceNumber;
+
+ /**
+ * Default version tuple.
+ *
+ * @param id identifier of the element
+ * @param termNumber the mastership termNumber
+ * @param sequenceNumber the sequenceNumber number within the termNumber
+ */
+ public OnosTimestamp(ElementId id, int termNumber, int sequenceNumber) {
+ this.id = checkNotNull(id);
+ this.termNumber = termNumber;
+ this.sequenceNumber = sequenceNumber;
+ }
+
+ @Override
+ public int compareTo(Timestamp o) {
+ checkArgument(o instanceof OnosTimestamp, "Must be OnosTimestamp", o);
+ OnosTimestamp that = (OnosTimestamp) o;
+ checkArgument(this.id.equals(that.id),
+ "Cannot compare version for different element this:%s, that:%s",
+ this, that);
+
+ return ComparisonChain.start()
+ .compare(this.termNumber, that.termNumber)
+ .compare(this.sequenceNumber, that.sequenceNumber)
+ .result();
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, termNumber, sequenceNumber);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof OnosTimestamp)) {
+ return false;
+ }
+ OnosTimestamp that = (OnosTimestamp) obj;
+ return Objects.equals(this.id, that.id) &&
+ Objects.equals(this.termNumber, that.termNumber) &&
+ Objects.equals(this.sequenceNumber, that.sequenceNumber);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("id", id)
+ .add("termNumber", termNumber)
+ .add("sequenceNumber", sequenceNumber)
+ .toString();
+ }
+
+ /**
+ * Returns the element.
+ *
+ * @return element identifier
+ */
+ public ElementId id() {
+ return id;
+ }
+
+ /**
+ * Returns the termNumber.
+ *
+ * @return termNumber
+ */
+ public int termNumber() {
+ return termNumber;
+ }
+
+ /**
+ * Returns the sequenceNumber number.
+ *
+ * @return sequenceNumber
+ */
+ public int sequenceNumber() {
+ return sequenceNumber;
+ }
+}
diff --git a/core/store/src/main/java/org/onlab/onos/store/impl/StoreManager.java b/core/store/src/main/java/org/onlab/onos/store/impl/StoreManager.java
index abd8ade..c22d915 100644
--- a/core/store/src/main/java/org/onlab/onos/store/impl/StoreManager.java
+++ b/core/store/src/main/java/org/onlab/onos/store/impl/StoreManager.java
@@ -4,7 +4,9 @@
import com.hazelcast.config.FileSystemXmlConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
+
import de.javakaffee.kryoserializers.URISerializer;
+
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
@@ -26,6 +28,7 @@
import org.onlab.onos.store.serializers.DeviceIdSerializer;
import org.onlab.onos.store.serializers.IpPrefixSerializer;
import org.onlab.onos.store.serializers.NodeIdSerializer;
+import org.onlab.onos.store.serializers.OnosTimestampSerializer;
import org.onlab.onos.store.serializers.PortNumberSerializer;
import org.onlab.onos.store.serializers.ProviderIdSerializer;
import org.onlab.packet.IpPrefix;
@@ -90,6 +93,7 @@
.register(DeviceId.class, new DeviceIdSerializer())
.register(PortNumber.class, new PortNumberSerializer())
.register(DefaultPort.class, new DefaultPortSerializer())
+ .register(OnosTimestamp.class, new OnosTimestampSerializer())
.build()
.populate(10);
}
diff --git a/core/store/src/main/java/org/onlab/onos/store/serializers/DefaultPortSerializer.java b/core/store/src/main/java/org/onlab/onos/store/serializers/DefaultPortSerializer.java
index 836adea..8455e80 100644
--- a/core/store/src/main/java/org/onlab/onos/store/serializers/DefaultPortSerializer.java
+++ b/core/store/src/main/java/org/onlab/onos/store/serializers/DefaultPortSerializer.java
@@ -9,7 +9,6 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
-// TODO move to util, etc.
/**
* Kryo Serializer for {@link DefaultPort}.
*/
diff --git a/core/store/src/main/java/org/onlab/onos/store/serializers/DeviceIdSerializer.java b/core/store/src/main/java/org/onlab/onos/store/serializers/DeviceIdSerializer.java
index 709dce0..c63b676 100644
--- a/core/store/src/main/java/org/onlab/onos/store/serializers/DeviceIdSerializer.java
+++ b/core/store/src/main/java/org/onlab/onos/store/serializers/DeviceIdSerializer.java
@@ -9,7 +9,6 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
-//TODO move to util, etc.
/**
* Kryo Serializer for {@link DeviceId}.
*/
diff --git a/core/store/src/main/java/org/onlab/onos/store/serializers/IpPrefixSerializer.java b/core/store/src/main/java/org/onlab/onos/store/serializers/IpPrefixSerializer.java
index 8ac6679..2dbec57 100644
--- a/core/store/src/main/java/org/onlab/onos/store/serializers/IpPrefixSerializer.java
+++ b/core/store/src/main/java/org/onlab/onos/store/serializers/IpPrefixSerializer.java
@@ -7,7 +7,6 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
-// TODO move to util, etc.
/**
* Kryo Serializer for {@link IpPrefix}.
*/
diff --git a/core/store/src/main/java/org/onlab/onos/store/serializers/OnosTimestampSerializer.java b/core/store/src/main/java/org/onlab/onos/store/serializers/OnosTimestampSerializer.java
new file mode 100644
index 0000000..812bc9d
--- /dev/null
+++ b/core/store/src/main/java/org/onlab/onos/store/serializers/OnosTimestampSerializer.java
@@ -0,0 +1,37 @@
+package org.onlab.onos.store.serializers;
+
+import org.onlab.onos.net.ElementId;
+import org.onlab.onos.store.impl.OnosTimestamp;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
+/**
+ * Kryo Serializer for {@link OnosTimestamp}.
+ */
+public class OnosTimestampSerializer extends Serializer<OnosTimestamp> {
+
+ /**
+ * Default constructor.
+ */
+ public OnosTimestampSerializer() {
+ // non-null, immutable
+ super(false, true);
+ }
+ @Override
+ public void write(Kryo kryo, Output output, OnosTimestamp object) {
+ kryo.writeClassAndObject(output, object.id());
+ output.writeInt(object.termNumber());
+ output.writeInt(object.sequenceNumber());
+ }
+
+ @Override
+ public OnosTimestamp read(Kryo kryo, Input input, Class<OnosTimestamp> type) {
+ ElementId id = (ElementId) kryo.readClassAndObject(input);
+ final int term = input.readInt();
+ final int sequence = input.readInt();
+ return new OnosTimestamp(id, term, sequence);
+ }
+}
diff --git a/core/store/src/main/java/org/onlab/onos/store/serializers/PortNumberSerializer.java b/core/store/src/main/java/org/onlab/onos/store/serializers/PortNumberSerializer.java
index e89c379..02805bb 100644
--- a/core/store/src/main/java/org/onlab/onos/store/serializers/PortNumberSerializer.java
+++ b/core/store/src/main/java/org/onlab/onos/store/serializers/PortNumberSerializer.java
@@ -7,7 +7,6 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
-// TODO move to util, etc.
/**
* Serializer for {@link PortNumber}.
*/
diff --git a/core/store/src/main/java/org/onlab/onos/store/serializers/ProviderIdSerializer.java b/core/store/src/main/java/org/onlab/onos/store/serializers/ProviderIdSerializer.java
index 3abb784..1a1c6f6 100644
--- a/core/store/src/main/java/org/onlab/onos/store/serializers/ProviderIdSerializer.java
+++ b/core/store/src/main/java/org/onlab/onos/store/serializers/ProviderIdSerializer.java
@@ -7,7 +7,6 @@
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
-//TODO move to util, etc.
/**
* Serializer for {@link ProviderId}.
*/