blob: 235bd07380ca2b4d4bd1f956e4ce02692efb94c4 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.Map;
19import java.util.Set;
20
21import org.onosproject.cluster.Leadership;
22import org.onosproject.cluster.NodeId;
23
Madan Jampanifc981772016-02-16 09:46:42 -080024
Madan Jampani5e5b3d62016-02-01 16:03:33 -080025import com.google.common.base.MoreObjects;
26import com.google.common.base.Strings;
27
28import io.atomix.catalyst.buffer.BufferInput;
29import io.atomix.catalyst.buffer.BufferOutput;
30import io.atomix.catalyst.serializer.CatalystSerializable;
Madan Jampanifc981772016-02-16 09:46:42 -080031import io.atomix.catalyst.serializer.SerializableTypeResolver;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080032import io.atomix.catalyst.serializer.Serializer;
Madan Jampanifc981772016-02-16 09:46:42 -080033import io.atomix.catalyst.serializer.SerializerRegistry;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080034import io.atomix.catalyst.util.Assert;
Madan Jampani3a9911c2016-02-21 11:25:45 -080035import io.atomix.copycat.Command;
36import io.atomix.copycat.Query;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080037
38/**
39 * {@link AtomixLeaderElector} resource state machine operations.
40 */
41public final class AtomixLeaderElectorCommands {
42
43 private AtomixLeaderElectorCommands() {
44 }
45
46 /**
47 * Abstract election query.
48 */
49 @SuppressWarnings("serial")
50 public abstract static class ElectionQuery<V> implements Query<V>, CatalystSerializable {
51
52 @Override
53 public ConsistencyLevel consistency() {
54 return ConsistencyLevel.BOUNDED_LINEARIZABLE;
55 }
56
57 @Override
58 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
59 }
60
61 @Override
62 public void readObject(BufferInput<?> buffer, Serializer serializer) {
63 }
64 }
65
66 /**
67 * Abstract election topic query.
68 */
69 @SuppressWarnings("serial")
70 public abstract static class TopicQuery<V> extends ElectionQuery<V> implements CatalystSerializable {
71 String topic;
72
73 public TopicQuery() {
74 }
75
76 public TopicQuery(String topic) {
77 this.topic = Assert.notNull(topic, "topic");
78 }
79
80 /**
81 * Returns the topic.
82 * @return topic
83 */
84 public String topic() {
85 return topic;
86 }
87
88 @Override
89 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
90 serializer.writeObject(topic, buffer);
91 }
92
93 @Override
94 public void readObject(BufferInput<?> buffer, Serializer serializer) {
95 topic = serializer.readObject(buffer);
96 }
97 }
98
99 /**
100 * Abstract election command.
101 */
102 @SuppressWarnings("serial")
103 public abstract static class ElectionCommand<V> implements Command<V>, CatalystSerializable {
104
105 @Override
106 public ConsistencyLevel consistency() {
107 return ConsistencyLevel.LINEARIZABLE;
108 }
109
110 @Override
111 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
112 }
113
114 @Override
115 public void readObject(BufferInput<?> buffer, Serializer serializer) {
116 }
117 }
118
119 /**
120 * Listen command.
121 */
122 @SuppressWarnings("serial")
123 public static class Listen extends ElectionCommand<Void> {
124 }
125
126 /**
127 * Unlisten command.
128 */
129 @SuppressWarnings("serial")
130 public static class Unlisten extends ElectionCommand<Void> {
131
132 @Override
133 public CompactionMode compaction() {
134 return CompactionMode.QUORUM;
135 }
136 }
137
138 /**
139 * GetLeader query.
140 */
141 @SuppressWarnings("serial")
142 public static class GetLeadership extends TopicQuery<Leadership> {
143
144 public GetLeadership() {
145 }
146
147 public GetLeadership(String topic) {
148 super(topic);
149 }
150
151 @Override
152 public String toString() {
153 return MoreObjects.toStringHelper(getClass())
154 .add("topic", topic)
155 .toString();
156 }
157 }
158
159 /**
160 * GetAllLeaders query.
161 */
162 @SuppressWarnings("serial")
163 public static class GetAllLeaderships extends ElectionQuery<Map<String, Leadership>> {
164 }
165
166 /**
167 * GetElectedTopics query.
168 */
169 @SuppressWarnings("serial")
170 public static class GetElectedTopics extends ElectionQuery<Set<String>> {
171 private NodeId nodeId;
172
173 public GetElectedTopics() {
174 }
175
176 public GetElectedTopics(NodeId nodeId) {
177 this.nodeId = Assert.argNot(nodeId, nodeId == null, "nodeId cannot be null");
178 }
179
180 /**
181 * Returns the nodeId to check.
182 *
183 * @return The nodeId to check.
184 */
185 public NodeId nodeId() {
186 return nodeId;
187 }
188
189 @Override
190 public String toString() {
191 return MoreObjects.toStringHelper(getClass())
192 .add("nodeId", nodeId)
193 .toString();
194 }
195 }
196
197 /**
198 * Enter and run for leadership.
199 */
200 @SuppressWarnings("serial")
201 public static class Run extends ElectionCommand<Leadership> {
202 private String topic;
203 private NodeId nodeId;
204
205 public Run() {
206 }
207
208 public Run(String topic, NodeId nodeId) {
209 this.topic = Assert.argNot(topic, Strings.isNullOrEmpty(topic), "topic cannot be null or empty");
210 this.nodeId = Assert.argNot(nodeId, nodeId == null, "nodeId cannot be null");
211 }
212
213 /**
214 * Returns the topic.
215 *
216 * @return topic
217 */
218 public String topic() {
219 return topic;
220 }
221
222 /**
223 * Returns the nodeId.
224 *
225 * @return the nodeId
226 */
227 public NodeId nodeId() {
228 return nodeId;
229 }
230
231 @Override
232 public String toString() {
233 return MoreObjects.toStringHelper(getClass())
234 .add("topic", topic)
235 .add("nodeId", nodeId)
236 .toString();
237 }
Madan Jampanifc981772016-02-16 09:46:42 -0800238
239 @Override
240 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
241 buffer.writeString(topic);
242 buffer.writeString(nodeId.toString());
243 }
244
245 @Override
246 public void readObject(BufferInput<?> buffer, Serializer serializer) {
247 topic = buffer.readString();
248 nodeId = new NodeId(buffer.readString());
249 }
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800250 }
251
252 /**
253 * Withdraw from a leadership contest.
254 */
255 @SuppressWarnings("serial")
256 public static class Withdraw extends ElectionCommand<Void> {
257 private String topic;
258
259 public Withdraw() {
260 }
261
262 public Withdraw(String topic) {
263 this.topic = Assert.argNot(topic, Strings.isNullOrEmpty(topic), "topic cannot be null or empty");
264 }
265
266 /**
267 * Returns the topic.
268 *
269 * @return The topic
270 */
271 public String topic() {
272 return topic;
273 }
274
275 @Override
276 public String toString() {
277 return MoreObjects.toStringHelper(getClass())
278 .add("topic", topic)
279 .toString();
280 }
Madan Jampanifc981772016-02-16 09:46:42 -0800281
282 @Override
283 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
284 buffer.writeString(topic);
285 }
286
287 @Override
288 public void readObject(BufferInput<?> buffer, Serializer serializer) {
289 topic = buffer.readString();
290 }
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800291 }
292
293 /**
294 * Command for administratively anointing a node as leader.
295 */
296 @SuppressWarnings("serial")
297 public static class Anoint extends ElectionCommand<Boolean> {
298 private String topic;
299 private NodeId nodeId;
300
301 public Anoint() {
302 }
303
304 public Anoint(String topic, NodeId nodeId) {
305 this.topic = topic;
306 this.nodeId = nodeId;
307 }
308
309 /**
310 * Returns the topic.
311 *
312 * @return The topic
313 */
314 public String topic() {
315 return topic;
316 }
317
318 /**
319 * Returns the nodeId to make leader.
320 *
321 * @return The nodeId
322 */
323 public NodeId nodeId() {
324 return nodeId;
325 }
326
327 @Override
328 public String toString() {
329 return MoreObjects.toStringHelper(getClass())
330 .add("topic", topic)
331 .add("nodeId", nodeId)
332 .toString();
333 }
Madan Jampanifc981772016-02-16 09:46:42 -0800334
335 @Override
336 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
337 buffer.writeString(topic);
338 buffer.writeString(nodeId.toString());
339 }
340
341 @Override
342 public void readObject(BufferInput<?> buffer, Serializer serializer) {
343 topic = buffer.readString();
344 nodeId = new NodeId(buffer.readString());
345 }
346 }
347
348 /**
349 * Map command type resolver.
350 */
351 public static class TypeResolver implements SerializableTypeResolver {
352 @Override
353 public void resolve(SerializerRegistry registry) {
354 registry.register(Run.class, -861);
355 registry.register(Withdraw.class, -862);
356 registry.register(Anoint.class, -863);
357 registry.register(GetAllLeaderships.class, -864);
358 registry.register(GetElectedTopics.class, -865);
359 registry.register(GetLeadership.class, -866);
360 registry.register(Listen.class, -867);
361 registry.register(Unlisten.class, -868);
362 }
Madan Jampani5e5b3d62016-02-01 16:03:33 -0800363 }
364}