Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 3 | import org.apache.karaf.shell.commands.Command; |
| 4 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 5 | import org.onlab.onos.cli.Comparators; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 6 | import org.onlab.onos.net.Host; |
| 7 | import org.onlab.onos.net.host.HostService; |
| 8 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 9 | import java.util.Collections; |
| 10 | import java.util.List; |
| 11 | |
| 12 | import static com.google.common.collect.Lists.newArrayList; |
| 13 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 14 | /** |
| 15 | * Lists all currently-known hosts. |
| 16 | */ |
| 17 | @Command(scope = "onos", name = "hosts", |
| 18 | description = "Lists all currently-known hosts.") |
| 19 | public 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 Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 24 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 25 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 26 | HostService service = get(HostService.class); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 27 | for (Host host : getSortedHosts(service)) { |
| 28 | printHost(host); |
| 29 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 30 | } |
| 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()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 40 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 41 | 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 | } |