Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 3 | * |
| 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 Vachuska | 99b7bbe | 2018-02-01 15:29:46 -0800 | [diff] [blame] | 16 | package org.onosproject.ui.impl.cli; |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Thomas Vachuska | 99b7bbe | 2018-02-01 15:29:46 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.UiExtension; |
| 25 | import org.onosproject.ui.UiExtensionService; |
| 26 | |
| 27 | import java.util.List; |
| 28 | |
| 29 | /** |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 30 | * Lists all registered UI views. |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 32 | @Service |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "ui-views", |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 34 | description = "Lists all registered UI views") |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 35 | public class UiViewListCommand extends AbstractShellCommand { |
| 36 | |
| 37 | private static final String FMT = "id=%s, category=%s, label=%s, icon=%s"; |
| 38 | |
| 39 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | protected void doExecute() { |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 41 | UiExtensionService service = get(UiExtensionService.class); |
| 42 | if (outputJson()) { |
| 43 | print("%s", json(service.getExtensions())); |
| 44 | } else { |
| 45 | service.getExtensions().forEach(ext -> ext.views() |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 46 | .forEach(v -> print(FMT, v.id(), v.category(), |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 47 | 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 Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 57 | .put("category", v.category().toString()) |
Thomas Vachuska | 4cfcc56 | 2015-06-03 09:51:02 -0700 | [diff] [blame] | 58 | .put("label", v.label()) |
| 59 | .put("icon", v.iconId())))); |
| 60 | return node; |
| 61 | } |
| 62 | |
| 63 | } |