blob: 3a9f86095db1ad39960483f46d7d1d2f02c3b72d [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
Andrea Campanella13032532016-03-31 10:50:50 -070024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.Host;
26import org.onosproject.net.host.HostService;
27import org.onosproject.utils.Comparators;
28
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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Ayaka Koshibe43530be2014-09-15 11:14:52 -070038@Command(scope = "onos", name = "hosts",
Andrea Campanella13032532016-03-31 10:50:50 -070039 description = "Lists all currently-known hosts.")
Ayaka Koshibe43530be2014-09-15 11:14:52 -070040public class HostsListCommand extends AbstractShellCommand {
41
42 private static final String FMT =
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -070043 "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s%s, innerVlan=%s, outerTPID=%s, " +
44 "provider=%s:%s, configured=%s";
Ayaka Koshibe43530be2014-09-15 11:14:52 -070045
Andrea Campanella13032532016-03-31 10:50:50 -070046 private static final String FMT_SHORT =
Yi Tsengbbee6a62017-06-27 13:22:37 -070047 "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s";
Andrea Campanella13032532016-03-31 10:50:50 -070048
49 @Option(name = "-s", aliases = "--short", description = "Show short output only",
50 required = false, multiValued = false)
51 private boolean shortOnly = false;
52
Ayaka Koshibe43530be2014-09-15 11:14:52 -070053 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 protected void doExecute() {
tomcaf3bf72014-09-23 13:20:53 -070055 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070056 if (outputJson()) {
57 print("%s", json(getSortedHosts(service)));
58 } else {
59 for (Host host : getSortedHosts(service)) {
60 printHost(host);
61 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070062 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070063 }
64
tom32085cf2014-10-16 00:04:33 -070065 // Produces JSON structure.
Ray Milkey3078fc02015-05-06 16:14:14 -070066 private JsonNode json(Iterable<Host> hosts) {
tom32085cf2014-10-16 00:04:33 -070067 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
tom32085cf2014-10-16 00:04:33 -070069
Ray Milkey3078fc02015-05-06 16:14:14 -070070 hosts.forEach(host -> result.add(jsonForEntity(host, Host.class)));
tom32085cf2014-10-16 00:04:33 -070071 return result;
72 }
73
Ayaka Koshibe43530be2014-09-15 11:14:52 -070074 /**
75 * Returns the list of devices sorted using the device ID URIs.
76 *
77 * @param service device service
78 * @return sorted device list
79 */
80 protected List<Host> getSortedHosts(HostService service) {
81 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070082 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070083 return hosts;
84 }
85
86 /**
87 * Prints information about a host.
88 *
tom32085cf2014-10-16 00:04:33 -070089 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -070090 */
91 protected void printHost(Host host) {
Andrea Campanella13032532016-03-31 10:50:50 -070092 if (shortOnly) {
93 print(FMT_SHORT, host.id(), host.mac(),
Charles Chancd06c692017-04-27 20:46:06 -070094 host.locations(),
Andrea Campanella13032532016-03-31 10:50:50 -070095 host.vlan(), host.ipAddresses());
96 } else {
Ayaka Koshibe43530be2014-09-15 11:14:52 -070097 print(FMT, host.id(), host.mac(),
Charles Chancd06c692017-04-27 20:46:06 -070098 host.locations(),
sdn5e935452016-08-30 04:12:54 -070099 host.vlan(), host.ipAddresses(), annotations(host.annotations()),
Jonghwan Hyun2c95acf2018-03-14 16:47:34 -0700100 host.innerVlan(), host.tpid().toString(),
Charles Chan4e553012017-07-24 21:13:01 -0700101 host.providerId().scheme(), host.providerId().id(),
sdn5e935452016-08-30 04:12:54 -0700102 host.configured());
Ayaka Koshibe43530be2014-09-15 11:14:52 -0700103 }
104 }
tom32085cf2014-10-16 00:04:33 -0700105}
sdn5e935452016-08-30 04:12:54 -0700106