tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli; |
| 2 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 6 | import com.google.common.collect.Lists; |
| 7 | import org.apache.karaf.shell.commands.Command; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 8 | import org.onlab.onos.cluster.ClusterService; |
| 9 | import org.onlab.onos.cluster.ControllerNode; |
Yuta HIGUCHI | 80912e6 | 2014-10-12 00:15:47 -0700 | [diff] [blame] | 10 | import org.onlab.onos.mastership.MastershipService; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.DeviceId; |
| 12 | |
| 13 | import java.util.Collections; |
| 14 | import java.util.List; |
| 15 | |
| 16 | import 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") |
| 23 | public 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); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 31 | |
| 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(); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 51 | ControllerNode self = service.getLocalNode(); |
| 52 | for (ControllerNode node : nodes) { |
| 53 | List<DeviceId> ids = Lists.newArrayList(mastershipService.getDevicesOf(node.id())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 54 | result.add(mapper.createObjectNode() |
| 55 | .put("id", node.id().toString()) |
| 56 | .put("size", ids.size()) |
| 57 | .set("devices", json(mapper, ids))); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 58 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | 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; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } |