blob: 95b72e5d1c1ed755712c7c436b15e1c3c7f72bd1 [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;
27
28import java.util.Collections;
29import java.util.List;
tom1380eee2014-09-24 09:22:02 -070030
31import static com.google.common.collect.Lists.newArrayList;
32
Ayaka Koshibe43530be2014-09-15 11:14:52 -070033/**
34 * Lists all currently-known hosts.
35 */
36@Command(scope = "onos", name = "hosts",
Andrea Campanella13032532016-03-31 10:50:50 -070037 description = "Lists all currently-known hosts.")
Ayaka Koshibe43530be2014-09-15 11:14:52 -070038public class HostsListCommand extends AbstractShellCommand {
39
40 private static final String FMT =
Charles Chancd06c692017-04-27 20:46:06 -070041 "id=%s, mac=%s, location=%s, vlan=%s, ip(s)=%s%s, configured=%s";
Ayaka Koshibe43530be2014-09-15 11:14:52 -070042
Andrea Campanella13032532016-03-31 10:50:50 -070043 private static final String FMT_SHORT =
Charles Chancd06c692017-04-27 20:46:06 -070044 "id=%s, mac=%s, location=%s, vlan=%s, ip(s)=%s";
Andrea Campanella13032532016-03-31 10:50:50 -070045
46 @Option(name = "-s", aliases = "--short", description = "Show short output only",
47 required = false, multiValued = false)
48 private boolean shortOnly = false;
49
Ayaka Koshibe43530be2014-09-15 11:14:52 -070050 @Override
tom0872a172014-09-23 11:24:26 -070051 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070052 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070053 if (outputJson()) {
54 print("%s", json(getSortedHosts(service)));
55 } else {
56 for (Host host : getSortedHosts(service)) {
57 printHost(host);
58 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070059 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070060 }
61
tom32085cf2014-10-16 00:04:33 -070062 // Produces JSON structure.
Ray Milkey3078fc02015-05-06 16:14:14 -070063 private JsonNode json(Iterable<Host> hosts) {
tom32085cf2014-10-16 00:04:33 -070064 ObjectMapper mapper = new ObjectMapper();
65 ArrayNode result = mapper.createArrayNode();
tom32085cf2014-10-16 00:04:33 -070066
Ray Milkey3078fc02015-05-06 16:14:14 -070067 hosts.forEach(host -> result.add(jsonForEntity(host, Host.class)));
tom32085cf2014-10-16 00:04:33 -070068 return result;
69 }
70
Ayaka Koshibe43530be2014-09-15 11:14:52 -070071 /**
72 * Returns the list of devices sorted using the device ID URIs.
73 *
74 * @param service device service
75 * @return sorted device list
76 */
77 protected List<Host> getSortedHosts(HostService service) {
78 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070079 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070080 return hosts;
81 }
82
83 /**
84 * Prints information about a host.
85 *
tom32085cf2014-10-16 00:04:33 -070086 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -070087 */
88 protected void printHost(Host host) {
Andrea Campanella13032532016-03-31 10:50:50 -070089 if (shortOnly) {
90 print(FMT_SHORT, host.id(), host.mac(),
Charles Chancd06c692017-04-27 20:46:06 -070091 host.locations(),
Andrea Campanella13032532016-03-31 10:50:50 -070092 host.vlan(), host.ipAddresses());
93 } else {
Ayaka Koshibe43530be2014-09-15 11:14:52 -070094 print(FMT, host.id(), host.mac(),
Charles Chancd06c692017-04-27 20:46:06 -070095 host.locations(),
sdn5e935452016-08-30 04:12:54 -070096 host.vlan(), host.ipAddresses(), annotations(host.annotations()),
97 host.configured());
Ayaka Koshibe43530be2014-09-15 11:14:52 -070098 }
99 }
tom32085cf2014-10-16 00:04:33 -0700100}
sdn5e935452016-08-30 04:12:54 -0700101