blob: 1025208a8f8b62544fc79a6ce9f07883ef0bef17 [file] [log] [blame]
Madan Jampania89f8f92015-04-01 14:39:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampania89f8f92015-04-01 14:39:54 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.store.service.MapInfo;
24import org.onosproject.store.service.StorageAdminService;
25
26import com.fasterxml.jackson.databind.JsonNode;
27import com.fasterxml.jackson.databind.ObjectMapper;
28import com.fasterxml.jackson.databind.node.ArrayNode;
29import com.fasterxml.jackson.databind.node.ObjectNode;
30
31/**
32 * Command to list the various maps in the system.
33 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034@Service
Madan Jampania89f8f92015-04-01 14:39:54 -070035@Command(scope = "onos", name = "maps",
36 description = "Lists information about consistent maps in the system")
37public class MapsListCommand extends AbstractShellCommand {
38
39 // TODO: Add support to display different eventually
40 // consistent maps as well.
41
Madan Jampani3e033bd2015-04-08 13:03:49 -070042 private static final String FMT = "name=%s size=%d";
Madan Jampania89f8f92015-04-01 14:39:54 -070043
44 /**
45 * Displays map info as text.
46 *
47 * @param mapInfo map descriptions
48 */
49 private void displayMaps(List<MapInfo> mapInfo) {
Madan Jampania89f8f92015-04-01 14:39:54 -070050 for (MapInfo info : mapInfo) {
51 print(FMT, info.name(), info.size());
52 }
Madan Jampania89f8f92015-04-01 14:39:54 -070053 }
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 SHIMIZUa09e1bb2016-08-01 14:25:25 -070065 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 Jampania89f8f92015-04-01 14:39:54 -070071
72 return maps;
73 }
74
75 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 protected void doExecute() {
Madan Jampania89f8f92015-04-01 14:39:54 -070077 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}