blob: f76513d8c9105aab60108acc9c6fca00e53e5d9f [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 HIGUCHI2fcfde92014-10-20 18:37:14 -070028import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070029import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070030import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070031import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070032import org.onlab.packet.IpPrefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070033import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070034import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070035
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070036import com.google.common.collect.ImmutableList;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070037import com.google.common.collect.ImmutableMap;
38import com.google.common.collect.ImmutableSet;
39import com.google.common.testing.EqualsTester;
40
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070041public class KryoSerializerTest {
42
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070043 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070044 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070045 private static final DeviceId DID1 = deviceId("of:foo");
46 private static final DeviceId DID2 = deviceId("of:bar");
47 private static final PortNumber P1 = portNumber(1);
48 private static final PortNumber P2 = portNumber(2);
49 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
50 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
51 private static final String MFR = "whitebox";
52 private static final String HW = "1.1.x";
53 private static final String SW1 = "3.8.1";
54 private static final String SW2 = "3.9.5";
55 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070056 private static final ChassisId CID = new ChassisId();
57 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
58 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070059 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
60 .set("A1", "a1")
61 .set("B1", "b1")
62 .build();
63 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
64 .remove("A1")
65 .set("B3", "b3")
66 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070067
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070068 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070069
70 @BeforeClass
71 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070072 }
73
74 @Before
75 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070076 serializer = new KryoSerializer() {
77
78 @Override
79 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070080 serializerPool = KryoNamespace.newBuilder()
81 .register(KryoNamespaces.API)
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070082 .build()
83 .populate(1);
84 }
85 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070086 }
87
88 @After
89 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070090 }
91
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070092 private <T> void testSerialized(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070093 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070094 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070095 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070096 T copy = serializer.decode(buffer);
97
98 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070099
100 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700101 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700102 .testEquals();
103 }
104
105
106 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700107 public void testConnectPoint() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700108 testSerialized(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700109 }
110
111 @Test
112 public void testDefaultLink() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700113 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700114 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700115 }
116
117 @Test
118 public void testDefaultPort() {
119 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700120 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700121 }
122
123 @Test
124 public void testDeviceId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700125 testSerialized(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700126 }
127
128 @Test
129 public void testImmutableMap() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700130 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
131 testSerialized(ImmutableMap.of(DID1, DEV1));
132 testSerialized(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700133 }
134
135 @Test
136 public void testImmutableSet() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700137 testSerialized(ImmutableSet.of(DID1, DID2));
138 testSerialized(ImmutableSet.of(DID1));
139 testSerialized(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700140 }
141
142 @Test
143 public void testImmutableList() {
144 testSerialized(ImmutableList.of(DID1, DID2));
145 testSerialized(ImmutableList.of(DID1));
146 testSerialized(ImmutableList.of());
147 }
148
149 @Test
150 public void testIpPrefix() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700151 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700152 }
153
154 @Test
155 public void testIpAddress() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700156 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700157 }
158
159 @Test
160 public void testMacAddress() {
161 testSerialized(MacAddress.valueOf("12:34:56:78:90:ab"));
162 }
163
164 @Test
165 public void testLinkKey() {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700166 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700167 }
168
169 @Test
170 public void testNodeId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700171 testSerialized(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700172 }
173
174 @Test
175 public void testPortNumber() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700176 testSerialized(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700177 }
178
179 @Test
180 public void testProviderId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700181 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700182 testSerialized(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700183 }
184
185 @Test
186 public void testMastershipTerm() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700187 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700188 }
189
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700190 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700191 public void testHostLocation() {
192 testSerialized(new HostLocation(CP1, 1234L));
193 }
194
195 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700196 public void testFlowId() {
197 testSerialized(FlowId.valueOf(0x12345678L));
198 }
199
200 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700201 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700202 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700203 final byte[] a1Bytes = serializer.encode(A1);
204 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700205 assertAnnotationsEquals(copiedA1, A1);
206
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700207 final byte[] a12Bytes = serializer.encode(A1_2);
208 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700209 assertAnnotationsEquals(copiedA12, A1_2);
210 }
211
212 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700213 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700214 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700215 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700216 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700217 }
218 assertEquals(expected.keys(), actual.keys());
219 for (String key : expected.keys()) {
220 assertEquals(expected.value(key), actual.value(key));
221 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700222 }
223
224}