blob: 92d365b5b2b30f84291795df89c0707202c5adae [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/**
28 * Lists all UI views.
29 */
30@Command(scope = "onos", name = "ui-views",
31 description = "Lists all UI views")
32public 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()
43 .forEach(v -> print(FMT, v.id(), v.category().label(),
44 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())
54 .put("category", v.category().label())
55 .put("label", v.label())
56 .put("icon", v.iconId()))));
57 return node;
58 }
59
60}