blob: 535b9963022a8f15bee55435c246a4cad4246f77 [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 HIGUCHI80912e62014-10-12 00:15:47 -070029import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070030import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070031import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070032import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070033import org.onlab.onos.net.DefaultDevice;
34import org.onlab.onos.net.DefaultLink;
35import org.onlab.onos.net.DefaultPort;
36import org.onlab.onos.net.Device;
37import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070038import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070039import org.onlab.onos.net.Link;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080040import org.onlab.onos.net.Link.Type;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070041import org.onlab.onos.net.LinkKey;
42import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070043import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -070044import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080045import org.onlab.onos.net.intent.IntentId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070046import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080047import org.onlab.onos.net.resource.Bandwidth;
48import org.onlab.onos.net.resource.BandwidthResourceAllocation;
49import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
50import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
51import org.onlab.onos.net.resource.Lambda;
52import org.onlab.onos.net.resource.LambdaResourceAllocation;
53import org.onlab.onos.net.resource.LinkResourceRequest;
54import org.onlab.onos.net.resource.ResourceAllocation;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080055import org.onlab.onos.net.intent.constraint.AnnotationConstraint;
56import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
57import org.onlab.onos.net.intent.constraint.LambdaConstraint;
58import org.onlab.onos.net.intent.constraint.LatencyConstraint;
59import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
60import org.onlab.onos.net.intent.constraint.ObstacleConstraint;
61import org.onlab.onos.net.intent.constraint.WaypointConstraint;
alshabib7911a052014-10-16 17:49:37 -070062import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070063import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080064import org.onlab.packet.Ip4Address;
65import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070066import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080067import org.onlab.packet.Ip4Prefix;
68import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070069import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070070import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070071
Thomas Vachuskad16ce182014-10-29 17:25:29 -070072import java.nio.ByteBuffer;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080073import java.util.Arrays;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080074import java.util.HashMap;
75import java.util.Map;
76import java.util.Set;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080077import java.time.Duration;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070078
79import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080080import static org.junit.Assert.*;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070081import static org.onlab.onos.net.DeviceId.deviceId;
82import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070083
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070084public class KryoSerializerTest {
85
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070086 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070087 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070088 private static final DeviceId DID1 = deviceId("of:foo");
89 private static final DeviceId DID2 = deviceId("of:bar");
90 private static final PortNumber P1 = portNumber(1);
91 private static final PortNumber P2 = portNumber(2);
92 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
93 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
94 private static final String MFR = "whitebox";
95 private static final String HW = "1.1.x";
96 private static final String SW1 = "3.8.1";
97 private static final String SW2 = "3.9.5";
98 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070099 private static final ChassisId CID = new ChassisId();
100 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
101 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700102 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
103 .set("A1", "a1")
104 .set("B1", "b1")
105 .build();
106 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
107 .remove("A1")
108 .set("B3", "b3")
109 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700110
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700111 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700112
113 @BeforeClass
114 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700115 }
116
117 @Before
118 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700119 serializer = new KryoSerializer() {
120
121 @Override
122 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700123 serializerPool = KryoNamespace.newBuilder()
124 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800125 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
126 .build();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700127 }
128 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700129 }
130
131 @After
132 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700133 }
134
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800135 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700136 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700137 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700138 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700139 T copy = serializer.decode(buffer);
140
141 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700142
143 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700144 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700145 .testEquals();
146 }
147
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800148 private <T> void testSerializable(T original) {
149 byte[] bs = serializer.encode(original);
150 T copy = serializer.decode(bs);
151 assertNotNull(copy);
152 }
153
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700154
155 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700156 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800157 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700158 }
159
160 @Test
161 public void testDefaultLink() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800162 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
163 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700164 }
165
166 @Test
167 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800168 testSerializedEquals(new DefaultPort(DEV1, P1, true));
169 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700170 }
171
172 @Test
173 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800174 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700175 }
176
177 @Test
178 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800179 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
180 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
181 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700182 }
183
184 @Test
185 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800186 testSerializedEquals(ImmutableSet.of(DID1, DID2));
187 testSerializedEquals(ImmutableSet.of(DID1));
188 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700189 }
190
191 @Test
192 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800193 testSerializedEquals(ImmutableList.of(DID1, DID2));
194 testSerializedEquals(ImmutableList.of(DID1));
195 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700196 }
197
198 @Test
199 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800200 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700201 }
202
203 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800204 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800205 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800206 }
207
208 @Test
209 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800210 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800211 }
212
213 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700214 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800215 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700216 }
217
218 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800219 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800220 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800221 }
222
223 @Test
224 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800225 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800226 }
227
228 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700229 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800230 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700231 }
232
233 @Test
234 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800235 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700236 }
237
238 @Test
239 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800240 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700241 }
242
243 @Test
244 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800245 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700246 }
247
248 @Test
249 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800250 testSerializedEquals(PID);
251 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700252 }
253
254 @Test
255 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800256 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
257 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700258 }
259
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700260 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700261 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800262 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700263 }
264
265 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700266 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800267 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700268 }
269
270 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700271 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800272 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700273 asList(new NodeId("stby1"), new NodeId("stby2"))));
274 }
275
276 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800277 public void testDefaultLinkResourceRequest() {
278 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
279 .addLambdaRequest()
280 .addBandwidthRequest(32.195)
281 .build()
282 );
283 }
284
285 @Test
286 public void testDefaultLinkResourceAllocations() {
287 LinkResourceRequest request = DefaultLinkResourceRequest
288 .builder(IntentId.valueOf(2501), ImmutableList.of())
289 .addLambdaRequest()
290 .addBandwidthRequest(32.195)
291 .build();
292 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
293 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
294 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
295 new LambdaResourceAllocation(Lambda.valueOf(1))));
296 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
297 }
298
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800299 @Test
300 public void testLambdaConstraint() {
301 testSerializable(new LambdaConstraint(Lambda.valueOf(1)));
302 }
303
304 @Test
305 public void testBandwidthConstraint() {
306 testSerializable(new BandwidthConstraint(Bandwidth.valueOf(1000.0)));
307 }
308
309 @Test
310 public void testLinkTypeConstraint() {
311 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
312 }
313
314 @Test
315 public void testLatencyConstraint() {
316 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
317 }
318
319 @Test
320 public void testWaypointConstraint() {
321 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
322 }
323
324 @Test
325 public void testObstacleConstraint() {
326 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
327 }
328
329 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800330 public void testArraysAsList() {
331 testSerializedEquals(Arrays.asList(1, 2, 3));
332 }
333
334 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800335 public void testAnnotationConstraint() {
336 testSerializable(new AnnotationConstraint("distance", 100.0));
337 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800338
339 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700340 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700341 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700342 final byte[] a1Bytes = serializer.encode(A1);
343 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700344 assertAnnotationsEquals(copiedA1, A1);
345
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700346 final byte[] a12Bytes = serializer.encode(A1_2);
347 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700348 assertAnnotationsEquals(copiedA12, A1_2);
349 }
350
351 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700352 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700353 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700354 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700355 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700356 }
357 assertEquals(expected.keys(), actual.keys());
358 for (String key : expected.keys()) {
359 assertEquals(expected.value(key), actual.value(key));
360 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700361 }
362
363}