Ayaka Koshibe

Host list command added to CLI

Change-Id: I52d11d3b1d56650e92357b6cb2c67007be742114
1 +package org.onlab.onos.cli.net;
2 +
3 +import java.util.Iterator;
4 +import java.util.List;
5 +import java.util.SortedSet;
6 +
7 +import org.apache.karaf.shell.console.Completer;
8 +import org.apache.karaf.shell.console.completer.StringsCompleter;
9 +import org.onlab.onos.cli.AbstractShellCommand;
10 +import org.onlab.onos.net.Host;
11 +import org.onlab.onos.net.host.HostService;
12 +
13 +public class HostIdCompleter implements Completer {
14 +
15 + @Override
16 + public int complete(String buffer, int cursor, List<String> candidates) {
17 + // Delegate string completer
18 + StringsCompleter delegate = new StringsCompleter();
19 +
20 + HostService service = AbstractShellCommand.get(HostService.class);
21 + Iterator<Host> it = service.getHosts().iterator();
22 + SortedSet<String> strings = delegate.getStrings();
23 + while (it.hasNext()) {
24 + strings.add(it.next().id().toString());
25 + }
26 +
27 + // Now let the completer do the work for figuring out what to offer.
28 + return delegate.complete(buffer, cursor, candidates);
29 +
30 + }
31 +
32 +}
1 +package org.onlab.onos.cli.net;
2 +
3 +import static com.google.common.collect.Lists.newArrayList;
4 +
5 +import java.util.Collections;
6 +import java.util.Comparator;
7 +import java.util.List;
8 +
9 +import org.apache.karaf.shell.commands.Command;
10 +import org.onlab.onos.cli.AbstractShellCommand;
11 +import org.onlab.onos.net.Host;
12 +import org.onlab.onos.net.host.HostService;
13 +
14 +/**
15 + * Lists all currently-known hosts.
16 + */
17 +@Command(scope = "onos", name = "hosts",
18 + description = "Lists all currently-known hosts.")
19 +public class HostsListCommand extends AbstractShellCommand {
20 +
21 + private static final String FMT =
22 + "id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
23 +
24 + protected static final Comparator<Host> ID_COMPARATOR = new Comparator<Host>() {
25 + @Override
26 + public int compare(Host h1, Host h2) {
27 + return h1.id().uri().toString().compareTo(h2.id().uri().toString());
28 + }
29 + };
30 +
31 + @Override
32 + protected Object doExecute() throws Exception {
33 + HostService service = getService(HostService.class);
34 + for (Host host : getSortedHosts(service)) {
35 + printHost(host);
36 + }
37 + return null;
38 + }
39 +
40 + /**
41 + * Returns the list of devices sorted using the device ID URIs.
42 + *
43 + * @param service device service
44 + * @return sorted device list
45 + */
46 + protected List<Host> getSortedHosts(HostService service) {
47 + List<Host> hosts = newArrayList(service.getHosts());
48 + Collections.sort(hosts, ID_COMPARATOR);
49 + return hosts;
50 + }
51 +
52 + /**
53 + * Prints information about a host.
54 + *
55 + * @param host
56 + */
57 + protected void printHost(Host host) {
58 + if (host != null) {
59 + print(FMT, host.id(), host.mac(),
60 + host.location().deviceId(),
61 + host.location().port(),
62 + host.vlan(), host.ipAddresses());
63 + }
64 + }
65 + }
...@@ -55,10 +55,19 @@ ...@@ -55,10 +55,19 @@
55 <ref component-id="clusterIdCompleter"/> 55 <ref component-id="clusterIdCompleter"/>
56 </completers> 56 </completers>
57 </command> 57 </command>
58 +
59 + <command>
60 + <action class="org.onlab.onos.cli.net.HostsListCommand"/>
61 + <completers>
62 + <ref component-id="hostIdCompleter"/>
63 + </completers>
64 + </command>
65 +
58 </command-bundle> 66 </command-bundle>
59 67
60 <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/> 68 <bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/>
61 <bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/> 69 <bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/>
62 <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/> 70 <bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
71 + <bean id="hostIdCompleter" class="org.onlab.onos.cli.net.HostIdCompleter"/>
63 72
64 </blueprint> 73 </blueprint>
......
...@@ -19,6 +19,7 @@ import org.onlab.onos.net.host.HostProviderRegistry; ...@@ -19,6 +19,7 @@ import org.onlab.onos.net.host.HostProviderRegistry;
19 import org.onlab.onos.net.host.HostProviderService; 19 import org.onlab.onos.net.host.HostProviderService;
20 import org.onlab.onos.net.provider.AbstractProvider; 20 import org.onlab.onos.net.provider.AbstractProvider;
21 import org.onlab.onos.net.provider.ProviderId; 21 import org.onlab.onos.net.provider.ProviderId;
22 +import org.onlab.onos.of.controller.Dpid;
22 import org.onlab.onos.of.controller.OpenFlowController; 23 import org.onlab.onos.of.controller.OpenFlowController;
23 import org.onlab.onos.of.controller.OpenFlowPacketContext; 24 import org.onlab.onos.of.controller.OpenFlowPacketContext;
24 import org.onlab.onos.of.controller.PacketListener; 25 import org.onlab.onos.of.controller.PacketListener;
...@@ -61,7 +62,7 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid ...@@ -61,7 +62,7 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid
61 @Activate 62 @Activate
62 public void activate() { 63 public void activate() {
63 providerService = providerRegistry.register(this); 64 providerService = providerRegistry.register(this);
64 - controller.addPacketListener(0, listener); 65 + controller.addPacketListener(10, listener);
65 66
66 log.info("Started"); 67 log.info("Started");
67 } 68 }
...@@ -92,7 +93,7 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid ...@@ -92,7 +93,7 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid
92 HostId hid = HostId.hostId( 93 HostId hid = HostId.hostId(
93 eth.getSourceMAC(), vlan); 94 eth.getSourceMAC(), vlan);
94 HostLocation hloc = new HostLocation( 95 HostLocation hloc = new HostLocation(
95 - DeviceId.deviceId("of:" + Long.toHexString(pktCtx.dpid().value())), 96 + DeviceId.deviceId(Dpid.uri(pktCtx.dpid())),
96 PortNumber.portNumber(pktCtx.inPort()), 97 PortNumber.portNumber(pktCtx.inPort()),
97 System.currentTimeMillis()); 98 System.currentTimeMillis());
98 ARP arp = (ARP) eth.getPayload(); 99 ARP arp = (ARP) eth.getPayload();
......