blob: 62aefc5d9e6db5e7524424e391018ee03c61cc8b [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;
27import org.onosproject.store.service.Serializer;
28import org.onosproject.store.service.StorageService;
29import org.onosproject.store.service.Task;
30import org.onosproject.store.service.WorkQueue;
31import org.onosproject.store.service.WorkQueueStats;
32
33import com.google.common.base.Throwables;
34
35/**
36 * CLI command to test a distributed work queue.
37 */
38@Command(scope = "onos", name = "work-queue-test",
39 description = "Test a distributed work queue")
40public class WorkQueueTestCommand extends AbstractShellCommand {
41
42 @Argument(index = 0, name = "name",
43 description = "Work Queue name",
44 required = true, multiValued = false)
45 String name = null;
46
47 @Argument(index = 1, name = "operation",
48 description = "operation name. One of {add, addMutiple, "
49 + "takeAndComplete, totalPending, totalInProgress, totalCompleted}",
50 required = true, multiValued = false)
51 String operation = null;
52
53 @Argument(index = 2, name = "value1",
54 description = "first arg",
55 required = false, multiValued = false)
56 String value1 = null;
57
58 @Argument(index = 3, name = "value2",
59 description = "second arg",
60 required = false, multiValued = false)
61 String value2 = null;
62
63 WorkQueue<String> queue;
64
65 @Override
66 protected void execute() {
67 StorageService storageService = get(StorageService.class);
68 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
69 queue = storageService.getWorkQueue(name, serializer);
70 if (operation.equals("add")) {
71 if (value1 == null) {
72 print("Usage: add <value1>");
73 } else {
74 get(queue.addOne(value1));
75 print("Done");
76 }
77 } else if (operation.equals("addMultiple")) {
78 if (value1 == null || value2 == null) {
79 print("Usage: addMultiple <value1> <value2>");
80 } else {
81 get(queue.addMultiple(Arrays.asList(value1, value2)));
82 print("Done");
83 }
84 } else if (operation.equals("takeAndComplete")) {
85 int maxItems = value1 != null ? Integer.parseInt(value1) : 1;
86 Collection<Task<String>> tasks = get(queue.take(maxItems));
87 tasks.forEach(task -> get(queue.complete(task.taskId())));
88 print("Done");
89 } else if (operation.equals("totalPending")) {
90 WorkQueueStats stats = get(queue.stats());
91 print("%d", stats.totalPending());
92 } else if (operation.equals("totalInProgress")) {
93 WorkQueueStats stats = get(queue.stats());
94 print("%d", stats.totalInProgress());
95 } else if (operation.equals("totalCompleted")) {
96 WorkQueueStats stats = get(queue.stats());
97 print("%d", stats.totalCompleted());
98 } else {
99 print("Invalid operation name. Valid operations names are:"
100 + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted]");
101 }
102 }
103
104 private <T> T get(CompletableFuture<T> future) {
105 try {
106 return future.get(1, TimeUnit.SECONDS);
107 } catch (Exception e) {
108 throw Throwables.propagate(e);
109 }
110 }
111}