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 2017-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 | |
| 18 | import io.atomix.protocols.raft.operation.OperationId; |
| 19 | import io.atomix.protocols.raft.operation.OperationType; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.store.serializers.KryoNamespaces; |
| 22 | |
| 23 | /** |
| 24 | * Counter commands. |
| 25 | */ |
| 26 | public enum AtomixCounterOperations implements OperationId { |
| 27 | SET("set", OperationType.COMMAND), |
| 28 | COMPARE_AND_SET("compareAndSet", OperationType.COMMAND), |
| 29 | INCREMENT_AND_GET("incrementAndGet", OperationType.COMMAND), |
| 30 | GET_AND_INCREMENT("getAndIncrement", OperationType.COMMAND), |
| 31 | ADD_AND_GET("addAndGet", OperationType.COMMAND), |
| 32 | GET_AND_ADD("getAndAdd", OperationType.COMMAND), |
| 33 | GET("get", OperationType.QUERY); |
| 34 | |
| 35 | private final String id; |
| 36 | private final OperationType type; |
| 37 | |
| 38 | AtomixCounterOperations(String id, OperationType type) { |
| 39 | this.id = id; |
| 40 | this.type = type; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public String id() { |
| 45 | return id; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public OperationType type() { |
| 50 | return type; |
| 51 | } |
| 52 | |
| 53 | public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder() |
| 54 | .register(KryoNamespaces.BASIC) |
| 55 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 56 | .register(Get.class) |
| 57 | .register(Set.class) |
| 58 | .register(CompareAndSet.class) |
| 59 | .register(AddAndGet.class) |
| 60 | .register(GetAndAdd.class) |
| 61 | .build("AtomixCounterOperations"); |
| 62 | |
| 63 | /** |
| 64 | * Abstract value command. |
| 65 | */ |
| 66 | public abstract static class ValueOperation { |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get query. |
| 71 | */ |
| 72 | public static class Get extends ValueOperation { |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Set command. |
| 77 | */ |
| 78 | public static class Set extends ValueOperation { |
| 79 | private Long value; |
| 80 | |
| 81 | public Set() { |
| 82 | } |
| 83 | |
| 84 | public Set(Long value) { |
| 85 | this.value = value; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the command value. |
| 90 | * |
| 91 | * @return The command value. |
| 92 | */ |
| 93 | public Long value() { |
| 94 | return value; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return String.format("%s[value=%s]", getClass().getSimpleName(), value); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Compare and set command. |
| 105 | */ |
| 106 | public static class CompareAndSet extends ValueOperation { |
| 107 | private Long expect; |
| 108 | private Long update; |
| 109 | |
| 110 | public CompareAndSet() { |
| 111 | } |
| 112 | |
| 113 | public CompareAndSet(Long expect, Long update) { |
| 114 | this.expect = expect; |
| 115 | this.update = update; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the expected value. |
| 120 | * |
| 121 | * @return The expected value. |
| 122 | */ |
| 123 | public Long expect() { |
| 124 | return expect; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns the updated value. |
| 129 | * |
| 130 | * @return The updated value. |
| 131 | */ |
| 132 | public Long update() { |
| 133 | return update; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public String toString() { |
| 138 | return String.format("%s[expect=%s, update=%s]", getClass().getSimpleName(), expect, update); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Delta command. |
| 144 | */ |
| 145 | public abstract static class DeltaOperation extends ValueOperation { |
| 146 | private long delta; |
| 147 | |
| 148 | public DeltaOperation() { |
| 149 | } |
| 150 | |
| 151 | public DeltaOperation(long delta) { |
| 152 | this.delta = delta; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns the delta. |
| 157 | * |
| 158 | * @return The delta. |
| 159 | */ |
| 160 | public long delta() { |
| 161 | return delta; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get and add command. |
| 167 | */ |
| 168 | public static class GetAndAdd extends DeltaOperation { |
| 169 | public GetAndAdd() { |
| 170 | } |
| 171 | |
| 172 | public GetAndAdd(long delta) { |
| 173 | super(delta); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Add and get command. |
| 179 | */ |
| 180 | public static class AddAndGet extends DeltaOperation { |
| 181 | public AddAndGet() { |
| 182 | } |
| 183 | |
| 184 | public AddAndGet(long delta) { |
| 185 | super(delta); |
| 186 | } |
| 187 | } |
| 188 | } |