blob: 9c3910c2459807c2f253f1398b08299c2bd6af3e [file] [log] [blame]
Jian Licf744442016-02-25 17:32:42 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Licf744442016-02-25 17:32:42 +09003 *
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080021import org.onosproject.utils.Comparators;
Jian Licf744442016-02-25 17:32:42 +090022import org.onosproject.net.region.Region;
23import org.onosproject.net.region.RegionId;
24import org.onosproject.net.region.RegionService;
25
26import java.util.Collections;
27import java.util.List;
28
29import static com.google.common.collect.Lists.newArrayList;
30
31/**
32 * List Region details including membership.
33 */
34@Command(scope = "onos", name = "regions",
35 description = "List Region details including membership")
36public class RegionListCommand extends AbstractShellCommand {
37
38 private static final String FMT = "id=%s, name=%s, type=%s";
39 private static final String FMT_MASTER = " master=%s";
40
41 @Argument(index = 0, name = "id", description = "Region ID",
42 required = false, multiValued = false)
43 String id = null;
44
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070045 private RegionService regionService;
46
Jian Licf744442016-02-25 17:32:42 +090047 @Override
48 protected void execute() {
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070049 regionService = get(RegionService.class);
Jian Licf744442016-02-25 17:32:42 +090050 if (id == null) {
51 for (Region region : getSortedRegions(regionService)) {
52 printRegion(region);
53 }
54 } else {
55 Region region = regionService.getRegion(RegionId.regionId(id));
56
57 if (region == null) {
58 error("No such region %s", id);
59 } else {
60 printRegion(region);
61 }
62 }
63 }
64
65 /**
66 * Returns the list of regions sorted using the region identifier.
67 *
68 * @param service region service
69 * @return sorted region list
70 */
71 protected List<Region> getSortedRegions(RegionService service) {
72 List<Region> regions = newArrayList(service.getRegions());
73 Collections.sort(regions, Comparators.REGION_COMPARATOR);
74 return regions;
75 }
76
77 private void printRegion(Region region) {
78 print(FMT, region.id(), region.name(), region.type());
79 region.masters().forEach(m -> print(FMT_MASTER, m));
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070080 regionService.getRegionDevices(region.id()).forEach(id -> print(" %s", id));
Jian Licf744442016-02-25 17:32:42 +090081 }
82}