blob: cdf0a97cdef67d0a2552d068255909f62f8906cd [file] [log] [blame]
Madan Jampani9e1a8c12016-06-27 11:19:10 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani9e1a8c12016-06-27 11:19:10 -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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.service.AtomicCounter;
23import org.onosproject.store.service.StorageService;
24
25/**
26 * CLI command to increment a distributed counter.
27 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070028@Service
Madan Jampani9e1a8c12016-06-27 11:19:10 -070029@Command(scope = "onos", name = "counter-test",
30 description = "Manipulate a distributed counter")
31public class CounterTestCommand extends AbstractShellCommand {
32
33 @Argument(index = 0, name = "counter",
34 description = "Counter name",
35 required = true, multiValued = false)
36 String counter = null;
37
38 @Argument(index = 1, name = "operation",
39 description = "operation name",
40 required = true, multiValued = false)
41 String operation = null;
42
43 @Argument(index = 2, name = "value1",
44 description = "first arg",
45 required = false, multiValued = false)
46 Long value1 = null;
47
48 @Argument(index = 3, name = "value2",
49 description = "second arg",
50 required = false, multiValued = false)
51 Long value2 = null;
52
53 AtomicCounter atomicCounter;
54
55 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070057 StorageService storageService = get(StorageService.class);
58 atomicCounter = storageService.getAsyncAtomicCounter(counter).asAtomicCounter();
Jon Hall41e6cd72017-03-28 13:57:59 -070059 if ("get".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070060 print("%d", atomicCounter.get());
Jon Hall41e6cd72017-03-28 13:57:59 -070061 } else if ("set".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070062 atomicCounter.set(value1);
Jon Hall41e6cd72017-03-28 13:57:59 -070063 } else if ("incrementAndGet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070064 print("%d", atomicCounter.incrementAndGet());
Jon Hall41e6cd72017-03-28 13:57:59 -070065 } else if ("getAndIncrement".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070066 print("%d", atomicCounter.getAndIncrement());
Jon Hall41e6cd72017-03-28 13:57:59 -070067 } else if ("getAndAdd".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070068 print("%d", atomicCounter.getAndAdd(value1));
Jon Hall41e6cd72017-03-28 13:57:59 -070069 } else if ("addAndGet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070070 print("%d", atomicCounter.addAndGet(value1));
Jon Hall41e6cd72017-03-28 13:57:59 -070071 } else if ("compareAndSet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070072 print("%b", atomicCounter.compareAndSet(value1, value2));
Jon Hall41e6cd72017-03-28 13:57:59 -070073 } else if ("destroy".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070074 atomicCounter.destroy();
75 }
76 }
77}