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