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.Link;
47 import org.onosproject.net.Link.Type;
48 import org.onosproject.net.LinkKey;
49 import org.onosproject.net.OchPort;
50 import org.onosproject.net.OchSignal;
51 import org.onosproject.net.OduCltPort;
52 import org.onosproject.net.OmsPort;
53 import org.onosproject.net.PortNumber;
54 import org.onosproject.net.OduSignalType;
55 import org.onosproject.net.SparseAnnotations;
56 import org.onosproject.net.flow.DefaultFlowRule;
57 import org.onosproject.net.flow.DefaultTrafficSelector;
58 import org.onosproject.net.flow.DefaultTrafficTreatment;
59 import org.onosproject.net.flow.FlowId;
60 import org.onosproject.net.flow.FlowRule;
61 import org.onosproject.net.flow.FlowRuleBatchEntry;
62 import org.onosproject.net.intent.IntentId;
63 import org.onosproject.net.newresource.ResourcePath;
64 import org.onosproject.net.provider.ProviderId;
65 import org.onosproject.net.resource.link.BandwidthResource;
66 import org.onosproject.net.resource.link.BandwidthResourceAllocation;
67 import org.onosproject.net.resource.link.DefaultLinkResourceAllocations;
68 import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
69 import org.onosproject.net.resource.link.LambdaResource;
70 import org.onosproject.net.resource.link.LambdaResourceAllocation;
71 import org.onosproject.net.resource.link.LinkResourceRequest;
72 import org.onosproject.net.resource.ResourceAllocation;
73 import org.onosproject.net.intent.constraint.AnnotationConstraint;
74 import org.onosproject.net.intent.constraint.BandwidthConstraint;
75 import org.onosproject.net.intent.constraint.LambdaConstraint;
76 import org.onosproject.net.intent.constraint.LatencyConstraint;
77 import org.onosproject.net.intent.constraint.LinkTypeConstraint;
78 import org.onosproject.net.intent.constraint.ObstacleConstraint;
79 import org.onosproject.net.intent.constraint.WaypointConstraint;
80 import org.onlab.packet.ChassisId;
81 import org.onlab.packet.IpAddress;
82 import org.onlab.packet.Ip4Address;
83 import org.onlab.packet.Ip6Address;
84 import org.onlab.packet.IpPrefix;
85 import org.onlab.packet.Ip4Prefix;
86 import org.onlab.packet.Ip6Prefix;
87 import org.onlab.packet.MacAddress;
88 import org.onlab.util.KryoNamespace;
90 import java.nio.ByteBuffer;
91 import java.util.Arrays;
92 import java.util.Collections;
93 import java.util.HashMap;
96 import java.time.Duration;
98 import static java.util.Arrays.asList;
99 import static org.junit.Assert.*;
100 import static org.onosproject.net.DeviceId.deviceId;
101 import static org.onosproject.net.PortNumber.portNumber;
103 public class KryoSerializerTest {
105 private static final ProviderId PID = new ProviderId("of", "foo");
106 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
107 private static final DeviceId DID1 = deviceId("of:foo");
108 private static final DeviceId DID2 = deviceId("of:bar");
109 private static final PortNumber P1 = portNumber(1);
110 private static final PortNumber P2 = portNumber(2);
111 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
112 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
113 private static final String MFR = "whitebox";
114 private static final String HW = "1.1.x";
115 private static final String SW1 = "3.8.1";
116 private static final String SW2 = "3.9.5";
117 private static final String SN = "43311-12345";
118 private static final ChassisId CID = new ChassisId();
119 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
121 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
125 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
129 private static final OchSignal OCH_SIGNAL1 = (OchSignal) org.onosproject.net.Lambda.ochSignal(
130 GridType.DWDM, ChannelSpacing.CHL_100GHZ, -8, 4);
131 private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
133 private KryoSerializer serializer;
136 public static void setUpBeforeClass() throws Exception {
140 public void setUp() throws Exception {
141 serializer = new KryoSerializer() {
144 protected void setupKryoPool() {
145 serializerPool = KryoNamespace.newBuilder()
146 .register(KryoNamespaces.API)
147 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
154 public void tearDown() throws Exception {
157 private <T> void testSerializedEquals(T original) {
158 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
159 serializer.encode(original, buffer);
161 T copy = serializer.decode(buffer);
163 T copy2 = serializer.decode(serializer.encode(original));
166 .addEqualityGroup(original, copy, copy2)
170 private <T> void testSerializable(T original) {
171 byte[] bs = serializer.encode(original);
172 T copy = serializer.decode(bs);
178 public void testConnectPoint() {
179 testSerializedEquals(new ConnectPoint(DID1, P1));
183 public void testDefaultLink() {
184 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
185 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
189 public void testDefaultPort() {
190 testSerializedEquals(new DefaultPort(DEV1, P1, true));
191 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
195 public void testOmsPort() {
196 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
197 Frequency.ofGHz(100)));
198 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
199 Frequency.ofGHz(100), A1_2));
203 public void testOchPort() {
204 testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1));
205 testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1, A1_2));
209 public void testOduCltPort() {
210 testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE));
211 testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE, A1_2));
215 public void testDeviceId() {
216 testSerializedEquals(DID1);
220 public void testImmutableMap() {
221 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
222 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
223 testSerializedEquals(ImmutableMap.of());
227 public void testImmutableSet() {
228 testSerializedEquals(ImmutableSet.of(DID1, DID2));
229 testSerializedEquals(ImmutableSet.of(DID1));
230 testSerializedEquals(ImmutableSet.of());
234 public void testImmutableList() {
235 testSerializedEquals(ImmutableList.of(DID1, DID2));
236 testSerializedEquals(ImmutableList.of(DID1));
237 testSerializedEquals(ImmutableList.of());
241 public void testFlowRuleBatchEntry() {
242 final FlowRule rule1 =
243 DefaultFlowRule.builder()
245 .withSelector(DefaultTrafficSelector.emptySelector())
246 .withTreatment(DefaultTrafficTreatment.emptyTreatment())
248 .fromApp(new DefaultApplicationId(1, "1"))
252 final FlowRuleBatchEntry entry1 =
253 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
254 final FlowRuleBatchEntry entry2 =
255 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
257 testSerializedEquals(entry1);
258 testSerializedEquals(entry2);
262 public void testIpPrefix() {
263 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
267 public void testIp4Prefix() {
268 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
272 public void testIp6Prefix() {
273 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
277 public void testIpAddress() {
278 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
282 public void testIp4Address() {
283 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
287 public void testIp6Address() {
288 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
292 public void testMacAddress() {
293 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
297 public void testLinkKey() {
298 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
302 public void testNodeId() {
303 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
307 public void testPortNumber() {
308 testSerializedEquals(P1);
312 public void testProviderId() {
313 testSerializedEquals(PID);
314 testSerializedEquals(PIDA);
318 public void testMastershipTerm() {
319 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
320 testSerializedEquals(MastershipTerm.of(null, 0));
324 public void testHostLocation() {
325 testSerializedEquals(new HostLocation(CP1, 1234L));
329 public void testFlowId() {
330 testSerializedEquals(FlowId.valueOf(0x12345678L));
334 public void testRoleInfo() {
335 testSerializedEquals(new RoleInfo(new NodeId("master"),
336 asList(new NodeId("stby1"), new NodeId("stby2"))));
340 public void testIndexedLambda() {
341 testSerializedEquals(org.onosproject.net.Lambda.indexedLambda(10L));
345 public void testOchSignal() {
346 testSerializedEquals(org.onosproject.net.Lambda.ochSignal(
347 GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1
352 public void testDefaultLinkResourceRequest() {
353 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
355 .addBandwidthRequest(32.195)
361 public void testDefaultLinkResourceAllocations() {
362 LinkResourceRequest request = DefaultLinkResourceRequest
363 .builder(IntentId.valueOf(2501), ImmutableList.of())
365 .addBandwidthRequest(32.195)
367 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
368 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
369 ImmutableSet.of(new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(10.0))),
370 new LambdaResourceAllocation(LambdaResource.valueOf(1))));
371 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
375 public void testResourcePath() {
376 testSerializedEquals(ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1));
380 public void testResourceAllocation() {
381 testSerializedEquals(new org.onosproject.net.newresource.ResourceAllocation(
382 ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1),
383 IntentId.valueOf(30)));
387 public void testFrequency() {
388 testSerializedEquals(Frequency.ofGHz(100));
392 public void testBandwidth() {
393 testSerializedEquals(Bandwidth.mbps(1000.0));
397 public void testLambdaConstraint() {
398 testSerializable(new LambdaConstraint(LambdaResource.valueOf(1)));
402 public void testBandwidthConstraint() {
403 testSerializable(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1000.0))));
407 public void testLinkTypeConstraint() {
408 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
412 public void testLatencyConstraint() {
413 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
417 public void testWaypointConstraint() {
418 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
422 public void testObstacleConstraint() {
423 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
427 public void testArraysAsList() {
428 testSerializedEquals(Arrays.asList(1, 2, 3));
432 public void testAnnotationConstraint() {
433 testSerializable(new AnnotationConstraint("distance", 100.0));
437 public void testDefaultGroupId() {
438 testSerializedEquals(new DefaultGroupId(99));
442 public void testEmptySet() {
443 testSerializedEquals(Collections.emptySet());
447 public void testAnnotations() {
448 // Annotations does not have equals defined, manually test equality
449 final byte[] a1Bytes = serializer.encode(A1);
450 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
451 assertAnnotationsEquals(copiedA1, A1);
453 final byte[] a12Bytes = serializer.encode(A1_2);
454 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
455 assertAnnotationsEquals(copiedA12, A1_2);
459 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
460 SparseAnnotations expected = DefaultAnnotations.builder().build();
461 for (SparseAnnotations a : annotations) {
462 expected = DefaultAnnotations.union(expected, a);
464 assertEquals(expected.keys(), actual.keys());
465 for (String key : expected.keys()) {
466 assertEquals(expected.value(key), actual.value(key));