blob: a5a2507d85cd6d39a14c4dce329b301394da21ca [file] [log] [blame]
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07001package org.onlab.onos.store.serializers;
2
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -07003import static org.junit.Assert.assertEquals;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07004import static org.onlab.onos.net.DeviceId.deviceId;
5import static org.onlab.onos.net.PortNumber.portNumber;
6
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07007import java.nio.ByteBuffer;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -07008
Yuta HIGUCHI533ec322014-09-30 13:29:52 -07009import org.junit.After;
10import org.junit.Before;
11import org.junit.BeforeClass;
12import org.junit.Test;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070013import org.onlab.onos.cluster.MastershipTerm;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070014import org.onlab.onos.cluster.NodeId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070015import org.onlab.onos.net.Annotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070016import org.onlab.onos.net.ConnectPoint;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070017import org.onlab.onos.net.DefaultAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070018import org.onlab.onos.net.DefaultDevice;
19import org.onlab.onos.net.DefaultLink;
20import org.onlab.onos.net.DefaultPort;
21import org.onlab.onos.net.Device;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.Link;
24import org.onlab.onos.net.LinkKey;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070025import org.onlab.onos.net.MastershipRole;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070026import org.onlab.onos.net.PortNumber;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070027import org.onlab.onos.net.SparseAnnotations;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070028import org.onlab.onos.net.provider.ProviderId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070029import org.onlab.packet.IpAddress;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070030import org.onlab.packet.IpPrefix;
31import org.onlab.util.KryoPool;
32
33import com.google.common.collect.ImmutableMap;
34import com.google.common.collect.ImmutableSet;
35import com.google.common.testing.EqualsTester;
36
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070037public class KryoSerializerTests {
38 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070039 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070040 private static final DeviceId DID1 = deviceId("of:foo");
41 private static final DeviceId DID2 = deviceId("of:bar");
42 private static final PortNumber P1 = portNumber(1);
43 private static final PortNumber P2 = portNumber(2);
44 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
45 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
46 private static final String MFR = "whitebox";
47 private static final String HW = "1.1.x";
48 private static final String SW1 = "3.8.1";
49 private static final String SW2 = "3.9.5";
50 private static final String SN = "43311-12345";
51 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW, SW1, SN);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070052 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
53 .set("A1", "a1")
54 .set("B1", "b1")
55 .build();
56 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
57 .remove("A1")
58 .set("B3", "b3")
59 .build();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070060
61 private static KryoPool kryos;
62
63 @BeforeClass
64 public static void setUpBeforeClass() throws Exception {
65 kryos = KryoPool.newBuilder()
Yuta HIGUCHI03fec1f2014-10-03 09:13:50 -070066 .register(KryoPoolUtil.API)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070067 .register(ImmutableMap.class, new ImmutableMapSerializer())
68 .register(ImmutableSet.class, new ImmutableSetSerializer())
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070069 .build();
70 }
71
72 @Before
73 public void setUp() throws Exception {
74 }
75
76 @After
77 public void tearDown() throws Exception {
78 // removing Kryo instance to use fresh Kryo on each tests
79 kryos.getKryo();
80 }
81
82 private static <T> void testSerialized(T original) {
83 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
84 kryos.serialize(original, buffer);
85 buffer.flip();
86 T copy = kryos.deserialize(buffer);
87
88 new EqualsTester()
89 .addEqualityGroup(original, copy)
90 .testEquals();
91 }
92
93
94 @Test
95 public final void test() {
96 testSerialized(new ConnectPoint(DID1, P1));
97 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
98 testSerialized(new DefaultPort(DEV1, P1, true));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070099 testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
100 testSerialized(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700101 testSerialized(DID1);
102 testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
103 testSerialized(ImmutableMap.of(DID1, DEV1));
104 testSerialized(ImmutableMap.of());
105 testSerialized(ImmutableSet.of(DID1, DID2));
106 testSerialized(ImmutableSet.of(DID1));
107 testSerialized(ImmutableSet.of());
108 testSerialized(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700109 testSerialized(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700110 testSerialized(new LinkKey(CP1, CP2));
111 testSerialized(new NodeId("SomeNodeIdentifier"));
112 testSerialized(P1);
113 testSerialized(PID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700114 testSerialized(PIDA);
115 testSerialized(new NodeId("bar"));
116 testSerialized(MastershipTerm.of(new NodeId("foo"), 2));
117 for (MastershipRole role : MastershipRole.values()) {
118 testSerialized(role);
119 }
120 }
121
122 public final void testAnnotations() {
123 // Annotations does not have equals defined, manually test equality
124 final byte[] a1Bytes = kryos.serialize(A1);
125 SparseAnnotations copiedA1 = kryos.deserialize(a1Bytes);
126 assertAnnotationsEquals(copiedA1, A1);
127
128 final byte[] a12Bytes = kryos.serialize(A1_2);
129 SparseAnnotations copiedA12 = kryos.deserialize(a12Bytes);
130 assertAnnotationsEquals(copiedA12, A1_2);
131 }
132
133 // code clone
134 public static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
135 DefaultAnnotations expected = DefaultAnnotations.builder().build();
136 for (SparseAnnotations a : annotations) {
137 expected = DefaultAnnotations.merge(expected, a);
138 }
139 assertEquals(expected.keys(), actual.keys());
140 for (String key : expected.keys()) {
141 assertEquals(expected.value(key), actual.value(key));
142 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700143 }
144
145}