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; |
Yuta HIGUCHI | 4e34886 | 2014-11-04 10:24:26 -0800 | [diff] [blame] | 34 | import com.google.common.collect.Lists; |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 35 | |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 36 | /** |
| 37 | * A structure that holds node mastership roles associated with a |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 38 | * {@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] | 39 | */ |
Yuta HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 40 | final class RoleValue { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 42 | protected final Map<MastershipRole, List<NodeId>> value = new EnumMap<>(MastershipRole.class); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 43 | |
Yuta HIGUCHI | 4e34886 | 2014-11-04 10:24:26 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Constructs empty RoleValue. |
| 46 | */ |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 47 | 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 HIGUCHI | 4e34886 | 2014-11-04 10:24:26 -0800 | [diff] [blame] | 53 | /** |
| 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 HIGUCHI | 868def0 | 2014-10-23 12:09:43 -0700 | [diff] [blame] | 64 | // exposing internals for serialization purpose only |
| 65 | Map<MastershipRole, List<NodeId>> value() { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 66 | return Collections.unmodifiableMap(value); |
| 67 | } |
| 68 | |
| 69 | public List<NodeId> nodesOfRole(MastershipRole type) { |
| 70 | return value.get(type); |
| 71 | } |
| 72 | |
Ayaka Koshibe | 98bd12f | 2014-11-01 20:13:37 -0700 | [diff] [blame] | 73 | /** |
| 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 Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 80 | 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 88 | 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 Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 98 | /** |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 103 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 104 | */ |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 105 | public boolean add(MastershipRole type, NodeId nodeId) { |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 106 | List<NodeId> nodes = value.get(type); |
| 107 | |
| 108 | if (!nodes.contains(nodeId)) { |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 109 | return nodes.add(nodeId); |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 110 | } |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 111 | return false; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 112 | } |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 119 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 120 | */ |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 137 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 138 | */ |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 139 | public boolean reassign(NodeId nodeId, MastershipRole from, MastershipRole to) { |
| 140 | boolean modified = remove(from, nodeId); |
| 141 | modified |= add(to, nodeId); |
| 142 | return modified; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 143 | } |
| 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 HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 152 | * @return true if modified |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 153 | */ |
Yuta HIGUCHI | b5b71b3 | 2014-10-30 02:41:25 -0700 | [diff] [blame] | 154 | public boolean replace(NodeId from, NodeId to, MastershipRole type) { |
| 155 | boolean modified = remove(type, from); |
| 156 | modified |= add(type, to); |
| 157 | return modified; |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ayaka Koshibe | 67af1f4 | 2014-10-20 15:26:37 -0700 | [diff] [blame] | 160 | /** |
| 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 Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 171 | @Override |
| 172 | public String toString() { |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 173 | ToStringHelper helper = MoreObjects.toStringHelper(this.getClass()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 174 | for (Map.Entry<MastershipRole, List<NodeId>> el : value.entrySet()) { |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 175 | helper.add(el.getKey().toString(), el.getValue()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 176 | } |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 177 | return helper.toString(); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 178 | } |
Ayaka Koshibe | f9b02fc | 2014-10-15 17:07:05 -0700 | [diff] [blame] | 179 | } |