Yuta HIGUCHI

implemented GossipDeviceStore with multi-provider, annotation support

Change-Id: I1953bdc37b28af79703ebcfc9201a71a2af49ab2
......@@ -2,7 +2,15 @@ package org.onlab.onos.store;
/**
* Opaque version structure.
* <p>
* Classes implementing this interface must also implement
* {@link #hashCode()} and {@link #equals(Object)}.
*/
public interface Timestamp extends Comparable<Timestamp> {
@Override
public abstract int hashCode();
@Override
public abstract boolean equals(Object obj);
}
......
package org.onlab.onos.store.common.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;
import org.onlab.onos.store.Timestamp;
/**
* Wrapper class to store Timestamped value.
* @param <T>
*/
public final class Timestamped<T> {
private final Timestamp timestamp;
private final T value;
/**
* Creates a time stamped value.
*
* @param value to be timestamp
* @param timestamp the timestamp
*/
public Timestamped(T value, Timestamp timestamp) {
this.value = checkNotNull(value);
this.timestamp = checkNotNull(timestamp);
}
/**
* Returns the value.
* @return value
*/
public T value() {
return value;
}
/**
* Returns the time stamp.
* @return time stamp
*/
public Timestamp timestamp() {
return timestamp;
}
/**
* Tests if this timestamped value is newer than the other.
*
* @param other timestamped value
* @return true if this instance is newer.
*/
public boolean isNewer(Timestamped<T> other) {
return this.timestamp.compareTo(checkNotNull(other).timestamp()) > 0;
}
@Override
public int hashCode() {
return timestamp.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Timestamped)) {
return false;
}
@SuppressWarnings("unchecked")
Timestamped<T> that = (Timestamped<T>) obj;
return Objects.equals(this.timestamp, that.timestamp);
}
// Default constructor for serialization
@Deprecated
protected Timestamped() {
this.value = null;
this.timestamp = null;
}
}
/**
* Common abstractions and facilities for implementing distributed store
* using gossip protocol.
*/
package org.onlab.onos.store.common.impl;
......@@ -17,9 +17,12 @@ import org.onlab.onos.store.Timestamp;
import org.onlab.onos.store.impl.OnosTimestamp;
import org.slf4j.Logger;
/**
* Clock service to issue Timestamp based on Device Mastership.
*/
@Component(immediate = true)
@Service
public class OnosClockService implements ClockService {
public class DeviceClockManager implements ClockService {
private final Logger log = getLogger(getClass());
......
......@@ -84,4 +84,11 @@ public final class OnosTimestamp implements Timestamp {
public int sequenceNumber() {
return sequenceNumber;
}
// Default constructor for serialization
@Deprecated
protected OnosTimestamp() {
this.termNumber = -1;
this.sequenceNumber = -1;
}
}
......
package org.onlab.onos.store.common.impl;
import static org.junit.Assert.*;
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;
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 {
}
@Test
public final void testHashCode() {
Timestamped<String> a = new Timestamped<>("a", TS_1_1);
Timestamped<String> b = new Timestamped<>("b", TS_1_1);
assertTrue("value does not impact hashCode",
a.hashCode() == b.hashCode());
}
@Test
public final void testEquals() {
Timestamped<String> a = new Timestamped<>("a", TS_1_1);
Timestamped<String> b = new Timestamped<>("b", TS_1_1);
assertTrue("value does not impact equality",
a.equals(b));
new EqualsTester()
.addEqualityGroup(new Timestamped<>("a", TS_1_1),
new Timestamped<>("b", TS_1_1),
new Timestamped<>("c", TS_1_1))
.addEqualityGroup(new Timestamped<>("a", TS_1_2),
new Timestamped<>("b", TS_1_2),
new Timestamped<>("c", TS_1_2))
.addEqualityGroup(new Timestamped<>("a", TS_2_1),
new Timestamped<>("b", TS_2_1),
new Timestamped<>("c", TS_2_1))
.testEquals();
}
@Test
public final void testValue() {
final Integer n = Integer.valueOf(42);
Timestamped<Integer> tsv = new Timestamped<>(n, TS_1_1);
assertSame(n, tsv.value());
}
@Test(expected = NullPointerException.class)
public final void testValueNonNull() {
new Timestamped<>(null, TS_1_1);
}
@Test(expected = NullPointerException.class)
public final void testTimestampNonNull() {
new Timestamped<>("Foo", null);
}
@Test
public final void testIsNewer() {
Timestamped<String> a = new Timestamped<>("a", TS_1_2);
Timestamped<String> b = new Timestamped<>("b", TS_1_1);
assertTrue(a.isNewer(b));
assertFalse(b.isNewer(a));
}
@Test
public final void testKryoSerializable() {
final ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
final KryoPool kryos = KryoPool.newBuilder()
.register(Timestamped.class,
OnosTimestamp.class)
.build();
Timestamped<String> original = new Timestamped<>("foobar", TS_1_1);
kryos.serialize(original, buffer);
buffer.flip();
Timestamped<String> copy = kryos.deserialize(buffer);
new EqualsTester()
.addEqualityGroup(original, copy)
.testEquals();
}
}