blob: 82588b7c2fe4d2b9e2e36bc1a4e93cdd02b1cbe5 [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
Thomas Vachuskad16ce182014-10-29 17:25:29 -070018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.testing.EqualsTester;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070022import org.junit.After;
23import org.junit.Before;
24import org.junit.BeforeClass;
25import org.junit.Test;
26import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070027import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070028import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070029import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070030import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070031import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070032import org.onlab.onos.net.DefaultDevice;
33import org.onlab.onos.net.DefaultLink;
34import org.onlab.onos.net.DefaultPort;
35import org.onlab.onos.net.Device;
36import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070037import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070038import org.onlab.onos.net.Link;
39import org.onlab.onos.net.LinkKey;
40import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070041import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -070042import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070043import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070044import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070045import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080046import org.onlab.packet.Ip4Address;
47import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070048import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080049import org.onlab.packet.Ip4Prefix;
50import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070051import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070052import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070053
Thomas Vachuskad16ce182014-10-29 17:25:29 -070054import java.nio.ByteBuffer;
55
56import static java.util.Arrays.asList;
57import static org.junit.Assert.assertEquals;
58import static org.onlab.onos.net.DeviceId.deviceId;
59import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070060
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070061public class KryoSerializerTest {
62
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070063 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070064 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070065 private static final DeviceId DID1 = deviceId("of:foo");
66 private static final DeviceId DID2 = deviceId("of:bar");
67 private static final PortNumber P1 = portNumber(1);
68 private static final PortNumber P2 = portNumber(2);
69 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
70 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
71 private static final String MFR = "whitebox";
72 private static final String HW = "1.1.x";
73 private static final String SW1 = "3.8.1";
74 private static final String SW2 = "3.9.5";
75 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070076 private static final ChassisId CID = new ChassisId();
77 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
78 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070079 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
80 .set("A1", "a1")
81 .set("B1", "b1")
82 .build();
83 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
84 .remove("A1")
85 .set("B3", "b3")
86 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070087
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070088 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070089
90 @BeforeClass
91 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070092 }
93
94 @Before
95 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070096 serializer = new KryoSerializer() {
97
98 @Override
99 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700100 serializerPool = KryoNamespace.newBuilder()
101 .register(KryoNamespaces.API)
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700102 .build()
103 .populate(1);
104 }
105 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700106 }
107
108 @After
109 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700110 }
111
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700112 private <T> void testSerialized(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700113 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700114 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700115 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700116 T copy = serializer.decode(buffer);
117
118 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700119
120 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700121 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700122 .testEquals();
123 }
124
125
126 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700127 public void testConnectPoint() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700128 testSerialized(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700129 }
130
131 @Test
132 public void testDefaultLink() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700133 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700134 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700135 }
136
137 @Test
138 public void testDefaultPort() {
139 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700140 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700141 }
142
143 @Test
144 public void testDeviceId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700145 testSerialized(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700146 }
147
148 @Test
149 public void testImmutableMap() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700150 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
151 testSerialized(ImmutableMap.of(DID1, DEV1));
152 testSerialized(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700153 }
154
155 @Test
156 public void testImmutableSet() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700157 testSerialized(ImmutableSet.of(DID1, DID2));
158 testSerialized(ImmutableSet.of(DID1));
159 testSerialized(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700160 }
161
162 @Test
163 public void testImmutableList() {
164 testSerialized(ImmutableList.of(DID1, DID2));
165 testSerialized(ImmutableList.of(DID1));
166 testSerialized(ImmutableList.of());
167 }
168
169 @Test
170 public void testIpPrefix() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700171 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700172 }
173
174 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800175 public void testIp4Prefix() {
176 testSerialized(Ip4Prefix.valueOf("192.168.0.1/24"));
177 }
178
179 @Test
180 public void testIp6Prefix() {
181 testSerialized(Ip6Prefix.valueOf("1111:2222::/120"));
182 }
183
184 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700185 public void testIpAddress() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700186 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700187 }
188
189 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800190 public void testIp4Address() {
191 testSerialized(Ip4Address.valueOf("192.168.0.1"));
192 }
193
194 @Test
195 public void testIp6Address() {
196 testSerialized(Ip6Address.valueOf("1111:2222::"));
197 }
198
199 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700200 public void testMacAddress() {
201 testSerialized(MacAddress.valueOf("12:34:56:78:90:ab"));
202 }
203
204 @Test
205 public void testLinkKey() {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700206 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700207 }
208
209 @Test
210 public void testNodeId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700211 testSerialized(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700212 }
213
214 @Test
215 public void testPortNumber() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700216 testSerialized(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700217 }
218
219 @Test
220 public void testProviderId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700221 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700222 testSerialized(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700223 }
224
225 @Test
226 public void testMastershipTerm() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700227 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHIdfe6e3b2014-10-30 11:31:51 -0700228 testSerialized(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700229 }
230
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700231 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700232 public void testHostLocation() {
233 testSerialized(new HostLocation(CP1, 1234L));
234 }
235
236 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700237 public void testFlowId() {
238 testSerialized(FlowId.valueOf(0x12345678L));
239 }
240
241 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700242 public void testRoleInfo() {
243 testSerialized(new RoleInfo(new NodeId("master"),
244 asList(new NodeId("stby1"), new NodeId("stby2"))));
245 }
246
247 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700248 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700249 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700250 final byte[] a1Bytes = serializer.encode(A1);
251 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700252 assertAnnotationsEquals(copiedA1, A1);
253
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700254 final byte[] a12Bytes = serializer.encode(A1_2);
255 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700256 assertAnnotationsEquals(copiedA12, A1_2);
257 }
258
259 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700260 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700261 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700262 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700263 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700264 }
265 assertEquals(expected.keys(), actual.keys());
266 for (String key : expected.keys()) {
267 assertEquals(expected.value(key), actual.value(key));
268 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700269 }
270
271}