Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.serializers.KryoNamespaces; |
Jon Hall | 7f41225 | 2017-02-22 17:15:19 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.DistributedPrimitive; |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 24 | import org.onosproject.store.service.Serializer; |
| 25 | import org.onosproject.store.service.StorageService; |
| 26 | import org.onosproject.store.service.Task; |
| 27 | import org.onosproject.store.service.WorkQueue; |
| 28 | import org.onosproject.store.service.WorkQueueStats; |
| 29 | |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 30 | import java.util.Arrays; |
| 31 | import java.util.Collection; |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | import java.util.concurrent.TimeUnit; |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * CLI command to test a distributed work queue. |
| 37 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 38 | @Service |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 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 |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 67 | protected void doExecute() { |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 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 { |
Jordan Halterman | 6440b09 | 2017-05-24 17:48:08 -0700 | [diff] [blame] | 109 | return future.get(DistributedPrimitive.DEFAULT_OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 110 | } catch (Exception e) { |
Ray Milkey | 6a51cb9 | 2018-03-06 09:03:03 -0800 | [diff] [blame] | 111 | throw new IllegalStateException(e); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |