blob: 0d81f69faca3b3936e646f3e00e1903553ce7d47 [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 */
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070016package org.onlab.onos.store.serializers;
17
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;
27import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070028import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHIf6171232014-11-25 15:14:44 -080029import org.onlab.onos.core.DefaultGroupId;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070030import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070031import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070032import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070033import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070034import org.onlab.onos.net.DefaultDevice;
35import org.onlab.onos.net.DefaultLink;
36import org.onlab.onos.net.DefaultPort;
37import org.onlab.onos.net.Device;
38import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070039import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070040import org.onlab.onos.net.Link;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080041import org.onlab.onos.net.Link.Type;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070042import org.onlab.onos.net.LinkKey;
43import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070044import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -070045import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080046import org.onlab.onos.net.intent.IntentId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070047import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080048import org.onlab.onos.net.resource.Bandwidth;
49import org.onlab.onos.net.resource.BandwidthResourceAllocation;
50import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
51import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
52import org.onlab.onos.net.resource.Lambda;
53import org.onlab.onos.net.resource.LambdaResourceAllocation;
54import org.onlab.onos.net.resource.LinkResourceRequest;
55import org.onlab.onos.net.resource.ResourceAllocation;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080056import org.onlab.onos.net.intent.constraint.AnnotationConstraint;
57import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
58import org.onlab.onos.net.intent.constraint.LambdaConstraint;
59import org.onlab.onos.net.intent.constraint.LatencyConstraint;
60import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
61import org.onlab.onos.net.intent.constraint.ObstacleConstraint;
62import org.onlab.onos.net.intent.constraint.WaypointConstraint;
alshabib7911a052014-10-16 17:49:37 -070063import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070064import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080065import org.onlab.packet.Ip4Address;
66import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070067import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080068import org.onlab.packet.Ip4Prefix;
69import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070070import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070071import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070072
Thomas Vachuskad16ce182014-10-29 17:25:29 -070073import java.nio.ByteBuffer;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080074import java.util.Arrays;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080075import java.util.HashMap;
76import java.util.Map;
77import java.util.Set;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080078import java.time.Duration;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070079
80import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080081import static org.junit.Assert.*;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070082import static org.onlab.onos.net.DeviceId.deviceId;
83import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070084
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070085public class KryoSerializerTest {
86
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070087 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070088 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070089 private static final DeviceId DID1 = deviceId("of:foo");
90 private static final DeviceId DID2 = deviceId("of:bar");
91 private static final PortNumber P1 = portNumber(1);
92 private static final PortNumber P2 = portNumber(2);
93 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
94 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
95 private static final String MFR = "whitebox";
96 private static final String HW = "1.1.x";
97 private static final String SW1 = "3.8.1";
98 private static final String SW2 = "3.9.5";
99 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -0700100 private static final ChassisId CID = new ChassisId();
101 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
102 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700103 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
104 .set("A1", "a1")
105 .set("B1", "b1")
106 .build();
107 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
108 .remove("A1")
109 .set("B3", "b3")
110 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700111
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700112 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700113
114 @BeforeClass
115 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700116 }
117
118 @Before
119 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700120 serializer = new KryoSerializer() {
121
122 @Override
123 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700124 serializerPool = KryoNamespace.newBuilder()
125 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800126 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
127 .build();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700128 }
129 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700130 }
131
132 @After
133 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700134 }
135
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800136 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700137 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700138 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700139 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700140 T copy = serializer.decode(buffer);
141
142 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700143
144 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700145 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700146 .testEquals();
147 }
148
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800149 private <T> void testSerializable(T original) {
150 byte[] bs = serializer.encode(original);
151 T copy = serializer.decode(bs);
152 assertNotNull(copy);
153 }
154
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700155
156 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700157 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800158 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700159 }
160
161 @Test
162 public void testDefaultLink() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800163 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
164 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700165 }
166
167 @Test
168 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800169 testSerializedEquals(new DefaultPort(DEV1, P1, true));
170 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700171 }
172
173 @Test
174 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800175 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700176 }
177
178 @Test
179 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800180 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
181 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
182 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700183 }
184
185 @Test
186 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800187 testSerializedEquals(ImmutableSet.of(DID1, DID2));
188 testSerializedEquals(ImmutableSet.of(DID1));
189 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700190 }
191
192 @Test
193 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800194 testSerializedEquals(ImmutableList.of(DID1, DID2));
195 testSerializedEquals(ImmutableList.of(DID1));
196 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700197 }
198
199 @Test
200 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800201 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700202 }
203
204 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800205 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800206 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800207 }
208
209 @Test
210 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800211 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800212 }
213
214 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700215 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800216 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700217 }
218
219 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800220 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800221 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800222 }
223
224 @Test
225 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800226 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800227 }
228
229 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700230 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800231 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700232 }
233
234 @Test
235 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800236 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700237 }
238
239 @Test
240 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800241 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700242 }
243
244 @Test
245 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800246 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700247 }
248
249 @Test
250 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800251 testSerializedEquals(PID);
252 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700253 }
254
255 @Test
256 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800257 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
258 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700259 }
260
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700261 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700262 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800263 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700264 }
265
266 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700267 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800268 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700269 }
270
271 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700272 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800273 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700274 asList(new NodeId("stby1"), new NodeId("stby2"))));
275 }
276
277 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800278 public void testDefaultLinkResourceRequest() {
279 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
280 .addLambdaRequest()
281 .addBandwidthRequest(32.195)
282 .build()
283 );
284 }
285
286 @Test
287 public void testDefaultLinkResourceAllocations() {
288 LinkResourceRequest request = DefaultLinkResourceRequest
289 .builder(IntentId.valueOf(2501), ImmutableList.of())
290 .addLambdaRequest()
291 .addBandwidthRequest(32.195)
292 .build();
293 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
294 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
295 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
296 new LambdaResourceAllocation(Lambda.valueOf(1))));
297 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
298 }
299
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800300 @Test
301 public void testLambdaConstraint() {
302 testSerializable(new LambdaConstraint(Lambda.valueOf(1)));
303 }
304
305 @Test
306 public void testBandwidthConstraint() {
307 testSerializable(new BandwidthConstraint(Bandwidth.valueOf(1000.0)));
308 }
309
310 @Test
311 public void testLinkTypeConstraint() {
312 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
313 }
314
315 @Test
316 public void testLatencyConstraint() {
317 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
318 }
319
320 @Test
321 public void testWaypointConstraint() {
322 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
323 }
324
325 @Test
326 public void testObstacleConstraint() {
327 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
328 }
329
330 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800331 public void testArraysAsList() {
332 testSerializedEquals(Arrays.asList(1, 2, 3));
333 }
334
335 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800336 public void testAnnotationConstraint() {
337 testSerializable(new AnnotationConstraint("distance", 100.0));
338 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800339
340 @Test
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800341 public void testDefaultGroupId() {
342 testSerializedEquals(new DefaultGroupId(99));
343 }
344
345
346 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700347 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700348 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700349 final byte[] a1Bytes = serializer.encode(A1);
350 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700351 assertAnnotationsEquals(copiedA1, A1);
352
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700353 final byte[] a12Bytes = serializer.encode(A1_2);
354 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700355 assertAnnotationsEquals(copiedA12, A1_2);
356 }
357
358 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700359 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700360 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700361 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700362 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700363 }
364 assertEquals(expected.keys(), actual.keys());
365 for (String key : expected.keys()) {
366 assertEquals(expected.value(key), actual.value(key));
367 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700368 }
369
370}