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