blob: cb0007d0ed4db0ed9aec14ee6946085e363d034d [file] [log] [blame]
Thomas Vachuska4cfcc562015-06-03 09:51:02 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 */
Thomas Vachuska99b7bbe2018-02-01 15:29:46 -080016package org.onosproject.ui.impl.cli;
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Thomas Vachuska99b7bbe2018-02-01 15:29:46 -080023import org.onosproject.cli.AbstractShellCommand;
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070024import org.onosproject.ui.UiExtension;
25import org.onosproject.ui.UiExtensionService;
26
27import java.util.List;
28
29/**
Simon Hunt23f9c7b2017-07-10 20:00:30 -070030 * Lists all registered UI views.
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070031 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070033@Command(scope = "onos", name = "ui-views",
Simon Hunt23f9c7b2017-07-10 20:00:30 -070034 description = "Lists all registered UI views")
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070035public class UiViewListCommand extends AbstractShellCommand {
36
37 private static final String FMT = "id=%s, category=%s, label=%s, icon=%s";
38
39 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040 protected void doExecute() {
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070041 UiExtensionService service = get(UiExtensionService.class);
42 if (outputJson()) {
43 print("%s", json(service.getExtensions()));
44 } else {
45 service.getExtensions().forEach(ext -> ext.views()
Simon Hunt23f9c7b2017-07-10 20:00:30 -070046 .forEach(v -> print(FMT, v.id(), v.category(),
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070047 v.label(), v.iconId())));
48 }
49 }
50
51 private JsonNode json(List<UiExtension> extensions) {
52 ObjectMapper mapper = new ObjectMapper();
53 ArrayNode node = mapper.createArrayNode();
54 extensions.forEach(ext -> ext.views()
55 .forEach(v -> node.add(mapper.createObjectNode()
56 .put("id", v.id())
Simon Hunt23f9c7b2017-07-10 20:00:30 -070057 .put("category", v.category().toString())
Thomas Vachuska4cfcc562015-06-03 09:51:02 -070058 .put("label", v.label())
59 .put("icon", v.iconId()))));
60 return node;
61 }
62
63}