Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli; |
| 2 | |
| 3 | import static com.google.common.collect.Lists.newArrayList; |
| 4 | |
| 5 | import java.util.Collections; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import org.apache.karaf.shell.commands.Command; |
| 9 | import org.onlab.onos.cluster.NodeId; |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 10 | import org.onlab.onos.cluster.RoleInfo; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 11 | import org.onlab.onos.mastership.MastershipService; |
| 12 | import org.onlab.onos.net.Device; |
| 13 | import org.onlab.onos.net.DeviceId; |
| 14 | import org.onlab.onos.net.device.DeviceService; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * Lists mastership roles of nodes for each device. |
| 19 | */ |
| 20 | @Command(scope = "onos", name = "roles", |
| 21 | description = "Lists mastership roles of nodes for each device.") |
| 22 | public class RolesCommand extends AbstractShellCommand { |
| 23 | |
Ayaka Koshibe | fc981cf | 2014-10-21 12:44:17 -0700 | [diff] [blame] | 24 | private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]"; |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 25 | |
| 26 | @Override |
| 27 | protected void execute() { |
| 28 | DeviceService deviceService = get(DeviceService.class); |
| 29 | MastershipService roleService = get(MastershipService.class); |
| 30 | |
| 31 | for (Device d : getSortedDevices(deviceService)) { |
| 32 | DeviceId did = d.id(); |
| 33 | printRoles(roleService, did); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Returns the list of devices sorted using the device ID URIs. |
| 39 | * |
| 40 | * @param service device service |
| 41 | * @return sorted device list |
| 42 | */ |
| 43 | protected static List<Device> getSortedDevices(DeviceService service) { |
| 44 | List<Device> devices = newArrayList(service.getDevices()); |
| 45 | Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); |
| 46 | return devices; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Prints the role information for a device. |
| 51 | * |
| 52 | * @param deviceId the ID of the device |
| 53 | * @param master the current master |
| 54 | */ |
| 55 | protected void printRoles(MastershipService service, DeviceId deviceId) { |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 56 | RoleInfo nodes = service.getNodesFor(deviceId); |
| 57 | StringBuilder builder = new StringBuilder(); |
| 58 | for (NodeId nid : nodes.backups()) { |
| 59 | builder.append(nid).append(" "); |
| 60 | } |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 61 | |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 62 | print(FMT_HDR, deviceId, |
| 63 | nodes.master() == null ? "NONE" : nodes.master(), |
| 64 | builder.toString()); |
Ayaka Koshibe | e8e4535 | 2014-10-16 00:37:19 -0700 | [diff] [blame] | 65 | } |
| 66 | } |