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