blob: 59187da8f526c1d4aa02eff1723ac9432a53e530 [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 HIGUCHIadac04a2014-11-13 00:02:45 -080022
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070023import org.junit.After;
24import org.junit.Before;
25import org.junit.BeforeClass;
26import org.junit.Test;
27import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070028import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHIf6171232014-11-25 15:14:44 -080029import org.onlab.onos.core.DefaultGroupId;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070030import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070031import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070032import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070033import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070034import org.onlab.onos.net.DefaultDevice;
35import org.onlab.onos.net.DefaultLink;
36import org.onlab.onos.net.DefaultPort;
37import org.onlab.onos.net.Device;
38import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070039import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070040import org.onlab.onos.net.Link;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080041import org.onlab.onos.net.Link.Type;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070042import 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 HIGUCHIadac04a2014-11-13 00:02:45 -080046import org.onlab.onos.net.intent.IntentId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070047import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080048import org.onlab.onos.net.resource.Bandwidth;
49import org.onlab.onos.net.resource.BandwidthResourceAllocation;
50import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
51import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
52import org.onlab.onos.net.resource.Lambda;
53import org.onlab.onos.net.resource.LambdaResourceAllocation;
54import org.onlab.onos.net.resource.LinkResourceRequest;
55import org.onlab.onos.net.resource.ResourceAllocation;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080056import org.onlab.onos.net.intent.constraint.AnnotationConstraint;
57import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
58import org.onlab.onos.net.intent.constraint.LambdaConstraint;
59import org.onlab.onos.net.intent.constraint.LatencyConstraint;
60import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
61import org.onlab.onos.net.intent.constraint.ObstacleConstraint;
62import org.onlab.onos.net.intent.constraint.WaypointConstraint;
alshabib7911a052014-10-16 17:49:37 -070063import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070064import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080065import org.onlab.packet.Ip4Address;
66import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070067import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080068import org.onlab.packet.Ip4Prefix;
69import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070070import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070071import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070072
Thomas Vachuskad16ce182014-10-29 17:25:29 -070073import java.nio.ByteBuffer;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080074import java.util.Arrays;
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -080075import java.util.Collections;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080076import java.util.HashMap;
77import java.util.Map;
78import java.util.Set;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080079import java.time.Duration;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070080
81import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080082import static org.junit.Assert.*;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070083import static org.onlab.onos.net.DeviceId.deviceId;
84import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070085
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070086public class KryoSerializerTest {
87
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070088 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070089 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070090 private static final DeviceId DID1 = deviceId("of:foo");
91 private static final DeviceId DID2 = deviceId("of:bar");
92 private static final PortNumber P1 = portNumber(1);
93 private static final PortNumber P2 = portNumber(2);
94 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
95 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
96 private static final String MFR = "whitebox";
97 private static final String HW = "1.1.x";
98 private static final String SW1 = "3.8.1";
99 private static final String SW2 = "3.9.5";
100 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -0700101 private static final ChassisId CID = new ChassisId();
102 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
103 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700104 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
105 .set("A1", "a1")
106 .set("B1", "b1")
107 .build();
108 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
109 .remove("A1")
110 .set("B3", "b3")
111 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700112
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700113 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700114
115 @BeforeClass
116 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700117 }
118
119 @Before
120 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700121 serializer = new KryoSerializer() {
122
123 @Override
124 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700125 serializerPool = KryoNamespace.newBuilder()
126 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800127 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
128 .build();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700129 }
130 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700131 }
132
133 @After
134 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700135 }
136
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800137 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700138 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700139 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700140 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700141 T copy = serializer.decode(buffer);
142
143 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700144
145 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700146 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700147 .testEquals();
148 }
149
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800150 private <T> void testSerializable(T original) {
151 byte[] bs = serializer.encode(original);
152 T copy = serializer.decode(bs);
153 assertNotNull(copy);
154 }
155
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700156
157 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700158 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800159 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700160 }
161
162 @Test
163 public void testDefaultLink() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800164 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
165 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700166 }
167
168 @Test
169 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800170 testSerializedEquals(new DefaultPort(DEV1, P1, true));
171 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700172 }
173
174 @Test
175 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800176 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700177 }
178
179 @Test
180 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800181 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
182 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
183 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700184 }
185
186 @Test
187 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800188 testSerializedEquals(ImmutableSet.of(DID1, DID2));
189 testSerializedEquals(ImmutableSet.of(DID1));
190 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700191 }
192
193 @Test
194 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800195 testSerializedEquals(ImmutableList.of(DID1, DID2));
196 testSerializedEquals(ImmutableList.of(DID1));
197 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700198 }
199
200 @Test
201 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800202 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700203 }
204
205 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800206 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800207 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800208 }
209
210 @Test
211 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800212 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800213 }
214
215 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700216 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800217 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700218 }
219
220 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800221 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800222 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800223 }
224
225 @Test
226 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800227 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800228 }
229
230 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700231 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800232 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700233 }
234
235 @Test
236 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800237 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700238 }
239
240 @Test
241 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800242 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700243 }
244
245 @Test
246 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800247 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700248 }
249
250 @Test
251 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800252 testSerializedEquals(PID);
253 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700254 }
255
256 @Test
257 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800258 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
259 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700260 }
261
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700262 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700263 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800264 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700265 }
266
267 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700268 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800269 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700270 }
271
272 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700273 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800274 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700275 asList(new NodeId("stby1"), new NodeId("stby2"))));
276 }
277
278 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800279 public void testDefaultLinkResourceRequest() {
280 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
281 .addLambdaRequest()
282 .addBandwidthRequest(32.195)
283 .build()
284 );
285 }
286
287 @Test
288 public void testDefaultLinkResourceAllocations() {
289 LinkResourceRequest request = DefaultLinkResourceRequest
290 .builder(IntentId.valueOf(2501), ImmutableList.of())
291 .addLambdaRequest()
292 .addBandwidthRequest(32.195)
293 .build();
294 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
295 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
296 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
297 new LambdaResourceAllocation(Lambda.valueOf(1))));
298 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
299 }
300
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800301 @Test
302 public void testLambdaConstraint() {
303 testSerializable(new LambdaConstraint(Lambda.valueOf(1)));
304 }
305
306 @Test
307 public void testBandwidthConstraint() {
308 testSerializable(new BandwidthConstraint(Bandwidth.valueOf(1000.0)));
309 }
310
311 @Test
312 public void testLinkTypeConstraint() {
313 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
314 }
315
316 @Test
317 public void testLatencyConstraint() {
318 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
319 }
320
321 @Test
322 public void testWaypointConstraint() {
323 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
324 }
325
326 @Test
327 public void testObstacleConstraint() {
328 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
329 }
330
331 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800332 public void testArraysAsList() {
333 testSerializedEquals(Arrays.asList(1, 2, 3));
334 }
335
336 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800337 public void testAnnotationConstraint() {
338 testSerializable(new AnnotationConstraint("distance", 100.0));
339 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800340
341 @Test
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800342 public void testDefaultGroupId() {
343 testSerializedEquals(new DefaultGroupId(99));
344 }
345
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -0800346 @Test
347 public void testEmptySet() {
348 testSerializedEquals(Collections.emptySet());
349 }
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800350
351 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700352 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700353 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700354 final byte[] a1Bytes = serializer.encode(A1);
355 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700356 assertAnnotationsEquals(copiedA1, A1);
357
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700358 final byte[] a12Bytes = serializer.encode(A1_2);
359 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700360 assertAnnotationsEquals(copiedA12, A1_2);
361 }
362
363 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700364 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700365 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700366 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700367 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700368 }
369 assertEquals(expected.keys(), actual.keys());
370 for (String key : expected.keys()) {
371 assertEquals(expected.value(key), actual.value(key));
372 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700373 }
374
375}