blob: 8ba13f2c1fccd915a6288a523c0df94dfef7abc1 [file] [log] [blame]
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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
39 private static final String FMT = "id=%s, region=%s, parent=%s";
40
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) {
71 print(FMT, layout.id(), layout.regionId(),
72 layout.parent() != null ? layout.parent().id() : "none");
73 }
74}