blob: df58ea54846004af32a6c5472db3bffeeee58842 [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;
13import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070014import org.onlab.onos.mastership.MastershipTerm;
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;
25import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070026import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070027import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070028import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070029import org.onlab.packet.IpPrefix;
30import org.onlab.util.KryoPool;
31
32import com.google.common.collect.ImmutableMap;
33import com.google.common.collect.ImmutableSet;
34import com.google.common.testing.EqualsTester;
35
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070036public class KryoSerializerTest {
37
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070038 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070039 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070040 private static final DeviceId DID1 = deviceId("of:foo");
41 private static final DeviceId DID2 = deviceId("of:bar");
42 private static final PortNumber P1 = portNumber(1);
43 private static final PortNumber P2 = portNumber(2);
44 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
45 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
46 private static final String MFR = "whitebox";
47 private static final String HW = "1.1.x";
48 private static final String SW1 = "3.8.1";
49 private static final String SW2 = "3.9.5";
50 private static final String SN = "43311-12345";
51 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070052 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();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070060
61 private static KryoPool kryos;
62
63 @BeforeClass
64 public static void setUpBeforeClass() throws Exception {
65 kryos = KryoPool.newBuilder()
Yuta HIGUCHI03fec1f2014-10-03 09:13:50 -070066 .register(KryoPoolUtil.API)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070067 .register(ImmutableMap.class, new ImmutableMapSerializer())
68 .register(ImmutableSet.class, new ImmutableSetSerializer())
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070069 .build();
70 }
71
72 @Before
73 public void setUp() throws Exception {
74 }
75
76 @After
77 public void tearDown() throws Exception {
78 // removing Kryo instance to use fresh Kryo on each tests
79 kryos.getKryo();
80 }
81
82 private static <T> void testSerialized(T original) {
83 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
84 kryos.serialize(original, buffer);
85 buffer.flip();
86 T copy = kryos.deserialize(buffer);
87
88 new EqualsTester()
89 .addEqualityGroup(original, copy)
90 .testEquals();
91 }
92
93
94 @Test
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070095 public final void testSerialization() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070096 testSerialized(new ConnectPoint(DID1, P1));
97 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
98 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070099 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
100 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700101 testSerialized(DID1);
102 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
103 testSerialized(ImmutableMap.of(DID1, DEV1));
104 testSerialized(ImmutableMap.of());
105 testSerialized(ImmutableSet.of(DID1, DID2));
106 testSerialized(ImmutableSet.of(DID1));
107 testSerialized(ImmutableSet.of());
108 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700109 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700110 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700111 testSerialized(new NodeId("SomeNodeIdentifier"));
112 testSerialized(P1);
113 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700114 testSerialized(PIDA);
115 testSerialized(new NodeId("bar"));
116 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700117 }
118
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700119 @Test
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700120 public final void testAnnotations() {
121 // Annotations does not have equals defined, manually test equality
122 final byte[] a1Bytes = kryos.serialize(A1);
123 SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes);
124 assertAnnotationsEquals(copiedA1, A1);
125
126 final byte[] a12Bytes = kryos.serialize(A1_2);
127 SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes);
128 assertAnnotationsEquals(copiedA12, A1_2);
129 }
130
131 // code clone
132 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700133 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700134 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700135 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700136 }
137 assertEquals(expected.keys(), actual.keys());
138 for (String key : expected.keys()) {
139 assertEquals(expected.value(key), actual.value(key));
140 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700141 }
142
143}