Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
Jordan Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 19 | import java.util.LinkedHashMap; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 20 | import java.util.Optional; |
| 21 | |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | import io.atomix.protocols.raft.operation.OperationId; |
| 24 | import io.atomix.protocols.raft.operation.OperationType; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onlab.util.Match; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 27 | import org.onosproject.store.primitives.NodeUpdate; |
| 28 | import org.onosproject.store.primitives.TransactionId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.DocumentPath; |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.TransactionLog; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 32 | import org.onosproject.store.service.Versioned; |
| 33 | |
| 34 | /** |
| 35 | * {@link AtomixDocumentTree} resource state machine operations. |
| 36 | */ |
| 37 | public enum AtomixDocumentTreeOperations implements OperationId { |
Jordan Halterman | 2b7501c | 2017-11-29 14:05:33 -0800 | [diff] [blame] | 38 | 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 49 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 50 | private final OperationType type; |
| 51 | |
Jordan Halterman | 2b7501c | 2017-11-29 14:05:33 -0800 | [diff] [blame] | 52 | AtomixDocumentTreeOperations(OperationType type) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 53 | this.type = type; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public String id() { |
Jordan Halterman | 2b7501c | 2017-11-29 14:05:33 -0800 | [diff] [blame] | 58 | return name(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 59 | } |
| 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 Halterman | d0d8035 | 2017-08-10 15:08:27 -0700 | [diff] [blame] | 69 | .register(LinkedHashMap.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 70 | .register(Listen.class) |
| 71 | .register(Unlisten.class) |
| 72 | .register(Get.class) |
| 73 | .register(GetChildren.class) |
| 74 | .register(Update.class) |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 75 | .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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 87 | .register(DocumentPath.class) |
| 88 | .register(Match.class) |
| 89 | .register(Versioned.class) |
Jordan Halterman | e853d03 | 2017-08-01 15:10:28 -0700 | [diff] [blame] | 90 | .register(DocumentTreeResult.class) |
| 91 | .register(DocumentTreeResult.Status.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 92 | .build("AtomixDocumentTreeOperations"); |
| 93 | |
| 94 | /** |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 95 | * 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 102 | */ |
| 103 | @SuppressWarnings("serial") |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 104 | public abstract static class PathOperation extends DocumentTreeOperation { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 105 | private DocumentPath path; |
| 106 | |
Sithara Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 107 | PathOperation(DocumentPath path) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 108 | 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 Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 120 | public static class Get extends PathOperation { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 121 | 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 Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 141 | public static class GetChildren extends PathOperation { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 142 | 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 Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 162 | public static class Update extends PathOperation { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 163 | 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 Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 208 | public static class Listen extends PathOperation { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 209 | 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 Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 229 | public static class Unlisten extends PathOperation { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 230 | 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 Punnassery | 61a8025 | 2017-08-07 11:16:08 -0700 | [diff] [blame] | 245 | |
| 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 370 | } |