blob: 153dbcb0d93b05a2d3668ac9f093a8e78bfcc84b [file] [log] [blame]
Madan Jampani79924fa2016-09-13 13:57:03 -07001/*
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
17package org.onosproject.store.primitives.resources.impl;
18
19import io.atomix.catalyst.buffer.BufferInput;
20import io.atomix.catalyst.buffer.BufferOutput;
21import io.atomix.catalyst.serializer.CatalystSerializable;
22import io.atomix.catalyst.serializer.SerializableTypeResolver;
23import io.atomix.catalyst.serializer.Serializer;
24import io.atomix.catalyst.serializer.SerializerRegistry;
25import io.atomix.copycat.Command;
26import io.atomix.copycat.Query;
27
28import java.util.Map;
Madan Jampani4c8e3fe2016-09-16 16:20:28 -070029import java.util.Optional;
Madan Jampani79924fa2016-09-13 13:57:03 -070030
31import org.onlab.util.Match;
32import org.onosproject.store.service.DocumentPath;
33import org.onosproject.store.service.Versioned;
34
35import com.google.common.base.MoreObjects;
36
37/**
38 * {@link AtomixDocumentTree} resource state machine operations.
39 */
40public class AtomixDocumentTreeCommands {
41
42 /**
43 * Abstract DocumentTree operation.
44 */
45 public abstract static class DocumentTreeOperation<V> implements CatalystSerializable {
46
47 private DocumentPath path;
48
49 DocumentTreeOperation(DocumentPath path) {
50 this.path = path;
51 }
52
53 public DocumentPath path() {
54 return path;
55 }
56
57 @Override
58 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
59 serializer.writeObject(path, buffer);
60 }
61
62 @Override
63 public void readObject(BufferInput<?> buffer, Serializer serializer) {
64 path = serializer.readObject(buffer);
65 }
66 }
67
68 /**
69 * Abstract DocumentTree query.
70 */
71 @SuppressWarnings("serial")
72 public abstract static class DocumentTreeQuery<V> extends DocumentTreeOperation<V> implements Query<V> {
73
74 DocumentTreeQuery(DocumentPath path) {
75 super(path);
76 }
77
78 @Override
79 public ConsistencyLevel consistency() {
80 return ConsistencyLevel.SEQUENTIAL;
81 }
82 }
83
84 /**
85 * Abstract DocumentTree command.
86 */
87 @SuppressWarnings("serial")
88 public abstract static class DocumentTreeCommand<V> extends DocumentTreeOperation<V> implements Command<V> {
89
90 DocumentTreeCommand(DocumentPath path) {
91 super(path);
92 }
93 }
94
95 /**
96 * DocumentTree#get query.
97 */
98 @SuppressWarnings("serial")
99 public static class Get extends DocumentTreeQuery<Versioned<byte[]>> {
100 public Get() {
101 super(null);
102 }
103
104 public Get(DocumentPath path) {
105 super(path);
106 }
107
108 @Override
109 public String toString() {
110 return MoreObjects.toStringHelper(getClass())
111 .add("path", path())
112 .toString();
113 }
114 }
115
116 /**
117 * DocumentTree#getChildren query.
118 */
119 @SuppressWarnings("serial")
120 public static class GetChildren extends DocumentTreeQuery<Map<String, Versioned<byte[]>>> {
121 public GetChildren() {
122 super(null);
123 }
124
125 public GetChildren(DocumentPath path) {
126 super(path);
127 }
128
129 @Override
130 public String toString() {
131 return MoreObjects.toStringHelper(getClass())
132 .add("path", path())
133 .toString();
134 }
135 }
136
137 /**
138 * DocumentTree update command.
139 */
140 @SuppressWarnings("serial")
141 public static class Update extends DocumentTreeCommand<DocumentTreeUpdateResult<byte[]>> {
142
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700143 private Optional<byte[]> value;
Madan Jampani79924fa2016-09-13 13:57:03 -0700144 private Match<byte[]> valueMatch;
145 private Match<Long> versionMatch;
146
147 public Update() {
148 super(null);
149 this.value = null;
150 this.valueMatch = null;
151 this.versionMatch = null;
152 }
153
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700154 public Update(DocumentPath path, Optional<byte[]> value, Match<byte[]> valueMatch, Match<Long> versionMatch) {
Madan Jampani79924fa2016-09-13 13:57:03 -0700155 super(path);
156 this.value = value;
157 this.valueMatch = valueMatch;
158 this.versionMatch = versionMatch;
159 }
160
Madan Jampani4c8e3fe2016-09-16 16:20:28 -0700161 public Optional<byte[]> value() {
Madan Jampani79924fa2016-09-13 13:57:03 -0700162 return value;
163 }
164
165 public Match<byte[]> valueMatch() {
166 return valueMatch;
167 }
168
169 public Match<Long> versionMatch() {
170 return versionMatch;
171 }
172
173 @Override
174 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
175 super.writeObject(buffer, serializer);
176 serializer.writeObject(value, buffer);
177 serializer.writeObject(valueMatch, buffer);
178 serializer.writeObject(versionMatch, buffer);
179 }
180
181 @Override
182 public void readObject(BufferInput<?> buffer, Serializer serializer) {
183 super.readObject(buffer, serializer);
184 value = serializer.readObject(buffer);
185 valueMatch = serializer.readObject(buffer);
186 versionMatch = serializer.readObject(buffer);
187 }
188
189 @Override
Jordan Halterman260156a2017-02-05 22:25:27 -0800190 public CompactionMode compaction() {
191 return value == null ? CompactionMode.TOMBSTONE : CompactionMode.QUORUM;
192 }
193
194 @Override
Madan Jampani79924fa2016-09-13 13:57:03 -0700195 public String toString() {
196 return MoreObjects.toStringHelper(getClass())
197 .add("path", path())
198 .add("value", value)
199 .add("valueMatch", valueMatch)
200 .add("versionMatch", versionMatch)
201 .toString();
202 }
203 }
204
205 /**
206 * Clear command.
207 */
208 @SuppressWarnings("serial")
209 public static class Clear implements Command<Void>, CatalystSerializable {
210 @Override
211 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
212 }
213
214 @Override
215 public void readObject(BufferInput<?> buffer, Serializer serializer) {
216 }
Jordan Halterman260156a2017-02-05 22:25:27 -0800217
218 @Override
219 public CompactionMode compaction() {
220 return CompactionMode.TOMBSTONE;
221 }
Madan Jampani79924fa2016-09-13 13:57:03 -0700222 }
223
224 /**
225 * Change listen.
226 */
227 @SuppressWarnings("serial")
228 public static class Listen extends DocumentTreeCommand<Void> {
229
230 public Listen() {
231 this(DocumentPath.from("root"));
232 }
233
234 public Listen(DocumentPath path) {
235 super(path);
236 }
237
238 @Override
Jordan Halterman260156a2017-02-05 22:25:27 -0800239 public CompactionMode compaction() {
240 return CompactionMode.QUORUM;
241 }
242
243 @Override
Madan Jampani98094222016-09-15 21:12:46 -0700244 public String toString() {
245 return MoreObjects.toStringHelper(getClass())
246 .add("path", path())
247 .toString();
Madan Jampani79924fa2016-09-13 13:57:03 -0700248 }
249 }
250
251 /**
252 * Change unlisten.
253 */
254 @SuppressWarnings("serial")
255 public static class Unlisten extends DocumentTreeCommand<Void> {
256
257 public Unlisten() {
258 this(DocumentPath.from("root"));
259 }
260
261 public Unlisten(DocumentPath path) {
262 super(path);
263 }
264
265 @Override
Jordan Halterman260156a2017-02-05 22:25:27 -0800266 public CompactionMode compaction() {
267 return CompactionMode.TOMBSTONE;
268 }
269
270 @Override
Madan Jampani98094222016-09-15 21:12:46 -0700271 public String toString() {
272 return MoreObjects.toStringHelper(getClass())
273 .add("path", path())
274 .toString();
Madan Jampani79924fa2016-09-13 13:57:03 -0700275 }
276 }
277
278 /**
279 * DocumentTree command type resolver.
280 */
281 public static class TypeResolver implements SerializableTypeResolver {
282 @Override
283 public void resolve(SerializerRegistry registry) {
284 registry.register(Get.class, -911);
285 registry.register(GetChildren.class, -912);
286 registry.register(Update.class, -913);
287 registry.register(Listen.class, -914);
288 registry.register(Unlisten.class, -915);
289 registry.register(Clear.class, -916);
290 }
291 }
292}