blob: 6ec6091ab7844ac20ab9bded2138591bb18ca249 [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 {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080038 ADD_LISTENER(OperationType.COMMAND),
39 REMOVE_LISTENER(OperationType.COMMAND),
40 GET(OperationType.QUERY),
41 GET_CHILDREN(OperationType.QUERY),
42 UPDATE(OperationType.COMMAND),
43 CLEAR(OperationType.COMMAND),
44 BEGIN(OperationType.COMMAND),
45 PREPARE(OperationType.COMMAND),
46 PREPARE_AND_COMMIT(OperationType.COMMAND),
47 COMMIT(OperationType.COMMAND),
48 ROLLBACK(OperationType.COMMAND);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070049
Jordan Halterman2bf177c2017-06-29 01:49:08 -070050 private final OperationType type;
51
Jordan Halterman2b7501c2017-11-29 14:05:33 -080052 AtomixDocumentTreeOperations(OperationType type) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070053 this.type = type;
54 }
55
56 @Override
57 public String id() {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080058 return name();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070059 }
60
61 @Override
62 public OperationType type() {
63 return type;
64 }
65
66 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
67 .register(KryoNamespaces.BASIC)
68 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Jordan Haltermand0d80352017-08-10 15:08:27 -070069 .register(LinkedHashMap.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070070 .register(Listen.class)
71 .register(Unlisten.class)
72 .register(Get.class)
73 .register(GetChildren.class)
74 .register(Update.class)
Sithara Punnassery61a80252017-08-07 11:16:08 -070075 .register(TransactionBegin.class)
76 .register(TransactionPrepare.class)
77 .register(TransactionPrepareAndCommit.class)
78 .register(TransactionCommit.class)
79 .register(TransactionRollback.class)
80 .register(TransactionId.class)
81 .register(TransactionLog.class)
82 .register(PrepareResult.class)
83 .register(CommitResult.class)
84 .register(RollbackResult.class)
85 .register(NodeUpdate.class)
86 .register(NodeUpdate.Type.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070087 .register(DocumentPath.class)
88 .register(Match.class)
89 .register(Versioned.class)
Jordan Haltermane853d032017-08-01 15:10:28 -070090 .register(DocumentTreeResult.class)
91 .register(DocumentTreeResult.Status.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070092 .build("AtomixDocumentTreeOperations");
93
94 /**
Sithara Punnassery61a80252017-08-07 11:16:08 -070095 * Base class for document tree operations.
96 */
97 public abstract static class DocumentTreeOperation {
98 }
99
100 /**
101 * Base class for document tree operations that serialize a {@link DocumentPath}.
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700102 */
103 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700104 public abstract static class PathOperation extends DocumentTreeOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700105 private DocumentPath path;
106
Sithara Punnassery61a80252017-08-07 11:16:08 -0700107 PathOperation(DocumentPath path) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700108 this.path = path;
109 }
110
111 public DocumentPath path() {
112 return path;
113 }
114 }
115
116 /**
117 * DocumentTree#get query.
118 */
119 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700120 public static class Get extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700121 public Get() {
122 super(null);
123 }
124
125 public Get(DocumentPath path) {
126 super(path);
127 }
128
129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(getClass())
132 .add("path", path())
133 .toString();
134 }
135 }
136
137 /**
138 * DocumentTree#getChildren query.
139 */
140 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700141 public static class GetChildren extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700142 public GetChildren() {
143 super(null);
144 }
145
146 public GetChildren(DocumentPath path) {
147 super(path);
148 }
149
150 @Override
151 public String toString() {
152 return MoreObjects.toStringHelper(getClass())
153 .add("path", path())
154 .toString();
155 }
156 }
157
158 /**
159 * DocumentTree update command.
160 */
161 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700162 public static class Update extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700163 private Optional<byte[]> value;
164 private Match<byte[]> valueMatch;
165 private Match<Long> versionMatch;
166
167 public Update() {
168 super(null);
169 this.value = null;
170 this.valueMatch = null;
171 this.versionMatch = null;
172 }
173
174 public Update(DocumentPath path, Optional<byte[]> value, Match<byte[]> valueMatch, Match<Long> versionMatch) {
175 super(path);
176 this.value = value;
177 this.valueMatch = valueMatch;
178 this.versionMatch = versionMatch;
179 }
180
181 public Optional<byte[]> value() {
182 return value;
183 }
184
185 public Match<byte[]> valueMatch() {
186 return valueMatch;
187 }
188
189 public Match<Long> versionMatch() {
190 return versionMatch;
191 }
192
193 @Override
194 public String toString() {
195 return MoreObjects.toStringHelper(getClass())
196 .add("path", path())
197 .add("value", value)
198 .add("valueMatch", valueMatch)
199 .add("versionMatch", versionMatch)
200 .toString();
201 }
202 }
203
204 /**
205 * Change listen.
206 */
207 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700208 public static class Listen extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700209 public Listen() {
210 this(DocumentPath.from("root"));
211 }
212
213 public Listen(DocumentPath path) {
214 super(path);
215 }
216
217 @Override
218 public String toString() {
219 return MoreObjects.toStringHelper(getClass())
220 .add("path", path())
221 .toString();
222 }
223 }
224
225 /**
226 * Change unlisten.
227 */
228 @SuppressWarnings("serial")
Sithara Punnassery61a80252017-08-07 11:16:08 -0700229 public static class Unlisten extends PathOperation {
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700230 public Unlisten() {
231 this(DocumentPath.from("root"));
232 }
233
234 public Unlisten(DocumentPath path) {
235 super(path);
236 }
237
238 @Override
239 public String toString() {
240 return MoreObjects.toStringHelper(getClass())
241 .add("path", path())
242 .toString();
243 }
244 }
Sithara Punnassery61a80252017-08-07 11:16:08 -0700245
246 /**
247 * Transaction begin command.
248 */
249 public static class TransactionBegin extends PathOperation {
250 private TransactionId transactionId;
251
252 public TransactionBegin() {
253 super(null);
254 }
255
256 public TransactionBegin(TransactionId transactionId) {
257 super(DocumentPath.from(transactionId.toString()));
258 this.transactionId = transactionId;
259 }
260
261 public TransactionId transactionId() {
262 return transactionId;
263 }
264 }
265
266 /**
267 * Transaction prepare command.
268 */
269 @SuppressWarnings("serial")
270 public static class TransactionPrepare extends PathOperation {
271 private TransactionLog<NodeUpdate<byte[]>> transactionLog;
272
273 public TransactionPrepare() {
274 super(null);
275 }
276
277 public TransactionPrepare(TransactionLog<NodeUpdate<byte[]>> transactionLog) {
278 super(DocumentPath.from(transactionLog.transactionId().toString()));
279 this.transactionLog = transactionLog;
280 }
281
282 public TransactionLog<NodeUpdate<byte[]>> transactionLog() {
283 return transactionLog;
284 }
285
286 @Override
287 public String toString() {
288 return MoreObjects.toStringHelper(getClass())
289 .add("transactionLog", transactionLog)
290 .toString();
291 }
292 }
293
294 /**
295 * Transaction prepareAndCommit command.
296 */
297 @SuppressWarnings("serial")
298 public static class TransactionPrepareAndCommit extends TransactionPrepare {
299 public TransactionPrepareAndCommit() {
300 }
301
302 public TransactionPrepareAndCommit(TransactionLog<NodeUpdate<byte[]>> transactionLog) {
303 super(transactionLog);
304 }
305 }
306
307 /**
308 * Transaction commit command.
309 */
310 @SuppressWarnings("serial")
311 public static class TransactionCommit extends PathOperation {
312 private TransactionId transactionId;
313
314 public TransactionCommit() {
315 super(null);
316 }
317
318 public TransactionCommit(TransactionId transactionId) {
319 super(DocumentPath.from(transactionId.toString()));
320 this.transactionId = transactionId;
321 }
322
323 /**
324 * Returns the transaction identifier.
325 * @return transaction id
326 */
327 public TransactionId transactionId() {
328 return transactionId;
329 }
330
331 @Override
332 public String toString() {
333 return MoreObjects.toStringHelper(getClass())
334 .add("transactionId", transactionId)
335 .toString();
336 }
337 }
338
339 /**
340 * Transaction rollback command.
341 */
342 @SuppressWarnings("serial")
343 public static class TransactionRollback extends PathOperation {
344 private TransactionId transactionId;
345
346 public TransactionRollback() {
347 super(null);
348 }
349
350 public TransactionRollback(TransactionId transactionId) {
351 super(DocumentPath.from(transactionId.toString()));
352 this.transactionId = transactionId;
353 }
354
355 /**
356 * Returns the transaction identifier.
357 * @return transaction id
358 */
359 public TransactionId transactionId() {
360 return transactionId;
361 }
362
363 @Override
364 public String toString() {
365 return MoreObjects.toStringHelper(getClass())
366 .add("transactionId", transactionId)
367 .toString();
368 }
369 }
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700370}