tom

Enhanced topo viewer sample GUI to allow path selection from any node (host or d…

…evice). Fixed path service implementaiton.
......@@ -11,6 +11,7 @@ import org.onlab.onos.net.Path;
import org.onlab.onos.net.device.DeviceService;
import org.onlab.onos.net.host.HostService;
import org.onlab.onos.net.link.LinkService;
import org.onlab.onos.net.path.PathService;
import org.onlab.onos.net.topology.Topology;
import org.onlab.onos.net.topology.TopologyGraph;
import org.onlab.onos.net.topology.TopologyService;
......@@ -28,6 +29,7 @@ import java.util.Map;
import java.util.Set;
import static org.onlab.onos.net.DeviceId.deviceId;
import static org.onlab.onos.net.HostId.hostId;
import static org.onlab.onos.net.PortNumber.portNumber;
/**
......@@ -99,12 +101,11 @@ public class TopologyResource extends BaseResource {
@Produces("application/json")
public Response paths(@PathParam("src") String src, @PathParam("dst") String dst) {
ObjectMapper mapper = new ObjectMapper();
TopologyService topologyService = get(TopologyService.class);
Topology topology = topologyService.currentTopology();
PathService pathService = get(PathService.class);
Set<Path> paths = pathService.getPaths(elementId(src), elementId(dst));
ArrayNode pathsNode = mapper.createArrayNode();
for (Path path : topologyService.getPaths(topology, deviceId(src), deviceId(dst))) {
for (Path path : paths) {
pathsNode.add(json(mapper, path));
}
......@@ -114,6 +115,11 @@ public class TopologyResource extends BaseResource {
return Response.ok(rootNode.toString()).build();
}
// Creates either device ID or host ID as appropriate.
private ElementId elementId(String id) {
return id.startsWith("nic:") ? hostId(id) : deviceId(id);
}
// Scan all links and counts number of them between the same devices
// using a normalized link key.
private Map<String, AggLink> aggregateLinks() {
......
......@@ -23,8 +23,8 @@ public class DefaultEdgeLink extends DefaultLink implements EdgeLink {
*/
public DefaultEdgeLink(ProviderId providerId, ConnectPoint hostPoint,
HostLocation hostLocation, boolean isIngress) {
super(providerId, isIngress ? hostLocation : hostPoint,
isIngress ? hostPoint : hostLocation, Type.EDGE);
super(providerId, isIngress ? hostPoint : hostLocation,
isIngress ? hostLocation : hostPoint, Type.EDGE);
checkArgument(hostPoint.elementId() instanceof HostId,
"Host point does not refer to a host ID");
this.hostId = (HostId) hostPoint.elementId();
......
......@@ -44,7 +44,7 @@ public final class HostId extends ElementId {
*/
public static HostId hostId(MACAddress mac, VLANID vlanId) {
// FIXME: use more efficient means of encoding
return hostId("nic" + ":" + mac + "/" + vlanId);
return hostId("nic" + ":" + mac + "-" + vlanId);
}
/**
......
package org.onlab.onos.net.path;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.HostId;
import org.onlab.onos.net.ElementId;
import org.onlab.onos.net.Path;
import org.onlab.onos.net.topology.LinkWeight;
......@@ -15,44 +14,23 @@ public interface PathService {
/**
* Returns the set of all shortest paths, precomputed in terms of hop-count,
* between the specified source and destination devices.
* between the specified source and destination elements.
*
* @param src source device
* @param dst destination device
* @return set of all shortest paths between the two devices
* @param src source element
* @param dst destination element
* @return set of all shortest paths between the two elements
*/
Set<Path> getPaths(DeviceId src, DeviceId dst);
Set<Path> getPaths(ElementId src, ElementId dst);
/**
* Returns the set of all shortest paths, computed using the supplied
* edge-weight entity, between the specified source and destination devices.
* edge-weight entity, between the specified source and destination
* network elements.
*
* @param src source device
* @param dst destination device
* @return set of all shortest paths between the two devices
* @param src source element
* @param dst destination element
* @return set of all shortest paths between the two element
*/
Set<Path> getPaths(DeviceId src, DeviceId dst,
LinkWeight weight);
/**
* Returns the set of all shortest paths, precomputed in terms of hop-count,
* between the specified source and destination end-stations.
*
* @param src source device
* @param dst destination device
* @return set of all shortest paths between the two end-stations hosts
*/
Set<Path> getPaths(HostId src, HostId dst);
/**
* Returns the set of all shortest paths, computed using the supplied
* edge-weight entity, between the specified source and end-stations.
*
* @param src source host
* @param dst destination host
* @return set of all shortest paths between the two end-station hosts
*/
Set<Path> getPaths(HostId src, HostId dst, LinkWeight weight);
Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight);
}
......
......@@ -47,8 +47,8 @@ public class DefaultEdgeLinkTest {
public void basics() {
HostLocation hostLocation = new HostLocation(DID1, P1, 123L);
EdgeLink link = new DefaultEdgeLink(PID, cp(HID1, P0), hostLocation, false);
assertEquals("incorrect src", cp(HID1, P0), link.src());
assertEquals("incorrect dst", hostLocation, link.dst());
assertEquals("incorrect src", cp(HID1, P0), link.dst());
assertEquals("incorrect dst", hostLocation, link.src());
assertEquals("incorrect type", Link.Type.EDGE, link.type());
assertEquals("incorrect hostId", HID1, link.hostId());
assertEquals("incorrect connect point", hostLocation, link.hostLocation());
......
......@@ -22,7 +22,7 @@ public class HostIdTest extends ElementIdTest {
@Test
public void basics() {
new EqualsTester()
.addEqualityGroup(hostId("nic:00:11:00:00:00:01/11"),
.addEqualityGroup(hostId("nic:00:11:00:00:00:01-11"),
hostId(MAC1, VLAN1))
.addEqualityGroup(hostId(MAC2, VLAN2))
.testEquals();
......
......@@ -13,8 +13,10 @@ import org.onlab.onos.net.DefaultEdgeLink;
import org.onlab.onos.net.DefaultPath;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.EdgeLink;
import org.onlab.onos.net.ElementId;
import org.onlab.onos.net.Host;
import org.onlab.onos.net.HostId;
import org.onlab.onos.net.HostLocation;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.Path;
import org.onlab.onos.net.PortNumber;
......@@ -30,6 +32,7 @@ import java.util.List;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onlab.onos.net.DeviceId.deviceId;
import static org.slf4j.LoggerFactory.getLogger;
/**
......@@ -38,14 +41,15 @@ import static org.slf4j.LoggerFactory.getLogger;
*/
@Component(immediate = true)
@Service
public class PathManager implements PathService {
public class SimplePathManager implements PathService {
private static final String DEVICE_ID_NULL = "Device ID cannot be null";
private static final String HOST_ID_NULL = "Host ID cannot be null";
private static final String ELEMENT_ID_NULL = "Element ID cannot be null";
private static final ProviderId PID = new ProviderId("org.onlab.onos.core");
private static final PortNumber P0 = PortNumber.portNumber(0);
private static final EdgeLink NOT_HOST = new NotHost();
private final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......@@ -65,52 +69,25 @@ public class PathManager implements PathService {
}
@Override
public Set<Path> getPaths(DeviceId src, DeviceId dst) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
Topology topology = topologyService.currentTopology();
return topologyService.getPaths(topology, src, dst);
}
@Override
public Set<Path> getPaths(DeviceId src, DeviceId dst, LinkWeight weight) {
checkNotNull(src, DEVICE_ID_NULL);
checkNotNull(dst, DEVICE_ID_NULL);
Topology topology = topologyService.currentTopology();
return topologyService.getPaths(topology, src, dst, weight);
}
@Override
public Set<Path> getPaths(HostId src, HostId dst) {
public Set<Path> getPaths(ElementId src, ElementId dst) {
return getPaths(src, dst, null);
}
@Override
public Set<Path> getPaths(HostId src, HostId dst, LinkWeight weight) {
checkNotNull(src, HOST_ID_NULL);
checkNotNull(dst, HOST_ID_NULL);
// Resolve the source host, bail if unable.
Host srcHost = hostService.getHost(src);
if (srcHost == null) {
return Sets.newHashSet();
}
// Resolve the destination host, bail if unable.
Host dstHost = hostService.getHost(dst);
if (dstHost == null) {
return Sets.newHashSet();
}
public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
checkNotNull(src, ELEMENT_ID_NULL);
checkNotNull(dst, ELEMENT_ID_NULL);
// Get the source and destination edge locations
EdgeLink srcEdge = new DefaultEdgeLink(PID, new ConnectPoint(src, P0),
srcHost.location(), true);
EdgeLink dstEdge = new DefaultEdgeLink(PID, new ConnectPoint(dst, P0),
dstHost.location(), false);
EdgeLink srcEdge = getEdgeLink(src, true);
EdgeLink dstEdge = getEdgeLink(dst, false);
DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
// If the source and destination are on the same edge device, there
// is just one path, so build it and return it.
if (srcEdge.dst().deviceId().equals(dstEdge.src().deviceId())) {
if (srcDevice.equals(dstDevice)) {
return edgeToEdgePaths(srcEdge, dstEdge);
}
......@@ -118,26 +95,47 @@ public class PathManager implements PathService {
// devices.
Topology topology = topologyService.currentTopology();
Set<Path> paths = weight == null ?
topologyService.getPaths(topology, srcEdge.dst().deviceId(),
dstEdge.src().deviceId()) :
topologyService.getPaths(topology, srcEdge.dst().deviceId(),
dstEdge.src().deviceId(), weight);
topologyService.getPaths(topology, srcDevice, dstDevice) :
topologyService.getPaths(topology, srcDevice, dstDevice, weight);
return edgeToEdgePaths(srcEdge, dstEdge, paths);
}
// Finds the host edge link if the element ID is a host id of an existing
// host. Otherwise, if the host does not exist, it returns null and if
// the element ID is not a host ID, returns NOT_HOST edge link.
private EdgeLink getEdgeLink(ElementId elementId, boolean isIngress) {
if (elementId instanceof HostId) {
// Resolve the host, return null.
Host host = hostService.getHost((HostId) elementId);
if (host == null) {
return null;
}
return new DefaultEdgeLink(PID, new ConnectPoint(elementId, P0),
host.location(), isIngress);
}
return NOT_HOST;
}
// Produces a set of direct edge-to-edge paths.
private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink) {
Set<Path> endToEndPaths = Sets.newHashSetWithExpectedSize(1);
if (srcLink != NOT_HOST || dstLink != NOT_HOST) {
endToEndPaths.add(edgeToEdgePath(srcLink, dstLink));
}
return endToEndPaths;
}
// Produces a direct edge-to-edge path.
private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink) {
List<Link> links = Lists.newArrayListWithCapacity(2);
// Add source and destination edge links only if they are real.
if (srcLink != NOT_HOST) {
links.add(srcLink);
}
if (dstLink != NOT_HOST) {
links.add(dstLink);
}
return new DefaultPath(PID, links, 2);
}
......@@ -161,4 +159,12 @@ public class PathManager implements PathService {
return new DefaultPath(path.providerId(), links, path.cost() + 2);
}
// Special value for edge link to represent that this is really not an
// edge link since the src or dst are really an infrastructure device.
private static class NotHost extends DefaultEdgeLink implements EdgeLink {
NotHost() {
super(PID, new ConnectPoint(HostId.hostId("nic:none"), P0),
new HostLocation(deviceId("none:none"), P0, 0L), false);
}
}
}
......