Cleaning up and enhancing driver subsystem and the flow objective subsystem.

Change-Id: Ica600ef1aaa46d19e764cd7a197454a4e0f85a08
diff --git a/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java b/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
index 8b55fa3..4e9c6e4 100644
--- a/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
+++ b/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
@@ -50,6 +50,11 @@
     public static final String PROTOCOL = "protocol";
 
     /**
+     * Annotation key for the device driver name.
+     */
+    public static final String DRIVER = "driver";
+
+    /**
      * Annotation key for durable links.
      */
     public static final String DURABLE = "durable";
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java b/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java
index c4700fc..eda131a 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/Pipeliner.java
@@ -15,7 +15,6 @@
  */
 package org.onosproject.net.behaviour;
 
-import org.onlab.osgi.ServiceDirectory;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.driver.HandlerBehaviour;
 import org.onosproject.net.flowobjective.FilteringObjective;
@@ -31,20 +30,20 @@
 public interface Pipeliner extends HandlerBehaviour {
 
     /**
-     * Injecting the service directory into the driver.
+     * Initializes the driver with context required for its operation.
      *
      * @param deviceId the deviceId
-     * @param serviceDirectory the service directory.
+     * @param context  processing context
      */
-    void init(DeviceId deviceId, ServiceDirectory serviceDirectory);
+    void init(DeviceId deviceId, PipelinerContext context);
 
     /**
      * Installs the filtering rules onto the device.
      *
-     * @param filteringObjectives the collection of filters
+     * @param filterObjectives the collection of filters
      * @return a future indicating the success of the operation
      */
-     Future<Boolean> filter(Collection<FilteringObjective> filteringObjectives);
+    Future<Boolean> filter(Collection<FilteringObjective> filterObjectives);
 
     /**
      * Installs the forwarding rules onto the device.
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/PipelinerContext.java b/core/api/src/main/java/org/onosproject/net/behaviour/PipelinerContext.java
new file mode 100644
index 0000000..c2c6dfd
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/PipelinerContext.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.behaviour;
+
+import org.onlab.osgi.ServiceDirectory;
+
+/**
+ * Processing context and supporting services for the pipeline behaviour.
+ */
+public interface PipelinerContext {
+
+    /**
+     * Returns the service directory which can be used to obtain references
+     * to various supporting services.
+     *
+     * @return service directory
+     */
+    ServiceDirectory directory();
+
+    // TODO: add means to store and access shared state
+}
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverData.java b/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverData.java
index e83afe8..0d9ad9a 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverData.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverData.java
@@ -29,27 +29,27 @@
  */
 public class DefaultDriverData implements DriverData {
 
-    private final Driver type;
+    private final Driver driver;
     private final Map<String, String> properties;
 
     /**
      * Creates new driver data.
      *
-     * @param type parent driver type
+     * @param driver parent driver type
      */
-    public DefaultDriverData(Driver type) {
-        this.type = type;
+    public DefaultDriverData(Driver driver) {
+        this.driver = driver;
         this.properties = new HashMap<>();
     }
 
     @Override
-    public Driver type() {
-        return type;
+    public Driver driver() {
+        return driver;
     }
 
     @Override
     public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
-        return type.createBehaviour(this, behaviourClass, false);
+        return driver.createBehaviour(this, behaviourClass, false);
     }
 
     @Override
@@ -83,7 +83,7 @@
     @Override
     public String toString() {
         return toStringHelper(this)
-                .add("type", type)
+                .add("type", driver)
                 .add("properties", properties)
                 .toString();
     }
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverHandler.java b/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverHandler.java
index 247ccec..38ee3d5 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverHandler.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DefaultDriverHandler.java
@@ -34,13 +34,18 @@
     }
 
     @Override
+    public Driver driver() {
+        return data.driver();
+    }
+
+    @Override
     public DriverData data() {
         return data;
     }
 
     @Override
     public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
-        return data.type().createBehaviour(this.data, behaviourClass, true);
+        return data.driver().createBehaviour(this.data, behaviourClass, true);
     }
 
     @Override
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverData.java b/core/api/src/main/java/org/onosproject/net/driver/DriverData.java
index ff4d622..4cf7093 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DriverData.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverData.java
@@ -28,7 +28,7 @@
      *
      * @return device driver
      */
-    Driver type();
+    Driver driver();
 
     /**
      * Returns the specified facet of behaviour to access the device data.
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverHandler.java b/core/api/src/main/java/org/onosproject/net/driver/DriverHandler.java
index 0fb46aa..6e8a354 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DriverHandler.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverHandler.java
@@ -21,6 +21,13 @@
 public interface DriverHandler {
 
     /**
+     * Returns the parent device driver.
+     *
+     * @return device driver
+     */
+    Driver driver();
+
+    /**
      * Returns the device driver data.
      *
      * @return device driver data
diff --git a/core/api/src/main/java/org/onosproject/net/driver/DriverService.java b/core/api/src/main/java/org/onosproject/net/driver/DriverService.java
index 5d6d464..ffb7c93 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/DriverService.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/DriverService.java
@@ -38,6 +38,8 @@
      *
      * @param driverName driver name
      * @return driver
+     * @throws org.onlab.util.ItemNotFoundException if driver with the given
+     *                                              name is not found
      */
     Driver getDriver(String driverName);
 
@@ -53,25 +55,33 @@
     Driver getDriver(String mfr, String hw, String sw);
 
     /**
-     * Creates a new driver handler for the specified driver.
+     * Returns the driver for the specified device. If the device carries
+     * {@code driver} annotation, its value is used to look-up the driver.
+     * Otherwise, the device manufacturer, hardware and software version
+     * attributes are used to look-up the driver. First using their literal
+     * values and if no driver is found, using ERE matching against the
+     * driver manufacturer, hardware and software version fields.
      *
-     * @param driverName  driver name
-     * @param deviceId    device identifier
-     * @param credentials optional login credentials in string form
-     * @return driver handler
+     * @param deviceId device identifier
+     * @return driver or null of no matching one is found
+     * @throws org.onlab.util.ItemNotFoundException if device or driver for it
+     *                                              are not found
      */
-    DriverHandler createHandler(String driverName, DeviceId deviceId,
-                                String... credentials);
+    Driver getDriver(DeviceId deviceId);
 
     /**
-     * Creates a new driver handler for the specified driver data.
+     * Creates a new driver handler for interacting with the specified device.
+     * The driver is looked-up using the same semantics as
+     * {@link #getDriver(DeviceId)} method.
      *
-     * @param data        driver data
      * @param deviceId    device identifier
      * @param credentials optional login credentials in string form
      * @return driver handler
+     * @throws org.onlab.util.ItemNotFoundException if device or driver for it
+     *                                              are not found
      */
-    DriverHandler createHandler(DriverData data, DeviceId deviceId,
-                                String... credentials);
+    DriverHandler createHandler(DeviceId deviceId, String... credentials);
+
+    // TODO: Devise a mechanism for retaining DriverData for devices
 
 }
diff --git a/core/api/src/main/java/org/onosproject/net/flowobjective/FlowObjectiveService.java b/core/api/src/main/java/org/onosproject/net/flowobjective/FlowObjectiveService.java
index dc5e5e1..14af2b8 100644
--- a/core/api/src/main/java/org/onosproject/net/flowobjective/FlowObjectiveService.java
+++ b/core/api/src/main/java/org/onosproject/net/flowobjective/FlowObjectiveService.java
@@ -21,14 +21,36 @@
 import java.util.concurrent.Future;
 
 /**
- * Created by ash on 07/04/15.
+ * Service for programming data plane flow rules in manner independent of
+ * specific device table pipeline configuration.
  */
 public interface FlowObjectiveService {
 
-    Future<Boolean> filter(DeviceId deviceId, Collection<FilteringObjective> filterObjectives);
+    /**
+     * Installs the filtering rules onto the specified device.
+     *
+     * @param deviceId            device identifier
+     * @param filteringObjectives the collection of filters
+     * @return a future indicating the success of the operation
+     */
+    Future<Boolean> filter(DeviceId deviceId, Collection<FilteringObjective> filteringObjectives);
 
+    /**
+     * Installs the forwarding rules onto the specified device.
+     *
+     * @param deviceId             device identifier
+     * @param forwardingObjectives the collection of forwarding objectives
+     * @return a future indicating the success of the operation
+     */
     Future<Boolean> forward(DeviceId deviceId, Collection<ForwardingObjective> forwardingObjectives);
 
+    /**
+     * Installs the next hop elements into the specified device.
+     *
+     * @param deviceId       device identifier
+     * @param nextObjectives the collection of next objectives
+     * @return a future indicating the success of the operation
+     */
     Future<Boolean> next(DeviceId deviceId, Collection<NextObjective> nextObjectives);
 
 }
diff --git a/core/api/src/main/java/org/onosproject/net/flowobjective/package-info.java b/core/api/src/main/java/org/onosproject/net/flowobjective/package-info.java
new file mode 100644
index 0000000..65454a7
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/flowobjective/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Abstractions for objective-based flow programming of data plane without
+ * requiring device pipeline structure awareness.
+ */
+package org.onosproject.net.flowobjective;
\ No newline at end of file
diff --git a/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverDataTest.java b/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverDataTest.java
index 40e9bb1..bf4ad52 100644
--- a/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverDataTest.java
+++ b/core/api/src/test/java/org/onosproject/net/driver/DefaultDriverDataTest.java
@@ -37,7 +37,7 @@
 
     @Test
     public void basics() {
-        assertSame("incorrect type", ddc, data.type());
+        assertSame("incorrect type", ddc, data.driver());
         assertTrue("incorrect toString", data.toString().contains("foo.bar"));
     }
 
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
index dc9c6aa..9c7fddb 100644
--- a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
@@ -21,29 +21,45 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
 import org.onosproject.net.driver.Behaviour;
 import org.onosproject.net.driver.DefaultDriverData;
 import org.onosproject.net.driver.DefaultDriverHandler;
 import org.onosproject.net.driver.Driver;
 import org.onosproject.net.driver.DriverAdminService;
-import org.onosproject.net.driver.DriverData;
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 
+import static org.onlab.util.Tools.nullIsNotFound;
+import static org.onosproject.net.AnnotationKeys.DRIVER;
 
+/**
+ * Manages inventory of device drivers.
+ */
 @Component(immediate = true)
 @Service
 public class DriverManager implements DriverAdminService {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String NO_DRIVER = "Driver not found";
+    private static final String NO_DEVICE = "Device not found";
+    private static final String DEFAULT = "default";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
     private Set<DriverProvider> providers = Sets.newConcurrentHashSet();
     private Map<String, Driver> driverByName = Maps.newConcurrentMap();
     private Map<String, Driver> driverByKey = Maps.newConcurrentMap();
@@ -88,32 +104,65 @@
 
     @Override
     public Set<Driver> getDrivers(Class<? extends Behaviour>... withBehaviours) {
-        //TODO
-        return null;
+        ImmutableSet.Builder<Driver> builder = ImmutableSet.builder();
+        for (Class<? extends Behaviour> behaviour : withBehaviours) {
+            driverByName.forEach((name, driver) -> {
+                if (driver.hasBehaviour(behaviour)) {
+                    builder.add(driver);
+                }
+            });
+        }
+        return builder.build();
     }
 
     @Override
     public Driver getDriver(String driverName) {
-        //TODO: replace with fallback driver.
-        return driverByName.getOrDefault(driverName, driverByName.get("default"));
+        return nullIsNotFound(driverByName.get(driverName), NO_DRIVER);
     }
 
     @Override
     public Driver getDriver(String mfr, String hw, String sw) {
-        return driverByKey.getOrDefault(key(mfr, hw, sw), driverByName.get("default"));
+        // First attempt a literal search.
+        Driver driver = driverByKey.get(key(mfr, hw, sw));
+        if (driver != null) {
+            return driver;
+        }
+
+        // Otherwise, sweep through the key space and attempt to match using
+        // regular expression matching.
+        Optional<Driver> optional = driverByKey.values().stream()
+                .filter(d -> matches(d, mfr, hw, sw)).findFirst();
+
+        // If no matching driver is found, return default.
+        return optional.isPresent() ? optional.get() : driverByName.get(DEFAULT);
+    }
+
+    // Matches the given driver using ERE matching against the given criteria.
+    private boolean matches(Driver d, String mfr, String hw, String sw) {
+        // TODO: consider pre-compiling the expressions in the future
+        return mfr.matches(d.manufacturer()) &&
+                hw.matches(d.hwVersion()) &&
+                sw.matches(d.swVersion());
     }
 
     @Override
-    public DriverHandler createHandler(String driverName, DeviceId deviceId, String... credentials) {
-        Driver driver = driverByName.get(driverName);
+    public Driver getDriver(DeviceId deviceId) {
+        Device device = nullIsNotFound(deviceService.getDevice(deviceId), NO_DEVICE);
+        String driverName = device.annotations().value(DRIVER);
+        if (driverName != null) {
+            return getDriver(driverName);
+        }
+        return nullIsNotFound(getDriver(device.manufacturer(),
+                                        device.hwVersion(), device.swVersion()),
+                              NO_DRIVER);
+    }
+
+    @Override
+    public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
+        Driver driver = getDriver(deviceId);
         return new DefaultDriverHandler(new DefaultDriverData(driver));
     }
 
-    @Override
-    public DriverHandler createHandler(DriverData data, DeviceId deviceId, String... credentials) {
-        return null;
-    }
-
     private String key(String mfr, String hw, String sw) {
         return String.format("%s-%s-%s", mfr, hw, sw);
     }
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/driver/impl/package-info.java
new file mode 100644
index 0000000..2006391
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementation of the device driver management subsystem.
+ */
+package org.onosproject.net.driver.impl;
\ No newline at end of file
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
index 840d482..1a57ffb 100644
--- a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java
@@ -24,23 +24,26 @@
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.osgi.DefaultServiceDirectory;
 import org.onlab.osgi.ServiceDirectory;
+import org.onlab.util.ItemNotFoundException;
 import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.NodeId;
 import org.onosproject.mastership.MastershipEvent;
 import org.onosproject.mastership.MastershipListener;
 import org.onosproject.mastership.MastershipService;
-import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.Pipeliner;
+import org.onosproject.net.behaviour.PipelinerContext;
 import org.onosproject.net.device.DeviceEvent;
 import org.onosproject.net.device.DeviceListener;
 import org.onosproject.net.device.DeviceService;
-import org.onosproject.net.driver.Driver;
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.flow.FlowRuleService;
 import org.onosproject.net.flowobjective.FilteringObjective;
 import org.onosproject.net.flowobjective.FlowObjectiveService;
 import org.onosproject.net.flowobjective.ForwardingObjective;
 import org.onosproject.net.flowobjective.NextObjective;
+import org.onosproject.net.group.GroupService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,7 +54,7 @@
 import static com.google.common.base.Preconditions.checkState;
 
 /**
- * Created by ash on 07/04/15.
+ * Provides implementation of the flow objective programming service.
  */
 @Component(immediate = true)
 @Service
@@ -59,6 +62,8 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    public static final String NOT_INITIALIZED = "Driver not initialized";
+
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DriverService driverService;
 
@@ -71,20 +76,31 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected ClusterService clusterService;
 
+    // Note: The following dependencies are added on behalf of the pipeline
+    // driver behaviours to assure these services are available for their
+    // initialization.
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleService flowRuleService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected GroupService groupService;
+
+
+    private final Map<DeviceId, DriverHandler> driverHandlers = Maps.newConcurrentMap();
+
+    private final PipelinerContext context = new InnerPipelineContext();
+    private final MastershipListener mastershipListener = new InnerMastershipListener();
+    private final DeviceListener deviceListener = new InnerDeviceListener();
+
     protected ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
-
-    private MastershipListener mastershipListener = new InnerMastershipListener();
-
-    private DeviceListener deviceListener = new InnerDeviceListener();
-
-    private Map<DeviceId, DriverHandler> driverHandlers =
-            Maps.newConcurrentMap();
+    private NodeId localNode;
 
     @Activate
     protected void activate() {
+        localNode = clusterService.getLocalNode().id();
         mastershipService.addListener(mastershipListener);
         deviceService.addListener(deviceListener);
-        deviceService.getDevices().forEach(device -> setupDriver(device.id()));
+        deviceService.getDevices().forEach(device -> setupPipelineHandler(device.id()));
         log.info("Started");
     }
 
@@ -97,113 +113,90 @@
 
     @Override
     public Future<Boolean> filter(DeviceId deviceId,
-                                  Collection<FilteringObjective> filterObjectives) {
-        DriverHandler handler = driverHandlers.get(deviceId);
-        checkState(handler != null, "Driver not initialized");
-
-        Pipeliner pipe = handler.behaviour(Pipeliner.class);
-
-        return pipe.filter(filterObjectives);
+                                  Collection<FilteringObjective> filteringObjectives) {
+        return getDevicePipeliner(deviceId).filter(filteringObjectives);
     }
 
     @Override
     public Future<Boolean> forward(DeviceId deviceId,
                                    Collection<ForwardingObjective> forwardingObjectives) {
-        DriverHandler handler = driverHandlers.get(deviceId);
-        checkState(handler != null, "Driver not initialized");
-
-        Pipeliner pipe = handler.behaviour(Pipeliner.class);
-
-        return pipe.forward(forwardingObjectives);
+        return getDevicePipeliner(deviceId).forward(forwardingObjectives);
     }
 
     @Override
     public Future<Boolean> next(DeviceId deviceId,
                                 Collection<NextObjective> nextObjectives) {
+        return getDevicePipeliner(deviceId).next(nextObjectives);
+    }
+
+    // Retrieves the device handler pipeline behaviour from the cache.
+    private Pipeliner getDevicePipeliner(DeviceId deviceId) {
         DriverHandler handler = driverHandlers.get(deviceId);
-        checkState(handler != null, "Driver not initialized");
-
-        Pipeliner pipe = handler.behaviour(Pipeliner.class);
-
-        return pipe.next(nextObjectives);
+        checkState(handler != null, NOT_INITIALIZED);
+        return handler != null ? handler.behaviour(Pipeliner.class) : null;
     }
 
 
-
+    // Triggers driver setup when the local node becomes a device master.
     private class InnerMastershipListener implements MastershipListener {
         @Override
         public void event(MastershipEvent event) {
             switch (event.type()) {
-
                 case MASTER_CHANGED:
-                    setupDriver(event.subject());
-
-                    break;
-                case BACKUPS_CHANGED:
+                    setupPipelineHandler(event.subject());
                     break;
                 default:
-                    log.warn("Unknown mastership type {}", event.type());
+                    break;
             }
         }
-
-
     }
 
+    // Triggers driver setup when a device is (re)detected.
     private class InnerDeviceListener implements DeviceListener {
         @Override
         public void event(DeviceEvent event) {
             switch (event.type()) {
                 case DEVICE_ADDED:
                 case DEVICE_AVAILABILITY_CHANGED:
-                    setupDriver(event.subject().id());
-                    break;
-                case DEVICE_UPDATED:
-                    break;
-                case DEVICE_REMOVED:
-                    break;
-                case DEVICE_SUSPENDED:
-                    break;
-                case PORT_ADDED:
-                    break;
-                case PORT_UPDATED:
-                    break;
-                case PORT_REMOVED:
+                    setupPipelineHandler(event.subject().id());
                     break;
                 default:
-                    log.warn("Unknown event type {}", event.type());
+                    break;
             }
         }
     }
 
-    private void setupDriver(DeviceId deviceId) {
-        //TODO: Refactor this to make it nicer and use a cache.
-        if (mastershipService.getMasterFor(
-                deviceId).equals(clusterService.getLocalNode().id())) {
-
-            DriverHandler handler = lookupDriver(deviceId);
-            if (handler != null) {
-                Pipeliner pipe = handler.behaviour(Pipeliner.class);
-                pipe.init(deviceId, serviceDirectory);
+    private void setupPipelineHandler(DeviceId deviceId) {
+        if (localNode.equals(mastershipService.getMasterFor(deviceId))) {
+            // Attempt to lookup the handler in the cache
+            DriverHandler handler = driverHandlers.get(deviceId);
+            if (handler == null) {
+                try {
+                    // Otherwise create it and if it has pipeline behaviour, cache it
+                    handler = driverService.createHandler(deviceId);
+                    if (!handler.driver().hasBehaviour(Pipeliner.class)) {
+                        log.warn("Pipeline behaviour not supported for device {}",
+                                 deviceId);
+                        return;
+                    }
+                } catch (ItemNotFoundException e) {
+                    log.warn("No applicable driver for device {}", deviceId);
+                    return;
+                }
                 driverHandlers.put(deviceId, handler);
-                log.info("Driver {} bound to device {}",
-                         handler.data().type().name(), deviceId);
-            } else {
-                log.error("No driver for device {}", deviceId);
             }
+
+            // Always (re)initialize the pipeline behaviour
+            handler.behaviour(Pipeliner.class).init(deviceId, context);
+            log.info("Driver {} bound to device {}", handler.driver().name(), deviceId);
         }
     }
 
-
-    private DriverHandler lookupDriver(DeviceId deviceId) {
-        Device device = deviceService.getDevice(deviceId);
-        if (device == null) {
-            log.warn("Device is null!");
-            return null;
+    // Processing context for initializing pipeline driver behaviours.
+    private class InnerPipelineContext implements PipelinerContext {
+        @Override
+        public ServiceDirectory directory() {
+            return serviceDirectory;
         }
-        Driver driver = driverService.getDriver(device.manufacturer(),
-                                                device.hwVersion(), device.swVersion());
-
-        return driverService.createHandler(driver.name(), deviceId);
     }
-
 }
diff --git a/core/net/src/main/java/org/onosproject/net/flowobjective/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/package-info.java
new file mode 100644
index 0000000..c0779dc
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/flowobjective/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementations of the flow objective programming subsystem.
+ */
+package org.onosproject.net.flowobjective.impl;
\ No newline at end of file
diff --git a/docs/pom.xml b/docs/pom.xml
index 8d3563f..050362f 100644
--- a/docs/pom.xml
+++ b/docs/pom.xml
@@ -61,7 +61,7 @@
                         <group>
                             <title>Core Subsystems</title>
                             <packages>
-                                org.onosproject.impl:org.onosproject.core.impl:org.onosproject.cluster.impl:org.onosproject.net.device.impl:org.onosproject.net.link.impl:org.onosproject.net.host.impl:org.onosproject.net.topology.impl:org.onosproject.net.packet.impl:org.onosproject.net.flow.impl:org.onosproject.net.*.impl:org.onosproject.event.impl:org.onosproject.net.intent.impl*:org.onosproject.net.proxyarp.impl:org.onosproject.mastership.impl:org.onosproject.net.resource.impl:org.onosproject.json:org.onosproject.json.*:org.onosproject.provider.host.impl:org.onosproject.provider.lldp.impl:org.onosproject.net.statistic.impl:org.onosproject.app.impl:org.onosproject.common.*:org.onosproject.net.group.impl:org.onosproject.cfg.impl
+                                org.onosproject.impl:org.onosproject.core.impl:org.onosproject.cluster.impl:org.onosproject.net.device.impl:org.onosproject.net.link.impl:org.onosproject.net.host.impl:org.onosproject.net.topology.impl:org.onosproject.net.packet.impl:org.onosproject.net.flow.impl:org.onosproject.net.*.impl:org.onosproject.event.impl:org.onosproject.net.intent.impl*:org.onosproject.net.proxyarp.impl:org.onosproject.mastership.impl:org.onosproject.net.resource.impl:org.onosproject.json:org.onosproject.json.*:org.onosproject.provider.host.impl:org.onosproject.provider.lldp.impl:org.onosproject.net.statistic.impl:org.onosproject.app.impl:org.onosproject.common.*:org.onosproject.net.group.impl:org.onosproject.cfg.impl:org.onosproject.net.driver.impl:org.onosproject.net.flowobjective.impl:org.onosproject.net.flowext.impl
                             </packages>
                         </group>
                         <group>
@@ -83,6 +83,12 @@
                             </packages>
                         </group>
                         <group>
+                            <title>Built-in Device Drivers</title>
+                            <packages>
+                                org.onosproject.driver.*
+                            </packages>
+                        </group>
+                        <group>
                             <title>Utilities</title>
                             <packages>
                                 org.onlab.*
diff --git a/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultDrivers.java b/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultDrivers.java
index e42e346..580691b 100644
--- a/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultDrivers.java
+++ b/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultDrivers.java
@@ -27,6 +27,7 @@
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.io.InputStream;
 
 /**
  * Bootstrap for built in drivers.
@@ -36,6 +37,8 @@
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
+    private static final String DRIVERS_XML = "/onos-drivers.xml";
+
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DriverAdminService driverService;
 
@@ -43,25 +46,23 @@
 
     @Activate
     protected void activate() {
-        XmlDriverLoader xmlDriverLoader =
-                new XmlDriverLoader(getClass().getClassLoader());
+        ClassLoader classLoader = getClass().getClassLoader();
         try {
-            provider = xmlDriverLoader.loadDrivers(
-                    getClass().getResourceAsStream("/default.xml"));
-            driverService.registerProvider(
-                    provider);
+            InputStream stream = classLoader.getResourceAsStream(DRIVERS_XML);
+            provider = new XmlDriverLoader(classLoader).loadDrivers(stream);
+            driverService.registerProvider(provider);
         } catch (IOException e) {
-            log.warn("Unable to load drivers");
+            log.error("Unable to load default drivers", e);
         }
-
         log.info("Started");
     }
 
     @Deactivate
     protected void deactivate() {
-        driverService.unregisterProvider(provider);
+        if (provider != null) {
+            driverService.unregisterProvider(provider);
+        }
         log.info("Stopped");
     }
 
-
 }
diff --git a/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java b/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java
index 48114ab..6e99b35 100644
--- a/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java
+++ b/drivers/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java
@@ -20,6 +20,7 @@
 import org.onosproject.core.DefaultGroupId;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.Pipeliner;
+import org.onosproject.net.behaviour.PipelinerContext;
 import org.onosproject.net.driver.DriverData;
 import org.onosproject.net.flow.DefaultFlowRule;
 import org.onosproject.net.flow.FlowRule;
@@ -49,8 +50,8 @@
     private DeviceId deviceId;
 
     @Override
-    public void init(DeviceId deviceId, ServiceDirectory serviceDirectory) {
-        this.serviceDirectory = serviceDirectory;
+    public void init(DeviceId deviceId, PipelinerContext context) {
+        this.serviceDirectory = context.directory();
         this.deviceId = deviceId;
 
         flowRuleService = serviceDirectory.get(FlowRuleService.class);
diff --git a/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java b/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
index 84dd5db..2ac846c 100644
--- a/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
+++ b/drivers/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
@@ -23,6 +23,7 @@
 import org.onosproject.core.CoreService;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.behaviour.Pipeliner;
+import org.onosproject.net.behaviour.PipelinerContext;
 import org.onosproject.net.driver.DriverData;
 import org.onosproject.net.flow.DefaultFlowRule;
 import org.onosproject.net.flow.DefaultTrafficSelector;
@@ -61,8 +62,8 @@
     private ApplicationId appId;
 
     @Override
-    public void init(DeviceId deviceId, ServiceDirectory serviceDirectory) {
-        this.serviceDirectory = serviceDirectory;
+    public void init(DeviceId deviceId, PipelinerContext context) {
+        this.serviceDirectory = context.directory();
         this.deviceId = deviceId;
 
 
diff --git a/drivers/src/main/java/org/onosproject/driver/pipeline/package-info.java b/drivers/src/main/java/org/onosproject/driver/pipeline/package-info.java
new file mode 100644
index 0000000..880acf5
--- /dev/null
+++ b/drivers/src/main/java/org/onosproject/driver/pipeline/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Implementations of the pipeline driver behaviours.
+ */
+package org.onosproject.driver.pipeline;
\ No newline at end of file
diff --git a/drivers/src/main/resources/default.xml b/drivers/src/main/resources/onos-drivers.xml
similarity index 100%
rename from drivers/src/main/resources/default.xml
rename to drivers/src/main/resources/onos-drivers.xml
diff --git a/utils/misc/src/main/java/org/onlab/util/Tools.java b/utils/misc/src/main/java/org/onlab/util/Tools.java
index b788038..01dda9e 100644
--- a/utils/misc/src/main/java/org/onlab/util/Tools.java
+++ b/utils/misc/src/main/java/org/onlab/util/Tools.java
@@ -112,6 +112,23 @@
     }
 
     /**
+     * Returns the specified item if that items is null; otherwise throws
+     * not found exception.
+     *
+     * @param item    item to check
+     * @param message not found message
+     * @param <T>     item type
+     * @return item if not null
+     * @throws org.onlab.util.ItemNotFoundException if item is null
+     */
+    public static <T> T nullIsNotFound(T item, String message) {
+        if (item == null) {
+            throw new ItemNotFoundException(message);
+        }
+        return item;
+    }
+
+    /**
      * Converts a string from hex to long.
      *
      * @param string hex number in string form; sans 0x