Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.store.mastership.impl; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.List; |
| 4 | import java.util.Map; |
| 5 | |
| 6 | import org.onlab.onos.cluster.NodeId; |
| 7 | import org.onlab.onos.net.MastershipRole; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 8 | |
| 9 | import com.esotericsoftware.kryo.Kryo; |
| 10 | import com.esotericsoftware.kryo.Serializer; |
| 11 | import com.esotericsoftware.kryo.io.Input; |
| 12 | import com.esotericsoftware.kryo.io.Output; |
| 13 | |
| 14 | /** |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame^] | 15 | * Serializer for RoleValues used by {@link DistributedMastershipStore}. |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 16 | */ |
| 17 | public class RoleValueSerializer extends Serializer<RoleValue> { |
| 18 | |
| 19 | //RoleValues are assumed to hold a Map of MastershipRoles (an enum) |
| 20 | //to a List of NodeIds. |
| 21 | |
| 22 | @Override |
| 23 | public RoleValue read(Kryo kryo, Input input, Class<RoleValue> type) { |
| 24 | RoleValue rv = new RoleValue(); |
| 25 | int size = input.readInt(); |
| 26 | for (int i = 0; i < size; i++) { |
| 27 | MastershipRole role = MastershipRole.values()[input.readInt()]; |
| 28 | int s = input.readInt(); |
| 29 | for (int j = 0; j < s; j++) { |
| 30 | rv.add(role, new NodeId(input.readString())); |
| 31 | } |
| 32 | } |
| 33 | return rv; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public void write(Kryo kryo, Output output, RoleValue type) { |
| 38 | output.writeInt(type.value().size()); |
| 39 | |
| 40 | for (Map.Entry<MastershipRole, List<NodeId>> el : |
| 41 | type.value().entrySet()) { |
| 42 | output.writeInt(el.getKey().ordinal()); |
| 43 | |
| 44 | List<NodeId> nodes = el.getValue(); |
| 45 | output.writeInt(nodes.size()); |
| 46 | for (NodeId n : nodes) { |
| 47 | output.writeString(n.toString()); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | } |