Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -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 | |
| 18 | import java.util.Map; |
| 19 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.store.service.StorageAdminService; |
| 24 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | |
| 28 | /** |
| 29 | * Command to list the various counters in the system. |
| 30 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 31 | @Service |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 32 | @Command(scope = "onos", name = "counters", |
| 33 | description = "Lists information about atomic counters in the system") |
| 34 | public class CountersListCommand extends AbstractShellCommand { |
| 35 | |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 36 | private static final String FMT = "name=%s value=%d"; |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 37 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 38 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | protected void doExecute() { |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 40 | StorageAdminService storageAdminService = get(StorageAdminService.class); |
Madan Jampani | 832686d | 2016-04-04 21:57:26 -0700 | [diff] [blame] | 41 | Map<String, Long> counters = storageAdminService.getCounters(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 42 | if (outputJson()) { |
Madan Jampani | 832686d | 2016-04-04 21:57:26 -0700 | [diff] [blame] | 43 | ObjectMapper mapper = new ObjectMapper(); |
| 44 | ObjectNode jsonCounters = mapper.createObjectNode(); |
| 45 | counters.forEach((k, v) -> jsonCounters.put(k, v)); |
| 46 | print("%s", jsonCounters); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 47 | } else { |
Madan Jampani | 832686d | 2016-04-04 21:57:26 -0700 | [diff] [blame] | 48 | counters.keySet().stream().sorted().forEach(name -> print(FMT, name, counters.get(name))); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |