Ayaka Koshibe

Host list command added to CLI

Change-Id: I52d11d3b1d56650e92357b6cb2c67007be742114
package org.onlab.onos.cli.net;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import org.apache.karaf.shell.console.Completer;
import org.apache.karaf.shell.console.completer.StringsCompleter;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.net.Host;
import org.onlab.onos.net.host.HostService;
public class HostIdCompleter implements Completer {
@Override
public int complete(String buffer, int cursor, List<String> candidates) {
// Delegate string completer
StringsCompleter delegate = new StringsCompleter();
HostService service = AbstractShellCommand.get(HostService.class);
Iterator<Host> it = service.getHosts().iterator();
SortedSet<String> strings = delegate.getStrings();
while (it.hasNext()) {
strings.add(it.next().id().toString());
}
// Now let the completer do the work for figuring out what to offer.
return delegate.complete(buffer, cursor, candidates);
}
}
package org.onlab.onos.cli.net;
import static com.google.common.collect.Lists.newArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.net.Host;
import org.onlab.onos.net.host.HostService;
/**
* Lists all currently-known hosts.
*/
@Command(scope = "onos", name = "hosts",
description = "Lists all currently-known hosts.")
public class HostsListCommand extends AbstractShellCommand {
private static final String FMT =
"id=%s, mac=%s, location=%s/%s, vlan=%s, ip(s)=%s";
protected static final Comparator<Host> ID_COMPARATOR = new Comparator<Host>() {
@Override
public int compare(Host h1, Host h2) {
return h1.id().uri().toString().compareTo(h2.id().uri().toString());
}
};
@Override
protected Object doExecute() throws Exception {
HostService service = getService(HostService.class);
for (Host host : getSortedHosts(service)) {
printHost(host);
}
return null;
}
/**
* Returns the list of devices sorted using the device ID URIs.
*
* @param service device service
* @return sorted device list
*/
protected List<Host> getSortedHosts(HostService service) {
List<Host> hosts = newArrayList(service.getHosts());
Collections.sort(hosts, ID_COMPARATOR);
return hosts;
}
/**
* Prints information about a host.
*
* @param host
*/
protected void printHost(Host host) {
if (host != null) {
print(FMT, host.id(), host.mac(),
host.location().deviceId(),
host.location().port(),
host.vlan(), host.ipAddresses());
}
}
}
......@@ -55,10 +55,19 @@
<ref component-id="clusterIdCompleter"/>
</completers>
</command>
<command>
<action class="org.onlab.onos.cli.net.HostsListCommand"/>
<completers>
<ref component-id="hostIdCompleter"/>
</completers>
</command>
</command-bundle>
<bean id="deviceIdCompleter" class="org.onlab.onos.cli.net.DeviceIdCompleter"/>
<bean id="clusterIdCompleter" class="org.onlab.onos.cli.net.ClusterIdCompleter"/>
<bean id="roleCompleter" class="org.onlab.onos.cli.net.RoleCompleter"/>
<bean id="hostIdCompleter" class="org.onlab.onos.cli.net.HostIdCompleter"/>
</blueprint>
......
......@@ -19,6 +19,7 @@ import org.onlab.onos.net.host.HostProviderRegistry;
import org.onlab.onos.net.host.HostProviderService;
import org.onlab.onos.net.provider.AbstractProvider;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.onos.of.controller.Dpid;
import org.onlab.onos.of.controller.OpenFlowController;
import org.onlab.onos.of.controller.OpenFlowPacketContext;
import org.onlab.onos.of.controller.PacketListener;
......@@ -61,7 +62,7 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid
@Activate
public void activate() {
providerService = providerRegistry.register(this);
controller.addPacketListener(0, listener);
controller.addPacketListener(10, listener);
log.info("Started");
}
......@@ -92,7 +93,7 @@ public class OpenFlowHostProvider extends AbstractProvider implements HostProvid
HostId hid = HostId.hostId(
eth.getSourceMAC(), vlan);
HostLocation hloc = new HostLocation(
DeviceId.deviceId("of:" + Long.toHexString(pktCtx.dpid().value())),
DeviceId.deviceId(Dpid.uri(pktCtx.dpid())),
PortNumber.portNumber(pktCtx.inPort()),
System.currentTimeMillis());
ARP arp = (ARP) eth.getPayload();
......