blob: d05215cdfeb62ad189e4e84c33e7eecd169c5b1c [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 */
16
17package org.onosproject.store.primitives.resources.impl;
18
Jordan Haltermand0d80352017-08-10 15:08:27 -070019import java.util.LinkedHashMap;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020import java.util.Optional;
21
22import com.google.common.base.MoreObjects;
23import io.atomix.protocols.raft.operation.OperationId;
24import io.atomix.protocols.raft.operation.OperationType;
25import org.onlab.util.KryoNamespace;
26import org.onlab.util.Match;
Sithara Punnassery61a80252017-08-07 11:16:08 -070027import org.onosproject.store.primitives.NodeUpdate;
28import org.onosproject.store.primitives.TransactionId;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070029import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.DocumentPath;
Sithara Punnassery61a80252017-08-07 11:16:08 -070031import org.onosproject.store.service.TransactionLog;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070032import org.onosproject.store.service.Versioned;
33
34/**
35 * {@link AtomixDocumentTree} resource state machine operations.
36 */
37public enum AtomixDocumentTreeOperations implements OperationId {
38 ADD_LISTENER("set", OperationType.COMMAND),
39 REMOVE_LISTENER("compareAndSet", OperationType.COMMAND),
40 GET("incrementAndGet", OperationType.QUERY),
41 GET_CHILDREN("getAndIncrement", OperationType.QUERY),
42 UPDATE("addAndGet", OperationType.COMMAND),
Sithara Punnassery61a80252017-08-07 11:16:08 -070043 CLEAR("getAndAdd", OperationType.COMMAND),
44 BEGIN("begin", OperationType.COMMAND),
45 PREPARE("prepare", OperationType.COMMAND),
46 PREPARE_AND_COMMIT("prepareAndCommit", OperationType.COMMAND),
47 COMMIT("commit", OperationType.COMMAND),
48 ROLLBACK("rollback", OperationType.COMMAND);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070049
50 private final String id;
51 private final OperationType type;
52
53 AtomixDocumentTreeOperations(String id, OperationType type) {
54 this.id = id;
55 this.type = type;
56 }
57
58 @Override
59 public String id() {
60 return id;
61 }
62
63 @Override
64 public OperationType type() {
65 return type;
66 }
67
68 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
69 .register(KryoNamespaces.BASIC)
70 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Jordan Haltermand0d80352017-08-10 15:08:27 -070071 .register(LinkedHashMap.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070072 .register(Listen.class)
73 .register(Unlisten.class)
74 .register(Get.class)
75 .register(GetChildren.class)
76 .register(Update.class)
Sithara Punnassery61a80252017-08-07 11:16:08 -070077 .register(TransactionBegin.class)
78 .register(TransactionPrepare.class)
79 .register(TransactionPrepareAndCommit.class)
80 .register(TransactionCommit.class)
81 .register(TransactionRollback.class)
82 .register(TransactionId.class)
83 .register(TransactionLog.class)
84 .register(PrepareResult.class)
85 .register(CommitResult.class)
86 .register(RollbackResult.class)
87 .register(NodeUpdate.class)
88 .register(NodeUpdate.Type.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070089 .register(DocumentPath.class)
90 .register(Match.class)
91 .register(Versioned.class)
Jordan Haltermane853d032017-08-01 15:10:28 -070092 .register(DocumentTreeResult.class)
93 .register(DocumentTreeResult.Status.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070094 .build("AtomixDocumentTreeOperations");
95
96 /**
Sithara Punnassery61a80252017-08-07 11:16:08 -070097 * Base class for document tree operations.
98 */
99 public abstract static class DocumentTreeOperation {
100 }
101
102 /**
103 * Base class for document tree operations that serialize a {@link DocumentPath}.
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700104 */
105 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700106 public abstract static class PathOperation extends DocumentTreeOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700107 private DocumentPath path;
108
Sithara Punnassery61a80252017-08-07 11:16:08 -0700109 PathOperation(DocumentPath path) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700110 this.path = path;
111 }
112
113 public DocumentPath path() {
114 return path;
115 }
116 }
117
118 /**
119 * DocumentTree#get query.
120 */
121 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700122 public static class Get extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700123 public Get() {
124 super(null);
125 }
126
127 public Get(DocumentPath path) {
128 super(path);
129 }
130
131 @Override
132 public String toString() {
133 return MoreObjects.toStringHelper(getClass())
134 .add("path", path())
135 .toString();
136 }
137 }
138
139 /**
140 * DocumentTree#getChildren query.
141 */
142 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700143 public static class GetChildren extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700144 public GetChildren() {
145 super(null);
146 }
147
148 public GetChildren(DocumentPath path) {
149 super(path);
150 }
151
152 @Override
153 public String toString() {
154 return MoreObjects.toStringHelper(getClass())
155 .add("path", path())
156 .toString();
157 }
158 }
159
160 /**
161 * DocumentTree update command.
162 */
163 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700164 public static class Update extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700165 private Optional<byte[]> value;
166 private Match<byte[]> valueMatch;
167 private Match<Long> versionMatch;
168
169 public Update() {
170 super(null);
171 this.value = null;
172 this.valueMatch = null;
173 this.versionMatch = null;
174 }
175
176 public Update(DocumentPath path, Optional<byte[]> value, Match<byte[]> valueMatch, Match<Long> versionMatch) {
177 super(path);
178 this.value = value;
179 this.valueMatch = valueMatch;
180 this.versionMatch = versionMatch;
181 }
182
183 public Optional<byte[]> value() {
184 return value;
185 }
186
187 public Match<byte[]> valueMatch() {
188 return valueMatch;
189 }
190
191 public Match<Long> versionMatch() {
192 return versionMatch;
193 }
194
195 @Override
196 public String toString() {
197 return MoreObjects.toStringHelper(getClass())
198 .add("path", path())
199 .add("value", value)
200 .add("valueMatch", valueMatch)
201 .add("versionMatch", versionMatch)
202 .toString();
203 }
204 }
205
206 /**
207 * Change listen.
208 */
209 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700210 public static class Listen extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700211 public Listen() {
212 this(DocumentPath.from("root"));
213 }
214
215 public Listen(DocumentPath path) {
216 super(path);
217 }
218
219 @Override
220 public String toString() {
221 return MoreObjects.toStringHelper(getClass())
222 .add("path", path())
223 .toString();
224 }
225 }
226
227 /**
228 * Change unlisten.
229 */
230 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700231 public static class Unlisten extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700232 public Unlisten() {
233 this(DocumentPath.from("root"));
234 }
235
236 public Unlisten(DocumentPath path) {
237 super(path);
238 }
239
240 @Override
241 public String toString() {
242 return MoreObjects.toStringHelper(getClass())
243 .add("path", path())
244 .toString();
245 }
246 }
Sithara Punnassery61a80252017-08-07 11:16:08 -0700247
248 /**
249 * Transaction begin command.
250 */
251 public static class TransactionBegin extends PathOperation {
252 private TransactionId transactionId;
253
254 public TransactionBegin() {
255 super(null);
256 }
257
258 public TransactionBegin(TransactionId transactionId) {
259 super(DocumentPath.from(transactionId.toString()));
260 this.transactionId = transactionId;
261 }
262
263 public TransactionId transactionId() {
264 return transactionId;
265 }
266 }
267
268 /**
269 * Transaction prepare command.
270 */
271 @SuppressWarnings("serial")
272 public static class TransactionPrepare extends PathOperation {
273 private TransactionLog<NodeUpdate<byte[]>> transactionLog;
274
275 public TransactionPrepare() {
276 super(null);
277 }
278
279 public TransactionPrepare(TransactionLog<NodeUpdate<byte[]>> transactionLog) {
280 super(DocumentPath.from(transactionLog.transactionId().toString()));
281 this.transactionLog = transactionLog;
282 }
283
284 public TransactionLog<NodeUpdate<byte[]>> transactionLog() {
285 return transactionLog;
286 }
287
288 @Override
289 public String toString() {
290 return MoreObjects.toStringHelper(getClass())
291 .add("transactionLog", transactionLog)
292 .toString();
293 }
294 }
295
296 /**
297 * Transaction prepareAndCommit command.
298 */
299 @SuppressWarnings("serial")
300 public static class TransactionPrepareAndCommit extends TransactionPrepare {
301 public TransactionPrepareAndCommit() {
302 }
303
304 public TransactionPrepareAndCommit(TransactionLog<NodeUpdate<byte[]>> transactionLog) {
305 super(transactionLog);
306 }
307 }
308
309 /**
310 * Transaction commit command.
311 */
312 @SuppressWarnings("serial")
313 public static class TransactionCommit extends PathOperation {
314 private TransactionId transactionId;
315
316 public TransactionCommit() {
317 super(null);
318 }
319
320 public TransactionCommit(TransactionId transactionId) {
321 super(DocumentPath.from(transactionId.toString()));
322 this.transactionId = transactionId;
323 }
324
325 /**
326 * Returns the transaction identifier.
327 * @return transaction id
328 */
329 public TransactionId transactionId() {
330 return transactionId;
331 }
332
333 @Override
334 public String toString() {
335 return MoreObjects.toStringHelper(getClass())
336 .add("transactionId", transactionId)
337 .toString();
338 }
339 }
340
341 /**
342 * Transaction rollback command.
343 */
344 @SuppressWarnings("serial")
345 public static class TransactionRollback extends PathOperation {
346 private TransactionId transactionId;
347
348 public TransactionRollback() {
349 super(null);
350 }
351
352 public TransactionRollback(TransactionId transactionId) {
353 super(DocumentPath.from(transactionId.toString()));
354 this.transactionId = transactionId;
355 }
356
357 /**
358 * Returns the transaction identifier.
359 * @return transaction id
360 */
361 public TransactionId transactionId() {
362 return transactionId;
363 }
364
365 @Override
366 public String toString() {
367 return MoreObjects.toStringHelper(getClass())
368 .add("transactionId", transactionId)
369 .toString();
370 }
371 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700372}