blob: 82372a2dcf323feaee579b8f6ae4dcc441da4ae6 [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.ArrayList;
20import java.util.Collection;
21
22import com.google.common.base.MoreObjects;
23import com.google.common.collect.Maps;
24import io.atomix.protocols.raft.operation.OperationId;
25import io.atomix.protocols.raft.operation.OperationType;
26import org.onlab.util.KryoNamespace;
27import org.onlab.util.Match;
28import org.onosproject.store.serializers.KryoNamespaces;
29import org.onosproject.store.service.Versioned;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * AsyncConsistentMultimap state machine commands.
35 */
36public enum AtomixConsistentSetMultimapOperations implements OperationId {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080037 GET(OperationType.QUERY),
38 SIZE(OperationType.QUERY),
39 IS_EMPTY(OperationType.QUERY),
40 CONTAINS_KEY(OperationType.QUERY),
41 CONTAINS_VALUE(OperationType.QUERY),
42 CONTAINS_ENTRY(OperationType.QUERY),
43 KEY_SET(OperationType.QUERY),
44 KEYS(OperationType.QUERY),
45 VALUES(OperationType.QUERY),
46 ENTRIES(OperationType.QUERY),
47 PUT(OperationType.COMMAND),
48 REMOVE(OperationType.COMMAND),
49 REMOVE_ALL(OperationType.COMMAND),
50 REPLACE(OperationType.COMMAND),
51 CLEAR(OperationType.COMMAND),
52 ADD_LISTENER(OperationType.COMMAND),
Jordan Halterman5e884352018-05-21 22:11:07 -070053 REMOVE_LISTENER(OperationType.COMMAND),
54 ITERATE(OperationType.COMMAND);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070055
Jordan Halterman2bf177c2017-06-29 01:49:08 -070056 private final OperationType type;
57
Jordan Halterman2b7501c2017-11-29 14:05:33 -080058 AtomixConsistentSetMultimapOperations(OperationType type) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070059 this.type = type;
60 }
61
62 @Override
63 public String id() {
Jordan Halterman2b7501c2017-11-29 14:05:33 -080064 return name();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070065 }
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(ContainsEntry.class)
76 .register(ContainsKey.class)
77 .register(ContainsValue.class)
78 .register(Get.class)
79 .register(MultiRemove.class)
80 .register(Put.class)
81 .register(RemoveAll.class)
82 .register(Replace.class)
83 .register(Match.class)
84 .register(Versioned.class)
85 .register(ArrayList.class)
86 .register(Maps.immutableEntry("", "").getClass())
87 .build("AtomixConsistentSetMultimapOperations");
88
89 /**
90 * Abstract multimap command.
91 */
92 @SuppressWarnings("serial")
93 public abstract static class MultimapOperation {
94 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(getClass())
97 .toString();
98 }
99 }
100
101 /**
102 * Abstract key-based multimap query.
103 */
104 @SuppressWarnings("serial")
105 public abstract static class KeyOperation extends MultimapOperation {
106 protected String key;
107
108 public KeyOperation() {
109 }
110
111 public KeyOperation(String key) {
112 this.key = checkNotNull(key);
113 }
114
115 public String key() {
116 return key;
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(getClass())
122 .add("key", key)
123 .toString();
124 }
125 }
126
127 /**
128 * Abstract value-based query.
129 */
130 @SuppressWarnings("serial")
131 public abstract static class ValueOperation extends MultimapOperation {
132 protected byte[] value;
133
134 public ValueOperation() {
135 }
136
137 public ValueOperation(byte[] value) {
138 this.value = checkNotNull(value);
139 }
140
141 /**
142 * Returns the value.
143 *
144 * @return value.
145 */
146 public byte[] value() {
147 return value;
148 }
149
150 @Override
151 public String toString() {
152 return MoreObjects.toStringHelper(getClass())
153 .add("value", value)
154 .toString();
155 }
156 }
157
158 /**
159 * Contains key query.
160 */
161 @SuppressWarnings("serial")
162 public static class ContainsKey extends KeyOperation {
163 public ContainsKey() {
164 }
165
166 public ContainsKey(String key) {
167 super(key);
168 }
169 }
170
171 /**
172 * Contains value query.
173 */
174 @SuppressWarnings("serial")
175 public static class ContainsValue extends ValueOperation {
176 public ContainsValue() {
177 }
178
179 public ContainsValue(byte[] value) {
180 super(value);
181 }
182 }
183
184 /**
185 * Contains entry query.
186 */
187 @SuppressWarnings("serial")
188 public static class ContainsEntry extends MultimapOperation {
189 protected String key;
190 protected byte[] value;
191
192 public ContainsEntry() {
193 }
194
195 public ContainsEntry(String key, byte[] value) {
196 this.key = checkNotNull(key);
197 this.value = checkNotNull(value);
198 }
199
200 public String key() {
201 return key;
202 }
203
204 public byte[] value() {
205 return value;
206 }
207
208 @Override
209 public String toString() {
210 return MoreObjects.toStringHelper(getClass())
211 .add("key", key)
212 .add("value", value)
213 .toString();
214 }
215 }
216
217 /**
218 * Remove command, backs remove and removeAll's that return booleans.
219 */
220 @SuppressWarnings("serial")
221 public static class RemoveAll extends MultimapOperation {
222 private String key;
223 private Match<Long> versionMatch;
224
225 public RemoveAll() {
226 }
227
228 public RemoveAll(String key, Match<Long> versionMatch) {
229 this.key = checkNotNull(key);
230 this.versionMatch = versionMatch;
231 }
232
233 public String key() {
234 return this.key;
235 }
236
237 public Match<Long> versionMatch() {
238 return versionMatch;
239 }
240
241 @Override
242 public String toString() {
243 return MoreObjects.toStringHelper(getClass())
244 .add("key", key)
245 .add("versionMatch", versionMatch)
246 .toString();
247 }
248 }
249
250 /**
251 * Remove command, backs remove and removeAll's that return booleans.
252 */
253 @SuppressWarnings("serial")
254 public static class MultiRemove extends MultimapOperation {
255 private String key;
256 private Collection<byte[]> values;
257 private Match<Long> versionMatch;
258
259 public MultiRemove() {
260 }
261
262 public MultiRemove(String key, Collection<byte[]> valueMatches,
263 Match<Long> versionMatch) {
264 this.key = checkNotNull(key);
265 this.values = valueMatches;
266 this.versionMatch = versionMatch;
267 }
268
269 public String key() {
270 return this.key;
271 }
272
273 public Collection<byte[]> values() {
274 return values;
275 }
276
277 public Match<Long> versionMatch() {
278 return versionMatch;
279 }
280
281 @Override
282 public String toString() {
283 return MoreObjects.toStringHelper(getClass())
284 .add("key", key)
285 .add("values", values)
286 .add("versionMatch", versionMatch)
287 .toString();
288 }
289 }
290
291 /**
292 * Command to back the put and putAll methods.
293 */
294 @SuppressWarnings("serial")
295 public static class Put extends MultimapOperation {
296 private String key;
297 private Collection<? extends byte[]> values;
298 private Match<Long> versionMatch;
299
300 public Put() {
301 }
302
303 public Put(String key, Collection<? extends byte[]> values, Match<Long> versionMatch) {
304 this.key = checkNotNull(key);
305 this.values = values;
306 this.versionMatch = versionMatch;
307 }
308
309 public String key() {
310 return key;
311 }
312
313 public Collection<? extends byte[]> values() {
314 return values;
315 }
316
317 public Match<Long> versionMatch() {
318 return versionMatch;
319 }
320
321 @Override
322 public String toString() {
323 return MoreObjects.toStringHelper(getClass())
324 .add("key", key)
325 .add("values", values)
326 .add("versionMatch", versionMatch)
327 .toString();
328 }
329 }
330
331 /**
332 * Replace command, returns the collection that was replaced.
333 */
334 @SuppressWarnings("serial")
335 public static class Replace extends MultimapOperation {
336 private String key;
337 private Collection<byte[]> values;
338 private Match<Long> versionMatch;
339
340 public Replace() {
341 }
342
343 public Replace(String key, Collection<byte[]> values,
344 Match<Long> versionMatch) {
345 this.key = checkNotNull(key);
346 this.values = values;
347 this.versionMatch = versionMatch;
348 }
349
350 public String key() {
351 return this.key;
352 }
353
354 public Match<Long> versionMatch() {
355 return versionMatch;
356 }
357
358 public Collection<byte[]> values() {
359 return values;
360 }
361
362 @Override
363 public String toString() {
364 return MoreObjects.toStringHelper(getClass())
365 .add("key", key)
366 .add("values", values)
367 .add("versionMatch", versionMatch)
368 .toString();
369 }
370 }
371
372 /**
373 * Get value query.
374 */
375 public static class Get extends KeyOperation {
376 public Get() {
377 }
378
379 public Get(String key) {
380 super(key);
381 }
382 }
383}