Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 23 | import org.apache.karaf.shell.api.action.Option; |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.host.HostService; |
| 27 | import org.onosproject.utils.Comparators; |
| 28 | |
| 29 | import java.util.Collections; |
| 30 | import java.util.List; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 31 | |
| 32 | import static com.google.common.collect.Lists.newArrayList; |
| 33 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Lists all currently-known hosts. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "hosts", |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 39 | description = "Lists all currently-known hosts.") |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 40 | public class HostsListCommand extends AbstractShellCommand { |
| 41 | |
| 42 | private static final String FMT = |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 43 | "id=%s, mac=%s, locations=%s, auxLocations=%s, vlan=%s, ip(s)=%s%s, innerVlan=%s, outerTPID=%s, " + |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 44 | "provider=%s:%s, configured=%s"; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 45 | |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 46 | private static final String FMT_SHORT = |
Yi Tseng | bbee6a6 | 2017-06-27 13:22:37 -0700 | [diff] [blame] | 47 | "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s"; |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 48 | |
| 49 | @Option(name = "-s", aliases = "--short", description = "Show short output only", |
| 50 | required = false, multiValued = false) |
| 51 | private boolean shortOnly = false; |
| 52 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 53 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | protected void doExecute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 55 | HostService service = get(HostService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 56 | if (outputJson()) { |
| 57 | print("%s", json(getSortedHosts(service))); |
| 58 | } else { |
| 59 | for (Host host : getSortedHosts(service)) { |
| 60 | printHost(host); |
| 61 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 62 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 63 | } |
| 64 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 65 | // Produces JSON structure. |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 66 | private JsonNode json(Iterable<Host> hosts) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 67 | ObjectMapper mapper = new ObjectMapper(); |
| 68 | ArrayNode result = mapper.createArrayNode(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 69 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 70 | hosts.forEach(host -> result.add(jsonForEntity(host, Host.class))); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 71 | return result; |
| 72 | } |
| 73 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 74 | /** |
| 75 | * Returns the list of devices sorted using the device ID URIs. |
| 76 | * |
| 77 | * @param service device service |
| 78 | * @return sorted device list |
| 79 | */ |
| 80 | protected List<Host> getSortedHosts(HostService service) { |
| 81 | List<Host> hosts = newArrayList(service.getHosts()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 82 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 83 | return hosts; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Prints information about a host. |
| 88 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 89 | * @param host end-station host |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 90 | */ |
| 91 | protected void printHost(Host host) { |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 92 | if (shortOnly) { |
| 93 | print(FMT_SHORT, host.id(), host.mac(), |
Charles Chan | cd06c69 | 2017-04-27 20:46:06 -0700 | [diff] [blame] | 94 | host.locations(), |
Andrea Campanella | 1303253 | 2016-03-31 10:50:50 -0700 | [diff] [blame] | 95 | host.vlan(), host.ipAddresses()); |
| 96 | } else { |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 97 | print(FMT, host.id(), host.mac(), |
Charles Chan | 42d109d | 2019-12-18 15:49:39 -0800 | [diff] [blame] | 98 | host.locations(), host.auxLocations(), |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 99 | host.vlan(), host.ipAddresses(), annotations(host.annotations()), |
Jonghwan Hyun | 2c95acf | 2018-03-14 16:47:34 -0700 | [diff] [blame] | 100 | host.innerVlan(), host.tpid().toString(), |
Charles Chan | 4e55301 | 2017-07-24 21:13:01 -0700 | [diff] [blame] | 101 | host.providerId().scheme(), host.providerId().id(), |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 102 | host.configured()); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 105 | } |
sdn | 5e93545 | 2016-08-30 04:12:54 -0700 | [diff] [blame] | 106 | |