blob: 22d1b3579144040ab146cddfc800e4a7c34660b9 [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) {
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}