blob: f2c35597f027fc71a41a342c2f9441277d86cd92 [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;
10import org.onlab.onos.net.MastershipRole;
11
12/**
13 * A structure that holds node mastership roles associated with a
14 * {@link DeviceId}. This structure needs to be locked through IMap.
15 */
16public class RoleValue {
17
Ayaka Koshibee8e45352014-10-16 00:37:19 -070018 protected Map<MastershipRole, List<NodeId>> value = new HashMap<>();
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070019
20 public RoleValue() {
21 value.put(MastershipRole.MASTER, new LinkedList<NodeId>());
22 value.put(MastershipRole.STANDBY, new LinkedList<NodeId>());
23 value.put(MastershipRole.NONE, new LinkedList<NodeId>());
24 }
25
26 public Map<MastershipRole, List<NodeId>> value() {
27 return Collections.unmodifiableMap(value);
28 }
29
30 public List<NodeId> nodesOfRole(MastershipRole type) {
31 return value.get(type);
32 }
33
34 public NodeId get(MastershipRole type) {
35 return value.get(type).isEmpty() ? null : value.get(type).get(0);
36 }
37
38 public boolean contains(MastershipRole type, NodeId nodeId) {
39 return value.get(type).contains(nodeId);
40 }
41
42 /**
43 * Associates a node to a certain role.
44 *
45 * @param type the role
46 * @param nodeId the node ID of the node to associate
47 */
48 public void add(MastershipRole type, NodeId nodeId) {
49 List<NodeId> nodes = value.get(type);
50
51 if (!nodes.contains(nodeId)) {
52 nodes.add(nodeId);
53 }
54 }
55
56 /**
57 * Removes a node from a certain role.
58 *
59 * @param type the role
60 * @param nodeId the ID of the node to remove
61 * @return
62 */
63 public boolean remove(MastershipRole type, NodeId nodeId) {
64 List<NodeId> nodes = value.get(type);
65 if (!nodes.isEmpty()) {
66 return nodes.remove(nodeId);
67 } else {
68 return false;
69 }
70 }
71
72 /**
73 * Reassigns a node from one role to another. If the node was not of the
74 * old role, it will still be assigned the new role.
75 *
76 * @param nodeId the Node ID of node changing roles
77 * @param from the old role
78 * @param to the new role
79 */
80 // might want to add anyways as default behavior
81 public void reassign(NodeId nodeId, MastershipRole from, MastershipRole to) {
82 remove(from, nodeId);
83 add(to, nodeId);
84 }
85
86 /**
87 * Replaces a node in one role with another node. Even if there is no node to
88 * replace, the new node is associated to the role.
89 *
90 * @param from the old NodeId to replace
91 * @param to the new NodeId
92 * @param type the role associated with the old NodeId
93 */
94 // might want to add anyways as default behavior
95 public void replace(NodeId from, NodeId to, MastershipRole type) {
96 remove(type, from);
97 add(type, to);
98 }
99
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700100 @Override
101 public String toString() {
102 final StringBuilder builder = new StringBuilder();
103 for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) {
104 builder.append(el.getKey().toString()).append(": [");
105 for (NodeId n : el.getValue()) {
106 builder.append(n);
107 }
108 builder.append("]\n");
109 }
110 return builder.toString();
111 }
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700112}