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