Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.service.AtomicCounter; |
| 23 | import org.onosproject.store.service.StorageService; |
| 24 | |
| 25 | /** |
| 26 | * CLI command to increment a distributed counter. |
| 27 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 28 | @Service |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 29 | @Command(scope = "onos", name = "counter-test", |
| 30 | description = "Manipulate a distributed counter") |
| 31 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 56 | protected void doExecute() { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 57 | StorageService storageService = get(StorageService.class); |
| 58 | atomicCounter = storageService.getAsyncAtomicCounter(counter).asAtomicCounter(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 59 | if ("get".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 60 | print("%d", atomicCounter.get()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 61 | } else if ("set".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 62 | atomicCounter.set(value1); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 63 | } else if ("incrementAndGet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 64 | print("%d", atomicCounter.incrementAndGet()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 65 | } else if ("getAndIncrement".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 66 | print("%d", atomicCounter.getAndIncrement()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 67 | } else if ("getAndAdd".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 68 | print("%d", atomicCounter.getAndAdd(value1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 69 | } else if ("addAndGet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 70 | print("%d", atomicCounter.addAndGet(value1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 71 | } else if ("compareAndSet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 72 | print("%b", atomicCounter.compareAndSet(value1, value2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 73 | } else if ("destroy".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 74 | atomicCounter.destroy(); |
| 75 | } |
| 76 | } |
| 77 | } |