blob: 17b8c239e08542fc00448ecbde6fe2068500f33f [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
2 * Copyright 2016-present 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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import com.google.common.base.MoreObjects;
19import io.atomix.protocols.raft.operation.OperationId;
20import io.atomix.protocols.raft.operation.OperationType;
21import org.onlab.util.KryoNamespace;
22import org.onosproject.cluster.NodeId;
23import org.onosproject.store.serializers.KryoNamespaces;
24
25import static com.google.common.base.Preconditions.checkArgument;
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * {@link AtomixLeaderElector} resource state machine operations.
30 */
31public enum AtomixLeaderElectorOperations implements OperationId {
32 ADD_LISTENER("addListener", OperationType.COMMAND),
33 REMOVE_LISTENER("removeListener", OperationType.COMMAND),
34 RUN("run", OperationType.COMMAND),
35 WITHDRAW("withdraw", OperationType.COMMAND),
36 ANOINT("anoint", OperationType.COMMAND),
37 PROMOTE("promote", OperationType.COMMAND),
38 EVICT("evict", OperationType.COMMAND),
39 GET_LEADERSHIP("getLeadership", OperationType.QUERY),
40 GET_ALL_LEADERSHIPS("getAllLeaderships", OperationType.QUERY),
41 GET_ELECTED_TOPICS("getElectedTopics", OperationType.QUERY);
42
43 private final String id;
44 private final OperationType type;
45
46 AtomixLeaderElectorOperations(String id, OperationType type) {
47 this.id = id;
48 this.type = type;
49 }
50
51 @Override
52 public String id() {
53 return id;
54 }
55
56 @Override
57 public OperationType type() {
58 return type;
59 }
60
61 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
62 .register(KryoNamespaces.API)
63 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
64 .register(Run.class)
65 .register(Withdraw.class)
66 .register(Anoint.class)
67 .register(Promote.class)
68 .register(Evict.class)
69 .register(GetLeadership.class)
70 .register(GetElectedTopics.class)
71 .build("AtomixLeaderElectorOperations");
72
73 /**
74 * Abstract election query.
75 */
76 @SuppressWarnings("serial")
77 public abstract static class ElectionOperation {
78 }
79
80 /**
81 * Abstract election topic query.
82 */
83 @SuppressWarnings("serial")
84 public abstract static class TopicOperation extends ElectionOperation {
85 String topic;
86
87 public TopicOperation() {
88 }
89
90 public TopicOperation(String topic) {
91 this.topic = checkNotNull(topic);
92 }
93
94 /**
95 * Returns the topic.
96 * @return topic
97 */
98 public String topic() {
99 return topic;
100 }
101 }
102
103 /**
104 * GetLeader query.
105 */
106 @SuppressWarnings("serial")
107 public static class GetLeadership extends TopicOperation {
108
109 public GetLeadership() {
110 }
111
112 public GetLeadership(String topic) {
113 super(topic);
114 }
115
116 @Override
117 public String toString() {
118 return MoreObjects.toStringHelper(getClass())
119 .add("topic", topic)
120 .toString();
121 }
122 }
123
124 /**
125 * GetElectedTopics query.
126 */
127 @SuppressWarnings("serial")
128 public static class GetElectedTopics extends ElectionOperation {
129 private NodeId nodeId;
130
131 public GetElectedTopics() {
132 }
133
134 public GetElectedTopics(NodeId nodeId) {
135 checkArgument(nodeId != null, "nodeId cannot be null");
136 this.nodeId = nodeId;
137 }
138
139 /**
140 * Returns the nodeId to check.
141 *
142 * @return The nodeId to check.
143 */
144 public NodeId nodeId() {
145 return nodeId;
146 }
147
148 @Override
149 public String toString() {
150 return MoreObjects.toStringHelper(getClass())
151 .add("nodeId", nodeId)
152 .toString();
153 }
154 }
155
156 /**
157 * Enter and run for leadership.
158 */
159 @SuppressWarnings("serial")
160 public static class Run extends ElectionOperation {
161 private String topic;
162 private NodeId nodeId;
163
164 public Run() {
165 }
166
167 public Run(String topic, NodeId nodeId) {
168 this.topic = topic;
169 this.nodeId = nodeId;
170 }
171
172 /**
173 * Returns the topic.
174 *
175 * @return topic
176 */
177 public String topic() {
178 return topic;
179 }
180
181 /**
182 * Returns the nodeId.
183 *
184 * @return the nodeId
185 */
186 public NodeId nodeId() {
187 return nodeId;
188 }
189
190 @Override
191 public String toString() {
192 return MoreObjects.toStringHelper(getClass())
193 .add("topic", topic)
194 .add("nodeId", nodeId)
195 .toString();
196 }
197 }
198
199 /**
200 * Withdraw from a leadership contest.
201 */
202 @SuppressWarnings("serial")
203 public static class Withdraw extends ElectionOperation {
204 private String topic;
205
206 public Withdraw() {
207 }
208
209 public Withdraw(String topic) {
210 this.topic = topic;
211 }
212
213 /**
214 * Returns the topic.
215 *
216 * @return The topic
217 */
218 public String topic() {
219 return topic;
220 }
221
222 @Override
223 public String toString() {
224 return MoreObjects.toStringHelper(getClass())
225 .add("topic", topic)
226 .toString();
227 }
228 }
229
230 /**
231 * Command for administratively changing the leadership state for a node.
232 */
233 @SuppressWarnings("serial")
234 public abstract static class ElectionChangeOperation extends ElectionOperation {
235 private String topic;
236 private NodeId nodeId;
237
238 ElectionChangeOperation() {
239 topic = null;
240 nodeId = null;
241 }
242
243 public ElectionChangeOperation(String topic, NodeId nodeId) {
244 this.topic = topic;
245 this.nodeId = nodeId;
246 }
247
248 /**
249 * Returns the topic.
250 *
251 * @return The topic
252 */
253 public String topic() {
254 return topic;
255 }
256
257 /**
258 * Returns the nodeId to make leader.
259 *
260 * @return The nodeId
261 */
262 public NodeId nodeId() {
263 return nodeId;
264 }
265
266 @Override
267 public String toString() {
268 return MoreObjects.toStringHelper(getClass())
269 .add("topic", topic)
270 .add("nodeId", nodeId)
271 .toString();
272 }
273 }
274
275 /**
276 * Command for administratively anoint a node as leader.
277 */
278 @SuppressWarnings("serial")
279 public static class Anoint extends ElectionChangeOperation {
280
281 private Anoint() {
282 }
283
284 public Anoint(String topic, NodeId nodeId) {
285 super(topic, nodeId);
286 }
287 }
288
289 /**
290 * Command for administratively promote a node as top candidate.
291 */
292 @SuppressWarnings("serial")
293 public static class Promote extends ElectionChangeOperation {
294
295 private Promote() {
296 }
297
298 public Promote(String topic, NodeId nodeId) {
299 super(topic, nodeId);
300 }
301 }
302
303 /**
304 * Command for administratively evicting a node from all leadership topics.
305 */
306 @SuppressWarnings("serial")
307 public static class Evict extends ElectionOperation {
308 private NodeId nodeId;
309
310 public Evict() {
311 }
312
313 public Evict(NodeId nodeId) {
314 this.nodeId = nodeId;
315 }
316
317 /**
318 * Returns the node identifier.
319 *
320 * @return The nodeId
321 */
322 public NodeId nodeId() {
323 return nodeId;
324 }
325
326 @Override
327 public String toString() {
328 return MoreObjects.toStringHelper(getClass())
329 .add("nodeId", nodeId)
330 .toString();
331 }
332 }
333}