blob: 8cc05e8c350feb19484ce4506c396fcbced4e802 [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;
34
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070035/**
36 * A structure that holds node mastership roles associated with a
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070037 * {@link org.onlab.onos.net.DeviceId}. This structure needs to be locked through IMap.
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070038 */
Yuta HIGUCHI868def02014-10-23 12:09:43 -070039final class RoleValue {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070040
Yuta HIGUCHI868def02014-10-23 12:09:43 -070041 protected final Map<MastershipRole, List<NodeId>> value = new EnumMap<>(MastershipRole.class);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070042
43 public RoleValue() {
44 value.put(MastershipRole.MASTER, new LinkedList<NodeId>());
45 value.put(MastershipRole.STANDBY, new LinkedList<NodeId>());
46 value.put(MastershipRole.NONE, new LinkedList<NodeId>());
47 }
48
Yuta HIGUCHI868def02014-10-23 12:09:43 -070049 // exposing internals for serialization purpose only
50 Map<MastershipRole, List<NodeId>> value() {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070051 return Collections.unmodifiableMap(value);
52 }
53
54 public List<NodeId> nodesOfRole(MastershipRole type) {
55 return value.get(type);
56 }
57
58 public NodeId get(MastershipRole type) {
59 return value.get(type).isEmpty() ? null : value.get(type).get(0);
60 }
61
62 public boolean contains(MastershipRole type, NodeId nodeId) {
63 return value.get(type).contains(nodeId);
64 }
65
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070066 public MastershipRole getRole(NodeId nodeId) {
67 if (contains(MASTER, nodeId)) {
68 return MASTER;
69 }
70 if (contains(STANDBY, nodeId)) {
71 return STANDBY;
72 }
73 return NONE;
74 }
75
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070076 /**
77 * Associates a node to a certain role.
78 *
79 * @param type the role
80 * @param nodeId the node ID of the node to associate
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070081 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070082 */
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070083 public boolean add(MastershipRole type, NodeId nodeId) {
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070084 List<NodeId> nodes = value.get(type);
85
86 if (!nodes.contains(nodeId)) {
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070087 return nodes.add(nodeId);
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070088 }
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070089 return false;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070090 }
91
92 /**
93 * Removes a node from a certain role.
94 *
95 * @param type the role
96 * @param nodeId the ID of the node to remove
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -070097 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -070098 */
99 public boolean remove(MastershipRole type, NodeId nodeId) {
100 List<NodeId> nodes = value.get(type);
101 if (!nodes.isEmpty()) {
102 return nodes.remove(nodeId);
103 } else {
104 return false;
105 }
106 }
107
108 /**
109 * Reassigns a node from one role to another. If the node was not of the
110 * old role, it will still be assigned the new role.
111 *
112 * @param nodeId the Node ID of node changing roles
113 * @param from the old role
114 * @param to the new role
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700115 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700116 */
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700117 public boolean reassign(NodeId nodeId, MastershipRole from, MastershipRole to) {
118 boolean modified = remove(from, nodeId);
119 modified |= add(to, nodeId);
120 return modified;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700121 }
122
123 /**
124 * Replaces a node in one role with another node. Even if there is no node to
125 * replace, the new node is associated to the role.
126 *
127 * @param from the old NodeId to replace
128 * @param to the new NodeId
129 * @param type the role associated with the old NodeId
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700130 * @return true if modified
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700131 */
Yuta HIGUCHIb5b71b32014-10-30 02:41:25 -0700132 public boolean replace(NodeId from, NodeId to, MastershipRole type) {
133 boolean modified = remove(type, from);
134 modified |= add(type, to);
135 return modified;
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700136 }
137
Ayaka Koshibe67af1f42014-10-20 15:26:37 -0700138 /**
139 * Summarizes this RoleValue as a RoleInfo. Note that master and/or backups
140 * may be empty, so the values should be checked for safety.
141 *
142 * @return the RoleInfo.
143 */
144 public RoleInfo roleInfo() {
145 return new RoleInfo(
146 get(MastershipRole.MASTER), nodesOfRole(MastershipRole.STANDBY));
147 }
148
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700149 @Override
150 public String toString() {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700151 ToStringHelper helper = MoreObjects.toStringHelper(this.getClass());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700152 for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) {
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700153 helper.add(el.getKey().toString(), el.getValue());
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700154 }
Ayaka Koshibefc981cf2014-10-21 12:44:17 -0700155 return helper.toString();
Ayaka Koshibee8e45352014-10-16 00:37:19 -0700156 }
Ayaka Koshibef9b02fc2014-10-15 17:07:05 -0700157}