blob: 372452931335c995ce4a134b2fe4b1a0631f406b [file] [log] [blame]
Madan Jampani35708a92016-07-06 10:48:19 -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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.ArrayList;
19import java.util.Collection;
20import java.util.stream.Collectors;
21import java.util.stream.IntStream;
22
23import org.onosproject.store.service.Task;
24import org.onosproject.store.service.WorkQueueStats;
25
26import com.google.common.base.MoreObjects;
27
28import io.atomix.catalyst.buffer.BufferInput;
29import io.atomix.catalyst.buffer.BufferOutput;
30import io.atomix.catalyst.serializer.CatalystSerializable;
31import io.atomix.catalyst.serializer.SerializableTypeResolver;
32import io.atomix.catalyst.serializer.Serializer;
33import io.atomix.catalyst.serializer.SerializerRegistry;
34import io.atomix.copycat.Command;
35
36/**
37 * {@link AtomixWorkQueue} resource state machine operations.
38 */
39public 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
210 /**
211 * Work queue command type resolver.
212 */
213 public static class TypeResolver implements SerializableTypeResolver {
214 @Override
215 public void resolve(SerializerRegistry registry) {
216 registry.register(Register.class, -960);
217 registry.register(Unregister.class, -961);
218 registry.register(Take.class, -962);
219 registry.register(Add.class, -963);
220 registry.register(Complete.class, -964);
221 registry.register(Stats.class, -965);
222 }
223 }
224}