Added Kryo serializers for Ip4Address, Ip6Address, Ip4Prefix, Ip6Prefix
Change-Id: Ib014bbe40b3df5c778e2d16d7b65b1cb62944e1c
Showing
6 changed files
with
254 additions
and
0 deletions
core/store/serializers/src/main/java/org/onlab/onos/store/serializers/Ip4AddressSerializer.java
0 → 100644
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 | + */ | ||
16 | +package org.onlab.onos.store.serializers; | ||
17 | + | ||
18 | +import org.onlab.packet.Ip4Address; | ||
19 | +import com.esotericsoftware.kryo.Kryo; | ||
20 | +import com.esotericsoftware.kryo.Serializer; | ||
21 | +import com.esotericsoftware.kryo.io.Input; | ||
22 | +import com.esotericsoftware.kryo.io.Output; | ||
23 | + | ||
24 | +/** | ||
25 | + * Kryo Serializer for {@link Ip4Address}. | ||
26 | + */ | ||
27 | +public class Ip4AddressSerializer extends Serializer<Ip4Address> { | ||
28 | + | ||
29 | + /** | ||
30 | + * Creates {@link Ip4Address} serializer instance. | ||
31 | + */ | ||
32 | + public Ip4AddressSerializer() { | ||
33 | + // non-null, immutable | ||
34 | + super(false, true); | ||
35 | + } | ||
36 | + | ||
37 | + @Override | ||
38 | + public void write(Kryo kryo, Output output, Ip4Address object) { | ||
39 | + byte[] octs = object.toOctets(); | ||
40 | + // TODO: Writing (and reading) the number of octets is redundant: | ||
41 | + // It is always Ip4Address.BYTE_LENGTH | ||
42 | + output.writeInt(octs.length); | ||
43 | + output.writeBytes(octs); | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public Ip4Address read(Kryo kryo, Input input, Class<Ip4Address> type) { | ||
48 | + final int octLen = input.readInt(); | ||
49 | + byte[] octs = new byte[octLen]; | ||
50 | + input.readBytes(octs); | ||
51 | + return Ip4Address.valueOf(octs); | ||
52 | + } | ||
53 | +} |
core/store/serializers/src/main/java/org/onlab/onos/store/serializers/Ip4PrefixSerializer.java
0 → 100644
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 | + */ | ||
16 | +package org.onlab.onos.store.serializers; | ||
17 | + | ||
18 | +import org.onlab.packet.Ip4Prefix; | ||
19 | + | ||
20 | +import com.esotericsoftware.kryo.Kryo; | ||
21 | +import com.esotericsoftware.kryo.Serializer; | ||
22 | +import com.esotericsoftware.kryo.io.Input; | ||
23 | +import com.esotericsoftware.kryo.io.Output; | ||
24 | + | ||
25 | +/** | ||
26 | + * Kryo Serializer for {@link Ip4Prefix}. | ||
27 | + */ | ||
28 | +public final class Ip4PrefixSerializer extends Serializer<Ip4Prefix> { | ||
29 | + | ||
30 | + /** | ||
31 | + * Creates {@link Ip4Prefix} serializer instance. | ||
32 | + */ | ||
33 | + public Ip4PrefixSerializer() { | ||
34 | + // non-null, immutable | ||
35 | + super(false, true); | ||
36 | + } | ||
37 | + | ||
38 | + @Override | ||
39 | + public void write(Kryo kryo, Output output, | ||
40 | + Ip4Prefix object) { | ||
41 | + byte[] octs = object.address().toOctets(); | ||
42 | + // TODO: Writing (and reading) the number of octets is redundant: | ||
43 | + // It is always Ip6Address.BYTE_LENGTH | ||
44 | + output.writeInt(octs.length); | ||
45 | + output.writeBytes(octs); | ||
46 | + output.writeInt(object.prefixLength()); | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public Ip4Prefix read(Kryo kryo, Input input, | ||
51 | + Class<Ip4Prefix> type) { | ||
52 | + int octLen = input.readInt(); | ||
53 | + byte[] octs = new byte[octLen]; | ||
54 | + input.readBytes(octs); | ||
55 | + int prefLen = input.readInt(); | ||
56 | + return Ip4Prefix.valueOf(octs, prefLen); | ||
57 | + } | ||
58 | +} |
core/store/serializers/src/main/java/org/onlab/onos/store/serializers/Ip6AddressSerializer.java
0 → 100644
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 | + */ | ||
16 | +package org.onlab.onos.store.serializers; | ||
17 | + | ||
18 | +import org.onlab.packet.Ip6Address; | ||
19 | +import com.esotericsoftware.kryo.Kryo; | ||
20 | +import com.esotericsoftware.kryo.Serializer; | ||
21 | +import com.esotericsoftware.kryo.io.Input; | ||
22 | +import com.esotericsoftware.kryo.io.Output; | ||
23 | + | ||
24 | +/** | ||
25 | + * Kryo Serializer for {@link Ip6Address}. | ||
26 | + */ | ||
27 | +public class Ip6AddressSerializer extends Serializer<Ip6Address> { | ||
28 | + | ||
29 | + /** | ||
30 | + * Creates {@link Ip6Address} serializer instance. | ||
31 | + */ | ||
32 | + public Ip6AddressSerializer() { | ||
33 | + // non-null, immutable | ||
34 | + super(false, true); | ||
35 | + } | ||
36 | + | ||
37 | + @Override | ||
38 | + public void write(Kryo kryo, Output output, Ip6Address object) { | ||
39 | + byte[] octs = object.toOctets(); | ||
40 | + // TODO: Writing (and reading) the number of octets is redundant: | ||
41 | + // It is always Ip6Address.BYTE_LENGTH | ||
42 | + output.writeInt(octs.length); | ||
43 | + output.writeBytes(octs); | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public Ip6Address read(Kryo kryo, Input input, Class<Ip6Address> type) { | ||
48 | + final int octLen = input.readInt(); | ||
49 | + byte[] octs = new byte[octLen]; | ||
50 | + input.readBytes(octs); | ||
51 | + return Ip6Address.valueOf(octs); | ||
52 | + } | ||
53 | +} |
core/store/serializers/src/main/java/org/onlab/onos/store/serializers/Ip6PrefixSerializer.java
0 → 100644
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 | + */ | ||
16 | +package org.onlab.onos.store.serializers; | ||
17 | + | ||
18 | +import org.onlab.packet.Ip6Prefix; | ||
19 | + | ||
20 | +import com.esotericsoftware.kryo.Kryo; | ||
21 | +import com.esotericsoftware.kryo.Serializer; | ||
22 | +import com.esotericsoftware.kryo.io.Input; | ||
23 | +import com.esotericsoftware.kryo.io.Output; | ||
24 | + | ||
25 | +/** | ||
26 | + * Kryo Serializer for {@link Ip6Prefix}. | ||
27 | + */ | ||
28 | +public final class Ip6PrefixSerializer extends Serializer<Ip6Prefix> { | ||
29 | + | ||
30 | + /** | ||
31 | + * Creates {@link Ip6Prefix} serializer instance. | ||
32 | + */ | ||
33 | + public Ip6PrefixSerializer() { | ||
34 | + // non-null, immutable | ||
35 | + super(false, true); | ||
36 | + } | ||
37 | + | ||
38 | + @Override | ||
39 | + public void write(Kryo kryo, Output output, | ||
40 | + Ip6Prefix object) { | ||
41 | + byte[] octs = object.address().toOctets(); | ||
42 | + // TODO: Writing (and reading) the number of octets is redundant: | ||
43 | + // It is always Ip6Address.BYTE_LENGTH | ||
44 | + output.writeInt(octs.length); | ||
45 | + output.writeBytes(octs); | ||
46 | + output.writeInt(object.prefixLength()); | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public Ip6Prefix read(Kryo kryo, Input input, | ||
51 | + Class<Ip6Prefix> type) { | ||
52 | + int octLen = input.readInt(); | ||
53 | + byte[] octs = new byte[octLen]; | ||
54 | + input.readBytes(octs); | ||
55 | + int prefLen = input.readInt(); | ||
56 | + return Ip6Prefix.valueOf(octs, prefLen); | ||
57 | + } | ||
58 | +} |
... | @@ -88,7 +88,11 @@ import org.onlab.onos.net.resource.LinkResourceRequest; | ... | @@ -88,7 +88,11 @@ import org.onlab.onos.net.resource.LinkResourceRequest; |
88 | import org.onlab.onos.store.Timestamp; | 88 | import org.onlab.onos.store.Timestamp; |
89 | import org.onlab.packet.ChassisId; | 89 | import org.onlab.packet.ChassisId; |
90 | import org.onlab.packet.IpAddress; | 90 | import org.onlab.packet.IpAddress; |
91 | +import org.onlab.packet.Ip4Address; | ||
92 | +import org.onlab.packet.Ip6Address; | ||
91 | import org.onlab.packet.IpPrefix; | 93 | import org.onlab.packet.IpPrefix; |
94 | +import org.onlab.packet.Ip4Prefix; | ||
95 | +import org.onlab.packet.Ip6Prefix; | ||
92 | import org.onlab.packet.MacAddress; | 96 | import org.onlab.packet.MacAddress; |
93 | import org.onlab.packet.VlanId; | 97 | import org.onlab.packet.VlanId; |
94 | import org.onlab.util.KryoNamespace; | 98 | import org.onlab.util.KryoNamespace; |
... | @@ -104,7 +108,11 @@ public final class KryoNamespaces { | ... | @@ -104,7 +108,11 @@ public final class KryoNamespaces { |
104 | */ | 108 | */ |
105 | public static final KryoNamespace MISC = KryoNamespace.newBuilder() | 109 | public static final KryoNamespace MISC = KryoNamespace.newBuilder() |
106 | .register(IpPrefix.class, new IpPrefixSerializer()) | 110 | .register(IpPrefix.class, new IpPrefixSerializer()) |
111 | + .register(Ip4Prefix.class, new Ip4PrefixSerializer()) | ||
112 | + .register(Ip6Prefix.class, new Ip6PrefixSerializer()) | ||
107 | .register(IpAddress.class, new IpAddressSerializer()) | 113 | .register(IpAddress.class, new IpAddressSerializer()) |
114 | + .register(Ip4Address.class, new Ip4AddressSerializer()) | ||
115 | + .register(Ip6Address.class, new Ip6AddressSerializer()) | ||
108 | .register(MacAddress.class, new MacAddressSerializer()) | 116 | .register(MacAddress.class, new MacAddressSerializer()) |
109 | .register(VlanId.class) | 117 | .register(VlanId.class) |
110 | .build(); | 118 | .build(); | ... | ... |
... | @@ -43,7 +43,11 @@ import org.onlab.onos.net.flow.FlowId; | ... | @@ -43,7 +43,11 @@ import org.onlab.onos.net.flow.FlowId; |
43 | import org.onlab.onos.net.provider.ProviderId; | 43 | import org.onlab.onos.net.provider.ProviderId; |
44 | import org.onlab.packet.ChassisId; | 44 | import org.onlab.packet.ChassisId; |
45 | import org.onlab.packet.IpAddress; | 45 | import org.onlab.packet.IpAddress; |
46 | +import org.onlab.packet.Ip4Address; | ||
47 | +import org.onlab.packet.Ip6Address; | ||
46 | import org.onlab.packet.IpPrefix; | 48 | import org.onlab.packet.IpPrefix; |
49 | +import org.onlab.packet.Ip4Prefix; | ||
50 | +import org.onlab.packet.Ip6Prefix; | ||
47 | import org.onlab.packet.MacAddress; | 51 | import org.onlab.packet.MacAddress; |
48 | import org.onlab.util.KryoNamespace; | 52 | import org.onlab.util.KryoNamespace; |
49 | 53 | ||
... | @@ -168,11 +172,31 @@ public class KryoSerializerTest { | ... | @@ -168,11 +172,31 @@ public class KryoSerializerTest { |
168 | } | 172 | } |
169 | 173 | ||
170 | @Test | 174 | @Test |
175 | + public void testIp4Prefix() { | ||
176 | + testSerialized(Ip4Prefix.valueOf("192.168.0.1/24")); | ||
177 | + } | ||
178 | + | ||
179 | + @Test | ||
180 | + public void testIp6Prefix() { | ||
181 | + testSerialized(Ip6Prefix.valueOf("1111:2222::/120")); | ||
182 | + } | ||
183 | + | ||
184 | + @Test | ||
171 | public void testIpAddress() { | 185 | public void testIpAddress() { |
172 | testSerialized(IpAddress.valueOf("192.168.0.1")); | 186 | testSerialized(IpAddress.valueOf("192.168.0.1")); |
173 | } | 187 | } |
174 | 188 | ||
175 | @Test | 189 | @Test |
190 | + public void testIp4Address() { | ||
191 | + testSerialized(Ip4Address.valueOf("192.168.0.1")); | ||
192 | + } | ||
193 | + | ||
194 | + @Test | ||
195 | + public void testIp6Address() { | ||
196 | + testSerialized(Ip6Address.valueOf("1111:2222::")); | ||
197 | + } | ||
198 | + | ||
199 | + @Test | ||
176 | public void testMacAddress() { | 200 | public void testMacAddress() { |
177 | testSerialized(MacAddress.valueOf("12:34:56:78:90:ab")); | 201 | testSerialized(MacAddress.valueOf("12:34:56:78:90:ab")); |
178 | } | 202 | } | ... | ... |
-
Please register or login to post a comment