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