12c8140abad5c51d38a327877b91e248a2c92a99
[onosfw.git] /
1 /*
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  */
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
42     @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
43             required = false, multiValued = false)
44     private boolean inMemory = false;
45
46     @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding",
47             required = false, multiValued = false)
48     private boolean getFirst = false;
49
50     @Argument(index = 0, name = "counter",
51             description = "Counter name",
52             required = true, multiValued = false)
53     String counter = null;
54
55     @Argument(index = 1, name = "delta",
56             description = "Long to add to the counter",
57             required = false, multiValued = false)
58     Long delta = null;
59
60     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         }
76         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         }
90         try {
91             print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS));
92         } catch (InterruptedException e) {
93             return;
94         } catch (ExecutionException | TimeoutException e) {
95             e.printStackTrace();
96         }
97     }
98 }