Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.serializers; |
| 2 | |
| 3 | import java.net.URI; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.HashMap; |
| 6 | |
| 7 | import org.apache.felix.scr.annotations.Activate; |
| 8 | import org.apache.felix.scr.annotations.Component; |
| 9 | import org.apache.felix.scr.annotations.Deactivate; |
| 10 | import org.apache.felix.scr.annotations.Service; |
| 11 | import org.onlab.onos.cluster.ControllerNode; |
| 12 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 13 | import org.onlab.onos.cluster.NodeId; |
| 14 | import org.onlab.onos.net.ConnectPoint; |
| 15 | import org.onlab.onos.net.DefaultDevice; |
| 16 | import org.onlab.onos.net.DefaultLink; |
| 17 | import org.onlab.onos.net.DefaultPort; |
| 18 | import org.onlab.onos.net.Device; |
| 19 | import org.onlab.onos.net.DeviceId; |
| 20 | import org.onlab.onos.net.Element; |
| 21 | import org.onlab.onos.net.Link; |
| 22 | import org.onlab.onos.net.LinkKey; |
| 23 | import org.onlab.onos.net.MastershipRole; |
| 24 | import org.onlab.onos.net.Port; |
| 25 | import org.onlab.onos.net.PortNumber; |
| 26 | import org.onlab.onos.net.provider.ProviderId; |
| 27 | import org.onlab.packet.IpPrefix; |
| 28 | import org.onlab.util.KryoPool; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import de.javakaffee.kryoserializers.URISerializer; |
| 33 | |
| 34 | /** |
| 35 | * Serialization service using Kryo. |
| 36 | */ |
| 37 | @Component(immediate = true) |
| 38 | @Service |
| 39 | public class KryoSerializationManager implements KryoSerializationService { |
| 40 | |
| 41 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 42 | private KryoPool serializerPool; |
| 43 | |
| 44 | |
| 45 | @Activate |
| 46 | public void activate() { |
| 47 | setupKryoPool(); |
| 48 | log.info("Started"); |
| 49 | } |
| 50 | |
| 51 | @Deactivate |
| 52 | public void deactivate() { |
| 53 | log.info("Stopped"); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets up the common serialzers pool. |
| 58 | */ |
| 59 | protected void setupKryoPool() { |
| 60 | // FIXME Slice out types used in common to separate pool/namespace. |
| 61 | serializerPool = KryoPool.newBuilder() |
| 62 | .register(ArrayList.class, |
| 63 | HashMap.class, |
| 64 | |
| 65 | ControllerNode.State.class, |
| 66 | Device.Type.class, |
| 67 | |
| 68 | DefaultControllerNode.class, |
| 69 | DefaultDevice.class, |
| 70 | MastershipRole.class, |
| 71 | Port.class, |
| 72 | Element.class, |
| 73 | |
| 74 | Link.Type.class |
| 75 | ) |
| 76 | .register(IpPrefix.class, new IpPrefixSerializer()) |
| 77 | .register(URI.class, new URISerializer()) |
| 78 | .register(NodeId.class, new NodeIdSerializer()) |
| 79 | .register(ProviderId.class, new ProviderIdSerializer()) |
| 80 | .register(DeviceId.class, new DeviceIdSerializer()) |
| 81 | .register(PortNumber.class, new PortNumberSerializer()) |
| 82 | .register(DefaultPort.class, new DefaultPortSerializer()) |
| 83 | .register(LinkKey.class, new LinkKeySerializer()) |
| 84 | .register(ConnectPoint.class, new ConnectPointSerializer()) |
| 85 | .register(DefaultLink.class, new DefaultLinkSerializer()) |
| 86 | .build() |
| 87 | .populate(1); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public byte[] serialize(final Object obj) { |
| 92 | return serializerPool.serialize(obj); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public <T> T deserialize(final byte[] bytes) { |
| 97 | if (bytes == null) { |
| 98 | return null; |
| 99 | } |
| 100 | return serializerPool.deserialize(bytes); |
| 101 | } |
| 102 | |
| 103 | } |