blob: 019bb07a180f4ecb347877d5fa0ab5aeaf927aee [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
45 @Override
46 protected void execute() {
47 RegionService regionService = get(RegionService.class);
48
49 if (id == null) {
50 for (Region region : getSortedRegions(regionService)) {
51 printRegion(region);
52 }
53 } else {
54 Region region = regionService.getRegion(RegionId.regionId(id));
55
56 if (region == null) {
57 error("No such region %s", id);
58 } else {
59 printRegion(region);
60 }
61 }
62 }
63
64 /**
65 * Returns the list of regions sorted using the region identifier.
66 *
67 * @param service region service
68 * @return sorted region list
69 */
70 protected List<Region> getSortedRegions(RegionService service) {
71 List<Region> regions = newArrayList(service.getRegions());
72 Collections.sort(regions, Comparators.REGION_COMPARATOR);
73 return regions;
74 }
75
76 private void printRegion(Region region) {
77 print(FMT, region.id(), region.name(), region.type());
78 region.masters().forEach(m -> print(FMT_MASTER, m));
79 }
80}