blob: 04d75c2c3ee240b073a539249b0637d6bb0b4f27 [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jon Hall6b687cd2015-04-23 20:04:59 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.store.service.AsyncAtomicCounter;
24import org.onosproject.store.service.StorageService;
25import org.slf4j.Logger;
26
27import java.util.concurrent.CompletableFuture;
28import java.util.concurrent.ExecutionException;
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.TimeoutException;
31
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * CLI command to increment a distributed counter.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Jon Hall6b687cd2015-04-23 20:04:59 -070038@Command(scope = "onos", name = "counter-test-increment",
39 description = "Increment a distributed counter")
40public class CounterTestIncrementCommand extends AbstractShellCommand {
41
42 private final Logger log = getLogger(getClass());
43
Jon Hallf591b652015-07-16 16:36:19 -070044 @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding",
45 required = false, multiValued = false)
46 private boolean getFirst = false;
47
Jon Hall6b687cd2015-04-23 20:04:59 -070048 @Argument(index = 0, name = "counter",
49 description = "Counter name",
50 required = true, multiValued = false)
51 String counter = null;
52
Jon Hallf591b652015-07-16 16:36:19 -070053 @Argument(index = 1, name = "delta",
54 description = "Long to add to the counter",
55 required = false, multiValued = false)
56 Long delta = null;
57
Jon Hall6b687cd2015-04-23 20:04:59 -070058 AsyncAtomicCounter atomicCounter;
59
60
61 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070062 protected void doExecute() {
Jon Hall6b687cd2015-04-23 20:04:59 -070063 StorageService storageService = get(StorageService.class);
Madan Jampanid5714e02016-04-19 14:15:20 -070064 atomicCounter = storageService.getAsyncAtomicCounter(counter);
Jon Hallf591b652015-07-16 16:36:19 -070065 CompletableFuture<Long> result;
66 if (delta != null) {
67 if (getFirst) {
68 result = atomicCounter.getAndAdd(delta);
69 } else {
70 result = atomicCounter.addAndGet(delta);
71 }
72 } else {
73 if (getFirst) {
74 result = atomicCounter.getAndIncrement();
75 } else {
76 result = atomicCounter.incrementAndGet();
77 }
78 }
Jon Hall6b687cd2015-04-23 20:04:59 -070079 try {
Jon Hallf591b652015-07-16 16:36:19 -070080 print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS));
Jon Hall6b687cd2015-04-23 20:04:59 -070081 } catch (InterruptedException e) {
82 return;
Sho SHIMIZUf578b432015-09-11 11:29:01 -070083 } catch (ExecutionException | TimeoutException e) {
Jon Hall1ea57e12015-09-30 11:26:38 -070084 print("Error executing command");
85 log.error("Error executing command counter-test-increment", e);
Jon Hall6b687cd2015-04-23 20:04:59 -070086 }
87 }
88}