Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -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 | */ |
| 16 | |
| 17 | package org.onosproject.cli.net; |
| 18 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.ui.UiTopoLayoutService; |
| 24 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 25 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
| 26 | import org.onosproject.utils.Comparators; |
| 27 | |
| 28 | import java.util.Collections; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | |
| 33 | /** |
| 34 | * List layout details. |
| 35 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Service |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 37 | @Command(scope = "onos", name = "layouts", |
| 38 | description = "List layout details") |
| 39 | public class LayoutListCommand extends AbstractShellCommand { |
| 40 | |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 41 | private static final String FMT = "id=%s, bgref=%s, region=%s, parent=%s"; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 42 | |
| 43 | @Argument(index = 0, name = "id", description = "Layout ID", |
| 44 | required = false, multiValued = false) |
| 45 | String id = null; |
| 46 | |
| 47 | private UiTopoLayoutService layoutService; |
| 48 | |
| 49 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 51 | layoutService = get(UiTopoLayoutService.class); |
| 52 | if (id == null) { |
| 53 | for (UiTopoLayout layout : getSortedLayouts(layoutService)) { |
| 54 | printLayout(layout); |
| 55 | } |
| 56 | } else { |
| 57 | UiTopoLayout layout = layoutService.getLayout(UiTopoLayoutId.layoutId(id)); |
| 58 | if (layout == null) { |
| 59 | error("No such region %s", id); |
| 60 | } else { |
| 61 | printLayout(layout); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private List<UiTopoLayout> getSortedLayouts(UiTopoLayoutService service) { |
| 67 | List<UiTopoLayout> layouts = newArrayList(service.getLayouts()); |
| 68 | Collections.sort(layouts, Comparators.LAYOUT_COMPARATOR); |
| 69 | return layouts; |
| 70 | } |
| 71 | |
| 72 | private void printLayout(UiTopoLayout layout) { |
Simon Hunt | bc30e68 | 2017-02-15 18:39:23 -0800 | [diff] [blame] | 73 | String map = layout.geomap(); |
| 74 | String spr = layout.sprites(); |
| 75 | String bgRef = "."; |
| 76 | if (map != null) { |
| 77 | bgRef = "@" + map; |
| 78 | } else if (spr != null) { |
| 79 | bgRef = "+" + spr; |
| 80 | } |
| 81 | |
| 82 | String pid = layout.parent() != null ? layout.parent().id() : "(none)"; |
| 83 | |
| 84 | print(FMT, layout.id(), bgRef, layout.regionId(), pid); |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 85 | } |
| 86 | } |