blob: 625f4ad8c2a5427ecc29b592067b50dc8bbf8e9f [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;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070023import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070024import org.onlab.onos.net.Link;
25import org.onlab.onos.net.LinkKey;
26import 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;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070032import org.onlab.packet.MacAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070033import org.onlab.util.KryoPool;
34
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070035import com.google.common.collect.ImmutableList;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070036import com.google.common.collect.ImmutableMap;
37import com.google.common.collect.ImmutableSet;
38import com.google.common.testing.EqualsTester;
39
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070040public class KryoSerializerTest {
41
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070042 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070043 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070044 private static final DeviceId DID1 = deviceId("of:foo");
45 private static final DeviceId DID2 = deviceId("of:bar");
46 private static final PortNumber P1 = portNumber(1);
47 private static final PortNumber P2 = portNumber(2);
48 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
49 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
50 private static final String MFR = "whitebox";
51 private static final String HW = "1.1.x";
52 private static final String SW1 = "3.8.1";
53 private static final String SW2 = "3.9.5";
54 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070055 private static final ChassisId CID = new ChassisId();
56 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
57 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070058 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
59 .set("A1", "a1")
60 .set("B1", "b1")
61 .build();
62 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
63 .remove("A1")
64 .set("B3", "b3")
65 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070066
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070067 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070068
69 @BeforeClass
70 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070071 }
72
73 @Before
74 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070075 serializer = new KryoSerializer() {
76
77 @Override
78 protected void setupKryoPool() {
79 serializerPool = KryoPool.newBuilder()
80 .register(KryoPoolUtil.API)
81 .build()
82 .populate(1);
83 }
84 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070085 }
86
87 @After
88 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070089 }
90
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070091 private <T> void testSerialized(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070092 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070093 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070094 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070095 T copy = serializer.decode(buffer);
96
97 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070098
99 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700100 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700101 .testEquals();
102 }
103
104
105 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700106 public void testConnectPoint() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700107 testSerialized(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700108 }
109
110 @Test
111 public void testDefaultLink() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700112 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700113 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700114 }
115
116 @Test
117 public void testDefaultPort() {
118 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700119 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700120 }
121
122 @Test
123 public void testDeviceId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700124 testSerialized(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700125 }
126
127 @Test
128 public void testImmutableMap() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700129 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
130 testSerialized(ImmutableMap.of(DID1, DEV1));
131 testSerialized(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700132 }
133
134 @Test
135 public void testImmutableSet() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700136 testSerialized(ImmutableSet.of(DID1, DID2));
137 testSerialized(ImmutableSet.of(DID1));
138 testSerialized(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700139 }
140
141 @Test
142 public void testImmutableList() {
143 testSerialized(ImmutableList.of(DID1, DID2));
144 testSerialized(ImmutableList.of(DID1));
145 testSerialized(ImmutableList.of());
146 }
147
148 @Test
149 public void testIpPrefix() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700150 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700151 }
152
153 @Test
154 public void testIpAddress() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700155 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700156 }
157
158 @Test
159 public void testMacAddress() {
160 testSerialized(MacAddress.valueOf("12:34:56:78:90:ab"));
161 }
162
163 @Test
164 public void testLinkKey() {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700165 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700166 }
167
168 @Test
169 public void testNodeId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700170 testSerialized(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700171 }
172
173 @Test
174 public void testPortNumber() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700175 testSerialized(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700176 }
177
178 @Test
179 public void testProviderId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700180 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700181 testSerialized(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700182 }
183
184 @Test
185 public void testMastershipTerm() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700186 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700187 }
188
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700189 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700190 public void testHostLocation() {
191 testSerialized(new HostLocation(CP1, 1234L));
192 }
193
194 @Test
195 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700196 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700197 final byte[] a1Bytes = serializer.encode(A1);
198 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700199 assertAnnotationsEquals(copiedA1, A1);
200
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700201 final byte[] a12Bytes = serializer.encode(A1_2);
202 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700203 assertAnnotationsEquals(copiedA12, A1_2);
204 }
205
206 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700207 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700208 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700209 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700210 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700211 }
212 assertEquals(expected.keys(), actual.keys());
213 for (String key : expected.keys()) {
214 assertEquals(expected.value(key), actual.value(key));
215 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700216 }
217
218}