blob: 55f59d5b7c4f1d39270456413bf41c55831f2b5d [file] [log] [blame]
Ayaka Koshibee8e45352014-10-16 00:37:19 -07001package org.onlab.onos.cli;
2
3import static com.google.common.collect.Lists.newArrayList;
4
5import java.util.Collections;
6import java.util.List;
7
8import org.apache.karaf.shell.commands.Command;
9import org.onlab.onos.cluster.NodeId;
Ayaka Koshibeabedb092014-10-20 17:01:31 -070010import org.onlab.onos.cluster.RoleInfo;
Ayaka Koshibee8e45352014-10-16 00:37:19 -070011import org.onlab.onos.mastership.MastershipService;
12import org.onlab.onos.net.Device;
13import org.onlab.onos.net.DeviceId;
14import 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.")
22public class RolesCommand extends AbstractShellCommand {
23
Ayaka Koshibefc981cf2014-10-21 12:44:17 -070024 private static final String FMT_HDR = "%s: master=%s, standbys=[ %s]";
Ayaka Koshibee8e45352014-10-16 00:37:19 -070025
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 Koshibeabedb092014-10-20 17:01:31 -070056 RoleInfo nodes = service.getNodesFor(deviceId);
57 StringBuilder builder = new StringBuilder();
58 for (NodeId nid : nodes.backups()) {
59 builder.append(nid).append(" ");
60 }
Ayaka Koshibee8e45352014-10-16 00:37:19 -070061
Ayaka Koshibeabedb092014-10-20 17:01:31 -070062 print(FMT_HDR, deviceId,
63 nodes.master() == null ? "NONE" : nodes.master(),
64 builder.toString());
Ayaka Koshibee8e45352014-10-16 00:37:19 -070065 }
66}