Thomas Vachuska

Added JSON for role command.

...@@ -18,11 +18,10 @@ ...@@ -18,11 +18,10 @@
18 */ 18 */
19 package org.onlab.onos.cli; 19 package org.onlab.onos.cli;
20 20
21 -import static com.google.common.collect.Lists.newArrayList; 21 +import com.fasterxml.jackson.databind.JsonNode;
22 - 22 +import com.fasterxml.jackson.databind.ObjectMapper;
23 -import java.util.Collections; 23 +import com.fasterxml.jackson.databind.node.ArrayNode;
24 -import java.util.List; 24 +import com.fasterxml.jackson.databind.node.ObjectNode;
25 -
26 import org.apache.karaf.shell.commands.Command; 25 import org.apache.karaf.shell.commands.Command;
27 import org.onlab.onos.cluster.NodeId; 26 import org.onlab.onos.cluster.NodeId;
28 import org.onlab.onos.cluster.RoleInfo; 27 import org.onlab.onos.cluster.RoleInfo;
...@@ -31,6 +30,9 @@ import org.onlab.onos.net.Device; ...@@ -31,6 +30,9 @@ import org.onlab.onos.net.Device;
31 import org.onlab.onos.net.DeviceId; 30 import org.onlab.onos.net.DeviceId;
32 import org.onlab.onos.net.device.DeviceService; 31 import org.onlab.onos.net.device.DeviceService;
33 32
33 +import java.util.List;
34 +
35 +import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
34 36
35 /** 37 /**
36 * Lists mastership roles of nodes for each device. 38 * Lists mastership roles of nodes for each device.
...@@ -46,29 +48,47 @@ public class RolesCommand extends AbstractShellCommand { ...@@ -46,29 +48,47 @@ public class RolesCommand extends AbstractShellCommand {
46 DeviceService deviceService = get(DeviceService.class); 48 DeviceService deviceService = get(DeviceService.class);
47 MastershipService roleService = get(MastershipService.class); 49 MastershipService roleService = get(MastershipService.class);
48 50
51 + if (outputJson()) {
52 + print("%s", json(roleService, getSortedDevices(deviceService)));
53 + } else {
49 for (Device d : getSortedDevices(deviceService)) { 54 for (Device d : getSortedDevices(deviceService)) {
50 DeviceId did = d.id(); 55 DeviceId did = d.id();
51 printRoles(roleService, did); 56 printRoles(roleService, did);
52 } 57 }
53 } 58 }
59 + }
54 60
55 - /** 61 + // Produces JSON structure with role information for the given devices.
56 - * Returns the list of devices sorted using the device ID URIs. 62 + private JsonNode json(MastershipService service, List<Device> sortedDevices) {
57 - * 63 + ObjectMapper mapper = new ObjectMapper();
58 - * @param service device service 64 + ArrayNode results = mapper.createArrayNode();
59 - * @return sorted device list 65 + for (Device device : sortedDevices) {
60 - */ 66 + results.add(json(service, mapper, device));
61 - protected static List<Device> getSortedDevices(DeviceService service) { 67 + }
62 - List<Device> devices = newArrayList(service.getDevices()); 68 + return results;
63 - Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); 69 + }
64 - return devices; 70 +
71 + // Produces JSON structure with role information for the given device.
72 + private JsonNode json(MastershipService service, ObjectMapper mapper,
73 + Device device) {
74 + NodeId master = service.getMasterFor(device.id());
75 + ObjectNode result = mapper.createObjectNode()
76 + .put("id", device.id().toString())
77 + .put("master", master != null ? master.toString() : "none");
78 + RoleInfo nodes = service.getNodesFor(device.id());
79 + ArrayNode standbys = mapper.createArrayNode();
80 + for (NodeId nid : nodes.backups()) {
81 + standbys.add(nid.toString());
82 + }
83 + result.set("standbys", standbys);
84 + return result;
65 } 85 }
66 86
67 /** 87 /**
68 * Prints the role information for a device. 88 * Prints the role information for a device.
69 * 89 *
90 + * @param service mastership service
70 * @param deviceId the ID of the device 91 * @param deviceId the ID of the device
71 - * @param master the current master
72 */ 92 */
73 protected void printRoles(MastershipService service, DeviceId deviceId) { 93 protected void printRoles(MastershipService service, DeviceId deviceId) {
74 RoleInfo nodes = service.getNodesFor(deviceId); 94 RoleInfo nodes = service.getNodesFor(deviceId);
......
...@@ -101,7 +101,7 @@ public class DevicesListCommand extends AbstractShellCommand { ...@@ -101,7 +101,7 @@ public class DevicesListCommand extends AbstractShellCommand {
101 * @param service device service 101 * @param service device service
102 * @return sorted device list 102 * @return sorted device list
103 */ 103 */
104 - protected static List<Device> getSortedDevices(DeviceService service) { 104 + public static List<Device> getSortedDevices(DeviceService service) {
105 List<Device> devices = newArrayList(service.getDevices()); 105 List<Device> devices = newArrayList(service.getDevices());
106 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR); 106 Collections.sort(devices, Comparators.ELEMENT_COMPARATOR);
107 return devices; 107 return devices;
......
...@@ -701,7 +701,7 @@ public class GossipDeviceStore ...@@ -701,7 +701,7 @@ public class GossipDeviceStore
701 public synchronized DeviceEvent removeDevice(DeviceId deviceId) { 701 public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
702 final NodeId master = mastershipService.getMasterFor(deviceId); 702 final NodeId master = mastershipService.getMasterFor(deviceId);
703 if (!clusterService.getLocalNode().id().equals(master)) { 703 if (!clusterService.getLocalNode().id().equals(master)) {
704 - log.info("remove Device {} requested on non master node", deviceId); 704 + log.info("Removal of device {} requested on non master node", deviceId);
705 // FIXME silently ignoring. Should be forwarding or broadcasting to 705 // FIXME silently ignoring. Should be forwarding or broadcasting to
706 // master. 706 // master.
707 return null; 707 return null;
...@@ -778,7 +778,7 @@ public class GossipDeviceStore ...@@ -778,7 +778,7 @@ public class GossipDeviceStore
778 private Device composeDevice(DeviceId deviceId, 778 private Device composeDevice(DeviceId deviceId,
779 Map<ProviderId, DeviceDescriptions> providerDescs) { 779 Map<ProviderId, DeviceDescriptions> providerDescs) {
780 780
781 - checkArgument(!providerDescs.isEmpty(), "No Device descriptions supplied"); 781 + checkArgument(!providerDescs.isEmpty(), "No device descriptions supplied");
782 782
783 ProviderId primary = pickPrimaryPID(providerDescs); 783 ProviderId primary = pickPrimaryPID(providerDescs);
784 784
......