blob: a90627e451fbc0bf0fb1cdd16548faa4e415f7b2 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 java.util.Collections;
19import java.util.List;
20
Ayaka Koshibe43530be2014-09-15 11:14:52 -070021import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.cli.Comparators;
24import org.onosproject.net.Host;
25import org.onosproject.net.host.HostService;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070026
Ray Milkey3078fc02015-05-06 16:14:14 -070027import com.fasterxml.jackson.databind.JsonNode;
28import com.fasterxml.jackson.databind.ObjectMapper;
29import com.fasterxml.jackson.databind.node.ArrayNode;
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",
tom32085cf2014-10-16 00:04:33 -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 =
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070041 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s";
Ayaka Koshibe43530be2014-09-15 11:14:52 -070042
Ayaka Koshibe43530be2014-09-15 11:14:52 -070043 @Override
tom0872a172014-09-23 11:24:26 -070044 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070045 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070046 if (outputJson()) {
47 print("%s", json(getSortedHosts(service)));
48 } else {
49 for (Host host : getSortedHosts(service)) {
50 printHost(host);
51 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070052 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070053 }
54
tom32085cf2014-10-16 00:04:33 -070055 // Produces JSON structure.
Ray Milkey3078fc02015-05-06 16:14:14 -070056 private JsonNode json(Iterable<Host> hosts) {
tom32085cf2014-10-16 00:04:33 -070057 ObjectMapper mapper = new ObjectMapper();
58 ArrayNode result = mapper.createArrayNode();
tom32085cf2014-10-16 00:04:33 -070059
Ray Milkey3078fc02015-05-06 16:14:14 -070060 hosts.forEach(host -> result.add(jsonForEntity(host, Host.class)));
tom32085cf2014-10-16 00:04:33 -070061 return result;
62 }
63
Ayaka Koshibe43530be2014-09-15 11:14:52 -070064 /**
65 * Returns the list of devices sorted using the device ID URIs.
66 *
67 * @param service device service
68 * @return sorted device list
69 */
70 protected List<Host> getSortedHosts(HostService service) {
71 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070072 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070073 return hosts;
74 }
75
76 /**
77 * Prints information about a host.
78 *
tom32085cf2014-10-16 00:04:33 -070079 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -070080 */
81 protected void printHost(Host host) {
82 if (host != null) {
83 print(FMT, host.id(), host.mac(),
tom32085cf2014-10-16 00:04:33 -070084 host.location().deviceId(),
85 host.location().port(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070086 host.vlan(), host.ipAddresses(),
87 annotations(host.annotations()));
Ayaka Koshibe43530be2014-09-15 11:14:52 -070088 }
89 }
tom32085cf2014-10-16 00:04:33 -070090}