blob: a4f098adfea753090e81001d7b4fd955e8fa0077 [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 HIGUCHI533ec322014-09-30 13:29:52 -070022import org.junit.After;
23import org.junit.Before;
24import org.junit.BeforeClass;
25import org.junit.Test;
26import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI40d01772014-10-21 00:08:44 -070027import org.onlab.onos.cluster.RoleInfo;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070028import org.onlab.onos.mastership.MastershipTerm;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070029import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070030import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070031import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070032import org.onlab.onos.net.DefaultDevice;
33import org.onlab.onos.net.DefaultLink;
34import org.onlab.onos.net.DefaultPort;
35import org.onlab.onos.net.Device;
36import org.onlab.onos.net.DeviceId;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070037import org.onlab.onos.net.HostLocation;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070038import org.onlab.onos.net.Link;
39import org.onlab.onos.net.LinkKey;
40import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070041import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -070042import org.onlab.onos.net.flow.FlowId;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070043import org.onlab.onos.net.provider.ProviderId;
alshabib7911a052014-10-16 17:49:37 -070044import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070045import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070046import org.onlab.packet.IpPrefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070047import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070048import org.onlab.util.KryoNamespace;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070049
Thomas Vachuskad16ce182014-10-29 17:25:29 -070050import java.nio.ByteBuffer;
51
52import static java.util.Arrays.asList;
53import static org.junit.Assert.assertEquals;
54import static org.onlab.onos.net.DeviceId.deviceId;
55import static org.onlab.onos.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070056
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -070057public class KryoSerializerTest {
58
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070059 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070060 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070061 private static final DeviceId DID1 = deviceId("of:foo");
62 private static final DeviceId DID2 = deviceId("of:bar");
63 private static final PortNumber P1 = portNumber(1);
64 private static final PortNumber P2 = portNumber(2);
65 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
66 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
67 private static final String MFR = "whitebox";
68 private static final String HW = "1.1.x";
69 private static final String SW1 = "3.8.1";
70 private static final String SW2 = "3.9.5";
71 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070072 private static final ChassisId CID = new ChassisId();
73 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
74 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070075 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
76 .set("A1", "a1")
77 .set("B1", "b1")
78 .build();
79 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
80 .remove("A1")
81 .set("B3", "b3")
82 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070083
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070084 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070085
86 @BeforeClass
87 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070088 }
89
90 @Before
91 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070092 serializer = new KryoSerializer() {
93
94 @Override
95 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070096 serializerPool = KryoNamespace.newBuilder()
97 .register(KryoNamespaces.API)
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070098 .build()
99 .populate(1);
100 }
101 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700102 }
103
104 @After
105 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700106 }
107
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700108 private <T> void testSerialized(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700109 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700110 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700111 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700112 T copy = serializer.decode(buffer);
113
114 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700115
116 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700117 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700118 .testEquals();
119 }
120
121
122 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700123 public void testConnectPoint() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700124 testSerialized(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700125 }
126
127 @Test
128 public void testDefaultLink() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700129 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700130 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700131 }
132
133 @Test
134 public void testDefaultPort() {
135 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700136 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700137 }
138
139 @Test
140 public void testDeviceId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700141 testSerialized(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700142 }
143
144 @Test
145 public void testImmutableMap() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700146 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
147 testSerialized(ImmutableMap.of(DID1, DEV1));
148 testSerialized(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700149 }
150
151 @Test
152 public void testImmutableSet() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700153 testSerialized(ImmutableSet.of(DID1, DID2));
154 testSerialized(ImmutableSet.of(DID1));
155 testSerialized(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700156 }
157
158 @Test
159 public void testImmutableList() {
160 testSerialized(ImmutableList.of(DID1, DID2));
161 testSerialized(ImmutableList.of(DID1));
162 testSerialized(ImmutableList.of());
163 }
164
165 @Test
166 public void testIpPrefix() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700167 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700168 }
169
170 @Test
171 public void testIpAddress() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700172 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700173 }
174
175 @Test
176 public void testMacAddress() {
177 testSerialized(MacAddress.valueOf("12:34:56:78:90:ab"));
178 }
179
180 @Test
181 public void testLinkKey() {
Yuta HIGUCHI18ab8a92014-10-13 11:16:19 -0700182 testSerialized(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700183 }
184
185 @Test
186 public void testNodeId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700187 testSerialized(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700188 }
189
190 @Test
191 public void testPortNumber() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700192 testSerialized(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700193 }
194
195 @Test
196 public void testProviderId() {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700197 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700198 testSerialized(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700199 }
200
201 @Test
202 public void testMastershipTerm() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700203 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700204 }
205
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700206 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700207 public void testHostLocation() {
208 testSerialized(new HostLocation(CP1, 1234L));
209 }
210
211 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700212 public void testFlowId() {
213 testSerialized(FlowId.valueOf(0x12345678L));
214 }
215
216 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700217 public void testRoleInfo() {
218 testSerialized(new RoleInfo(new NodeId("master"),
219 asList(new NodeId("stby1"), new NodeId("stby2"))));
220 }
221
222 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700223 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700224 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700225 final byte[] a1Bytes = serializer.encode(A1);
226 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700227 assertAnnotationsEquals(copiedA1, A1);
228
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700229 final byte[] a12Bytes = serializer.encode(A1_2);
230 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700231 assertAnnotationsEquals(copiedA12, A1_2);
232 }
233
234 // code clone
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700235 protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700236 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700237 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700238 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700239 }
240 assertEquals(expected.keys(), actual.keys());
241 for (String key : expected.keys()) {
242 assertEquals(expected.value(key), actual.value(key));
243 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700244 }
245
246}