Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame^] | 1 | package org.onosproject.store.consistent.impl; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | |
| 5 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 6 | import org.apache.commons.lang3.tuple.Pair; |
| 7 | import org.onlab.util.KryoNamespace; |
| 8 | import org.onosproject.store.serializers.KryoNamespaces; |
| 9 | import org.onosproject.store.serializers.KryoSerializer; |
| 10 | import org.onosproject.store.service.Versioned; |
| 11 | |
| 12 | import net.kuujo.copycat.cluster.internal.MemberInfo; |
| 13 | import net.kuujo.copycat.protocol.rpc.AppendRequest; |
| 14 | import net.kuujo.copycat.protocol.rpc.AppendResponse; |
| 15 | import net.kuujo.copycat.protocol.rpc.CommitRequest; |
| 16 | import net.kuujo.copycat.protocol.rpc.CommitResponse; |
| 17 | import net.kuujo.copycat.protocol.rpc.PollRequest; |
| 18 | import net.kuujo.copycat.protocol.rpc.PollResponse; |
| 19 | import net.kuujo.copycat.protocol.rpc.QueryRequest; |
| 20 | import net.kuujo.copycat.protocol.rpc.QueryResponse; |
| 21 | import net.kuujo.copycat.protocol.rpc.ReplicaInfo; |
| 22 | import net.kuujo.copycat.protocol.rpc.SyncRequest; |
| 23 | import net.kuujo.copycat.protocol.rpc.SyncResponse; |
| 24 | import net.kuujo.copycat.util.serializer.SerializerConfig; |
| 25 | |
| 26 | /** |
| 27 | * Serializer for DatabaseManager's interaction with Copycat. |
| 28 | */ |
| 29 | public class DatabaseSerializer extends SerializerConfig { |
| 30 | |
| 31 | private static final KryoNamespace COPYCAT = KryoNamespace.newBuilder() |
| 32 | .nextId(KryoNamespace.FLOATING_ID) |
| 33 | .register(AppendRequest.class) |
| 34 | .register(AppendResponse.class) |
| 35 | .register(SyncRequest.class) |
| 36 | .register(SyncResponse.class) |
| 37 | .register(PollRequest.class) |
| 38 | .register(PollResponse.class) |
| 39 | .register(QueryRequest.class) |
| 40 | .register(QueryResponse.class) |
| 41 | .register(CommitRequest.class) |
| 42 | .register(CommitResponse.class) |
| 43 | .register(ReplicaInfo.class) |
| 44 | .register(MemberInfo.class) |
| 45 | .build(); |
| 46 | |
| 47 | private static final KryoNamespace ONOS_STORE = KryoNamespace.newBuilder() |
| 48 | .nextId(KryoNamespace.FLOATING_ID) |
| 49 | .register(Versioned.class) |
| 50 | .register(Pair.class) |
| 51 | .register(ImmutablePair.class) |
| 52 | .build(); |
| 53 | |
| 54 | private static final KryoSerializer SERIALIZER = new KryoSerializer() { |
| 55 | @Override |
| 56 | protected void setupKryoPool() { |
| 57 | serializerPool = KryoNamespace.newBuilder() |
| 58 | .register(KryoNamespaces.BASIC) |
| 59 | .register(COPYCAT) |
| 60 | .register(ONOS_STORE) |
| 61 | .build(); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | @Override |
| 66 | public ByteBuffer writeObject(Object object) { |
| 67 | return ByteBuffer.wrap(SERIALIZER.encode(object)); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public <T> T readObject(ByteBuffer buffer) { |
| 72 | return SERIALIZER.decode(buffer); |
| 73 | } |
| 74 | } |