blob: 4450e5b13ac5eff7056c7a7d241e423a8f1cea0d [file] [log] [blame]
Ayaka Koshibee8e45352014-10-16 00:37:19 -07001package org.onlab.onos.store.mastership.impl;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -07002
3import java.util.List;
4import java.util.Map;
5
6import org.onlab.onos.cluster.NodeId;
7import org.onlab.onos.net.MastershipRole;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -07008
9import com.esotericsoftware.kryo.Kryo;
10import com.esotericsoftware.kryo.Serializer;
11import com.esotericsoftware.kryo.io.Input;
12import com.esotericsoftware.kryo.io.Output;
13
14/**
Ayaka Koshibee8e45352014-10-16 00:37:19 -070015 * Serializer for RoleValues used by {@link DistributedMastershipStore}.
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070016 */
17public 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) {
Yuta HIGUCHI868def02014-10-23 12:09:43 -070038 final Map<MastershipRole, List<NodeId>> map = type.value();
39 output.writeInt(map.size());
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070040
Yuta HIGUCHI868def02014-10-23 12:09:43 -070041 for (Map.Entry<MastershipRole, List<NodeId>> el : map.entrySet()) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070042 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}