Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.apache.karaf.shell.commands.Option; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 23 | import org.onosproject.store.service.StorageService; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import java.util.concurrent.CompletableFuture; |
| 27 | import java.util.concurrent.ExecutionException; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | import java.util.concurrent.TimeoutException; |
| 30 | |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * CLI command to increment a distributed counter. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "counter-test-increment", |
| 37 | description = "Increment a distributed counter") |
| 38 | public class CounterTestIncrementCommand extends AbstractShellCommand { |
| 39 | |
| 40 | private final Logger log = getLogger(getClass()); |
| 41 | |
| 42 | @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?", |
| 43 | required = false, multiValued = false) |
| 44 | private boolean inMemory = false; |
| 45 | |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 46 | @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding", |
| 47 | required = false, multiValued = false) |
| 48 | private boolean getFirst = false; |
| 49 | |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 50 | @Argument(index = 0, name = "counter", |
| 51 | description = "Counter name", |
| 52 | required = true, multiValued = false) |
| 53 | String counter = null; |
| 54 | |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 55 | @Argument(index = 1, name = "delta", |
| 56 | description = "Long to add to the counter", |
| 57 | required = false, multiValued = false) |
| 58 | Long delta = null; |
| 59 | |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 60 | AsyncAtomicCounter atomicCounter; |
| 61 | |
| 62 | |
| 63 | @Override |
| 64 | protected void execute() { |
| 65 | StorageService storageService = get(StorageService.class); |
| 66 | if (inMemory) { |
| 67 | atomicCounter = storageService.atomicCounterBuilder() |
| 68 | .withName(counter) |
| 69 | .withPartitionsDisabled() |
| 70 | .buildAsyncCounter(); |
| 71 | } else { |
| 72 | atomicCounter = storageService.atomicCounterBuilder() |
| 73 | .withName(counter) |
| 74 | .buildAsyncCounter(); |
| 75 | } |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 76 | CompletableFuture<Long> result; |
| 77 | if (delta != null) { |
| 78 | if (getFirst) { |
| 79 | result = atomicCounter.getAndAdd(delta); |
| 80 | } else { |
| 81 | result = atomicCounter.addAndGet(delta); |
| 82 | } |
| 83 | } else { |
| 84 | if (getFirst) { |
| 85 | result = atomicCounter.getAndIncrement(); |
| 86 | } else { |
| 87 | result = atomicCounter.incrementAndGet(); |
| 88 | } |
| 89 | } |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 90 | try { |
Jon Hall | f591b65 | 2015-07-16 16:36:19 -0700 | [diff] [blame] | 91 | print("%s was updated to %d", counter, result.get(3, TimeUnit.SECONDS)); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 92 | } catch (InterruptedException e) { |
| 93 | return; |
| 94 | } catch (ExecutionException e) { |
| 95 | e.printStackTrace(); |
| 96 | } catch (TimeoutException e) { |
| 97 | e.printStackTrace(); |
| 98 | } |
| 99 | } |
| 100 | } |