blob: 718223e4ca352664680881eb9393437af1289969 [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
Jordan Haltermand0d80352017-08-10 15:08:27 -070019import java.util.LinkedHashMap;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020import java.util.Optional;
21
22import com.google.common.base.MoreObjects;
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.DocumentPath;
29import org.onosproject.store.service.Versioned;
30
31/**
32 * {@link AtomixDocumentTree} resource state machine operations.
33 */
34public enum AtomixDocumentTreeOperations implements OperationId {
35 ADD_LISTENER("set", OperationType.COMMAND),
36 REMOVE_LISTENER("compareAndSet", OperationType.COMMAND),
37 GET("incrementAndGet", OperationType.QUERY),
38 GET_CHILDREN("getAndIncrement", OperationType.QUERY),
39 UPDATE("addAndGet", OperationType.COMMAND),
40 CLEAR("getAndAdd", OperationType.COMMAND);
41
42 private final String id;
43 private final OperationType type;
44
45 AtomixDocumentTreeOperations(String id, OperationType type) {
46 this.id = id;
47 this.type = type;
48 }
49
50 @Override
51 public String id() {
52 return id;
53 }
54
55 @Override
56 public OperationType type() {
57 return type;
58 }
59
60 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
61 .register(KryoNamespaces.BASIC)
62 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Jordan Haltermand0d80352017-08-10 15:08:27 -070063 .register(LinkedHashMap.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070064 .register(Listen.class)
65 .register(Unlisten.class)
66 .register(Get.class)
67 .register(GetChildren.class)
68 .register(Update.class)
69 .register(DocumentPath.class)
70 .register(Match.class)
71 .register(Versioned.class)
Jordan Haltermane853d032017-08-01 15:10:28 -070072 .register(DocumentTreeResult.class)
73 .register(DocumentTreeResult.Status.class)
Jordan Halterman2bf177c2017-06-29 01:49:08 -070074 .build("AtomixDocumentTreeOperations");
75
76 /**
77 * Abstract DocumentTree command.
78 */
79 @SuppressWarnings("serial")
80 public abstract static class DocumentTreeOperation {
81 private DocumentPath path;
82
83 DocumentTreeOperation(DocumentPath path) {
84 this.path = path;
85 }
86
87 public DocumentPath path() {
88 return path;
89 }
90 }
91
92 /**
93 * DocumentTree#get query.
94 */
95 @SuppressWarnings("serial")
96 public static class Get extends DocumentTreeOperation {
97 public Get() {
98 super(null);
99 }
100
101 public Get(DocumentPath path) {
102 super(path);
103 }
104
105 @Override
106 public String toString() {
107 return MoreObjects.toStringHelper(getClass())
108 .add("path", path())
109 .toString();
110 }
111 }
112
113 /**
114 * DocumentTree#getChildren query.
115 */
116 @SuppressWarnings("serial")
117 public static class GetChildren extends DocumentTreeOperation {
118 public GetChildren() {
119 super(null);
120 }
121
122 public GetChildren(DocumentPath path) {
123 super(path);
124 }
125
126 @Override
127 public String toString() {
128 return MoreObjects.toStringHelper(getClass())
129 .add("path", path())
130 .toString();
131 }
132 }
133
134 /**
135 * DocumentTree update command.
136 */
137 @SuppressWarnings("serial")
138 public static class Update extends DocumentTreeOperation {
139 private Optional<byte[]> value;
140 private Match<byte[]> valueMatch;
141 private Match<Long> versionMatch;
142
143 public Update() {
144 super(null);
145 this.value = null;
146 this.valueMatch = null;
147 this.versionMatch = null;
148 }
149
150 public Update(DocumentPath path, Optional<byte[]> value, Match<byte[]> valueMatch, Match<Long> versionMatch) {
151 super(path);
152 this.value = value;
153 this.valueMatch = valueMatch;
154 this.versionMatch = versionMatch;
155 }
156
157 public Optional<byte[]> value() {
158 return value;
159 }
160
161 public Match<byte[]> valueMatch() {
162 return valueMatch;
163 }
164
165 public Match<Long> versionMatch() {
166 return versionMatch;
167 }
168
169 @Override
170 public String toString() {
171 return MoreObjects.toStringHelper(getClass())
172 .add("path", path())
173 .add("value", value)
174 .add("valueMatch", valueMatch)
175 .add("versionMatch", versionMatch)
176 .toString();
177 }
178 }
179
180 /**
181 * Change listen.
182 */
183 @SuppressWarnings("serial")
184 public static class Listen extends DocumentTreeOperation {
185 public Listen() {
186 this(DocumentPath.from("root"));
187 }
188
189 public Listen(DocumentPath path) {
190 super(path);
191 }
192
193 @Override
194 public String toString() {
195 return MoreObjects.toStringHelper(getClass())
196 .add("path", path())
197 .toString();
198 }
199 }
200
201 /**
202 * Change unlisten.
203 */
204 @SuppressWarnings("serial")
205 public static class Unlisten extends DocumentTreeOperation {
206 public Unlisten() {
207 this(DocumentPath.from("root"));
208 }
209
210 public Unlisten(DocumentPath path) {
211 super(path);
212 }
213
214 @Override
215 public String toString() {
216 return MoreObjects.toStringHelper(getClass())
217 .add("path", path())
218 .toString();
219 }
220 }
221}