blob: 47aa688cd102a2e814e420d747d6b8aa12b5069b [file] [log] [blame]
Ayaka Koshibe43530be2014-09-15 11:14:52 -07001package org.onlab.onos.cli.net;
2
3import static com.google.common.collect.Lists.newArrayList;
4
5import java.util.Collections;
6import java.util.Comparator;
7import java.util.List;
8
9import org.apache.karaf.shell.commands.Command;
10import org.onlab.onos.cli.AbstractShellCommand;
11import org.onlab.onos.net.Host;
12import org.onlab.onos.net.host.HostService;
13
14/**
15 * Lists all currently-known hosts.
16 */
17@Command(scope = "onos", name = "hosts",
18 description = "Lists all currently-known hosts.")
19public class HostsListCommand extends AbstractShellCommand {
20
21 private static final String FMT =
22 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
23
24 protected static final Comparator<Host> ID_COMPARATOR = new Comparator<Host>() {
25 @Override
26 public int compare(Host h1, Host h2) {
27 return h1.id().uri().toString().compareTo(h2.id().uri().toString());
28 }
29 };
30
31 @Override
32 protected Object doExecute() throws Exception {
33 HostService service = getService(HostService.class);
34 for (Host host : getSortedHosts(service)) {
35 printHost(host);
36 }
37 return null;
38 }
39
40 /**
41 * Returns the list of devices sorted using the device ID URIs.
42 *
43 * @param service device service
44 * @return sorted device list
45 */
46 protected List<Host> getSortedHosts(HostService service) {
47 List<Host> hosts = newArrayList(service.getHosts());
48 Collections.sort(hosts, ID_COMPARATOR);
49 return hosts;
50 }
51
52 /**
53 * Prints information about a host.
54 *
55 * @param host
56 */
57 protected void printHost(Host host) {
58 if (host != null) {
59 print(FMT, host.id(), host.mac(),
60 host.location().deviceId(),
61 host.location().port(),
62 host.vlan(), host.ipAddresses());
63 }
64 }
65 }