blob: 1939a7ec239f4be3b702b2c7747359ee1b054c4a [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;
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;
alshabib7911a052014-10-16 17:49:37 -070029import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070030import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070031import org.onlab.packet.IpPrefix;
32import org.onlab.util.KryoPool;
33
34import com.google.common.collect.ImmutableMap;
35import com.google.common.collect.ImmutableSet;
36import com.google.common.testing.EqualsTester;
37
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070038public class KryoSerializerTest {
39
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070040 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070041 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070042 private static final DeviceId DID1 = deviceId("of:foo");
43 private static final DeviceId DID2 = deviceId("of:bar");
44 private static final PortNumber P1 = portNumber(1);
45 private static final PortNumber P2 = portNumber(2);
46 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
47 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
48 private static final String MFR = "whitebox";
49 private static final String HW = "1.1.x";
50 private static final String SW1 = "3.8.1";
51 private static final String SW2 = "3.9.5";
52 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070053 private static final ChassisId CID = new ChassisId();
54 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
55 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070056 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
57 .set("A1", "a1")
58 .set("B1", "b1")
59 .build();
60 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
61 .remove("A1")
62 .set("B3", "b3")
63 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070064
65 private static KryoPool kryos;
66
67 @BeforeClass
68 public static void setUpBeforeClass() throws Exception {
69 kryos = KryoPool.newBuilder()
Yuta HIGUCHI03fec1f2014-10-03 09:13:50 -070070 .register(KryoPoolUtil.API)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070071 .register(ImmutableMap.class, new ImmutableMapSerializer())
72 .register(ImmutableSet.class, new ImmutableSetSerializer())
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070073 .build();
74 }
75
76 @Before
77 public void setUp() throws Exception {
78 }
79
80 @After
81 public void tearDown() throws Exception {
82 // removing Kryo instance to use fresh Kryo on each tests
83 kryos.getKryo();
84 }
85
86 private static <T> void testSerialized(T original) {
87 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
88 kryos.serialize(original, buffer);
89 buffer.flip();
90 T copy = kryos.deserialize(buffer);
91
92 new EqualsTester()
93 .addEqualityGroup(original, copy)
94 .testEquals();
95 }
96
97
98 @Test
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070099 public final void testSerialization() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700100 testSerialized(new ConnectPoint(DID1, P1));
101 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
102 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700103 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
104 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700105 testSerialized(DID1);
106 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
107 testSerialized(ImmutableMap.of(DID1, DEV1));
108 testSerialized(ImmutableMap.of());
109 testSerialized(ImmutableSet.of(DID1, DID2));
110 testSerialized(ImmutableSet.of(DID1));
111 testSerialized(ImmutableSet.of());
112 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700113 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700114 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700115 testSerialized(new NodeId("SomeNodeIdentifier"));
116 testSerialized(P1);
117 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700118 testSerialized(PIDA);
119 testSerialized(new NodeId("bar"));
120 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
121 for (MastershipRole role : MastershipRole.values()) {
122 testSerialized(role);
123 }
124 }
125
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700126 @Test
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700127 public final void testAnnotations() {
128 // Annotations does not have equals defined, manually test equality
129 final byte[] a1Bytes = kryos.serialize(A1);
130 SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes);
131 assertAnnotationsEquals(copiedA1, A1);
132
133 final byte[] a12Bytes = kryos.serialize(A1_2);
134 SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes);
135 assertAnnotationsEquals(copiedA12, A1_2);
136 }
137
138 // code clone
139 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700140 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700141 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700142 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700143 }
144 assertEquals(expected.keys(), actual.keys());
145 for (String key : expected.keys()) {
146 assertEquals(expected.value(key), actual.value(key));
147 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700148 }
149
150}