blob: a48d870f120679e6534b95bdc9f7fcc5da634ae2 [file] [log] [blame]
Madan Jampania89f8f92015-04-01 14:39:54 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampania89f8f92015-04-01 14:39:54 -07003 *
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
Madan Jampani3e033bd2015-04-08 13:03:49 -070040 private static final String FMT = "name=%s size=%d";
Madan Jampania89f8f92015-04-01 14:39:54 -070041
42 /**
43 * Displays map info as text.
44 *
45 * @param mapInfo map descriptions
46 */
47 private void displayMaps(List<MapInfo> mapInfo) {
Madan Jampania89f8f92015-04-01 14:39:54 -070048 for (MapInfo info : mapInfo) {
49 print(FMT, info.name(), info.size());
50 }
Madan Jampania89f8f92015-04-01 14:39:54 -070051 }
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
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070063 mapInfo.forEach(info -> {
64 ObjectNode map = mapper.createObjectNode();
65 map.put("name", info.name())
66 .put("size", info.size());
67 maps.add(map);
68 });
Madan Jampania89f8f92015-04-01 14:39:54 -070069
70 return maps;
71 }
72
73 @Override
74 protected void execute() {
75 StorageAdminService storageAdminService = get(StorageAdminService.class);
76 List<MapInfo> mapInfo = storageAdminService.getMapInfo();
77 if (outputJson()) {
78 print("%s", json(mapInfo));
79 } else {
80 displayMaps(mapInfo);
81 }
82 }
83}