Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.serializers; |
| 2 | |
| 3 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 4 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 5 | |
| 6 | import java.net.URI; |
| 7 | import java.nio.ByteBuffer; |
| 8 | import java.util.ArrayList; |
| 9 | import java.util.HashMap; |
| 10 | |
| 11 | import org.junit.After; |
| 12 | import org.junit.Before; |
| 13 | import org.junit.BeforeClass; |
| 14 | import org.junit.Test; |
Ayaka Koshibe | 8441136 | 2014-10-01 09:33:42 -0700 | [diff] [blame] | 15 | import org.onlab.onos.cluster.MastershipTerm; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 16 | import org.onlab.onos.cluster.NodeId; |
| 17 | import org.onlab.onos.net.ConnectPoint; |
| 18 | import org.onlab.onos.net.DefaultDevice; |
| 19 | import org.onlab.onos.net.DefaultLink; |
| 20 | import org.onlab.onos.net.DefaultPort; |
| 21 | import org.onlab.onos.net.Device; |
| 22 | import org.onlab.onos.net.DeviceId; |
| 23 | import org.onlab.onos.net.Link; |
| 24 | import org.onlab.onos.net.LinkKey; |
Ayaka Koshibe | 8441136 | 2014-10-01 09:33:42 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.MastershipRole; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.PortNumber; |
| 27 | import org.onlab.onos.net.provider.ProviderId; |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.util.KryoPool; |
| 30 | |
| 31 | import com.google.common.collect.ImmutableMap; |
| 32 | import com.google.common.collect.ImmutableSet; |
| 33 | import com.google.common.testing.EqualsTester; |
| 34 | |
| 35 | import de.javakaffee.kryoserializers.URISerializer; |
| 36 | |
| 37 | public class KryoSerializerTests { |
| 38 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 39 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 40 | private static final DeviceId DID2 = deviceId("of:bar"); |
| 41 | private static final PortNumber P1 = portNumber(1); |
| 42 | private static final PortNumber P2 = portNumber(2); |
| 43 | private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1); |
| 44 | private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2); |
| 45 | private static final String MFR = "whitebox"; |
| 46 | private static final String HW = "1.1.x"; |
| 47 | private static final String SW1 = "3.8.1"; |
| 48 | private static final String SW2 = "3.9.5"; |
| 49 | private static final String SN = "43311-12345"; |
| 50 | private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN); |
| 51 | |
| 52 | private static KryoPool kryos; |
| 53 | |
| 54 | @BeforeClass |
| 55 | public static void setUpBeforeClass() throws Exception { |
| 56 | kryos = KryoPool.newBuilder() |
| 57 | .register( |
| 58 | ArrayList.class, |
| 59 | HashMap.class |
| 60 | ) |
| 61 | .register( |
| 62 | Device.Type.class, |
| 63 | Link.Type.class |
| 64 | |
| 65 | // ControllerNode.State.class, |
| 66 | // DefaultControllerNode.class, |
| 67 | // MastershipRole.class, |
| 68 | // Port.class, |
| 69 | // Element.class, |
| 70 | ) |
| 71 | .register(ConnectPoint.class, new ConnectPointSerializer()) |
| 72 | .register(DefaultLink.class, new DefaultLinkSerializer()) |
| 73 | .register(DefaultPort.class, new DefaultPortSerializer()) |
| 74 | .register(DeviceId.class, new DeviceIdSerializer()) |
| 75 | .register(ImmutableMap.class, new ImmutableMapSerializer()) |
| 76 | .register(ImmutableSet.class, new ImmutableSetSerializer()) |
| 77 | .register(IpPrefix.class, new IpPrefixSerializer()) |
| 78 | .register(LinkKey.class, new LinkKeySerializer()) |
| 79 | .register(NodeId.class, new NodeIdSerializer()) |
| 80 | .register(PortNumber.class, new PortNumberSerializer()) |
| 81 | .register(ProviderId.class, new ProviderIdSerializer()) |
| 82 | |
| 83 | .register(DefaultDevice.class) |
| 84 | |
| 85 | .register(URI.class, new URISerializer()) |
Ayaka Koshibe | 8441136 | 2014-10-01 09:33:42 -0700 | [diff] [blame] | 86 | |
| 87 | .register(MastershipRole.class, new MastershipRoleSerializer()) |
| 88 | .register(MastershipTerm.class, new MastershipTermSerializer()) |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 89 | .build(); |
| 90 | } |
| 91 | |
| 92 | @Before |
| 93 | public void setUp() throws Exception { |
| 94 | } |
| 95 | |
| 96 | @After |
| 97 | public void tearDown() throws Exception { |
| 98 | // removing Kryo instance to use fresh Kryo on each tests |
| 99 | kryos.getKryo(); |
| 100 | } |
| 101 | |
| 102 | private static <T> void testSerialized(T original) { |
| 103 | ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); |
| 104 | kryos.serialize(original, buffer); |
| 105 | buffer.flip(); |
| 106 | T copy = kryos.deserialize(buffer); |
| 107 | |
| 108 | new EqualsTester() |
| 109 | .addEqualityGroup(original, copy) |
| 110 | .testEquals(); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | @Test |
| 115 | public final void test() { |
| 116 | testSerialized(new ConnectPoint(DID1, P1)); |
| 117 | testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); |
| 118 | testSerialized(new DefaultPort(DEV1, P1, true)); |
| 119 | testSerialized(DID1); |
| 120 | testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); |
| 121 | testSerialized(ImmutableMap.of(DID1, DEV1)); |
| 122 | testSerialized(ImmutableMap.of()); |
| 123 | testSerialized(ImmutableSet.of(DID1, DID2)); |
| 124 | testSerialized(ImmutableSet.of(DID1)); |
| 125 | testSerialized(ImmutableSet.of()); |
| 126 | testSerialized(IpPrefix.valueOf("192.168.0.1/24")); |
| 127 | testSerialized(new LinkKey(CP1, CP2)); |
| 128 | testSerialized(new NodeId("SomeNodeIdentifier")); |
| 129 | testSerialized(P1); |
| 130 | testSerialized(PID); |
| 131 | } |
| 132 | |
| 133 | } |