blob: 86ed13f16853cce094618d034195c2ae8adf67a2 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070017
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cluster.NodeId;
28import org.onosproject.cluster.RoleInfo;
29import org.onosproject.core.DefaultGroupId;
30import org.onosproject.mastership.MastershipTerm;
31import org.onosproject.net.Annotations;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultAnnotations;
34import org.onosproject.net.DefaultDevice;
35import org.onosproject.net.DefaultLink;
36import org.onosproject.net.DefaultPort;
37import org.onosproject.net.Device;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.HostLocation;
40import org.onosproject.net.Link;
41import org.onosproject.net.Link.Type;
42import org.onosproject.net.LinkKey;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.SparseAnnotations;
Sho SHIMIZU166e61e2015-01-21 18:07:12 -080045import org.onosproject.net.flow.DefaultFlowRule;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.flow.FlowId;
Sho SHIMIZU166e61e2015-01-21 18:07:12 -080049import org.onosproject.net.flow.FlowRule;
50import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080051import org.onosproject.net.intent.IntentId;
52import org.onosproject.net.provider.ProviderId;
53import org.onosproject.net.resource.Bandwidth;
54import org.onosproject.net.resource.BandwidthResourceAllocation;
55import org.onosproject.net.resource.DefaultLinkResourceAllocations;
56import org.onosproject.net.resource.DefaultLinkResourceRequest;
57import org.onosproject.net.resource.Lambda;
58import org.onosproject.net.resource.LambdaResourceAllocation;
59import org.onosproject.net.resource.LinkResourceRequest;
60import org.onosproject.net.resource.ResourceAllocation;
61import org.onosproject.net.intent.constraint.AnnotationConstraint;
62import org.onosproject.net.intent.constraint.BandwidthConstraint;
63import org.onosproject.net.intent.constraint.LambdaConstraint;
64import org.onosproject.net.intent.constraint.LatencyConstraint;
65import org.onosproject.net.intent.constraint.LinkTypeConstraint;
66import org.onosproject.net.intent.constraint.ObstacleConstraint;
67import org.onosproject.net.intent.constraint.WaypointConstraint;
alshabib7911a052014-10-16 17:49:37 -070068import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070069import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080070import org.onlab.packet.Ip4Address;
71import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070072import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080073import org.onlab.packet.Ip4Prefix;
74import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070075import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070076import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070077
Thomas Vachuskad16ce182014-10-29 17:25:29 -070078import java.nio.ByteBuffer;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080079import java.util.Arrays;
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -080080import java.util.Collections;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080081import java.util.HashMap;
82import java.util.Map;
83import java.util.Set;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080084import java.time.Duration;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070085
86import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080087import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080088import static org.onosproject.net.DeviceId.deviceId;
89import static org.onosproject.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070090
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070091public class KryoSerializerTest {
92
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070093 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070094 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070095 private static final DeviceId DID1 = deviceId("of:foo");
96 private static final DeviceId DID2 = deviceId("of:bar");
97 private static final PortNumber P1 = portNumber(1);
98 private static final PortNumber P2 = portNumber(2);
99 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
100 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
101 private static final String MFR = "whitebox";
102 private static final String HW = "1.1.x";
103 private static final String SW1 = "3.8.1";
104 private static final String SW2 = "3.9.5";
105 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -0700106 private static final ChassisId CID = new ChassisId();
107 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
108 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700109 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
110 .set("A1", "a1")
111 .set("B1", "b1")
112 .build();
113 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
114 .remove("A1")
115 .set("B3", "b3")
116 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700117
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700118 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700119
120 @BeforeClass
121 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700122 }
123
124 @Before
125 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700126 serializer = new KryoSerializer() {
127
128 @Override
129 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700130 serializerPool = KryoNamespace.newBuilder()
131 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800132 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
133 .build();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700134 }
135 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700136 }
137
138 @After
139 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700140 }
141
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800142 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700143 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700144 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700145 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700146 T copy = serializer.decode(buffer);
147
148 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700149
150 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700151 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700152 .testEquals();
153 }
154
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800155 private <T> void testSerializable(T original) {
156 byte[] bs = serializer.encode(original);
157 T copy = serializer.decode(bs);
158 assertNotNull(copy);
159 }
160
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700161
162 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700163 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800164 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700165 }
166
167 @Test
168 public void testDefaultLink() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800169 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
170 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700171 }
172
173 @Test
174 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800175 testSerializedEquals(new DefaultPort(DEV1, P1, true));
176 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700177 }
178
179 @Test
180 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800181 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700182 }
183
184 @Test
185 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800186 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
187 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
188 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700189 }
190
191 @Test
192 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800193 testSerializedEquals(ImmutableSet.of(DID1, DID2));
194 testSerializedEquals(ImmutableSet.of(DID1));
195 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700196 }
197
198 @Test
199 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800200 testSerializedEquals(ImmutableList.of(DID1, DID2));
201 testSerializedEquals(ImmutableList.of(DID1));
202 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700203 }
204
205 @Test
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800206 public void testFlowRuleBatchEntry() {
207 final FlowRule rule1 =
208 new DefaultFlowRule(DID1, DefaultTrafficSelector.builder().build(),
209 DefaultTrafficTreatment.builder().build(), 0, 0, 0, true);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800210 final FlowRuleBatchEntry entry1 =
211 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
212 final FlowRuleBatchEntry entry2 =
Sho SHIMIZU7446da92015-01-22 16:27:06 -0800213 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800214
Sho SHIMIZU7446da92015-01-22 16:27:06 -0800215 testSerializedEquals(entry1);
216 testSerializedEquals(entry2);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800217 }
218
219 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700220 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800221 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700222 }
223
224 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800225 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800226 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800227 }
228
229 @Test
230 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800231 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800232 }
233
234 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700235 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800236 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700237 }
238
239 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800240 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800241 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800242 }
243
244 @Test
245 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800246 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800247 }
248
249 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700250 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800251 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700252 }
253
254 @Test
255 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800256 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700257 }
258
259 @Test
260 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800261 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700262 }
263
264 @Test
265 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800266 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700267 }
268
269 @Test
270 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800271 testSerializedEquals(PID);
272 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700273 }
274
275 @Test
276 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800277 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
278 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700279 }
280
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700281 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700282 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800283 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700284 }
285
286 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700287 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800288 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700289 }
290
291 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700292 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800293 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700294 asList(new NodeId("stby1"), new NodeId("stby2"))));
295 }
296
297 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800298 public void testDefaultLinkResourceRequest() {
299 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
300 .addLambdaRequest()
301 .addBandwidthRequest(32.195)
302 .build()
303 );
304 }
305
306 @Test
307 public void testDefaultLinkResourceAllocations() {
308 LinkResourceRequest request = DefaultLinkResourceRequest
309 .builder(IntentId.valueOf(2501), ImmutableList.of())
310 .addLambdaRequest()
311 .addBandwidthRequest(32.195)
312 .build();
313 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
314 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
315 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
316 new LambdaResourceAllocation(Lambda.valueOf(1))));
317 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
318 }
319
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800320 @Test
321 public void testLambdaConstraint() {
322 testSerializable(new LambdaConstraint(Lambda.valueOf(1)));
323 }
324
325 @Test
326 public void testBandwidthConstraint() {
327 testSerializable(new BandwidthConstraint(Bandwidth.valueOf(1000.0)));
328 }
329
330 @Test
331 public void testLinkTypeConstraint() {
332 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
333 }
334
335 @Test
336 public void testLatencyConstraint() {
337 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
338 }
339
340 @Test
341 public void testWaypointConstraint() {
342 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
343 }
344
345 @Test
346 public void testObstacleConstraint() {
347 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
348 }
349
350 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800351 public void testArraysAsList() {
352 testSerializedEquals(Arrays.asList(1, 2, 3));
353 }
354
355 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800356 public void testAnnotationConstraint() {
357 testSerializable(new AnnotationConstraint("distance", 100.0));
358 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800359
360 @Test
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800361 public void testDefaultGroupId() {
362 testSerializedEquals(new DefaultGroupId(99));
363 }
364
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -0800365 @Test
366 public void testEmptySet() {
367 testSerializedEquals(Collections.emptySet());
368 }
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800369
370 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700371 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700372 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700373 final byte[] a1Bytes = serializer.encode(A1);
374 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700375 assertAnnotationsEquals(copiedA1, A1);
376
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700377 final byte[] a12Bytes = serializer.encode(A1_2);
378 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700379 assertAnnotationsEquals(copiedA12, A1_2);
380 }
381
382 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700383 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700384 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700385 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700386 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700387 }
388 assertEquals(expected.keys(), actual.keys());
389 for (String key : expected.keys()) {
390 assertEquals(expected.value(key), actual.value(key));
391 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700392 }
393
394}