Madan Jampani | aac5888 | 2016-08-31 14:24:31 -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 | aac5888 | 2016-08-31 14:24:31 -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.cli.net; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Madan Jampani | aac5888 | 2016-08-31 14:24:31 -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 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | |
| 28 | /** |
| 29 | * Command to display the current value of a atomic counter. |
| 30 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | @Service |
Madan Jampani | aac5888 | 2016-08-31 14:24:31 -0700 | [diff] [blame] | 32 | @Command(scope = "onos", name = "counter", |
| 33 | description = "Displays the current value of a atomic counter") |
| 34 | public class CounterCommand extends AbstractShellCommand { |
| 35 | |
| 36 | @Argument(index = 0, name = "counterName", description = "Counter Name", |
| 37 | required = true, multiValued = false) |
| 38 | String name = null; |
| 39 | |
| 40 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | protected void doExecute() { |
Madan Jampani | aac5888 | 2016-08-31 14:24:31 -0700 | [diff] [blame] | 42 | StorageService storageService = get(StorageService.class); |
| 43 | AtomicCounter counter = storageService.getAtomicCounter(name); |
| 44 | |
| 45 | if (outputJson()) { |
| 46 | ObjectMapper mapper = new ObjectMapper(); |
| 47 | ObjectNode counterJsonNode = mapper.createObjectNode(); |
| 48 | counterJsonNode.put("value", counter.get()); |
| 49 | print("%s", counterJsonNode); |
| 50 | } else { |
| 51 | print("%d", counter.get()); |
| 52 | } |
| 53 | } |
| 54 | } |