Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import io.atomix.protocols.raft.operation.OperationId; |
| 20 | import io.atomix.protocols.raft.operation.OperationType; |
| 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 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * {@link AtomixConsistentMap} resource state machine operations. |
| 33 | */ |
| 34 | public enum AtomixConsistentMapOperations implements OperationId { |
| 35 | IS_EMPTY("isEmpty", OperationType.QUERY), |
| 36 | SIZE("size", OperationType.QUERY), |
| 37 | CONTAINS_KEY("containsKey", OperationType.QUERY), |
| 38 | CONTAINS_VALUE("containsValue", OperationType.QUERY), |
| 39 | GET("get", OperationType.QUERY), |
| 40 | GET_OR_DEFAULT("getOrDefault", OperationType.QUERY), |
| 41 | KEY_SET("keySet", OperationType.QUERY), |
| 42 | VALUES("values", OperationType.QUERY), |
| 43 | ENTRY_SET("entrySet", OperationType.QUERY), |
| 44 | UPDATE_AND_GET("updateAndGet", OperationType.COMMAND), |
| 45 | CLEAR("clear", OperationType.COMMAND), |
| 46 | ADD_LISTENER("addListener", OperationType.COMMAND), |
| 47 | REMOVE_LISTENER("removeListener", OperationType.COMMAND), |
| 48 | BEGIN("begin", OperationType.COMMAND), |
| 49 | PREPARE("prepare", OperationType.COMMAND), |
| 50 | PREPARE_AND_COMMIT("prepareAndCommit", OperationType.COMMAND), |
| 51 | COMMIT("commit", OperationType.COMMAND), |
| 52 | ROLLBACK("rollback", OperationType.COMMAND); |
| 53 | |
| 54 | private final String id; |
| 55 | private final OperationType type; |
| 56 | |
| 57 | AtomixConsistentMapOperations(String id, OperationType type) { |
| 58 | this.id = id; |
| 59 | this.type = type; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String id() { |
| 64 | return id; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public OperationType type() { |
| 69 | return type; |
| 70 | } |
| 71 | |
| 72 | public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder() |
| 73 | .register(KryoNamespaces.BASIC) |
| 74 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 75 | .register(ContainsKey.class) |
| 76 | .register(ContainsValue.class) |
| 77 | .register(Get.class) |
| 78 | .register(GetOrDefault.class) |
| 79 | .register(UpdateAndGet.class) |
| 80 | .register(TransactionBegin.class) |
| 81 | .register(TransactionPrepare.class) |
| 82 | .register(TransactionPrepareAndCommit.class) |
| 83 | .register(TransactionCommit.class) |
| 84 | .register(TransactionRollback.class) |
| 85 | .register(TransactionId.class) |
| 86 | .register(TransactionLog.class) |
| 87 | .register(MapUpdate.class) |
| 88 | .register(MapUpdate.Type.class) |
| 89 | .register(PrepareResult.class) |
| 90 | .register(CommitResult.class) |
| 91 | .register(RollbackResult.class) |
| 92 | .register(Match.class) |
| 93 | .register(MapEntryUpdateResult.class) |
| 94 | .register(MapEntryUpdateResult.Status.class) |
| 95 | .register(Versioned.class) |
| 96 | .register(byte[].class) |
| 97 | .build("AtomixConsistentMapOperations"); |
| 98 | |
| 99 | /** |
| 100 | * Abstract map command. |
| 101 | */ |
| 102 | @SuppressWarnings("serial") |
| 103 | public abstract static class MapOperation { |
| 104 | @Override |
| 105 | public String toString() { |
| 106 | return MoreObjects.toStringHelper(getClass()) |
| 107 | .toString(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Abstract key-based query. |
| 113 | */ |
| 114 | @SuppressWarnings("serial") |
| 115 | public abstract static class KeyOperation extends MapOperation { |
| 116 | protected String key; |
| 117 | |
| 118 | public KeyOperation() { |
| 119 | } |
| 120 | |
| 121 | public KeyOperation(String key) { |
| 122 | this.key = checkNotNull(key, "key cannot be null"); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the key. |
| 127 | * @return key |
| 128 | */ |
| 129 | public String key() { |
| 130 | return key; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public String toString() { |
| 135 | return MoreObjects.toStringHelper(getClass()) |
| 136 | .add("key", key) |
| 137 | .toString(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Abstract value-based query. |
| 143 | */ |
| 144 | @SuppressWarnings("serial") |
| 145 | public abstract static class ValueOperation extends MapOperation { |
| 146 | protected byte[] value; |
| 147 | |
| 148 | public ValueOperation() { |
| 149 | } |
| 150 | |
| 151 | public ValueOperation(byte[] value) { |
| 152 | this.value = checkNotNull(value, "value cannot be null"); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the value. |
| 157 | * @return value |
| 158 | */ |
| 159 | public byte[] value() { |
| 160 | return value; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public String toString() { |
| 165 | return MoreObjects.toStringHelper(getClass()) |
| 166 | .add("value", value) |
| 167 | .toString(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Contains key command. |
| 173 | */ |
| 174 | @SuppressWarnings("serial") |
| 175 | public static class ContainsKey extends KeyOperation { |
| 176 | public ContainsKey() { |
| 177 | } |
| 178 | |
| 179 | public ContainsKey(String key) { |
| 180 | super(key); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Contains value command. |
| 186 | */ |
| 187 | @SuppressWarnings("serial") |
| 188 | public static class ContainsValue extends ValueOperation { |
| 189 | public ContainsValue() { |
| 190 | } |
| 191 | |
| 192 | public ContainsValue(byte[] value) { |
| 193 | super(value); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Transaction begin command. |
| 199 | */ |
| 200 | public static class TransactionBegin extends MapOperation { |
| 201 | private TransactionId transactionId; |
| 202 | |
| 203 | public TransactionBegin() { |
| 204 | } |
| 205 | |
| 206 | public TransactionBegin(TransactionId transactionId) { |
| 207 | this.transactionId = transactionId; |
| 208 | } |
| 209 | |
| 210 | public TransactionId transactionId() { |
| 211 | return transactionId; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Map prepare command. |
| 217 | */ |
| 218 | @SuppressWarnings("serial") |
| 219 | public static class TransactionPrepare extends MapOperation { |
| 220 | private TransactionLog<MapUpdate<String, byte[]>> transactionLog; |
| 221 | |
| 222 | public TransactionPrepare() { |
| 223 | } |
| 224 | |
| 225 | public TransactionPrepare(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 226 | this.transactionLog = transactionLog; |
| 227 | } |
| 228 | |
| 229 | public TransactionLog<MapUpdate<String, byte[]>> transactionLog() { |
| 230 | return transactionLog; |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public String toString() { |
| 235 | return MoreObjects.toStringHelper(getClass()) |
| 236 | .add("transactionLog", transactionLog) |
| 237 | .toString(); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Map prepareAndCommit command. |
| 243 | */ |
| 244 | @SuppressWarnings("serial") |
| 245 | public static class TransactionPrepareAndCommit extends TransactionPrepare { |
| 246 | public TransactionPrepareAndCommit() { |
| 247 | } |
| 248 | |
| 249 | public TransactionPrepareAndCommit(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 250 | super(transactionLog); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Map transaction commit command. |
| 256 | */ |
| 257 | @SuppressWarnings("serial") |
| 258 | public static class TransactionCommit extends MapOperation { |
| 259 | private TransactionId transactionId; |
| 260 | |
| 261 | public TransactionCommit() { |
| 262 | } |
| 263 | |
| 264 | public TransactionCommit(TransactionId transactionId) { |
| 265 | this.transactionId = transactionId; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Returns the transaction identifier. |
| 270 | * @return transaction id |
| 271 | */ |
| 272 | public TransactionId transactionId() { |
| 273 | return transactionId; |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public String toString() { |
| 278 | return MoreObjects.toStringHelper(getClass()) |
| 279 | .add("transactionId", transactionId) |
| 280 | .toString(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Map transaction rollback command. |
| 286 | */ |
| 287 | @SuppressWarnings("serial") |
| 288 | public static class TransactionRollback extends MapOperation { |
| 289 | private TransactionId transactionId; |
| 290 | |
| 291 | public TransactionRollback() { |
| 292 | } |
| 293 | |
| 294 | public TransactionRollback(TransactionId transactionId) { |
| 295 | this.transactionId = transactionId; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Returns the transaction identifier. |
| 300 | * @return transaction id |
| 301 | */ |
| 302 | public TransactionId transactionId() { |
| 303 | return transactionId; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public String toString() { |
| 308 | return MoreObjects.toStringHelper(getClass()) |
| 309 | .add("transactionId", transactionId) |
| 310 | .toString(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Map update command. |
| 316 | */ |
| 317 | @SuppressWarnings("serial") |
| 318 | public static class UpdateAndGet extends MapOperation { |
| 319 | private String key; |
| 320 | private byte[] value; |
| 321 | private Match<byte[]> valueMatch; |
| 322 | private Match<Long> versionMatch; |
| 323 | |
| 324 | public UpdateAndGet() { |
| 325 | } |
| 326 | |
| 327 | public UpdateAndGet(String key, |
| 328 | byte[] value, |
| 329 | Match<byte[]> valueMatch, |
| 330 | Match<Long> versionMatch) { |
| 331 | this.key = key; |
| 332 | this.value = value; |
| 333 | this.valueMatch = valueMatch; |
| 334 | this.versionMatch = versionMatch; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Returns the key. |
| 339 | * @return key |
| 340 | */ |
| 341 | public String key() { |
| 342 | return this.key; |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Returns the value. |
| 347 | * @return value |
| 348 | */ |
| 349 | public byte[] value() { |
| 350 | return this.value; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Returns the value match. |
| 355 | * @return value match |
| 356 | */ |
| 357 | public Match<byte[]> valueMatch() { |
| 358 | return this.valueMatch; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Returns the version match. |
| 363 | * @return version match |
| 364 | */ |
| 365 | public Match<Long> versionMatch() { |
| 366 | return this.versionMatch; |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public String toString() { |
| 371 | return MoreObjects.toStringHelper(getClass()) |
| 372 | .add("key", key) |
| 373 | .add("value", value) |
| 374 | .add("valueMatch", valueMatch) |
| 375 | .add("versionMatch", versionMatch) |
| 376 | .toString(); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get query. |
| 382 | */ |
| 383 | @SuppressWarnings("serial") |
| 384 | public static class Get extends KeyOperation { |
| 385 | public Get() { |
| 386 | } |
| 387 | |
| 388 | public Get(String key) { |
| 389 | super(key); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Get or default query. |
| 395 | */ |
| 396 | @SuppressWarnings("serial") |
| 397 | public static class GetOrDefault extends KeyOperation { |
| 398 | private byte[] defaultValue; |
| 399 | |
| 400 | public GetOrDefault() { |
| 401 | } |
| 402 | |
| 403 | public GetOrDefault(String key, byte[] defaultValue) { |
| 404 | super(key); |
| 405 | this.defaultValue = defaultValue; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Returns the default value. |
| 410 | * |
| 411 | * @return the default value |
| 412 | */ |
| 413 | public byte[] defaultValue() { |
| 414 | return defaultValue; |
| 415 | } |
| 416 | } |
| 417 | } |