blob: 21e3a5154f5ba874682cbdfa3031d96127e1626f [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070017
Ray Milkey3078fc02015-05-06 16:14:14 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
Andrea Campanella13032532016-03-31 10:50:50 -070021import org.apache.karaf.shell.commands.Command;
22import org.apache.karaf.shell.commands.Option;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.Host;
25import org.onosproject.net.host.HostService;
26import org.onosproject.utils.Comparators;
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053027import org.onlab.util.Tools;
Andrea Campanella13032532016-03-31 10:50:50 -070028
29import java.util.Collections;
30import java.util.List;
tom1380eee2014-09-24 09:22:02 -070031
32import static com.google.common.collect.Lists.newArrayList;
33
Ayaka Koshibe43530be2014-09-15 11:14:52 -070034/**
35 * Lists all currently-known hosts.
36 */
37@Command(scope = "onos", name = "hosts",
Andrea Campanella13032532016-03-31 10:50:50 -070038 description = "Lists all currently-known hosts.")
Ayaka Koshibe43530be2014-09-15 11:14:52 -070039public class HostsListCommand extends AbstractShellCommand {
40
41 private static final String FMT =
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053042 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s, last seen time=%s";
Ayaka Koshibe43530be2014-09-15 11:14:52 -070043
Andrea Campanella13032532016-03-31 10:50:50 -070044 private static final String FMT_SHORT =
45 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
46
47 @Option(name = "-s", aliases = "--short", description = "Show short output only",
48 required = false, multiValued = false)
49 private boolean shortOnly = false;
50
Ayaka Koshibe43530be2014-09-15 11:14:52 -070051 @Override
tom0872a172014-09-23 11:24:26 -070052 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070053 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070054 if (outputJson()) {
55 print("%s", json(getSortedHosts(service)));
56 } else {
57 for (Host host : getSortedHosts(service)) {
58 printHost(host);
59 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070060 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070061 }
62
tom32085cf2014-10-16 00:04:33 -070063 // Produces JSON structure.
Ray Milkey3078fc02015-05-06 16:14:14 -070064 private JsonNode json(Iterable<Host> hosts) {
tom32085cf2014-10-16 00:04:33 -070065 ObjectMapper mapper = new ObjectMapper();
66 ArrayNode result = mapper.createArrayNode();
tom32085cf2014-10-16 00:04:33 -070067
Ray Milkey3078fc02015-05-06 16:14:14 -070068 hosts.forEach(host -> result.add(jsonForEntity(host, Host.class)));
tom32085cf2014-10-16 00:04:33 -070069 return result;
70 }
71
Ayaka Koshibe43530be2014-09-15 11:14:52 -070072 /**
73 * Returns the list of devices sorted using the device ID URIs.
74 *
75 * @param service device service
76 * @return sorted device list
77 */
78 protected List<Host> getSortedHosts(HostService service) {
79 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070080 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070081 return hosts;
82 }
83
84 /**
85 * Prints information about a host.
86 *
tom32085cf2014-10-16 00:04:33 -070087 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -070088 */
89 protected void printHost(Host host) {
Andrea Campanella13032532016-03-31 10:50:50 -070090 if (shortOnly) {
91 print(FMT_SHORT, host.id(), host.mac(),
92 host.location().deviceId(), host.location().port(),
93 host.vlan(), host.ipAddresses());
94 } else {
Ayaka Koshibe43530be2014-09-15 11:14:52 -070095 print(FMT, host.id(), host.mac(),
Andrea Campanella13032532016-03-31 10:50:50 -070096 host.location().deviceId(), host.location().port(),
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +053097 host.vlan(), host.ipAddresses(), annotations(host.annotations()),
98 Tools.timeAgo(host.timestamp().unixTimestamp()));
Ayaka Koshibe43530be2014-09-15 11:14:52 -070099 }
100 }
tom32085cf2014-10-16 00:04:33 -0700101}
Deepa Vaddireddy50ad0802016-08-08 19:27:36 +0530102