Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -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; |
| 20 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 24 | import org.onosproject.store.service.StorageService; |
| 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import java.util.concurrent.CompletableFuture; |
| 28 | import java.util.concurrent.ExecutionException; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | import java.util.concurrent.TimeoutException; |
| 31 | |
| 32 | import static org.slf4j.LoggerFactory.getLogger; |
| 33 | |
| 34 | /** |
| 35 | * CLI command to increment a distributed counter. |
| 36 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 37 | @Service |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "counter-test-increment", |
| 39 | description = "Increment a distributed counter") |
| 40 | public class CounterTestIncrementCommand extends AbstractShellCommand { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
| 43 | |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 44 | @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding", |
| 45 | required = false, multiValued = false) |
| 46 | private boolean getFirst = false; |
| 47 | |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 48 | @Argument(index = 0, name = "counter", |
| 49 | description = "Counter name", |
| 50 | required = true, multiValued = false) |
| 51 | String counter = null; |
| 52 | |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 53 | @Argument(index = 1, name = "delta", |
| 54 | description = "Long to add to the counter", |
| 55 | required = false, multiValued = false) |
| 56 | Long delta = null; |
| 57 | |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 58 | AsyncAtomicCounter atomicCounter; |
| 59 | |
| 60 | |
| 61 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 62 | protected void doExecute() { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 63 | StorageService storageService = get(StorageService.class); |
Madan Jampani | d5714e0 | 2016-04-19 14:15:20 -0700 | [diff] [blame] | 64 | atomicCounter = storageService.getAsyncAtomicCounter(counter); |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 65 | CompletableFuture<Long> result; |
| 66 | if (delta != null) { |
| 67 | if (getFirst) { |
| 68 | result = atomicCounter.getAndAdd(delta); |
| 69 | } else { |
| 70 | result = atomicCounter.addAndGet(delta); |
| 71 | } |
| 72 | } else { |
| 73 | if (getFirst) { |
| 74 | result = atomicCounter.getAndIncrement(); |
| 75 | } else { |
| 76 | result = atomicCounter.incrementAndGet(); |
| 77 | } |
| 78 | } |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 79 | try { |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 80 | print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS)); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 81 | } catch (InterruptedException e) { |
| 82 | return; |
Sho SHIMIZU | f578b43 | 2015-09-11 11:29:01 -0700 | [diff] [blame] | 83 | } catch (ExecutionException | TimeoutException e) { |
Jon Hall | 1ea57e1 | 2015-09-30 11:26:38 -0700 | [diff] [blame] | 84 | print("Error executing command"); |
| 85 | log.error("Error executing command counter-test-increment", e); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |