Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 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; |
| 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 7 | import org.apache.karaf.shell.commands.Command; |
| 8 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 9 | import org.onlab.onos.cli.Comparators; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.Host; |
| 11 | import org.onlab.onos.net.host.HostService; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 12 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 13 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 14 | import java.util.Collections; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import static com.google.common.collect.Lists.newArrayList; |
| 18 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Lists all currently-known hosts. |
| 21 | */ |
| 22 | @Command(scope = "onos", name = "hosts", |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 23 | description = "Lists all currently-known hosts.") |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 24 | public class HostsListCommand extends AbstractShellCommand { |
| 25 | |
| 26 | private static final String FMT = |
| 27 | "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s"; |
| 28 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 29 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 30 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 31 | HostService service = get(HostService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 32 | if (outputJson()) { |
| 33 | print("%s", json(getSortedHosts(service))); |
| 34 | } else { |
| 35 | for (Host host : getSortedHosts(service)) { |
| 36 | printHost(host); |
| 37 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 38 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 39 | } |
| 40 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 41 | // Produces JSON structure. |
| 42 | private static JsonNode json(Iterable<Host> hosts) { |
| 43 | ObjectMapper mapper = new ObjectMapper(); |
| 44 | ArrayNode result = mapper.createArrayNode(); |
| 45 | for (Host host : hosts) { |
| 46 | result.add(json(mapper, host)); |
| 47 | } |
| 48 | return result; |
| 49 | } |
| 50 | |
| 51 | // Produces JSON structure. |
| 52 | private static JsonNode json(ObjectMapper mapper, Host host) { |
| 53 | ObjectNode loc = LinksListCommand.json(mapper, host.location()) |
| 54 | .put("time", host.location().time()); |
| 55 | ArrayNode ips = mapper.createArrayNode(); |
| 56 | for (IpPrefix ip : host.ipAddresses()) { |
| 57 | ips.add(ip.toString()); |
| 58 | } |
| 59 | ObjectNode result = mapper.createObjectNode() |
| 60 | .put("id", host.id().toString()) |
| 61 | .put("mac", host.mac().toString()) |
| 62 | .put("vlan", host.vlan().toString()); |
| 63 | result.set("location", loc); |
| 64 | result.set("ips", ips); |
| 65 | return result; |
| 66 | } |
| 67 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Returns the list of devices sorted using the device ID URIs. |
| 70 | * |
| 71 | * @param service device service |
| 72 | * @return sorted device list |
| 73 | */ |
| 74 | protected List<Host> getSortedHosts(HostService service) { |
| 75 | List<Host> hosts = newArrayList(service.getHosts()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 76 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 77 | return hosts; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Prints information about a host. |
| 82 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 83 | * @param host end-station host |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 84 | */ |
| 85 | protected void printHost(Host host) { |
| 86 | if (host != null) { |
| 87 | print(FMT, host.id(), host.mac(), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 88 | host.location().deviceId(), |
| 89 | host.location().port(), |
| 90 | host.vlan(), host.ipAddresses()); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 93 | } |