Yuta HIGUCHI

Kryo related fixes

- ProviderIdSerializer fix (ancillary)
- minor improvements
- add test cases

Change-Id: I6d969ac6518c44499f0fac167db4bdf1964f6aaa
...@@ -22,7 +22,7 @@ public class MastershipTermSerializer extends Serializer<MastershipTerm> { ...@@ -22,7 +22,7 @@ public class MastershipTermSerializer extends Serializer<MastershipTerm> {
22 22
23 @Override 23 @Override
24 public MastershipTerm read(Kryo kryo, Input input, Class<MastershipTerm> type) { 24 public MastershipTerm read(Kryo kryo, Input input, Class<MastershipTerm> type) {
25 - final NodeId node = new NodeId(kryo.readObject(input, String.class)); 25 + final NodeId node = new NodeId(input.readString());
26 final int term = input.readInt(); 26 final int term = input.readInt();
27 return MastershipTerm.of(node, term); 27 return MastershipTerm.of(node, term);
28 } 28 }
......
...@@ -22,12 +22,12 @@ public final class NodeIdSerializer extends Serializer<NodeId> { ...@@ -22,12 +22,12 @@ public final class NodeIdSerializer extends Serializer<NodeId> {
22 22
23 @Override 23 @Override
24 public void write(Kryo kryo, Output output, NodeId object) { 24 public void write(Kryo kryo, Output output, NodeId object) {
25 - kryo.writeObject(output, object.toString()); 25 + output.writeString(object.toString());
26 } 26 }
27 27
28 @Override 28 @Override
29 public NodeId read(Kryo kryo, Input input, Class<NodeId> type) { 29 public NodeId read(Kryo kryo, Input input, Class<NodeId> type) {
30 - final String id = kryo.readObject(input, String.class); 30 + final String id = input.readString();
31 return new NodeId(id); 31 return new NodeId(id);
32 } 32 }
33 } 33 }
......
...@@ -24,13 +24,15 @@ public class ProviderIdSerializer extends Serializer<ProviderId> { ...@@ -24,13 +24,15 @@ public class ProviderIdSerializer extends Serializer<ProviderId> {
24 public void write(Kryo kryo, Output output, ProviderId object) { 24 public void write(Kryo kryo, Output output, ProviderId object) {
25 output.writeString(object.scheme()); 25 output.writeString(object.scheme());
26 output.writeString(object.id()); 26 output.writeString(object.id());
27 + output.writeBoolean(object.isAncillary());
27 } 28 }
28 29
29 @Override 30 @Override
30 public ProviderId read(Kryo kryo, Input input, Class<ProviderId> type) { 31 public ProviderId read(Kryo kryo, Input input, Class<ProviderId> type) {
31 String scheme = input.readString(); 32 String scheme = input.readString();
32 String id = input.readString(); 33 String id = input.readString();
33 - return new ProviderId(scheme, id); 34 + boolean isAncillary = input.readBoolean();
35 + return new ProviderId(scheme, id, isAncillary);
34 } 36 }
35 37
36 } 38 }
......
1 package org.onlab.onos.store.serializers; 1 package org.onlab.onos.store.serializers;
2 2
3 +import static org.junit.Assert.assertEquals;
3 import static org.onlab.onos.net.DeviceId.deviceId; 4 import static org.onlab.onos.net.DeviceId.deviceId;
4 import static org.onlab.onos.net.PortNumber.portNumber; 5 import static org.onlab.onos.net.PortNumber.portNumber;
5 6
6 import java.nio.ByteBuffer; 7 import java.nio.ByteBuffer;
8 +
7 import org.junit.After; 9 import org.junit.After;
8 import org.junit.Before; 10 import org.junit.Before;
9 import org.junit.BeforeClass; 11 import org.junit.BeforeClass;
10 import org.junit.Test; 12 import org.junit.Test;
13 +import org.onlab.onos.cluster.MastershipTerm;
11 import org.onlab.onos.cluster.NodeId; 14 import org.onlab.onos.cluster.NodeId;
15 +import org.onlab.onos.net.Annotations;
12 import org.onlab.onos.net.ConnectPoint; 16 import org.onlab.onos.net.ConnectPoint;
17 +import org.onlab.onos.net.DefaultAnnotations;
13 import org.onlab.onos.net.DefaultDevice; 18 import org.onlab.onos.net.DefaultDevice;
14 import org.onlab.onos.net.DefaultLink; 19 import org.onlab.onos.net.DefaultLink;
15 import org.onlab.onos.net.DefaultPort; 20 import org.onlab.onos.net.DefaultPort;
...@@ -17,8 +22,11 @@ import org.onlab.onos.net.Device; ...@@ -17,8 +22,11 @@ import org.onlab.onos.net.Device;
17 import org.onlab.onos.net.DeviceId; 22 import org.onlab.onos.net.DeviceId;
18 import org.onlab.onos.net.Link; 23 import org.onlab.onos.net.Link;
19 import org.onlab.onos.net.LinkKey; 24 import org.onlab.onos.net.LinkKey;
25 +import org.onlab.onos.net.MastershipRole;
20 import org.onlab.onos.net.PortNumber; 26 import org.onlab.onos.net.PortNumber;
27 +import org.onlab.onos.net.SparseAnnotations;
21 import org.onlab.onos.net.provider.ProviderId; 28 import org.onlab.onos.net.provider.ProviderId;
29 +import org.onlab.packet.IpAddress;
22 import org.onlab.packet.IpPrefix; 30 import org.onlab.packet.IpPrefix;
23 import org.onlab.util.KryoPool; 31 import org.onlab.util.KryoPool;
24 32
...@@ -28,6 +36,7 @@ import com.google.common.testing.EqualsTester; ...@@ -28,6 +36,7 @@ import com.google.common.testing.EqualsTester;
28 36
29 public class KryoSerializerTests { 37 public class KryoSerializerTests {
30 private static final ProviderId PID = new ProviderId("of", "foo"); 38 private static final ProviderId PID = new ProviderId("of", "foo");
39 + private static final ProviderId PIDA = new ProviderId("of", "foo", true);
31 private static final DeviceId DID1 = deviceId("of:foo"); 40 private static final DeviceId DID1 = deviceId("of:foo");
32 private static final DeviceId DID2 = deviceId("of:bar"); 41 private static final DeviceId DID2 = deviceId("of:bar");
33 private static final PortNumber P1 = portNumber(1); 42 private static final PortNumber P1 = portNumber(1);
...@@ -40,6 +49,14 @@ public class KryoSerializerTests { ...@@ -40,6 +49,14 @@ public class KryoSerializerTests {
40 private static final String SW2 = "3.9.5"; 49 private static final String SW2 = "3.9.5";
41 private static final String SN = "43311-12345"; 50 private static final String SN = "43311-12345";
42 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN); 51 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN);
52 + private static final SparseAnnotations A1 = DefaultAnnotations.builder()
53 + .set("A1", "a1")
54 + .set("B1", "b1")
55 + .build();
56 + private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
57 + .remove("A1")
58 + .set("B3", "b3")
59 + .build();
43 60
44 private static KryoPool kryos; 61 private static KryoPool kryos;
45 62
...@@ -49,9 +66,6 @@ public class KryoSerializerTests { ...@@ -49,9 +66,6 @@ public class KryoSerializerTests {
49 .register(KryoPoolUtil.API) 66 .register(KryoPoolUtil.API)
50 .register(ImmutableMap.class, new ImmutableMapSerializer()) 67 .register(ImmutableMap.class, new ImmutableMapSerializer())
51 .register(ImmutableSet.class, new ImmutableSetSerializer()) 68 .register(ImmutableSet.class, new ImmutableSetSerializer())
52 -
53 -
54 -
55 .build(); 69 .build();
56 } 70 }
57 71
...@@ -82,6 +96,8 @@ public class KryoSerializerTests { ...@@ -82,6 +96,8 @@ public class KryoSerializerTests {
82 testSerialized(new ConnectPoint(DID1, P1)); 96 testSerialized(new ConnectPoint(DID1, P1));
83 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); 97 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
84 testSerialized(new DefaultPort(DEV1, P1, true)); 98 testSerialized(new DefaultPort(DEV1, P1, true));
99 + testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
100 + testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
85 testSerialized(DID1); 101 testSerialized(DID1);
86 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); 102 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
87 testSerialized(ImmutableMap.of(DID1, DEV1)); 103 testSerialized(ImmutableMap.of(DID1, DEV1));
...@@ -90,10 +106,40 @@ public class KryoSerializerTests { ...@@ -90,10 +106,40 @@ public class KryoSerializerTests {
90 testSerialized(ImmutableSet.of(DID1)); 106 testSerialized(ImmutableSet.of(DID1));
91 testSerialized(ImmutableSet.of()); 107 testSerialized(ImmutableSet.of());
92 testSerialized(IpPrefix.valueOf("192.168.0.1/24")); 108 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
109 + testSerialized(IpAddress.valueOf("192.168.0.1"));
93 testSerialized(new LinkKey(CP1, CP2)); 110 testSerialized(new LinkKey(CP1, CP2));
94 testSerialized(new NodeId("SomeNodeIdentifier")); 111 testSerialized(new NodeId("SomeNodeIdentifier"));
95 testSerialized(P1); 112 testSerialized(P1);
96 testSerialized(PID); 113 testSerialized(PID);
114 + testSerialized(PIDA);
115 + testSerialized(new NodeId("bar"));
116 + testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
117 + for (MastershipRole role : MastershipRole.values()) {
118 + testSerialized(role);
119 + }
120 + }
121 +
122 + public final void testAnnotations() {
123 + // Annotations does not have equals defined, manually test equality
124 + final byte[] a1Bytes = kryos.serialize(A1);
125 + SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes);
126 + assertAnnotationsEquals(copiedA1, A1);
127 +
128 + final byte[] a12Bytes = kryos.serialize(A1_2);
129 + SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes);
130 + assertAnnotationsEquals(copiedA12, A1_2);
131 + }
132 +
133 + // code clone
134 + public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
135 + DefaultAnnotations expected = DefaultAnnotations.builder().build();
136 + for (SparseAnnotations a : annotations) {
137 + expected = DefaultAnnotations.merge(expected, a);
138 + }
139 + assertEquals(expected.keys(), actual.keys());
140 + for (String key : expected.keys()) {
141 + assertEquals(expected.value(key), actual.value(key));
142 + }
97 } 143 }
98 144
99 } 145 }
......