blob: c45470cc262f1f446564c307cf7c962d052add7e [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
tom0872a172014-09-23 11:24:26 -070032 protected void execute() {
Ayaka Koshibe43530be2014-09-15 11:14:52 -070033 HostService service = getService(HostService.class);
34 for (Host host : getSortedHosts(service)) {
35 printHost(host);
36 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070037 }
38
39 /**
40 * Returns the list of devices sorted using the device ID URIs.
41 *
42 * @param service device service
43 * @return sorted device list
44 */
45 protected List<Host> getSortedHosts(HostService service) {
46 List<Host> hosts = newArrayList(service.getHosts());
47 Collections.sort(hosts, ID_COMPARATOR);
48 return hosts;
49 }
50
51 /**
52 * Prints information about a host.
53 *
54 * @param host
55 */
56 protected void printHost(Host host) {
57 if (host != null) {
58 print(FMT, host.id(), host.mac(),
59 host.location().deviceId(),
60 host.location().port(),
61 host.vlan(), host.ipAddresses());
62 }
63 }
64 }