blob: a2ceb39cff7ec3f8166dd8134a3de48d87e9d892 [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);
210 final FlowRule rule2 =
211 new DefaultFlowRule(DID1, DefaultTrafficSelector.builder().build(),
212 DefaultTrafficTreatment.builder().build(), 0, 0, 0, true);
213 final FlowRuleBatchEntry entry1 =
214 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
215 final FlowRuleBatchEntry entry2 =
216 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule2);
217
218 testSerializedEquals(ImmutableList.of(entry1, entry2));
219 }
220
221 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700222 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800223 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700224 }
225
226 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800227 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800228 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800229 }
230
231 @Test
232 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800233 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800234 }
235
236 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700237 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800238 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700239 }
240
241 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800242 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800243 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800244 }
245
246 @Test
247 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800248 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800249 }
250
251 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700252 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800253 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700254 }
255
256 @Test
257 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800258 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700259 }
260
261 @Test
262 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800263 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700264 }
265
266 @Test
267 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800268 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700269 }
270
271 @Test
272 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800273 testSerializedEquals(PID);
274 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700275 }
276
277 @Test
278 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800279 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
280 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700281 }
282
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700283 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700284 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800285 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700286 }
287
288 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700289 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800290 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700291 }
292
293 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700294 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800295 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700296 asList(new NodeId("stby1"), new NodeId("stby2"))));
297 }
298
299 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800300 public void testDefaultLinkResourceRequest() {
301 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
302 .addLambdaRequest()
303 .addBandwidthRequest(32.195)
304 .build()
305 );
306 }
307
308 @Test
309 public void testDefaultLinkResourceAllocations() {
310 LinkResourceRequest request = DefaultLinkResourceRequest
311 .builder(IntentId.valueOf(2501), ImmutableList.of())
312 .addLambdaRequest()
313 .addBandwidthRequest(32.195)
314 .build();
315 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
316 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
317 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
318 new LambdaResourceAllocation(Lambda.valueOf(1))));
319 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
320 }
321
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800322 @Test
323 public void testLambdaConstraint() {
324 testSerializable(new LambdaConstraint(Lambda.valueOf(1)));
325 }
326
327 @Test
328 public void testBandwidthConstraint() {
329 testSerializable(new BandwidthConstraint(Bandwidth.valueOf(1000.0)));
330 }
331
332 @Test
333 public void testLinkTypeConstraint() {
334 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
335 }
336
337 @Test
338 public void testLatencyConstraint() {
339 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
340 }
341
342 @Test
343 public void testWaypointConstraint() {
344 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
345 }
346
347 @Test
348 public void testObstacleConstraint() {
349 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
350 }
351
352 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800353 public void testArraysAsList() {
354 testSerializedEquals(Arrays.asList(1, 2, 3));
355 }
356
357 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800358 public void testAnnotationConstraint() {
359 testSerializable(new AnnotationConstraint("distance", 100.0));
360 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800361
362 @Test
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800363 public void testDefaultGroupId() {
364 testSerializedEquals(new DefaultGroupId(99));
365 }
366
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -0800367 @Test
368 public void testEmptySet() {
369 testSerializedEquals(Collections.emptySet());
370 }
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800371
372 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700373 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700374 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700375 final byte[] a1Bytes = serializer.encode(A1);
376 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700377 assertAnnotationsEquals(copiedA1, A1);
378
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700379 final byte[] a12Bytes = serializer.encode(A1_2);
380 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700381 assertAnnotationsEquals(copiedA12, A1_2);
382 }
383
384 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700385 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700386 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700387 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700388 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700389 }
390 assertEquals(expected.keys(), actual.keys());
391 for (String key : expected.keys()) {
392 assertEquals(expected.value(key), actual.value(key));
393 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700394 }
395
396}