Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -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.cli.net; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.service.StorageAdminService; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.JsonNode; |
| 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 27 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 28 | |
| 29 | /** |
| 30 | * Command to list the various counters in the system. |
| 31 | */ |
| 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 | |
| 38 | /** |
| 39 | * Displays counters as text. |
| 40 | * |
Charles M.C. Chan | 6f5bdc6 | 2015-04-22 01:21:09 +0800 | [diff] [blame] | 41 | * @param counters counter info |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 42 | */ |
| 43 | private void displayCounters(Map<String, Long> counters) { |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 44 | counters.forEach((name, value) -> print(FMT, name, value)); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Converts info for counters into a JSON object. |
| 49 | * |
| 50 | * @param counters counter info |
| 51 | */ |
| 52 | private JsonNode json(Map<String, Long> counters) { |
| 53 | ObjectMapper mapper = new ObjectMapper(); |
| 54 | ArrayNode jsonCounters = mapper.createArrayNode(); |
| 55 | |
| 56 | // Create a JSON node for each counter |
| 57 | counters.forEach((name, value) -> { |
| 58 | ObjectNode jsonCounter = mapper.createObjectNode(); |
| 59 | jsonCounter.put("name", name) |
| 60 | .put("value", value); |
| 61 | jsonCounters.add(jsonCounter); |
| 62 | }); |
| 63 | |
| 64 | return jsonCounters; |
| 65 | } |
| 66 | |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Converts info for counters from different databases into a JSON object. |
| 69 | * |
| 70 | * @param partitionedDbCounters counters info |
| 71 | * @param inMemoryDbCounters counters info |
| 72 | */ |
| 73 | private JsonNode jsonAllCounters(Map<String, Long> partitionedDbCounters, |
| 74 | Map<String, Long> inMemoryDbCounters) { |
| 75 | ObjectMapper mapper = new ObjectMapper(); |
| 76 | ArrayNode jsonCounters = mapper.createArrayNode(); |
| 77 | |
| 78 | // Create a JSON node for partitioned database counter |
| 79 | ObjectNode jsonPartitionedDatabaseCounters = mapper.createObjectNode(); |
Ray Milkey | 6e8b81e | 2015-06-25 13:08:02 -0700 | [diff] [blame] | 80 | jsonPartitionedDatabaseCounters.set("partitionedDatabaseCounters", |
Jon Hall | a1cc16f | 2015-05-27 14:59:01 -0700 | [diff] [blame] | 81 | json(partitionedDbCounters)); |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 82 | jsonCounters.add(jsonPartitionedDatabaseCounters); |
| 83 | // Create a JSON node for in-memory database counter |
| 84 | ObjectNode jsonInMemoryDatabseCounters = mapper.createObjectNode(); |
Ray Milkey | 6e8b81e | 2015-06-25 13:08:02 -0700 | [diff] [blame] | 85 | jsonInMemoryDatabseCounters.set("inMemoryDatabaseCounters", |
Jon Hall | a1cc16f | 2015-05-27 14:59:01 -0700 | [diff] [blame] | 86 | json(inMemoryDbCounters)); |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 87 | jsonCounters.add(jsonInMemoryDatabseCounters); |
| 88 | |
| 89 | return jsonCounters; |
| 90 | } |
| 91 | |
| 92 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 93 | @Override |
| 94 | protected void execute() { |
| 95 | StorageAdminService storageAdminService = get(StorageAdminService.class); |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 96 | Map<String, Long> partitionedDatabaseCounters = storageAdminService.getPartitionedDatabaseCounters(); |
| 97 | Map<String, Long> inMemoryDatabaseCounters = storageAdminService.getInMemoryDatabaseCounters(); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 98 | if (outputJson()) { |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 99 | print("%s", jsonAllCounters(partitionedDatabaseCounters, inMemoryDatabaseCounters)); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 100 | } else { |
Kaouther Abrougui | d8b565a | 2015-05-20 16:07:20 -0700 | [diff] [blame] | 101 | print("Partitioned database counters:"); |
| 102 | displayCounters(partitionedDatabaseCounters); |
| 103 | print("In-memory database counters:"); |
| 104 | displayCounters(inMemoryDatabaseCounters); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |