blob: ab0065b4f59478869cdc8bfe117c453f6b84b9b1 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Licf744442016-02-25 17:32:42 +090022import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080023import org.onosproject.utils.Comparators;
Jian Licf744442016-02-25 17:32:42 +090024import org.onosproject.net.region.Region;
25import org.onosproject.net.region.RegionId;
26import org.onosproject.net.region.RegionService;
27
28import java.util.Collections;
29import java.util.List;
30
31import static com.google.common.collect.Lists.newArrayList;
32
33/**
34 * List Region details including membership.
35 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
Jian Licf744442016-02-25 17:32:42 +090037@Command(scope = "onos", name = "regions",
38 description = "List Region details including membership")
39public class RegionListCommand extends AbstractShellCommand {
40
41 private static final String FMT = "id=%s, name=%s, type=%s";
42 private static final String FMT_MASTER = " master=%s";
43
44 @Argument(index = 0, name = "id", description = "Region ID",
45 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070046 @Completion(RegionIdCompleter.class)
Jian Licf744442016-02-25 17:32:42 +090047 String id = null;
48
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070049 private RegionService regionService;
50
Jian Licf744442016-02-25 17:32:42 +090051 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 protected void doExecute() {
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070053 regionService = get(RegionService.class);
Jian Licf744442016-02-25 17:32:42 +090054 if (id == null) {
55 for (Region region : getSortedRegions(regionService)) {
56 printRegion(region);
57 }
58 } else {
59 Region region = regionService.getRegion(RegionId.regionId(id));
60
61 if (region == null) {
62 error("No such region %s", id);
63 } else {
64 printRegion(region);
65 }
66 }
67 }
68
69 /**
70 * Returns the list of regions sorted using the region identifier.
71 *
72 * @param service region service
73 * @return sorted region list
74 */
75 protected List<Region> getSortedRegions(RegionService service) {
76 List<Region> regions = newArrayList(service.getRegions());
77 Collections.sort(regions, Comparators.REGION_COMPARATOR);
78 return regions;
79 }
80
81 private void printRegion(Region region) {
82 print(FMT, region.id(), region.name(), region.type());
83 region.masters().forEach(m -> print(FMT_MASTER, m));
Thomas Vachuska3ce80f72016-07-21 14:52:15 -070084 regionService.getRegionDevices(region.id()).forEach(id -> print(" %s", id));
Jian Licf744442016-02-25 17:32:42 +090085 }
86}