blob: 10d015ffd95036f17f40377ece42eaf96e60de6c [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
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070022import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.cli.Comparators;
25import org.onosproject.net.Host;
26import org.onosproject.net.host.HostService;
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070027import org.onlab.packet.IpAddress;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070028
tom1380eee2014-09-24 09:22:02 -070029import java.util.Collections;
30import java.util.List;
31
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",
tom32085cf2014-10-16 00:04:33 -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 =
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070042 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s%s";
Ayaka Koshibe43530be2014-09-15 11:14:52 -070043
Ayaka Koshibe43530be2014-09-15 11:14:52 -070044 @Override
tom0872a172014-09-23 11:24:26 -070045 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070046 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070047 if (outputJson()) {
48 print("%s", json(getSortedHosts(service)));
49 } else {
50 for (Host host : getSortedHosts(service)) {
51 printHost(host);
52 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070053 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070054 }
55
tom32085cf2014-10-16 00:04:33 -070056 // Produces JSON structure.
57 private static JsonNode json(Iterable<Host> hosts) {
58 ObjectMapper mapper = new ObjectMapper();
59 ArrayNode result = mapper.createArrayNode();
60 for (Host host : hosts) {
61 result.add(json(mapper, host));
62 }
63 return result;
64 }
65
66 // Produces JSON structure.
67 private static JsonNode json(ObjectMapper mapper, Host host) {
68 ObjectNode loc = LinksListCommand.json(mapper, host.location())
69 .put("time", host.location().time());
70 ArrayNode ips = mapper.createArrayNode();
Pavlin Radoslavov33f228a2014-10-27 19:33:16 -070071 for (IpAddress ip : host.ipAddresses()) {
tom32085cf2014-10-16 00:04:33 -070072 ips.add(ip.toString());
73 }
74 ObjectNode result = mapper.createObjectNode()
75 .put("id", host.id().toString())
76 .put("mac", host.mac().toString())
77 .put("vlan", host.vlan().toString());
78 result.set("location", loc);
79 result.set("ips", ips);
Thomas Vachuska944cb6c2014-10-22 17:05:42 -070080 result.set("annotations", annotations(mapper, host.annotations()));
tom32085cf2014-10-16 00:04:33 -070081 return result;
82 }
83
Ayaka Koshibe43530be2014-09-15 11:14:52 -070084 /**
85 * Returns the list of devices sorted using the device ID URIs.
86 *
87 * @param service device service
88 * @return sorted device list
89 */
90 protected List<Host> getSortedHosts(HostService service) {
91 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070092 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070093 return hosts;
94 }
95
96 /**
97 * Prints information about a host.
98 *
tom32085cf2014-10-16 00:04:33 -070099 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -0700100 */
101 protected void printHost(Host host) {
102 if (host != null) {
103 print(FMT, host.id(), host.mac(),
tom32085cf2014-10-16 00:04:33 -0700104 host.location().deviceId(),
105 host.location().port(),
Thomas Vachuska944cb6c2014-10-22 17:05:42 -0700106 host.vlan(), host.ipAddresses(),
107 annotations(host.annotations()));
Ayaka Koshibe43530be2014-09-15 11:14:52 -0700108 }
109 }
tom32085cf2014-10-16 00:04:33 -0700110}