Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.apache.karaf.shell.commands.Option; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.Host; |
| 25 | import org.onosproject.net.host.HostService; |
| 26 | import org.onosproject.utils.Comparators; |
| 27 | |
| 28 | import java.util.Collections; |
| 29 | import java.util.List; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 30 | |
| 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Lists all currently-known hosts. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "hosts", |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 37 | description = "Lists all currently-known hosts.") |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 38 | public class HostsListCommand extends AbstractShellCommand { |
| 39 | |
| 40 | private static final String FMT = |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 41 | "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s, configured=%s"; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 42 | |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 43 | private static final String FMT_SHORT = |
| 44 | "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s"; |
| 45 | |
| 46 | @Option(name = "-s", aliases = "--short", description = "Show short output only", |
| 47 | required = false, multiValued = false) |
| 48 | private boolean shortOnly = false; |
| 49 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 50 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 51 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 52 | HostService service = get(HostService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 53 | if (outputJson()) { |
| 54 | print("%s", json(getSortedHosts(service))); |
| 55 | } else { |
| 56 | for (Host host : getSortedHosts(service)) { |
| 57 | printHost(host); |
| 58 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 59 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 60 | } |
| 61 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 62 | // Produces JSON structure. |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 63 | private JsonNode json(Iterable<Host> hosts) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 64 | ObjectMapper mapper = new ObjectMapper(); |
| 65 | ArrayNode result = mapper.createArrayNode(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 66 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 67 | hosts.forEach(host -> result.add(jsonForEntity(host, Host.class))); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 68 | return result; |
| 69 | } |
| 70 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Returns the list of devices sorted using the device ID URIs. |
| 73 | * |
| 74 | * @param service device service |
| 75 | * @return sorted device list |
| 76 | */ |
| 77 | protected List<Host> getSortedHosts(HostService service) { |
| 78 | List<Host> hosts = newArrayList(service.getHosts()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 79 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 80 | return hosts; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Prints information about a host. |
| 85 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 86 | * @param host end-station host |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 87 | */ |
| 88 | protected void printHost(Host host) { |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 89 | if (shortOnly) { |
| 90 | print(FMT_SHORT, host.id(), host.mac(), |
| 91 | host.location().deviceId(), host.location().port(), |
| 92 | host.vlan(), host.ipAddresses()); |
| 93 | } else { |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 94 | print(FMT, host.id(), host.mac(), |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 95 | host.location().deviceId(), host.location().port(), |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 96 | host.vlan(), host.ipAddresses(), annotations(host.annotations()), |
| 97 | host.configured()); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 100 | } |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 101 | |