Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.Set; |
| 20 | |
| 21 | import org.onosproject.cluster.Leadership; |
| 22 | import org.onosproject.cluster.NodeId; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | import com.google.common.base.Strings; |
| 26 | |
| 27 | import io.atomix.catalyst.buffer.BufferInput; |
| 28 | import io.atomix.catalyst.buffer.BufferOutput; |
| 29 | import io.atomix.catalyst.serializer.CatalystSerializable; |
| 30 | import io.atomix.catalyst.serializer.Serializer; |
| 31 | import io.atomix.catalyst.util.Assert; |
| 32 | import io.atomix.copycat.client.Command; |
| 33 | import io.atomix.copycat.client.Query; |
| 34 | |
| 35 | /** |
| 36 | * {@link AtomixLeaderElector} resource state machine operations. |
| 37 | */ |
| 38 | public final class AtomixLeaderElectorCommands { |
| 39 | |
| 40 | private AtomixLeaderElectorCommands() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Abstract election query. |
| 45 | */ |
| 46 | @SuppressWarnings("serial") |
| 47 | public abstract static class ElectionQuery<V> implements Query<V>, CatalystSerializable { |
| 48 | |
| 49 | @Override |
| 50 | public ConsistencyLevel consistency() { |
| 51 | return ConsistencyLevel.BOUNDED_LINEARIZABLE; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Abstract election topic query. |
| 65 | */ |
| 66 | @SuppressWarnings("serial") |
| 67 | public abstract static class TopicQuery<V> extends ElectionQuery<V> implements CatalystSerializable { |
| 68 | String topic; |
| 69 | |
| 70 | public TopicQuery() { |
| 71 | } |
| 72 | |
| 73 | public TopicQuery(String topic) { |
| 74 | this.topic = Assert.notNull(topic, "topic"); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the topic. |
| 79 | * @return topic |
| 80 | */ |
| 81 | public String topic() { |
| 82 | return topic; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 87 | serializer.writeObject(topic, buffer); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 92 | topic = serializer.readObject(buffer); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Abstract election command. |
| 98 | */ |
| 99 | @SuppressWarnings("serial") |
| 100 | public abstract static class ElectionCommand<V> implements Command<V>, CatalystSerializable { |
| 101 | |
| 102 | @Override |
| 103 | public ConsistencyLevel consistency() { |
| 104 | return ConsistencyLevel.LINEARIZABLE; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Listen command. |
| 118 | */ |
| 119 | @SuppressWarnings("serial") |
| 120 | public static class Listen extends ElectionCommand<Void> { |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Unlisten command. |
| 125 | */ |
| 126 | @SuppressWarnings("serial") |
| 127 | public static class Unlisten extends ElectionCommand<Void> { |
| 128 | |
| 129 | @Override |
| 130 | public CompactionMode compaction() { |
| 131 | return CompactionMode.QUORUM; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * GetLeader query. |
| 137 | */ |
| 138 | @SuppressWarnings("serial") |
| 139 | public static class GetLeadership extends TopicQuery<Leadership> { |
| 140 | |
| 141 | public GetLeadership() { |
| 142 | } |
| 143 | |
| 144 | public GetLeadership(String topic) { |
| 145 | super(topic); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public String toString() { |
| 150 | return MoreObjects.toStringHelper(getClass()) |
| 151 | .add("topic", topic) |
| 152 | .toString(); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * GetAllLeaders query. |
| 158 | */ |
| 159 | @SuppressWarnings("serial") |
| 160 | public static class GetAllLeaderships extends ElectionQuery<Map<String, Leadership>> { |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * GetElectedTopics query. |
| 165 | */ |
| 166 | @SuppressWarnings("serial") |
| 167 | public static class GetElectedTopics extends ElectionQuery<Set<String>> { |
| 168 | private NodeId nodeId; |
| 169 | |
| 170 | public GetElectedTopics() { |
| 171 | } |
| 172 | |
| 173 | public GetElectedTopics(NodeId nodeId) { |
| 174 | this.nodeId = Assert.argNot(nodeId, nodeId == null, "nodeId cannot be null"); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the nodeId to check. |
| 179 | * |
| 180 | * @return The nodeId to check. |
| 181 | */ |
| 182 | public NodeId nodeId() { |
| 183 | return nodeId; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String toString() { |
| 188 | return MoreObjects.toStringHelper(getClass()) |
| 189 | .add("nodeId", nodeId) |
| 190 | .toString(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Enter and run for leadership. |
| 196 | */ |
| 197 | @SuppressWarnings("serial") |
| 198 | public static class Run extends ElectionCommand<Leadership> { |
| 199 | private String topic; |
| 200 | private NodeId nodeId; |
| 201 | |
| 202 | public Run() { |
| 203 | } |
| 204 | |
| 205 | public Run(String topic, NodeId nodeId) { |
| 206 | this.topic = Assert.argNot(topic, Strings.isNullOrEmpty(topic), "topic cannot be null or empty"); |
| 207 | this.nodeId = Assert.argNot(nodeId, nodeId == null, "nodeId cannot be null"); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns the topic. |
| 212 | * |
| 213 | * @return topic |
| 214 | */ |
| 215 | public String topic() { |
| 216 | return topic; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns the nodeId. |
| 221 | * |
| 222 | * @return the nodeId |
| 223 | */ |
| 224 | public NodeId nodeId() { |
| 225 | return nodeId; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public String toString() { |
| 230 | return MoreObjects.toStringHelper(getClass()) |
| 231 | .add("topic", topic) |
| 232 | .add("nodeId", nodeId) |
| 233 | .toString(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Withdraw from a leadership contest. |
| 239 | */ |
| 240 | @SuppressWarnings("serial") |
| 241 | public static class Withdraw extends ElectionCommand<Void> { |
| 242 | private String topic; |
| 243 | |
| 244 | public Withdraw() { |
| 245 | } |
| 246 | |
| 247 | public Withdraw(String topic) { |
| 248 | this.topic = Assert.argNot(topic, Strings.isNullOrEmpty(topic), "topic cannot be null or empty"); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Returns the topic. |
| 253 | * |
| 254 | * @return The topic |
| 255 | */ |
| 256 | public String topic() { |
| 257 | return topic; |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public String toString() { |
| 262 | return MoreObjects.toStringHelper(getClass()) |
| 263 | .add("topic", topic) |
| 264 | .toString(); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Command for administratively anointing a node as leader. |
| 270 | */ |
| 271 | @SuppressWarnings("serial") |
| 272 | public static class Anoint extends ElectionCommand<Boolean> { |
| 273 | private String topic; |
| 274 | private NodeId nodeId; |
| 275 | |
| 276 | public Anoint() { |
| 277 | } |
| 278 | |
| 279 | public Anoint(String topic, NodeId nodeId) { |
| 280 | this.topic = topic; |
| 281 | this.nodeId = nodeId; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Returns the topic. |
| 286 | * |
| 287 | * @return The topic |
| 288 | */ |
| 289 | public String topic() { |
| 290 | return topic; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Returns the nodeId to make leader. |
| 295 | * |
| 296 | * @return The nodeId |
| 297 | */ |
| 298 | public NodeId nodeId() { |
| 299 | return nodeId; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public String toString() { |
| 304 | return MoreObjects.toStringHelper(getClass()) |
| 305 | .add("topic", topic) |
| 306 | .add("nodeId", nodeId) |
| 307 | .toString(); |
| 308 | } |
| 309 | } |
| 310 | } |