Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 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.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.store.service.AtomicCounter; |
| 22 | import 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") |
| 29 | public 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 Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 57 | if ("get".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 58 | print("%d", atomicCounter.get()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 59 | } else if ("set".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 60 | atomicCounter.set(value1); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 61 | } else if ("incrementAndGet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 62 | print("%d", atomicCounter.incrementAndGet()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 63 | } else if ("getAndIncrement".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 64 | print("%d", atomicCounter.getAndIncrement()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 65 | } else if ("getAndAdd".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 66 | print("%d", atomicCounter.getAndAdd(value1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 67 | } else if ("addAndGet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 68 | print("%d", atomicCounter.addAndGet(value1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 69 | } else if ("compareAndSet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 70 | print("%b", atomicCounter.compareAndSet(value1, value2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 71 | } else if ("destroy".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 72 | atomicCounter.destroy(); |
| 73 | } |
| 74 | } |
| 75 | } |