blob: cd9ba08fb5bc24ef560e4dd91e41bb723d408181 [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;
tom91c7bd02014-09-25 22:50:44 -07005import org.onlab.onos.cli.Comparators;
Ayaka Koshibe43530be2014-09-15 11:14:52 -07006import org.onlab.onos.net.Host;
7import org.onlab.onos.net.host.HostService;
8
tom1380eee2014-09-24 09:22:02 -07009import java.util.Collections;
10import java.util.List;
11
12import static com.google.common.collect.Lists.newArrayList;
13
Ayaka Koshibe43530be2014-09-15 11:14:52 -070014/**
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
Ayaka Koshibe43530be2014-09-15 11:14:52 -070024 @Override
tom0872a172014-09-23 11:24:26 -070025 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070026 HostService service = get(HostService.class);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070027 for (Host host : getSortedHosts(service)) {
28 printHost(host);
29 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070030 }
31
32 /**
33 * Returns the list of devices sorted using the device ID URIs.
34 *
35 * @param service device service
36 * @return sorted device list
37 */
38 protected List<Host> getSortedHosts(HostService service) {
39 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070040 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070041 return hosts;
42 }
43
44 /**
45 * Prints information about a host.
46 *
47 * @param host
48 */
49 protected void printHost(Host host) {
50 if (host != null) {
51 print(FMT, host.id(), host.mac(),
52 host.location().deviceId(),
53 host.location().port(),
54 host.vlan(), host.ipAddresses());
55 }
56 }
57 }