Merge remote-tracking branch 'origin/master'
diff --git a/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java b/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java
new file mode 100644
index 0000000..5d1f34d
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/cluster/MastershipService.java
@@ -0,0 +1,19 @@
+package org.onlab.onos.cluster;
+
+/**
+ * Service responsible for determining the controller instance mastership of
+ * a device in a clustered environment. This is the central authority for
+ * determining mastership, but is not responsible for actually applying it
+ * to the devices; this falls on the device service.
+ */
+public interface MastershipService {
+
+    // InstanceId getMasterFor(DeviceId deviceId)
+    // Set<DeviceId> getDevicesOf(InstanceId instanceId);
+
+    // MastershipRole requestRoleFor(DeviceId deviceId);
+
+    // addListener/removeLister(MastershipListener listener);
+        // types of events would be MASTER_CHANGED (subject ==> deviceId; master ==> instanceId)
+
+}
diff --git a/core/api/src/test/java/org/onlab/onos/net/NetTestTools.java b/core/api/src/test/java/org/onlab/onos/net/NetTestTools.java
new file mode 100644
index 0000000..ce64e46
--- /dev/null
+++ b/core/api/src/test/java/org/onlab/onos/net/NetTestTools.java
@@ -0,0 +1,66 @@
+package org.onlab.onos.net;
+
+import org.onlab.onos.net.provider.ProviderId;
+import org.onlab.packet.IpPrefix;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+import static org.onlab.onos.net.DeviceId.deviceId;
+import static org.onlab.onos.net.HostId.hostId;
+import static org.onlab.onos.net.PortNumber.portNumber;
+import static org.onlab.packet.MacAddress.valueOf;
+import static org.onlab.packet.VlanId.vlanId;
+
+/**
+ * Miscellaneous tools for testing core related to the network model.
+ */
+public final class NetTestTools {
+
+    private NetTestTools() {
+    }
+
+    public static final ProviderId PID = new ProviderId("of", "foo");
+
+    // Short-hand for producing a device id from a string
+    public static DeviceId did(String id) {
+        return deviceId("of:" + id);
+    }
+
+
+    // Short-hand for producing a host id from a string
+    public static HostId hid(String id) {
+        return hostId("nic:" + id);
+    }
+
+    // Crates a new device with the specified id
+    public static Device device(String id) {
+        return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
+                                 "mfg", "1.0", "1.1", "1234");
+    }
+
+    // Crates a new host with the specified id
+    public static Host host(String id, String did) {
+        return new DefaultHost(PID, hid(id), valueOf(1234), vlanId((short) 2),
+                               new HostLocation(did(did), portNumber(1), 321),
+                               new HashSet<IpPrefix>());
+    }
+
+    // Short-hand for creating a link.
+    public static Link link(String src, int sp, String dst, int dp) {
+        return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)),
+                               new ConnectPoint(did(dst), portNumber(dp)),
+                               Link.Type.DIRECT);
+    }
+
+    // Creates a path that leads through the given devices.
+    public static Path createPath(String... ids) {
+        List<Link> links = new ArrayList<>();
+        for (int i = 0; i < ids.length - 1; i++) {
+            links.add(link(ids[i], i, ids[i + 1], i));
+        }
+        return new DefaultPath(PID, links, ids.length);
+    }
+
+}
diff --git a/core/net/pom.xml b/core/net/pom.xml
index 1e233ca..b252636 100644
--- a/core/net/pom.xml
+++ b/core/net/pom.xml
@@ -24,24 +24,31 @@
 
         <dependency>
             <groupId>org.onlab.onos</groupId>
+            <artifactId>onos-api</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onlab.onos</groupId>
             <artifactId>onos-core-trivial</artifactId>
             <version>${project.version}</version>
             <scope>test</scope>
         </dependency>
 
+        <!-- TODO Consider removing store dependency.
+              Currently required for DistributedDeviceManagerTest. -->
+        <dependency>
+            <groupId>org.onlab.onos</groupId>
+            <artifactId>onos-core-store</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.scr.annotations</artifactId>
         </dependency>
-
-        <!-- TODO Consider removing store dependency.
-              Currently required for DistributedDeviceManagerTest. -->
-        <dependency>
-          <groupId>org.onlab.onos</groupId>
-          <artifactId>onos-core-store</artifactId>
-            <version>${project.version}</version>
-          <scope>test</scope>
-        </dependency>
     </dependencies>
 
     <build>
diff --git a/core/net/src/main/java/org/onlab/onos/net/topology/impl/PathManager.java b/core/net/src/main/java/org/onlab/onos/net/topology/impl/PathManager.java
index 47cb376..86be9a5 100644
--- a/core/net/src/main/java/org/onlab/onos/net/topology/impl/PathManager.java
+++ b/core/net/src/main/java/org/onlab/onos/net/topology/impl/PathManager.java
@@ -1,5 +1,6 @@
 package org.onlab.onos.net.topology.impl;
 
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.apache.felix.scr.annotations.Activate;
@@ -59,12 +60,12 @@
     protected HostService hostService;
 
     @Activate
-    public void setUp() {
+    public void activate() {
         log.info("Started");
     }
 
     @Deactivate
-    public void tearDown() {
+    public void deactivate() {
         log.info("Stopped");
     }
 
@@ -82,6 +83,11 @@
         EdgeLink srcEdge = getEdgeLink(src, true);
         EdgeLink dstEdge = getEdgeLink(dst, false);
 
+        // If either edge is null, bail with no paths.
+        if (srcEdge == null || dstEdge == null) {
+            return ImmutableSet.of();
+        }
+
         DeviceId srcDevice = srcEdge != NOT_HOST ? srcEdge.dst().deviceId() : (DeviceId) src;
         DeviceId dstDevice = dstEdge != NOT_HOST ? dstEdge.src().deviceId() : (DeviceId) dst;
 
@@ -117,28 +123,14 @@
         return NOT_HOST;
     }
 
-    // Produces a set of direct edge-to-edge paths.
+    // Produces a set of edge-to-edge paths using the set of infrastructure
+    // paths and the given edge links.
     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));
-        }
+        endToEndPaths.add(edgeToEdgePath(srcLink, dstLink, null));
         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);
-    }
-
     // Produces a set of edge-to-edge paths using the set of infrastructure
     // paths and the given edge links.
     private Set<Path> edgeToEdgePaths(EdgeLink srcLink, EdgeLink dstLink, Set<Path> paths) {
@@ -149,14 +141,21 @@
         return endToEndPaths;
     }
 
-    // Produces an edge-to-edge path using the specified infrastructure path
-    // and edge links.
+    // Produces a direct edge-to-edge path.
     private Path edgeToEdgePath(EdgeLink srcLink, EdgeLink dstLink, Path path) {
-        List<Link> links = Lists.newArrayListWithCapacity(path.links().size() + 2);
-        links.add(srcLink);
-        links.addAll(path.links());
-        links.add(dstLink);
-        return new DefaultPath(path.providerId(), links, path.cost() + 2);
+        List<Link> links = Lists.newArrayListWithCapacity(2);
+        // Add source and destination edge links only if they are real and
+        // add the infrastructure path only if it is not null.
+        if (srcLink != NOT_HOST) {
+            links.add(srcLink);
+        }
+        if (path != null) {
+            links.addAll(path.links());
+        }
+        if (dstLink != NOT_HOST) {
+            links.add(dstLink);
+        }
+        return new DefaultPath(PID, links, 2);
     }
 
     // Special value for edge link to represent that this is really not an
diff --git a/core/net/src/test/java/org/onlab/onos/net/topology/impl/DefaultTopologyProviderTest.java b/core/net/src/test/java/org/onlab/onos/net/topology/impl/DefaultTopologyProviderTest.java
index 0055d82..c9fd96d 100644
--- a/core/net/src/test/java/org/onlab/onos/net/topology/impl/DefaultTopologyProviderTest.java
+++ b/core/net/src/test/java/org/onlab/onos/net/topology/impl/DefaultTopologyProviderTest.java
@@ -25,6 +25,8 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.onlab.junit.TestTools.assertAfter;
+import static org.onlab.onos.net.NetTestTools.device;
+import static org.onlab.onos.net.NetTestTools.link;
 import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED;
 import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED;
 
@@ -79,8 +81,8 @@
             @Override
             public void run() {
                 validateSubmission();
-                deviceService.post(new DeviceEvent(DEVICE_ADDED, TopologyManagerTest.device("z"), null));
-                linkService.post(new LinkEvent(LINK_ADDED, TopologyManagerTest.link("z", 1, "a", 4)));
+                deviceService.post(new DeviceEvent(DEVICE_ADDED, device("z"), null));
+                linkService.post(new LinkEvent(LINK_ADDED, link("z", 1, "a", 4)));
                 validateSubmission();
             }
         });
@@ -128,9 +130,9 @@
 
         @Override
         public Iterable<Device> getDevices() {
-            return ImmutableSet.of(TopologyManagerTest.device("a"), TopologyManagerTest.device("b"),
-                                   TopologyManagerTest.device("c"), TopologyManagerTest.device("d"),
-                                   TopologyManagerTest.device("e"), TopologyManagerTest.device("f"));
+            return ImmutableSet.of(device("a"), device("b"),
+                                   device("c"), device("d"),
+                                   device("e"), device("f"));
         }
 
         void post(DeviceEvent event) {
@@ -146,11 +148,11 @@
 
         @Override
         public Iterable<Link> getLinks() {
-            return ImmutableSet.of(TopologyManagerTest.link("a", 1, "b", 1), TopologyManagerTest.link("b", 1, "a", 1),
-                                   TopologyManagerTest.link("b", 2, "c", 1), TopologyManagerTest.link("c", 1, "b", 2),
-                                   TopologyManagerTest.link("c", 2, "d", 1), TopologyManagerTest.link("d", 1, "c", 2),
-                                   TopologyManagerTest.link("d", 2, "a", 2), TopologyManagerTest.link("a", 2, "d", 2),
-                                   TopologyManagerTest.link("e", 1, "f", 1), TopologyManagerTest.link("f", 1, "e", 1));
+            return ImmutableSet.of(link("a", 1, "b", 1), link("b", 1, "a", 1),
+                                   link("b", 2, "c", 1), link("c", 1, "b", 2),
+                                   link("c", 2, "d", 1), link("d", 1, "c", 2),
+                                   link("d", 2, "a", 2), link("a", 2, "d", 2),
+                                   link("e", 1, "f", 1), link("f", 1, "e", 1));
         }
 
         void post(LinkEvent event) {
diff --git a/core/net/src/test/java/org/onlab/onos/net/topology/impl/PathManagerTest.java b/core/net/src/test/java/org/onlab/onos/net/topology/impl/PathManagerTest.java
new file mode 100644
index 0000000..2ecf7f5
--- /dev/null
+++ b/core/net/src/test/java/org/onlab/onos/net/topology/impl/PathManagerTest.java
@@ -0,0 +1,148 @@
+package org.onlab.onos.net.topology.impl;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.onos.net.DeviceId;
+import org.onlab.onos.net.ElementId;
+import org.onlab.onos.net.Host;
+import org.onlab.onos.net.HostId;
+import org.onlab.onos.net.Path;
+import org.onlab.onos.net.host.HostService;
+import org.onlab.onos.net.host.HostServiceAdapter;
+import org.onlab.onos.net.provider.ProviderId;
+import org.onlab.onos.net.topology.LinkWeight;
+import org.onlab.onos.net.topology.PathService;
+import org.onlab.onos.net.topology.Topology;
+import org.onlab.onos.net.topology.TopologyService;
+import org.onlab.onos.net.topology.TopologyServiceAdapter;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.onos.net.NetTestTools.*;
+
+/**
+ * Test of the path selection subsystem.
+ */
+public class PathManagerTest {
+
+    private static final ProviderId PID = new ProviderId("of", "foo");
+
+    private PathManager mgr;
+    private PathService service;
+
+    private FakeTopoMgr fakeTopoMgr = new FakeTopoMgr();
+    private FakeHostMgr fakeHostMgr = new FakeHostMgr();
+
+    @Before
+    public void setUp() {
+        mgr = new PathManager();
+        service = mgr;
+        mgr.topologyService = fakeTopoMgr;
+        mgr.hostService = fakeHostMgr;
+        mgr.activate();
+    }
+
+    @After
+    public void tearDown() {
+        mgr.deactivate();
+    }
+
+    @Test
+    public void infraToInfra() {
+        DeviceId src = did("src");
+        DeviceId dst = did("dst");
+        fakeTopoMgr.paths.add(createPath("src", "middle", "dst"));
+        Set<Path> paths = service.getPaths(src, dst);
+        validatePaths(paths, 1, 2, src, dst);
+    }
+
+    @Test
+    public void infraToEdge() {
+        DeviceId src = did("src");
+        HostId dst = hid("dst");
+        fakeTopoMgr.paths.add(createPath("src", "middle", "edge"));
+        fakeHostMgr.hosts.put(dst, host("dst", "edge"));
+        Set<Path> paths = service.getPaths(src, dst);
+        validatePaths(paths, 1, 3, src, dst);
+    }
+
+    @Test
+    public void edgeToInfra() {
+        HostId src = hid("src");
+        DeviceId dst = did("dst");
+        fakeTopoMgr.paths.add(createPath("edge", "middle", "dst"));
+        fakeHostMgr.hosts.put(src, host("src", "edge"));
+        Set<Path> paths = service.getPaths(src, dst);
+        validatePaths(paths, 1, 3, src, dst);
+    }
+
+    @Test
+    public void edgeToEdge() {
+        HostId src = hid("src");
+        HostId dst = hid("dst");
+        fakeTopoMgr.paths.add(createPath("srcEdge", "middle", "dstEdge"));
+        fakeHostMgr.hosts.put(src, host("src", "srcEdge"));
+        fakeHostMgr.hosts.put(dst, host("dst", "dstEdge"));
+        Set<Path> paths = service.getPaths(src, dst);
+        validatePaths(paths, 1, 4, src, dst);
+    }
+
+    @Test
+    public void edgeToEdgeDirect() {
+        HostId src = hid("src");
+        HostId dst = hid("dst");
+        fakeHostMgr.hosts.put(src, host("src", "edge"));
+        fakeHostMgr.hosts.put(dst, host("dst", "edge"));
+        Set<Path> paths = service.getPaths(src, dst);
+        validatePaths(paths, 1, 2, src, dst);
+    }
+
+    @Test
+    public void noEdge() {
+        Set<Path> paths = service.getPaths(hid("src"), hid("dst"));
+        assertTrue("there should be no paths", paths.isEmpty());
+    }
+
+    // Makes sure the set of paths meets basic expectations.
+    private void validatePaths(Set<Path> paths, int count, int length,
+                               ElementId src, ElementId dst) {
+        assertEquals("incorrect path count", count, paths.size());
+        for (Path path : paths) {
+            assertEquals("incorrect length", length, path.links().size());
+            assertEquals("incorrect source", src, path.src().elementId());
+            assertEquals("incorrect destination", dst, path.dst().elementId());
+        }
+    }
+
+    // Fake entity to give out paths.
+    private class FakeTopoMgr extends TopologyServiceAdapter implements TopologyService {
+        Set<Path> paths = new HashSet<>();
+
+        @Override
+        public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) {
+            return paths;
+        }
+
+        @Override
+        public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
+            return paths;
+        }
+    }
+
+    // Fake entity to give out hosts.
+    private class FakeHostMgr extends HostServiceAdapter implements HostService {
+        private Map<HostId, Host> hosts = new HashMap<>();
+
+        @Override
+        public Host getHost(HostId hostId) {
+            return hosts.get(hostId);
+        }
+    }
+
+}
diff --git a/core/net/src/test/java/org/onlab/onos/net/topology/impl/TopologyManagerTest.java b/core/net/src/test/java/org/onlab/onos/net/topology/impl/TopologyManagerTest.java
index 2295370..1a19f96 100644
--- a/core/net/src/test/java/org/onlab/onos/net/topology/impl/TopologyManagerTest.java
+++ b/core/net/src/test/java/org/onlab/onos/net/topology/impl/TopologyManagerTest.java
@@ -6,10 +6,7 @@
 import org.onlab.onos.event.Event;
 import org.onlab.onos.event.impl.TestEventDispatcher;
 import org.onlab.onos.net.ConnectPoint;
-import org.onlab.onos.net.DefaultDevice;
-import org.onlab.onos.net.DefaultLink;
 import org.onlab.onos.net.Device;
-import org.onlab.onos.net.DeviceId;
 import org.onlab.onos.net.Link;
 import org.onlab.onos.net.Path;
 import org.onlab.onos.net.provider.AbstractProvider;
@@ -35,7 +32,7 @@
 
 import static com.google.common.collect.ImmutableSet.of;
 import static org.junit.Assert.*;
-import static org.onlab.onos.net.DeviceId.deviceId;
+import static org.onlab.onos.net.NetTestTools.*;
 import static org.onlab.onos.net.PortNumber.portNumber;
 import static org.onlab.onos.net.topology.ClusterId.clusterId;
 import static org.onlab.onos.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
@@ -184,24 +181,6 @@
         assertEquals("wrong path cost", 6.6, path.cost(), 0.01);
     }
 
-    // Short-hand for creating a link.
-    public static Link link(String src, int sp, String dst, int dp) {
-        return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)),
-                               new ConnectPoint(did(dst), portNumber(dp)),
-                               Link.Type.DIRECT);
-    }
-
-    // Crates a new device with the specified id
-    public static Device device(String id) {
-        return new DefaultDevice(PID, did(id), Device.Type.SWITCH,
-                                 "mfg", "1.0", "1.1", "1234");
-    }
-
-    // Short-hand for producing a device id from a string
-    public static DeviceId did(String id) {
-        return deviceId("of:" + id);
-    }
-
     protected void validateEvents(Enum... types) {
         int i = 0;
         assertEquals("wrong events received", types.length, listener.events.size());
diff --git a/features/features.xml b/features/features.xml
index c7fddb7..5ad9f95 100644
--- a/features/features.xml
+++ b/features/features.xml
@@ -8,6 +8,8 @@
         <bundle>mvn:commons-lang/commons-lang/2.6</bundle>
         <bundle>mvn:com.google.guava/guava/18.0</bundle>
         <bundle>mvn:io.netty/netty/3.9.2.Final</bundle>
+        <bundle>mvn:com.esotericsoftware.kryo/kryo/2.24.0</bundle>
+        <bundle>mvn:com.esotericsoftware.minlog/minlog/1.3</bundle>
     </feature>
 
     <feature name="onos-thirdparty-web" version="1.0.0"