blob: c23b6427e5e9c0da7792e1ab9e44e533f1d7663e [file] [log] [blame]
Thomas Vachuska4cfcc562015-06-03 09:51:02 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4cfcc562015-06-03 09:51:02 -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;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.ui.UiExtension;
23import org.onosproject.ui.UiExtensionService;
24
25import java.util.List;
26
27/**
Simon Hunt23f9c7b2017-07-10 20:00:30 -070028 * Lists all registered UI views.
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070029 */
30@Command(scope = "onos", name = "ui-views",
Simon Hunt23f9c7b2017-07-10 20:00:30 -070031 description = "Lists all registered UI views")
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070032public class UiViewListCommand extends AbstractShellCommand {
33
34 private static final String FMT = "id=%s, category=%s, label=%s, icon=%s";
35
36 @Override
37 protected void execute() {
38 UiExtensionService service = get(UiExtensionService.class);
39 if (outputJson()) {
40 print("%s", json(service.getExtensions()));
41 } else {
42 service.getExtensions().forEach(ext -> ext.views()
Simon Hunt23f9c7b2017-07-10 20:00:30 -070043 .forEach(v -> print(FMT, v.id(), v.category(),
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070044 v.label(), v.iconId())));
45 }
46 }
47
48 private JsonNode json(List<UiExtension> extensions) {
49 ObjectMapper mapper = new ObjectMapper();
50 ArrayNode node = mapper.createArrayNode();
51 extensions.forEach(ext -> ext.views()
52 .forEach(v -> node.add(mapper.createObjectNode()
53 .put("id", v.id())
Simon Hunt23f9c7b2017-07-10 20:00:30 -070054 .put("category", v.category().toString())
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070055 .put("label", v.label())
56 .put("icon", v.iconId()))));
57 return node;
58 }
59
60}