2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.distributedprimitives.cli;
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;
26 import java.util.concurrent.CompletableFuture;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeUnit;
29 import java.util.concurrent.TimeoutException;
31 import static org.slf4j.LoggerFactory.getLogger;
34 * CLI command to increment a distributed counter.
36 @Command(scope = "onos", name = "counter-test-increment",
37 description = "Increment a distributed counter")
38 public class CounterTestIncrementCommand extends AbstractShellCommand {
40 private final Logger log = getLogger(getClass());
42 @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
43 required = false, multiValued = false)
44 private boolean inMemory = false;
46 @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding",
47 required = false, multiValued = false)
48 private boolean getFirst = false;
50 @Argument(index = 0, name = "counter",
51 description = "Counter name",
52 required = true, multiValued = false)
53 String counter = null;
55 @Argument(index = 1, name = "delta",
56 description = "Long to add to the counter",
57 required = false, multiValued = false)
60 AsyncAtomicCounter atomicCounter;
64 protected void execute() {
65 StorageService storageService = get(StorageService.class);
67 atomicCounter = storageService.atomicCounterBuilder()
69 .withPartitionsDisabled()
72 atomicCounter = storageService.atomicCounterBuilder()
76 CompletableFuture<Long> result;
79 result = atomicCounter.getAndAdd(delta);
81 result = atomicCounter.addAndGet(delta);
85 result = atomicCounter.getAndIncrement();
87 result = atomicCounter.incrementAndGet();
91 print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS));
92 } catch (InterruptedException e) {
94 } catch (ExecutionException | TimeoutException e) {