blob: d651d565673968a4bc393aa25f2458e8b01628ac [file] [log] [blame]
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07001package org.onlab.onos.store.serializers;
2
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -07003import static org.junit.Assert.assertEquals;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07004import static org.onlab.onos.net.DeviceId.deviceId;
5import static org.onlab.onos.net.PortNumber.portNumber;
6
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07007import java.nio.ByteBuffer;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -07008
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07009import org.junit.After;
10import org.junit.Before;
11import org.junit.BeforeClass;
12import org.junit.Test;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070013import org.onlab.onos.cluster.MastershipTerm;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070014import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070015import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070016import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070017import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070018import org.onlab.onos.net.DefaultDevice;
19import org.onlab.onos.net.DefaultLink;
20import org.onlab.onos.net.DefaultPort;
21import org.onlab.onos.net.Device;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.Link;
24import org.onlab.onos.net.LinkKey;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070025import org.onlab.onos.net.MastershipRole;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070026import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070027import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070028import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070029import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070030import org.onlab.packet.IpPrefix;
31import org.onlab.util.KryoPool;
32
33import com.google.common.collect.ImmutableMap;
34import com.google.common.collect.ImmutableSet;
35import com.google.common.testing.EqualsTester;
36
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070037public class KryoSerializerTest {
38
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070039 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070040 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070041 private static final DeviceId DID1 = deviceId("of:foo");
42 private static final DeviceId DID2 = deviceId("of:bar");
43 private static final PortNumber P1 = portNumber(1);
44 private static final PortNumber P2 = portNumber(2);
45 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
46 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
47 private static final String MFR = "whitebox";
48 private static final String HW = "1.1.x";
49 private static final String SW1 = "3.8.1";
50 private static final String SW2 = "3.9.5";
51 private static final String SN = "43311-12345";
52 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070053 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
54 .set("A1", "a1")
55 .set("B1", "b1")
56 .build();
57 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
58 .remove("A1")
59 .set("B3", "b3")
60 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070061
62 private static KryoPool kryos;
63
64 @BeforeClass
65 public static void setUpBeforeClass() throws Exception {
66 kryos = KryoPool.newBuilder()
Yuta HIGUCHI03fec1f2014-10-03 09:13:50 -070067 .register(KryoPoolUtil.API)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070068 .register(ImmutableMap.class, new ImmutableMapSerializer())
69 .register(ImmutableSet.class, new ImmutableSetSerializer())
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070070 .build();
71 }
72
73 @Before
74 public void setUp() throws Exception {
75 }
76
77 @After
78 public void tearDown() throws Exception {
79 // removing Kryo instance to use fresh Kryo on each tests
80 kryos.getKryo();
81 }
82
83 private static <T> void testSerialized(T original) {
84 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
85 kryos.serialize(original, buffer);
86 buffer.flip();
87 T copy = kryos.deserialize(buffer);
88
89 new EqualsTester()
90 .addEqualityGroup(original, copy)
91 .testEquals();
92 }
93
94
95 @Test
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070096 public final void testSerialization() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070097 testSerialized(new ConnectPoint(DID1, P1));
98 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
99 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700100 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
101 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700102 testSerialized(DID1);
103 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
104 testSerialized(ImmutableMap.of(DID1, DEV1));
105 testSerialized(ImmutableMap.of());
106 testSerialized(ImmutableSet.of(DID1, DID2));
107 testSerialized(ImmutableSet.of(DID1));
108 testSerialized(ImmutableSet.of());
109 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700110 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700111 testSerialized(new LinkKey(CP1, CP2));
112 testSerialized(new NodeId("SomeNodeIdentifier"));
113 testSerialized(P1);
114 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700115 testSerialized(PIDA);
116 testSerialized(new NodeId("bar"));
117 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
118 for (MastershipRole role : MastershipRole.values()) {
119 testSerialized(role);
120 }
121 }
122
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700123 @Test
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700124 public final void testAnnotations() {
125 // Annotations does not have equals defined, manually test equality
126 final byte[] a1Bytes = kryos.serialize(A1);
127 SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes);
128 assertAnnotationsEquals(copiedA1, A1);
129
130 final byte[] a12Bytes = kryos.serialize(A1_2);
131 SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes);
132 assertAnnotationsEquals(copiedA12, A1_2);
133 }
134
135 // code clone
136 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700137 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700138 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700139 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700140 }
141 assertEquals(expected.keys(), actual.keys());
142 for (String key : expected.keys()) {
143 assertEquals(expected.value(key), actual.value(key));
144 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700145 }
146
147}