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