blob: d93ad78f57364cd9a29c11b0f8957333e3654c2e [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
2 * Copyright 2015 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 */
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
42 @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
43 required = false, multiValued = false)
44 private boolean inMemory = false;
45
Jon Hallf591b652015-07-16 16:36:19 -070046 @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding",
47 required = false, multiValued = false)
48 private boolean getFirst = false;
49
Jon Hall6b687cd2015-04-23 20:04:59 -070050 @Argument(index = 0, name = "counter",
51 description = "Counter name",
52 required = true, multiValued = false)
53 String counter = null;
54
Jon Hallf591b652015-07-16 16:36:19 -070055 @Argument(index = 1, name = "delta",
56 description = "Long to add to the counter",
57 required = false, multiValued = false)
58 Long delta = null;
59
Jon Hall6b687cd2015-04-23 20:04:59 -070060 AsyncAtomicCounter atomicCounter;
61
62
63 @Override
64 protected void execute() {
65 StorageService storageService = get(StorageService.class);
66 if (inMemory) {
67 atomicCounter = storageService.atomicCounterBuilder()
68 .withName(counter)
69 .withPartitionsDisabled()
70 .buildAsyncCounter();
71 } else {
72 atomicCounter = storageService.atomicCounterBuilder()
73 .withName(counter)
74 .buildAsyncCounter();
75 }
Jon Hallf591b652015-07-16 16:36:19 -070076 CompletableFuture<Long> result;
77 if (delta != null) {
78 if (getFirst) {
79 result = atomicCounter.getAndAdd(delta);
80 } else {
81 result = atomicCounter.addAndGet(delta);
82 }
83 } else {
84 if (getFirst) {
85 result = atomicCounter.getAndIncrement();
86 } else {
87 result = atomicCounter.incrementAndGet();
88 }
89 }
Jon Hall6b687cd2015-04-23 20:04:59 -070090 try {
Jon Hallf591b652015-07-16 16:36:19 -070091 print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS));
Jon Hall6b687cd2015-04-23 20:04:59 -070092 } catch (InterruptedException e) {
93 return;
Sho SHIMIZUf578b432015-09-11 11:29:01 -070094 } catch (ExecutionException | TimeoutException e) {
Jon Hall1ea57e12015-09-30 11:26:38 -070095 print("Error executing command");
96 log.error("Error executing command counter-test-increment", e);
Jon Hall6b687cd2015-04-23 20:04:59 -070097 }
98 }
99}