Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.serializers; |
| 17 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 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; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.BeforeClass; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.onos.cluster.NodeId; |
Yuta HIGUCHI | 40d0177 | 2014-10-21 00:08:44 -0700 | [diff] [blame] | 27 | import org.onlab.onos.cluster.RoleInfo; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 28 | import org.onlab.onos.mastership.MastershipTerm; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 29 | import org.onlab.onos.net.Annotations; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.ConnectPoint; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.DefaultAnnotations; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.DefaultDevice; |
| 33 | import org.onlab.onos.net.DefaultLink; |
| 34 | import org.onlab.onos.net.DefaultPort; |
| 35 | import org.onlab.onos.net.Device; |
| 36 | import org.onlab.onos.net.DeviceId; |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.HostLocation; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.Link; |
| 39 | import org.onlab.onos.net.LinkKey; |
| 40 | import org.onlab.onos.net.PortNumber; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 41 | import org.onlab.onos.net.SparseAnnotations; |
Yuta HIGUCHI | 2fcfde9 | 2014-10-20 18:37:14 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.flow.FlowId; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 43 | import org.onlab.onos.net.provider.ProviderId; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 44 | import org.onlab.packet.ChassisId; |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 45 | import org.onlab.packet.IpAddress; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 46 | import org.onlab.packet.IpPrefix; |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 47 | import org.onlab.packet.MacAddress; |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 48 | import org.onlab.util.KryoNamespace; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 49 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 50 | import java.nio.ByteBuffer; |
| 51 | |
| 52 | import static java.util.Arrays.asList; |
| 53 | import static org.junit.Assert.assertEquals; |
| 54 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 55 | import static org.onlab.onos.net.PortNumber.portNumber; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 56 | |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 57 | public class KryoSerializerTest { |
| 58 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 59 | private static final ProviderId PID = new ProviderId("of", "foo"); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 60 | private static final ProviderId PIDA = new ProviderId("of", "foo", true); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 61 | 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"; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 72 | 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 HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 75 | 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 HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 83 | |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 84 | private KryoSerializer serializer; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 85 | |
| 86 | @BeforeClass |
| 87 | public static void setUpBeforeClass() throws Exception { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Before |
| 91 | public void setUp() throws Exception { |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 92 | serializer = new KryoSerializer() { |
| 93 | |
| 94 | @Override |
| 95 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 96 | serializerPool = KryoNamespace.newBuilder() |
| 97 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 98 | .build() |
| 99 | .populate(1); |
| 100 | } |
| 101 | }; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @After |
| 105 | public void tearDown() throws Exception { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 108 | private <T> void testSerialized(T original) { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 109 | ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 110 | serializer.encode(original, buffer); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 111 | buffer.flip(); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 112 | T copy = serializer.decode(buffer); |
| 113 | |
| 114 | T copy2 = serializer.decode(serializer.encode(original)); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 115 | |
| 116 | new EqualsTester() |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 117 | .addEqualityGroup(original, copy, copy2) |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 118 | .testEquals(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | @Test |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 123 | public void testConnectPoint() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 124 | testSerialized(new ConnectPoint(DID1, P1)); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testDefaultLink() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 129 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 130 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1)); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
| 134 | public void testDefaultPort() { |
| 135 | testSerialized(new DefaultPort(DEV1, P1, true)); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 136 | testSerialized(new DefaultPort(DEV1, P1, true, A1_2)); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testDeviceId() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 141 | testSerialized(DID1); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testImmutableMap() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 146 | testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); |
| 147 | testSerialized(ImmutableMap.of(DID1, DEV1)); |
| 148 | testSerialized(ImmutableMap.of()); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void testImmutableSet() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 153 | testSerialized(ImmutableSet.of(DID1, DID2)); |
| 154 | testSerialized(ImmutableSet.of(DID1)); |
| 155 | testSerialized(ImmutableSet.of()); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 156 | } |
| 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 HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 167 | testSerialized(IpPrefix.valueOf("192.168.0.1/24")); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Test |
| 171 | public void testIpAddress() { |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 172 | testSerialized(IpAddress.valueOf("192.168.0.1")); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 173 | } |
| 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 HIGUCHI | 18ab8a9 | 2014-10-13 11:16:19 -0700 | [diff] [blame] | 182 | testSerialized(LinkKey.linkKey(CP1, CP2)); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void testNodeId() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 187 | testSerialized(new NodeId("SomeNodeIdentifier")); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void testPortNumber() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 192 | testSerialized(P1); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void testProviderId() { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 197 | testSerialized(PID); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 198 | testSerialized(PIDA); |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Test |
| 202 | public void testMastershipTerm() { |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 203 | testSerialized(MastershipTerm.of(new NodeId("foo"), 2)); |
Yuta HIGUCHI | dfe6e3b | 2014-10-30 11:31:51 -0700 | [diff] [blame] | 204 | testSerialized(MastershipTerm.of(null, 0)); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 207 | @Test |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 208 | public void testHostLocation() { |
| 209 | testSerialized(new HostLocation(CP1, 1234L)); |
| 210 | } |
| 211 | |
| 212 | @Test |
Yuta HIGUCHI | 2fcfde9 | 2014-10-20 18:37:14 -0700 | [diff] [blame] | 213 | public void testFlowId() { |
| 214 | testSerialized(FlowId.valueOf(0x12345678L)); |
| 215 | } |
| 216 | |
| 217 | @Test |
Yuta HIGUCHI | 40d0177 | 2014-10-21 00:08:44 -0700 | [diff] [blame] | 218 | public void testRoleInfo() { |
| 219 | testSerialized(new RoleInfo(new NodeId("master"), |
| 220 | asList(new NodeId("stby1"), new NodeId("stby2")))); |
| 221 | } |
| 222 | |
| 223 | @Test |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 224 | public void testAnnotations() { |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 225 | // Annotations does not have equals defined, manually test equality |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 226 | final byte[] a1Bytes = serializer.encode(A1); |
| 227 | SparseAnnotations copiedA1 = serializer.decode(a1Bytes); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 228 | assertAnnotationsEquals(copiedA1, A1); |
| 229 | |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 230 | final byte[] a12Bytes = serializer.encode(A1_2); |
| 231 | SparseAnnotations copiedA12 = serializer.decode(a12Bytes); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 232 | assertAnnotationsEquals(copiedA12, A1_2); |
| 233 | } |
| 234 | |
| 235 | // code clone |
Yuta HIGUCHI | aaac0d7 | 2014-10-19 17:57:47 -0700 | [diff] [blame] | 236 | protected static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) { |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 237 | SparseAnnotations expected = DefaultAnnotations.builder().build(); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 238 | for (SparseAnnotations a : annotations) { |
Yuta HIGUCHI | 9ee0d5b | 2014-10-05 00:03:47 -0700 | [diff] [blame] | 239 | expected = DefaultAnnotations.union(expected, a); |
Yuta HIGUCHI | 5bdebe3 | 2014-10-04 21:40:41 -0700 | [diff] [blame] | 240 | } |
| 241 | assertEquals(expected.keys(), actual.keys()); |
| 242 | for (String key : expected.keys()) { |
| 243 | assertEquals(expected.value(key), actual.value(key)); |
| 244 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | } |