Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 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.distributedprimitives.cli; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collection; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
| 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.store.serializers.KryoNamespaces; |
Jon Hall | 7f41225 | 2017-02-22 17:15:19 -0800 | [diff] [blame] | 27 | import org.onosproject.store.service.DistributedPrimitive; |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 28 | import org.onosproject.store.service.Serializer; |
| 29 | import org.onosproject.store.service.StorageService; |
| 30 | import org.onosproject.store.service.Task; |
| 31 | import org.onosproject.store.service.WorkQueue; |
| 32 | import org.onosproject.store.service.WorkQueueStats; |
| 33 | |
| 34 | import 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") |
| 41 | public 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 Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 49 | description = "operation name. One of {add, addMultiple, " |
Madan Jampani | d4684b4 | 2016-09-02 22:26:31 -0700 | [diff] [blame] | 50 | + "takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy}", |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 51 | 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 Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 71 | if ("add".equals(operation)) { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 72 | if (value1 == null) { |
| 73 | print("Usage: add <value1>"); |
| 74 | } else { |
| 75 | get(queue.addOne(value1)); |
| 76 | print("Done"); |
| 77 | } |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 78 | } else if ("addMultiple".equals(operation)) { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 79 | 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 Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 85 | } else if ("takeAndComplete".equals(operation)) { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 86 | 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 Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 90 | } else if ("totalPending".equals(operation)) { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 91 | WorkQueueStats stats = get(queue.stats()); |
| 92 | print("%d", stats.totalPending()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 93 | } else if ("totalInProgress".equals(operation)) { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 94 | WorkQueueStats stats = get(queue.stats()); |
| 95 | print("%d", stats.totalInProgress()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 96 | } else if ("totalCompleted".equals(operation)) { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 97 | WorkQueueStats stats = get(queue.stats()); |
| 98 | print("%d", stats.totalCompleted()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 99 | } else if ("destroy".equals(operation)) { |
Madan Jampani | d4684b4 | 2016-09-02 22:26:31 -0700 | [diff] [blame] | 100 | get(queue.destroy()); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 101 | } else { |
| 102 | print("Invalid operation name. Valid operations names are:" |
Madan Jampani | d4684b4 | 2016-09-02 22:26:31 -0700 | [diff] [blame] | 103 | + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy]"); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
| 107 | private <T> T get(CompletableFuture<T> future) { |
| 108 | try { |
Jon Hall | 7f41225 | 2017-02-22 17:15:19 -0800 | [diff] [blame] | 109 | return future.get(DistributedPrimitive.DEFAULT_OPERTATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 110 | } catch (Exception e) { |
| 111 | throw Throwables.propagate(e); |
| 112 | } |
| 113 | } |
| 114 | } |