blob: 9921b884e01a4e9c3c350e6cac6e37aa24159a1a [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
190 public String toString() {
191 return MoreObjects.toStringHelper(getClass())
192 .add("path", path())
193 .add("value", value)
194 .add("valueMatch", valueMatch)
195 .add("versionMatch", versionMatch)
196 .toString();
197 }
198 }
199
200 /**
201 * Clear command.
202 */
203 @SuppressWarnings("serial")
204 public static class Clear implements Command<Void>, CatalystSerializable {
205 @Override
206 public void writeObject(BufferOutput<?> buffer, Serializer serializer) {
207 }
208
209 @Override
210 public void readObject(BufferInput<?> buffer, Serializer serializer) {
211 }
212 }
213
214 /**
215 * Change listen.
216 */
217 @SuppressWarnings("serial")
218 public static class Listen extends DocumentTreeCommand<Void> {
219
220 public Listen() {
221 this(DocumentPath.from("root"));
222 }
223
224 public Listen(DocumentPath path) {
225 super(path);
226 }
227
228 @Override
Madan Jampani98094222016-09-15 21:12:46 -0700229 public String toString() {
230 return MoreObjects.toStringHelper(getClass())
231 .add("path", path())
232 .toString();
Madan Jampani79924fa2016-09-13 13:57:03 -0700233 }
234 }
235
236 /**
237 * Change unlisten.
238 */
239 @SuppressWarnings("serial")
240 public static class Unlisten extends DocumentTreeCommand<Void> {
241
242 public Unlisten() {
243 this(DocumentPath.from("root"));
244 }
245
246 public Unlisten(DocumentPath path) {
247 super(path);
248 }
249
250 @Override
Madan Jampani98094222016-09-15 21:12:46 -0700251 public String toString() {
252 return MoreObjects.toStringHelper(getClass())
253 .add("path", path())
254 .toString();
Madan Jampani79924fa2016-09-13 13:57:03 -0700255 }
256 }
257
258 /**
259 * DocumentTree command type resolver.
260 */
261 public static class TypeResolver implements SerializableTypeResolver {
262 @Override
263 public void resolve(SerializerRegistry registry) {
264 registry.register(Get.class, -911);
265 registry.register(GetChildren.class, -912);
266 registry.register(Update.class, -913);
267 registry.register(Listen.class, -914);
268 registry.register(Unlisten.class, -915);
269 registry.register(Clear.class, -916);
270 }
271 }
272}