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