Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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(); |
| 57 | if (operation.equals("get")) { |
| 58 | print("%d", atomicCounter.get()); |
| 59 | } else if (operation.equals("set")) { |
| 60 | atomicCounter.set(value1); |
| 61 | } else if (operation.equals("incrementAndGet")) { |
| 62 | print("%d", atomicCounter.incrementAndGet()); |
| 63 | } else if (operation.equals("getAndIncrement")) { |
| 64 | print("%d", atomicCounter.getAndIncrement()); |
| 65 | } else if (operation.equals("getAndAdd")) { |
| 66 | print("%d", atomicCounter.getAndAdd(value1)); |
| 67 | } else if (operation.equals("addAndGet")) { |
| 68 | print("%d", atomicCounter.addAndGet(value1)); |
| 69 | } else if (operation.equals("compareAndSet")) { |
| 70 | print("%b", atomicCounter.compareAndSet(value1, value2)); |
| 71 | } else if (operation.equals("destroy")) { |
| 72 | atomicCounter.destroy(); |
| 73 | } |
| 74 | } |
| 75 | } |