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