Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -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 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame^] | 18 | import io.atomix.catalyst.buffer.BufferInput; |
| 19 | import io.atomix.catalyst.buffer.BufferOutput; |
| 20 | import io.atomix.catalyst.serializer.CatalystSerializable; |
| 21 | import io.atomix.catalyst.serializer.SerializableTypeResolver; |
| 22 | import io.atomix.catalyst.serializer.Serializer; |
| 23 | import io.atomix.catalyst.serializer.SerializerRegistry; |
| 24 | import io.atomix.copycat.Command; |
| 25 | |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | import java.util.Collection; |
| 28 | import java.util.stream.Collectors; |
| 29 | import java.util.stream.IntStream; |
| 30 | |
| 31 | import org.onosproject.store.service.Task; |
| 32 | import org.onosproject.store.service.WorkQueueStats; |
| 33 | |
| 34 | import com.google.common.base.MoreObjects; |
| 35 | |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 36 | /** |
| 37 | * {@link AtomixWorkQueue} resource state machine operations. |
| 38 | */ |
| 39 | public final class AtomixWorkQueueCommands { |
| 40 | |
| 41 | private AtomixWorkQueueCommands() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Command to add a collection of tasks to the queue. |
| 46 | */ |
| 47 | @SuppressWarnings("serial") |
| 48 | public static class Add implements Command<Void>, CatalystSerializable { |
| 49 | |
| 50 | private Collection<byte[]> items; |
| 51 | |
| 52 | private Add() { |
| 53 | } |
| 54 | |
| 55 | public Add(Collection<byte[]> items) { |
| 56 | this.items = items; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 61 | buffer.writeInt(items.size()); |
| 62 | items.forEach(task -> serializer.writeObject(task, buffer)); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 67 | items = IntStream.range(0, buffer.readInt()) |
| 68 | .mapToObj(i -> serializer.<byte[]>readObject(buffer)) |
| 69 | .collect(Collectors.toCollection(ArrayList::new)); |
| 70 | } |
| 71 | |
| 72 | public Collection<byte[]> items() { |
| 73 | return items; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public String toString() { |
| 78 | return MoreObjects.toStringHelper(getClass()) |
| 79 | .add("items", items) |
| 80 | .toString(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Command to take a task from the queue. |
| 86 | */ |
| 87 | @SuppressWarnings("serial") |
| 88 | public static class Take implements Command<Collection<Task<byte[]>>>, CatalystSerializable { |
| 89 | |
| 90 | private int maxTasks; |
| 91 | |
| 92 | private Take() { |
| 93 | } |
| 94 | |
| 95 | public Take(int maxTasks) { |
| 96 | this.maxTasks = maxTasks; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 101 | buffer.writeInt(maxTasks); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 106 | maxTasks = buffer.readInt(); |
| 107 | } |
| 108 | |
| 109 | public int maxTasks() { |
| 110 | return maxTasks; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString() { |
| 115 | return MoreObjects.toStringHelper(getClass()) |
| 116 | .add("maxTasks", maxTasks) |
| 117 | .toString(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @SuppressWarnings("serial") |
| 122 | public static class Stats implements Command<WorkQueueStats>, CatalystSerializable { |
| 123 | |
| 124 | @Override |
| 125 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String toString() { |
| 134 | return MoreObjects.toStringHelper(getClass()) |
| 135 | .toString(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | @SuppressWarnings("serial") |
| 142 | public static class Register implements Command<Void>, CatalystSerializable { |
| 143 | |
| 144 | @Override |
| 145 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public String toString() { |
| 154 | return MoreObjects.toStringHelper(getClass()) |
| 155 | .toString(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | @SuppressWarnings("serial") |
| 160 | public static class Unregister implements Command<Void>, CatalystSerializable { |
| 161 | |
| 162 | @Override |
| 163 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String toString() { |
| 172 | return MoreObjects.toStringHelper(getClass()) |
| 173 | .toString(); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | @SuppressWarnings("serial") |
| 178 | public static class Complete implements Command<Void>, CatalystSerializable { |
| 179 | private Collection<String> taskIds; |
| 180 | |
| 181 | private Complete() { |
| 182 | } |
| 183 | |
| 184 | public Complete(Collection<String> taskIds) { |
| 185 | this.taskIds = taskIds; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 190 | serializer.writeObject(taskIds, buffer); |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 195 | taskIds = serializer.readObject(buffer); |
| 196 | } |
| 197 | |
| 198 | public Collection<String> taskIds() { |
| 199 | return taskIds; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public String toString() { |
| 204 | return MoreObjects.toStringHelper(getClass()) |
| 205 | .add("taskIds", taskIds) |
| 206 | .toString(); |
| 207 | } |
| 208 | } |
| 209 | |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame^] | 210 | @SuppressWarnings("serial") |
| 211 | public static class Clear implements Command<Void>, CatalystSerializable { |
| 212 | |
| 213 | @Override |
| 214 | public void writeObject(BufferOutput<?> buffer, Serializer serializer) { |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void readObject(BufferInput<?> buffer, Serializer serializer) { |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public String toString() { |
| 223 | return MoreObjects.toStringHelper(getClass()) |
| 224 | .toString(); |
| 225 | } |
| 226 | } |
| 227 | |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Work queue command type resolver. |
| 230 | */ |
| 231 | public static class TypeResolver implements SerializableTypeResolver { |
| 232 | @Override |
| 233 | public void resolve(SerializerRegistry registry) { |
| 234 | registry.register(Register.class, -960); |
| 235 | registry.register(Unregister.class, -961); |
| 236 | registry.register(Take.class, -962); |
| 237 | registry.register(Add.class, -963); |
| 238 | registry.register(Complete.class, -964); |
| 239 | registry.register(Stats.class, -965); |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame^] | 240 | registry.register(Clear.class, -966); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |