Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 27 | import org.onosproject.store.service.Serializer; |
| 28 | import org.onosproject.store.service.StorageService; |
| 29 | import org.onosproject.store.service.Task; |
| 30 | import org.onosproject.store.service.WorkQueue; |
| 31 | import org.onosproject.store.service.WorkQueueStats; |
| 32 | |
| 33 | import 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") |
| 40 | public 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, " |
Madan Jampani | d4684b4 | 2016-09-02 22:26:31 -0700 | [diff] [blame] | 49 | + "takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy}", |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 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()); |
Madan Jampani | d4684b4 | 2016-09-02 22:26:31 -0700 | [diff] [blame] | 98 | } else if (operation.equals("destroy")) { |
| 99 | get(queue.destroy()); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 100 | } else { |
| 101 | print("Invalid operation name. Valid operations names are:" |
Madan Jampani | d4684b4 | 2016-09-02 22:26:31 -0700 | [diff] [blame] | 102 | + " [add, addMultiple takeAndComplete, totalPending, totalInProgress, totalCompleted, destroy]"); |
Madan Jampani | ce24073 | 2016-09-02 10:42:58 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | private <T> T get(CompletableFuture<T> future) { |
| 107 | try { |
| 108 | return future.get(1, TimeUnit.SECONDS); |
| 109 | } catch (Exception e) { |
| 110 | throw Throwables.propagate(e); |
| 111 | } |
| 112 | } |
| 113 | } |