2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.store.serializers;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.testing.EqualsTester;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.onlab.packet.VlanId;
28 import org.onlab.util.Bandwidth;
29 import org.onlab.util.Frequency;
30 import org.onosproject.cluster.NodeId;
31 import org.onosproject.cluster.RoleInfo;
32 import org.onosproject.core.DefaultApplicationId;
33 import org.onosproject.core.DefaultGroupId;
34 import org.onosproject.mastership.MastershipTerm;
35 import org.onosproject.net.Annotations;
36 import org.onosproject.net.ChannelSpacing;
37 import org.onosproject.net.ConnectPoint;
38 import org.onosproject.net.DefaultAnnotations;
39 import org.onosproject.net.DefaultDevice;
40 import org.onosproject.net.DefaultLink;
41 import org.onosproject.net.DefaultPort;
42 import org.onosproject.net.Device;
43 import org.onosproject.net.DeviceId;
44 import org.onosproject.net.GridType;
45 import org.onosproject.net.HostLocation;
46 import org.onosproject.net.IndexedLambda;
47 import org.onosproject.net.Link;
48 import org.onosproject.net.Link.Type;
49 import org.onosproject.net.LinkKey;
50 import org.onosproject.net.OchPort;
51 import org.onosproject.net.OchSignal;
52 import org.onosproject.net.OduCltPort;
53 import org.onosproject.net.OmsPort;
54 import org.onosproject.net.PortNumber;
55 import org.onosproject.net.OduSignalType;
56 import org.onosproject.net.SparseAnnotations;
57 import org.onosproject.net.flow.DefaultFlowRule;
58 import org.onosproject.net.flow.DefaultTrafficSelector;
59 import org.onosproject.net.flow.DefaultTrafficTreatment;
60 import org.onosproject.net.flow.FlowId;
61 import org.onosproject.net.flow.FlowRule;
62 import org.onosproject.net.flow.FlowRuleBatchEntry;
63 import org.onosproject.net.intent.IntentId;
64 import org.onosproject.net.newresource.ResourcePath;
65 import org.onosproject.net.provider.ProviderId;
66 import org.onosproject.net.resource.link.BandwidthResource;
67 import org.onosproject.net.resource.link.BandwidthResourceAllocation;
68 import org.onosproject.net.resource.link.DefaultLinkResourceAllocations;
69 import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
70 import org.onosproject.net.resource.link.LambdaResource;
71 import org.onosproject.net.resource.link.LambdaResourceAllocation;
72 import org.onosproject.net.resource.link.LinkResourceRequest;
73 import org.onosproject.net.resource.ResourceAllocation;
74 import org.onosproject.net.intent.constraint.AnnotationConstraint;
75 import org.onosproject.net.intent.constraint.BandwidthConstraint;
76 import org.onosproject.net.intent.constraint.LambdaConstraint;
77 import org.onosproject.net.intent.constraint.LatencyConstraint;
78 import org.onosproject.net.intent.constraint.LinkTypeConstraint;
79 import org.onosproject.net.intent.constraint.ObstacleConstraint;
80 import org.onosproject.net.intent.constraint.WaypointConstraint;
81 import org.onlab.packet.ChassisId;
82 import org.onlab.packet.IpAddress;
83 import org.onlab.packet.Ip4Address;
84 import org.onlab.packet.Ip6Address;
85 import org.onlab.packet.IpPrefix;
86 import org.onlab.packet.Ip4Prefix;
87 import org.onlab.packet.Ip6Prefix;
88 import org.onlab.packet.MacAddress;
89 import org.onlab.util.KryoNamespace;
91 import java.nio.ByteBuffer;
92 import java.util.Arrays;
93 import java.util.Collections;
94 import java.util.HashMap;
97 import java.time.Duration;
99 import static java.util.Arrays.asList;
100 import static org.junit.Assert.*;
101 import static org.onosproject.net.DeviceId.deviceId;
102 import static org.onosproject.net.PortNumber.portNumber;
104 public class KryoSerializerTest {
106 private static final ProviderId PID = new ProviderId("of", "foo");
107 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
108 private static final DeviceId DID1 = deviceId("of:foo");
109 private static final DeviceId DID2 = deviceId("of:bar");
110 private static final PortNumber P1 = portNumber(1);
111 private static final PortNumber P2 = portNumber(2);
112 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
113 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
114 private static final String MFR = "whitebox";
115 private static final String HW = "1.1.x";
116 private static final String SW1 = "3.8.1";
117 private static final String SW2 = "3.9.5";
118 private static final String SN = "43311-12345";
119 private static final ChassisId CID = new ChassisId();
120 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
122 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
126 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
130 private static final OchSignal OCH_SIGNAL1 = (OchSignal) org.onosproject.net.Lambda.ochSignal(
131 GridType.DWDM, ChannelSpacing.CHL_100GHZ, -8, 4);
132 private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
134 private KryoSerializer serializer;
137 public static void setUpBeforeClass() throws Exception {
141 public void setUp() throws Exception {
142 serializer = new KryoSerializer() {
145 protected void setupKryoPool() {
146 serializerPool = KryoNamespace.newBuilder()
147 .register(KryoNamespaces.API)
148 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
155 public void tearDown() throws Exception {
158 private <T> void testSerializedEquals(T original) {
159 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
160 serializer.encode(original, buffer);
162 T copy = serializer.decode(buffer);
164 T copy2 = serializer.decode(serializer.encode(original));
167 .addEqualityGroup(original, copy, copy2)
171 private <T> void testSerializable(T original) {
172 byte[] bs = serializer.encode(original);
173 T copy = serializer.decode(bs);
179 public void testConnectPoint() {
180 testSerializedEquals(new ConnectPoint(DID1, P1));
184 public void testDefaultLink() {
185 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
186 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
190 public void testDefaultPort() {
191 testSerializedEquals(new DefaultPort(DEV1, P1, true));
192 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
196 public void testOmsPort() {
197 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
198 Frequency.ofGHz(100)));
199 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
200 Frequency.ofGHz(100), A1_2));
204 public void testOchPort() {
205 testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1));
206 testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1, A1_2));
210 public void testOduCltPort() {
211 testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE));
212 testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE, A1_2));
216 public void testDeviceId() {
217 testSerializedEquals(DID1);
221 public void testImmutableMap() {
222 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
223 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
224 testSerializedEquals(ImmutableMap.of());
228 public void testImmutableSet() {
229 testSerializedEquals(ImmutableSet.of(DID1, DID2));
230 testSerializedEquals(ImmutableSet.of(DID1));
231 testSerializedEquals(ImmutableSet.of());
235 public void testImmutableList() {
236 testSerializedEquals(ImmutableList.of(DID1, DID2));
237 testSerializedEquals(ImmutableList.of(DID1));
238 testSerializedEquals(ImmutableList.of());
242 public void testFlowRuleBatchEntry() {
243 final FlowRule rule1 =
244 DefaultFlowRule.builder()
246 .withSelector(DefaultTrafficSelector.emptySelector())
247 .withTreatment(DefaultTrafficTreatment.emptyTreatment())
249 .fromApp(new DefaultApplicationId(1, "1"))
253 final FlowRuleBatchEntry entry1 =
254 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
255 final FlowRuleBatchEntry entry2 =
256 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
258 testSerializedEquals(entry1);
259 testSerializedEquals(entry2);
263 public void testIpPrefix() {
264 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
268 public void testIp4Prefix() {
269 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
273 public void testIp6Prefix() {
274 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
278 public void testIpAddress() {
279 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
283 public void testIp4Address() {
284 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
288 public void testIp6Address() {
289 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
293 public void testMacAddress() {
294 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
298 public void testLinkKey() {
299 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
303 public void testNodeId() {
304 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
308 public void testPortNumber() {
309 testSerializedEquals(P1);
313 public void testProviderId() {
314 testSerializedEquals(PID);
315 testSerializedEquals(PIDA);
319 public void testMastershipTerm() {
320 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
321 testSerializedEquals(MastershipTerm.of(null, 0));
325 public void testHostLocation() {
326 testSerializedEquals(new HostLocation(CP1, 1234L));
330 public void testFlowId() {
331 testSerializedEquals(FlowId.valueOf(0x12345678L));
335 public void testRoleInfo() {
336 testSerializedEquals(new RoleInfo(new NodeId("master"),
337 asList(new NodeId("stby1"), new NodeId("stby2"))));
341 public void testIndexedLambda() {
342 testSerializedEquals(org.onosproject.net.Lambda.indexedLambda(10L));
346 public void testOchSignal() {
347 testSerializedEquals(org.onosproject.net.Lambda.ochSignal(
348 GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1
353 public void testDefaultLinkResourceRequest() {
354 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
356 .addBandwidthRequest(32.195)
362 public void testDefaultLinkResourceAllocations() {
363 LinkResourceRequest request = DefaultLinkResourceRequest
364 .builder(IntentId.valueOf(2501), ImmutableList.of())
366 .addBandwidthRequest(32.195)
368 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
369 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
370 ImmutableSet.of(new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(10.0))),
371 new LambdaResourceAllocation(LambdaResource.valueOf(1))));
372 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
376 public void testResourcePath() {
377 testSerializedEquals(ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1));
381 public void testResourceAllocation() {
382 testSerializedEquals(new org.onosproject.net.newresource.ResourceAllocation(
383 ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1),
384 IntentId.valueOf(30)));
388 public void testFrequency() {
389 testSerializedEquals(Frequency.ofGHz(100));
393 public void testBandwidth() {
394 testSerializedEquals(Bandwidth.mbps(1000.0));
398 public void testLambdaConstraint() {
399 testSerializable(new LambdaConstraint(new IndexedLambda(1)));
403 public void testBandwidthConstraint() {
404 testSerializable(new BandwidthConstraint(Bandwidth.bps(1000.0)));
408 public void testLinkTypeConstraint() {
409 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
413 public void testLatencyConstraint() {
414 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
418 public void testWaypointConstraint() {
419 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
423 public void testObstacleConstraint() {
424 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
428 public void testArraysAsList() {
429 testSerializedEquals(Arrays.asList(1, 2, 3));
433 public void testAnnotationConstraint() {
434 testSerializable(new AnnotationConstraint("distance", 100.0));
438 public void testDefaultGroupId() {
439 testSerializedEquals(new DefaultGroupId(99));
443 public void testEmptySet() {
444 testSerializedEquals(Collections.emptySet());
448 public void testAnnotations() {
449 // Annotations does not have equals defined, manually test equality
450 final byte[] a1Bytes = serializer.encode(A1);
451 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
452 assertAnnotationsEquals(copiedA1, A1);
454 final byte[] a12Bytes = serializer.encode(A1_2);
455 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
456 assertAnnotationsEquals(copiedA12, A1_2);
460 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
461 SparseAnnotations expected = DefaultAnnotations.builder().build();
462 for (SparseAnnotations a : annotations) {
463 expected = DefaultAnnotations.union(expected, a);
465 assertEquals(expected.keys(), actual.keys());
466 for (String key : expected.keys()) {
467 assertEquals(expected.value(key), actual.value(key));