Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.mastership.impl; |
| 2 | |
| 3 | import java.util.Collections; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import org.onlab.onos.cluster.NodeId; |
| 9 | import org.onlab.onos.net.MastershipRole; |
| 10 | |
| 11 | /** |
| 12 | * A structure that holds node mastership roles associated with a |
| 13 | * {@link DeviceId}. This structure needs to be locked through IMap. |
| 14 | */ |
| 15 | public class RoleValue { |
| 16 | |
| 17 | Map<MastershipRole, List<NodeId>> value; |
| 18 | |
| 19 | public RoleValue() { |
| 20 | value.put(MastershipRole.MASTER, new LinkedList<NodeId>()); |
| 21 | value.put(MastershipRole.STANDBY, new LinkedList<NodeId>()); |
| 22 | value.put(MastershipRole.NONE, new LinkedList<NodeId>()); |
| 23 | } |
| 24 | |
| 25 | public Map<MastershipRole, List<NodeId>> value() { |
| 26 | return Collections.unmodifiableMap(value); |
| 27 | } |
| 28 | |
| 29 | public List<NodeId> nodesOfRole(MastershipRole type) { |
| 30 | return value.get(type); |
| 31 | } |
| 32 | |
| 33 | public NodeId get(MastershipRole type) { |
| 34 | return value.get(type).isEmpty() ? null : value.get(type).get(0); |
| 35 | } |
| 36 | |
| 37 | public boolean contains(MastershipRole type, NodeId nodeId) { |
| 38 | return value.get(type).contains(nodeId); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Associates a node to a certain role. |
| 43 | * |
| 44 | * @param type the role |
| 45 | * @param nodeId the node ID of the node to associate |
| 46 | */ |
| 47 | public void add(MastershipRole type, NodeId nodeId) { |
| 48 | List<NodeId> nodes = value.get(type); |
| 49 | |
| 50 | if (!nodes.contains(nodeId)) { |
| 51 | nodes.add(nodeId); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Removes a node from a certain role. |
| 57 | * |
| 58 | * @param type the role |
| 59 | * @param nodeId the ID of the node to remove |
| 60 | * @return |
| 61 | */ |
| 62 | public boolean remove(MastershipRole type, NodeId nodeId) { |
| 63 | List<NodeId> nodes = value.get(type); |
| 64 | if (!nodes.isEmpty()) { |
| 65 | return nodes.remove(nodeId); |
| 66 | } else { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Reassigns a node from one role to another. If the node was not of the |
| 73 | * old role, it will still be assigned the new role. |
| 74 | * |
| 75 | * @param nodeId the Node ID of node changing roles |
| 76 | * @param from the old role |
| 77 | * @param to the new role |
| 78 | */ |
| 79 | // might want to add anyways as default behavior |
| 80 | public void reassign(NodeId nodeId, MastershipRole from, MastershipRole to) { |
| 81 | remove(from, nodeId); |
| 82 | add(to, nodeId); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Replaces a node in one role with another node. Even if there is no node to |
| 87 | * replace, the new node is associated to the role. |
| 88 | * |
| 89 | * @param from the old NodeId to replace |
| 90 | * @param to the new NodeId |
| 91 | * @param type the role associated with the old NodeId |
| 92 | */ |
| 93 | // might want to add anyways as default behavior |
| 94 | public void replace(NodeId from, NodeId to, MastershipRole type) { |
| 95 | remove(type, from); |
| 96 | add(type, to); |
| 97 | } |
| 98 | |
| 99 | } |