blob: 683ab261600fa1162cae2bfd728f31f4cefab614 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 =
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070041 "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s%s, innerVlan=%s, outerTPID=%s, " +
42 "provider=%s:%s, configured=%s";
Ayaka Koshibe43530be2014-09-15 11:14:52 -070043
Andrea Campanella13032532016-03-31 10:50:50 -070044 private static final String FMT_SHORT =
Yi Tsengbbee6a62017-06-27 13:22:37 -070045 "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s";
Andrea Campanella13032532016-03-31 10:50:50 -070046
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(),
Charles Chancd06c692017-04-27 20:46:06 -070092 host.locations(),
Andrea Campanella13032532016-03-31 10:50:50 -070093 host.vlan(), host.ipAddresses());
94 } else {
Ayaka Koshibe43530be2014-09-15 11:14:52 -070095 print(FMT, host.id(), host.mac(),
Charles Chancd06c692017-04-27 20:46:06 -070096 host.locations(),
sdn5e935452016-08-30 04:12:54 -070097 host.vlan(), host.ipAddresses(), annotations(host.annotations()),
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070098 host.innerVlan(), host.tpid().toString(),
Charles Chan4e553012017-07-24 21:13:01 -070099 host.providerId().scheme(), host.providerId().id(),
sdn5e935452016-08-30 04:12:54 -0700100 host.configured());
Ayaka Koshibe43530be2014-09-15 11:14:52 -0700101 }
102 }
tom32085cf2014-10-16 00:04:33 -0700103}
sdn5e935452016-08-30 04:12:54 -0700104