Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 java.util.Collections; |
| 19 | import java.util.List; |
| 20 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.cli.Comparators; |
| 24 | import org.onosproject.net.Host; |
| 25 | import org.onosproject.net.host.HostService; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 26 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 27 | import com.fasterxml.jackson.databind.JsonNode; |
| 28 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | import com.fasterxml.jackson.databind.node.ArrayNode; |
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", |
tom | 32085cf | 2014-10-16 00:04:33 -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 = |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 41 | "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s"; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 42 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 43 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 44 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 45 | HostService service = get(HostService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 46 | if (outputJson()) { |
| 47 | print("%s", json(getSortedHosts(service))); |
| 48 | } else { |
| 49 | for (Host host : getSortedHosts(service)) { |
| 50 | printHost(host); |
| 51 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 52 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 53 | } |
| 54 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 55 | // Produces JSON structure. |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 56 | private JsonNode json(Iterable<Host> hosts) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 57 | ObjectMapper mapper = new ObjectMapper(); |
| 58 | ArrayNode result = mapper.createArrayNode(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 60 | hosts.forEach(host -> result.add(jsonForEntity(host, Host.class))); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 61 | return result; |
| 62 | } |
| 63 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Returns the list of devices sorted using the device ID URIs. |
| 66 | * |
| 67 | * @param service device service |
| 68 | * @return sorted device list |
| 69 | */ |
| 70 | protected List<Host> getSortedHosts(HostService service) { |
| 71 | List<Host> hosts = newArrayList(service.getHosts()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 72 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 73 | return hosts; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Prints information about a host. |
| 78 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 79 | * @param host end-station host |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 80 | */ |
| 81 | protected void printHost(Host host) { |
| 82 | if (host != null) { |
| 83 | print(FMT, host.id(), host.mac(), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 84 | host.location().deviceId(), |
| 85 | host.location().port(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 86 | host.vlan(), host.ipAddresses(), |
| 87 | annotations(host.annotations())); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 90 | } |