blob: 1340bd83bbb556ec7f559c56ddc9b32abeb2523c [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;
alshabib7911a052014-10-16 17:49:37 -070055import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070056import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080057import org.onlab.packet.Ip4Address;
58import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070059import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080060import org.onlab.packet.Ip4Prefix;
61import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070062import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070063import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070064
Thomas Vachuskad16ce182014-10-29 17:25:29 -070065import java.nio.ByteBuffer;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080066import java.util.HashMap;
67import java.util.Map;
68import java.util.Set;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070069
70import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080071import static org.junit.Assert.*;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070072import static org.onlab.onos.net.DeviceId.deviceId;
73import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070074
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070075public class KryoSerializerTest {
76
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070077 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070078 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070079 private static final DeviceId DID1 = deviceId("of:foo");
80 private static final DeviceId DID2 = deviceId("of:bar");
81 private static final PortNumber P1 = portNumber(1);
82 private static final PortNumber P2 = portNumber(2);
83 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
84 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
85 private static final String MFR = "whitebox";
86 private static final String HW = "1.1.x";
87 private static final String SW1 = "3.8.1";
88 private static final String SW2 = "3.9.5";
89 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070090 private static final ChassisId CID = new ChassisId();
91 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
92 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070093 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
94 .set("A1", "a1")
95 .set("B1", "b1")
96 .build();
97 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
98 .remove("A1")
99 .set("B3", "b3")
100 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700101
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700102 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700103
104 @BeforeClass
105 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700106 }
107
108 @Before
109 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700110 serializer = new KryoSerializer() {
111
112 @Override
113 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700114 serializerPool = KryoNamespace.newBuilder()
115 .register(KryoNamespaces.API)
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700116 .build()
117 .populate(1);
118 }
119 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700120 }
121
122 @After
123 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700124 }
125
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800126 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700127 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700128 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700129 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700130 T copy = serializer.decode(buffer);
131
132 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700133
134 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700135 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700136 .testEquals();
137 }
138
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800139 private <T> void testSerializable(T original) {
140 byte[] bs = serializer.encode(original);
141 T copy = serializer.decode(bs);
142 assertNotNull(copy);
143 }
144
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700145
146 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700147 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800148 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700149 }
150
151 @Test
152 public void testDefaultLink() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800153 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
154 testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700155 }
156
157 @Test
158 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800159 testSerializedEquals(new DefaultPort(DEV1, P1, true));
160 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700161 }
162
163 @Test
164 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800165 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700166 }
167
168 @Test
169 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800170 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
171 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
172 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700173 }
174
175 @Test
176 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800177 testSerializedEquals(ImmutableSet.of(DID1, DID2));
178 testSerializedEquals(ImmutableSet.of(DID1));
179 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700180 }
181
182 @Test
183 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800184 testSerializedEquals(ImmutableList.of(DID1, DID2));
185 testSerializedEquals(ImmutableList.of(DID1));
186 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700187 }
188
189 @Test
190 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800191 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700192 }
193
194 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800195 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800196 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800197 }
198
199 @Test
200 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800201 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800202 }
203
204 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700205 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800206 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700207 }
208
209 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800210 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800211 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800212 }
213
214 @Test
215 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800216 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800217 }
218
219 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700220 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800221 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700222 }
223
224 @Test
225 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800226 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700227 }
228
229 @Test
230 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800231 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700232 }
233
234 @Test
235 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800236 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700237 }
238
239 @Test
240 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800241 testSerializedEquals(PID);
242 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700243 }
244
245 @Test
246 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800247 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
248 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700249 }
250
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700251 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700252 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800253 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700254 }
255
256 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700257 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800258 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700259 }
260
261 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700262 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800263 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700264 asList(new NodeId("stby1"), new NodeId("stby2"))));
265 }
266
267 @Test
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800268 public void testDefaultLinkResourceRequest() {
269 testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
270 .addLambdaRequest()
271 .addBandwidthRequest(32.195)
272 .build()
273 );
274 }
275
276 @Test
277 public void testDefaultLinkResourceAllocations() {
278 LinkResourceRequest request = DefaultLinkResourceRequest
279 .builder(IntentId.valueOf(2501), ImmutableList.of())
280 .addLambdaRequest()
281 .addBandwidthRequest(32.195)
282 .build();
283 Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
284 allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
285 ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
286 new LambdaResourceAllocation(Lambda.valueOf(1))));
287 testSerializable(new DefaultLinkResourceAllocations(request, allocations));
288 }
289
290
291 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700292 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700293 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700294 final byte[] a1Bytes = serializer.encode(A1);
295 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700296 assertAnnotationsEquals(copiedA1, A1);
297
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700298 final byte[] a12Bytes = serializer.encode(A1_2);
299 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700300 assertAnnotationsEquals(copiedA12, A1_2);
301 }
302
303 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700304 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700305 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700306 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700307 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700308 }
309 assertEquals(expected.keys(), actual.keys());
310 for (String key : expected.keys()) {
311 assertEquals(expected.value(key), actual.value(key));
312 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700313 }
314
315}