blob: 13e886704aa77aa4c09a74deb051a133695c24f8 [file] [log] [blame]
Madan Jampania89f8f92015-04-01 14:39:54 -07001/*
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 */
16package org.onosproject.cli.net;
17
18import java.util.List;
19
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.service.MapInfo;
23import org.onosproject.store.service.StorageAdminService;
24
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ArrayNode;
28import 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")
35public class MapsListCommand extends AbstractShellCommand {
36
37 // TODO: Add support to display different eventually
38 // consistent maps as well.
39
40 private static final String FMT = "%-20s %8s";
41
42 /**
43 * Displays map info as text.
44 *
45 * @param mapInfo map descriptions
46 */
47 private void displayMaps(List<MapInfo> mapInfo) {
48 print("------------------------------");
49 print(FMT, "Name", "Size");
50 print("------------------------------");
51
52
53 for (MapInfo info : mapInfo) {
54 print(FMT, info.name(), info.size());
55 }
56 if (mapInfo.size() > 0) {
57 print("------------------------------");
58 }
59 }
60
61 /**
62 * Converts list of map info into a JSON object.
63 *
64 * @param mapInfo map descriptions
65 */
66 private JsonNode json(List<MapInfo> mapInfo) {
67 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode maps = mapper.createArrayNode();
69
70 // Create a JSON node for each map
71 mapInfo.stream()
72 .forEach(info -> {
73 ObjectNode map = mapper.createObjectNode();
74 map.put("name", info.name())
75 .put("size", info.size());
76 maps.add(map);
77 });
78
79 return maps;
80 }
81
82 @Override
83 protected void execute() {
84 StorageAdminService storageAdminService = get(StorageAdminService.class);
85 List<MapInfo> mapInfo = storageAdminService.getMapInfo();
86 if (outputJson()) {
87 print("%s", json(mapInfo));
88 } else {
89 displayMaps(mapInfo);
90 }
91 }
92}