Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.mastership.impl; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.List; |
| 19 | import java.util.Map; |
| 20 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.cluster.NodeId; |
| 22 | import org.onosproject.net.MastershipRole; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 23 | |
| 24 | import com.esotericsoftware.kryo.Kryo; |
| 25 | import com.esotericsoftware.kryo.Serializer; |
| 26 | import com.esotericsoftware.kryo.io.Input; |
| 27 | import com.esotericsoftware.kryo.io.Output; |
| 28 | |
| 29 | /** |
Thomas Vachuska | ba082b8 | 2015-05-20 13:47:38 -0700 | [diff] [blame] | 30 | * Serializer for RoleValues used by {@link org.onosproject.mastership.MastershipStore}. |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 31 | */ |
| 32 | public 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 HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 53 | final Map<MastershipRole, List<NodeId>> map = type.value(); |
| 54 | output.writeInt(map.size()); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 55 | |
Yuta HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 56 | for (Map.Entry<MastershipRole, List<NodeId>> el : map.entrySet()) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 57 | 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 | } |