11a62d4e199f4806449240df42893eb298926ed0
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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  */
16 package org.onosproject.store.serializers;
17
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;
22
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;
89
90 import java.nio.ByteBuffer;
91 import java.util.Arrays;
92 import java.util.Collections;
93 import java.util.HashMap;
94 import java.util.Map;
95 import java.util.Set;
96 import java.time.Duration;
97
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;
102
103 public class KryoSerializerTest {
104
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,
120                                                          SW1, SN, CID);
121     private static final SparseAnnotations A1 = DefaultAnnotations.builder()
122             .set("A1", "a1")
123             .set("B1", "b1")
124             .build();
125     private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
126             .remove("A1")
127             .set("B3", "b3")
128             .build();
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);
132
133     private KryoSerializer serializer;
134
135     @BeforeClass
136     public static void setUpBeforeClass() throws Exception {
137     }
138
139     @Before
140     public void setUp() throws Exception {
141         serializer = new KryoSerializer() {
142
143             @Override
144             protected void setupKryoPool() {
145                 serializerPool = KryoNamespace.newBuilder()
146                         .register(KryoNamespaces.API)
147                         .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
148                         .build();
149             }
150         };
151     }
152
153     @After
154     public void tearDown() throws Exception {
155     }
156
157     private <T> void testSerializedEquals(T original) {
158         ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
159         serializer.encode(original, buffer);
160         buffer.flip();
161         T copy = serializer.decode(buffer);
162
163         T copy2 = serializer.decode(serializer.encode(original));
164
165         new EqualsTester()
166             .addEqualityGroup(original, copy, copy2)
167             .testEquals();
168     }
169
170     private <T> void testSerializable(T original) {
171         byte[] bs = serializer.encode(original);
172         T copy = serializer.decode(bs);
173         assertNotNull(copy);
174     }
175
176
177     @Test
178     public void testConnectPoint() {
179         testSerializedEquals(new ConnectPoint(DID1, P1));
180     }
181
182     @Test
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));
186     }
187
188     @Test
189     public void testDefaultPort() {
190         testSerializedEquals(new DefaultPort(DEV1, P1, true));
191         testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
192     }
193
194     @Test
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));
200     }
201
202     @Test
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));
206     }
207
208     @Test
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));
212     }
213
214     @Test
215     public void testDeviceId() {
216         testSerializedEquals(DID1);
217     }
218
219     @Test
220     public void testImmutableMap() {
221         testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
222         testSerializedEquals(ImmutableMap.of(DID1, DEV1));
223         testSerializedEquals(ImmutableMap.of());
224     }
225
226     @Test
227     public void testImmutableSet() {
228         testSerializedEquals(ImmutableSet.of(DID1, DID2));
229         testSerializedEquals(ImmutableSet.of(DID1));
230         testSerializedEquals(ImmutableSet.of());
231     }
232
233     @Test
234     public void testImmutableList() {
235         testSerializedEquals(ImmutableList.of(DID1, DID2));
236         testSerializedEquals(ImmutableList.of(DID1));
237         testSerializedEquals(ImmutableList.of());
238     }
239
240     @Test
241     public void testFlowRuleBatchEntry() {
242         final FlowRule rule1 =
243                 DefaultFlowRule.builder()
244                         .forDevice(DID1)
245                         .withSelector(DefaultTrafficSelector.emptySelector())
246                         .withTreatment(DefaultTrafficTreatment.emptyTreatment())
247                         .withPriority(0)
248                         .fromApp(new DefaultApplicationId(1, "1"))
249                         .makeTemporary(1)
250                         .build();
251
252         final FlowRuleBatchEntry entry1 =
253                 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
254         final FlowRuleBatchEntry entry2 =
255                 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
256
257         testSerializedEquals(entry1);
258         testSerializedEquals(entry2);
259     }
260
261     @Test
262     public void testIpPrefix() {
263         testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
264     }
265
266     @Test
267     public void testIp4Prefix() {
268         testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
269     }
270
271     @Test
272     public void testIp6Prefix() {
273         testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
274     }
275
276     @Test
277     public void testIpAddress() {
278         testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
279     }
280
281     @Test
282     public void testIp4Address() {
283         testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
284     }
285
286     @Test
287     public void testIp6Address() {
288         testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
289     }
290
291     @Test
292     public void testMacAddress() {
293         testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
294     }
295
296     @Test
297     public void testLinkKey() {
298         testSerializedEquals(LinkKey.linkKey(CP1, CP2));
299     }
300
301     @Test
302     public void testNodeId() {
303         testSerializedEquals(new NodeId("SomeNodeIdentifier"));
304     }
305
306     @Test
307     public void testPortNumber() {
308         testSerializedEquals(P1);
309     }
310
311     @Test
312     public void testProviderId() {
313         testSerializedEquals(PID);
314         testSerializedEquals(PIDA);
315     }
316
317     @Test
318     public void testMastershipTerm() {
319         testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
320         testSerializedEquals(MastershipTerm.of(null, 0));
321     }
322
323     @Test
324     public void testHostLocation() {
325         testSerializedEquals(new HostLocation(CP1, 1234L));
326     }
327
328     @Test
329     public void testFlowId() {
330         testSerializedEquals(FlowId.valueOf(0x12345678L));
331     }
332
333     @Test
334     public void testRoleInfo() {
335         testSerializedEquals(new RoleInfo(new NodeId("master"),
336                             asList(new NodeId("stby1"), new NodeId("stby2"))));
337     }
338
339     @Test
340     public void testIndexedLambda() {
341         testSerializedEquals(org.onosproject.net.Lambda.indexedLambda(10L));
342     }
343
344     @Test
345     public void testOchSignal() {
346         testSerializedEquals(org.onosproject.net.Lambda.ochSignal(
347                 GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1
348         ));
349     }
350
351     @Test
352     public void testDefaultLinkResourceRequest() {
353         testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
354                         .addLambdaRequest()
355                         .addBandwidthRequest(32.195)
356                         .build()
357         );
358     }
359
360     @Test
361     public void testDefaultLinkResourceAllocations() {
362         LinkResourceRequest request = DefaultLinkResourceRequest
363                     .builder(IntentId.valueOf(2501), ImmutableList.of())
364                         .addLambdaRequest()
365                         .addBandwidthRequest(32.195)
366                         .build();
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));
372     }
373
374     @Test
375     public void testResourcePath() {
376         testSerializedEquals(ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1));
377     }
378
379     @Test
380     public void testResourceAllocation() {
381         testSerializedEquals(new org.onosproject.net.newresource.ResourceAllocation(
382                 ResourcePath.discrete(LinkKey.linkKey(CP1, CP2), VLAN1),
383                 IntentId.valueOf(30)));
384     }
385
386     @Test
387     public void testFrequency() {
388         testSerializedEquals(Frequency.ofGHz(100));
389     }
390
391     @Test
392     public void testBandwidth() {
393         testSerializedEquals(Bandwidth.mbps(1000.0));
394     }
395
396     @Test
397     public void testLambdaConstraint() {
398         testSerializable(new LambdaConstraint(LambdaResource.valueOf(1)));
399     }
400
401     @Test
402     public void testBandwidthConstraint() {
403         testSerializable(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1000.0))));
404     }
405
406     @Test
407     public void testLinkTypeConstraint() {
408         testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
409     }
410
411     @Test
412     public void testLatencyConstraint() {
413         testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
414     }
415
416     @Test
417     public void testWaypointConstraint() {
418         testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
419     }
420
421     @Test
422     public void testObstacleConstraint() {
423         testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
424     }
425
426     @Test
427     public void testArraysAsList() {
428         testSerializedEquals(Arrays.asList(1, 2, 3));
429     }
430
431     @Test
432     public void testAnnotationConstraint() {
433         testSerializable(new AnnotationConstraint("distance", 100.0));
434     }
435
436     @Test
437     public void testDefaultGroupId() {
438         testSerializedEquals(new DefaultGroupId(99));
439     }
440
441     @Test
442     public void testEmptySet() {
443         testSerializedEquals(Collections.emptySet());
444     }
445
446     @Test
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);
452
453         final byte[] a12Bytes = serializer.encode(A1_2);
454         SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
455         assertAnnotationsEquals(copiedA12, A1_2);
456     }
457
458     // code clone
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);
463         }
464         assertEquals(expected.keys(), actual.keys());
465         for (String key : expected.keys()) {
466             assertEquals(expected.value(key), actual.value(key));
467         }
468     }
469
470 }