blob: fff495550769e041563be80bd69b9e4c7c714ab7 [file] [log] [blame]
tom1380eee2014-09-24 09:22:02 -07001package org.onlab.onos.cli;
2
tom32085cf2014-10-16 00:04:33 -07003import com.fasterxml.jackson.databind.JsonNode;
4import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
tom1380eee2014-09-24 09:22:02 -07006import com.google.common.collect.Lists;
7import org.apache.karaf.shell.commands.Command;
tom1380eee2014-09-24 09:22:02 -07008import org.onlab.onos.cluster.ClusterService;
9import org.onlab.onos.cluster.ControllerNode;
Yuta HIGUCHI80912e62014-10-12 00:15:47 -070010import org.onlab.onos.mastership.MastershipService;
tom1380eee2014-09-24 09:22:02 -070011import org.onlab.onos.net.DeviceId;
12
13import java.util.Collections;
14import java.util.List;
15
16import static com.google.common.collect.Lists.newArrayList;
17
18/**
19 * Lists device mastership information.
20 */
21@Command(scope = "onos", name = "masters",
22 description = "Lists device mastership information")
23public class MastersListCommand extends AbstractShellCommand {
24
25 @Override
26 protected void execute() {
27 ClusterService service = get(ClusterService.class);
28 MastershipService mastershipService = get(MastershipService.class);
29 List<ControllerNode> nodes = newArrayList(service.getNodes());
30 Collections.sort(nodes, Comparators.NODE_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070031
32 if (outputJson()) {
33 print("%s", json(service, mastershipService, nodes));
34 } else {
35 for (ControllerNode node : nodes) {
36 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
37 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
38 print("%s: %d devices", node.id(), ids.size());
39 for (DeviceId deviceId : ids) {
40 print(" %s", deviceId);
41 }
42 }
43 }
44 }
45
46 // Produces JSON structure.
47 private JsonNode json(ClusterService service, MastershipService mastershipService,
48 List<ControllerNode> nodes) {
49 ObjectMapper mapper = new ObjectMapper();
50 ArrayNode result = mapper.createArrayNode();
tom1380eee2014-09-24 09:22:02 -070051 ControllerNode self = service.getLocalNode();
52 for (ControllerNode node : nodes) {
53 List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id()));
tom32085cf2014-10-16 00:04:33 -070054 result.add(mapper.createObjectNode()
55 .put("id", node.id().toString())
56 .put("size", ids.size())
57 .set("devices", json(mapper, ids)));
tom1380eee2014-09-24 09:22:02 -070058 }
tom32085cf2014-10-16 00:04:33 -070059 return result;
60 }
61
62 /**
63 * Produces a JSON array containing the specified device identifiers.
64 *
65 * @param mapper object mapper
66 * @param ids collection of device identifiers
67 * @return JSON array
68 */
69 public static JsonNode json(ObjectMapper mapper, Iterable<DeviceId> ids) {
70 ArrayNode result = mapper.createArrayNode();
71 for (DeviceId deviceId : ids) {
72 result.add(deviceId.toString());
73 }
74 return result;
tom1380eee2014-09-24 09:22:02 -070075 }
76
77}