blob: e959d3e18bc516ea3077d7ea643cdfa70ea8d2fd [file] [log] [blame]
Jian Licf744442016-02-25 17:32:42 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Licf744442016-02-25 17:32:42 +090021import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080022import org.onosproject.utils.Comparators;
Jian Licf744442016-02-25 17:32:42 +090023import org.onosproject.net.region.Region;
24import org.onosproject.net.region.RegionId;
25import org.onosproject.net.region.RegionService;
26
27import java.util.Collections;
28import java.util.List;
29
30import static com.google.common.collect.Lists.newArrayList;
31
32/**
33 * List Region details including membership.
34 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035@Service
Jian Licf744442016-02-25 17:32:42 +090036@Command(scope = "onos", name = "regions",
37 description = "List Region details including membership")
38public class RegionListCommand extends AbstractShellCommand {
39
40 private static final String FMT = "id=%s, name=%s, type=%s";
41 private static final String FMT_MASTER = " master=%s";
42
43 @Argument(index = 0, name = "id", description = "Region ID",
44 required = false, multiValued = false)
45 String id = null;
46
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070047 private RegionService regionService;
48
Jian Licf744442016-02-25 17:32:42 +090049 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 protected void doExecute() {
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070051 regionService = get(RegionService.class);
Jian Licf744442016-02-25 17:32:42 +090052 if (id == null) {
53 for (Region region : getSortedRegions(regionService)) {
54 printRegion(region);
55 }
56 } else {
57 Region region = regionService.getRegion(RegionId.regionId(id));
58
59 if (region == null) {
60 error("No such region %s", id);
61 } else {
62 printRegion(region);
63 }
64 }
65 }
66
67 /**
68 * Returns the list of regions sorted using the region identifier.
69 *
70 * @param service region service
71 * @return sorted region list
72 */
73 protected List<Region> getSortedRegions(RegionService service) {
74 List<Region> regions = newArrayList(service.getRegions());
75 Collections.sort(regions, Comparators.REGION_COMPARATOR);
76 return regions;
77 }
78
79 private void printRegion(Region region) {
80 print(FMT, region.id(), region.name(), region.type());
81 region.masters().forEach(m -> print(FMT_MASTER, m));
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070082 regionService.getRegionDevices(region.id()).forEach(id -> print(" %s", id));
Jian Licf744442016-02-25 17:32:42 +090083 }
84}