Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import io.atomix.catalyst.buffer.BufferInput; |
| 20 | import io.atomix.catalyst.buffer.BufferOutput; |
| 21 | import io.atomix.catalyst.serializer.CatalystSerializable; |
| 22 | import io.atomix.catalyst.serializer.SerializableTypeResolver; |
| 23 | import io.atomix.catalyst.serializer.Serializer; |
| 24 | import io.atomix.catalyst.serializer.SerializerRegistry; |
| 25 | import io.atomix.copycat.Command; |
| 26 | import io.atomix.copycat.Query; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import org.onlab.util.Match; |
| 31 | import org.onosproject.store.service.DocumentPath; |
| 32 | import org.onosproject.store.service.Versioned; |
| 33 | |
| 34 | import com.google.common.base.MoreObjects; |
| 35 | |
| 36 | /** |
| 37 | * {@link AtomixDocumentTree} resource state machine operations. |
| 38 | */ |
| 39 | public class AtomixDocumentTreeCommands { |
| 40 | |
| 41 | /** |
| 42 | * Abstract DocumentTree operation. |
| 43 | */ |
| 44 | public abstract static class DocumentTreeOperation<V> implements CatalystSerializable { |
| 45 | |
| 46 | private DocumentPath path; |
| 47 | |
| 48 | DocumentTreeOperation(DocumentPath path) { |
| 49 | this.path = path; |
| 50 | } |
| 51 | |
| 52 | public DocumentPath path() { |
| 53 | return path; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 58 | serializer.writeObject(path, buffer); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 63 | path = serializer.readObject(buffer); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Abstract DocumentTree query. |
| 69 | */ |
| 70 | @SuppressWarnings("serial") |
| 71 | public abstract static class DocumentTreeQuery<V> extends DocumentTreeOperation<V> implements Query<V> { |
| 72 | |
| 73 | DocumentTreeQuery(DocumentPath path) { |
| 74 | super(path); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public ConsistencyLevel consistency() { |
| 79 | return ConsistencyLevel.SEQUENTIAL; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Abstract DocumentTree command. |
| 85 | */ |
| 86 | @SuppressWarnings("serial") |
| 87 | public abstract static class DocumentTreeCommand<V> extends DocumentTreeOperation<V> implements Command<V> { |
| 88 | |
| 89 | DocumentTreeCommand(DocumentPath path) { |
| 90 | super(path); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * DocumentTree#get query. |
| 96 | */ |
| 97 | @SuppressWarnings("serial") |
| 98 | public static class Get extends DocumentTreeQuery<Versioned<byte[]>> { |
| 99 | public Get() { |
| 100 | super(null); |
| 101 | } |
| 102 | |
| 103 | public Get(DocumentPath path) { |
| 104 | super(path); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(getClass()) |
| 110 | .add("path", path()) |
| 111 | .toString(); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * DocumentTree#getChildren query. |
| 117 | */ |
| 118 | @SuppressWarnings("serial") |
| 119 | public static class GetChildren extends DocumentTreeQuery<Map<String, Versioned<byte[]>>> { |
| 120 | public GetChildren() { |
| 121 | super(null); |
| 122 | } |
| 123 | |
| 124 | public GetChildren(DocumentPath path) { |
| 125 | super(path); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String toString() { |
| 130 | return MoreObjects.toStringHelper(getClass()) |
| 131 | .add("path", path()) |
| 132 | .toString(); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * DocumentTree update command. |
| 138 | */ |
| 139 | @SuppressWarnings("serial") |
| 140 | public static class Update extends DocumentTreeCommand<DocumentTreeUpdateResult<byte[]>> { |
| 141 | |
| 142 | private byte[] value; |
| 143 | private Match<byte[]> valueMatch; |
| 144 | private Match<Long> versionMatch; |
| 145 | |
| 146 | public Update() { |
| 147 | super(null); |
| 148 | this.value = null; |
| 149 | this.valueMatch = null; |
| 150 | this.versionMatch = null; |
| 151 | } |
| 152 | |
| 153 | public Update(DocumentPath path, byte[] value, Match<byte[]> valueMatch, Match<Long> versionMatch) { |
| 154 | super(path); |
| 155 | this.value = value; |
| 156 | this.valueMatch = valueMatch; |
| 157 | this.versionMatch = versionMatch; |
| 158 | } |
| 159 | |
| 160 | public byte[] value() { |
| 161 | return value; |
| 162 | } |
| 163 | |
| 164 | public Match<byte[]> valueMatch() { |
| 165 | return valueMatch; |
| 166 | } |
| 167 | |
| 168 | public Match<Long> versionMatch() { |
| 169 | return versionMatch; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 174 | super.writeObject(buffer, serializer); |
| 175 | serializer.writeObject(value, buffer); |
| 176 | serializer.writeObject(valueMatch, buffer); |
| 177 | serializer.writeObject(versionMatch, buffer); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 182 | super.readObject(buffer, serializer); |
| 183 | value = serializer.readObject(buffer); |
| 184 | valueMatch = serializer.readObject(buffer); |
| 185 | versionMatch = serializer.readObject(buffer); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public String toString() { |
| 190 | return MoreObjects.toStringHelper(getClass()) |
| 191 | .add("path", path()) |
| 192 | .add("value", value) |
| 193 | .add("valueMatch", valueMatch) |
| 194 | .add("versionMatch", versionMatch) |
| 195 | .toString(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Clear command. |
| 201 | */ |
| 202 | @SuppressWarnings("serial") |
| 203 | public static class Clear implements Command<Void>, CatalystSerializable { |
| 204 | @Override |
| 205 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Change listen. |
| 215 | */ |
| 216 | @SuppressWarnings("serial") |
| 217 | public static class Listen extends DocumentTreeCommand<Void> { |
| 218 | |
| 219 | public Listen() { |
| 220 | this(DocumentPath.from("root")); |
| 221 | } |
| 222 | |
| 223 | public Listen(DocumentPath path) { |
| 224 | super(path); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 233 | } |
| 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 |
| 251 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * DocumentTree command type resolver. |
| 261 | */ |
| 262 | public static class TypeResolver implements SerializableTypeResolver { |
| 263 | @Override |
| 264 | public void resolve(SerializerRegistry registry) { |
| 265 | registry.register(Get.class, -911); |
| 266 | registry.register(GetChildren.class, -912); |
| 267 | registry.register(Update.class, -913); |
| 268 | registry.register(Listen.class, -914); |
| 269 | registry.register(Unlisten.class, -915); |
| 270 | registry.register(Clear.class, -916); |
| 271 | } |
| 272 | } |
| 273 | } |