blob: 03db8037ee7bd0030618da06c6ba4560283e7b61 [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jon Hall6b687cd2015-04-23 20:04:59 -07003 *
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 */
16package org.onosproject.distributedprimitives.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.service.AsyncAtomicCounter;
23import org.onosproject.store.service.StorageService;
24import org.slf4j.Logger;
25
26import java.util.concurrent.CompletableFuture;
27import java.util.concurrent.ExecutionException;
28import java.util.concurrent.TimeUnit;
29import java.util.concurrent.TimeoutException;
30
31import 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")
38public class CounterTestIncrementCommand extends AbstractShellCommand {
39
40 private final Logger log = getLogger(getClass());
41
Jon Hallf591b652015-07-16 16:36:19 -070042 @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 Hall6b687cd2015-04-23 20:04:59 -070046 @Argument(index = 0, name = "counter",
47 description = "Counter name",
48 required = true, multiValued = false)
49 String counter = null;
50
Jon Hallf591b652015-07-16 16:36:19 -070051 @Argument(index = 1, name = "delta",
52 description = "Long to add to the counter",
53 required = false, multiValued = false)
54 Long delta = null;
55
Jon Hall6b687cd2015-04-23 20:04:59 -070056 AsyncAtomicCounter atomicCounter;
57
58
59 @Override
60 protected void execute() {
61 StorageService storageService = get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -070062 atomicCounter = storageService.getAsyncAtomicCounter(counter);
Jon Hallf591b652015-07-16 16:36:19 -070063 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 Hall6b687cd2015-04-23 20:04:59 -070077 try {
Jon Hallf591b652015-07-16 16:36:19 -070078 print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS));
Jon Hall6b687cd2015-04-23 20:04:59 -070079 } catch (InterruptedException e) {
80 return;
Sho SHIMIZUf578b432015-09-11 11:29:01 -070081 } catch (ExecutionException | TimeoutException e) {
Jon Hall1ea57e12015-09-30 11:26:38 -070082 print("Error executing command");
83 log.error("Error executing command counter-test-increment", e);
Jon Hall6b687cd2015-04-23 20:04:59 -070084 }
85 }
86}