blob: c156143cba938deb06056afd1124b79a94234bff [file] [log] [blame]
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -07001package org.onlab.onos.store.mastership.impl;
2
3import java.util.Collections;
Ayaka Koshibee8e45352014-10-16 00:37:19 -07004import java.util.HashMap;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -07005import java.util.LinkedList;
6import java.util.List;
7import java.util.Map;
8
9import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070010import org.onlab.onos.cluster.RoleInfo;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070011import org.onlab.onos.net.MastershipRole;
12
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070013import com.google.common.base.MoreObjects;
14import com.google.common.base.MoreObjects.ToStringHelper;
15
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070016/**
17 * A structure that holds node mastership roles associated with a
18 * {@link DeviceId}. This structure needs to be locked through IMap.
19 */
20public class RoleValue {
21
Ayaka Koshibee8e45352014-10-16 00:37:19 -070022 protected Map<MastershipRole, List<NodeId>> value = new HashMap<>();
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070023
24 public RoleValue() {
25 value.put(MastershipRole.MASTER, new LinkedList<NodeId>());
26 value.put(MastershipRole.STANDBY, new LinkedList<NodeId>());
27 value.put(MastershipRole.NONE, new LinkedList<NodeId>());
28 }
29
30 public Map<MastershipRole, List<NodeId>> value() {
31 return Collections.unmodifiableMap(value);
32 }
33
34 public List<NodeId> nodesOfRole(MastershipRole type) {
35 return value.get(type);
36 }
37
38 public NodeId get(MastershipRole type) {
39 return value.get(type).isEmpty() ? null : value.get(type).get(0);
40 }
41
42 public boolean contains(MastershipRole type, NodeId nodeId) {
43 return value.get(type).contains(nodeId);
44 }
45
46 /**
47 * Associates a node to a certain role.
48 *
49 * @param type the role
50 * @param nodeId the node ID of the node to associate
51 */
52 public void add(MastershipRole type, NodeId nodeId) {
53 List<NodeId> nodes = value.get(type);
54
55 if (!nodes.contains(nodeId)) {
56 nodes.add(nodeId);
57 }
58 }
59
60 /**
61 * Removes a node from a certain role.
62 *
63 * @param type the role
64 * @param nodeId the ID of the node to remove
65 * @return
66 */
67 public boolean remove(MastershipRole type, NodeId nodeId) {
68 List<NodeId> nodes = value.get(type);
69 if (!nodes.isEmpty()) {
70 return nodes.remove(nodeId);
71 } else {
72 return false;
73 }
74 }
75
76 /**
77 * Reassigns a node from one role to another. If the node was not of the
78 * old role, it will still be assigned the new role.
79 *
80 * @param nodeId the Node ID of node changing roles
81 * @param from the old role
82 * @param to the new role
83 */
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070084 public void reassign(NodeId nodeId, MastershipRole from, MastershipRole to) {
85 remove(from, nodeId);
86 add(to, nodeId);
87 }
88
89 /**
90 * Replaces a node in one role with another node. Even if there is no node to
91 * replace, the new node is associated to the role.
92 *
93 * @param from the old NodeId to replace
94 * @param to the new NodeId
95 * @param type the role associated with the old NodeId
96 */
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070097 public void replace(NodeId from, NodeId to, MastershipRole type) {
98 remove(type, from);
99 add(type, to);
100 }
101
Ayaka Koshibe67af1f42014-10-20 15:26:37 -0700102 /**
103 * Summarizes this RoleValue as a RoleInfo. Note that master and/or backups
104 * may be empty, so the values should be checked for safety.
105 *
106 * @return the RoleInfo.
107 */
108 public RoleInfo roleInfo() {
109 return new RoleInfo(
110 get(MastershipRole.MASTER), nodesOfRole(MastershipRole.STANDBY));
111 }
112
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700113 @Override
114 public String toString() {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700115 ToStringHelper helper = MoreObjects.toStringHelper(this.getClass());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700116 for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700117 helper.add(el.getKey().toString(), el.getValue());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700118 }
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700119 return helper.toString();
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700120 }
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700121}