Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -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 | a89f8f9 | 2015-04-01 14:39:54 -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.List; |
| 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 | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.store.service.MapInfo; |
| 24 | import org.onosproject.store.service.StorageAdminService; |
| 25 | |
| 26 | import com.fasterxml.jackson.databind.JsonNode; |
| 27 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 29 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 30 | |
| 31 | /** |
| 32 | * Command to list the various maps in the system. |
| 33 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 34 | @Service |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 35 | @Command(scope = "onos", name = "maps", |
| 36 | description = "Lists information about consistent maps in the system") |
| 37 | public class MapsListCommand extends AbstractShellCommand { |
| 38 | |
| 39 | // TODO: Add support to display different eventually |
| 40 | // consistent maps as well. |
| 41 | |
Madan Jampani | 3e033bd | 2015-04-08 13:03:49 -0700 | [diff] [blame] | 42 | private static final String FMT = "name=%s size=%d"; |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Displays map info as text. |
| 46 | * |
| 47 | * @param mapInfo map descriptions |
| 48 | */ |
| 49 | private void displayMaps(List<MapInfo> mapInfo) { |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 50 | for (MapInfo info : mapInfo) { |
| 51 | print(FMT, info.name(), info.size()); |
| 52 | } |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Converts list of map info into a JSON object. |
| 57 | * |
| 58 | * @param mapInfo map descriptions |
| 59 | */ |
| 60 | private JsonNode json(List<MapInfo> mapInfo) { |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | ArrayNode maps = mapper.createArrayNode(); |
| 63 | |
| 64 | // Create a JSON node for each map |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 65 | mapInfo.forEach(info -> { |
| 66 | ObjectNode map = mapper.createObjectNode(); |
| 67 | map.put("name", info.name()) |
| 68 | .put("size", info.size()); |
| 69 | maps.add(map); |
| 70 | }); |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 71 | |
| 72 | return maps; |
| 73 | } |
| 74 | |
| 75 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 76 | protected void doExecute() { |
Madan Jampani | a89f8f9 | 2015-04-01 14:39:54 -0700 | [diff] [blame] | 77 | StorageAdminService storageAdminService = get(StorageAdminService.class); |
| 78 | List<MapInfo> mapInfo = storageAdminService.getMapInfo(); |
| 79 | if (outputJson()) { |
| 80 | print("%s", json(mapInfo)); |
| 81 | } else { |
| 82 | displayMaps(mapInfo); |
| 83 | } |
| 84 | } |
| 85 | } |