tom

Added commands to list paths.

Added protection against bad input on some command-lines.
......@@ -31,4 +31,14 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport {
System.out.println(String.format(format, args));
}
/**
* Prints the arguments using the specified format to error stream.
*
* @param format format string; see {@link String#format}
* @param args arguments
*/
public static void error(String format, Object... args) {
System.err.println(String.format(format, args));
}
}
......
......@@ -35,11 +35,16 @@ public class ClusterDevicesCommand extends ClustersListCommand {
int cid = Integer.parseInt(id);
init();
TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster));
Collections.sort(ids, ID_COMPARATOR);
for (DeviceId deviceId : ids) {
print("%s", deviceId);
if (cluster == null) {
error("No such cluster %s", cid);
} else {
List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster));
Collections.sort(ids, ID_COMPARATOR);
for (DeviceId deviceId : ids) {
print("%s", deviceId);
}
}
return null;
}
......
......@@ -24,8 +24,12 @@ public class ClusterLinksCommand extends ClustersListCommand {
int cid = Integer.parseInt(id);
init();
TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
for (Link link : service.getClusterLinks(topology, cluster)) {
print(linkString(link));
if (cluster == null) {
error("No such cluster %s", cid);
} else {
for (Link link : service.getClusterLinks(topology, cluster)) {
print(linkString(link));
}
}
return null;
}
......
......@@ -42,7 +42,12 @@ public class DevicePortsListCommand extends DevicesListCommand {
printDevice(service, device);
}
} else {
printDevice(service, service.getDevice(deviceId(uri)));
Device device = service.getDevice(deviceId(uri));
if (device == null) {
error("No such device %s", uri);
} else {
printDevice(service, device);
}
}
return null;
}
......
......@@ -56,10 +56,12 @@ public class DevicesListCommand extends AbstractShellCommand {
* @param device infrastructure device
*/
protected void printDevice(DeviceService service, Device device) {
print(FMT, device.id(), service.isAvailable(device.id()),
service.getRole(device.id()), device.type(),
device.manufacturer(), device.hwVersion(), device.swVersion(),
device.serialNumber());
if (device != null) {
print(FMT, device.id(), service.isAvailable(device.id()),
service.getRole(device.id()), device.type(),
device.manufacturer(), device.hwVersion(), device.swVersion(),
device.serialNumber());
}
}
}
......
......@@ -16,6 +16,7 @@ import static org.onlab.onos.net.DeviceId.deviceId;
public class LinksListCommand extends AbstractShellCommand {
private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s";
private static final String COMPACT = "%s/%s-%s/%s";
@Argument(index = 0, name = "uri", description = "Device ID",
required = false, multiValued = false)
......@@ -43,4 +44,16 @@ public class LinksListCommand extends AbstractShellCommand {
link.dst().deviceId(), link.dst().port(), link.type());
}
/**
* Returns a compact string representing the given link.
*
* @param link infrastructure link
* @return formatted link string
*/
public static String compactLinkString(Link link) {
return String.format(COMPACT, link.src().deviceId(), link.src().port(),
link.dst().deviceId(), link.dst().port());
}
}
......
......@@ -2,10 +2,12 @@ package org.onlab.onos.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.Path;
import java.util.Set;
import static org.onlab.onos.cli.net.LinksListCommand.compactLinkString;
import static org.onlab.onos.net.DeviceId.deviceId;
/**
......@@ -16,13 +18,13 @@ import static org.onlab.onos.net.DeviceId.deviceId;
description = "Lists all shortest-paths paths between the specified source and destination devices")
public class PathListCommand extends TopologyCommand {
private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s";
private static final String SEP = "==>";
@Argument(index = 0, name = "src", description = "Source device ID",
required = true, multiValued = false)
String src = null;
@Argument(index = 0, name = "dst", description = "Destination device ID",
@Argument(index = 1, name = "dst", description = "Destination device ID",
required = true, multiValued = false)
String dst = null;
......@@ -36,8 +38,20 @@ public class PathListCommand extends TopologyCommand {
return null;
}
private String pathString(Path path) {
return path.toString();
/**
* Produces a formatted string representing the specified path.
*
* @param path network path
* @return formatted path string
*/
protected String pathString(Path path) {
StringBuilder sb = new StringBuilder();
for (Link link : path.links()) {
sb.append(compactLinkString(link)).append(SEP);
}
sb.delete(sb.lastIndexOf(SEP), sb.length());
sb.append("; cost=").append(path.cost());
return sb.toString();
}
}
......
......@@ -35,6 +35,12 @@
<action class="org.onlab.onos.cli.net.TopologyCommand"/>
</command>
<command>
<action class="org.onlab.onos.cli.net.PathListCommand"/>
<completers>
<ref component-id="deviceIdCompleter"/>
</completers>
</command>
<command>
<action class="org.onlab.onos.cli.net.ClustersListCommand"/>
</command>
<command>
......