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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 18 | import io.atomix.protocols.raft.operation.OperationId; |
| 19 | import io.atomix.protocols.raft.operation.OperationType; |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 20 | import io.atomix.utils.ArraySizeHashPrinter; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 21 | import org.onlab.util.KryoNamespace; |
| 22 | import org.onlab.util.Match; |
| 23 | import org.onosproject.store.primitives.MapUpdate; |
| 24 | import org.onosproject.store.primitives.TransactionId; |
| 25 | import org.onosproject.store.serializers.KryoNamespaces; |
| 26 | import org.onosproject.store.service.TransactionLog; |
| 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 29 | import static com.google.common.base.MoreObjects.toStringHelper; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * {@link AtomixConsistentMap} resource state machine operations. |
| 34 | */ |
| 35 | public enum AtomixConsistentMapOperations implements OperationId { |
Jordan Halterman | dd2b4c6 | 2017-11-29 14:05:33 -0800 | [diff] [blame^] | 36 | IS_EMPTY(OperationType.QUERY), |
| 37 | SIZE(OperationType.QUERY), |
| 38 | CONTAINS_KEY(OperationType.QUERY), |
| 39 | CONTAINS_VALUE(OperationType.QUERY), |
| 40 | GET(OperationType.QUERY), |
| 41 | GET_OR_DEFAULT(OperationType.QUERY), |
| 42 | KEY_SET(OperationType.QUERY), |
| 43 | VALUES(OperationType.QUERY), |
| 44 | ENTRY_SET(OperationType.QUERY), |
| 45 | PUT(OperationType.COMMAND), |
| 46 | PUT_IF_ABSENT(OperationType.COMMAND), |
| 47 | PUT_AND_GET(OperationType.COMMAND), |
| 48 | REMOVE(OperationType.COMMAND), |
| 49 | REMOVE_VALUE(OperationType.COMMAND), |
| 50 | REMOVE_VERSION(OperationType.COMMAND), |
| 51 | REPLACE(OperationType.COMMAND), |
| 52 | REPLACE_VALUE(OperationType.COMMAND), |
| 53 | REPLACE_VERSION(OperationType.COMMAND), |
| 54 | CLEAR(OperationType.COMMAND), |
| 55 | ADD_LISTENER(OperationType.COMMAND), |
| 56 | REMOVE_LISTENER(OperationType.COMMAND), |
| 57 | BEGIN(OperationType.COMMAND), |
| 58 | PREPARE(OperationType.COMMAND), |
| 59 | PREPARE_AND_COMMIT(OperationType.COMMAND), |
| 60 | COMMIT(OperationType.COMMAND), |
| 61 | ROLLBACK(OperationType.COMMAND); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 63 | private final OperationType type; |
| 64 | |
Jordan Halterman | dd2b4c6 | 2017-11-29 14:05:33 -0800 | [diff] [blame^] | 65 | AtomixConsistentMapOperations(OperationType type) { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 66 | this.type = type; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String id() { |
Jordan Halterman | dd2b4c6 | 2017-11-29 14:05:33 -0800 | [diff] [blame^] | 71 | return name(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public OperationType type() { |
| 76 | return type; |
| 77 | } |
| 78 | |
| 79 | public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder() |
| 80 | .register(KryoNamespaces.BASIC) |
| 81 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 82 | .register(ContainsKey.class) |
| 83 | .register(ContainsValue.class) |
| 84 | .register(Get.class) |
| 85 | .register(GetOrDefault.class) |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 86 | .register(Put.class) |
| 87 | .register(Remove.class) |
| 88 | .register(RemoveValue.class) |
| 89 | .register(RemoveVersion.class) |
| 90 | .register(Replace.class) |
| 91 | .register(ReplaceValue.class) |
| 92 | .register(ReplaceVersion.class) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 93 | .register(TransactionBegin.class) |
| 94 | .register(TransactionPrepare.class) |
| 95 | .register(TransactionPrepareAndCommit.class) |
| 96 | .register(TransactionCommit.class) |
| 97 | .register(TransactionRollback.class) |
| 98 | .register(TransactionId.class) |
| 99 | .register(TransactionLog.class) |
| 100 | .register(MapUpdate.class) |
| 101 | .register(MapUpdate.Type.class) |
| 102 | .register(PrepareResult.class) |
| 103 | .register(CommitResult.class) |
| 104 | .register(RollbackResult.class) |
| 105 | .register(Match.class) |
| 106 | .register(MapEntryUpdateResult.class) |
| 107 | .register(MapEntryUpdateResult.Status.class) |
| 108 | .register(Versioned.class) |
| 109 | .register(byte[].class) |
| 110 | .build("AtomixConsistentMapOperations"); |
| 111 | |
| 112 | /** |
| 113 | * Abstract map command. |
| 114 | */ |
| 115 | @SuppressWarnings("serial") |
| 116 | public abstract static class MapOperation { |
| 117 | @Override |
| 118 | public String toString() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 119 | return toStringHelper(getClass()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 120 | .toString(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Abstract key-based query. |
| 126 | */ |
| 127 | @SuppressWarnings("serial") |
| 128 | public abstract static class KeyOperation extends MapOperation { |
| 129 | protected String key; |
| 130 | |
| 131 | public KeyOperation() { |
| 132 | } |
| 133 | |
| 134 | public KeyOperation(String key) { |
| 135 | this.key = checkNotNull(key, "key cannot be null"); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns the key. |
| 140 | * @return key |
| 141 | */ |
| 142 | public String key() { |
| 143 | return key; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public String toString() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 148 | return toStringHelper(getClass()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 149 | .add("key", key) |
| 150 | .toString(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Abstract value-based query. |
| 156 | */ |
| 157 | @SuppressWarnings("serial") |
| 158 | public abstract static class ValueOperation extends MapOperation { |
| 159 | protected byte[] value; |
| 160 | |
| 161 | public ValueOperation() { |
| 162 | } |
| 163 | |
| 164 | public ValueOperation(byte[] value) { |
Jordan Halterman | 4922a06 | 2017-07-31 15:55:36 -0700 | [diff] [blame] | 165 | this.value = value; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the value. |
| 170 | * @return value |
| 171 | */ |
| 172 | public byte[] value() { |
| 173 | return value; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public String toString() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 178 | return toStringHelper(getClass()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 179 | .add("value", value) |
| 180 | .toString(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 185 | * Abstract key/value operation. |
| 186 | */ |
| 187 | @SuppressWarnings("serial") |
| 188 | public abstract static class KeyValueOperation extends KeyOperation { |
| 189 | protected byte[] value; |
| 190 | |
| 191 | public KeyValueOperation() { |
| 192 | } |
| 193 | |
| 194 | public KeyValueOperation(String key, byte[] value) { |
| 195 | super(key); |
| 196 | this.value = value; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the value. |
| 201 | * @return value |
| 202 | */ |
| 203 | public byte[] value() { |
| 204 | return value; |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public String toString() { |
| 209 | return toStringHelper(getClass()) |
| 210 | .add("key", key) |
| 211 | .add("value", ArraySizeHashPrinter.of(value)) |
| 212 | .toString(); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Abstract key/version operation. |
| 218 | */ |
| 219 | @SuppressWarnings("serial") |
| 220 | public abstract static class KeyVersionOperation extends KeyOperation { |
| 221 | protected long version; |
| 222 | |
| 223 | public KeyVersionOperation() { |
| 224 | } |
| 225 | |
| 226 | public KeyVersionOperation(String key, long version) { |
| 227 | super(key); |
| 228 | this.version = version; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns the version. |
| 233 | * @return version |
| 234 | */ |
| 235 | public long version() { |
| 236 | return version; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public String toString() { |
| 241 | return toStringHelper(getClass()) |
| 242 | .add("key", key) |
| 243 | .add("version", version) |
| 244 | .toString(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 249 | * Contains key command. |
| 250 | */ |
| 251 | @SuppressWarnings("serial") |
| 252 | public static class ContainsKey extends KeyOperation { |
| 253 | public ContainsKey() { |
| 254 | } |
| 255 | |
| 256 | public ContainsKey(String key) { |
| 257 | super(key); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Contains value command. |
| 263 | */ |
| 264 | @SuppressWarnings("serial") |
| 265 | public static class ContainsValue extends ValueOperation { |
| 266 | public ContainsValue() { |
| 267 | } |
| 268 | |
| 269 | public ContainsValue(byte[] value) { |
| 270 | super(value); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 275 | * Map put operation. |
| 276 | */ |
| 277 | public static class Put extends KeyValueOperation { |
| 278 | public Put() { |
| 279 | } |
| 280 | |
| 281 | public Put(String key, byte[] value) { |
| 282 | super(key, value); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Remove operation. |
| 288 | */ |
| 289 | public static class Remove extends KeyOperation { |
| 290 | public Remove() { |
| 291 | } |
| 292 | |
| 293 | public Remove(String key) { |
| 294 | super(key); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Remove if value match operation. |
| 300 | */ |
| 301 | public static class RemoveValue extends KeyValueOperation { |
| 302 | public RemoveValue() { |
| 303 | } |
| 304 | |
| 305 | public RemoveValue(String key, byte[] value) { |
| 306 | super(key, value); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Remove if version match operation. |
| 312 | */ |
| 313 | public static class RemoveVersion extends KeyVersionOperation { |
| 314 | public RemoveVersion() { |
| 315 | } |
| 316 | |
| 317 | public RemoveVersion(String key, long version) { |
| 318 | super(key, version); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Replace operation. |
| 324 | */ |
| 325 | public static class Replace extends KeyValueOperation { |
| 326 | public Replace() { |
| 327 | } |
| 328 | |
| 329 | public Replace(String key, byte[] value) { |
| 330 | super(key, value); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Replace by value operation. |
| 336 | */ |
| 337 | public static class ReplaceValue extends KeyOperation { |
| 338 | private byte[] oldValue; |
| 339 | private byte[] newValue; |
| 340 | |
| 341 | public ReplaceValue() { |
| 342 | } |
| 343 | |
| 344 | public ReplaceValue(String key, byte[] oldValue, byte[] newValue) { |
| 345 | super(key); |
| 346 | this.oldValue = oldValue; |
| 347 | this.newValue = newValue; |
| 348 | } |
| 349 | |
| 350 | public byte[] oldValue() { |
| 351 | return oldValue; |
| 352 | } |
| 353 | |
| 354 | public byte[] newValue() { |
| 355 | return newValue; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public String toString() { |
| 360 | return toStringHelper(this) |
| 361 | .add("key", key) |
| 362 | .add("oldValue", ArraySizeHashPrinter.of(oldValue)) |
| 363 | .add("newValue", ArraySizeHashPrinter.of(newValue)) |
| 364 | .toString(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Replace by version operation. |
| 370 | */ |
| 371 | public static class ReplaceVersion extends KeyOperation { |
| 372 | private long oldVersion; |
| 373 | private byte[] newValue; |
| 374 | |
| 375 | public ReplaceVersion() { |
| 376 | } |
| 377 | |
| 378 | public ReplaceVersion(String key, long oldVersion, byte[] newValue) { |
| 379 | super(key); |
| 380 | this.oldVersion = oldVersion; |
| 381 | this.newValue = newValue; |
| 382 | } |
| 383 | |
| 384 | public long oldVersion() { |
| 385 | return oldVersion; |
| 386 | } |
| 387 | |
| 388 | public byte[] newValue() { |
| 389 | return newValue; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public String toString() { |
| 394 | return toStringHelper(this) |
| 395 | .add("key", key) |
| 396 | .add("oldVersion", oldVersion) |
| 397 | .add("newValue", ArraySizeHashPrinter.of(newValue)) |
| 398 | .toString(); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 403 | * Transaction begin command. |
| 404 | */ |
| 405 | public static class TransactionBegin extends MapOperation { |
| 406 | private TransactionId transactionId; |
| 407 | |
| 408 | public TransactionBegin() { |
| 409 | } |
| 410 | |
| 411 | public TransactionBegin(TransactionId transactionId) { |
| 412 | this.transactionId = transactionId; |
| 413 | } |
| 414 | |
| 415 | public TransactionId transactionId() { |
| 416 | return transactionId; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Map prepare command. |
| 422 | */ |
| 423 | @SuppressWarnings("serial") |
| 424 | public static class TransactionPrepare extends MapOperation { |
| 425 | private TransactionLog<MapUpdate<String, byte[]>> transactionLog; |
| 426 | |
| 427 | public TransactionPrepare() { |
| 428 | } |
| 429 | |
| 430 | public TransactionPrepare(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 431 | this.transactionLog = transactionLog; |
| 432 | } |
| 433 | |
| 434 | public TransactionLog<MapUpdate<String, byte[]>> transactionLog() { |
| 435 | return transactionLog; |
| 436 | } |
| 437 | |
| 438 | @Override |
| 439 | public String toString() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 440 | return toStringHelper(getClass()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 441 | .add("transactionLog", transactionLog) |
| 442 | .toString(); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Map prepareAndCommit command. |
| 448 | */ |
| 449 | @SuppressWarnings("serial") |
| 450 | public static class TransactionPrepareAndCommit extends TransactionPrepare { |
| 451 | public TransactionPrepareAndCommit() { |
| 452 | } |
| 453 | |
| 454 | public TransactionPrepareAndCommit(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 455 | super(transactionLog); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Map transaction commit command. |
| 461 | */ |
| 462 | @SuppressWarnings("serial") |
| 463 | public static class TransactionCommit extends MapOperation { |
| 464 | private TransactionId transactionId; |
| 465 | |
| 466 | public TransactionCommit() { |
| 467 | } |
| 468 | |
| 469 | public TransactionCommit(TransactionId transactionId) { |
| 470 | this.transactionId = transactionId; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Returns the transaction identifier. |
| 475 | * @return transaction id |
| 476 | */ |
| 477 | public TransactionId transactionId() { |
| 478 | return transactionId; |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public String toString() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 483 | return toStringHelper(getClass()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 484 | .add("transactionId", transactionId) |
| 485 | .toString(); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Map transaction rollback command. |
| 491 | */ |
| 492 | @SuppressWarnings("serial") |
| 493 | public static class TransactionRollback extends MapOperation { |
| 494 | private TransactionId transactionId; |
| 495 | |
| 496 | public TransactionRollback() { |
| 497 | } |
| 498 | |
| 499 | public TransactionRollback(TransactionId transactionId) { |
| 500 | this.transactionId = transactionId; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Returns the transaction identifier. |
| 505 | * @return transaction id |
| 506 | */ |
| 507 | public TransactionId transactionId() { |
| 508 | return transactionId; |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public String toString() { |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 513 | return toStringHelper(getClass()) |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 514 | .add("transactionId", transactionId) |
| 515 | .toString(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 520 | * Get query. |
| 521 | */ |
| 522 | @SuppressWarnings("serial") |
| 523 | public static class Get extends KeyOperation { |
| 524 | public Get() { |
| 525 | } |
| 526 | |
| 527 | public Get(String key) { |
| 528 | super(key); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Get or default query. |
| 534 | */ |
| 535 | @SuppressWarnings("serial") |
| 536 | public static class GetOrDefault extends KeyOperation { |
| 537 | private byte[] defaultValue; |
| 538 | |
| 539 | public GetOrDefault() { |
| 540 | } |
| 541 | |
| 542 | public GetOrDefault(String key, byte[] defaultValue) { |
| 543 | super(key); |
| 544 | this.defaultValue = defaultValue; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Returns the default value. |
| 549 | * |
| 550 | * @return the default value |
| 551 | */ |
| 552 | public byte[] defaultValue() { |
| 553 | return defaultValue; |
| 554 | } |
Jordan Halterman | 71635ae | 2017-07-28 10:35:43 -0700 | [diff] [blame] | 555 | |
| 556 | @Override |
| 557 | public String toString() { |
| 558 | return toStringHelper(this) |
| 559 | .add("key", key) |
| 560 | .add("defaultValue", ArraySizeHashPrinter.of(defaultValue)) |
| 561 | .toString(); |
| 562 | } |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 563 | } |
| 564 | } |