blob: 4e7a8e01de9bfdec6a46ef8766a3ddd4e7c9ee21 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070016package org.onlab.onos.store.serializers;
17
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070018import static org.junit.Assert.assertEquals;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070019import static org.onlab.onos.net.DeviceId.deviceId;
20import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070021import static java.util.Arrays.asList;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070022
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070023import java.nio.ByteBuffer;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070024
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070025import org.junit.After;
26import org.junit.Before;
27import org.junit.BeforeClass;
28import org.junit.Test;
29import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070030import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070031import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070032import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070033import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070034import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070035import org.onlab.onos.net.DefaultDevice;
36import org.onlab.onos.net.DefaultLink;
37import org.onlab.onos.net.DefaultPort;
38import org.onlab.onos.net.Device;
39import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070040import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070041import org.onlab.onos.net.Link;
42import org.onlab.onos.net.LinkKey;
43import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070044import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -070045import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070046import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070047import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070048import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070049import org.onlab.packet.IpPrefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070050import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070051import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070052
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070053import com.google.common.collect.ImmutableList;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070054import com.google.common.collect.ImmutableMap;
55import com.google.common.collect.ImmutableSet;
56import com.google.common.testing.EqualsTester;
57
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070058public class KryoSerializerTest {
59
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070060 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070061 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070062 private static final DeviceId DID1 = deviceId("of:foo");
63 private static final DeviceId DID2 = deviceId("of:bar");
64 private static final PortNumber P1 = portNumber(1);
65 private static final PortNumber P2 = portNumber(2);
66 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
67 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
68 private static final String MFR = "whitebox";
69 private static final String HW = "1.1.x";
70 private static final String SW1 = "3.8.1";
71 private static final String SW2 = "3.9.5";
72 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070073 private static final ChassisId CID = new ChassisId();
74 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
75 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070076 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
77 .set("A1", "a1")
78 .set("B1", "b1")
79 .build();
80 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
81 .remove("A1")
82 .set("B3", "b3")
83 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070084
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070085 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070086
87 @BeforeClass
88 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070089 }
90
91 @Before
92 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070093 serializer = new KryoSerializer() {
94
95 @Override
96 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070097 serializerPool = KryoNamespace.newBuilder()
98 .register(KryoNamespaces.API)
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070099 .build()
100 .populate(1);
101 }
102 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700103 }
104
105 @After
106 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700107 }
108
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700109 private <T> void testSerialized(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700110 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700111 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700112 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700113 T copy = serializer.decode(buffer);
114
115 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700116
117 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700118 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700119 .testEquals();
120 }
121
122
123 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700124 public void testConnectPoint() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700125 testSerialized(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700126 }
127
128 @Test
129 public void testDefaultLink() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700130 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700131 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700132 }
133
134 @Test
135 public void testDefaultPort() {
136 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700137 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700138 }
139
140 @Test
141 public void testDeviceId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700142 testSerialized(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700143 }
144
145 @Test
146 public void testImmutableMap() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700147 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
148 testSerialized(ImmutableMap.of(DID1, DEV1));
149 testSerialized(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700150 }
151
152 @Test
153 public void testImmutableSet() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700154 testSerialized(ImmutableSet.of(DID1, DID2));
155 testSerialized(ImmutableSet.of(DID1));
156 testSerialized(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700157 }
158
159 @Test
160 public void testImmutableList() {
161 testSerialized(ImmutableList.of(DID1, DID2));
162 testSerialized(ImmutableList.of(DID1));
163 testSerialized(ImmutableList.of());
164 }
165
166 @Test
167 public void testIpPrefix() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700168 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700169 }
170
171 @Test
172 public void testIpAddress() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700173 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700174 }
175
176 @Test
177 public void testMacAddress() {
178 testSerialized(MacAddress.valueOf("12:34:56:78:90:ab"));
179 }
180
181 @Test
182 public void testLinkKey() {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700183 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700184 }
185
186 @Test
187 public void testNodeId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700188 testSerialized(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700189 }
190
191 @Test
192 public void testPortNumber() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700193 testSerialized(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700194 }
195
196 @Test
197 public void testProviderId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700198 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700199 testSerialized(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700200 }
201
202 @Test
203 public void testMastershipTerm() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700204 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700205 }
206
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700207 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700208 public void testHostLocation() {
209 testSerialized(new HostLocation(CP1, 1234L));
210 }
211
212 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700213 public void testFlowId() {
214 testSerialized(FlowId.valueOf(0x12345678L));
215 }
216
217 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700218 public void testRoleInfo() {
219 testSerialized(new RoleInfo(new NodeId("master"),
220 asList(new NodeId("stby1"), new NodeId("stby2"))));
221 }
222
223 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700224 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700225 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700226 final byte[] a1Bytes = serializer.encode(A1);
227 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700228 assertAnnotationsEquals(copiedA1, A1);
229
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700230 final byte[] a12Bytes = serializer.encode(A1_2);
231 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700232 assertAnnotationsEquals(copiedA12, A1_2);
233 }
234
235 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700236 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700237 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700238 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700239 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700240 }
241 assertEquals(expected.keys(), actual.keys());
242 for (String key : expected.keys()) {
243 assertEquals(expected.value(key), actual.value(key));
244 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700245 }
246
247}