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 | */ |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -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; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 24 | import org.onlab.onos.cli.Comparators; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.Host; |
| 26 | import org.onlab.onos.net.host.HostService; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 28 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 29 | import java.util.Collections; |
| 30 | import java.util.List; |
| 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 | */ |
| 37 | @Command(scope = "onos", name = "hosts", |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 38 | description = "Lists all currently-known hosts.") |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 39 | public class HostsListCommand extends AbstractShellCommand { |
| 40 | |
| 41 | private static final String FMT = |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 42 | "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] | 43 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 44 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 45 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 46 | HostService service = get(HostService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 47 | if (outputJson()) { |
| 48 | print("%s", json(getSortedHosts(service))); |
| 49 | } else { |
| 50 | for (Host host : getSortedHosts(service)) { |
| 51 | printHost(host); |
| 52 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 53 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 56 | // Produces JSON structure. |
| 57 | private static JsonNode json(Iterable<Host> hosts) { |
| 58 | ObjectMapper mapper = new ObjectMapper(); |
| 59 | ArrayNode result = mapper.createArrayNode(); |
| 60 | for (Host host : hosts) { |
| 61 | result.add(json(mapper, host)); |
| 62 | } |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | // Produces JSON structure. |
| 67 | private static JsonNode json(ObjectMapper mapper, Host host) { |
| 68 | ObjectNode loc = LinksListCommand.json(mapper, host.location()) |
| 69 | .put("time", host.location().time()); |
| 70 | ArrayNode ips = mapper.createArrayNode(); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 71 | for (IpAddress ip : host.ipAddresses()) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 72 | ips.add(ip.toString()); |
| 73 | } |
| 74 | ObjectNode result = mapper.createObjectNode() |
| 75 | .put("id", host.id().toString()) |
| 76 | .put("mac", host.mac().toString()) |
| 77 | .put("vlan", host.vlan().toString()); |
| 78 | result.set("location", loc); |
| 79 | result.set("ips", ips); |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 80 | result.set("annotations", annotations(mapper, host.annotations())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 81 | return result; |
| 82 | } |
| 83 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Returns the list of devices sorted using the device ID URIs. |
| 86 | * |
| 87 | * @param service device service |
| 88 | * @return sorted device list |
| 89 | */ |
| 90 | protected List<Host> getSortedHosts(HostService service) { |
| 91 | List<Host> hosts = newArrayList(service.getHosts()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 92 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 93 | return hosts; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Prints information about a host. |
| 98 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 99 | * @param host end-station host |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 100 | */ |
| 101 | protected void printHost(Host host) { |
| 102 | if (host != null) { |
| 103 | print(FMT, host.id(), host.mac(), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 104 | host.location().deviceId(), |
| 105 | host.location().port(), |
Thomas Vachuska | 944cb6c | 2014-10-22 17:05:42 -0700 | [diff] [blame] | 106 | host.vlan(), host.ipAddresses(), |
| 107 | annotations(host.annotations())); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 110 | } |