1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/*
* Copyright 2014-2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.serializers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.testing.EqualsTester;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onlab.packet.VlanId;
import org.onlab.util.Bandwidth;
import org.onlab.util.Frequency;
import org.onosproject.cluster.NodeId;
import org.onosproject.cluster.RoleInfo;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.core.DefaultGroupId;
import org.onosproject.mastership.MastershipTerm;
import org.onosproject.net.Annotations;
import org.onosproject.net.ChannelSpacing;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.DefaultDevice;
import org.onosproject.net.DefaultLink;
import org.onosproject.net.DefaultPort;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.GridType;
import org.onosproject.net.HostLocation;
import org.onosproject.net.IndexedLambda;
import org.onosproject.net.Link;
import org.onosproject.net.Link.Type;
import org.onosproject.net.LinkKey;
import org.onosproject.net.OchPort;
import org.onosproject.net.OchSignal;
import org.onosproject.net.OduCltPort;
import org.onosproject.net.OmsPort;
import org.onosproject.net.PortNumber;
import org.onosproject.net.OduSignalType;
import org.onosproject.net.SparseAnnotations;
import org.onosproject.net.flow.DefaultFlowRule;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.FlowId;
import org.onosproject.net.flow.FlowRule;
import org.onosproject.net.flow.FlowRuleBatchEntry;
import org.onosproject.net.intent.IntentId;
import org.onosproject.net.newresource.ResourcePath;
import org.onosproject.net.provider.ProviderId;
import org.onosproject.net.resource.link.BandwidthResource;
import org.onosproject.net.resource.link.BandwidthResourceAllocation;
import org.onosproject.net.resource.link.DefaultLinkResourceAllocations;
import org.onosproject.net.resource.link.DefaultLinkResourceRequest;
import org.onosproject.net.resource.link.LambdaResource;
import org.onosproject.net.resource.link.LambdaResourceAllocation;
import org.onosproject.net.resource.link.LinkResourceRequest;
import org.onosproject.net.resource.ResourceAllocation;
import org.onosproject.net.intent.constraint.AnnotationConstraint;
import org.onosproject.net.intent.constraint.BandwidthConstraint;
import org.onosproject.net.intent.constraint.LambdaConstraint;
import org.onosproject.net.intent.constraint.LatencyConstraint;
import org.onosproject.net.intent.constraint.LinkTypeConstraint;
import org.onosproject.net.intent.constraint.ObstacleConstraint;
import org.onosproject.net.intent.constraint.WaypointConstraint;
import org.onlab.packet.ChassisId;
import org.onlab.packet.IpAddress;
import org.onlab.packet.Ip4Address;
import org.onlab.packet.Ip6Address;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.Ip4Prefix;
import org.onlab.packet.Ip6Prefix;
import org.onlab.packet.MacAddress;
import org.onlab.util.KryoNamespace;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.time.Duration;
import static java.util.Arrays.asList;
import static org.junit.Assert.*;
import static org.onosproject.net.DeviceId.deviceId;
import static org.onosproject.net.PortNumber.portNumber;
public class KryoSerializerTest {
private static final ProviderId PID = new ProviderId("of", "foo");
private static final ProviderId PIDA = new ProviderId("of", "foo", true);
private static final DeviceId DID1 = deviceId("of:foo");
private static final DeviceId DID2 = deviceId("of:bar");
private static final PortNumber P1 = portNumber(1);
private static final PortNumber P2 = portNumber(2);
private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
private static final String MFR = "whitebox";
private static final String HW = "1.1.x";
private static final String SW1 = "3.8.1";
private static final String SW2 = "3.9.5";
private static final String SN = "43311-12345";
private static final ChassisId CID = new ChassisId();
private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
SW1, SN, CID);
private static final SparseAnnotations A1 = DefaultAnnotations.builder()
.set("A1", "a1")
.set("B1", "b1")
.build();
private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
.remove("A1")
.set("B3", "b3")
.build();
private static final OchSignal OCH_SIGNAL1 = (OchSignal) org.onosproject.net.Lambda.ochSignal(
GridType.DWDM, ChannelSpacing.CHL_100GHZ, -8, 4);
private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
private KryoSerializer serializer;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Before
public void setUp() throws Exception {
serializer = new KryoSerializer() {
@Override
protected void setupKryoPool() {
serializerPool = KryoNamespace.newBuilder()
.register(KryoNamespaces.API)
.nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
.build();
}
};
}
@After
public void tearDown() throws Exception {
}
private <T> void testSerializedEquals(T original) {
ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
serializer.encode(original, buffer);
buffer.flip();
T copy = serializer.decode(buffer);
T copy2 = serializer.decode(serializer.encode(original));
new EqualsTester()
.addEqualityGroup(original, copy, copy2)
.testEquals();
}
private <T> void testSerializable(T original) {
byte[] bs = serializer.encode(original);
T copy = serializer.decode(bs);
assertNotNull(copy);
}
@Test
public void testConnectPoint() {
testSerializedEquals(new ConnectPoint(DID1, P1));
}
@Test
public void testDefaultLink() {
testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
}
@Test
public void testDefaultPort() {
testSerializedEquals(new DefaultPort(DEV1, P1, true));
testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
}
@Test
public void testOmsPort() {
testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
Frequency.ofGHz(100)));
testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
Frequency.ofGHz(100), A1_2));
}
@Test
public void testOchPort() {
testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1));
testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1, A1_2));
}
@Test
public void testOduCltPort() {
testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE));
testSerializedEquals(new OduCltPort(DEV1, P1, true, OduCltPort.SignalType.CLT_10GBE, A1_2));
}
@Test
public void testDeviceId() {
testSerializedEquals(DID1);
}
@Test
public void testImmutableMap() {
testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
testSerializedEquals(ImmutableMap.of(DID1, DEV1));
testSerializedEquals(ImmutableMap.of());
}
@Test
public void testImmutableSet() {
testSerializedEquals(ImmutableSet.of(DID1, DID2));
testSerializedEquals(ImmutableSet.of(DID1));
testSerializedEquals(ImmutableSet.of());
}
@Test
public void testImmutableList() {
testSerializedEquals(ImmutableList.of(DID1, DID2));
testSerializedEquals(ImmutableList.of(DID1));
testSerializedEquals(ImmutableList.of());
}
@Test
public void testFlowRuleBatchEntry() {
final FlowRule rule1 =
DefaultFlowRule.builder()
.forDevice(DID1)
.withSelector(DefaultTrafficSelector.emptySelector())
.withTreatment(DefaultTrafficTreatment.emptyTreatment())
.withPriority(0)
.fromApp(new DefaultApplicationId(1, "1"))
.makeTemporary(1)
.build();
final FlowRuleBatchEntry entry1 =
new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
final FlowRuleBatchEntry entry2 =
new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
testSerializedEquals(entry1);
testSerializedEquals(entry2);
}
@Test
public void testIpPrefix() {
testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
}
@Test
public void testIp4Prefix() {
testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
}
@Test
public void testIp6Prefix() {
testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
}
@Test
public void testIpAddress() {
testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
}
@Test
public void testIp4Address() {
testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
}
@Test
public void testIp6Address() {
testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
}
@Test
public void testMacAddress() {
testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
}
@Test
public void testLinkKey() {
testSerializedEquals(LinkKey.linkKey(CP1, CP2));
}
@Test
public void testNodeId() {
testSerializedEquals(new NodeId("SomeNodeIdentifier"));
}
@Test
public void testPortNumber() {
testSerializedEquals(P1);
}
@Test
public void testProviderId() {
testSerializedEquals(PID);
testSerializedEquals(PIDA);
}
@Test
public void testMastershipTerm() {
testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
testSerializedEquals(MastershipTerm.of(null, 0));
}
@Test
public void testHostLocation() {
testSerializedEquals(new HostLocation(CP1, 1234L));
}
@Test
public void testFlowId() {
testSerializedEquals(FlowId.valueOf(0x12345678L));
}
@Test
public void testRoleInfo() {
testSerializedEquals(new RoleInfo(new NodeId("master"),
asList(new NodeId("stby1"), new NodeId("stby2"))));
}
@Test
public void testIndexedLambda() {
testSerializedEquals(org.onosproject.net.Lambda.indexedLambda(10L));
}
@Test
public void testOchSignal() {
testSerializedEquals(org.onosproject.net.Lambda.ochSignal(
GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1
));
}
@Test
public void testDefaultLinkResourceRequest() {
testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
.addLambdaRequest()
.addBandwidthRequest(32.195)
.build()
);
}
@Test
public void testDefaultLinkResourceAllocations() {
LinkResourceRequest request = DefaultLinkResourceRequest
.builder(IntentId.valueOf(2501), ImmutableList.of())
.addLambdaRequest()
.addBandwidthRequest(32.195)
.build();
Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
ImmutableSet.of(new BandwidthResourceAllocation(new BandwidthResource(Bandwidth.bps(10.0))),
new LambdaResourceAllocation(LambdaResource.valueOf(1))));
testSerializable(new DefaultLinkResourceAllocations(request, allocations));
}
@Test
public void testResourcePath() {
testSerializedEquals(ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1));
}
@Test
public void testResourceAllocation() {
testSerializedEquals(new org.onosproject.net.newresource.ResourceAllocation(
ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1),
IntentId.valueOf(30)));
}
@Test
public void testFrequency() {
testSerializedEquals(Frequency.ofGHz(100));
}
@Test
public void testBandwidth() {
testSerializedEquals(Bandwidth.mbps(1000.0));
}
@Test
public void testLambdaConstraint() {
testSerializable(new LambdaConstraint(new IndexedLambda(1)));
}
@Test
public void testBandwidthConstraint() {
testSerializable(new BandwidthConstraint(Bandwidth.bps(1000.0)));
}
@Test
public void testLinkTypeConstraint() {
testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
}
@Test
public void testLatencyConstraint() {
testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
}
@Test
public void testWaypointConstraint() {
testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
}
@Test
public void testObstacleConstraint() {
testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
}
@Test
public void testArraysAsList() {
testSerializedEquals(Arrays.asList(1, 2, 3));
}
@Test
public void testAnnotationConstraint() {
testSerializable(new AnnotationConstraint("distance", 100.0));
}
@Test
public void testDefaultGroupId() {
testSerializedEquals(new DefaultGroupId(99));
}
@Test
public void testEmptySet() {
testSerializedEquals(Collections.emptySet());
}
@Test
public void testAnnotations() {
// Annotations does not have equals defined, manually test equality
final byte[] a1Bytes = serializer.encode(A1);
SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
assertAnnotationsEquals(copiedA1, A1);
final byte[] a12Bytes = serializer.encode(A1_2);
SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
assertAnnotationsEquals(copiedA12, A1_2);
}
// code clone
protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
SparseAnnotations expected = DefaultAnnotations.builder().build();
for (SparseAnnotations a : annotations) {
expected = DefaultAnnotations.union(expected, a);
}
assertEquals(expected.keys(), actual.keys());
for (String key : expected.keys()) {
assertEquals(expected.value(key), actual.value(key));
}
}
}
|