blob: bf64f00f5dda6193291f44a7063a1e890b415957 [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
46 @Argument(index = 0, name = "counter",
47 description = "Counter name",
48 required = true, multiValued = false)
49 String counter = null;
50
51 AsyncAtomicCounter atomicCounter;
52
53
54 @Override
55 protected void execute() {
56 StorageService storageService = get(StorageService.class);
57 if (inMemory) {
58 atomicCounter = storageService.atomicCounterBuilder()
59 .withName(counter)
60 .withPartitionsDisabled()
61 .buildAsyncCounter();
62 } else {
63 atomicCounter = storageService.atomicCounterBuilder()
64 .withName(counter)
65 .buildAsyncCounter();
66 }
67
68 CompletableFuture<Long> result = atomicCounter.incrementAndGet();
69 try {
70 print("%s was incremented to %d", counter, result.get(3, TimeUnit.SECONDS));
71 } catch (InterruptedException e) {
72 return;
73 } catch (ExecutionException e) {
74 e.printStackTrace();
75 } catch (TimeoutException e) {
76 e.printStackTrace();
77 }
78 }
79}