tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.store.serializers; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | import org.onlab.onos.net.DefaultPort; |
| 7 | import org.onlab.onos.net.Element; |
| 8 | import org.onlab.onos.net.PortNumber; |
| 9 | import org.onlab.packet.IpPrefix; |
| 10 | |
| 11 | import com.esotericsoftware.kryo.Kryo; |
| 12 | import com.esotericsoftware.kryo.Serializer; |
| 13 | import com.esotericsoftware.kryo.io.Input; |
| 14 | import com.esotericsoftware.kryo.io.Output; |
| 15 | import com.esotericsoftware.kryo.serializers.CollectionSerializer; |
| 16 | import com.google.common.collect.ImmutableSet; |
| 17 | |
| 18 | // TODO move to util, etc. |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Kryo Serializer for {@link DefaultPort}. |
| 21 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 22 | public final class DefaultPortSerializer extends |
| 23 | Serializer<DefaultPort> { |
| 24 | |
| 25 | private final CollectionSerializer ipAddrSerializer |
| 26 | = new CollectionSerializer(IpPrefix.class, |
| 27 | new IpPrefixSerializer(), false); |
| 28 | |
Yuta HIGUCHI | c705201 | 2014-09-22 19:11:00 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Default constructor. |
| 31 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 32 | public DefaultPortSerializer() { |
| 33 | // non-null, immutable |
| 34 | super(false, true); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void write(Kryo kryo, Output output, DefaultPort object) { |
| 39 | kryo.writeClassAndObject(output, object.element()); |
| 40 | kryo.writeObject(output, object.number()); |
| 41 | output.writeBoolean(object.isEnabled()); |
| 42 | kryo.writeObject(output, object.ipAddresses(), |
| 43 | ipAddrSerializer); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public DefaultPort read(Kryo kryo, Input input, |
| 48 | Class<DefaultPort> type) { |
| 49 | Element element = (Element) kryo.readClassAndObject(input); |
| 50 | PortNumber number = kryo.readObject(input, PortNumber.class); |
| 51 | boolean isEnabled = input.readBoolean(); |
| 52 | @SuppressWarnings("unchecked") |
| 53 | Collection<IpPrefix> ipAddresses = kryo.readObject( |
| 54 | input, ArrayList.class, ipAddrSerializer); |
| 55 | |
| 56 | return new DefaultPort(element, number, isEnabled, |
| 57 | ImmutableSet.copyOf(ipAddresses)); |
| 58 | } |
| 59 | } |