blob: 1bd2097952a70897e8f3d0e39633922107ca1dce [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;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -07006import static java.util.Arrays.asList;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07007
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07008import java.nio.ByteBuffer;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -07009
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070010import org.junit.After;
11import org.junit.Before;
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070015import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070016import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070017import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070018import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070019import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070020import org.onlab.onos.net.DefaultDevice;
21import org.onlab.onos.net.DefaultLink;
22import org.onlab.onos.net.DefaultPort;
23import org.onlab.onos.net.Device;
24import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070025import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070026import org.onlab.onos.net.Link;
27import org.onlab.onos.net.LinkKey;
28import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070029import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -070030import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070031import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070032import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070033import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070034import org.onlab.packet.IpPrefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070035import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070036import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070037
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070038import com.google.common.collect.ImmutableList;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070039import com.google.common.collect.ImmutableMap;
40import com.google.common.collect.ImmutableSet;
41import com.google.common.testing.EqualsTester;
42
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070043public class KryoSerializerTest {
44
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070045 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070046 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070047 private static final DeviceId DID1 = deviceId("of:foo");
48 private static final DeviceId DID2 = deviceId("of:bar");
49 private static final PortNumber P1 = portNumber(1);
50 private static final PortNumber P2 = portNumber(2);
51 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
52 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
53 private static final String MFR = "whitebox";
54 private static final String HW = "1.1.x";
55 private static final String SW1 = "3.8.1";
56 private static final String SW2 = "3.9.5";
57 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070058 private static final ChassisId CID = new ChassisId();
59 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
60 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070061 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
62 .set("A1", "a1")
63 .set("B1", "b1")
64 .build();
65 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
66 .remove("A1")
67 .set("B3", "b3")
68 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070069
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070070 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070071
72 @BeforeClass
73 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070074 }
75
76 @Before
77 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070078 serializer = new KryoSerializer() {
79
80 @Override
81 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070082 serializerPool = KryoNamespace.newBuilder()
83 .register(KryoNamespaces.API)
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070084 .build()
85 .populate(1);
86 }
87 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070088 }
89
90 @After
91 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070092 }
93
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070094 private <T> void testSerialized(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070095 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070096 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070097 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070098 T copy = serializer.decode(buffer);
99
100 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700101
102 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700103 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700104 .testEquals();
105 }
106
107
108 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700109 public void testConnectPoint() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700110 testSerialized(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700111 }
112
113 @Test
114 public void testDefaultLink() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700115 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700116 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700117 }
118
119 @Test
120 public void testDefaultPort() {
121 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700122 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700123 }
124
125 @Test
126 public void testDeviceId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700127 testSerialized(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700128 }
129
130 @Test
131 public void testImmutableMap() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700132 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
133 testSerialized(ImmutableMap.of(DID1, DEV1));
134 testSerialized(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700135 }
136
137 @Test
138 public void testImmutableSet() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700139 testSerialized(ImmutableSet.of(DID1, DID2));
140 testSerialized(ImmutableSet.of(DID1));
141 testSerialized(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700142 }
143
144 @Test
145 public void testImmutableList() {
146 testSerialized(ImmutableList.of(DID1, DID2));
147 testSerialized(ImmutableList.of(DID1));
148 testSerialized(ImmutableList.of());
149 }
150
151 @Test
152 public void testIpPrefix() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700153 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700154 }
155
156 @Test
157 public void testIpAddress() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700158 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700159 }
160
161 @Test
162 public void testMacAddress() {
163 testSerialized(MacAddress.valueOf("12:34:56:78:90:ab"));
164 }
165
166 @Test
167 public void testLinkKey() {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700168 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700169 }
170
171 @Test
172 public void testNodeId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700173 testSerialized(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700174 }
175
176 @Test
177 public void testPortNumber() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700178 testSerialized(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700179 }
180
181 @Test
182 public void testProviderId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700183 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700184 testSerialized(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700185 }
186
187 @Test
188 public void testMastershipTerm() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700189 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700190 }
191
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700192 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700193 public void testHostLocation() {
194 testSerialized(new HostLocation(CP1, 1234L));
195 }
196
197 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700198 public void testFlowId() {
199 testSerialized(FlowId.valueOf(0x12345678L));
200 }
201
202 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700203 public void testRoleInfo() {
204 testSerialized(new RoleInfo(new NodeId("master"),
205 asList(new NodeId("stby1"), new NodeId("stby2"))));
206 }
207
208 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700209 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700210 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700211 final byte[] a1Bytes = serializer.encode(A1);
212 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700213 assertAnnotationsEquals(copiedA1, A1);
214
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700215 final byte[] a12Bytes = serializer.encode(A1_2);
216 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700217 assertAnnotationsEquals(copiedA12, A1_2);
218 }
219
220 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700221 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700222 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700223 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700224 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700225 }
226 assertEquals(expected.keys(), actual.keys());
227 for (String key : expected.keys()) {
228 assertEquals(expected.value(key), actual.value(key));
229 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700230 }
231
232}