blob: 74ae7ebe64fb092659527a1b762b519cb1b9dc66 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jordan Halterman2bf177c2017-06-29 01:49:08 -07003 *
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
17package org.onosproject.store.primitives.resources.impl;
18
19import java.util.AbstractMap;
20
21import com.google.common.base.MoreObjects;
22import com.google.common.collect.Maps;
23import io.atomix.protocols.raft.operation.OperationId;
24import io.atomix.protocols.raft.operation.OperationType;
25import org.onlab.util.KryoNamespace;
26import org.onlab.util.Match;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.Versioned;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * {@link org.onosproject.store.service.AsyncConsistentTreeMap} Resource
34 * state machine operations.
35 */
36public enum AtomixConsistentTreeMapOperations implements OperationId {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080037 SUB_MAP(OperationType.QUERY),
38 FIRST_KEY(OperationType.QUERY),
39 LAST_KEY(OperationType.QUERY),
40 FIRST_ENTRY(OperationType.QUERY),
41 LAST_ENTRY(OperationType.QUERY),
42 POLL_FIRST_ENTRY(OperationType.QUERY),
43 POLL_LAST_ENTRY(OperationType.QUERY),
44 LOWER_ENTRY(OperationType.QUERY),
45 LOWER_KEY(OperationType.QUERY),
46 FLOOR_ENTRY(OperationType.QUERY),
47 FLOOR_KEY(OperationType.QUERY),
48 CEILING_ENTRY(OperationType.QUERY),
49 CEILING_KEY(OperationType.QUERY),
50 HIGHER_ENTRY(OperationType.QUERY),
51 HIGHER_KEY(OperationType.QUERY);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070052
Jordan Halterman2bf177c2017-06-29 01:49:08 -070053 private final OperationType type;
54
Jordan Halterman2b7501c2017-11-29 14:05:33 -080055 AtomixConsistentTreeMapOperations(OperationType type) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070056 this.type = type;
57 }
58
59 @Override
60 public String id() {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080061 return name();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070062 }
63
64 @Override
65 public OperationType type() {
66 return type;
67 }
68
69 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
70 .register(KryoNamespaces.BASIC)
Jordan Halterman71635ae2017-07-28 10:35:43 -070071 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID + 100)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070072 .register(LowerKey.class)
73 .register(LowerEntry.class)
74 .register(HigherKey.class)
75 .register(HigherEntry.class)
76 .register(FloorKey.class)
77 .register(FloorEntry.class)
78 .register(CeilingKey.class)
79 .register(CeilingEntry.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070080 .register(Versioned.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070081 .register(AbstractMap.SimpleImmutableEntry.class)
82 .register(Maps.immutableEntry("", "").getClass())
83 .build("AtomixConsistentTreeMapOperations");
84
85 /**
86 * Abstract treeMap command.
87 */
88 @SuppressWarnings("serial")
89 public abstract static class TreeOperation {
90 @Override
91 public String toString() {
92 return MoreObjects.toStringHelper(getClass())
93 .toString();
94 }
95 }
96
97 /**
98 * Abstract key-based query.
99 */
100 @SuppressWarnings("serial")
101 public abstract static class KeyOperation extends TreeOperation {
102 protected String key;
103
104 public KeyOperation(String key) {
105 this.key = checkNotNull(key);
106 }
107
108 public KeyOperation() {
109 }
110
111 public String key() {
112 return key;
113 }
114
115 @Override
116 public String toString() {
117 return MoreObjects.toStringHelper(getClass())
118 .add("key", key)
119 .toString();
120 }
121 }
122
123 /**
124 * Abstract value-based query.
125 */
126 @SuppressWarnings("serial")
127 public abstract static class ValueOperation extends TreeOperation {
128 protected byte[] value;
129
130 public ValueOperation() {}
131
132 public ValueOperation(byte[] value) {
133 this.value = checkNotNull(value);
134 }
135
136 public byte[] value() {
137 return value;
138 }
139
140 @Override
141 public String toString() {
142 return MoreObjects.toStringHelper(getClass())
143 .add("value", value)
144 .toString();
145 }
146 }
147
148 /**
149 * Contains key command.
150 */
151 @SuppressWarnings("serial")
152 public static class ContainsKey extends KeyOperation {
153
154 public ContainsKey(String key) {
155 super(key);
156 }
157
158 public ContainsKey() {
159 }
160 }
161 /**
162 * Contains value command.
163 */
164 @SuppressWarnings("serial")
165 public static class ContainsValue extends ValueOperation {
166 public ContainsValue() {
167 }
168
169 public ContainsValue(byte[] value) {
170 super(value);
171 }
172
173 }
174
175 /**
176 * AsyncConsistentTreeMap update command.
177 */
178 @SuppressWarnings("serial")
179 public static class UpdateAndGet extends TreeOperation {
180 private String key;
181 private byte[] value;
182 private Match<byte[]> valueMatch;
183 private Match<Long> versionMatch;
184 public UpdateAndGet() {
185 }
186
187 public UpdateAndGet(String key,
188 byte[] value,
189 Match<byte[]> valueMatch,
190 Match<Long> versionMatch) {
191 this.key = key;
192 this.value = value;
193 this.valueMatch = valueMatch;
194 this.versionMatch = versionMatch;
195 }
196
197 public String key() {
198 return this.key;
199 }
200
201 public byte[] value() {
202 return this.value;
203 }
204
205 public Match<byte[]> valueMatch() {
206 return this.valueMatch;
207 }
208
209 public Match<Long> versionMatch() {
210 return this.versionMatch;
211 }
212
213 @Override
214 public String toString() {
215 return MoreObjects.toStringHelper(getClass())
216 .add("key", key)
217 .add("value", value)
218 .add("valueMatch", valueMatch)
219 .add("versionMatch", versionMatch)
220 .toString();
221 }
222 }
223
224 /**
225 * Get query.
226 */
227 @SuppressWarnings("serial")
228 public static class Get extends KeyOperation {
229 public Get() {
230 }
231
232 public Get(String key) {
233 super(key);
234 }
235 }
236
237 /**
238 * Get or default query.
239 */
240 @SuppressWarnings("serial")
241 public static class GetOrDefault extends KeyOperation {
242 private byte[] defaultValue;
243
244 public GetOrDefault() {
245 }
246
247 public GetOrDefault(String key, byte[] defaultValue) {
248 super(key);
249 this.defaultValue = defaultValue;
250 }
251
252 /**
253 * Returns the default value.
254 *
255 * @return the default value
256 */
257 public byte[] defaultValue() {
258 return defaultValue;
259 }
260 }
261
262 /**
263 * Query returns the entry associated with the largest key less than the
264 * passed in key.
265 */
266 @SuppressWarnings("serial")
267 public static class LowerEntry extends KeyOperation {
268 public LowerEntry() {
269 }
270
271 public LowerEntry(String key) {
272 super(key);
273 }
274 }
275
276 /**
277 * Query returns the largest key less than the specified key.
278 */
279 @SuppressWarnings("serial")
280 public static class LowerKey extends KeyOperation {
281 public LowerKey() {
282 }
283
284 public LowerKey(String key) {
285 super(key);
286 }
287 }
288
289 /**
290 * Query returns the entry associated with the largest key smaller than or
291 * equal to the specified key.
292 */
293 @SuppressWarnings("serial")
294 public static class FloorEntry extends KeyOperation {
295 public FloorEntry() {
296 }
297
298 public FloorEntry(String key) {
299 super(key);
300 }
301 }
302
303 /**
304 * Query returns the largest key smaller than or equal to the passed in
305 * key.
306 */
307 @SuppressWarnings("serial")
308 public static class FloorKey extends KeyOperation {
309 public FloorKey() {
310 }
311
312 public FloorKey(String key) {
313 super(key);
314 }
315 }
316
317 /**
318 * Returns the entry associated with the smallest key larger than or equal
319 * to the specified key.
320 */
321 @SuppressWarnings("serial")
322 public static class CeilingEntry extends KeyOperation {
323 public CeilingEntry() {
324 }
325
326 public CeilingEntry(String key) {
327 super(key);
328 }
329 }
330
331 /**
332 * Returns the smallest key larger than or equal to the specified key.
333 */
334 @SuppressWarnings("serial")
335 public static class CeilingKey extends KeyOperation {
336 public CeilingKey() {
337 }
338
339 public CeilingKey(String key) {
340 super(key);
341 }
342 }
343
344 /**
345 * Returns the entry associated with the smallest key larger than the
346 * specified key.
347 */
348 @SuppressWarnings("serial")
349 public static class HigherEntry extends KeyOperation {
350 public HigherEntry() {
351 }
352
353 public HigherEntry(String key) {
354 super(key);
355 }
356 }
357
358 /**
359 * Returns the smallest key larger than the specified key.
360 */
361 @SuppressWarnings("serial")
362 public static class HigherKey extends KeyOperation {
363 public HigherKey() {
364 }
365
366 public HigherKey(String key) {
367 super(key);
368 }
369 }
370
371 @SuppressWarnings("serial")
372 public static class SubMap<K, V> extends TreeOperation {
373 private K fromKey;
374 private K toKey;
375 private boolean inclusiveFrom;
376 private boolean inclusiveTo;
377
378 public SubMap() {
379 }
380
381 public SubMap(K fromKey, K toKey, boolean inclusiveFrom,
382 boolean inclusiveTo) {
383 this.fromKey = fromKey;
384 this.toKey = toKey;
385 this.inclusiveFrom = inclusiveFrom;
386 this.inclusiveTo = inclusiveTo;
387 }
388
389 @Override
390 public String toString() {
391 return MoreObjects.toStringHelper(getClass())
392 .add("getFromKey", fromKey)
393 .add("getToKey", toKey)
Jon Hall7d77fe12018-04-24 18:03:10 -0700394 .add("inclusiveFromBound", inclusiveFrom)
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700395 .add("inclusiveToBound", inclusiveTo)
396 .toString();
397 }
398
399 public K fromKey() {
400 return fromKey;
401 }
402
403 public K toKey() {
404 return toKey;
405 }
406
407 public boolean isInclusiveFrom() {
408 return inclusiveFrom;
409 }
410
411 public boolean isInclusiveTo() {
412 return inclusiveTo;
413 }
414 }
415}