Jonathan Hart

Added CLI command to view address bindings

Change-Id: I1f1ac8ec7766691fb7dadee33a6f75cc560b0da8
......@@ -15,16 +15,18 @@
*/
package org.onlab.onos.cli;
import org.onlab.onos.core.ApplicationId;
import java.util.Comparator;
import org.onlab.onos.cluster.ControllerNode;
import org.onlab.onos.core.ApplicationId;
import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.Element;
import org.onlab.onos.net.ElementId;
import org.onlab.onos.net.Port;
import org.onlab.onos.net.flow.FlowRule;
import org.onlab.onos.net.host.PortAddresses;
import org.onlab.onos.net.topology.TopologyCluster;
import java.util.Comparator;
/**
* Various comparators.
*/
......@@ -84,4 +86,21 @@ public final class Comparators {
}
};
public static final Comparator<ConnectPoint> CONNECT_POINT_COMPARATOR = new Comparator<ConnectPoint>() {
@Override
public int compare(ConnectPoint o1, ConnectPoint o2) {
int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId());
return (compareId != 0) ?
compareId :
Long.signum(o1.port().toLong() - o2.port().toLong());
}
};
public static final Comparator<PortAddresses> ADDRESSES_COMPARATOR = new Comparator<PortAddresses>() {
@Override
public int compare(PortAddresses arg0, PortAddresses arg1) {
return CONNECT_POINT_COMPARATOR.compare(arg0.connectPoint(), arg1.connectPoint());
}
};
}
......
package org.onlab.onos.cli.net;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.cli.Comparators;
import org.onlab.onos.net.host.HostService;
import org.onlab.onos.net.host.InterfaceIpAddress;
import org.onlab.onos.net.host.PortAddresses;
import com.google.common.collect.Lists;
/**
* Lists all configured address port bindings.
*/
@Command(scope = "onos", name = "address-bindings",
description = "Lists all configured address port bindings.")
public class AddressBindingsListCommand extends AbstractShellCommand {
private static final String FORMAT =
"port=%s/%s, ip(s)=%s, mac=%s";
@Override
protected void execute() {
HostService hostService = get(HostService.class);
List<PortAddresses> addresses =
Lists.newArrayList(hostService.getAddressBindings());
Collections.sort(addresses, Comparators.ADDRESSES_COMPARATOR);
for (PortAddresses pa : addresses) {
print(FORMAT, pa.connectPoint().deviceId(), pa.connectPoint().port(),
printIpAddresses(pa.ipAddresses()), pa.mac());
}
}
private String printIpAddresses(Set<InterfaceIpAddress> addresses) {
StringBuilder output = new StringBuilder("[");
for (InterfaceIpAddress address : addresses) {
output.append(address.ipAddress().toString());
output.append("/");
output.append(address.subnetAddress().prefixLength());
output.append(", ");
}
// Remove the last comma
output.delete(output.length() - 2 , output.length());
output.append("]");
return output.toString();
}
}
......@@ -169,6 +169,9 @@
<command>
<action class="org.onlab.onos.cli.net.HostsListCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.net.AddressBindingsListCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.net.FlowsListCommand"/>
......