blob: 71e053a03e0e384a0edd4af5a34a81cadd8178c4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.mastership.impl;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070017
18import java.util.List;
19import java.util.Map;
20
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.cluster.NodeId;
22import org.onosproject.net.MastershipRole;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070023
24import com.esotericsoftware.kryo.Kryo;
25import com.esotericsoftware.kryo.Serializer;
26import com.esotericsoftware.kryo.io.Input;
27import com.esotericsoftware.kryo.io.Output;
28
29/**
Ayaka Koshibee8e45352014-10-16 00:37:19 -070030 * Serializer for RoleValues used by {@link DistributedMastershipStore}.
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070031 */
32public class RoleValueSerializer extends Serializer<RoleValue> {
33
34 //RoleValues are assumed to hold a Map of MastershipRoles (an enum)
35 //to a List of NodeIds.
36
37 @Override
38 public RoleValue read(Kryo kryo, Input input, Class<RoleValue> type) {
39 RoleValue rv = new RoleValue();
40 int size = input.readInt();
41 for (int i = 0; i < size; i++) {
42 MastershipRole role = MastershipRole.values()[input.readInt()];
43 int s = input.readInt();
44 for (int j = 0; j < s; j++) {
45 rv.add(role, new NodeId(input.readString()));
46 }
47 }
48 return rv;
49 }
50
51 @Override
52 public void write(Kryo kryo, Output output, RoleValue type) {
Yuta HIGUCHI868def02014-10-23 12:09:43 -070053 final Map<MastershipRole, List<NodeId>> map = type.value();
54 output.writeInt(map.size());
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070055
Yuta HIGUCHI868def02014-10-23 12:09:43 -070056 for (Map.Entry<MastershipRole, List<NodeId>> el : map.entrySet()) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070057 output.writeInt(el.getKey().ordinal());
58
59 List<NodeId> nodes = el.getValue();
60 output.writeInt(nodes.size());
61 for (NodeId n : nodes) {
62 output.writeString(n.toString());
63 }
64 }
65 }
66
67}