blob: f43114200f6ef0b3fc0457e0e6d05d56b1c4fc87 [file] [log] [blame]
Ayaka Koshibe43530be2014-09-15 11:14:52 -07001package org.onlab.onos.cli.net;
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;
6import com.fasterxml.jackson.databind.node.ObjectNode;
Ayaka Koshibe43530be2014-09-15 11:14:52 -07007import org.apache.karaf.shell.commands.Command;
8import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -07009import org.onlab.onos.cli.Comparators;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070010import org.onlab.onos.net.Host;
11import org.onlab.onos.net.host.HostService;
tom32085cf2014-10-16 00:04:33 -070012import org.onlab.packet.IpPrefix;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070013
tom1380eee2014-09-24 09:22:02 -070014import java.util.Collections;
15import java.util.List;
16
17import static com.google.common.collect.Lists.newArrayList;
18
Ayaka Koshibe43530be2014-09-15 11:14:52 -070019/**
20 * Lists all currently-known hosts.
21 */
22@Command(scope = "onos", name = "hosts",
tom32085cf2014-10-16 00:04:33 -070023 description = "Lists all currently-known hosts.")
Ayaka Koshibe43530be2014-09-15 11:14:52 -070024public 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 Koshibe43530be2014-09-15 11:14:52 -070029 @Override
tom0872a172014-09-23 11:24:26 -070030 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070031 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070032 if (outputJson()) {
33 print("%s", json(getSortedHosts(service)));
34 } else {
35 for (Host host : getSortedHosts(service)) {
36 printHost(host);
37 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070038 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070039 }
40
tom32085cf2014-10-16 00:04:33 -070041 // 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 Koshibe43530be2014-09-15 11:14:52 -070068 /**
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());
tom1380eee2014-09-24 09:22:02 -070076 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070077 return hosts;
78 }
79
80 /**
81 * Prints information about a host.
82 *
tom32085cf2014-10-16 00:04:33 -070083 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -070084 */
85 protected void printHost(Host host) {
86 if (host != null) {
87 print(FMT, host.id(), host.mac(),
tom32085cf2014-10-16 00:04:33 -070088 host.location().deviceId(),
89 host.location().port(),
90 host.vlan(), host.ipAddresses());
Ayaka Koshibe43530be2014-09-15 11:14:52 -070091 }
92 }
tom32085cf2014-10-16 00:04:33 -070093}