Timestamp, OnosTimestamp and it's serializer
Change-Id: I02e3563229ddae8fc4b705c708f4066d82e24cf4
Showing
9 changed files
with
154 additions
and
5 deletions
1 | +package org.onlab.onos.store.impl; | ||
2 | + | ||
3 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
4 | +import static com.google.common.base.Preconditions.checkArgument; | ||
5 | + | ||
6 | +import java.util.Objects; | ||
7 | + | ||
8 | +import org.onlab.onos.net.ElementId; | ||
9 | +import org.onlab.onos.store.Timestamp; | ||
10 | + | ||
11 | +import com.google.common.base.MoreObjects; | ||
12 | +import com.google.common.collect.ComparisonChain; | ||
13 | + | ||
14 | +// If it is store specific, implement serializable interfaces? | ||
15 | +/** | ||
16 | + * Default implementation of Timestamp. | ||
17 | + */ | ||
18 | +public final class OnosTimestamp implements Timestamp { | ||
19 | + | ||
20 | + private final ElementId id; | ||
21 | + private final int termNumber; | ||
22 | + private final int sequenceNumber; | ||
23 | + | ||
24 | + /** | ||
25 | + * Default version tuple. | ||
26 | + * | ||
27 | + * @param id identifier of the element | ||
28 | + * @param termNumber the mastership termNumber | ||
29 | + * @param sequenceNumber the sequenceNumber number within the termNumber | ||
30 | + */ | ||
31 | + public OnosTimestamp(ElementId id, int termNumber, int sequenceNumber) { | ||
32 | + this.id = checkNotNull(id); | ||
33 | + this.termNumber = termNumber; | ||
34 | + this.sequenceNumber = sequenceNumber; | ||
35 | + } | ||
36 | + | ||
37 | + @Override | ||
38 | + public int compareTo(Timestamp o) { | ||
39 | + checkArgument(o instanceof OnosTimestamp, "Must be OnosTimestamp", o); | ||
40 | + OnosTimestamp that = (OnosTimestamp) o; | ||
41 | + checkArgument(this.id.equals(that.id), | ||
42 | + "Cannot compare version for different element this:%s, that:%s", | ||
43 | + this, that); | ||
44 | + | ||
45 | + return ComparisonChain.start() | ||
46 | + .compare(this.termNumber, that.termNumber) | ||
47 | + .compare(this.sequenceNumber, that.sequenceNumber) | ||
48 | + .result(); | ||
49 | + } | ||
50 | + | ||
51 | + @Override | ||
52 | + public int hashCode() { | ||
53 | + return Objects.hash(id, termNumber, sequenceNumber); | ||
54 | + } | ||
55 | + | ||
56 | + @Override | ||
57 | + public boolean equals(Object obj) { | ||
58 | + if (this == obj) { | ||
59 | + return true; | ||
60 | + } | ||
61 | + if (!(obj instanceof OnosTimestamp)) { | ||
62 | + return false; | ||
63 | + } | ||
64 | + OnosTimestamp that = (OnosTimestamp) obj; | ||
65 | + return Objects.equals(this.id, that.id) && | ||
66 | + Objects.equals(this.termNumber, that.termNumber) && | ||
67 | + Objects.equals(this.sequenceNumber, that.sequenceNumber); | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + public String toString() { | ||
72 | + return MoreObjects.toStringHelper(getClass()) | ||
73 | + .add("id", id) | ||
74 | + .add("termNumber", termNumber) | ||
75 | + .add("sequenceNumber", sequenceNumber) | ||
76 | + .toString(); | ||
77 | + } | ||
78 | + | ||
79 | + /** | ||
80 | + * Returns the element. | ||
81 | + * | ||
82 | + * @return element identifier | ||
83 | + */ | ||
84 | + public ElementId id() { | ||
85 | + return id; | ||
86 | + } | ||
87 | + | ||
88 | + /** | ||
89 | + * Returns the termNumber. | ||
90 | + * | ||
91 | + * @return termNumber | ||
92 | + */ | ||
93 | + public int termNumber() { | ||
94 | + return termNumber; | ||
95 | + } | ||
96 | + | ||
97 | + /** | ||
98 | + * Returns the sequenceNumber number. | ||
99 | + * | ||
100 | + * @return sequenceNumber | ||
101 | + */ | ||
102 | + public int sequenceNumber() { | ||
103 | + return sequenceNumber; | ||
104 | + } | ||
105 | +} |
... | @@ -4,7 +4,9 @@ import com.hazelcast.config.Config; | ... | @@ -4,7 +4,9 @@ import com.hazelcast.config.Config; |
4 | import com.hazelcast.config.FileSystemXmlConfig; | 4 | import com.hazelcast.config.FileSystemXmlConfig; |
5 | import com.hazelcast.core.Hazelcast; | 5 | import com.hazelcast.core.Hazelcast; |
6 | import com.hazelcast.core.HazelcastInstance; | 6 | import com.hazelcast.core.HazelcastInstance; |
7 | + | ||
7 | import de.javakaffee.kryoserializers.URISerializer; | 8 | import de.javakaffee.kryoserializers.URISerializer; |
9 | + | ||
8 | import org.apache.felix.scr.annotations.Activate; | 10 | import org.apache.felix.scr.annotations.Activate; |
9 | import org.apache.felix.scr.annotations.Component; | 11 | import org.apache.felix.scr.annotations.Component; |
10 | import org.apache.felix.scr.annotations.Deactivate; | 12 | import org.apache.felix.scr.annotations.Deactivate; |
... | @@ -26,6 +28,7 @@ import org.onlab.onos.store.serializers.DefaultPortSerializer; | ... | @@ -26,6 +28,7 @@ import org.onlab.onos.store.serializers.DefaultPortSerializer; |
26 | import org.onlab.onos.store.serializers.DeviceIdSerializer; | 28 | import org.onlab.onos.store.serializers.DeviceIdSerializer; |
27 | import org.onlab.onos.store.serializers.IpPrefixSerializer; | 29 | import org.onlab.onos.store.serializers.IpPrefixSerializer; |
28 | import org.onlab.onos.store.serializers.NodeIdSerializer; | 30 | import org.onlab.onos.store.serializers.NodeIdSerializer; |
31 | +import org.onlab.onos.store.serializers.OnosTimestampSerializer; | ||
29 | import org.onlab.onos.store.serializers.PortNumberSerializer; | 32 | import org.onlab.onos.store.serializers.PortNumberSerializer; |
30 | import org.onlab.onos.store.serializers.ProviderIdSerializer; | 33 | import org.onlab.onos.store.serializers.ProviderIdSerializer; |
31 | import org.onlab.packet.IpPrefix; | 34 | import org.onlab.packet.IpPrefix; |
... | @@ -90,6 +93,7 @@ public class StoreManager implements StoreService { | ... | @@ -90,6 +93,7 @@ public class StoreManager implements StoreService { |
90 | .register(DeviceId.class, new DeviceIdSerializer()) | 93 | .register(DeviceId.class, new DeviceIdSerializer()) |
91 | .register(PortNumber.class, new PortNumberSerializer()) | 94 | .register(PortNumber.class, new PortNumberSerializer()) |
92 | .register(DefaultPort.class, new DefaultPortSerializer()) | 95 | .register(DefaultPort.class, new DefaultPortSerializer()) |
96 | + .register(OnosTimestamp.class, new OnosTimestampSerializer()) | ||
93 | .build() | 97 | .build() |
94 | .populate(10); | 98 | .populate(10); |
95 | } | 99 | } | ... | ... |
... | @@ -9,7 +9,6 @@ import com.esotericsoftware.kryo.Serializer; | ... | @@ -9,7 +9,6 @@ import com.esotericsoftware.kryo.Serializer; |
9 | import com.esotericsoftware.kryo.io.Input; | 9 | import com.esotericsoftware.kryo.io.Input; |
10 | import com.esotericsoftware.kryo.io.Output; | 10 | import com.esotericsoftware.kryo.io.Output; |
11 | 11 | ||
12 | -// TODO move to util, etc. | ||
13 | /** | 12 | /** |
14 | * Kryo Serializer for {@link DefaultPort}. | 13 | * Kryo Serializer for {@link DefaultPort}. |
15 | */ | 14 | */ | ... | ... |
... | @@ -9,7 +9,6 @@ import com.esotericsoftware.kryo.Serializer; | ... | @@ -9,7 +9,6 @@ import com.esotericsoftware.kryo.Serializer; |
9 | import com.esotericsoftware.kryo.io.Input; | 9 | import com.esotericsoftware.kryo.io.Input; |
10 | import com.esotericsoftware.kryo.io.Output; | 10 | import com.esotericsoftware.kryo.io.Output; |
11 | 11 | ||
12 | -//TODO move to util, etc. | ||
13 | /** | 12 | /** |
14 | * Kryo Serializer for {@link DeviceId}. | 13 | * Kryo Serializer for {@link DeviceId}. |
15 | */ | 14 | */ | ... | ... |
... | @@ -7,7 +7,6 @@ import com.esotericsoftware.kryo.Serializer; | ... | @@ -7,7 +7,6 @@ import com.esotericsoftware.kryo.Serializer; |
7 | import com.esotericsoftware.kryo.io.Input; | 7 | import com.esotericsoftware.kryo.io.Input; |
8 | import com.esotericsoftware.kryo.io.Output; | 8 | import com.esotericsoftware.kryo.io.Output; |
9 | 9 | ||
10 | -// TODO move to util, etc. | ||
11 | /** | 10 | /** |
12 | * Kryo Serializer for {@link IpPrefix}. | 11 | * Kryo Serializer for {@link IpPrefix}. |
13 | */ | 12 | */ | ... | ... |
1 | +package org.onlab.onos.store.serializers; | ||
2 | + | ||
3 | +import org.onlab.onos.net.ElementId; | ||
4 | +import org.onlab.onos.store.impl.OnosTimestamp; | ||
5 | + | ||
6 | +import com.esotericsoftware.kryo.Kryo; | ||
7 | +import com.esotericsoftware.kryo.Serializer; | ||
8 | +import com.esotericsoftware.kryo.io.Input; | ||
9 | +import com.esotericsoftware.kryo.io.Output; | ||
10 | + | ||
11 | +/** | ||
12 | + * Kryo Serializer for {@link OnosTimestamp}. | ||
13 | + */ | ||
14 | +public class OnosTimestampSerializer extends Serializer<OnosTimestamp> { | ||
15 | + | ||
16 | + /** | ||
17 | + * Default constructor. | ||
18 | + */ | ||
19 | + public OnosTimestampSerializer() { | ||
20 | + // non-null, immutable | ||
21 | + super(false, true); | ||
22 | + } | ||
23 | + @Override | ||
24 | + public void write(Kryo kryo, Output output, OnosTimestamp object) { | ||
25 | + kryo.writeClassAndObject(output, object.id()); | ||
26 | + output.writeInt(object.termNumber()); | ||
27 | + output.writeInt(object.sequenceNumber()); | ||
28 | + } | ||
29 | + | ||
30 | + @Override | ||
31 | + public OnosTimestamp read(Kryo kryo, Input input, Class<OnosTimestamp> type) { | ||
32 | + ElementId id = (ElementId) kryo.readClassAndObject(input); | ||
33 | + final int term = input.readInt(); | ||
34 | + final int sequence = input.readInt(); | ||
35 | + return new OnosTimestamp(id, term, sequence); | ||
36 | + } | ||
37 | +} |
... | @@ -7,7 +7,6 @@ import com.esotericsoftware.kryo.Serializer; | ... | @@ -7,7 +7,6 @@ import com.esotericsoftware.kryo.Serializer; |
7 | import com.esotericsoftware.kryo.io.Input; | 7 | import com.esotericsoftware.kryo.io.Input; |
8 | import com.esotericsoftware.kryo.io.Output; | 8 | import com.esotericsoftware.kryo.io.Output; |
9 | 9 | ||
10 | -// TODO move to util, etc. | ||
11 | /** | 10 | /** |
12 | * Serializer for {@link PortNumber}. | 11 | * Serializer for {@link PortNumber}. |
13 | */ | 12 | */ | ... | ... |
... | @@ -7,7 +7,6 @@ import com.esotericsoftware.kryo.Serializer; | ... | @@ -7,7 +7,6 @@ import com.esotericsoftware.kryo.Serializer; |
7 | import com.esotericsoftware.kryo.io.Input; | 7 | import com.esotericsoftware.kryo.io.Input; |
8 | import com.esotericsoftware.kryo.io.Output; | 8 | import com.esotericsoftware.kryo.io.Output; |
9 | 9 | ||
10 | -//TODO move to util, etc. | ||
11 | /** | 10 | /** |
12 | * Serializer for {@link ProviderId}. | 11 | * Serializer for {@link ProviderId}. |
13 | */ | 12 | */ | ... | ... |
-
Please register or login to post a comment