blob: b3f1d6281ebede1f1e5efe68d22c3123dded1a23 [file] [log] [blame]
Madan Jampanice240732016-09-02 10:42:58 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampanice240732016-09-02 10:42:58 -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.distributedprimitives.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Madan Jampanice240732016-09-02 10:42:58 -070020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.store.serializers.KryoNamespaces;
Jon Hall7f412252017-02-22 17:15:19 -080022import org.onosproject.store.service.DistributedPrimitive;
Madan Jampanice240732016-09-02 10:42:58 -070023import org.onosproject.store.service.Serializer;
24import org.onosproject.store.service.StorageService;
25import org.onosproject.store.service.Task;
26import org.onosproject.store.service.WorkQueue;
27import org.onosproject.store.service.WorkQueueStats;
28
Ray Milkey6a51cb92018-03-06 09:03:03 -080029import java.util.Arrays;
30import java.util.Collection;
31import java.util.concurrent.CompletableFuture;
32import java.util.concurrent.TimeUnit;
Madan Jampanice240732016-09-02 10:42:58 -070033
34/**
35 * CLI command to test a distributed work queue.
36 */
37@Command(scope = "onos", name = "work-queue-test",
38 description = "Test a distributed work queue")
39public class WorkQueueTestCommand extends AbstractShellCommand {
40
41 @Argument(index = 0, name = "name",
42 description = "Work Queue name",
43 required = true, multiValued = false)
44 String name = null;
45
46 @Argument(index = 1, name = "operation",
Jon Hall41e6cd72017-03-28 13:57:59 -070047 description = "operation name. One of {add, addMultiple, "
Madan Jampanid4684b42016-09-02 22:26:31 -070048 + "takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy}",
Madan Jampanice240732016-09-02 10:42:58 -070049 required = true, multiValued = false)
50 String operation = null;
51
52 @Argument(index = 2, name = "value1",
53 description = "first arg",
54 required = false, multiValued = false)
55 String value1 = null;
56
57 @Argument(index = 3, name = "value2",
58 description = "second arg",
59 required = false, multiValued = false)
60 String value2 = null;
61
62 WorkQueue<String> queue;
63
64 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070065 protected void doExecute() {
Madan Jampanice240732016-09-02 10:42:58 -070066 StorageService storageService = get(StorageService.class);
67 Serializer serializer = Serializer.using(KryoNamespaces.BASIC);
68 queue = storageService.getWorkQueue(name, serializer);
Jon Hall41e6cd72017-03-28 13:57:59 -070069 if ("add".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070070 if (value1 == null) {
71 print("Usage: add <value1>");
72 } else {
73 get(queue.addOne(value1));
74 print("Done");
75 }
Jon Hall41e6cd72017-03-28 13:57:59 -070076 } else if ("addMultiple".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070077 if (value1 == null || value2 == null) {
78 print("Usage: addMultiple <value1> <value2>");
79 } else {
80 get(queue.addMultiple(Arrays.asList(value1, value2)));
81 print("Done");
82 }
Jon Hall41e6cd72017-03-28 13:57:59 -070083 } else if ("takeAndComplete".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070084 int maxItems = value1 != null ? Integer.parseInt(value1) : 1;
85 Collection<Task<String>> tasks = get(queue.take(maxItems));
86 tasks.forEach(task -> get(queue.complete(task.taskId())));
87 print("Done");
Jon Hall41e6cd72017-03-28 13:57:59 -070088 } else if ("totalPending".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070089 WorkQueueStats stats = get(queue.stats());
90 print("%d", stats.totalPending());
Jon Hall41e6cd72017-03-28 13:57:59 -070091 } else if ("totalInProgress".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070092 WorkQueueStats stats = get(queue.stats());
93 print("%d", stats.totalInProgress());
Jon Hall41e6cd72017-03-28 13:57:59 -070094 } else if ("totalCompleted".equals(operation)) {
Madan Jampanice240732016-09-02 10:42:58 -070095 WorkQueueStats stats = get(queue.stats());
96 print("%d", stats.totalCompleted());
Jon Hall41e6cd72017-03-28 13:57:59 -070097 } else if ("destroy".equals(operation)) {
Madan Jampanid4684b42016-09-02 22:26:31 -070098 get(queue.destroy());
Madan Jampanice240732016-09-02 10:42:58 -070099 } else {
100 print("Invalid operation name. Valid operations names are:"
Madan Jampanid4684b42016-09-02 22:26:31 -0700101 + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy]");
Madan Jampanice240732016-09-02 10:42:58 -0700102 }
103 }
104
105 private <T> T get(CompletableFuture<T> future) {
106 try {
Jordan Halterman6440b092017-05-24 17:48:08 -0700107 return future.get(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
Madan Jampanice240732016-09-02 10:42:58 -0700108 } catch (Exception e) {
Ray Milkey6a51cb92018-03-06 09:03:03 -0800109 throw new IllegalStateException(e);
Madan Jampanice240732016-09-02 10:42:58 -0700110 }
111 }
112}