Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.mastership.impl; |
| 17 | |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 18 | import static org.onlab.onos.net.MastershipRole.MASTER; |
| 19 | import static org.onlab.onos.net.MastershipRole.NONE; |
| 20 | import static org.onlab.onos.net.MastershipRole.STANDBY; |
| 21 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 22 | import java.util.Collections; |
Yuta HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 23 | import java.util.EnumMap; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 24 | import java.util.LinkedList; |
| 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import org.onlab.onos.cluster.NodeId; |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 29 | import org.onlab.onos.cluster.RoleInfo; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.MastershipRole; |
| 31 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 32 | import com.google.common.base.MoreObjects; |
| 33 | import com.google.common.base.MoreObjects.ToStringHelper; |
| 34 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 35 | /** |
| 36 | * A structure that holds node mastership roles associated with a |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 37 | * {@link org.onlab.onos.net.DeviceId}. This structure needs to be locked through IMap. |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 38 | */ |
Yuta HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 39 | final class RoleValue { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 41 | protected final Map<MastershipRole, List<NodeId>> value = new EnumMap<>(MastershipRole.class); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 42 | |
| 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 HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 49 | // exposing internals for serialization purpose only |
| 50 | Map<MastershipRole, List<NodeId>> value() { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 51 | 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 66 | 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 Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 76 | /** |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 81 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 82 | */ |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 83 | public boolean add(MastershipRole type, NodeId nodeId) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 84 | List<NodeId> nodes = value.get(type); |
| 85 | |
| 86 | if (!nodes.contains(nodeId)) { |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 87 | return nodes.add(nodeId); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 88 | } |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 89 | return false; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 90 | } |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 97 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 98 | */ |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 115 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 116 | */ |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 117 | public boolean reassign(NodeId nodeId, MastershipRole from, MastershipRole to) { |
| 118 | boolean modified = remove(from, nodeId); |
| 119 | modified |= add(to, nodeId); |
| 120 | return modified; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 121 | } |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 130 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 131 | */ |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 132 | public boolean replace(NodeId from, NodeId to, MastershipRole type) { |
| 133 | boolean modified = remove(type, from); |
| 134 | modified |= add(type, to); |
| 135 | return modified; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 138 | /** |
| 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 Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 149 | @Override |
| 150 | public String toString() { |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 151 | ToStringHelper helper = MoreObjects.toStringHelper(this.getClass()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 152 | for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) { |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 153 | helper.add(el.getKey().toString(), el.getValue()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 154 | } |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 155 | return helper.toString(); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 156 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 157 | } |