Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 3 | * |
| 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 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 21 | import org.onosproject.utils.Comparators; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 22 | import org.onosproject.net.region.Region; |
| 23 | import org.onosproject.net.region.RegionId; |
| 24 | import org.onosproject.net.region.RegionService; |
| 25 | |
| 26 | import java.util.Collections; |
| 27 | import java.util.List; |
| 28 | |
| 29 | import 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") |
| 36 | public 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 Vachuska | 3ce80f7 | 2016-07-21 14:52:15 -0700 | [diff] [blame] | 45 | private RegionService regionService; |
| 46 | |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 47 | @Override |
| 48 | protected void execute() { |
Thomas Vachuska | 3ce80f7 | 2016-07-21 14:52:15 -0700 | [diff] [blame] | 49 | regionService = get(RegionService.class); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 50 | 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 Vachuska | 3ce80f7 | 2016-07-21 14:52:15 -0700 | [diff] [blame] | 80 | regionService.getRegionDevices(region.id()).forEach(id -> print(" %s", id)); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 81 | } |
| 82 | } |