Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 23 | import org.onosproject.utils.Comparators; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 24 | import org.onosproject.net.region.Region; |
| 25 | import org.onosproject.net.region.RegionId; |
| 26 | import org.onosproject.net.region.RegionService; |
| 27 | |
| 28 | import java.util.Collections; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | |
| 33 | /** |
| 34 | * List Region details including membership. |
| 35 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Service |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 37 | @Command(scope = "onos", name = "regions", |
| 38 | description = "List Region details including membership") |
| 39 | public 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 Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 46 | @Completion(RegionIdCompleter.class) |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 47 | String id = null; |
| 48 | |
Thomas Vachuska | 3ce80f7 | 2016-07-21 14:52:15 -0700 | [diff] [blame] | 49 | private RegionService regionService; |
| 50 | |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 51 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | protected void doExecute() { |
Thomas Vachuska | 3ce80f7 | 2016-07-21 14:52:15 -0700 | [diff] [blame] | 53 | regionService = get(RegionService.class); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 54 | 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 Vachuska | 3ce80f7 | 2016-07-21 14:52:15 -0700 | [diff] [blame] | 84 | regionService.getRegionDevices(region.id()).forEach(id -> print(" %s", id)); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 85 | } |
| 86 | } |