Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.onlab.onos.cli.AbstractShellCommand; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 27 | import org.onlab.onos.cli.Comparators; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 28 | import org.onlab.onos.net.Host; |
| 29 | import org.onlab.onos.net.host.HostService; |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 31 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 32 | import java.util.Collections; |
| 33 | import java.util.List; |
| 34 | |
| 35 | import static com.google.common.collect.Lists.newArrayList; |
| 36 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Lists all currently-known hosts. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "hosts", |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 41 | description = "Lists all currently-known hosts.") |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 42 | public class HostsListCommand extends AbstractShellCommand { |
| 43 | |
| 44 | private static final String FMT = |
| 45 | "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s"; |
| 46 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 47 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 48 | protected void execute() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 49 | HostService service = get(HostService.class); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 50 | if (outputJson()) { |
| 51 | print("%s", json(getSortedHosts(service))); |
| 52 | } else { |
| 53 | for (Host host : getSortedHosts(service)) { |
| 54 | printHost(host); |
| 55 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 56 | } |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 57 | } |
| 58 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | // Produces JSON structure. |
| 60 | private static JsonNode json(Iterable<Host> hosts) { |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | ArrayNode result = mapper.createArrayNode(); |
| 63 | for (Host host : hosts) { |
| 64 | result.add(json(mapper, host)); |
| 65 | } |
| 66 | return result; |
| 67 | } |
| 68 | |
| 69 | // Produces JSON structure. |
| 70 | private static JsonNode json(ObjectMapper mapper, Host host) { |
| 71 | ObjectNode loc = LinksListCommand.json(mapper, host.location()) |
| 72 | .put("time", host.location().time()); |
| 73 | ArrayNode ips = mapper.createArrayNode(); |
| 74 | for (IpPrefix ip : host.ipAddresses()) { |
| 75 | ips.add(ip.toString()); |
| 76 | } |
| 77 | ObjectNode result = mapper.createObjectNode() |
| 78 | .put("id", host.id().toString()) |
| 79 | .put("mac", host.mac().toString()) |
| 80 | .put("vlan", host.vlan().toString()); |
| 81 | result.set("location", loc); |
| 82 | result.set("ips", ips); |
| 83 | return result; |
| 84 | } |
| 85 | |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Returns the list of devices sorted using the device ID URIs. |
| 88 | * |
| 89 | * @param service device service |
| 90 | * @return sorted device list |
| 91 | */ |
| 92 | protected List<Host> getSortedHosts(HostService service) { |
| 93 | List<Host> hosts = newArrayList(service.getHosts()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 94 | Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 95 | return hosts; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Prints information about a host. |
| 100 | * |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 101 | * @param host end-station host |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 102 | */ |
| 103 | protected void printHost(Host host) { |
| 104 | if (host != null) { |
| 105 | print(FMT, host.id(), host.mac(), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 106 | host.location().deviceId(), |
| 107 | host.location().port(), |
| 108 | host.vlan(), host.ipAddresses()); |
Ayaka Koshibe | 43530be | 2014-09-15 11:14:52 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 111 | } |