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