blob: a074b944b5f9aa5c25eddd32313f10523f1ef2ee [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 */
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070016package org.onlab.onos.store.mastership.impl;
17
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070018import static org.onlab.onos.net.MastershipRole.MASTER;
19import static org.onlab.onos.net.MastershipRole.NONE;
20import static org.onlab.onos.net.MastershipRole.STANDBY;
21
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070022import java.util.Collections;
Yuta HIGUCHI868def02014-10-23 12:09:43 -070023import java.util.EnumMap;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070024import java.util.LinkedList;
25import java.util.List;
26import java.util.Map;
27
28import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070029import org.onlab.onos.cluster.RoleInfo;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070030import org.onlab.onos.net.MastershipRole;
31
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070032import com.google.common.base.MoreObjects;
33import com.google.common.base.MoreObjects.ToStringHelper;
Yuta HIGUCHI4e348862014-11-04 10:24:26 -080034import com.google.common.collect.Lists;
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070035
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070036/**
37 * A structure that holds node mastership roles associated with a
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070038 * {@link org.onlab.onos.net.DeviceId}. This structure needs to be locked through IMap.
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070039 */
Yuta HIGUCHI868def02014-10-23 12:09:43 -070040final class RoleValue {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070041
Yuta HIGUCHI868def02014-10-23 12:09:43 -070042 protected final Map<MastershipRole, List<NodeId>> value = new EnumMap<>(MastershipRole.class);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070043
Yuta HIGUCHI4e348862014-11-04 10:24:26 -080044 /**
45 * Constructs empty RoleValue.
46 */
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070047 public RoleValue() {
48 value.put(MastershipRole.MASTER, new LinkedList<NodeId>());
49 value.put(MastershipRole.STANDBY, new LinkedList<NodeId>());
50 value.put(MastershipRole.NONE, new LinkedList<NodeId>());
51 }
52
Yuta HIGUCHI4e348862014-11-04 10:24:26 -080053 /**
54 * Constructs copy of specified RoleValue.
55 *
56 * @param original original to create copy from
57 */
58 public RoleValue(final RoleValue original) {
59 value.put(MASTER, Lists.newLinkedList(original.value.get(MASTER)));
60 value.put(STANDBY, Lists.newLinkedList(original.value.get(STANDBY)));
61 value.put(NONE, Lists.newLinkedList(original.value.get(NONE)));
62 }
63
Yuta HIGUCHI868def02014-10-23 12:09:43 -070064 // exposing internals for serialization purpose only
65 Map<MastershipRole, List<NodeId>> value() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070066 return Collections.unmodifiableMap(value);
67 }
68
69 public List<NodeId> nodesOfRole(MastershipRole type) {
70 return value.get(type);
71 }
72
Ayaka Koshibe98bd12f2014-11-01 20:13:37 -070073 /**
74 * Returns the first node to match the MastershipRole, or if there
75 * are none, null.
76 *
77 * @param type the role
78 * @return a node ID or null
79 */
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070080 public NodeId get(MastershipRole type) {
81 return value.get(type).isEmpty() ? null : value.get(type).get(0);
82 }
83
84 public boolean contains(MastershipRole type, NodeId nodeId) {
85 return value.get(type).contains(nodeId);
86 }
87
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070088 public MastershipRole getRole(NodeId nodeId) {
89 if (contains(MASTER, nodeId)) {
90 return MASTER;
91 }
92 if (contains(STANDBY, nodeId)) {
93 return STANDBY;
94 }
95 return NONE;
96 }
97
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070098 /**
99 * Associates a node to a certain role.
100 *
101 * @param type the role
102 * @param nodeId the node ID of the node to associate
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700103 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700104 */
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700105 public boolean add(MastershipRole type, NodeId nodeId) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700106 List<NodeId> nodes = value.get(type);
107
108 if (!nodes.contains(nodeId)) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700109 return nodes.add(nodeId);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700110 }
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700111 return false;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700112 }
113
114 /**
115 * Removes a node from a certain role.
116 *
117 * @param type the role
118 * @param nodeId the ID of the node to remove
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700119 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700120 */
121 public boolean remove(MastershipRole type, NodeId nodeId) {
122 List<NodeId> nodes = value.get(type);
123 if (!nodes.isEmpty()) {
124 return nodes.remove(nodeId);
125 } else {
126 return false;
127 }
128 }
129
130 /**
131 * Reassigns a node from one role to another. If the node was not of the
132 * old role, it will still be assigned the new role.
133 *
134 * @param nodeId the Node ID of node changing roles
135 * @param from the old role
136 * @param to the new role
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700137 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700138 */
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700139 public boolean reassign(NodeId nodeId, MastershipRole from, MastershipRole to) {
140 boolean modified = remove(from, nodeId);
141 modified |= add(to, nodeId);
142 return modified;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700143 }
144
145 /**
146 * Replaces a node in one role with another node. Even if there is no node to
147 * replace, the new node is associated to the role.
148 *
149 * @param from the old NodeId to replace
150 * @param to the new NodeId
151 * @param type the role associated with the old NodeId
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700152 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700153 */
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700154 public boolean replace(NodeId from, NodeId to, MastershipRole type) {
155 boolean modified = remove(type, from);
156 modified |= add(type, to);
157 return modified;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700158 }
159
Ayaka Koshibe67af1f42014-10-20 15:26:37 -0700160 /**
161 * Summarizes this RoleValue as a RoleInfo. Note that master and/or backups
162 * may be empty, so the values should be checked for safety.
163 *
164 * @return the RoleInfo.
165 */
166 public RoleInfo roleInfo() {
167 return new RoleInfo(
168 get(MastershipRole.MASTER), nodesOfRole(MastershipRole.STANDBY));
169 }
170
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700171 @Override
172 public String toString() {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700173 ToStringHelper helper = MoreObjects.toStringHelper(this.getClass());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700174 for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700175 helper.add(el.getKey().toString(), el.getValue());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700176 }
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700177 return helper.toString();
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700178 }
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700179}