blob: 2452cf8d41e77b9b4ccd8434b724febb53ca120b [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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.Collection;
19
20import com.google.common.base.MoreObjects;
21import io.atomix.protocols.raft.operation.OperationId;
22import io.atomix.protocols.raft.operation.OperationType;
23import org.onlab.util.KryoNamespace;
24import org.onosproject.store.serializers.KryoNamespaces;
25import org.onosproject.store.service.Task;
26import org.onosproject.store.service.WorkQueueStats;
27
28/**
29 * {@link AtomixWorkQueue} resource state machine operations.
30 */
31public enum AtomixWorkQueueOperations implements OperationId {
Jordan Haltermandd2b4c62017-11-29 14:05:33 -080032 STATS(OperationType.QUERY),
33 REGISTER(OperationType.COMMAND),
34 UNREGISTER(OperationType.COMMAND),
35 ADD(OperationType.COMMAND),
36 TAKE(OperationType.COMMAND),
37 COMPLETE(OperationType.COMMAND),
38 CLEAR(OperationType.COMMAND);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070039
Jordan Halterman2bf177c2017-06-29 01:49:08 -070040 private final OperationType type;
41
Jordan Haltermandd2b4c62017-11-29 14:05:33 -080042 AtomixWorkQueueOperations(OperationType type) {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070043 this.type = type;
44 }
45
46 @Override
47 public String id() {
Jordan Haltermandd2b4c62017-11-29 14:05:33 -080048 return name();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070049 }
50
51 @Override
52 public OperationType type() {
53 return type;
54 }
55
56 public static final KryoNamespace NAMESPACE = KryoNamespace.newBuilder()
57 .register(KryoNamespaces.BASIC)
58 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
59 .register(Add.class)
60 .register(Take.class)
61 .register(Complete.class)
62 .register(Task.class)
63 .register(WorkQueueStats.class)
64 .build("AtomixWorkQueueOperations");
65
66 /**
67 * Work queue operation.
68 */
69 public abstract static class WorkQueueOperation {
70 }
71
72 /**
73 * Command to add a collection of tasks to the queue.
74 */
75 @SuppressWarnings("serial")
76 public static class Add extends WorkQueueOperation {
77 private Collection<byte[]> items;
78
79 private Add() {
80 }
81
82 public Add(Collection<byte[]> items) {
83 this.items = items;
84 }
85
86 public Collection<byte[]> items() {
87 return items;
88 }
89
90 @Override
91 public String toString() {
92 return MoreObjects.toStringHelper(getClass())
93 .add("items", items)
94 .toString();
95 }
96 }
97
98 /**
99 * Command to take a task from the queue.
100 */
101 @SuppressWarnings("serial")
102 public static class Take extends WorkQueueOperation {
103 private int maxTasks;
104
105 private Take() {
106 }
107
108 public Take(int maxTasks) {
109 this.maxTasks = maxTasks;
110 }
111
112 public int maxTasks() {
113 return maxTasks;
114 }
115
116 @Override
117 public String toString() {
118 return MoreObjects.toStringHelper(getClass())
119 .add("maxTasks", maxTasks)
120 .toString();
121 }
122 }
123
124 @SuppressWarnings("serial")
125 public static class Complete extends WorkQueueOperation {
126 private Collection<String> taskIds;
127
128 private Complete() {
129 }
130
131 public Complete(Collection<String> taskIds) {
132 this.taskIds = taskIds;
133 }
134
135 public Collection<String> taskIds() {
136 return taskIds;
137 }
138
139 @Override
140 public String toString() {
141 return MoreObjects.toStringHelper(getClass())
142 .add("taskIds", taskIds)
143 .toString();
144 }
145 }
146}