renamed OnosTimestamp -> MastershipBasedTimestamp
Change-Id: Ie6ee5d85bc0f5aadfa32e27e1d31e3c1d5c9fea8
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/impl/OnosTimestamp.java b/core/store/dist/src/main/java/org/onlab/onos/store/common/impl/MastershipBasedTimestamp.java
similarity index 78%
rename from core/store/dist/src/main/java/org/onlab/onos/store/impl/OnosTimestamp.java
rename to core/store/dist/src/main/java/org/onlab/onos/store/common/impl/MastershipBasedTimestamp.java
index 3039f32..0f4f894 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/impl/OnosTimestamp.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/common/impl/MastershipBasedTimestamp.java
@@ -1,4 +1,4 @@
-package org.onlab.onos.store.impl;
+package org.onlab.onos.store.common.impl;
import static com.google.common.base.Preconditions.checkArgument;
@@ -9,12 +9,11 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.ComparisonChain;
-// If it is store specific, implement serializable interfaces?
/**
* Default implementation of Timestamp.
* TODO: Better documentation.
*/
-public final class OnosTimestamp implements Timestamp {
+public final class MastershipBasedTimestamp implements Timestamp {
private final int termNumber;
private final int sequenceNumber;
@@ -25,15 +24,16 @@
* @param termNumber the mastership termNumber
* @param sequenceNumber the sequenceNumber number within the termNumber
*/
- public OnosTimestamp(int termNumber, int sequenceNumber) {
+ public MastershipBasedTimestamp(int termNumber, int sequenceNumber) {
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(o instanceof MastershipBasedTimestamp,
+ "Must be MastershipBasedTimestamp", o);
+ MastershipBasedTimestamp that = (MastershipBasedTimestamp) o;
return ComparisonChain.start()
.compare(this.termNumber, that.termNumber)
@@ -51,10 +51,10 @@
if (this == obj) {
return true;
}
- if (!(obj instanceof OnosTimestamp)) {
+ if (!(obj instanceof MastershipBasedTimestamp)) {
return false;
}
- OnosTimestamp that = (OnosTimestamp) obj;
+ MastershipBasedTimestamp that = (MastershipBasedTimestamp) obj;
return Objects.equals(this.termNumber, that.termNumber) &&
Objects.equals(this.sequenceNumber, that.sequenceNumber);
}
@@ -87,7 +87,7 @@
// Default constructor for serialization
@Deprecated
- protected OnosTimestamp() {
+ protected MastershipBasedTimestamp() {
this.termNumber = -1;
this.sequenceNumber = -1;
}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/device/impl/DeviceClockManager.java b/core/store/dist/src/main/java/org/onlab/onos/store/device/impl/DeviceClockManager.java
index ff45285..e1e3692 100644
--- a/core/store/dist/src/main/java/org/onlab/onos/store/device/impl/DeviceClockManager.java
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/device/impl/DeviceClockManager.java
@@ -15,7 +15,7 @@
import org.onlab.onos.store.ClockProviderService;
import org.onlab.onos.store.ClockService;
import org.onlab.onos.store.Timestamp;
-import org.onlab.onos.store.impl.OnosTimestamp;
+import org.onlab.onos.store.common.impl.MastershipBasedTimestamp;
import org.slf4j.Logger;
/**
@@ -47,7 +47,7 @@
if (term == null) {
throw new IllegalStateException("Requesting timestamp for a deviceId without mastership");
}
- return new OnosTimestamp(term.termNumber(), ticker.incrementAndGet());
+ return new MastershipBasedTimestamp(term.termNumber(), ticker.incrementAndGet());
}
@Override
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/serializers/MastershipBasedTimestampSerializer.java b/core/store/dist/src/main/java/org/onlab/onos/store/serializers/MastershipBasedTimestampSerializer.java
new file mode 100644
index 0000000..9250076
--- /dev/null
+++ b/core/store/dist/src/main/java/org/onlab/onos/store/serializers/MastershipBasedTimestampSerializer.java
@@ -0,0 +1,36 @@
+package org.onlab.onos.store.serializers;
+
+import org.onlab.onos.store.common.impl.MastershipBasedTimestamp;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.Serializer;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+
+// To be used if Timestamp ever needs to cross bundle boundary.
+/**
+ * Kryo Serializer for {@link MastershipBasedTimestamp}.
+ */
+public class MastershipBasedTimestampSerializer extends Serializer<MastershipBasedTimestamp> {
+
+ /**
+ * Default constructor.
+ */
+ public MastershipBasedTimestampSerializer() {
+ // non-null, immutable
+ super(false, true);
+ }
+
+ @Override
+ public void write(Kryo kryo, Output output, MastershipBasedTimestamp object) {
+ output.writeInt(object.termNumber());
+ output.writeInt(object.sequenceNumber());
+ }
+
+ @Override
+ public MastershipBasedTimestamp read(Kryo kryo, Input input, Class<MastershipBasedTimestamp> type) {
+ final int term = input.readInt();
+ final int sequence = input.readInt();
+ return new MastershipBasedTimestamp(term, sequence);
+ }
+}
diff --git a/core/store/dist/src/main/java/org/onlab/onos/store/serializers/OnosTimestampSerializer.java b/core/store/dist/src/main/java/org/onlab/onos/store/serializers/OnosTimestampSerializer.java
deleted file mode 100644
index 192e035..0000000
--- a/core/store/dist/src/main/java/org/onlab/onos/store/serializers/OnosTimestampSerializer.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package org.onlab.onos.store.serializers;
-
-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) {
- output.writeInt(object.termNumber());
- output.writeInt(object.sequenceNumber());
- }
-
- @Override
- public OnosTimestamp read(Kryo kryo, Input input, Class<OnosTimestamp> type) {
- final int term = input.readInt();
- final int sequence = input.readInt();
- return new OnosTimestamp(term, sequence);
- }
-}
diff --git a/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/MastershipBasedTimestampTest.java b/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/MastershipBasedTimestampTest.java
new file mode 100644
index 0000000..ea63ef8
--- /dev/null
+++ b/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/MastershipBasedTimestampTest.java
@@ -0,0 +1,95 @@
+package org.onlab.onos.store.common.impl;
+
+import static org.junit.Assert.*;
+
+import java.nio.ByteBuffer;
+
+import org.junit.Test;
+import org.onlab.onos.store.Timestamp;
+import org.onlab.onos.store.serializers.MastershipBasedTimestampSerializer;
+import org.onlab.util.KryoPool;
+
+import com.google.common.testing.EqualsTester;
+
+/**
+ * Test of {@link MastershipBasedTimestamp}.
+ */
+public class MastershipBasedTimestampTest {
+
+ private static final Timestamp TS_1_1 = new MastershipBasedTimestamp(1, 1);
+ private static final Timestamp TS_1_2 = new MastershipBasedTimestamp(1, 2);
+ private static final Timestamp TS_2_1 = new MastershipBasedTimestamp(2, 1);
+ private static final Timestamp TS_2_2 = new MastershipBasedTimestamp(2, 2);
+
+ @Test
+ public final void testBasic() {
+ final int termNumber = 5;
+ final int sequenceNumber = 6;
+ MastershipBasedTimestamp ts = new MastershipBasedTimestamp(termNumber,
+ sequenceNumber);
+
+ assertEquals(termNumber, ts.termNumber());
+ assertEquals(sequenceNumber, ts.sequenceNumber());
+ }
+
+ @Test
+ public final void testCompareTo() {
+ assertTrue(TS_1_1.compareTo(TS_1_1) == 0);
+ assertTrue(TS_1_1.compareTo(new MastershipBasedTimestamp(1, 1)) == 0);
+
+ assertTrue(TS_1_1.compareTo(TS_1_2) < 0);
+ assertTrue(TS_1_2.compareTo(TS_1_1) > 0);
+
+ assertTrue(TS_1_2.compareTo(TS_2_1) < 0);
+ assertTrue(TS_1_2.compareTo(TS_2_2) < 0);
+ assertTrue(TS_2_1.compareTo(TS_1_1) > 0);
+ assertTrue(TS_2_2.compareTo(TS_1_1) > 0);
+ }
+
+ @Test
+ public final void testEqualsObject() {
+ new EqualsTester()
+ .addEqualityGroup(new MastershipBasedTimestamp(1, 1),
+ new MastershipBasedTimestamp(1, 1), TS_1_1)
+ .addEqualityGroup(new MastershipBasedTimestamp(1, 2),
+ new MastershipBasedTimestamp(1, 2), TS_1_2)
+ .addEqualityGroup(new MastershipBasedTimestamp(2, 1),
+ new MastershipBasedTimestamp(2, 1), TS_2_1)
+ .addEqualityGroup(new MastershipBasedTimestamp(2, 2),
+ new MastershipBasedTimestamp(2, 2), TS_2_2)
+ .testEquals();
+ }
+
+ @Test
+ public final void testKryoSerializable() {
+ final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
+ final KryoPool kryos = KryoPool.newBuilder()
+ .register(MastershipBasedTimestamp.class)
+ .build();
+
+ kryos.serialize(TS_2_1, buffer);
+ buffer.flip();
+ Timestamp copy = kryos.deserialize(buffer);
+
+ new EqualsTester()
+ .addEqualityGroup(TS_2_1, copy)
+ .testEquals();
+ }
+
+ @Test
+ public final void testKryoSerializableWithHandcraftedSerializer() {
+ final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
+ final KryoPool kryos = KryoPool.newBuilder()
+ .register(MastershipBasedTimestamp.class, new MastershipBasedTimestampSerializer())
+ .build();
+
+ kryos.serialize(TS_1_2, buffer);
+ buffer.flip();
+ Timestamp copy = kryos.deserialize(buffer);
+
+ new EqualsTester()
+ .addEqualityGroup(TS_1_2, copy)
+ .testEquals();
+ }
+
+}
diff --git a/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/TimestampedTest.java b/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/TimestampedTest.java
index f7fc291..4b44d40 100644
--- a/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/TimestampedTest.java
+++ b/core/store/dist/src/test/java/org/onlab/onos/store/common/impl/TimestampedTest.java
@@ -4,38 +4,20 @@
import java.nio.ByteBuffer;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.onos.store.Timestamp;
-import org.onlab.onos.store.impl.OnosTimestamp;
import org.onlab.util.KryoPool;
import com.google.common.testing.EqualsTester;
+/**
+ * Test of {@link Timestamped}.
+ */
public class TimestampedTest {
- private static final Timestamp TS_1_1 = new OnosTimestamp(1, 1);
- private static final Timestamp TS_1_2 = new OnosTimestamp(1, 2);
- private static final Timestamp TS_2_1 = new OnosTimestamp(2, 1);
-
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
-
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- }
-
- @Before
- public void setUp() throws Exception {
- }
-
- @After
- public void tearDown() throws Exception {
- }
+ private static final Timestamp TS_1_1 = new MastershipBasedTimestamp(1, 1);
+ private static final Timestamp TS_1_2 = new MastershipBasedTimestamp(1, 2);
+ private static final Timestamp TS_2_1 = new MastershipBasedTimestamp(2, 1);
@Test
public final void testHashCode() {
@@ -97,7 +79,7 @@
final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
final KryoPool kryos = KryoPool.newBuilder()
.register(Timestamped.class,
- OnosTimestamp.class)
+ MastershipBasedTimestamp.class)
.build();
Timestamped<String> original = new Timestamped<>("foobar", TS_1_1);