blob: 2291f4e076530c801c046baa0aa41106ada251c9 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Ayaka Koshibe43530be2014-09-15 11:14:52 -070019package org.onlab.onos.cli.net;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070025import org.apache.karaf.shell.commands.Command;
26import org.onlab.onos.cli.AbstractShellCommand;
tom91c7bd02014-09-25 22:50:44 -070027import org.onlab.onos.cli.Comparators;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070028import org.onlab.onos.net.Host;
29import org.onlab.onos.net.host.HostService;
tom32085cf2014-10-16 00:04:33 -070030import org.onlab.packet.IpPrefix;
Ayaka Koshibe43530be2014-09-15 11:14:52 -070031
tom1380eee2014-09-24 09:22:02 -070032import java.util.Collections;
33import java.util.List;
34
35import static com.google.common.collect.Lists.newArrayList;
36
Ayaka Koshibe43530be2014-09-15 11:14:52 -070037/**
38 * Lists all currently-known hosts.
39 */
40@Command(scope = "onos", name = "hosts",
tom32085cf2014-10-16 00:04:33 -070041 description = "Lists all currently-known hosts.")
Ayaka Koshibe43530be2014-09-15 11:14:52 -070042public class HostsListCommand extends AbstractShellCommand {
43
44 private static final String FMT =
45 "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
46
Ayaka Koshibe43530be2014-09-15 11:14:52 -070047 @Override
tom0872a172014-09-23 11:24:26 -070048 protected void execute() {
tomcaf3bf72014-09-23 13:20:53 -070049 HostService service = get(HostService.class);
tom32085cf2014-10-16 00:04:33 -070050 if (outputJson()) {
51 print("%s", json(getSortedHosts(service)));
52 } else {
53 for (Host host : getSortedHosts(service)) {
54 printHost(host);
55 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070056 }
Ayaka Koshibe43530be2014-09-15 11:14:52 -070057 }
58
tom32085cf2014-10-16 00:04:33 -070059 // Produces JSON structure.
60 private static JsonNode json(Iterable<Host> hosts) {
61 ObjectMapper mapper = new ObjectMapper();
62 ArrayNode result = mapper.createArrayNode();
63 for (Host host : hosts) {
64 result.add(json(mapper, host));
65 }
66 return result;
67 }
68
69 // Produces JSON structure.
70 private static JsonNode json(ObjectMapper mapper, Host host) {
71 ObjectNode loc = LinksListCommand.json(mapper, host.location())
72 .put("time", host.location().time());
73 ArrayNode ips = mapper.createArrayNode();
74 for (IpPrefix ip : host.ipAddresses()) {
75 ips.add(ip.toString());
76 }
77 ObjectNode result = mapper.createObjectNode()
78 .put("id", host.id().toString())
79 .put("mac", host.mac().toString())
80 .put("vlan", host.vlan().toString());
81 result.set("location", loc);
82 result.set("ips", ips);
83 return result;
84 }
85
Ayaka Koshibe43530be2014-09-15 11:14:52 -070086 /**
87 * Returns the list of devices sorted using the device ID URIs.
88 *
89 * @param service device service
90 * @return sorted device list
91 */
92 protected List<Host> getSortedHosts(HostService service) {
93 List<Host> hosts = newArrayList(service.getHosts());
tom1380eee2014-09-24 09:22:02 -070094 Collections.sort(hosts, Comparators.ELEMENT_COMPARATOR);
Ayaka Koshibe43530be2014-09-15 11:14:52 -070095 return hosts;
96 }
97
98 /**
99 * Prints information about a host.
100 *
tom32085cf2014-10-16 00:04:33 -0700101 * @param host end-station host
Ayaka Koshibe43530be2014-09-15 11:14:52 -0700102 */
103 protected void printHost(Host host) {
104 if (host != null) {
105 print(FMT, host.id(), host.mac(),
tom32085cf2014-10-16 00:04:33 -0700106 host.location().deviceId(),
107 host.location().port(),
108 host.vlan(), host.ipAddresses());
Ayaka Koshibe43530be2014-09-15 11:14:52 -0700109 }
110 }
tom32085cf2014-10-16 00:04:33 -0700111}