blob: 4fd3f0f4c39787a88eaf1ab8389f023d9a8cb5d1 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.store.service.AtomicCounter;
22import org.onosproject.store.service.StorageService;
23
24/**
25 * CLI command to increment a distributed counter.
26 */
27@Command(scope = "onos", name = "counter-test",
28 description = "Manipulate a distributed counter")
29public class CounterTestCommand extends AbstractShellCommand {
30
31 @Argument(index = 0, name = "counter",
32 description = "Counter name",
33 required = true, multiValued = false)
34 String counter = null;
35
36 @Argument(index = 1, name = "operation",
37 description = "operation name",
38 required = true, multiValued = false)
39 String operation = null;
40
41 @Argument(index = 2, name = "value1",
42 description = "first arg",
43 required = false, multiValued = false)
44 Long value1 = null;
45
46 @Argument(index = 3, name = "value2",
47 description = "second arg",
48 required = false, multiValued = false)
49 Long value2 = null;
50
51 AtomicCounter atomicCounter;
52
53 @Override
54 protected void execute() {
55 StorageService storageService = get(StorageService.class);
56 atomicCounter = storageService.getAsyncAtomicCounter(counter).asAtomicCounter();
Jon Hall41e6cd72017-03-28 13:57:59 -070057 if ("get".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070058 print("%d", atomicCounter.get());
Jon Hall41e6cd72017-03-28 13:57:59 -070059 } else if ("set".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070060 atomicCounter.set(value1);
Jon Hall41e6cd72017-03-28 13:57:59 -070061 } else if ("incrementAndGet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070062 print("%d", atomicCounter.incrementAndGet());
Jon Hall41e6cd72017-03-28 13:57:59 -070063 } else if ("getAndIncrement".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070064 print("%d", atomicCounter.getAndIncrement());
Jon Hall41e6cd72017-03-28 13:57:59 -070065 } else if ("getAndAdd".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070066 print("%d", atomicCounter.getAndAdd(value1));
Jon Hall41e6cd72017-03-28 13:57:59 -070067 } else if ("addAndGet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070068 print("%d", atomicCounter.addAndGet(value1));
Jon Hall41e6cd72017-03-28 13:57:59 -070069 } else if ("compareAndSet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070070 print("%b", atomicCounter.compareAndSet(value1, value2));
Jon Hall41e6cd72017-03-28 13:57:59 -070071 } else if ("destroy".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070072 atomicCounter.destroy();
73 }
74 }
75}