blob: 42d303bd3f8dad63336b3d9f37cfac334988fc9a [file] [log] [blame]
Madan Jampanice240732016-09-02 10:42:58 -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.distributedprimitives.cli;
17
18import java.util.Arrays;
19import java.util.Collection;
20import java.util.concurrent.CompletableFuture;
21import java.util.concurrent.TimeUnit;
22
23import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.store.serializers.KryoNamespaces;
Jon Hall7f412252017-02-22 17:15:19 -080027import org.onosproject.store.service.DistributedPrimitive;
Madan Jampanice240732016-09-02 10:42:58 -070028import org.onosproject.store.service.Serializer;
29import org.onosproject.store.service.StorageService;
30import org.onosproject.store.service.Task;
31import org.onosproject.store.service.WorkQueue;
32import org.onosproject.store.service.WorkQueueStats;
33
34import com.google.common.base.Throwables;
35
36/**
37 * CLI command to test a distributed work queue.
38 */
39@Command(scope = "onos", name = "work-queue-test",
40 description = "Test a distributed work queue")
41public class WorkQueueTestCommand extends AbstractShellCommand {
42
43 @Argument(index = 0, name = "name",
44 description = "Work Queue name",
45 required = true, multiValued = false)
46 String name = null;
47
48 @Argument(index = 1, name = "operation",
Jon Hall41e6cd72017-03-28 13:57:59 -070049 description = "operation name. One of {add, addMultiple, "
Madan Jampanid4684b42016-09-02 22:26:31 -070050 + "takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy}",
Madan Jampanice240732016-09-02 10:42:58 -070051 required = true, multiValued = false)
52 String operation = null;
53
54 @Argument(index = 2, name = "value1",
55 description = "first arg",
56 required = false, multiValued = false)
57 String value1 = null;
58
59 @Argument(index = 3, name = "value2",
60 description = "second arg",
61 required = false, multiValued = false)
62 String value2 = null;
63
64 WorkQueue<String> queue;
65
66 @Override
67 protected void execute() {
68 StorageService storageService = get(StorageService.class);
69 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
70 queue = storageService.getWorkQueue(name, serializer);
Jon Hall41e6cd72017-03-28 13:57:59 -070071 if ("add".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070072 if (value1 == null) {
73 print("Usage: add <value1>");
74 } else {
75 get(queue.addOne(value1));
76 print("Done");
77 }
Jon Hall41e6cd72017-03-28 13:57:59 -070078 } else if ("addMultiple".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070079 if (value1 == null || value2 == null) {
80 print("Usage: addMultiple <value1> <value2>");
81 } else {
82 get(queue.addMultiple(Arrays.asList(value1, value2)));
83 print("Done");
84 }
Jon Hall41e6cd72017-03-28 13:57:59 -070085 } else if ("takeAndComplete".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070086 int maxItems = value1 != null ? Integer.parseInt(value1) : 1;
87 Collection<Task<String>> tasks = get(queue.take(maxItems));
88 tasks.forEach(task -> get(queue.complete(task.taskId())));
89 print("Done");
Jon Hall41e6cd72017-03-28 13:57:59 -070090 } else if ("totalPending".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070091 WorkQueueStats stats = get(queue.stats());
92 print("%d", stats.totalPending());
Jon Hall41e6cd72017-03-28 13:57:59 -070093 } else if ("totalInProgress".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070094 WorkQueueStats stats = get(queue.stats());
95 print("%d", stats.totalInProgress());
Jon Hall41e6cd72017-03-28 13:57:59 -070096 } else if ("totalCompleted".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070097 WorkQueueStats stats = get(queue.stats());
98 print("%d", stats.totalCompleted());
Jon Hall41e6cd72017-03-28 13:57:59 -070099 } else if ("destroy".equals(operation)) {
Madan Jampanid4684b42016-09-02 22:26:31 -0700100 get(queue.destroy());
Madan Jampanice240732016-09-02 10:42:58 -0700101 } else {
102 print("Invalid operation name. Valid operations names are:"
Madan Jampanid4684b42016-09-02 22:26:31 -0700103 + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy]");
Madan Jampanice240732016-09-02 10:42:58 -0700104 }
105 }
106
107 private <T> T get(CompletableFuture<T> future) {
108 try {
Jordan Halterman6440b092017-05-24 17:48:08 -0700109 return future.get(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
Madan Jampanice240732016-09-02 10:42:58 -0700110 } catch (Exception e) {
111 throw Throwables.propagate(e);
112 }
113 }
114}