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