blob: 4e1f16b92267190557e7eb60888b2a17237c86ef [file] [log] [blame]
Ayaka Koshibe43530be2014-09-15 11:14:52 -07001package org.onlab.onos.cli.net;
2
Ayaka Koshibe43530be2014-09-15 11:14:52 -07003import org.apache.karaf.shell.commands.Command;
4import org.onlab.onos.cli.AbstractShellCommand;
5import org.onlab.onos.net.Host;
6import org.onlab.onos.net.host.HostService;
7
tom1380eee2014-09-24 09:22:02 -07008import java.util.Collections;
9import java.util.List;
10
11import static com.google.common.collect.Lists.newArrayList;
12
Ayaka Koshibe43530be2014-09-15 11:14:52 -070013/**
14 * Lists all currently-known hosts.
15 */
16@Command(scope = "onos", name = "hosts",
17 description = "Lists all currently-known hosts.")
18public class HostsListCommand extends AbstractShellCommand {
19
20 private static final String FMT =
21 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
22
Ayaka Koshibe43530be2014-09-15 11:14:52 -070023 @Override
tom0872a172014-09-23 11:24:26 -070024 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070025 HostService service = get(HostService.class);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070026 for (Host host : getSortedHosts(service)) {
27 printHost(host);
28 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070029 }
30
31 /**
32 * Returns the list of devices sorted using the device ID URIs.
33 *
34 * @param service device service
35 * @return sorted device list
36 */
37 protected List<Host> getSortedHosts(HostService service) {
38 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070039 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070040 return hosts;
41 }
42
43 /**
44 * Prints information about a host.
45 *
46 * @param host
47 */
48 protected void printHost(Host host) {
49 if (host != null) {
50 print(FMT, host.id(), host.mac(),
51 host.location().deviceId(),
52 host.location().port(),
53 host.vlan(), host.ipAddresses());
54 }
55 }
56 }